diff --git a/CHANGELOG.md b/CHANGELOG.md index c06b49882..18a1ac1f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,7 @@ By @cwfitzgerald in [#8999](https://github.com/gfx-rs/wgpu/pull/8999). - `ir::Block::is_empty` - `ir::Block::len` - Changed passthrough shaders to not require an entry point parameter, so that the same shader module may be used in multiple entry points. Also added support for metallib passthrough. By @inner-daemons in #8886. +- Added support for `@builtin(draw_index)` to the vulkan backend. By @inner-daemons in #8883. - Added support for `insert_debug_marker`, `push_debug_group` and `pop_debug_group` on WebGPU. By @evilpie in [#9017](https://github.com/gfx-rs/wgpu/pull/9017). - Added support for `enable primitive_index` and `@builtin(primitive_index)` with support on all platforms. By @inner-daemons in #8879. diff --git a/naga/src/back/glsl/conv.rs b/naga/src/back/glsl/conv.rs index dad78cb73..eea8d5934 100644 --- a/naga/src/back/glsl/conv.rs +++ b/naga/src/back/glsl/conv.rs @@ -88,7 +88,7 @@ pub(in crate::back::glsl) const fn glsl_built_in( } Bi::PointSize => "gl_PointSize", Bi::VertexIndex => "uint(gl_VertexID)", - Bi::DrawID => "gl_DrawID", + Bi::DrawIndex => "gl_DrawID", // fragment Bi::FragDepth => "gl_FragDepth", Bi::PointCoord => "gl_PointCoord", diff --git a/naga/src/back/glsl/features.rs b/naga/src/back/glsl/features.rs index 38cec90b2..6b5956973 100644 --- a/naga/src/back/glsl/features.rs +++ b/naga/src/back/glsl/features.rs @@ -624,7 +624,7 @@ impl Writer<'_, W> { self.features.request(Features::SAMPLE_VARIABLES) } crate::BuiltIn::ViewIndex => self.features.request(Features::MULTI_VIEW), - crate::BuiltIn::InstanceIndex | crate::BuiltIn::DrawID => { + crate::BuiltIn::InstanceIndex | crate::BuiltIn::DrawIndex => { self.features.request(Features::INSTANCE_INDEX) } crate::BuiltIn::Barycentric { .. } => { diff --git a/naga/src/back/hlsl/conv.rs b/naga/src/back/hlsl/conv.rs index d38a7b7d5..f7ad4ffd1 100644 --- a/naga/src/back/hlsl/conv.rs +++ b/naga/src/back/hlsl/conv.rs @@ -182,7 +182,7 @@ impl crate::BuiltIn { Self::BaseInstance | Self::BaseVertex | Self::WorkGroupSize => { return Err(Error::Unimplemented(format!("builtin {self:?}"))) } - Self::PointSize | Self::PointCoord | Self::DrawID => { + Self::PointSize | Self::PointCoord | Self::DrawIndex => { return Err(Error::Custom(format!("Unsupported builtin {self:?}"))) } Self::CullPrimitive => "SV_CullPrimitive", diff --git a/naga/src/back/msl/mod.rs b/naga/src/back/msl/mod.rs index 36da152aa..627706d36 100644 --- a/naga/src/back/msl/mod.rs +++ b/naga/src/back/msl/mod.rs @@ -713,7 +713,7 @@ impl ResolvedBinding { Bi::SubgroupId => "simdgroup_index_in_threadgroup", Bi::SubgroupSize => "threads_per_simdgroup", Bi::SubgroupInvocationId => "thread_index_in_simdgroup", - Bi::CullDistance | Bi::DrawID => { + Bi::CullDistance | Bi::DrawIndex => { return Err(Error::UnsupportedBuiltIn(built_in)) } Bi::CullPrimitive => "primitive_culled", diff --git a/naga/src/back/spv/writer.rs b/naga/src/back/spv/writer.rs index ec73ced5d..af6e6750f 100644 --- a/naga/src/back/spv/writer.rs +++ b/naga/src/back/spv/writer.rs @@ -3077,7 +3077,14 @@ impl Writer { Bi::InstanceIndex => BuiltIn::InstanceIndex, Bi::PointSize => BuiltIn::PointSize, Bi::VertexIndex => BuiltIn::VertexIndex, - Bi::DrawID => BuiltIn::DrawIndex, + Bi::DrawIndex => { + self.use_extension("SPV_KHR_shader_draw_parameters"); + self.require_any( + "`draw_index built-in", + &[spirv::Capability::DrawParameters], + )?; + BuiltIn::DrawIndex + } // fragment Bi::FragDepth => BuiltIn::FragDepth, Bi::PointCoord => BuiltIn::PointCoord, diff --git a/naga/src/back/wgsl/writer.rs b/naga/src/back/wgsl/writer.rs index 6e7b6a229..22209592e 100644 --- a/naga/src/back/wgsl/writer.rs +++ b/naga/src/back/wgsl/writer.rs @@ -286,6 +286,7 @@ impl Writer { mesh_shaders: bool, primitive_index: bool, cooperative_matrix: bool, + draw_index: bool, } let mut needed = RequiredEnabled::default(); @@ -308,6 +309,7 @@ impl Writer { } => { needed.mesh_shaders = true; } + crate::Binding::BuiltIn(crate::BuiltIn::DrawIndex) => needed.draw_index = true, crate::Binding::BuiltIn( crate::BuiltIn::MeshTaskSize | crate::BuiltIn::CullPrimitive @@ -387,6 +389,10 @@ impl Writer { writeln!(self.out, "enable wgpu_mesh_shader;")?; any_written = true; } + if needed.draw_index { + writeln!(self.out, "enable draw_index;")?; + any_written = true; + } if needed.primitive_index { writeln!(self.out, "enable primitive_index;")?; any_written = true; diff --git a/naga/src/common/wgsl/to_wgsl.rs b/naga/src/common/wgsl/to_wgsl.rs index c59a5d954..85ba771a7 100644 --- a/naga/src/common/wgsl/to_wgsl.rs +++ b/naga/src/common/wgsl/to_wgsl.rs @@ -169,6 +169,7 @@ impl TryToWgsl for crate::BuiltIn { Bi::FragDepth => "frag_depth", Bi::FrontFacing => "front_facing", Bi::PrimitiveIndex => "primitive_index", + Bi::DrawIndex => "draw_index", Bi::Barycentric { perspective: true } => "barycentric", Bi::Barycentric { perspective: false } => "barycentric_no_perspective", Bi::SampleIndex => "sample_index", @@ -198,7 +199,6 @@ impl TryToWgsl for crate::BuiltIn { | Bi::BaseVertex | Bi::CullDistance | Bi::PointSize - | Bi::DrawID | Bi::PointCoord | Bi::WorkGroupSize | Bi::RayInvocationId diff --git a/naga/src/front/glsl/variables.rs b/naga/src/front/glsl/variables.rs index adb38b6a5..166c8adf3 100644 --- a/naga/src/front/glsl/variables.rs +++ b/naga/src/front/glsl/variables.rs @@ -207,7 +207,7 @@ impl Frontend { "gl_VertexIndex" => BuiltIn::VertexIndex, "gl_SampleID" => BuiltIn::SampleIndex, "gl_LocalInvocationIndex" => BuiltIn::LocalInvocationIndex, - "gl_DrawID" => BuiltIn::DrawID, + "gl_DrawID" => BuiltIn::DrawIndex, _ => return Ok(None), }; diff --git a/naga/src/front/spv/convert.rs b/naga/src/front/spv/convert.rs index 3dbe8fe54..5592adf58 100644 --- a/naga/src/front/spv/convert.rs +++ b/naga/src/front/spv/convert.rs @@ -141,7 +141,7 @@ pub(super) fn map_builtin(word: spirv::Word, invariant: bool) -> Result crate::BuiltIn::InstanceIndex, Some(Bi::PointSize) => crate::BuiltIn::PointSize, Some(Bi::VertexIndex) => crate::BuiltIn::VertexIndex, - Some(Bi::DrawIndex) => crate::BuiltIn::DrawID, + Some(Bi::DrawIndex) => crate::BuiltIn::DrawIndex, // fragment Some(Bi::FragDepth) => crate::BuiltIn::FragDepth, Some(Bi::PointCoord) => crate::BuiltIn::PointCoord, diff --git a/naga/src/front/wgsl/parse/conv.rs b/naga/src/front/wgsl/parse/conv.rs index 7160ed6d5..a9296928e 100644 --- a/naga/src/front/wgsl/parse/conv.rs +++ b/naga/src/front/wgsl/parse/conv.rs @@ -104,6 +104,7 @@ pub fn map_built_in( "front_facing" => crate::BuiltIn::FrontFacing, "frag_depth" => crate::BuiltIn::FragDepth, "primitive_index" => crate::BuiltIn::PrimitiveIndex, + "draw_index" => crate::BuiltIn::DrawIndex, "barycentric" => crate::BuiltIn::Barycentric { perspective: true }, "barycentric_no_perspective" => crate::BuiltIn::Barycentric { perspective: false }, "sample_index" => crate::BuiltIn::SampleIndex, @@ -154,6 +155,14 @@ pub fn map_built_in( crate::BuiltIn::PrimitiveIndex => { enable_extensions.require(ImplementedEnableExtension::PrimitiveIndex, span)? } + crate::BuiltIn::DrawIndex => { + if !enable_extensions.contains(ImplementedEnableExtension::DrawIndex) { + return Err(Box::new(Error::EnableExtensionNotEnabled { + span, + kind: ImplementedEnableExtension::DrawIndex.into(), + })); + } + } crate::BuiltIn::CullPrimitive | crate::BuiltIn::PointIndex | crate::BuiltIn::LineIndices diff --git a/naga/src/front/wgsl/parse/directive/enable_extension.rs b/naga/src/front/wgsl/parse/directive/enable_extension.rs index 85ea4dbfb..2e1157bdc 100644 --- a/naga/src/front/wgsl/parse/directive/enable_extension.rs +++ b/naga/src/front/wgsl/parse/directive/enable_extension.rs @@ -19,6 +19,7 @@ pub(crate) struct EnableExtensions { f16: bool, clip_distances: bool, wgpu_cooperative_matrix: bool, + draw_index: bool, primitive_index: bool, } @@ -33,6 +34,7 @@ impl EnableExtensions { dual_source_blending: false, clip_distances: false, wgpu_cooperative_matrix: false, + draw_index: false, primitive_index: false, } } @@ -52,6 +54,7 @@ impl EnableExtensions { ImplementedEnableExtension::F16 => &mut self.f16, ImplementedEnableExtension::ClipDistances => &mut self.clip_distances, ImplementedEnableExtension::WgpuCooperativeMatrix => &mut self.wgpu_cooperative_matrix, + ImplementedEnableExtension::DrawIndex => &mut self.draw_index, ImplementedEnableExtension::PrimitiveIndex => &mut self.primitive_index, }; *field = true; @@ -70,6 +73,7 @@ impl EnableExtensions { ImplementedEnableExtension::F16 => self.f16, ImplementedEnableExtension::ClipDistances => self.clip_distances, ImplementedEnableExtension::WgpuCooperativeMatrix => self.wgpu_cooperative_matrix, + ImplementedEnableExtension::DrawIndex => self.draw_index, ImplementedEnableExtension::PrimitiveIndex => self.primitive_index, } } @@ -122,6 +126,7 @@ impl EnableExtension { const COOPERATIVE_MATRIX: &'static str = "wgpu_cooperative_matrix"; const SUBGROUPS: &'static str = "subgroups"; const PRIMITIVE_INDEX: &'static str = "primitive_index"; + const DRAW_INDEX: &'static str = "draw_index"; /// Convert from a sentinel word in WGSL into its associated [`EnableExtension`], if possible. pub(crate) fn from_ident(word: &str, span: Span) -> Result<'_, Self> { @@ -143,6 +148,7 @@ impl EnableExtension { Self::Implemented(ImplementedEnableExtension::WgpuCooperativeMatrix) } Self::SUBGROUPS => Self::Unimplemented(UnimplementedEnableExtension::Subgroups), + Self::DRAW_INDEX => Self::Implemented(ImplementedEnableExtension::DrawIndex), Self::PRIMITIVE_INDEX => Self::Implemented(ImplementedEnableExtension::PrimitiveIndex), _ => return Err(Box::new(Error::UnknownEnableExtension(span, word))), }) @@ -161,6 +167,7 @@ impl EnableExtension { ImplementedEnableExtension::DualSourceBlending => Self::DUAL_SOURCE_BLENDING, ImplementedEnableExtension::F16 => Self::F16, ImplementedEnableExtension::ClipDistances => Self::CLIP_DISTANCES, + ImplementedEnableExtension::DrawIndex => Self::DRAW_INDEX, ImplementedEnableExtension::PrimitiveIndex => Self::PRIMITIVE_INDEX, ImplementedEnableExtension::WgpuRayTracingPipeline => Self::RAY_TRACING_PIPELINE, }, @@ -203,6 +210,8 @@ pub enum ImplementedEnableExtension { WgpuRayTracingPipeline, /// Enables the `wgpu_cooperative_matrix` extension, native only. WgpuCooperativeMatrix, + /// Enables the `draw_index` builtin. Not currently part of the WGSL spec but probably will be at some point. + DrawIndex, /// Enables the `@builtin(primitive_index)` attribute in WGSL. /// /// In the WGSL standard, this corresponds to [`enable primitive-index;`]. @@ -222,6 +231,7 @@ impl ImplementedEnableExtension { Self::WgpuRayQueryVertexReturn, Self::WgpuRayTracingPipeline, Self::WgpuCooperativeMatrix, + Self::DrawIndex, Self::PrimitiveIndex, ]; @@ -242,6 +252,7 @@ impl ImplementedEnableExtension { Self::WgpuRayQueryVertexReturn => C::RAY_HIT_VERTEX_POSITION, Self::WgpuCooperativeMatrix => C::COOPERATIVE_MATRIX, Self::WgpuRayTracingPipeline => C::RAY_TRACING_PIPELINE, + Self::DrawIndex => C::DRAW_INDEX, Self::PrimitiveIndex => C::PRIMITIVE_INDEX, } } diff --git a/naga/src/ir/mod.rs b/naga/src/ir/mod.rs index 70623b4d1..53e3a5343 100644 --- a/naga/src/ir/mod.rs +++ b/naga/src/ir/mod.rs @@ -421,7 +421,7 @@ pub enum BuiltIn { /// Read in vertex shaders VertexIndex, /// Read in vertex & task shaders, or mesh shaders in pipelines without task shaders - DrawID, + DrawIndex, /// Written in fragment shaders FragDepth, diff --git a/naga/src/valid/interface.rs b/naga/src/valid/interface.rs index ceddbe524..6f470b2d8 100644 --- a/naga/src/valid/interface.rs +++ b/naga/src/valid/interface.rs @@ -254,6 +254,7 @@ impl VaryingContext<'_> { | Bi::SubgroupId | Bi::SubgroupSize | Bi::SubgroupInvocationId => Capabilities::SUBGROUP, + Bi::DrawIndex => Capabilities::DRAW_INDEX, _ => Capabilities::empty(), }; if !self.capabilities.contains(required) { @@ -278,7 +279,7 @@ impl VaryingContext<'_> { && !self.output, *ty_inner == Ti::Scalar(crate::Scalar::U32), ), - Bi::DrawID => ( + Bi::DrawIndex => ( // Always allowed in task/vertex stage. Allowed in mesh stage if there is no task stage in the pipeline. (self.stage == St::Vertex || self.stage == St::Task diff --git a/naga/src/valid/mod.rs b/naga/src/valid/mod.rs index 903d21a76..e4d7e179f 100644 --- a/naga/src/valid/mod.rs +++ b/naga/src/valid/mod.rs @@ -207,6 +207,8 @@ bitflags::bitflags! { const PER_VERTEX = 1 << 37; /// Support for ray generation, any hit, closest hit, and miss shaders. const RAY_TRACING_PIPELINE = 1 << 38; + /// Support for draw index builtin + const DRAW_INDEX = 1 << 39; } } diff --git a/naga/tests/in/wgsl/draw-index.toml b/naga/tests/in/wgsl/draw-index.toml new file mode 100644 index 000000000..8c1b118bd --- /dev/null +++ b/naga/tests/in/wgsl/draw-index.toml @@ -0,0 +1,6 @@ +capabilities = "DRAW_INDEX" +targets = "SPIRV | WGSL" + +[spv] +version = [1, 4] +capabilities = ["DrawParameters"] diff --git a/naga/tests/in/wgsl/draw-index.wgsl b/naga/tests/in/wgsl/draw-index.wgsl new file mode 100644 index 000000000..291c10208 --- /dev/null +++ b/naga/tests/in/wgsl/draw-index.wgsl @@ -0,0 +1,10 @@ +enable draw_index; + +struct Input { + @builtin(draw_index) draw_index: u32, +} + +@vertex +fn vertex(input: Input) -> @builtin(position) vec4 { + return vec4(f32(input.draw_index), 1.0, 1.0, 1.0); +} \ No newline at end of file diff --git a/naga/tests/out/spv/wgsl-draw-index.spvasm b/naga/tests/out/spv/wgsl-draw-index.spvasm new file mode 100644 index 000000000..0c4e2b000 --- /dev/null +++ b/naga/tests/out/spv/wgsl-draw-index.spvasm @@ -0,0 +1,36 @@ +; SPIR-V +; Version: 1.4 +; Generator: rspirv +; Bound: 21 +OpCapability Shader +OpCapability DrawParameters +OpExtension "SPV_KHR_shader_draw_parameters" +%1 = OpExtInstImport "GLSL.std.450" +OpMemoryModel Logical GLSL450 +OpEntryPoint Vertex %14 "vertex" %9 %12 +OpMemberDecorate %4 0 Offset 0 +OpDecorate %9 BuiltIn DrawIndex +OpDecorate %12 BuiltIn Position +%2 = OpTypeVoid +%3 = OpTypeInt 32 0 +%4 = OpTypeStruct %3 +%6 = OpTypeFloat 32 +%5 = OpTypeVector %6 4 +%10 = OpTypePointer Input %3 +%9 = OpVariable %10 Input +%13 = OpTypePointer Output %5 +%12 = OpVariable %13 Output +%15 = OpTypeFunction %2 +%16 = OpConstant %6 1 +%14 = OpFunction %2 None %15 +%7 = OpLabel +%11 = OpLoad %3 %9 +%8 = OpCompositeConstruct %4 %11 +OpBranch %17 +%17 = OpLabel +%18 = OpCompositeExtract %3 %8 0 +%19 = OpConvertUToF %6 %18 +%20 = OpCompositeConstruct %5 %19 %16 %16 %16 +OpStore %12 %20 +OpReturn +OpFunctionEnd \ No newline at end of file diff --git a/naga/tests/out/wgsl/wgsl-draw-index.wgsl b/naga/tests/out/wgsl/wgsl-draw-index.wgsl new file mode 100644 index 000000000..5ad9517d2 --- /dev/null +++ b/naga/tests/out/wgsl/wgsl-draw-index.wgsl @@ -0,0 +1,10 @@ +enable draw_index; + +struct Input { + @builtin(draw_index) draw_index: u32, +} + +@vertex +fn vertex(input: Input) -> @builtin(position) vec4 { + return vec4(f32(input.draw_index), 1f, 1f, 1f); +} diff --git a/tests/tests/wgpu-gpu/draw_index.rs b/tests/tests/wgpu-gpu/draw_index.rs new file mode 100644 index 000000000..771363249 --- /dev/null +++ b/tests/tests/wgpu-gpu/draw_index.rs @@ -0,0 +1,193 @@ +use wgpu::{ + Backends, ColorTargetState, ColorWrites, Features, FragmentState, Limits, + MeshPipelineDescriptor, MeshState, RenderPipelineDescriptor, ShaderModuleDescriptor, TaskState, + TextureFormat, VertexState, +}; +use wgpu_test::{ + gpu_test, FailureCase, GpuTestConfiguration, GpuTestInitializer, TestParameters, TestingContext, +}; + +pub fn all_tests(tests: &mut Vec) { + tests.push(DRAW_INDEX); + tests.push(DRAW_INDEX_MESH_NO_TASK); + tests.push(DRAW_INDEX_MESH_TASK); + tests.push(DRAW_INDEX_TASK_NO_MESH); +} + +async fn test(ctx: TestingContext) { + const CODE: &str = "\ +enable draw_index; + +struct Input { + @builtin(draw_index) draw_index: u32, +} + +@vertex +fn vertex(input: Input) -> @builtin(position) vec4 { + return vec4(f32(input.draw_index), 1.0, 1.0, 1.0); +} +@fragment +fn fragment() -> @location(0) vec4 { + return vec4(1.0, 1.0, 1.0, 1.0); +} +"; + let module = ctx.device.create_shader_module(ShaderModuleDescriptor { + label: None, + source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(CODE)), + }); + let _pipeline = ctx + .device + .create_render_pipeline(&RenderPipelineDescriptor { + label: None, + layout: None, + vertex: VertexState { + module: &module, + entry_point: Some("vertex"), + compilation_options: Default::default(), + buffers: &[], + }, + primitive: Default::default(), + depth_stencil: None, + multisample: Default::default(), + fragment: Some(FragmentState { + module: &module, + entry_point: Some("fragment"), + compilation_options: Default::default(), + targets: &[Some(ColorTargetState { + format: TextureFormat::Rgba8Unorm, + blend: None, + write_mask: ColorWrites::all(), + })], + }), + multiview_mask: None, + cache: None, + }); +} + +#[gpu_test] +static DRAW_INDEX: GpuTestConfiguration = GpuTestConfiguration::new() + .parameters(TestParameters::default().features(Features::SHADER_DRAW_INDEX)) + .run_async(test); + +async fn test_mesh(ctx: TestingContext, use_task: bool, mesh_uses_draw_id: bool) { + const CODE: &str = "\ +enable draw_index; +enable wgpu_mesh_shader; + +struct VertexOutput { + @builtin(position) position: vec4, +} +struct PrimitiveOutput { + @builtin(triangle_indices) indices: vec3, +} +struct MeshOutput { + @builtin(vertices) vertices: array, + @builtin(vertex_count) vertex_count: u32, + @builtin(primitives) primitives: array, + @builtin(primitive_count) primitive_count: u32, +} +var mesh_output: MeshOutput; +struct TaskPayload { value: u32 } +var payload: TaskPayload; + +@task +@payload(payload) +@workgroup_size(1) +fn task(@builtin(draw_index) id: u32) -> @builtin(mesh_task_size) vec3 { + return vec3(1, 1, 1); +} + +@mesh(mesh_output) +@payload(payload) +@workgroup_size(1) +fn mesh_ts(MESH_DRAW_INDEX1) { + mesh_output.vertex_count = 0; + mesh_output.primitive_count = 0; +} + +@mesh(mesh_output) +@workgroup_size(1) +fn mesh_no_ts(MESH_DRAW_INDEX2) { + mesh_output.vertex_count = 0; + mesh_output.primitive_count = 0; +} +@fragment +fn fragment() -> @location(0) vec4 { + return vec4(1.0, 1.0, 1.0, 1.0); +} +"; + + let used_code = CODE + .replace( + "MESH_DRAW_INDEX2", + if mesh_uses_draw_id { + "@builtin(draw_index) id: u32" + } else { + "" + }, + ) + .replace( + "MESH_DRAW_INDEX1", + if mesh_uses_draw_id && use_task { + "@builtin(draw_index) id: u32" + } else { + "" + }, + ); + let module = ctx.device.create_shader_module(ShaderModuleDescriptor { + label: None, + source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Owned(used_code)), + }); + let _ = ctx.device.create_mesh_pipeline(&MeshPipelineDescriptor { + label: None, + layout: None, + task: use_task.then_some(TaskState { + module: &module, + entry_point: None, + compilation_options: Default::default(), + }), + mesh: MeshState { + module: &module, + entry_point: Some(if use_task { "mesh_ts" } else { "mesh_no_ts" }), + compilation_options: Default::default(), + }, + primitive: Default::default(), + depth_stencil: None, + multisample: Default::default(), + fragment: Some(FragmentState { + module: &module, + entry_point: None, + compilation_options: Default::default(), + targets: &[Some(ColorTargetState { + format: TextureFormat::Rgba8Unorm, + blend: None, + write_mask: ColorWrites::all(), + })], + }), + multiview: None, + cache: None, + }); +} + +fn mesh_params() -> TestParameters { + // TODO: when support for mesh shaders in naga on dx12/metal lands enable those backends + TestParameters::default() + .features(Features::SHADER_DRAW_INDEX | Features::EXPERIMENTAL_MESH_SHADER) + .skip(FailureCase::backend(Backends::DX12 | Backends::METAL)) + .limits(Limits::defaults().using_recommended_minimum_mesh_shader_values()) +} + +#[gpu_test] +static DRAW_INDEX_TASK_NO_MESH: GpuTestConfiguration = GpuTestConfiguration::new() + .parameters(mesh_params()) + .run_async(async |ctx| test_mesh(ctx, true, false).await); + +#[gpu_test] +static DRAW_INDEX_MESH_NO_TASK: GpuTestConfiguration = GpuTestConfiguration::new() + .parameters(mesh_params()) + .run_async(async |ctx| test_mesh(ctx, false, true).await); + +#[gpu_test] +static DRAW_INDEX_MESH_TASK: GpuTestConfiguration = GpuTestConfiguration::new() + .parameters(mesh_params().expect_fail(FailureCase::always())) + .run_async(async |ctx| test_mesh(ctx, true, true).await); diff --git a/tests/tests/wgpu-gpu/main.rs b/tests/tests/wgpu-gpu/main.rs index 185732ba8..de4994ace 100644 --- a/tests/tests/wgpu-gpu/main.rs +++ b/tests/tests/wgpu-gpu/main.rs @@ -25,6 +25,7 @@ mod compute_pass_ownership; mod create_surface_error; mod device; mod dispatch_workgroups_indirect; +mod draw_index; mod draw_indirect; mod dual_source_blending; mod encoder; @@ -94,6 +95,7 @@ fn all_tests() -> Vec { compute_pass_ownership::all_tests(&mut tests); device::all_tests(&mut tests); dispatch_workgroups_indirect::all_tests(&mut tests); + draw_index::all_tests(&mut tests); draw_indirect::all_tests(&mut tests); dual_source_blending::all_tests(&mut tests); encoder::all_tests(&mut tests); diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index ef5d1223d..03c39f5f6 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -538,6 +538,10 @@ pub fn features_to_naga_capabilities( Caps::PER_VERTEX, features.intersects(wgt::Features::SHADER_PER_VERTEX), ); + caps.set( + Caps::DRAW_INDEX, + features.intersects(wgt::Features::SHADER_DRAW_INDEX), + ); caps } diff --git a/wgpu-core/src/validation.rs b/wgpu-core/src/validation.rs index 6b45c00ee..d3ba43258 100644 --- a/wgpu-core/src/validation.rs +++ b/wgpu-core/src/validation.rs @@ -1513,7 +1513,7 @@ impl Interface { Varying::BuiltIn(naga::BuiltIn::PrimitiveIndex) => { this_stage_primitive_index = true; } - Varying::BuiltIn(naga::BuiltIn::DrawID) => { + Varying::BuiltIn(naga::BuiltIn::DrawIndex) => { has_draw_id = true; } Varying::BuiltIn(_) => {} diff --git a/wgpu-core/src/validation/shader_io_deductions.rs b/wgpu-core/src/validation/shader_io_deductions.rs index 0a16d53b5..678fff5d6 100644 --- a/wgpu-core/src/validation/shader_io_deductions.rs +++ b/wgpu-core/src/validation/shader_io_deductions.rs @@ -75,7 +75,7 @@ impl MaxFragmentShaderInputDeduction { | BuiltIn::InstanceIndex | BuiltIn::PointSize | BuiltIn::VertexIndex - | BuiltIn::DrawID + | BuiltIn::DrawIndex | BuiltIn::FragDepth | BuiltIn::GlobalInvocationId | BuiltIn::LocalInvocationId diff --git a/wgpu-hal/src/vulkan/adapter.rs b/wgpu-hal/src/vulkan/adapter.rs index e960358be..0db307da8 100644 --- a/wgpu-hal/src/vulkan/adapter.rs +++ b/wgpu-hal/src/vulkan/adapter.rs @@ -141,6 +141,8 @@ pub struct PhysicalDeviceFeatures { /// Features provided by `VK_KHR_vulkan_memory_model`, promoted to Vulkan 1.2 vulkan_memory_model: Option>, + + shader_draw_parameters: Option>, } impl PhysicalDeviceFeatures { @@ -226,6 +228,9 @@ impl PhysicalDeviceFeatures { if let Some(ref mut feature) = self.vulkan_memory_model { info = info.push_next(feature); } + if let Some(ref mut feature) = self.shader_draw_parameters { + info = info.push_next(feature); + } info } @@ -606,6 +611,14 @@ impl PhysicalDeviceFeatures { } else { None }, + shader_draw_parameters: if device_api_version >= vk::API_VERSION_1_1 { + Some( + vk::PhysicalDeviceShaderDrawParametersFeatures::default() + .shader_draw_parameters(true), + ) + } else { + None + }, } } @@ -1006,6 +1019,13 @@ impl PhysicalDeviceFeatures { !caps.cooperative_matrix_properties.is_empty(), ); + features.set( + F::SHADER_DRAW_INDEX, + self.shader_draw_parameters + .is_some_and(|a| a.shader_draw_parameters != 0) + || caps.supports_extension(c"VK_KHR_shader_draw_parameters"), + ); + (features, dl_flags) } } @@ -1147,6 +1167,10 @@ impl PhysicalDeviceProperties { // we require that one already. extensions.push(khr::_16bit_storage::NAME); } + + if requested_features.contains(wgt::Features::SHADER_DRAW_INDEX) { + extensions.push(khr::shader_draw_parameters::NAME); + } } if self.device_api_version < vk::API_VERSION_1_2 { @@ -1829,6 +1853,13 @@ impl super::InstanceShared { features2 = features2.push_next(next); } + if capabilities.device_api_version >= vk::API_VERSION_1_1 { + let next = features + .shader_draw_parameters + .insert(vk::PhysicalDeviceShaderDrawParametersFeatures::default()); + features2 = features2.push_next(next); + } + unsafe { get_device_properties.get_physical_device_features2(phd, &mut features2) }; features2.features } else { @@ -2358,6 +2389,10 @@ impl super::Adapter { capabilities.push(spv::Capability::FragmentBarycentricKHR); } + if features.contains(wgt::Features::SHADER_DRAW_INDEX) { + capabilities.push(spv::Capability::DrawParameters); + } + let mut flags = spv::WriterFlags::empty(); flags.set( spv::WriterFlags::DEBUG, diff --git a/wgpu-types/src/features.rs b/wgpu-types/src/features.rs index 81155bac1..755c9f932 100644 --- a/wgpu-types/src/features.rs +++ b/wgpu-types/src/features.rs @@ -1284,6 +1284,19 @@ bitflags_array! { /// /// This is a native only feature. const SHADER_PER_VERTEX = 1 << 58; + + /// Enables shader `draw_index` builtin. + /// + /// Supported platforms: + /// - GLES + /// - Vulkan + /// + /// Potential platforms: + /// - DX12 + /// - Metal + /// + /// This is a native only feature. + const SHADER_DRAW_INDEX = 1 << 59; } /// Features that are not guaranteed to be supported.