Skip to content

Commit ce96254

Browse files
authored
[msl] fix vertex pulling with a stride of 0 (#8265)
1 parent 3375284 commit ce96254

File tree

10 files changed

+56
-34
lines changed

10 files changed

+56
-34
lines changed

cts_runner/test.lst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ webgpu:api,operation,compute,basic:memcpy:*
1818
webgpu:api,operation,compute_pipeline,overrides:*
1919
webgpu:api,operation,device,lost:*
2020
webgpu:api,operation,render_pass,storeOp:*
21-
fails-if(metal,vulkan) webgpu:api,operation,vertex_state,correctness:array_stride_zero:*
21+
fails-if(vulkan) webgpu:api,operation,vertex_state,correctness:array_stride_zero:*
2222
// Presumably vertex pulling, revisit after https://github.com/gfx-rs/wgpu/issues/7981 is fixed.
2323
fails-if(metal) webgpu:api,operation,vertex_state,correctness:setVertexBuffer_offset_and_attribute_offset:*
2424
fails-if(dx12) webgpu:api,validation,capability_checks,limits,maxBindGroups:setBindGroup,*

naga/src/back/msl/mod.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,17 @@ pub enum VertexFormat {
418418
Unorm8x4Bgra = 44,
419419
}
420420

421+
/// Defines how to advance the data in vertex buffers.
422+
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
423+
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
424+
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
425+
pub enum VertexBufferStepMode {
426+
Constant,
427+
#[default]
428+
ByVertex,
429+
ByInstance,
430+
}
431+
421432
/// A mapping of vertex buffers and their attributes to shader
422433
/// locations.
423434
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -446,9 +457,8 @@ pub struct VertexBufferMapping {
446457
pub id: u32,
447458
/// Size of the structure in bytes
448459
pub stride: u32,
449-
/// True if the buffer is indexed by vertex, false if indexed
450-
/// by instance.
451-
pub indexed_by_vertex: bool,
460+
/// Vertex buffer step mode
461+
pub step_mode: VertexBufferStepMode,
452462
/// Vec of the attributes within the structure
453463
pub attributes: Vec<AttributeMapping>,
454464
}

naga/src/back/msl/writer.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6359,7 +6359,7 @@ template <typename A>
63596359
struct VertexBufferMappingResolved<'a> {
63606360
id: u32,
63616361
stride: u32,
6362-
indexed_by_vertex: bool,
6362+
step_mode: back::msl::VertexBufferStepMode,
63636363
ty_name: String,
63646364
param_name: String,
63656365
elem_name: String,
@@ -6395,10 +6395,14 @@ template <typename A>
63956395
"Vertex pulling requires a non-zero buffer stride."
63966396
);
63976397

