diff --git a/CHANGELOG.md b/CHANGELOG.md index 0963757e0..c237ebb82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -141,6 +141,7 @@ By @sagudev in [#10115](https://github.com/gfx-rs/wgpu/pull/10115). - Fix panics when shader `var` size is larger than 256 bytes. By @beicause in [#9725](https://github.com/gfx-rs/wgpu/pull/9725). - Fix a panic in the SPIR-V frontend when a subgroup collective operation (e.g. `OpGroupNonUniformUMin`) or `OpGroupNonUniformBallot` used an argument whose value needed to be spilled to a temporary variable, such as when the argument was computed inside a loop. By @nazar-pc in [#9957](https://github.com/gfx-rs/wgpu/issues/9957). +- Lower `@builtin(instance_index)` in `@any_hit` and `@closest_hit` entry points to SPIR-V's `InstanceId` rather than `InstanceIndex`, which Vulkan only permits in the vertex stage. By @JMS55 in [10154](https://github.com/gfx-rs/wgpu/pull/10154). #### Validation diff --git a/naga/src/back/spv/writer.rs b/naga/src/back/spv/writer.rs index 8f42e8b6f..218cd6914 100644 --- a/naga/src/back/spv/writer.rs +++ b/naga/src/back/spv/writer.rs @@ -3210,7 +3210,12 @@ impl Writer { )?; BuiltIn::CullDistance } - Bi::InstanceIndex => BuiltIn::InstanceIndex, + Bi::InstanceIndex => match stage { + crate::ShaderStage::AnyHit | crate::ShaderStage::ClosestHit => { + BuiltIn::InstanceId + } + _ => BuiltIn::InstanceIndex, + }, Bi::PointSize => BuiltIn::PointSize, Bi::VertexIndex => BuiltIn::VertexIndex, Bi::DrawIndex => { diff --git a/naga/tests/in/wgsl/ray-tracing-pipeline.wgsl b/naga/tests/in/wgsl/ray-tracing-pipeline.wgsl index 8930ffef4..c03fe036a 100644 --- a/naga/tests/in/wgsl/ray-tracing-pipeline.wgsl +++ b/naga/tests/in/wgsl/ray-tracing-pipeline.wgsl @@ -38,6 +38,7 @@ fn closest_hit_main(@builtin(object_ray_origin) origin: vec3, @builtin(obje @closest_hit @incoming_payload(incoming_hit_num) -fn closest_hit_triangle(@builtin(primitive_index) primitive_index: u32) { +fn closest_hit_triangle(@builtin(instance_index) instance_index: u32, @builtin(primitive_index) primitive_index: u32) { incoming_hit_num.hit_num = primitive_index; + incoming_hit_num.hit_num = instance_index; } diff --git a/naga/tests/out/spv/wgsl-ray-tracing-pipeline.spvasm b/naga/tests/out/spv/wgsl-ray-tracing-pipeline.spvasm index da2d2d553..13bf8b8af 100644 --- a/naga/tests/out/spv/wgsl-ray-tracing-pipeline.spvasm +++ b/naga/tests/out/spv/wgsl-ray-tracing-pipeline.spvasm @@ -1,7 +1,7 @@ ; SPIR-V ; Version: 1.4 ; Generator: rspirv -; Bound: 176 +; Bound: 179 OpCapability Shader OpCapability RayTracingKHR OpExtension "SPV_KHR_ray_tracing" @@ -11,7 +11,7 @@ OpEntryPoint RayGenerationKHR %23 "ray_gen_main" %18 %21 %11 %13 OpEntryPoint MissKHR %138 "miss" %130 %133 %135 OpEntryPoint AnyHitKHR %150 "any_hit_main" %141 %144 %146 %148 %15 OpEntryPoint ClosestHitKHR %168 "closest_hit_main" %159 %161 %163 %166 -OpEntryPoint ClosestHitKHR %173 "closest_hit_triangle" %171 %15 +OpEntryPoint ClosestHitKHR %175 "closest_hit_triangle" %171 %173 %15 OpMemberDecorate %4 0 Offset 0 OpMemberDecorate %4 1 Offset 4 OpMemberDecorate %9 0 Offset 0 @@ -35,7 +35,8 @@ OpDecorate %159 BuiltIn ObjectRayOriginKHR OpDecorate %161 BuiltIn ObjectRayDirectionKHR OpDecorate %163 BuiltIn ObjectToWorldKHR OpDecorate %166 BuiltIn WorldToObjectKHR -OpDecorate %171 BuiltIn PrimitiveId +OpDecorate %171 BuiltIn InstanceId +OpDecorate %173 BuiltIn PrimitiveId %2 = OpTypeVoid %3 = OpTypeInt 32 0 %4 = OpTypeStruct %3 %3 @@ -94,6 +95,7 @@ OpDecorate %171 BuiltIn PrimitiveId %163 = OpVariable %164 Input %166 = OpVariable %164 Input %171 = OpVariable %142 Input +%173 = OpVariable %142 Input %49 = OpFunction %2 None %48 %50 = OpFunctionParameter %5 %51 = OpFunctionParameter %9 @@ -227,12 +229,15 @@ OpBranch %169 %169 = OpLabel OpReturn OpFunctionEnd -%173 = OpFunction %2 None %24 +%175 = OpFunction %2 None %24 %170 = OpLabel %172 = OpLoad %3 %171 -OpBranch %174 -%174 = OpLabel -%175 = OpAccessChain %152 %15 %31 -OpStore %175 %172 +%174 = OpLoad %3 %173 +OpBranch %176 +%176 = OpLabel +%177 = OpAccessChain %152 %15 %31 +OpStore %177 %174 +%178 = OpAccessChain %152 %15 %31 +OpStore %178 %172 OpReturn OpFunctionEnd \ No newline at end of file diff --git a/naga/tests/out/spv/wgsl-ray-tracing-pipeline.spvasm.glsl b/naga/tests/out/spv/wgsl-ray-tracing-pipeline.spvasm.glsl index 2d78335db..17443a130 100644 --- a/naga/tests/out/spv/wgsl-ray-tracing-pipeline.spvasm.glsl +++ b/naga/tests/out/spv/wgsl-ray-tracing-pipeline.spvasm.glsl @@ -161,5 +161,6 @@ layout(location = 0) rayPayloadInEXT _4 _15; void main() { _15._m0 = uint(gl_PrimitiveID); + _15._m0 = uint(gl_InstanceID); } diff --git a/naga/tests/out/wgsl/wgsl-ray-tracing-pipeline.wgsl b/naga/tests/out/wgsl/wgsl-ray-tracing-pipeline.wgsl index 6386978e3..e08bedc05 100644 --- a/naga/tests/out/wgsl/wgsl-ray-tracing-pipeline.wgsl +++ b/naga/tests/out/wgsl/wgsl-ray-tracing-pipeline.wgsl @@ -48,7 +48,8 @@ fn closest_hit_main(@builtin(object_ray_origin) origin_1: vec3, @builtin(ob } @closest_hit @incoming_payload(incoming_hit_num) -fn closest_hit_triangle(@builtin(primitive_index) primitive_index: u32) { +fn closest_hit_triangle(@builtin(instance_index) instance_index: u32, @builtin(primitive_index) primitive_index: u32) { incoming_hit_num.hit_num = primitive_index; + incoming_hit_num.hit_num = instance_index; return; }