Skip to content
This repository has been archived by the owner on Jun 18, 2021. It is now read-only.

Commit

Permalink
Update to wgpu version with raw passes
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jan 12, 2020
1 parent eb64359 commit 87650d9
Showing 1 changed file with 120 additions and 66 deletions.
186 changes: 120 additions & 66 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pub use wgc::{
CommandBufferDescriptor,
CommandEncoderDescriptor,
LoadOp,
RenderPassDepthStencilAttachmentDescriptor,
StoreOp,
},
device::{
Expand Down Expand Up @@ -410,6 +409,11 @@ pub struct ComputePipelineDescriptor<'a> {
pub compute_stage: ProgrammableStageDescriptor<'a>,
}

pub type RenderPassColorAttachmentDescriptor<'a> =
wgc::command::RenderPassColorAttachmentDescriptorBase<&'a TextureView, Option<&'a TextureView>>;
pub type RenderPassDepthStencilAttachmentDescriptor<'a> =
wgc::command::RenderPassDepthStencilAttachmentDescriptorBase<&'a TextureView>;

/// A description of all the attachments of a render pass.
#[derive(Debug)]
pub struct RenderPassDescriptor<'a> {
Expand All @@ -418,26 +422,7 @@ pub struct RenderPassDescriptor<'a> {

/// The depth and stencil attachment of the render pass, if any.
pub depth_stencil_attachment:
Option<RenderPassDepthStencilAttachmentDescriptor<&'a TextureView>>,
}

/// A description of a color attachment.
#[derive(Clone, Debug)]
pub struct RenderPassColorAttachmentDescriptor<'a> {
/// The actual color attachment.
pub attachment: &'a TextureView,

/// The resolve target for this color attachment, if any.
pub resolve_target: Option<&'a TextureView>,

/// The beginning-of-pass load operation for this color attachment.
pub load_op: LoadOp,

/// The end-of-pass store operation for this color attachment.
pub store_op: StoreOp,

/// The color that will be assigned to every pixel of this attachment when cleared.
pub clear_color: Color,
Option<RenderPassDepthStencilAttachmentDescriptor<'a>>,
}

