From 67b4f3c7e5d2a3bd39240abeaed558cc502db8a3 Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Tue, 31 Mar 2026 21:31:01 +0200 Subject: [PATCH] [wgpu] make vertex buffer slots optional --- benches/benches/wgpu-benchmark/renderpass.rs | 4 +- examples/features/src/boids/mod.rs | 8 +-- examples/features/src/cube/mod.rs | 4 +- examples/features/src/msaa_line/mod.rs | 4 +- examples/features/src/ray_shadows/mod.rs | 4 +- examples/features/src/shadow/mod.rs | 8 +-- examples/features/src/skybox/mod.rs | 4 +- examples/features/src/srgb_blend/mod.rs | 4 +- .../features/src/stencil_triangles/mod.rs | 4 +- examples/features/src/texture_arrays/mod.rs | 4 +- examples/features/src/water/mod.rs | 8 +-- tests/tests/wgpu-gpu/draw_indirect.rs | 16 +++--- tests/tests/wgpu-gpu/per_vertex/mod.rs | 4 +- tests/tests/wgpu-gpu/regression/issue_3457.rs | 12 ++-- tests/tests/wgpu-gpu/render_pass_ownership.rs | 4 +- tests/tests/wgpu-gpu/render_target.rs | 8 +-- .../tests/wgpu-gpu/shader_barycentric/mod.rs | 4 +- .../wgpu-gpu/shader_primitive_index/mod.rs | 4 +- tests/tests/wgpu-gpu/vertex_formats/mod.rs | 4 +- tests/tests/wgpu-gpu/vertex_indices/mod.rs | 12 ++-- tests/tests/wgpu-gpu/vertex_state.rs | 10 ++-- wgpu/src/api/render_bundle_encoder.rs | 22 +++++--- wgpu/src/api/render_pass.rs | 22 +++++--- wgpu/src/api/render_pipeline.rs | 2 +- wgpu/src/backend/webgpu.rs | 55 ++++++++++--------- wgpu/src/backend/wgpu_core.rs | 24 ++++---- wgpu/src/dispatch.rs | 4 +- wgpu/src/util/encoder.rs | 6 +- 28 files changed, 143 insertions(+), 126 deletions(-) diff --git a/benches/benches/wgpu-benchmark/renderpass.rs b/benches/benches/wgpu-benchmark/renderpass.rs index 046ce83ca..d5a8b8d66 100644 --- a/benches/benches/wgpu-benchmark/renderpass.rs +++ b/benches/benches/wgpu-benchmark/renderpass.rs @@ -183,11 +183,11 @@ impl RenderpassState { let mut vertex_buffer_layouts = Vec::with_capacity(VERTEX_BUFFERS_PER_DRAW as usize); for attributes in &vertex_buffer_attributes { - vertex_buffer_layouts.push(wgpu::VertexBufferLayout { + vertex_buffer_layouts.push(Some(wgpu::VertexBufferLayout { array_stride: 16, step_mode: wgpu::VertexStepMode::Vertex, attributes, - }); + })); } let pipeline = diff --git a/examples/features/src/boids/mod.rs b/examples/features/src/boids/mod.rs index 5dd2b3571..886f1aec5 100644 --- a/examples/features/src/boids/mod.rs +++ b/examples/features/src/boids/mod.rs @@ -127,16 +127,16 @@ impl crate::framework::Example for Example { entry_point: Some("main_vs"), compilation_options: Default::default(), buffers: &[ - wgpu::VertexBufferLayout { + Some(wgpu::VertexBufferLayout { array_stride: 4 * 4, step_mode: wgpu::VertexStepMode::Instance, attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x2], - }, - wgpu::VertexBufferLayout { + }), + Some(wgpu::VertexBufferLayout { array_stride: 2 * 4, step_mode: wgpu::VertexStepMode::Vertex, attributes: &wgpu::vertex_attr_array![2 => Float32x2], - }, + }), ], }, fragment: Some(wgpu::FragmentState { diff --git a/examples/features/src/cube/mod.rs b/examples/features/src/cube/mod.rs index 5b6c5c8fc..668b98e04 100644 --- a/examples/features/src/cube/mod.rs +++ b/examples/features/src/cube/mod.rs @@ -218,7 +218,7 @@ impl crate::framework::Example for Example { let shader = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl")); - let vertex_buffers = [wgpu::VertexBufferLayout { + let vertex_buffers = [Some(wgpu::VertexBufferLayout { array_stride: vertex_size as wgpu::BufferAddress, step_mode: wgpu::VertexStepMode::Vertex, attributes: &[ @@ -233,7 +233,7 @@ impl crate::framework::Example for Example { shader_location: 1, }, ], - }]; + })]; let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: None, diff --git a/examples/features/src/msaa_line/mod.rs b/examples/features/src/msaa_line/mod.rs index bcf931681..9e8a34a59 100644 --- a/examples/features/src/msaa_line/mod.rs +++ b/examples/features/src/msaa_line/mod.rs @@ -55,11 +55,11 @@ impl Example { module: shader, entry_point: Some("vs_main"), compilation_options: Default::default(), - buffers: &[wgpu::VertexBufferLayout { + buffers: &[Some(wgpu::VertexBufferLayout { array_stride: size_of::() as wgpu::BufferAddress, step_mode: wgpu::VertexStepMode::Vertex, attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x4], - }], + })], }, fragment: Some(wgpu::FragmentState { module: shader, diff --git a/examples/features/src/ray_shadows/mod.rs b/examples/features/src/ray_shadows/mod.rs index 53201de94..6cd76c59a 100644 --- a/examples/features/src/ray_shadows/mod.rs +++ b/examples/features/src/ray_shadows/mod.rs @@ -196,11 +196,11 @@ impl crate::framework::Example for Example { module: &shader, entry_point: Some("vs_main"), compilation_options: Default::default(), - buffers: &[VertexBufferLayout { + buffers: &[Some(VertexBufferLayout { array_stride: mem::size_of::() as wgpu::BufferAddress, step_mode: Default::default(), attributes: &vertex_attr_array![0 => Float32x3, 1 => Float32x3], - }], + })], }, fragment: Some(wgpu::FragmentState { module: &shader, diff --git a/examples/features/src/shadow/mod.rs b/examples/features/src/shadow/mod.rs index 5e2b405c7..1369037d5 100644 --- a/examples/features/src/shadow/mod.rs +++ b/examples/features/src/shadow/mod.rs @@ -438,11 +438,11 @@ impl crate::framework::Example for Example { }); let vertex_attr = wgpu::vertex_attr_array![0 => Sint8x4, 1 => Sint8x4]; - let vb_desc = wgpu::VertexBufferLayout { + let vertex_buffers = [Some(wgpu::VertexBufferLayout { array_stride: vertex_size as wgpu::BufferAddress, step_mode: wgpu::VertexStepMode::Vertex, attributes: &vertex_attr, - }; + })]; let shader = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl")); @@ -494,7 +494,7 @@ impl crate::framework::Example for Example { module: &shader, entry_point: Some("vs_bake"), compilation_options: Default::default(), - buffers: std::slice::from_ref(&vb_desc), + buffers: &vertex_buffers, }, fragment: None, primitive: wgpu::PrimitiveState { @@ -628,7 +628,7 @@ impl crate::framework::Example for Example { module: &shader, entry_point: Some("vs_main"), compilation_options: Default::default(), - buffers: &[vb_desc], + buffers: &vertex_buffers, }, fragment: Some(wgpu::FragmentState { module: &shader, diff --git a/examples/features/src/skybox/mod.rs b/examples/features/src/skybox/mod.rs index 137cf6304..4ce57e2d1 100644 --- a/examples/features/src/skybox/mod.rs +++ b/examples/features/src/skybox/mod.rs @@ -228,11 +228,11 @@ impl crate::framework::Example for Example { module: &shader, entry_point: Some("vs_entity"), compilation_options: Default::default(), - buffers: &[wgpu::VertexBufferLayout { + buffers: &[Some(wgpu::VertexBufferLayout { array_stride: size_of::() as wgpu::BufferAddress, step_mode: wgpu::VertexStepMode::Vertex, attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3], - }], + })], }, fragment: Some(wgpu::FragmentState { module: &shader, diff --git a/examples/features/src/srgb_blend/mod.rs b/examples/features/src/srgb_blend/mod.rs index ba97121aa..bc4992d2e 100644 --- a/examples/features/src/srgb_blend/mod.rs +++ b/examples/features/src/srgb_blend/mod.rs @@ -104,7 +104,7 @@ impl crate::framework::Example for Example { let shader = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl")); - let vertex_buffers = [wgpu::VertexBufferLayout { + let vertex_buffers = [Some(wgpu::VertexBufferLayout { array_stride: vertex_size as wgpu::BufferAddress, step_mode: wgpu::VertexStepMode::Vertex, attributes: &[ @@ -119,7 +119,7 @@ impl crate::framework::Example for Example { shader_location: 1, }, ], - }]; + })]; let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: None, diff --git a/examples/features/src/stencil_triangles/mod.rs b/examples/features/src/stencil_triangles/mod.rs index 6a108c9da..210ce1481 100644 --- a/examples/features/src/stencil_triangles/mod.rs +++ b/examples/features/src/stencil_triangles/mod.rs @@ -53,7 +53,7 @@ impl crate::framework::Example for Example { let shader = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl")); - let vertex_buffers = [wgpu::VertexBufferLayout { + let vertex_buffers = [Some(wgpu::VertexBufferLayout { array_stride: vertex_size as wgpu::BufferAddress, step_mode: wgpu::VertexStepMode::Vertex, attributes: &[wgpu::VertexAttribute { @@ -61,7 +61,7 @@ impl crate::framework::Example for Example { offset: 0, shader_location: 0, }], - }]; + })]; let mask_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: None, diff --git a/examples/features/src/texture_arrays/mod.rs b/examples/features/src/texture_arrays/mod.rs index 082b09e7a..da909670c 100644 --- a/examples/features/src/texture_arrays/mod.rs +++ b/examples/features/src/texture_arrays/mod.rs @@ -340,11 +340,11 @@ impl crate::framework::Example for Example { module: &base_shader_module, entry_point: Some("vert_main"), compilation_options: Default::default(), - buffers: &[wgpu::VertexBufferLayout { + buffers: &[Some(wgpu::VertexBufferLayout { array_stride: vertex_size as wgpu::BufferAddress, step_mode: wgpu::VertexStepMode::Vertex, attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x2, 2 => Sint32], - }], + })], }, fragment: Some(wgpu::FragmentState { module: fragment_shader_module, diff --git a/examples/features/src/water/mod.rs b/examples/features/src/water/mod.rs index 20485f895..0db1622b2 100644 --- a/examples/features/src/water/mod.rs +++ b/examples/features/src/water/mod.rs @@ -512,11 +512,11 @@ impl crate::framework::Example for Example { // ensured by tagging on either a `#[repr(C)]` onto a // struct, or a `#[repr(transparent)]` if it only contains // one item, which is itself `repr(C)`. - buffers: &[wgpu::VertexBufferLayout { + buffers: &[Some(wgpu::VertexBufferLayout { array_stride: water_vertex_size as wgpu::BufferAddress, step_mode: wgpu::VertexStepMode::Vertex, attributes: &wgpu::vertex_attr_array![0 => Sint16x2, 1 => Sint8x4], - }], + })], }, // Fragment shader and output targets fragment: Some(wgpu::FragmentState { @@ -579,11 +579,11 @@ impl crate::framework::Example for Example { module: &terrain_module, entry_point: Some("vs_main"), compilation_options: Default::default(), - buffers: &[wgpu::VertexBufferLayout { + buffers: &[Some(wgpu::VertexBufferLayout { array_stride: terrain_vertex_size as wgpu::BufferAddress, step_mode: wgpu::VertexStepMode::Vertex, attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3, 2 => Unorm8x4], - }], + })], }, fragment: Some(wgpu::FragmentState { module: &terrain_module, diff --git a/tests/tests/wgpu-gpu/draw_indirect.rs b/tests/tests/wgpu-gpu/draw_indirect.rs index 7125fc938..391253c3d 100644 --- a/tests/tests/wgpu-gpu/draw_indirect.rs +++ b/tests/tests/wgpu-gpu/draw_indirect.rs @@ -127,11 +127,11 @@ async fn run_test_inner( use_multi_draw: bool, ) { let mut vertex_buffer_layouts = Vec::new(); - vertex_buffer_layouts.push(wgpu::VertexBufferLayout { + vertex_buffer_layouts.push(Some(wgpu::VertexBufferLayout { array_stride: 8, step_mode: wgpu::VertexStepMode::Vertex, attributes: &vertex_attr_array![0 => Float32x2], - }); + })); let vertex_buffer = ctx.device.create_buffer_init(&BufferInitDescriptor { label: None, contents: bytemuck::cast_slice(test_data.vertex_buffer_content()), @@ -151,11 +151,11 @@ async fn run_test_inner( }; let instance_buffer = test_data.instanced.as_ref().map(|instanced| { - vertex_buffer_layouts.push(wgpu::VertexBufferLayout { + vertex_buffer_layouts.push(Some(wgpu::VertexBufferLayout { array_stride: 8, step_mode: wgpu::VertexStepMode::Instance, attributes: &vertex_attr_array![1 => Float32x2], - }); + })); ctx.device.create_buffer_init(&BufferInitDescriptor { label: None, contents: bytemuck::cast_slice(instanced.instance_buffer_content), @@ -689,16 +689,16 @@ async fn indirect_buffer_offsets(ctx: TestingContext) { layout: None, vertex: wgpu::VertexState { buffers: &[ - wgpu::VertexBufferLayout { + Some(wgpu::VertexBufferLayout { array_stride: 8, step_mode: wgpu::VertexStepMode::Vertex, attributes: &vertex_attr_array![0 => Float32x2], - }, - wgpu::VertexBufferLayout { + }), + Some(wgpu::VertexBufferLayout { array_stride: 8, step_mode: wgpu::VertexStepMode::Instance, attributes: &vertex_attr_array![1 => Float32x2], - }, + }), ], module: &shader, entry_point: Some("vs_main"), diff --git a/tests/tests/wgpu-gpu/per_vertex/mod.rs b/tests/tests/wgpu-gpu/per_vertex/mod.rs index c0eb1e4ef..21d2046d2 100644 --- a/tests/tests/wgpu-gpu/per_vertex/mod.rs +++ b/tests/tests/wgpu-gpu/per_vertex/mod.rs @@ -83,7 +83,7 @@ async fn per_vertex(ctx: TestingContext) { module: &shader, entry_point: Some("vs_main"), compilation_options: Default::default(), - buffers: &[wgpu::VertexBufferLayout { + buffers: &[Some(wgpu::VertexBufferLayout { array_stride: 12, step_mode: wgpu::VertexStepMode::Vertex, attributes: &[wgpu::VertexAttribute { @@ -91,7 +91,7 @@ async fn per_vertex(ctx: TestingContext) { offset: 0, shader_location: 0, }], - }], + })], }, primitive: wgpu::PrimitiveState { topology: wgpu::PrimitiveTopology::TriangleStrip, diff --git a/tests/tests/wgpu-gpu/regression/issue_3457.rs b/tests/tests/wgpu-gpu/regression/issue_3457.rs index 731e1896b..52c0f9155 100644 --- a/tests/tests/wgpu-gpu/regression/issue_3457.rs +++ b/tests/tests/wgpu-gpu/regression/issue_3457.rs @@ -59,16 +59,16 @@ static PASS_RESET_VERTEX_BUFFER: GpuTestConfiguration = GpuTestConfiguration::ne entry_point: Some("double_buffer_vert"), compilation_options: Default::default(), buffers: &[ - VertexBufferLayout { + Some(VertexBufferLayout { array_stride: 16, step_mode: VertexStepMode::Vertex, attributes: &vertex_attr_array![0 => Float32x4], - }, - VertexBufferLayout { + }), + Some(VertexBufferLayout { array_stride: 4, step_mode: VertexStepMode::Vertex, attributes: &vertex_attr_array![5 => Float32], - }, + }), ], }, primitive: PrimitiveState::default(), @@ -97,11 +97,11 @@ static PASS_RESET_VERTEX_BUFFER: GpuTestConfiguration = GpuTestConfiguration::ne module: &module, entry_point: Some("single_buffer_vert"), compilation_options: Default::default(), - buffers: &[VertexBufferLayout { + buffers: &[Some(VertexBufferLayout { array_stride: 16, step_mode: VertexStepMode::Vertex, attributes: &vertex_attr_array![0 => Float32x4], - }], + })], }, primitive: PrimitiveState::default(), depth_stencil: None, diff --git a/tests/tests/wgpu-gpu/render_pass_ownership.rs b/tests/tests/wgpu-gpu/render_pass_ownership.rs index 5932b6e0e..874d92521 100644 --- a/tests/tests/wgpu-gpu/render_pass_ownership.rs +++ b/tests/tests/wgpu-gpu/render_pass_ownership.rs @@ -520,11 +520,11 @@ fn resource_setup(ctx: &TestingContext) -> ResourceSetup { module: &sm, entry_point: Some("vs_main"), compilation_options: Default::default(), - buffers: &[wgpu::VertexBufferLayout { + buffers: &[Some(wgpu::VertexBufferLayout { array_stride: 4, step_mode: wgpu::VertexStepMode::Vertex, attributes: &wgpu::vertex_attr_array![0 => Uint32], - }], + })], }, fragment: Some(wgpu::FragmentState { module: &sm, diff --git a/tests/tests/wgpu-gpu/render_target.rs b/tests/tests/wgpu-gpu/render_target.rs index 7be8ab0c3..055b8304f 100644 --- a/tests/tests/wgpu-gpu/render_target.rs +++ b/tests/tests/wgpu-gpu/render_target.rs @@ -80,11 +80,11 @@ async fn run_test( label: None, layout: None, vertex: wgpu::VertexState { - buffers: &[wgpu::VertexBufferLayout { + buffers: &[Some(wgpu::VertexBufferLayout { array_stride: 8, step_mode: wgpu::VertexStepMode::Vertex, attributes: &vertex_attr_array![0 => Float32x2], - }], + })], module: &shader, entry_point: Some("vs_main"), compilation_options: Default::default(), @@ -305,11 +305,11 @@ async fn run_test_3d(ctx: TestingContext) { label: None, layout: None, vertex: wgpu::VertexState { - buffers: &[wgpu::VertexBufferLayout { + buffers: &[Some(wgpu::VertexBufferLayout { array_stride: 8, step_mode: wgpu::VertexStepMode::Vertex, attributes: &vertex_attr_array![0 => Float32x2], - }], + })], module: &shader, entry_point: Some("vs_main"), compilation_options: Default::default(), diff --git a/tests/tests/wgpu-gpu/shader_barycentric/mod.rs b/tests/tests/wgpu-gpu/shader_barycentric/mod.rs index f8a478f31..fab72f4fd 100644 --- a/tests/tests/wgpu-gpu/shader_barycentric/mod.rs +++ b/tests/tests/wgpu-gpu/shader_barycentric/mod.rs @@ -82,7 +82,7 @@ async fn barycentric(ctx: TestingContext, no_perspective: bool) { module: &shader, entry_point: Some("vs_main"), compilation_options: Default::default(), - buffers: &[wgpu::VertexBufferLayout { + buffers: &[Some(wgpu::VertexBufferLayout { array_stride: 8, step_mode: wgpu::VertexStepMode::Vertex, attributes: &[wgpu::VertexAttribute { @@ -90,7 +90,7 @@ async fn barycentric(ctx: TestingContext, no_perspective: bool) { offset: 0, shader_location: 0, }], - }], + })], }, primitive: wgpu::PrimitiveState::default(), depth_stencil: None, diff --git a/tests/tests/wgpu-gpu/shader_primitive_index/mod.rs b/tests/tests/wgpu-gpu/shader_primitive_index/mod.rs index 3b37e75e1..8061ae01e 100644 --- a/tests/tests/wgpu-gpu/shader_primitive_index/mod.rs +++ b/tests/tests/wgpu-gpu/shader_primitive_index/mod.rs @@ -127,7 +127,7 @@ async fn pulling_common( module: &shader, entry_point: Some("vs_main"), compilation_options: Default::default(), - buffers: &[wgpu::VertexBufferLayout { + buffers: &[Some(wgpu::VertexBufferLayout { array_stride: 8, step_mode: wgpu::VertexStepMode::Vertex, attributes: &[wgpu::VertexAttribute { @@ -135,7 +135,7 @@ async fn pulling_common( offset: 0, shader_location: 0, }], - }], + })], }, primitive: wgpu::PrimitiveState::default(), depth_stencil: None, diff --git a/tests/tests/wgpu-gpu/vertex_formats/mod.rs b/tests/tests/wgpu-gpu/vertex_formats/mod.rs index a9b967186..100d72604 100644 --- a/tests/tests/wgpu-gpu/vertex_formats/mod.rs +++ b/tests/tests/wgpu-gpu/vertex_formats/mod.rs @@ -292,11 +292,11 @@ async fn vertex_formats_common(ctx: TestingContext, tests: &[Test<'_>]) { label: None, layout: Some(&ppl), vertex: wgpu::VertexState { - buffers: &[wgpu::VertexBufferLayout { + buffers: &[Some(wgpu::VertexBufferLayout { array_stride: 0, // Calculate, please! step_mode: wgpu::VertexStepMode::Vertex, attributes: test.attributes, - }], + })], module: &shader, entry_point: Some(test.entry_point), compilation_options: Default::default(), diff --git a/tests/tests/wgpu-gpu/vertex_indices/mod.rs b/tests/tests/wgpu-gpu/vertex_indices/mod.rs index f9c01cecc..8b2c3f76a 100644 --- a/tests/tests/wgpu-gpu/vertex_indices/mod.rs +++ b/tests/tests/wgpu-gpu/vertex_indices/mod.rs @@ -266,16 +266,16 @@ async fn vertex_index_common(ctx: TestingContext) { pipeline_desc.vertex.entry_point = Some("vs_main_buffers"); pipeline_desc.vertex.buffers = &[ - wgpu::VertexBufferLayout { + Some(wgpu::VertexBufferLayout { array_stride: 4, step_mode: wgpu::VertexStepMode::Instance, attributes: &wgpu::vertex_attr_array![0 => Uint32], - }, - wgpu::VertexBufferLayout { + }), + Some(wgpu::VertexBufferLayout { array_stride: 4, step_mode: wgpu::VertexStepMode::Vertex, attributes: &wgpu::vertex_attr_array![1 => Uint32], - }, + }), ]; let buffer_pipeline = ctx.device.create_render_pipeline(&pipeline_desc); @@ -391,8 +391,8 @@ async fn vertex_index_common(ctx: TestingContext) { .map(|r| r as &mut dyn RenderEncoder) .unwrap_or(&mut rpass); - render_encoder.set_vertex_buffer(0, identity_buffer.slice(..)); - render_encoder.set_vertex_buffer(1, identity_buffer.slice(..)); + render_encoder.set_vertex_buffer(0, Some(identity_buffer.slice(..))); + render_encoder.set_vertex_buffer(1, Some(identity_buffer.slice(..))); render_encoder.set_index_buffer(identity_buffer.slice(..), wgpu::IndexFormat::Uint32); render_encoder.set_pipeline(pipeline); render_encoder.set_bind_group(0, Some(&bg), &[]); diff --git a/tests/tests/wgpu-gpu/vertex_state.rs b/tests/tests/wgpu-gpu/vertex_state.rs index e120cf479..b8995469c 100644 --- a/tests/tests/wgpu-gpu/vertex_state.rs +++ b/tests/tests/wgpu-gpu/vertex_state.rs @@ -74,16 +74,16 @@ async fn set_array_stride_to_0(ctx: TestingContext) { }); let vbl = [ - wgpu::VertexBufferLayout { + Some(wgpu::VertexBufferLayout { array_stride: 8, step_mode: wgpu::VertexStepMode::Vertex, attributes: &vertex_attr_array![0 => Float32x2], - }, - wgpu::VertexBufferLayout { + }), + Some(wgpu::VertexBufferLayout { array_stride: 0, step_mode: wgpu::VertexStepMode::Vertex, attributes: &vertex_attr_array![1 => Float32x4], - }, + }), ]; let pipeline_desc = wgpu::RenderPipelineDescriptor { label: None, @@ -112,7 +112,7 @@ async fn set_array_stride_to_0(ctx: TestingContext) { }; let mut first_pipeline_desc = pipeline_desc.clone(); let mut first_vbl = vbl.clone(); - first_vbl[1].array_stride = 16; + first_vbl[1].as_mut().unwrap().array_stride = 16; first_pipeline_desc.vertex.buffers = &first_vbl; let pipeline = ctx.device.create_render_pipeline(&pipeline_desc); let first_pipeline = ctx.device.create_render_pipeline(&first_pipeline_desc); diff --git a/wgpu/src/api/render_bundle_encoder.rs b/wgpu/src/api/render_bundle_encoder.rs index cea7aa330..dcc3b9f05 100644 --- a/wgpu/src/api/render_bundle_encoder.rs +++ b/wgpu/src/api/render_bundle_encoder.rs @@ -107,13 +107,21 @@ impl<'a> RenderBundleEncoder<'a> { /// /// [`draw`]: RenderBundleEncoder::draw /// [`draw_indexed`]: RenderBundleEncoder::draw_indexed - pub fn set_vertex_buffer(&mut self, slot: u32, buffer_slice: BufferSlice<'a>) { - self.inner.set_vertex_buffer( - slot, - &buffer_slice.buffer.inner, - buffer_slice.offset, - Some(buffer_slice.size), - ); + pub fn set_vertex_buffer<'b, B>(&mut self, slot: u32, buffer_slice: B) + where + Option>: From, + { + let buffer_slice: Option> = buffer_slice.into(); + if let Some(buffer_slice) = buffer_slice { + self.inner.set_vertex_buffer( + slot, + Some(&buffer_slice.buffer.inner), + buffer_slice.offset, + Some(buffer_slice.size), + ); + } else { + self.inner.set_vertex_buffer(slot, None, 0, None); + } } /// Draws primitives from the active vertex buffer(s). diff --git a/wgpu/src/api/render_pass.rs b/wgpu/src/api/render_pass.rs index 3bd24d2c5..6050ed1da 100644 --- a/wgpu/src/api/render_pass.rs +++ b/wgpu/src/api/render_pass.rs @@ -121,13 +121,21 @@ impl RenderPass<'_> { /// /// [`draw`]: RenderPass::draw /// [`draw_indexed`]: RenderPass::draw_indexed - pub fn set_vertex_buffer(&mut self, slot: u32, buffer_slice: BufferSlice<'_>) { - self.inner.set_vertex_buffer( - slot, - &buffer_slice.buffer.inner, - buffer_slice.offset, - Some(buffer_slice.size), - ); + pub fn set_vertex_buffer<'b, B>(&mut self, slot: u32, buffer_slice: B) + where + Option>: From, + { + let buffer_slice: Option> = buffer_slice.into(); + if let Some(buffer_slice) = buffer_slice { + self.inner.set_vertex_buffer( + slot, + Some(&buffer_slice.buffer.inner), + buffer_slice.offset, + Some(buffer_slice.size), + ); + } else { + self.inner.set_vertex_buffer(slot, None, 0, None); + } } /// Sets the scissor rectangle used during the rasterization stage. diff --git a/wgpu/src/api/render_pipeline.rs b/wgpu/src/api/render_pipeline.rs index ebc89633b..b7312f9e1 100644 --- a/wgpu/src/api/render_pipeline.rs +++ b/wgpu/src/api/render_pipeline.rs @@ -112,7 +112,7 @@ pub struct VertexState<'a> { /// /// The attribute locations and types specified in this layout must match the /// locations and types of the inputs to the `entry_point` function. - pub buffers: &'a [VertexBufferLayout<'a>], + pub buffers: &'a [Option>], } #[cfg(send_sync)] static_assertions::assert_impl_all!(VertexState<'_>: Send, Sync); diff --git a/wgpu/src/backend/webgpu.rs b/wgpu/src/backend/webgpu.rs index f1dd95228..d7bceed14 100644 --- a/wgpu/src/backend/webgpu.rs +++ b/wgpu/src/backend/webgpu.rs @@ -2215,25 +2215,28 @@ impl dispatch::DeviceInterface for WebDevice { .vertex .buffers .iter() - .map(|vbuf| { - let mapped_attributes = vbuf - .attributes - .iter() - .map(|attr| { - webgpu_sys::GpuVertexAttribute::new_with_f64( - map_vertex_format(attr.format), - attr.offset as f64, - attr.shader_location, - ) - }) - .collect::>(); + .map(|vbuf| match vbuf { + Some(vbuf) => { + let mapped_attributes = vbuf + .attributes + .iter() + .map(|attr| { + webgpu_sys::GpuVertexAttribute::new_with_f64( + map_vertex_format(attr.format), + attr.offset as f64, + attr.shader_location, + ) + }) + .collect::>(); - let mapped_vbuf = webgpu_sys::GpuVertexBufferLayout::new_with_f64( - vbuf.array_stride as f64, - &mapped_attributes, - ); - mapped_vbuf.set_step_mode(map_vertex_step_mode(vbuf.step_mode)); - js_sys::JsOption::wrap(mapped_vbuf) + let mapped_vbuf = webgpu_sys::GpuVertexBufferLayout::new_with_f64( + vbuf.array_stride as f64, + &mapped_attributes, + ); + mapped_vbuf.set_step_mode(map_vertex_step_mode(vbuf.step_mode)); + js_sys::JsOption::wrap(mapped_vbuf) + } + None => js_sys::JsOption::new(), }) .collect::>>(); @@ -3517,22 +3520,22 @@ impl dispatch::RenderPassInterface for WebRenderPassEncoder { fn set_vertex_buffer( &mut self, slot: u32, - buffer: &dispatch::DispatchBuffer, + buffer: Option<&dispatch::DispatchBuffer>, offset: crate::BufferAddress, size: Option, ) { - let buffer = buffer.as_webgpu(); + let buffer = buffer.map(|buffer| &buffer.as_webgpu().inner); if let Some(size) = size { self.inner.set_vertex_buffer_with_f64_and_f64( slot, - Some(&buffer.inner), + buffer, offset as f64, size.get() as f64, ); } else { self.inner - .set_vertex_buffer_with_f64(slot, Some(&buffer.inner), offset as f64); + .set_vertex_buffer_with_f64(slot, buffer, offset as f64); } } @@ -3808,22 +3811,22 @@ impl dispatch::RenderBundleEncoderInterface for WebRenderBundleEncoder { fn set_vertex_buffer( &mut self, slot: u32, - buffer: &dispatch::DispatchBuffer, + buffer: Option<&dispatch::DispatchBuffer>, offset: crate::BufferAddress, size: Option, ) { - let buffer = buffer.as_webgpu(); + let buffer = buffer.map(|buffer| &buffer.as_webgpu().inner); if let Some(size) = size { self.inner.set_vertex_buffer_with_f64_and_f64( slot, - Some(&buffer.inner), + buffer, offset as f64, size.get() as f64, ); } else { self.inner - .set_vertex_buffer_with_f64(slot, Some(&buffer.inner), offset as f64); + .set_vertex_buffer_with_f64(slot, buffer, offset as f64); } } diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index c6b1a16b1..9bf68c129 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -1339,7 +1339,7 @@ impl dispatch::DeviceInterface for CoreDevice { .buffers .iter() .map(|vbuf| { - Some(pipe::VertexBufferLayout { + vbuf.as_ref().map(|vbuf| pipe::VertexBufferLayout { array_stride: vbuf.array_stride, step_mode: vbuf.step_mode, attributes: Borrowed(vbuf.attributes), @@ -3240,19 +3240,17 @@ impl dispatch::RenderPassInterface for CoreRenderPass { fn set_vertex_buffer( &mut self, slot: u32, - buffer: &dispatch::DispatchBuffer, + buffer: Option<&dispatch::DispatchBuffer>, offset: crate::BufferAddress, size: Option, ) { - let buffer = buffer.as_core(); + let buffer = buffer.map(|buffer| buffer.as_core().id); - if let Err(cause) = self.context.0.render_pass_set_vertex_buffer( - &mut self.pass, - slot, - Some(buffer.id), - offset, - size, - ) { + if let Err(cause) = + self.context + .0 + .render_pass_set_vertex_buffer(&mut self.pass, slot, buffer, offset, size) + { self.context.handle_error( &self.error_sink, cause, @@ -3825,13 +3823,13 @@ impl dispatch::RenderBundleEncoderInterface for CoreRenderBundleEncoder { fn set_vertex_buffer( &mut self, slot: u32, - buffer: &dispatch::DispatchBuffer, + buffer: Option<&dispatch::DispatchBuffer>, offset: crate::BufferAddress, size: Option, ) { - let buffer = buffer.as_core(); + let buffer = buffer.map(|buffer| buffer.as_core().id); - wgpu_render_bundle_set_vertex_buffer(&mut self.encoder, slot, Some(buffer.id), offset, size) + wgpu_render_bundle_set_vertex_buffer(&mut self.encoder, slot, buffer, offset, size) } fn set_immediates(&mut self, offset: u32, data: &[u8]) { diff --git a/wgpu/src/dispatch.rs b/wgpu/src/dispatch.rs index 099ad81b5..2f8031ee8 100644 --- a/wgpu/src/dispatch.rs +++ b/wgpu/src/dispatch.rs @@ -438,7 +438,7 @@ pub trait RenderPassInterface: CommonTraits + Drop { fn set_vertex_buffer( &mut self, slot: u32, - buffer: &DispatchBuffer, + buffer: Option<&DispatchBuffer>, offset: crate::BufferAddress, size: Option, ); @@ -549,7 +549,7 @@ pub trait RenderBundleEncoderInterface: CommonTraits { fn set_vertex_buffer( &mut self, slot: u32, - buffer: &DispatchBuffer, + buffer: Option<&DispatchBuffer>, offset: crate::BufferAddress, size: Option, ); diff --git a/wgpu/src/util/encoder.rs b/wgpu/src/util/encoder.rs index 64d9b6c3a..8f5d255b5 100644 --- a/wgpu/src/util/encoder.rs +++ b/wgpu/src/util/encoder.rs @@ -38,7 +38,7 @@ pub trait RenderEncoder<'a> { /// /// [`draw`]: RenderEncoder::draw /// [`draw_indexed`]: RenderEncoder::draw_indexed - fn set_vertex_buffer(&mut self, slot: u32, buffer_slice: BufferSlice<'a>); + fn set_vertex_buffer(&mut self, slot: u32, buffer_slice: Option>); /// Draws primitives from the active vertex buffer(s). /// @@ -106,7 +106,7 @@ impl<'a> RenderEncoder<'a> for RenderPass<'a> { } #[inline(always)] - fn set_vertex_buffer(&mut self, slot: u32, buffer_slice: BufferSlice<'a>) { + fn set_vertex_buffer(&mut self, slot: u32, buffer_slice: Option>) { Self::set_vertex_buffer(self, slot, buffer_slice); } @@ -162,7 +162,7 @@ impl<'a> RenderEncoder<'a> for RenderBundleEncoder<'a> { } #[inline(always)] - fn set_vertex_buffer(&mut self, slot: u32, buffer_slice: BufferSlice<'a>) { + fn set_vertex_buffer(&mut self, slot: u32, buffer_slice: Option>) { Self::set_vertex_buffer(self, slot, buffer_slice); }