6398-
if vbm.indexed_by_vertex {
6399-
needs_vertex_id = true;
6400-
} else {
6401-
needs_instance_id = true;
6398+
match vbm.step_mode {
6399+
back::msl::VertexBufferStepMode::Constant => {}
6400+
back::msl::VertexBufferStepMode::ByVertex => {
6401+
needs_vertex_id = true;
6402+
}
6403+
back::msl::VertexBufferStepMode::ByInstance => {
6404+
needs_instance_id = true;
6405+
}
64026406
}
64036407

64046408
let buffer_ty = self.namer.call(format!("vb_{buffer_id}_type").as_str());
@@ -6408,7 +6412,7 @@ template <typename A>
64086412
vbm_resolved.push(VertexBufferMappingResolved {
64096413
id: buffer_id,
64106414
stride: buffer_stride,
6411-
indexed_by_vertex: vbm.indexed_by_vertex,
6415+
step_mode: vbm.step_mode,
64126416
ty_name: buffer_ty,
64136417
param_name: buffer_param,
64146418
elem_name: buffer_elem,
@@ -7199,8 +7203,6 @@ template <typename A>
71997203
}
72007204

72017205
if do_vertex_pulling {
7202-
assert!(needs_vertex_id || needs_instance_id);
7203-
72047206
let mut separator = if is_first_argument {
72057207
is_first_argument = false;
72067208
' '
@@ -7278,16 +7280,22 @@ template <typename A>
72787280

72797281
let idx = &vbm.id;
72807282
let stride = &vbm.stride;
7281-
let index_name = if vbm.indexed_by_vertex {
7282-
if let Some(ref name) = v_existing_id {
7283-
name
7284-
} else {
7285-
&v_id
7283+
let index_name = match vbm.step_mode {
7284+
back::msl::VertexBufferStepMode::Constant => "0",
7285+
back::msl::VertexBufferStepMode::ByVertex => {
7286+
if let Some(ref name) = v_existing_id {
7287+
name
7288+
} else {
7289+
&v_id
7290+
}
7291+
}
7292+
back::msl::VertexBufferStepMode::ByInstance => {
7293+
if let Some(ref name) = i_existing_id {
7294+
name
7295+
} else {
7296+
&i_id
7297+
}
72867298
}
7287-
} else if let Some(ref name) = i_existing_id {
7288-
name
7289-
} else {
7290-
&i_id
72917299
};
72927300
write!(
72937301
self.out,

naga/tests/in/wgsl/msl-vpt-formats-x1.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ attributes = [
4949
{ offset = 640, shader_location = 40, format = "unorm8x4-bgra" },
5050
]
5151
id = 1
52-
indexed_by_vertex = true
52+
step_mode = "ByVertex"
5353
stride = 644

naga/tests/in/wgsl/msl-vpt-formats-x2.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ attributes = [
4949
{ offset = 640, shader_location = 40, format = "unorm8x4-bgra" },
5050
]
5151
id = 1
52-
indexed_by_vertex = true
52+
step_mode = "ByVertex"
5353
stride = 644

naga/tests/in/wgsl/msl-vpt-formats-x3.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ attributes = [
4949
{ offset = 640, shader_location = 40, format = "unorm8x4-bgra" },
5050
]
5151
id = 1
52-
indexed_by_vertex = true
52+
step_mode = "ByVertex"
5353
stride = 644

naga/tests/in/wgsl/msl-vpt-formats-x4.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ attributes = [
4949
{ offset = 640, shader_location = 40, format = "unorm8x4-bgra" },
5050
]
5151
id = 1
52-
indexed_by_vertex = true
52+
step_mode = "ByVertex"
5353
stride = 644

naga/tests/in/wgsl/msl-vpt.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ attributes = [
1010
{ offset = 4, shader_location = 1, format = "Float32x4" },
1111
]
1212
id = 1
13-
indexed_by_vertex = true
13+
step_mode = "ByVertex"
1414
stride = 20
1515

1616
[[msl_pipeline.vertex_buffer_mappings]]
1717
attributes = [{ offset = 0, shader_location = 2, format = "Float32x2" }]
1818
id = 2
19-
indexed_by_vertex = false
19+
step_mode = "ByInstance"
2020
stride = 16

tests/tests/wgpu-gpu/vertex_state.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use wgpu::{
33
vertex_attr_array,
44
};
55
use wgpu_test::{
6-
gpu_test, FailureCase, GpuTestConfiguration, GpuTestInitializer, TestParameters, TestingContext,
6+
gpu_test, GpuTestConfiguration, GpuTestInitializer, TestParameters, TestingContext,
77
};
88

99
pub fn all_tests(vec: &mut Vec<GpuTestInitializer>) {
@@ -12,11 +12,7 @@ pub fn all_tests(vec: &mut Vec<GpuTestInitializer>) {
1212

1313
#[gpu_test]
1414
static SET_ARRAY_STRIDE_TO_0: GpuTestConfiguration = GpuTestConfiguration::new()
15-
.parameters(
16-
TestParameters::default()
17-
.limits(wgpu::Limits::downlevel_defaults())
18-
.expect_fail(FailureCase::backend(wgpu::Backends::METAL)),
19-
)
15+
.parameters(TestParameters::default().limits(wgpu::Limits::downlevel_defaults()))
2016
.run_async(set_array_stride_to_0);
2117

2218
/// Tests that draws using a vertex buffer with stride of 0 works correctly (especially on the

wgpu-hal/src/metal/device.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,15 @@ impl crate::Device for super::Device {
11541154
.try_into()
11551155
.unwrap()
11561156
},
1157-
indexed_by_vertex: (vbl.step_mode == wgt::VertexStepMode::Vertex {}),
1157+
step_mode: match (vbl.array_stride == 0, vbl.step_mode) {
1158+
(true, _) => naga::back::msl::VertexBufferStepMode::Constant,
1159+
(false, wgt::VertexStepMode::Vertex) => {
1160+
naga::back::msl::VertexBufferStepMode::ByVertex
1161+
}
1162+
(false, wgt::VertexStepMode::Instance) => {
1163+
naga::back::msl::VertexBufferStepMode::ByInstance
1164+
}
1165+
},
11581166
attributes,
11591167
});
11601168
}

0 commit comments

Comments
 (0)