/// A swap chain image that can be rendered to.
Expand Down Expand Up @@ -1092,15 +1077,15 @@ impl CommandEncoder {
.iter()
.map(|ca| wgc::command::RenderPassColorAttachmentDescriptor {
attachment: ca.attachment.id,
resolve_target: ca.resolve_target.map_or(ptr::null(), |v| &v.id as *const _),
resolve_target: ca.resolve_target.map(|rt| &rt.id),
load_op: ca.load_op,
store_op: ca.store_op,
clear_color: ca.clear_color,
})
.collect::<ArrayVec<[_; 4]>>();

let depth_stencil = desc.depth_stencil_attachment.as_ref().map(|dsa| {
RenderPassDepthStencilAttachmentDescriptor {
wgc::command::RenderPassDepthStencilAttachmentDescriptor {
attachment: dsa.attachment.id,
depth_load_op: dsa.depth_load_op,
depth_store_op: dsa.depth_store_op,
Expand All @@ -1112,17 +1097,16 @@ impl CommandEncoder {
});

RenderPass {
id: wgn::wgpu_command_encoder_begin_render_pass(
self.id,
&wgc::command::RenderPassDescriptor {
color_attachments: colors.as_ptr(),
color_attachments_length: colors.len(),
depth_stencil_attachment: depth_stencil
.as_ref()
.map(|at| at as *const _)
.unwrap_or(ptr::null()),
},
),
id: unsafe {
wgn::wgpu_command_encoder_begin_render_pass(
self.id,
&wgc::command::RenderPassDescriptor {
color_attachments: colors.as_ptr(),
color_attachments_length: colors.len(),
depth_stencil_attachment: depth_stencil.as_ref(),
},
)
},
_parent: self,
}
}
Expand All @@ -1132,7 +1116,9 @@ impl CommandEncoder {
/// This function returns a [`ComputePass`] object which records a single compute pass.
pub fn begin_compute_pass(&mut self) -> ComputePass {
ComputePass {
id: wgn::wgpu_command_encoder_begin_compute_pass(self.id, None),
id: unsafe {
wgn::wgpu_command_encoder_begin_compute_pass(self.id, None)
},
_parent: self,
}
}
Expand Down Expand Up @@ -1212,7 +1198,7 @@ impl<'a> RenderPass<'a> {
) {
unsafe {
wgn::wgpu_render_pass_set_bind_group(
self.id,
self.id.as_mut().unwrap(),
index,
bind_group.id,
offsets.as_ptr(),
Expand All @@ -1225,19 +1211,35 @@ impl<'a> RenderPass<'a> {
///
/// Subsequent draw calls will exhibit the behavior defined by `pipeline`.
pub fn set_pipeline(&mut self, pipeline: &RenderPipeline) {
wgn::wgpu_render_pass_set_pipeline(self.id, pipeline.id);
unsafe {
wgn::wgpu_render_pass_set_pipeline(
self.id.as_mut().unwrap(),
pipeline.id,
);
}
}

pub fn set_blend_color(&mut self, color: Color) {
wgn::wgpu_render_pass_set_blend_color(self.id, &color);
unsafe {
wgn::wgpu_render_pass_set_blend_color(
self.id.as_mut().unwrap(),
&color,
);
}
}

/// Sets the active index buffer.
///
/// Subsequent calls to [`draw_indexed`](RenderPass::draw_indexed) on this [`RenderPass`] will
/// use `buffer` as the source index buffer.
pub fn set_index_buffer(&mut self, buffer: &Buffer, offset: BufferAddress) {
wgn::wgpu_render_pass_set_index_buffer(self.id, buffer.id, offset);
unsafe {
wgn::wgpu_render_pass_set_index_buffer(
self.id.as_mut().unwrap(),
buffer.id,
offset,
);
}
}

/// Sets the active vertex buffers, starting from `start_slot`.
Expand All @@ -1257,7 +1259,7 @@ impl<'a> RenderPass<'a> {
}
unsafe {
wgn::wgpu_render_pass_set_vertex_buffers(
self.id,
self.id.as_mut().unwrap(),
start_slot,
buffers.as_ptr(),
offsets.as_ptr(),
Expand All @@ -1270,56 +1272,82 @@ impl<'a> RenderPass<'a> {
///
/// Subsequent draw calls will discard any fragments that fall outside this region.
pub fn set_scissor_rect(&mut self, x: u32, y: u32, w: u32, h: u32) {
wgn::wgpu_render_pass_set_scissor_rect(self.id, x, y, w, h)
unsafe {
wgn::wgpu_render_pass_set_scissor_rect(
self.id.as_mut().unwrap(),
x, y, w, h,
);
}
}

/// Sets the viewport region.
///
/// Subsequent draw calls will draw any fragments in this region.
pub fn set_viewport(&mut self, x: f32, y: f32, w: f32, h: f32, min_depth: f32, max_depth: f32) {
wgn::wgpu_render_pass_set_viewport(self.id, x, y, w, h, min_depth, max_depth)
unsafe {
wgn::wgpu_render_pass_set_viewport(
self.id.as_mut().unwrap(),
x, y, w, h,
min_depth, max_depth,
);
}
}

/// Sets the stencil reference.
///
/// Subsequent stencil tests will test against this value.
pub fn set_stencil_reference(&mut self, reference: u32) {
wgn::wgpu_render_pass_set_stencil_reference(self.id, reference)
unsafe {
wgn::wgpu_render_pass_set_stencil_reference(
self.id.as_mut().unwrap(),
reference,
);
}
}

/// Draws primitives from the active vertex buffer(s).
///
/// The active vertex buffers can be set with [`RenderPass::set_vertex_buffers`].
pub fn draw(&mut self, vertices: Range<u32>, instances: Range<u32>) {
wgn::wgpu_render_pass_draw(
self.id,
vertices.end - vertices.start,
instances.end - instances.start,
vertices.start,
instances.start,
);
unsafe {
wgn::wgpu_render_pass_draw(
self.id.as_mut().unwrap(),
vertices.end - vertices.start,
instances.end - instances.start,
vertices.start,
instances.start,
);
}
}

/// Draws indexed primitives using the active index buffer and the active vertex buffers.
///
/// The active index buffer can be set with [`RenderPass::set_index_buffer`], while the active
/// vertex buffers can be set with [`RenderPass::set_vertex_buffers`].
pub fn draw_indexed(&mut self, indices: Range<u32>, base_vertex: i32, instances: Range<u32>) {
wgn::wgpu_render_pass_draw_indexed(
self.id,
indices.end - indices.start,
instances.end - instances.start,
indices.start,
base_vertex,
instances.start,
);
unsafe {
wgn::wgpu_render_pass_draw_indexed(
self.id.as_mut().unwrap(),
indices.end - indices.start,
instances.end - instances.start,
indices.start,
base_vertex,
instances.start,
);
}
}

/// Draws primitives from the active vertex buffer(s) based on the contents of the `indirect_buffer`.
///
/// The active vertex buffers can be set with [`RenderPass::set_vertex_buffers`].
pub fn draw_indirect(&mut self, indirect_buffer: &Buffer, indirect_offset: BufferAddress) {
wgn::wgpu_render_pass_draw_indirect(self.id, indirect_buffer.id, indirect_offset);
unsafe {
wgn::wgpu_render_pass_draw_indirect(
self.id.as_mut().unwrap(),
indirect_buffer.id,
indirect_offset,
);
}
}

/// Draws indexed primitives using the active index buffer and the active vertex buffers,
Expand All @@ -1332,14 +1360,22 @@ impl<'a> RenderPass<'a> {
indirect_buffer: &Buffer,
indirect_offset: BufferAddress,
) {
wgn::wgpu_render_pass_draw_indexed_indirect(self.id, indirect_buffer.id, indirect_offset);
unsafe {
wgn::wgpu_render_pass_draw_indexed_indirect(
self.id.as_mut().unwrap(),
indirect_buffer.id,
indirect_offset,
);
}
}
}

impl<'a> Drop for RenderPass<'a> {
fn drop(&mut self) {
if !thread::panicking() {
wgn::wgpu_render_pass_end_pass(self.id);
unsafe {
wgn::wgpu_render_pass_end_pass(self.id);
}
}
}
}
Expand All @@ -1354,7 +1390,7 @@ impl<'a> ComputePass<'a> {
) {
unsafe {
wgn::wgpu_compute_pass_set_bind_group(
self.id,
self.id.as_mut().unwrap(),
index,
bind_group.id,
offsets.as_ptr(),
Expand All @@ -1365,26 +1401,44 @@ impl<'a> ComputePass<'a> {

/// Sets the active compute pipeline.
pub fn set_pipeline(&mut self, pipeline: &ComputePipeline) {
wgn::wgpu_compute_pass_set_pipeline(self.id, pipeline.id);
unsafe {
wgn::wgpu_compute_pass_set_pipeline(
self.id.as_mut().unwrap(),
pipeline.id,
);
}
}

/// Dispatches compute work operations.
///
/// `x`, `y` and `z` denote the number of work groups to dispatch in each dimension.
pub fn dispatch(&mut self, x: u32, y: u32, z: u32) {
wgn::wgpu_compute_pass_dispatch(self.id, x, y, z);
unsafe {
wgn::wgpu_compute_pass_dispatch(
self.id.as_mut().unwrap(),
x, y, z,
);
}
}

/// Dispatches compute work operations, based on the contents of the `indirect_buffer`.
pub fn dispatch_indirect(&mut self, indirect_buffer: &Buffer, indirect_offset: BufferAddress) {
wgn::wgpu_compute_pass_dispatch_indirect(self.id, indirect_buffer.id, indirect_offset);
unsafe {
wgn::wgpu_compute_pass_dispatch_indirect(
self.id.as_mut().unwrap(),
indirect_buffer.id,
indirect_offset,
);
}
}
}

impl<'a> Drop for ComputePass<'a> {
fn drop(&mut self) {
if !thread::panicking() {
wgn::wgpu_compute_pass_end_pass(self.id);
unsafe {
wgn::wgpu_compute_pass_end_pass(self.id);
}
}
}
}
Expand Down

0 comments on commit 87650d9

Please sign in to comment.