From f8d6ab273b3b467fa2e680741f81d8dece56792e Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Sat, 20 Jun 2020 01:11:43 -0400 Subject: [PATCH 1/2] [rs] Update wgpu without peek-poke --- wgpu/Cargo.toml | 7 +- wgpu/examples/boids/main.rs | 21 ++- wgpu/examples/capture/main.rs | 9 +- wgpu/examples/cube/main.rs | 19 +-- wgpu/examples/hello-compute/main.rs | 2 +- wgpu/examples/hello-triangle/main.rs | 9 +- wgpu/examples/mipmap/main.rs | 28 ++-- wgpu/examples/msaa-line/main.rs | 14 +- wgpu/examples/shadow/main.rs | 73 +++++----- wgpu/examples/skybox/main.rs | 17 ++- wgpu/examples/texture-arrays/main.rs | 12 +- wgpu/examples/water/main.rs | 118 ++++++++-------- wgpu/src/backend/direct.rs | 199 +++++++++++---------------- wgpu/src/backend/web.rs | 43 +++--- wgpu/src/lib.rs | 74 ++++------ 15 files changed, 314 insertions(+), 331 deletions(-) diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 6ce0039e46..ae7078cc35 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -28,14 +28,14 @@ vulkan = ["wgc/gfx-backend-vulkan"] package = "wgpu-core" version = "0.5" git = "https://github.com/gfx-rs/wgpu" -rev = "a02a56684114da702a64e30af49d0167e273402b" +rev = "c7be94047d156a3bde88d03cd5c229bedb6efe62" features = ["raw-window-handle"] [dependencies.wgt] package = "wgpu-types" version = "0.5" git = "https://github.com/gfx-rs/wgpu" -rev = "a02a56684114da702a64e30af49d0167e273402b" +rev = "c7be94047d156a3bde88d03cd5c229bedb6efe62" [dependencies] arrayvec = "0.5" @@ -68,6 +68,9 @@ test = true #wgpu-types = { version = "0.5.0", path = "../wgpu/wgpu-types" } #wgpu-core = { version = "0.5.0", path = "../wgpu/wgpu-core" } +[patch."https://github.com/gfx-rs/naga"] +#naga = { path = "../naga" } + [patch.crates-io] #gfx-hal = { version = "0.5.0", path = "../gfx/src/hal" } #gfx-backend-empty = { version = "0.5.0", path = "../gfx/src/backend/empty" } diff --git a/wgpu/examples/boids/main.rs b/wgpu/examples/boids/main.rs index 460ac5215a..ee0c954b61 100644 --- a/wgpu/examples/boids/main.rs +++ b/wgpu/examples/boids/main.rs @@ -65,9 +65,7 @@ impl framework::Example for Example { wgpu::ShaderStage::COMPUTE, wgpu::BindingType::UniformBuffer { dynamic: false, - min_binding_size: wgpu::NonZeroBufferAddress::new( - sim_param_data.len() as _ - ), + min_binding_size: wgpu::BufferSize::new(sim_param_data.len() as _), }, ), wgpu::BindGroupLayoutEntry::new( @@ -75,9 +73,7 @@ impl framework::Example for Example { wgpu::ShaderStage::COMPUTE, wgpu::BindingType::StorageBuffer { dynamic: false, - min_binding_size: wgpu::NonZeroBufferAddress::new( - (NUM_PARTICLES * 16) as _, - ), + min_binding_size: wgpu::BufferSize::new((NUM_PARTICLES * 16) as _), readonly: false, }, ), @@ -86,9 +82,7 @@ impl framework::Example for Example { wgpu::ShaderStage::COMPUTE, wgpu::BindingType::StorageBuffer { dynamic: false, - min_binding_size: wgpu::NonZeroBufferAddress::new( - (NUM_PARTICLES * 16) as _, - ), + min_binding_size: wgpu::BufferSize::new((NUM_PARTICLES * 16) as _), readonly: false, }, ), @@ -269,9 +263,12 @@ impl framework::Example for Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color::BLACK, + channel: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_value: wgpu::Color::BLACK, + read_only: false, + }, }], depth_stencil_attachment: None, }; diff --git a/wgpu/examples/capture/main.rs b/wgpu/examples/capture/main.rs index 09c8d035ca..136ce424ac 100644 --- a/wgpu/examples/capture/main.rs +++ b/wgpu/examples/capture/main.rs @@ -86,9 +86,12 @@ async fn run() { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &texture.create_default_view(), resolve_target: None, - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color::RED, + channel: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_value: wgpu::Color::RED, + read_only: false, + }, }], depth_stencil_attachment: None, }); diff --git a/wgpu/examples/cube/main.rs b/wgpu/examples/cube/main.rs index cb02ecbf75..e5ac3b9cd7 100644 --- a/wgpu/examples/cube/main.rs +++ b/wgpu/examples/cube/main.rs @@ -140,7 +140,7 @@ impl framework::Example for Example { wgpu::ShaderStage::VERTEX, wgpu::BindingType::UniformBuffer { dynamic: false, - min_binding_size: wgpu::NonZeroBufferAddress::new(64), + min_binding_size: wgpu::BufferSize::new(64), }, ), wgpu::BindGroupLayoutEntry::new( @@ -326,13 +326,16 @@ impl framework::Example for Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color { - r: 0.1, - g: 0.2, - b: 0.3, - a: 1.0, + channel: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_value: wgpu::Color { + r: 0.1, + g: 0.2, + b: 0.3, + a: 1.0, + }, + read_only: false, }, }], depth_stencil_attachment: None, diff --git a/wgpu/examples/hello-compute/main.rs b/wgpu/examples/hello-compute/main.rs index b37ef2f978..699d956002 100644 --- a/wgpu/examples/hello-compute/main.rs +++ b/wgpu/examples/hello-compute/main.rs @@ -68,7 +68,7 @@ async fn execute_gpu(numbers: Vec) -> Vec { wgpu::BindingType::StorageBuffer { dynamic: false, readonly: false, - min_binding_size: wgpu::NonZeroBufferAddress::new(4), + min_binding_size: wgpu::BufferSize::new(4), }, )], }); diff --git a/wgpu/examples/hello-triangle/main.rs b/wgpu/examples/hello-triangle/main.rs index f6febdb682..478ea6ca14 100644 --- a/wgpu/examples/hello-triangle/main.rs +++ b/wgpu/examples/hello-triangle/main.rs @@ -114,9 +114,12 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu:: color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color::GREEN, + channel: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_value: wgpu::Color::GREEN, + read_only: false, + }, }], depth_stencil_attachment: None, }); diff --git a/wgpu/examples/mipmap/main.rs b/wgpu/examples/mipmap/main.rs index e57af3c387..cce3d25898 100644 --- a/wgpu/examples/mipmap/main.rs +++ b/wgpu/examples/mipmap/main.rs @@ -186,9 +186,12 @@ impl Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &views[target_mip], resolve_target: None, - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color::WHITE, + channel: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_value: wgpu::Color::WHITE, + read_only: false, + }, }], depth_stencil_attachment: None, }); @@ -226,7 +229,7 @@ impl framework::Example for Example { wgpu::ShaderStage::VERTEX, wgpu::BindingType::UniformBuffer { dynamic: false, - min_binding_size: wgpu::NonZeroBufferAddress::new(64), + min_binding_size: wgpu::BufferSize::new(64), }, ), wgpu::BindGroupLayoutEntry::new( @@ -412,13 +415,16 @@ impl framework::Example for Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color { - r: 0.1, - g: 0.2, - b: 0.3, - a: 1.0, + channel: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_value: wgpu::Color { + r: 0.1, + g: 0.2, + b: 0.3, + a: 1.0, + }, + read_only: false, }, }], depth_stencil_attachment: None, diff --git a/wgpu/examples/msaa-line/main.rs b/wgpu/examples/msaa-line/main.rs index 19d9c57d78..e0728ea63e 100644 --- a/wgpu/examples/msaa-line/main.rs +++ b/wgpu/examples/msaa-line/main.rs @@ -255,21 +255,23 @@ impl framework::Example for Example { let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); { + let channel = wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_value: wgpu::Color::BLACK, + read_only: false, + }; let rpass_color_attachment = if self.sample_count == 1 { wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color::BLACK, + channel, } } else { wgpu::RenderPassColorAttachmentDescriptor { attachment: &self.multisampled_framebuffer, resolve_target: Some(&frame.view), - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color::BLACK, + channel, } }; diff --git a/wgpu/examples/shadow/main.rs b/wgpu/examples/shadow/main.rs index 8526a00655..59778c3873 100644 --- a/wgpu/examples/shadow/main.rs +++ b/wgpu/examples/shadow/main.rs @@ -247,11 +247,9 @@ impl framework::Example for Example { wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, wgpu::BindingType::UniformBuffer { dynamic: false, - min_binding_size: wgpu::NonZeroBufferAddress::new(mem::size_of::< - EntityUniforms, - >( - ) - as _), + min_binding_size: wgpu::BufferSize::new( + mem::size_of::() as _ + ), }, )], label: None, @@ -437,7 +435,7 @@ impl framework::Example for Example { wgpu::ShaderStage::VERTEX, wgpu::BindingType::UniformBuffer { dynamic: false, - min_binding_size: wgpu::NonZeroBufferAddress::new(uniform_size), + min_binding_size: wgpu::BufferSize::new(uniform_size), }, )], label: None, @@ -521,7 +519,7 @@ impl framework::Example for Example { wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, wgpu::BindingType::UniformBuffer { dynamic: false, - min_binding_size: wgpu::NonZeroBufferAddress::new(mem::size_of::< + min_binding_size: wgpu::BufferSize::new(mem::size_of::< ForwardUniforms, >( ) @@ -533,9 +531,7 @@ impl framework::Example for Example { wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, wgpu::BindingType::UniformBuffer { dynamic: false, - min_binding_size: wgpu::NonZeroBufferAddress::new( - light_uniform_size, - ), + min_binding_size: wgpu::BufferSize::new(light_uniform_size), }, ), wgpu::BindGroupLayoutEntry::new( @@ -760,14 +756,18 @@ impl framework::Example for Example { color_attachments: &[], depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor { attachment: &light.target_view, - depth_load_op: wgpu::LoadOp::Clear, - depth_store_op: wgpu::StoreOp::Store, - depth_read_only: false, - stencil_load_op: wgpu::LoadOp::Clear, - stencil_store_op: wgpu::StoreOp::Store, - clear_depth: 1.0, - clear_stencil: 0, - stencil_read_only: false, + depth: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_value: 1.0, + read_only: false, + }, + stencil: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_value: 0, + read_only: false, + }, }), }); pass.set_pipeline(&self.shadow_pass.pipeline); @@ -787,25 +787,32 @@ impl framework::Example for Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color { - r: 0.1, - g: 0.2, - b: 0.3, - a: 1.0, + channel: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_value: wgpu::Color { + r: 0.1, + g: 0.2, + b: 0.3, + a: 1.0, + }, + read_only: false, }, }], depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor { attachment: &self.forward_depth, - depth_load_op: wgpu::LoadOp::Clear, - depth_store_op: wgpu::StoreOp::Store, - depth_read_only: false, - stencil_load_op: wgpu::LoadOp::Clear, - stencil_store_op: wgpu::StoreOp::Store, - clear_depth: 1.0, - clear_stencil: 0, - stencil_read_only: false, + depth: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Clear, + clear_value: 1.0, + read_only: false, + }, + stencil: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Clear, + clear_value: 0, + read_only: false, + }, }), }); pass.set_pipeline(&self.forward_pass.pipeline); diff --git a/wgpu/examples/skybox/main.rs b/wgpu/examples/skybox/main.rs index 8b275c3513..24612e0596 100644 --- a/wgpu/examples/skybox/main.rs +++ b/wgpu/examples/skybox/main.rs @@ -278,13 +278,16 @@ impl framework::Example for Skybox { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color { - r: 0.1, - g: 0.2, - b: 0.3, - a: 1.0, + channel: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_value: wgpu::Color { + r: 0.1, + g: 0.2, + b: 0.3, + a: 1.0, + }, + read_only: false, }, }], depth_stencil_attachment: None, diff --git a/wgpu/examples/texture-arrays/main.rs b/wgpu/examples/texture-arrays/main.rs index 0a45ee3875..849cbde96e 100644 --- a/wgpu/examples/texture-arrays/main.rs +++ b/wgpu/examples/texture-arrays/main.rs @@ -371,13 +371,11 @@ impl framework::Example for Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color { - r: 0.0, - g: 0.0, - b: 0.0, - a: 1.0, + channel: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_value: wgpu::Color::BLACK, + read_only: false, }, }], depth_stencil_attachment: None, diff --git a/wgpu/examples/water/main.rs b/wgpu/examples/water/main.rs index a692ab220d..7123ba3a0c 100644 --- a/wgpu/examples/water/main.rs +++ b/wgpu/examples/water/main.rs @@ -358,11 +358,9 @@ impl framework::Example for Example { wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, wgpu::BindingType::UniformBuffer { dynamic: false, - min_binding_size: wgpu::NonZeroBufferAddress::new(mem::size_of::< - WaterUniforms, - >( - ) - as _), + min_binding_size: wgpu::BufferSize::new( + mem::size_of::() as _, + ), }, ), // Reflection texture. @@ -404,11 +402,9 @@ impl framework::Example for Example { wgpu::ShaderStage::VERTEX, wgpu::BindingType::UniformBuffer { dynamic: false, - min_binding_size: wgpu::NonZeroBufferAddress::new(mem::size_of::< - TerrainUniforms, - >( - ) - as _), + min_binding_size: wgpu::BufferSize::new( + mem::size_of::() as _, + ), }, ), ], @@ -687,6 +683,12 @@ impl framework::Example for Example { ) -> wgpu::CommandBuffer { // Increment frame count regardless of if we draw. self.current_frame += 1; + let back_color = wgpu::Color { + r: 161.0 / 255.0, + g: 246.0 / 255.0, + b: 255.0 / 255.0, + a: 1.0, + }; // Write the sin/cos values to the uniform buffer for the water. let (water_sin, water_cos) = ((self.current_frame as f32) / 600.0).sin_cos(); @@ -717,27 +719,29 @@ impl framework::Example for Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &self.reflect_view, resolve_target: None, - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color { - r: 161.0 / 255.0, - g: 246.0 / 255.0, - b: 255.0 / 255.0, - a: 1.0, + channel: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_value: back_color, + read_only: false, }, }], // We still need to use the depth buffer here // since the pipeline requires it. depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor { attachment: &self.depth_buffer, - depth_load_op: wgpu::LoadOp::Clear, - depth_store_op: wgpu::StoreOp::Clear, - clear_depth: 1.0, - depth_read_only: false, - stencil_load_op: wgpu::LoadOp::Clear, - stencil_store_op: wgpu::StoreOp::Store, - clear_stencil: 0, - stencil_read_only: false, + depth: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Clear, + clear_value: 1.0, + read_only: false, + }, + stencil: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Clear, + clear_value: 0, + read_only: false, + }, }), }); rpass.set_pipeline(&self.terrain_pipeline); @@ -752,25 +756,27 @@ impl framework::Example for Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color { - r: 161.0 / 255.0, - g: 246.0 / 255.0, - b: 255.0 / 255.0, - a: 1.0, + channel: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_value: back_color, + read_only: false, }, }], depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor { attachment: &self.depth_buffer, - depth_load_op: wgpu::LoadOp::Clear, - depth_store_op: wgpu::StoreOp::Store, - clear_depth: 1.0, - depth_read_only: false, - stencil_load_op: wgpu::LoadOp::Clear, - stencil_store_op: wgpu::StoreOp::Store, - clear_stencil: 0, - stencil_read_only: false, + depth: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_value: 1.0, + read_only: false, + }, + stencil: wgpu::PassChannel { + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_value: 0, + read_only: false, + }, }), }); rpass.set_pipeline(&self.terrain_pipeline); @@ -785,25 +791,27 @@ impl framework::Example for Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - load_op: wgpu::LoadOp::Load, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color { - r: 161.0 / 255.0, - g: 246.0 / 255.0, - b: 255.0 / 255.0, - a: 1.0, + channel: wgpu::PassChannel { + load_op: wgpu::LoadOp::Load, + store_op: wgpu::StoreOp::Store, + clear_value: back_color, + read_only: false, }, }], depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor { attachment: &self.depth_buffer, - depth_load_op: wgpu::LoadOp::Load, - depth_store_op: wgpu::StoreOp::Store, - clear_depth: 1.0, - depth_read_only: true, - stencil_load_op: wgpu::LoadOp::Load, - stencil_store_op: wgpu::StoreOp::Store, - clear_stencil: 0, - stencil_read_only: false, + depth: wgpu::PassChannel { + load_op: wgpu::LoadOp::Load, + store_op: wgpu::StoreOp::Store, + clear_value: 1.0, + read_only: true, + }, + stencil: wgpu::PassChannel { + load_op: wgpu::LoadOp::Load, + store_op: wgpu::StoreOp::Store, + clear_value: 0, + read_only: false, + }, }), }); diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index 5dba242c4b..744ca32a96 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -36,9 +36,9 @@ mod pass_impl { use std::ops::Range; use wgc::command::{bundle_ffi::*, compute_ffi::*, render_ffi::*}; - impl crate::ComputePassInner for wgc::command::RawPass { + impl crate::ComputePassInner for wgc::command::ComputePass { fn set_pipeline(&mut self, pipeline: &wgc::id::ComputePipelineId) { - unsafe { wgpu_compute_pass_set_pipeline(self, *pipeline) } + wgpu_compute_pass_set_pipeline(self, *pipeline) } fn set_bind_group( &mut self, @@ -57,20 +57,20 @@ mod pass_impl { } } fn dispatch(&mut self, x: u32, y: u32, z: u32) { - unsafe { wgpu_compute_pass_dispatch(self, x, y, z) } + wgpu_compute_pass_dispatch(self, x, y, z) } fn dispatch_indirect( &mut self, indirect_buffer: &wgc::id::BufferId, indirect_offset: wgt::BufferAddress, ) { - unsafe { wgpu_compute_pass_dispatch_indirect(self, *indirect_buffer, indirect_offset) } + wgpu_compute_pass_dispatch_indirect(self, *indirect_buffer, indirect_offset) } } - impl crate::RenderInner for wgc::command::RawPass { + impl crate::RenderInner for wgc::command::RenderPass { fn set_pipeline(&mut self, pipeline: &wgc::id::RenderPipelineId) { - unsafe { wgpu_render_pass_set_pipeline(self, *pipeline) } + wgpu_render_pass_set_pipeline(self, *pipeline) } fn set_bind_group( &mut self, @@ -92,66 +92,60 @@ mod pass_impl { &mut self, buffer: &wgc::id::BufferId, offset: wgt::BufferAddress, - size: wgt::BufferSize, + size: Option, ) { - unsafe { wgpu_render_pass_set_index_buffer(self, *buffer, offset, size) } + wgpu_render_pass_set_index_buffer(self, *buffer, offset, size) } fn set_vertex_buffer( &mut self, slot: u32, buffer: &wgc::id::BufferId, offset: wgt::BufferAddress, - size: wgt::BufferSize, + size: Option, ) { - unsafe { wgpu_render_pass_set_vertex_buffer(self, slot, *buffer, offset, size) } + wgpu_render_pass_set_vertex_buffer(self, slot, *buffer, offset, size) } fn draw(&mut self, vertices: Range, instances: Range) { - unsafe { - wgpu_render_pass_draw( - self, - vertices.end - vertices.start, - instances.end - instances.start, - vertices.start, - instances.start, - ) - } + wgpu_render_pass_draw( + self, + vertices.end - vertices.start, + instances.end - instances.start, + vertices.start, + instances.start, + ) } fn draw_indexed(&mut self, indices: Range, base_vertex: i32, instances: Range) { - unsafe { - wgpu_render_pass_draw_indexed( - self, - indices.end - indices.start, - instances.end - instances.start, - indices.start, - base_vertex, - instances.start, - ) - } + wgpu_render_pass_draw_indexed( + self, + indices.end - indices.start, + instances.end - instances.start, + indices.start, + base_vertex, + instances.start, + ) } fn draw_indirect( &mut self, indirect_buffer: &wgc::id::BufferId, indirect_offset: wgt::BufferAddress, ) { - unsafe { wgpu_render_pass_draw_indirect(self, *indirect_buffer, indirect_offset) } + wgpu_render_pass_draw_indirect(self, *indirect_buffer, indirect_offset) } fn draw_indexed_indirect( &mut self, indirect_buffer: &wgc::id::BufferId, indirect_offset: wgt::BufferAddress, ) { - unsafe { - wgpu_render_pass_draw_indexed_indirect(self, *indirect_buffer, indirect_offset) - } + wgpu_render_pass_draw_indexed_indirect(self, *indirect_buffer, indirect_offset) } } - impl crate::RenderPassInner for wgc::command::RawPass { + impl crate::RenderPassInner for wgc::command::RenderPass { fn set_blend_color(&mut self, color: wgt::Color) { - unsafe { wgpu_render_pass_set_blend_color(self, &color) } + wgpu_render_pass_set_blend_color(self, &color) } fn set_scissor_rect(&mut self, x: u32, y: u32, width: u32, height: u32) { - unsafe { wgpu_render_pass_set_scissor_rect(self, x, y, width, height) } + wgpu_render_pass_set_scissor_rect(self, x, y, width, height) } fn set_viewport( &mut self, @@ -162,12 +156,10 @@ mod pass_impl { min_depth: f32, max_depth: f32, ) { - unsafe { - wgpu_render_pass_set_viewport(self, x, y, width, height, min_depth, max_depth) - } + wgpu_render_pass_set_viewport(self, x, y, width, height, min_depth, max_depth) } fn set_stencil_reference(&mut self, reference: u32) { - unsafe { wgpu_render_pass_set_stencil_reference(self, reference) } + wgpu_render_pass_set_stencil_reference(self, reference) } fn insert_debug_marker(&mut self, label: &str) { @@ -185,9 +177,7 @@ mod pass_impl { } fn pop_debug_group(&mut self) { - unsafe { - wgpu_render_pass_pop_debug_group(self); - } + wgpu_render_pass_pop_debug_group(self); } fn execute_bundles<'a, I: Iterator>( @@ -207,7 +197,7 @@ mod pass_impl { impl crate::RenderInner for wgc::command::RenderBundleEncoder { fn set_pipeline(&mut self, pipeline: &wgc::id::RenderPipelineId) { - unsafe { wgpu_render_bundle_set_pipeline(self, *pipeline) } + wgpu_render_bundle_set_pipeline(self, *pipeline) } fn set_bind_group( &mut self, @@ -229,57 +219,51 @@ mod pass_impl { &mut self, buffer: &wgc::id::BufferId, offset: wgt::BufferAddress, - size: wgt::BufferSize, + size: Option, ) { - unsafe { wgpu_render_bundle_set_index_buffer(self, *buffer, offset, size) } + wgpu_render_bundle_set_index_buffer(self, *buffer, offset, size) } fn set_vertex_buffer( &mut self, slot: u32, buffer: &wgc::id::BufferId, offset: wgt::BufferAddress, - size: wgt::BufferSize, + size: Option, ) { - unsafe { wgpu_render_bundle_set_vertex_buffer(self, slot, *buffer, offset, size) } + wgpu_render_bundle_set_vertex_buffer(self, slot, *buffer, offset, size) } fn draw(&mut self, vertices: Range, instances: Range) { - unsafe { - wgpu_render_bundle_draw( - self, - vertices.end - vertices.start, - instances.end - instances.start, - vertices.start, - instances.start, - ) - } + wgpu_render_bundle_draw( + self, + vertices.end - vertices.start, + instances.end - instances.start, + vertices.start, + instances.start, + ) } fn draw_indexed(&mut self, indices: Range, base_vertex: i32, instances: Range) { - unsafe { - wgpu_render_bundle_draw_indexed( - self, - indices.end - indices.start, - instances.end - instances.start, - indices.start, - base_vertex, - instances.start, - ) - } + wgpu_render_bundle_draw_indexed( + self, + indices.end - indices.start, + instances.end - instances.start, + indices.start, + base_vertex, + instances.start, + ) } fn draw_indirect( &mut self, indirect_buffer: &wgc::id::BufferId, indirect_offset: wgt::BufferAddress, ) { - unsafe { wgpu_render_bundle_draw_indirect(self, *indirect_buffer, indirect_offset) } + wgpu_render_bundle_draw_indirect(self, *indirect_buffer, indirect_offset) } fn draw_indexed_indirect( &mut self, indirect_buffer: &wgc::id::BufferId, indirect_offset: wgt::BufferAddress, ) { - unsafe { - wgpu_render_pass_bundle_indexed_indirect(self, *indirect_buffer, indirect_offset) - } + wgpu_render_pass_bundle_indexed_indirect(self, *indirect_buffer, indirect_offset) } } } @@ -314,8 +298,8 @@ impl crate::Context for Context { type RenderPipelineId = wgc::id::RenderPipelineId; type ComputePipelineId = wgc::id::ComputePipelineId; type CommandEncoderId = wgc::id::CommandEncoderId; - type ComputePassId = wgc::command::RawPass; - type RenderPassId = wgc::command::RawPass; + type ComputePassId = wgc::command::ComputePass; + type RenderPassId = wgc::command::RenderPass; type CommandBufferId = wgc::id::CommandBufferId; type RenderBundleEncoderId = wgc::command::RenderBundleEncoder; type RenderBundleId = wgc::id::RenderBundleId; @@ -469,7 +453,7 @@ impl crate::Context for Context { bindings: &bindings, }, PhantomData - )) + )).unwrap() } fn device_create_pipeline_layout( @@ -647,7 +631,7 @@ impl crate::Context for Context { device: &Self::DeviceId, desc: &wgt::RenderBundleEncoderDescriptor, ) -> Self::RenderBundleEncoderId { - wgc::command::RenderBundleEncoder::new(desc, *device) + wgc::command::RenderBundleEncoder::new(desc, *device, None) } fn device_drop(&self, device: &Self::DeviceId) { @@ -711,7 +695,7 @@ impl crate::Context for Context { let ptr = gfx_select!(*buffer => self.buffer_get_mapped_range( *buffer, sub_range.start, - wgt::BufferSize(size) + wgt::BufferSize::new(size) )); unsafe { slice::from_raw_parts(ptr, size as usize) } } @@ -725,7 +709,7 @@ impl crate::Context for Context { let ptr = gfx_select!(*buffer => self.buffer_get_mapped_range( *buffer, sub_range.start, - wgt::BufferSize(size) + wgt::BufferSize::new(size) )); unsafe { slice::from_raw_parts_mut(ptr, size as usize) } } @@ -873,7 +857,7 @@ impl crate::Context for Context { &self, encoder: &Self::CommandEncoderId, ) -> Self::ComputePassId { - unsafe { wgc::command::RawPass::new_compute(*encoder) } + wgc::command::ComputePass::new(*encoder) } fn command_encoder_end_compute_pass( @@ -881,13 +865,7 @@ impl crate::Context for Context { encoder: &Self::CommandEncoderId, pass: &mut Self::ComputePassId, ) { - let data = unsafe { - let mut length = 0; - let ptr = wgc::command::compute_ffi::wgpu_compute_pass_finish(pass, &mut length); - slice::from_raw_parts(ptr, length) - }; - gfx_select!(*encoder => self.command_encoder_run_compute_pass(*encoder, data)); - unsafe { pass.invalidate() }; + gfx_select!(*encoder => self.command_encoder_run_compute_pass(*encoder, pass)); } fn command_encoder_begin_render_pass<'a>( @@ -898,39 +876,28 @@ impl crate::Context for Context { let colors = desc .color_attachments .iter() - .map(|ca| wgc::command::RenderPassColorAttachmentDescriptor { + .map(|ca| wgc::command::ColorAttachmentDescriptor { attachment: ca.attachment.id, resolve_target: ca.resolve_target.map(|rt| rt.id), - load_op: ca.load_op, - store_op: ca.store_op, - clear_color: ca.clear_color, + channel: ca.channel.clone(), }) - .collect::>(); + .collect::>(); let depth_stencil = desc.depth_stencil_attachment.as_ref().map(|dsa| { - wgc::command::RenderPassDepthStencilAttachmentDescriptor { + wgc::command::DepthStencilAttachmentDescriptor { attachment: dsa.attachment.id, - depth_load_op: dsa.depth_load_op, - depth_store_op: dsa.depth_store_op, - depth_read_only: dsa.depth_read_only, - clear_depth: dsa.clear_depth, - stencil_load_op: dsa.stencil_load_op, - stencil_store_op: dsa.stencil_store_op, - clear_stencil: dsa.clear_stencil, - stencil_read_only: dsa.depth_read_only, + depth: dsa.depth.clone(), + stencil: dsa.stencil.clone(), } }); - unsafe { - wgc::command::RawPass::new_render( - *encoder, - &wgc::command::RenderPassDescriptor { - color_attachments: colors.as_ptr(), - color_attachments_length: colors.len(), - depth_stencil_attachment: depth_stencil.as_ref(), - }, - ) - } + wgc::command::RenderPass::new( + *encoder, + wgc::command::RenderPassDescriptor { + color_attachments: &colors, + depth_stencil_attachment: depth_stencil.as_ref(), + }, + ) } fn command_encoder_end_render_pass( @@ -938,13 +905,7 @@ impl crate::Context for Context { encoder: &Self::CommandEncoderId, pass: &mut Self::RenderPassId, ) { - let data = unsafe { - let mut length = 0; - let ptr = wgc::command::render_ffi::wgpu_render_pass_finish(pass, &mut length); - slice::from_raw_parts(ptr, length) - }; - gfx_select!(*encoder => self.command_encoder_run_render_pass(*encoder, data)); - unsafe { pass.invalidate() }; + gfx_select!(*encoder => self.command_encoder_run_render_pass(*encoder, pass)); } fn command_encoder_finish(&self, encoder: &Self::CommandEncoderId) -> Self::CommandBufferId { @@ -958,7 +919,11 @@ impl crate::Context for Context { desc: &crate::RenderBundleDescriptor, ) -> Self::RenderBundleId { let owned_label = OwnedLabel::new(desc.label.as_deref()); - gfx_select!(encoder.parent() => self.render_bundle_encoder_finish(encoder, &desc.map_label(|_| owned_label.as_ptr()), PhantomData)) + gfx_select!(encoder.parent() => self.render_bundle_encoder_finish( + encoder, + &desc.map_label(|_| owned_label.as_ptr()), + PhantomData + )) } fn queue_write_buffer( diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index ccbb50da66..f6607aa98e 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -107,22 +107,27 @@ impl crate::RenderInner for RenderPass { &mut self, buffer: &Sendable, offset: wgt::BufferAddress, - size: wgt::BufferSize, + size: Option, ) { - assert_ne!(size, wgt::BufferSize::WHOLE); //TODO - self.0 - .set_index_buffer_with_f64_and_f64(&buffer.0, offset as f64, size.0 as f64); + self.0.set_index_buffer_with_f64_and_f64( + &buffer.0, + offset as f64, + size.expect("TODO").get() as f64, + ); } fn set_vertex_buffer( &mut self, slot: u32, buffer: &Sendable, offset: wgt::BufferAddress, - size: wgt::BufferSize, + size: Option, ) { - assert_ne!(size, wgt::BufferSize::WHOLE); //TODO - self.0 - .set_vertex_buffer_with_f64_and_f64(slot, &buffer.0, offset as f64, size.0 as f64); + self.0.set_vertex_buffer_with_f64_and_f64( + slot, + &buffer.0, + offset as f64, + size.expect("TODO").get() as f64, + ); } fn draw(&mut self, vertices: Range, instances: Range) { self.0 @@ -879,8 +884,8 @@ impl crate::Context for Context { let mut mapped_buffer_binding = web_sys::GpuBufferBinding::new(&buffer_slice.buffer.id.0); mapped_buffer_binding.offset(buffer_slice.offset as f64); - if buffer_slice.size != wgt::BufferSize::WHOLE { - mapped_buffer_binding.size(buffer_slice.size.0 as f64); + if let Some(s) = buffer_slice.size { + mapped_buffer_binding.size(s.get() as f64); } JsValue::from(mapped_buffer_binding.clone()) } @@ -1286,9 +1291,9 @@ impl crate::Context for Context { let mut mapped_color_attachment = web_sys::GpuRenderPassColorAttachmentDescriptor::new( &ca.attachment.id.0, - &match ca.load_op { + &match ca.channel.load_op { wgt::LoadOp::Clear => { - wasm_bindgen::JsValue::from(map_color(ca.clear_color)) + wasm_bindgen::JsValue::from(map_color(ca.channel.clear_value)) } wgt::LoadOp::Load => { wasm_bindgen::JsValue::from(web_sys::GpuLoadOp::Load) @@ -1300,7 +1305,7 @@ impl crate::Context for Context { mapped_color_attachment.resolve_target(&rt.id.0); } - mapped_color_attachment.store_op(map_store_op(ca.store_op)); + mapped_color_attachment.store_op(map_store_op(ca.channel.store_op)); mapped_color_attachment }) @@ -1314,16 +1319,16 @@ impl crate::Context for Context { let mapped_depth_stencil_attachment = web_sys::GpuRenderPassDepthStencilAttachmentDescriptor::new( &dsa.attachment.id.0, - &match dsa.depth_load_op { - wgt::LoadOp::Clear => wasm_bindgen::JsValue::from(dsa.clear_depth), + &match dsa.depth.load_op { + wgt::LoadOp::Clear => wasm_bindgen::JsValue::from(dsa.depth.clear_value), wgt::LoadOp::Load => wasm_bindgen::JsValue::from(web_sys::GpuLoadOp::Load), }, - map_store_op(dsa.depth_store_op), - &match dsa.stencil_load_op { - wgt::LoadOp::Clear => wasm_bindgen::JsValue::from(dsa.clear_stencil), + map_store_op(dsa.depth.store_op), + &match dsa.stencil.load_op { + wgt::LoadOp::Clear => wasm_bindgen::JsValue::from(dsa.stencil.clear_value), wgt::LoadOp::Load => wasm_bindgen::JsValue::from(web_sys::GpuLoadOp::Load), }, - map_store_op(dsa.stencil_store_op), + map_store_op(dsa.stencil.store_op), ); mapped_desc.depth_stencil_attachment(&mapped_depth_stencil_attachment); diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index b5e839581a..9c492894df 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -26,7 +26,7 @@ pub use wgt::{ Capabilities, Color, ColorStateDescriptor, ColorWrite, CommandBufferDescriptor, CompareFunction, CullMode, DepthStencilStateDescriptor, DeviceDescriptor, DynamicOffset, Extensions, Extent3d, FilterMode, FrontFace, IndexFormat, InputStepMode, Limits, LoadOp, - NonZeroBufferAddress, Origin3d, PowerPreference, PresentMode, PrimitiveTopology, + Origin3d, PassChannel, PowerPreference, PresentMode, PrimitiveTopology, RasterizationStateDescriptor, RenderBundleEncoderDescriptor, ShaderLocation, ShaderStage, StencilOperation, StencilStateFaceDescriptor, StoreOp, SwapChainDescriptor, SwapChainStatus, TextureAspect, TextureComponentType, TextureDataLayout, TextureDimension, TextureFormat, @@ -60,13 +60,18 @@ trait RenderInner { bind_group: &Ctx::BindGroupId, offsets: &[DynamicOffset], ); - fn set_index_buffer(&mut self, buffer: &Ctx::BufferId, offset: BufferAddress, size: BufferSize); + fn set_index_buffer( + &mut self, + buffer: &Ctx::BufferId, + offset: BufferAddress, + size: Option, + ); fn set_vertex_buffer( &mut self, slot: u32, buffer: &Ctx::BufferId, offset: BufferAddress, - size: BufferSize, + size: Option, ); fn draw(&mut self, vertices: Range, instances: Range); fn draw_indexed(&mut self, indices: Range, base_vertex: i32, instances: Range); @@ -420,11 +425,10 @@ impl MapContext { ); } - fn add(&mut self, offset: BufferAddress, size: BufferSize) -> BufferAddress { - let end = if size == BufferSize::WHOLE { - self.initial_range.end - } else { - offset + size.0 + fn add(&mut self, offset: BufferAddress, size: Option) -> BufferAddress { + let end = match size { + Some(s) => offset + s.get(), + None => self.initial_range.end, }; assert!(self.initial_range.start <= offset && end <= self.initial_range.end); for sub in self.sub_ranges.iter() { @@ -438,11 +442,10 @@ impl MapContext { end } - fn remove(&mut self, offset: BufferAddress, size: BufferSize) { - let end = if size == BufferSize::WHOLE { - self.initial_range.end - } else { - offset + size.0 + fn remove(&mut self, offset: BufferAddress, size: Option) { + let end = match size { + Some(s) => offset + s.get(), + None => self.initial_range.end, }; // Switch this out with `Vec::remove_item` once that stabilizes. @@ -474,7 +477,7 @@ pub struct Buffer { pub struct BufferSlice<'a> { buffer: &'a Buffer, offset: BufferAddress, - size: BufferSize, + size: Option, } /// Handle to a texture on the GPU. @@ -1313,16 +1316,18 @@ pub enum MapMode { Write, } -fn range_to_offset_size>(bounds: S) -> (BufferAddress, BufferSize) { +fn range_to_offset_size>( + bounds: S, +) -> (BufferAddress, Option) { let offset = match bounds.start_bound() { Bound::Included(&bound) => bound, Bound::Excluded(&bound) => bound + 1, Bound::Unbounded => 0, }; let size = match bounds.end_bound() { - Bound::Included(&bound) => BufferSize(bound + 1 - offset), - Bound::Excluded(&bound) => BufferSize(bound - offset), - Bound::Unbounded => BufferSize::WHOLE, + Bound::Included(&bound) => BufferSize::new(bound + 1 - offset), + Bound::Excluded(&bound) => BufferSize::new(bound - offset), + Bound::Unbounded => None, }; (offset, size) @@ -1408,31 +1413,7 @@ impl Buffer { } impl BufferSlice<'_> { - /// Use only a portion of this BufferSlice for a given operation. Choosing a range with no end - /// will use the rest of the buffer. Using a totally unbounded range will use the entire BufferSlice. - pub fn slice>(&self, bounds: S) -> Self { - let (sub_offset, sub_size) = range_to_offset_size(bounds); - let new_offset = self.offset + sub_offset; - let new_size = if sub_size == BufferSize::WHOLE { - BufferSize( - self.size - .0 - .checked_sub(sub_offset) - .expect("underflow when slicing `BufferSlice`"), - ) - } else { - assert!( - new_offset + sub_size.0 <= self.offset + self.size.0, - "offset and size must stay within the bounds of the parent slice" - ); - sub_size - }; - Self { - buffer: self.buffer, - offset: new_offset, - size: new_size, - } - } + //TODO: fn slice(&self) -> Self /// Map the buffer. Buffer is ready to map once the future is resolved. /// @@ -1454,10 +1435,9 @@ impl BufferSlice<'_> { "Buffer {:?} is already mapped", self.buffer.id ); - let end = if self.size == BufferSize::WHOLE { - mc.total_size - } else { - self.offset + self.size.0 + let end = match self.size { + Some(s) => self.offset + s.get(), + None => mc.total_size, }; mc.initial_range = self.offset..end; end From dcca52a177536760dabc8d9ab3540e2591e4349c Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Sat, 20 Jun 2020 18:56:31 -0400 Subject: [PATCH 2/2] [rs] Another refactor of RenderPassDescriptor API --- wgpu/examples/boids/main.rs | 8 ++-- wgpu/examples/capture/main.rs | 8 ++-- wgpu/examples/cube/main.rs | 10 ++-- wgpu/examples/hello-triangle/main.rs | 8 ++-- wgpu/examples/mipmap/main.rs | 27 +++++------ wgpu/examples/msaa-line/main.rs | 12 ++--- wgpu/examples/shadow/main.rs | 44 ++++++----------- wgpu/examples/skybox/main.rs | 10 ++-- wgpu/examples/texture-arrays/main.rs | 8 ++-- wgpu/examples/water/main.rs | 72 ++++++++-------------------- wgpu/src/backend/direct.rs | 52 +++++++++++++++++--- wgpu/src/backend/web.rs | 71 ++++++++++++++++----------- wgpu/src/lib.rs | 47 +++++++++++++----- 13 files changed, 195 insertions(+), 182 deletions(-) diff --git a/wgpu/examples/boids/main.rs b/wgpu/examples/boids/main.rs index ee0c954b61..2c38f1e456 100644 --- a/wgpu/examples/boids/main.rs +++ b/wgpu/examples/boids/main.rs @@ -263,11 +263,9 @@ impl framework::Example for Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - channel: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_value: wgpu::Color::BLACK, - read_only: false, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::BLACK), + store: true, }, }], depth_stencil_attachment: None, diff --git a/wgpu/examples/capture/main.rs b/wgpu/examples/capture/main.rs index 136ce424ac..41e0f9a595 100644 --- a/wgpu/examples/capture/main.rs +++ b/wgpu/examples/capture/main.rs @@ -86,11 +86,9 @@ async fn run() { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &texture.create_default_view(), resolve_target: None, - channel: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_value: wgpu::Color::RED, - read_only: false, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::RED), + store: true, }, }], depth_stencil_attachment: None, diff --git a/wgpu/examples/cube/main.rs b/wgpu/examples/cube/main.rs index e5ac3b9cd7..26e57a38da 100644 --- a/wgpu/examples/cube/main.rs +++ b/wgpu/examples/cube/main.rs @@ -326,16 +326,14 @@ impl framework::Example for Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - channel: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_value: wgpu::Color { + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color { r: 0.1, g: 0.2, b: 0.3, a: 1.0, - }, - read_only: false, + }), + store: true, }, }], depth_stencil_attachment: None, diff --git a/wgpu/examples/hello-triangle/main.rs b/wgpu/examples/hello-triangle/main.rs index 478ea6ca14..02829521e0 100644 --- a/wgpu/examples/hello-triangle/main.rs +++ b/wgpu/examples/hello-triangle/main.rs @@ -114,11 +114,9 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu:: color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - channel: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_value: wgpu::Color::GREEN, - read_only: false, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::GREEN), + store: true, }, }], depth_stencil_attachment: None, diff --git a/wgpu/examples/mipmap/main.rs b/wgpu/examples/mipmap/main.rs index cce3d25898..da2c634907 100644 --- a/wgpu/examples/mipmap/main.rs +++ b/wgpu/examples/mipmap/main.rs @@ -186,11 +186,9 @@ impl Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &views[target_mip], resolve_target: None, - channel: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_value: wgpu::Color::WHITE, - read_only: false, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::WHITE), + store: true, }, }], depth_stencil_attachment: None, @@ -411,20 +409,19 @@ impl framework::Example for Example { let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); { + let clear_color = wgpu::Color { + r: 0.1, + g: 0.2, + b: 0.3, + a: 1.0, + }; let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - channel: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_value: wgpu::Color { - r: 0.1, - g: 0.2, - b: 0.3, - a: 1.0, - }, - read_only: false, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(clear_color), + store: true, }, }], depth_stencil_attachment: None, diff --git a/wgpu/examples/msaa-line/main.rs b/wgpu/examples/msaa-line/main.rs index e0728ea63e..482e74a9fe 100644 --- a/wgpu/examples/msaa-line/main.rs +++ b/wgpu/examples/msaa-line/main.rs @@ -255,23 +255,21 @@ impl framework::Example for Example { let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); { - let channel = wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_value: wgpu::Color::BLACK, - read_only: false, + let ops = wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::BLACK), + store: true, }; let rpass_color_attachment = if self.sample_count == 1 { wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - channel, + ops, } } else { wgpu::RenderPassColorAttachmentDescriptor { attachment: &self.multisampled_framebuffer, resolve_target: Some(&frame.view), - channel, + ops, } }; diff --git a/wgpu/examples/shadow/main.rs b/wgpu/examples/shadow/main.rs index 59778c3873..c243e870aa 100644 --- a/wgpu/examples/shadow/main.rs +++ b/wgpu/examples/shadow/main.rs @@ -756,18 +756,11 @@ impl framework::Example for Example { color_attachments: &[], depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor { attachment: &light.target_view, - depth: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_value: 1.0, - read_only: false, - }, - stencil: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_value: 0, - read_only: false, - }, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: true, + }), + stencil_ops: None, }), }); pass.set_pipeline(&self.shadow_pass.pipeline); @@ -787,32 +780,23 @@ impl framework::Example for Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - channel: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_value: wgpu::Color { + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color { r: 0.1, g: 0.2, b: 0.3, a: 1.0, - }, - read_only: false, + }), + store: true, }, }], depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor { attachment: &self.forward_depth, - depth: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Clear, - clear_value: 1.0, - read_only: false, - }, - stencil: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Clear, - clear_value: 0, - read_only: false, - }, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: false, + }), + stencil_ops: None, }), }); pass.set_pipeline(&self.forward_pass.pipeline); diff --git a/wgpu/examples/skybox/main.rs b/wgpu/examples/skybox/main.rs index 24612e0596..05cb66bfba 100644 --- a/wgpu/examples/skybox/main.rs +++ b/wgpu/examples/skybox/main.rs @@ -278,16 +278,14 @@ impl framework::Example for Skybox { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - channel: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_value: wgpu::Color { + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color { r: 0.1, g: 0.2, b: 0.3, a: 1.0, - }, - read_only: false, + }), + store: true, }, }], depth_stencil_attachment: None, diff --git a/wgpu/examples/texture-arrays/main.rs b/wgpu/examples/texture-arrays/main.rs index 849cbde96e..ce9b1b716c 100644 --- a/wgpu/examples/texture-arrays/main.rs +++ b/wgpu/examples/texture-arrays/main.rs @@ -371,11 +371,9 @@ impl framework::Example for Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - channel: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_value: wgpu::Color::BLACK, - read_only: false, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::BLACK), + store: true, }, }], depth_stencil_attachment: None, diff --git a/wgpu/examples/water/main.rs b/wgpu/examples/water/main.rs index 7123ba3a0c..b280e248b0 100644 --- a/wgpu/examples/water/main.rs +++ b/wgpu/examples/water/main.rs @@ -719,29 +719,20 @@ impl framework::Example for Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &self.reflect_view, resolve_target: None, - channel: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_value: back_color, - read_only: false, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(back_color), + store: true, }, }], // We still need to use the depth buffer here // since the pipeline requires it. depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor { attachment: &self.depth_buffer, - depth: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Clear, - clear_value: 1.0, - read_only: false, - }, - stencil: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Clear, - clear_value: 0, - read_only: false, - }, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: true, + }), + stencil_ops: None, }), }); rpass.set_pipeline(&self.terrain_pipeline); @@ -756,27 +747,18 @@ impl framework::Example for Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - channel: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_value: back_color, - read_only: false, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(back_color), + store: true, }, }], depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor { attachment: &self.depth_buffer, - depth: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_value: 1.0, - read_only: false, - }, - stencil: wgpu::PassChannel { - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_value: 0, - read_only: false, - }, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: true, + }), + stencil_ops: None, }), }); rpass.set_pipeline(&self.terrain_pipeline); @@ -791,27 +773,15 @@ impl framework::Example for Example { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: &frame.view, resolve_target: None, - channel: wgpu::PassChannel { - load_op: wgpu::LoadOp::Load, - store_op: wgpu::StoreOp::Store, - clear_value: back_color, - read_only: false, + ops: wgpu::Operations { + load: wgpu::LoadOp::Load, + store: true, }, }], depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor { attachment: &self.depth_buffer, - depth: wgpu::PassChannel { - load_op: wgpu::LoadOp::Load, - store_op: wgpu::StoreOp::Store, - clear_value: 1.0, - read_only: true, - }, - stencil: wgpu::PassChannel { - load_op: wgpu::LoadOp::Load, - store_op: wgpu::StoreOp::Store, - clear_value: 0, - read_only: false, - }, + depth_ops: None, + stencil_ops: None, }), }); diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index 744ca32a96..9d1a48bebf 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -1,9 +1,9 @@ use crate::{ backend::native_gpu_future, BindGroupDescriptor, BindGroupLayoutDescriptor, BindingResource, BufferDescriptor, Capabilities, CommandEncoderDescriptor, ComputePipelineDescriptor, - Extensions, Limits, MapMode, PipelineLayoutDescriptor, RenderPipelineDescriptor, - SamplerDescriptor, ShaderModuleSource, SwapChainStatus, TextureDescriptor, - TextureViewDescriptor, + Extensions, Limits, LoadOp, MapMode, Operations, PipelineLayoutDescriptor, + RenderPipelineDescriptor, SamplerDescriptor, ShaderModuleSource, SwapChainStatus, + TextureDescriptor, TextureViewDescriptor, }; use arrayvec::ArrayVec; @@ -283,6 +283,43 @@ fn map_texture_copy_view(view: crate::TextureCopyView) -> wgc::command::TextureC } } +fn map_pass_channel(ops: Option<&Operations>) -> wgt::PassChannel { + match ops { + Some(&Operations { + load: LoadOp::Clear(clear_value), + store, + }) => wgt::PassChannel { + load_op: wgt::LoadOp::Clear, + store_op: if store { + wgt::StoreOp::Store + } else { + wgt::StoreOp::Clear + }, + clear_value, + read_only: false, + }, + Some(&Operations { + load: LoadOp::Load, + store, + }) => wgt::PassChannel { + load_op: wgt::LoadOp::Load, + store_op: if store { + wgt::StoreOp::Store + } else { + wgt::StoreOp::Clear + }, + clear_value: V::default(), + read_only: false, + }, + None => wgt::PassChannel { + load_op: wgt::LoadOp::Load, + store_op: wgt::StoreOp::Store, + clear_value: V::default(), + read_only: true, + }, + } +} + impl crate::Context for Context { type AdapterId = wgc::id::AdapterId; type DeviceId = wgc::id::DeviceId; @@ -453,7 +490,8 @@ impl crate::Context for Context { bindings: &bindings, }, PhantomData - )).unwrap() + )) + .unwrap() } fn device_create_pipeline_layout( @@ -879,15 +917,15 @@ impl crate::Context for Context { .map(|ca| wgc::command::ColorAttachmentDescriptor { attachment: ca.attachment.id, resolve_target: ca.resolve_target.map(|rt| rt.id), - channel: ca.channel.clone(), + channel: map_pass_channel(Some(&ca.ops)), }) .collect::>(); let depth_stencil = desc.depth_stencil_attachment.as_ref().map(|dsa| { wgc::command::DepthStencilAttachmentDescriptor { attachment: dsa.attachment.id, - depth: dsa.depth.clone(), - stencil: dsa.stencil.clone(), + depth: map_pass_channel(dsa.depth_ops.as_ref()), + stencil: map_pass_channel(dsa.stencil_ops.as_ref()), } }); diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index f6607aa98e..6b73621252 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -1,6 +1,6 @@ use crate::{ BindGroupDescriptor, BindGroupLayoutDescriptor, BindingResource, BindingType, BufferDescriptor, - CommandEncoderDescriptor, ComputePipelineDescriptor, PipelineLayoutDescriptor, + CommandEncoderDescriptor, ComputePipelineDescriptor, LoadOp, PipelineLayoutDescriptor, ProgrammableStageDescriptor, RenderPipelineDescriptor, SamplerDescriptor, ShaderModuleSource, SwapChainStatus, TextureDescriptor, TextureViewDescriptor, TextureViewDimension, }; @@ -190,11 +190,11 @@ impl crate::RenderPassInner for RenderPass { self.0.set_stencil_reference(reference); } - fn insert_debug_marker(&mut self, label: &str) { + fn insert_debug_marker(&mut self, _label: &str) { unimplemented!() } - fn push_debug_group(&mut self, group_label: &str) { + fn push_debug_group(&mut self, _group_label: &str) { unimplemented!() } @@ -202,7 +202,7 @@ impl crate::RenderPassInner for RenderPass { unimplemented!() } - fn execute_bundles<'a, I: Iterator>(&mut self, render_bundles: I) { + fn execute_bundles<'a, I: Iterator>(&mut self, _render_bundles: I) { unimplemented!() } } @@ -563,10 +563,11 @@ fn map_color(color: wgt::Color) -> web_sys::GpuColorDict { web_sys::GpuColorDict::new(color.a, color.b, color.g, color.r) } -fn map_store_op(op: wgt::StoreOp) -> web_sys::GpuStoreOp { - match op { - wgt::StoreOp::Clear => web_sys::GpuStoreOp::Clear, - wgt::StoreOp::Store => web_sys::GpuStoreOp::Store, +fn map_store_op(store: bool) -> web_sys::GpuStoreOp { + if store { + web_sys::GpuStoreOp::Store + } else { + web_sys::GpuStoreOp::Clear } } @@ -783,7 +784,7 @@ impl crate::Context for Context { ShaderModuleSource::SpirV(spv) => { web_sys::GpuShaderModuleDescriptor::new(&js_sys::Uint32Array::from(spv)) } - ShaderModuleSource::Wgsl(code) => { + ShaderModuleSource::Wgsl(_code) => { panic!("WGSL is not yet supported by the Web backend") } }; @@ -1291,13 +1292,9 @@ impl crate::Context for Context { let mut mapped_color_attachment = web_sys::GpuRenderPassColorAttachmentDescriptor::new( &ca.attachment.id.0, - &match ca.channel.load_op { - wgt::LoadOp::Clear => { - wasm_bindgen::JsValue::from(map_color(ca.channel.clear_value)) - } - wgt::LoadOp::Load => { - wasm_bindgen::JsValue::from(web_sys::GpuLoadOp::Load) - } + &match ca.ops.load { + LoadOp::Clear(color) => wasm_bindgen::JsValue::from(map_color(color)), + LoadOp::Load => wasm_bindgen::JsValue::from(web_sys::GpuLoadOp::Load), }, ); @@ -1305,7 +1302,7 @@ impl crate::Context for Context { mapped_color_attachment.resolve_target(&rt.id.0); } - mapped_color_attachment.store_op(map_store_op(ca.channel.store_op)); + mapped_color_attachment.store_op(map_store_op(ca.ops.store)); mapped_color_attachment }) @@ -1316,19 +1313,39 @@ impl crate::Context for Context { // TODO: label if let Some(dsa) = &desc.depth_stencil_attachment { + let (depth_load_op, depth_store_op) = match dsa.depth_ops { + Some(ref ops) => { + let load_op = match ops.load { + LoadOp::Clear(value) => wasm_bindgen::JsValue::from(value), + LoadOp::Load => wasm_bindgen::JsValue::from(web_sys::GpuLoadOp::Load), + }; + (load_op, map_store_op(ops.store)) + } + None => ( + wasm_bindgen::JsValue::from(web_sys::GpuLoadOp::Load), + web_sys::GpuStoreOp::Store, + ), + }; + let (stencil_load_op, stencil_store_op) = match dsa.depth_ops { + Some(ref ops) => { + let load_op = match ops.load { + LoadOp::Clear(value) => wasm_bindgen::JsValue::from(value), + LoadOp::Load => wasm_bindgen::JsValue::from(web_sys::GpuLoadOp::Load), + }; + (load_op, map_store_op(ops.store)) + } + None => ( + wasm_bindgen::JsValue::from(web_sys::GpuLoadOp::Load), + web_sys::GpuStoreOp::Store, + ), + }; let mapped_depth_stencil_attachment = web_sys::GpuRenderPassDepthStencilAttachmentDescriptor::new( &dsa.attachment.id.0, - &match dsa.depth.load_op { - wgt::LoadOp::Clear => wasm_bindgen::JsValue::from(dsa.depth.clear_value), - wgt::LoadOp::Load => wasm_bindgen::JsValue::from(web_sys::GpuLoadOp::Load), - }, - map_store_op(dsa.depth.store_op), - &match dsa.stencil.load_op { - wgt::LoadOp::Clear => wasm_bindgen::JsValue::from(dsa.stencil.clear_value), - wgt::LoadOp::Load => wasm_bindgen::JsValue::from(web_sys::GpuLoadOp::Load), - }, - map_store_op(dsa.stencil.store_op), + &depth_load_op, + depth_store_op, + &stencil_load_op, + stencil_store_op, ); mapped_desc.depth_stencil_attachment(&mapped_depth_stencil_attachment); diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 9c492894df..87826c449c 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -25,12 +25,12 @@ pub use wgt::{ BlendDescriptor, BlendFactor, BlendOperation, BufferAddress, BufferSize, BufferUsage, Capabilities, Color, ColorStateDescriptor, ColorWrite, CommandBufferDescriptor, CompareFunction, CullMode, DepthStencilStateDescriptor, DeviceDescriptor, DynamicOffset, - Extensions, Extent3d, FilterMode, FrontFace, IndexFormat, InputStepMode, Limits, LoadOp, - Origin3d, PassChannel, PowerPreference, PresentMode, PrimitiveTopology, - RasterizationStateDescriptor, RenderBundleEncoderDescriptor, ShaderLocation, ShaderStage, - StencilOperation, StencilStateFaceDescriptor, StoreOp, SwapChainDescriptor, SwapChainStatus, - TextureAspect, TextureComponentType, TextureDataLayout, TextureDimension, TextureFormat, - TextureUsage, TextureViewDimension, UnsafeExtensions, VertexAttributeDescriptor, VertexFormat, + Extensions, Extent3d, FilterMode, FrontFace, IndexFormat, InputStepMode, Limits, Origin3d, + PowerPreference, PresentMode, PrimitiveTopology, RasterizationStateDescriptor, + RenderBundleEncoderDescriptor, ShaderLocation, ShaderStage, StencilOperation, + StencilStateFaceDescriptor, SwapChainDescriptor, SwapChainStatus, TextureAspect, + TextureComponentType, TextureDataLayout, TextureDimension, TextureFormat, TextureUsage, + TextureViewDimension, UnsafeExtensions, VertexAttributeDescriptor, VertexFormat, BIND_BUFFER_ALIGNMENT, COPY_BUFFER_ALIGNMENT, COPY_BYTES_PER_ROW_ALIGNMENT, }; @@ -892,18 +892,39 @@ pub struct ComputePipelineDescriptor<'a> { pub compute_stage: ProgrammableStageDescriptor<'a>, } -// The underlying types are also exported so that documentation shows up for them +/// Operation to perform to the output attachment at the start of a renderpass. +#[derive(Clone, Copy, Debug, Hash, PartialEq)] +pub enum LoadOp { + /// Clear with a specified value. + Clear(V), + /// Load from memory. + Load, +} + +/// Pair of load and store operations for an attachment aspect. +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct Operations { + pub load: LoadOp, + pub store: bool, +} -pub use wgt::RenderPassColorAttachmentDescriptorBase; /// Describes a color attachment to a [`RenderPass`]. -pub type RenderPassColorAttachmentDescriptor<'a> = - wgt::RenderPassColorAttachmentDescriptorBase<&'a TextureView>; -pub use wgt::RenderPassDepthStencilAttachmentDescriptorBase; +#[derive(Clone)] +pub struct RenderPassColorAttachmentDescriptor<'a> { + pub attachment: &'a TextureView, + pub resolve_target: Option<&'a TextureView>, + pub ops: Operations, +} /// Describes a depth/stencil attachment to a [`RenderPass`]. -pub type RenderPassDepthStencilAttachmentDescriptor<'a> = - wgt::RenderPassDepthStencilAttachmentDescriptorBase<&'a TextureView>; +#[derive(Clone)] +pub struct RenderPassDepthStencilAttachmentDescriptor<'a> { + pub attachment: &'a TextureView, + pub depth_ops: Option>, + pub stencil_ops: Option>, +} /// Describes the attachments of a [`RenderPass`]. +#[derive(Clone)] pub struct RenderPassDescriptor<'a, 'b> { /// The color attachments of the render pass. pub color_attachments: &'b [RenderPassColorAttachmentDescriptor<'a>],