From da95fe6b1e6994b3aaa184d08f95afdb85cfd536 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Mon, 1 Oct 2018 10:11:44 -0400 Subject: [PATCH] Rust side render pass begin/end --- examples/hello_triangle_rust/main.rs | 21 +++++++++++- wgpu-native/src/command/mod.rs | 16 ++++----- wgpu-native/src/lib.rs | 12 +++++++ wgpu-rs/src/lib.rs | 51 ++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 9 deletions(-) diff --git a/examples/hello_triangle_rust/main.rs b/examples/hello_triangle_rust/main.rs index 519e176406..a00d6b8e11 100644 --- a/examples/hello_triangle_rust/main.rs +++ b/examples/hello_triangle_rust/main.rs @@ -9,6 +9,7 @@ fn main() { anisotropic_filtering: false, }, }); + let vs_bytes = include_bytes!("./../data/hello_triangle.vert.spv"); let vs_module = device.create_shader_module(vs_bytes); let fs_bytes = include_bytes!("./../data/hello_triangle.frag.spv"); @@ -49,7 +50,25 @@ fn main() { attachment_state: &attachment_state, }); - let cmd_buf = device.create_command_buffer(wgpu::CommandBufferDescriptor {}); + let mut cmd_buf = device.create_command_buffer(wgpu::CommandBufferDescriptor {}); + + { + let color_view = unimplemented!(); //TODO! + let rpass = cmd_buf.begin_render_pass(wgpu::RenderPassDescriptor { + color_attachments: &[ + wgpu::RenderPassColorAttachmentDescriptor { + attachment: &color_view, + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_color: wgpu::Color::GREEN, + }, + ], + depth_stencil_attachment: None, + }); + rpass.end_pass(); + } + + let queue = device.get_queue(); queue.submit(&[cmd_buf]); } diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index 1d17809509..c40d0279bd 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -30,16 +30,16 @@ pub enum StoreOp { } #[repr(C)] -pub struct RenderPassColorAttachmentDescriptor { - pub attachment: TextureViewId, +pub struct RenderPassColorAttachmentDescriptor { + pub attachment: T, pub load_op: LoadOp, pub store_op: StoreOp, pub clear_color: Color, } #[repr(C)] -pub struct RenderPassDepthStencilAttachmentDescriptor { - pub attachment: TextureViewId, +pub struct RenderPassDepthStencilAttachmentDescriptor { + pub attachment: T, pub depth_load_op: LoadOp, pub depth_store_op: StoreOp, pub clear_depth: f32, @@ -49,9 +49,9 @@ pub struct RenderPassDepthStencilAttachmentDescriptor { } #[repr(C)] -pub struct RenderPassDescriptor<'a> { - pub color_attachments: &'a [RenderPassColorAttachmentDescriptor], - pub depth_stencil_attachment: RenderPassDepthStencilAttachmentDescriptor, +pub struct RenderPassDescriptor<'a, T: 'a> { + pub color_attachments: &'a [RenderPassColorAttachmentDescriptor], + pub depth_stencil_attachment: Option>, } #[repr(C)] @@ -83,7 +83,7 @@ pub struct CommandBufferDescriptor {} #[no_mangle] pub extern "C" fn wgpu_command_buffer_begin_render_pass( command_buffer_id: CommandBufferId, - _descriptor: RenderPassDescriptor, + _descriptor: RenderPassDescriptor, ) -> RenderPassId { let raw = registry::COMMAND_BUFFER_REGISTRY .lock() diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index f150bd5649..9fe0a8eb51 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -41,6 +41,7 @@ use back::Backend as B; use registry::Id; #[repr(C)] +#[derive(Clone, Copy, Debug)] pub struct Color { pub r: f32, pub g: f32, @@ -48,7 +49,17 @@ pub struct Color { pub a: f32, } +impl Color { + pub const TRANSPARENT : Self = Color { r: 0.0, g: 0.0, b: 0.0, a: 0.0 }; + pub const BLACK : Self = Color { r: 0.0, g: 0.0, b: 0.0, a: 1.0 }; + pub const WHITE : Self = Color { r: 1.0, g: 1.0, b: 1.0, a: 1.0 }; + pub const RED : Self = Color { r: 1.0, g: 0.0, b: 0.0, a: 1.0 }; + pub const GREEN : Self = Color { r: 0.0, g: 1.0, b: 0.0, a: 1.0 }; + pub const BLUE : Self = Color { r: 0.0, g: 0.0, b: 1.0, a: 1.0 }; +} + #[repr(C)] +#[derive(Clone, Copy, Debug)] pub struct Origin3d { pub x: f32, pub y: f32, @@ -56,6 +67,7 @@ pub struct Origin3d { } #[repr(C)] +#[derive(Clone, Copy, Debug)] pub struct Extent3d { pub width: f32, pub height: f32, diff --git a/wgpu-rs/src/lib.rs b/wgpu-rs/src/lib.rs index 32e14cbab0..630074918e 100644 --- a/wgpu-rs/src/lib.rs +++ b/wgpu-rs/src/lib.rs @@ -10,6 +10,8 @@ pub use wgn::{ Origin3d, PowerPreference, ShaderModuleDescriptor, ShaderStage, BindGroupLayoutBinding, TextureFormat, PrimitiveTopology, BlendStateDescriptor, ColorWriteFlags, DepthStencilStateDescriptor, + RenderPassDescriptor, RenderPassColorAttachmentDescriptor, RenderPassDepthStencilAttachmentDescriptor, + LoadOp, StoreOp, }; @@ -25,6 +27,10 @@ pub struct Device { id: wgn::DeviceId, } +pub struct TextureView { + id: wgn::TextureViewId, +} + pub struct BindGroupLayout { id: wgn::BindGroupLayoutId, } @@ -57,6 +63,11 @@ pub struct CommandBuffer { id: wgn::CommandBufferId, } +pub struct RenderPass<'a> { + id: wgn::RenderPassId, + parent: &'a mut CommandBuffer, +} + pub struct Queue { id: wgn::QueueId, } @@ -205,6 +216,46 @@ impl Device { } } +impl CommandBuffer { + pub fn begin_render_pass(&mut self, desc: RenderPassDescriptor<&TextureView>) -> RenderPass { + let colors = desc.color_attachments + .iter() + .map(|ca| RenderPassColorAttachmentDescriptor { + attachment: ca.attachment.id, + load_op: ca.load_op, + store_op: ca.store_op, + clear_color: ca.clear_color, + }) + .collect::>(); + + let depth_stencil = desc.depth_stencil_attachment + .map(|dsa| RenderPassDepthStencilAttachmentDescriptor { + attachment: dsa.attachment.id, + depth_load_op: dsa.depth_load_op, + depth_store_op: dsa.depth_store_op, + clear_depth: dsa.clear_depth, + stencil_load_op: dsa.stencil_load_op, + stencil_store_op: dsa.stencil_store_op, + clear_stencil: dsa.clear_stencil, + }); + + RenderPass { + id: wgn::wgpu_command_buffer_begin_render_pass(self.id, RenderPassDescriptor { + color_attachments: &colors, + depth_stencil_attachment: depth_stencil, + }), + parent: self, + } + } +} + +impl<'a> RenderPass<'a> { + pub fn end_pass(self) -> &'a mut CommandBuffer { + wgn::wgpu_render_pass_end_pass(self.id); + self.parent + } +} + impl Queue { pub fn submit(&self, command_buffers: &[CommandBuffer]) { wgn::wgpu_queue_submit(