From dd5c40b474be6df8bceaafcea0f54fc3c475163a Mon Sep 17 00:00:00 2001 From: JMS55 <47158642+JMS55@users.noreply.github.com> Date: Fri, 4 Sep 2026 16:58:29 -0400 Subject: [PATCH] Support raytracing barycentrics for spirv, and fix spirv 1.4 handling for mesh and raytracing shaders (#10193) --- CHANGELOG.md | 5 + naga/src/back/glsl/conv.rs | 3 +- naga/src/back/hlsl/conv.rs | 3 +- naga/src/back/msl/mod.rs | 3 +- naga/src/back/spv/writer.rs | 33 +- naga/src/back/wgsl/writer.rs | 4 +- naga/src/common/wgsl/to_wgsl.rs | 1 + naga/src/front/wgsl/parse/conv.rs | 1 + naga/src/ir/mod.rs | 4 + naga/src/valid/interface.rs | 17 + naga/tests/in/wgsl/ray-tracing-pipeline.wgsl | 8 +- naga/tests/naga/wgsl_errors.rs | 25 + .../out/spv/wgsl-ray-tracing-pipeline.spvasm | 431 +++++++++--------- .../spv/wgsl-ray-tracing-pipeline.spvasm.glsl | 47 +- .../out/wgsl/wgsl-ray-tracing-pipeline.wgsl | 11 +- wgpu-core/src/validation.rs | 3 + .../src/validation/shader_io_deductions.rs | 3 +- wgpu-hal/src/vulkan/adapter.rs | 27 +- .../documentation/extensions/ray_tracing.rs | 28 ++ 19 files changed, 410 insertions(+), 247 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4e710527..11c851733 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -93,6 +93,10 @@ By @sagudev in [#10109](https://github.com/gfx-rs/wgpu/pull/10109). - `wgpu::WriteOnly<[_]>` now implements `Send`. By @kpreid in [#10163](https://github.com/gfx-rs/wgpu/pull/10163). - Added `Texture::mark_externally_initialized()` to stop a texture from being lazily cleared if it was written to externally (e.g. via `as_hal`). By @R-Cramer4 in [#10075](https://github.com/gfx-rs/wgpu/pull/10075). +#### Naga + +- Add `@builtin(hit_barycentrics)`, a `vec2` readable in `@any_hit` and `@closest_hit` ray tracing pipeline shaders, holding two of the barycentric coordinates of the hit point on the triangle (the third is `1.0 - x - y`). Currently only supported with the SPIR-V backend. By @JMS55 in [#10193](https://github.com/gfx-rs/wgpu/pull/10193). + #### Hal - Add `BufferBinding::buffer`, a public read accessor for the bound buffer, which was previously inaccessible to out-of-tree `wgpu_hal::Api` implementations. By @danlehmann in [#9820](https://github.com/gfx-rs/wgpu/pull/9820). @@ -191,6 +195,7 @@ By @sagudev in [#10109](https://github.com/gfx-rs/wgpu/pull/10109). - Fix `HalCounters::textures` drifting negative: `create_texture` never incremented it while `destroy_texture` always decremented it. By @dustyleary in [#10022](https://github.com/gfx-rs/wgpu/pull/10022). - Add OpenHarmony surface support via `VK_OHOS_surface`. Previously the Vulkan backend could not create a surface on OpenHarmony, leaving GLES as the only usable backend. By @ozongzi in [#9908](https://github.com/gfx-rs/wgpu/pull/9908). - Fix crash on older Vulkan drivers when `poolSizeCount == 0`. By @lucasmerlin in [#10124](https://github.com/gfx-rs/wgpu/pull/10124). +- Request `VK_KHR_spirv_1_4` and raise the generated SPIR-V version to 1.4 when `EXPERIMENTAL_RAY_TRACING_PIPELINES` or `EXPERIMENTAL_MESH_SHADER` is enabled on a pre-Vulkan-1.2 device. Both `SPV_KHR_ray_tracing` and `SPV_EXT_mesh_shader` require SPIR-V 1.4, but shaders were generated as 1.3 there: ray tracing pipelines requested neither the extension nor the version, and mesh shaders requested the extension without raising the version. Naga now rejects ray tracing pipeline shaders targeting below SPIR-V 1.4, as it already did for mesh shaders. By @JMS55 in [#10193](https://github.com/gfx-rs/wgpu/pull/10193). #### Metal diff --git a/naga/src/back/glsl/conv.rs b/naga/src/back/glsl/conv.rs index ebbc1df99..857b6a996 100644 --- a/naga/src/back/glsl/conv.rs +++ b/naga/src/back/glsl/conv.rs @@ -153,7 +153,8 @@ pub(in crate::back::glsl) const fn glsl_built_in( | Bi::RayTCurrentMax | Bi::ObjectToWorld | Bi::WorldToObject - | Bi::HitKind => { + | Bi::HitKind + | Bi::HitBarycentrics => { unimplemented!() } } diff --git a/naga/src/back/hlsl/conv.rs b/naga/src/back/hlsl/conv.rs index 6b3abc625..9ca2559ee 100644 --- a/naga/src/back/hlsl/conv.rs +++ b/naga/src/back/hlsl/conv.rs @@ -210,7 +210,8 @@ impl crate::BuiltIn { | Self::RayTCurrentMax | Self::ObjectToWorld | Self::WorldToObject - | Self::HitKind => unreachable!(), + | Self::HitKind + | Self::HitBarycentrics => unreachable!(), })) } } diff --git a/naga/src/back/msl/mod.rs b/naga/src/back/msl/mod.rs index c16bc637d..06239de15 100644 --- a/naga/src/back/msl/mod.rs +++ b/naga/src/back/msl/mod.rs @@ -705,7 +705,8 @@ impl ResolvedBinding { | Bi::RayTCurrentMax | Bi::ObjectToWorld | Bi::WorldToObject - | Bi::HitKind => unreachable!(), + | Bi::HitKind + | Bi::HitBarycentrics => unreachable!(), }; write!(out, "{name}")?; } diff --git a/naga/src/back/spv/writer.rs b/naga/src/back/spv/writer.rs index 218cd6914..74d1bfe24 100644 --- a/naga/src/back/spv/writer.rs +++ b/naga/src/back/spv/writer.rs @@ -2963,7 +2963,8 @@ impl Writer { /// the interface, and adds appropriate decorations to indicate which /// builtin or location it represents, how it should be interpolated, and so /// on. The `class` argument gives the variable's SPIR-V storage class, - /// which should be either [`Input`] or [`Output`]. + /// which should be either [`Input`] or [`Output`]. The one exception is + /// `hit_barycentrics`, which overrides `class` to `HitAttributeKHR`. /// /// [`Binding`]: crate::Binding /// [`Function`]: crate::Function @@ -2979,6 +2980,25 @@ impl Writer { ty: Handle, binding: &crate::Binding, ) -> Result { + // Triangle barycentrics are supplied as a `HitAttributeKHR` variable, not as an `Input` + // builtin: when a hit group has no intersection shader, the hit attribute an any-hit or + // closest-hit shader reads is a two-component float vector holding the barycentric + // coordinates of the hit, and hit attributes carry no `BuiltIn` decoration. + // + // SPEC: https://registry.khronos.org/vulkan/specs/latest/html/vkspec.html#interfaces-raypipeline + // + // Declaring it here, alongside the entry point's other varyings, works because ray tracing + // requires SPIR-V 1.4, where the `OpEntryPoint` interface lists global variables from every + // storage class rather than just `Input` and `Output`. + // + // SPEC: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpEntryPoint + let class = match *binding { + crate::Binding::BuiltIn(crate::BuiltIn::HitBarycentrics) => { + spirv::StorageClass::HitAttributeKHR + } + _ => class, + }; + let id = self.id_gen.next(); let ty_inner = &ir_module.types[ty].inner; let needs_polyfill = self.needs_f16_polyfill(ty_inner); @@ -3351,6 +3371,8 @@ impl Writer { Bi::ObjectToWorld => BuiltIn::ObjectToWorldKHR, Bi::WorldToObject => BuiltIn::WorldToObjectKHR, Bi::HitKind => BuiltIn::HitKindKHR, + // Read from the entry point's `HitAttributeKHR` variable instead. + Bi::HitBarycentrics => return Ok(BindingDecorations::None), }; use crate::ScalarKind as Sk; @@ -3755,6 +3777,15 @@ impl Writer { } } if has_ray_tracing_pipeline { + // `SPV_KHR_ray_tracing` requires SPIR-V 1.4. That is also what lets `write_varying` + // emit `hit_barycentrics` as a `HitAttributeKHR` variable, since only from 1.4 on does + // the `OpEntryPoint` interface cover storage classes other than `Input` and `Output`. + // + // SPEC: https://github.khronos.org/SPIRV-Registry/extensions/KHR/SPV_KHR_ray_tracing.html + let lang_version = self.lang_version(); + if lang_version.0 <= 1 && lang_version.1 < 4 { + return Err(Error::SpirvVersionTooLow(1, 4)); + } Instruction::extension("SPV_KHR_ray_tracing") .to_words(&mut self.logical_layout.extensions) } diff --git a/naga/src/back/wgsl/writer.rs b/naga/src/back/wgsl/writer.rs index f106fbfc1..a1ec2a877 100644 --- a/naga/src/back/wgsl/writer.rs +++ b/naga/src/back/wgsl/writer.rs @@ -342,7 +342,9 @@ impl Writer { | crate::BuiltIn::RayTmin | crate::BuiltIn::RayTCurrentMax | crate::BuiltIn::ObjectToWorld - | crate::BuiltIn::WorldToObject, + | crate::BuiltIn::WorldToObject + | crate::BuiltIn::HitKind + | crate::BuiltIn::HitBarycentrics, ) => { needed.ray_tracing_pipeline = true; } diff --git a/naga/src/common/wgsl/to_wgsl.rs b/naga/src/common/wgsl/to_wgsl.rs index c3fa20832..06c0502e6 100644 --- a/naga/src/common/wgsl/to_wgsl.rs +++ b/naga/src/common/wgsl/to_wgsl.rs @@ -208,6 +208,7 @@ impl TryToWgsl for crate::BuiltIn { Bi::ObjectToWorld => "object_to_world", Bi::WorldToObject => "world_to_object", Bi::HitKind => "hit_kind", + Bi::HitBarycentrics => "hit_barycentrics", Bi::BaseInstance | Bi::BaseVertex diff --git a/naga/src/front/wgsl/parse/conv.rs b/naga/src/front/wgsl/parse/conv.rs index 4dc277090..10bf0e62d 100644 --- a/naga/src/front/wgsl/parse/conv.rs +++ b/naga/src/front/wgsl/parse/conv.rs @@ -145,6 +145,7 @@ pub fn map_built_in( "object_to_world" => crate::BuiltIn::ObjectToWorld, "world_to_object" => crate::BuiltIn::WorldToObject, "hit_kind" => crate::BuiltIn::HitKind, + "hit_barycentrics" => crate::BuiltIn::HitBarycentrics, _ => return Err(Box::new(Error::UnknownBuiltin(span))), }; match built_in { diff --git a/naga/src/ir/mod.rs b/naga/src/ir/mod.rs index 5bd099a7f..249c9d121 100644 --- a/naga/src/ir/mod.rs +++ b/naga/src/ir/mod.rs @@ -502,6 +502,10 @@ pub enum BuiltIn { /// the intersection function if any, otherwise this is 254 (0xFE) for a /// front facing triangle and 255 (0xFF) for a back facing triangle HitKind, + /// Read in closest hit and any hit shaders, the second and third barycentric + /// coordinates of the hit point on the triangle. The first can be computed as + /// `1.0 - x - y`. Only meaningful if the hit was a triangle. + HitBarycentrics, } /// Number of bytes per scalar. diff --git a/naga/src/valid/interface.rs b/naga/src/valid/interface.rs index 5a17906a8..917ab24fe 100644 --- a/naga/src/valid/interface.rs +++ b/naga/src/valid/interface.rs @@ -640,6 +640,23 @@ impl VaryingContext<'_> { }, *ty_inner == Ti::Scalar(crate::Scalar::U32), ), + Bi::HitBarycentrics => ( + match self.stage { + St::RayGeneration + | St::Miss + | St::Vertex + | St::Fragment + | St::Compute + | St::Mesh + | St::Task => false, + St::AnyHit | St::ClosestHit => !self.output, + }, + *ty_inner + == Ti::Vector { + size: Vs::Bi, + scalar: crate::Scalar::F32, + }, + ), // Validated elsewhere, shouldn't be here Bi::VertexCount | Bi::PrimitiveCount | Bi::Vertices | Bi::Primitives => { (false, true) diff --git a/naga/tests/in/wgsl/ray-tracing-pipeline.wgsl b/naga/tests/in/wgsl/ray-tracing-pipeline.wgsl index c03fe036a..224659ec4 100644 --- a/naga/tests/in/wgsl/ray-tracing-pipeline.wgsl +++ b/naga/tests/in/wgsl/ray-tracing-pipeline.wgsl @@ -27,14 +27,16 @@ fn miss(@builtin(world_ray_origin) origin: vec3, @builtin(world_ray_directi @any_hit @incoming_payload(incoming_hit_num) -fn any_hit_main(@builtin(instance_custom_data) data: u32, @builtin(geometry_index) geo_idx: u32, @builtin(ray_t_current_max) max: f32, @builtin(hit_kind) kind: u32) { +fn any_hit_main(@builtin(instance_custom_data) data: u32, @builtin(geometry_index) geo_idx: u32, @builtin(ray_t_current_max) max: f32, @builtin(hit_kind) kind: u32, @builtin(hit_barycentrics) bary: vec2) { incoming_hit_num.hit_num++; - incoming_hit_num.selected_hit = data; + incoming_hit_num.selected_hit = data + u32(bary.x + bary.y); } @closest_hit @incoming_payload(incoming_hit_num) -fn closest_hit_main(@builtin(object_ray_origin) origin: vec3, @builtin(object_ray_direction) dir: vec3, @builtin(object_to_world) obj_to_world: mat4x3, @builtin(world_to_object) world_to_obj: mat4x3) {} +fn closest_hit_main(@builtin(object_ray_origin) origin: vec3, @builtin(object_ray_direction) dir: vec3, @builtin(object_to_world) obj_to_world: mat4x3, @builtin(world_to_object) world_to_obj: mat4x3, @builtin(hit_barycentrics) bary: vec2) { + incoming_hit_num.selected_hit = u32(1.0 - bary.x - bary.y); +} @closest_hit @incoming_payload(incoming_hit_num) diff --git a/naga/tests/naga/wgsl_errors.rs b/naga/tests/naga/wgsl_errors.rs index 48e71dda8..7bbfcf6a9 100644 --- a/naga/tests/naga/wgsl_errors.rs +++ b/naga/tests/naga/wgsl_errors.rs @@ -5439,6 +5439,7 @@ fn check_ray_tracing_pipeline_bindings() { ("object_to_world", "mat4x3"), ("world_to_object", "mat4x3"), ("hit_kind", "u32"), + ("hit_barycentrics", "vec2"), ] { for stage in ["@compute @workgroup_size(1)", " @vertex", "@fragment"] { check_one_validation!( @@ -5459,6 +5460,30 @@ fn check_ray_tracing_pipeline_bindings() { } } +/// Checks that `hit_barycentrics` is rejected in the ray tracing pipeline stages +/// that have no hit, since it is backed by a hit attribute. +#[test] +fn check_ray_tracing_pipeline_hit_barycentrics_stage() { + for stage in ["@ray_generation", "@miss @incoming_payload(incoming)"] { + check_one_validation!( + &format!( + "enable wgpu_ray_tracing_pipeline; + var incoming: u32; + + {stage} fn main(@builtin(hit_barycentrics) bary: vec2) {{}}" + ), + Err(naga::valid::ValidationError::EntryPoint { + source: naga::valid::EntryPointError::Argument( + 0, + naga::valid::VaryingError::InvalidBuiltInStage(_), + ), + .. + },), + Capabilities::RAY_TRACING_PIPELINE + ); + } +} + /// Checks ray generation stage is invalid without enable extension (other stages require `@incoming_payload` which forces a ray payload which is checked in [`check_ray_tracing_pipeline_payload`]) #[test] fn check_ray_tracing_pipeline_ray_generation() { diff --git a/naga/tests/out/spv/wgsl-ray-tracing-pipeline.spvasm b/naga/tests/out/spv/wgsl-ray-tracing-pipeline.spvasm index 13bf8b8af..44f4e9917 100644 --- a/naga/tests/out/spv/wgsl-ray-tracing-pipeline.spvasm +++ b/naga/tests/out/spv/wgsl-ray-tracing-pipeline.spvasm @@ -1,17 +1,17 @@ ; SPIR-V ; Version: 1.4 ; Generator: rspirv -; Bound: 179 +; Bound: 199 OpCapability Shader OpCapability RayTracingKHR OpExtension "SPV_KHR_ray_tracing" %1 = OpExtInstImport "GLSL.std.450" OpMemoryModel Logical GLSL450 -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 %175 "closest_hit_triangle" %171 %173 %15 +OpEntryPoint RayGenerationKHR %24 "ray_gen_main" %19 %22 %12 %14 +OpEntryPoint MissKHR %139 "miss" %131 %134 %136 +OpEntryPoint AnyHitKHR %154 "any_hit_main" %142 %145 %147 %149 %151 %16 +OpEntryPoint ClosestHitKHR %181 "closest_hit_main" %170 %172 %174 %177 %179 %16 +OpEntryPoint ClosestHitKHR %195 "closest_hit_triangle" %191 %193 %16 OpMemberDecorate %4 0 Offset 0 OpMemberDecorate %4 1 Offset 4 OpMemberDecorate %9 0 Offset 0 @@ -20,23 +20,23 @@ OpMemberDecorate %9 2 Offset 8 OpMemberDecorate %9 3 Offset 12 OpMemberDecorate %9 4 Offset 16 OpMemberDecorate %9 5 Offset 32 -OpDecorate %13 DescriptorSet 0 -OpDecorate %13 Binding 0 -OpDecorate %18 BuiltIn LaunchIdKHR -OpDecorate %21 BuiltIn LaunchSizeKHR -OpDecorate %130 BuiltIn WorldRayOriginKHR -OpDecorate %133 BuiltIn WorldRayDirectionKHR -OpDecorate %135 BuiltIn RayTminKHR -OpDecorate %141 BuiltIn InstanceCustomIndexKHR -OpDecorate %144 BuiltIn RayGeometryIndexKHR -OpDecorate %146 BuiltIn RayTmaxKHR -OpDecorate %148 BuiltIn HitKindKHR -OpDecorate %159 BuiltIn ObjectRayOriginKHR -OpDecorate %161 BuiltIn ObjectRayDirectionKHR -OpDecorate %163 BuiltIn ObjectToWorldKHR -OpDecorate %166 BuiltIn WorldToObjectKHR -OpDecorate %171 BuiltIn InstanceId -OpDecorate %173 BuiltIn PrimitiveId +OpDecorate %14 DescriptorSet 0 +OpDecorate %14 Binding 0 +OpDecorate %19 BuiltIn LaunchIdKHR +OpDecorate %22 BuiltIn LaunchSizeKHR +OpDecorate %131 BuiltIn WorldRayOriginKHR +OpDecorate %134 BuiltIn WorldRayDirectionKHR +OpDecorate %136 BuiltIn RayTminKHR +OpDecorate %142 BuiltIn InstanceCustomIndexKHR +OpDecorate %145 BuiltIn RayGeometryIndexKHR +OpDecorate %147 BuiltIn RayTmaxKHR +OpDecorate %149 BuiltIn HitKindKHR +OpDecorate %170 BuiltIn ObjectRayOriginKHR +OpDecorate %172 BuiltIn ObjectRayDirectionKHR +OpDecorate %174 BuiltIn ObjectToWorldKHR +OpDecorate %177 BuiltIn WorldToObjectKHR +OpDecorate %191 BuiltIn InstanceId +OpDecorate %193 BuiltIn PrimitiveId %2 = OpTypeVoid %3 = OpTypeInt 32 0 %4 = OpTypeStruct %3 %3 @@ -45,199 +45,220 @@ OpDecorate %173 BuiltIn PrimitiveId %7 = OpTypeFloat 32 %8 = OpTypeVector %7 3 %9 = OpTypeStruct %3 %3 %7 %7 %8 %8 -%10 = OpTypeMatrix %8 4 -%12 = OpTypePointer RayPayloadKHR %4 -%11 = OpVariable %12 RayPayloadKHR -%14 = OpTypePointer UniformConstant %5 -%13 = OpVariable %14 UniformConstant -%16 = OpTypePointer IncomingRayPayloadKHR %4 -%15 = OpVariable %16 IncomingRayPayloadKHR -%19 = OpTypePointer Input %6 -%18 = OpVariable %19 Input -%21 = OpVariable %19 Input -%24 = OpTypeFunction %2 -%26 = OpConstantNull %4 -%27 = OpConstant %7 0 -%28 = OpConstant %7 2 -%29 = OpConstant %7 1 -%30 = OpConstantComposite %8 %29 %29 %29 -%31 = OpConstant %3 0 -%32 = OpConstantComposite %8 %27 %29 %27 -%33 = OpConstant %3 255 -%34 = OpConstant %7 0.01 -%35 = OpConstant %7 100 -%36 = OpConstantComposite %8 %27 %27 %27 -%48 = OpTypeFunction %2 %5 %9 -%53 = OpTypeBool -%54 = OpTypeVector %53 3 -%75 = OpConstant %3 256 -%78 = OpConstant %3 512 -%83 = OpConstant %3 16 -%86 = OpConstant %3 32 -%95 = OpConstant %3 1 -%98 = OpConstant %3 2 -%101 = OpConstant %3 64 -%104 = OpConstant %3 128 -%131 = OpTypePointer Input %8 -%130 = OpVariable %131 Input -%133 = OpVariable %131 Input -%136 = OpTypePointer Input %7 -%135 = OpVariable %136 Input -%142 = OpTypePointer Input %3 -%141 = OpVariable %142 Input -%144 = OpVariable %142 Input -%146 = OpVariable %136 Input -%148 = OpVariable %142 Input -%152 = OpTypePointer IncomingRayPayloadKHR %3 -%159 = OpVariable %131 Input -%161 = OpVariable %131 Input -%164 = OpTypePointer Input %10 -%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 -%52 = OpLabel -%55 = OpCompositeExtract %3 %51 0 -%56 = OpCompositeExtract %3 %51 1 -%57 = OpCompositeExtract %7 %51 2 -%58 = OpCompositeExtract %7 %51 3 -%59 = OpCompositeExtract %8 %51 4 -%60 = OpCompositeExtract %8 %51 5 -%61 = OpFOrdLessThanEqual %53 %57 %58 -%62 = OpFOrdGreaterThanEqual %53 %57 %27 -%63 = OpIsInf %54 %59 -%64 = OpAny %53 %63 -%65 = OpIsNan %54 %59 -%66 = OpAny %53 %65 -%67 = OpLogicalOr %53 %66 %64 -%68 = OpLogicalNot %53 %67 -%69 = OpIsInf %54 %60 -%70 = OpAny %53 %69 -%71 = OpIsNan %54 %60 -%72 = OpAny %53 %71 -%73 = OpLogicalOr %53 %72 %70 -%74 = OpLogicalNot %53 %73 -%76 = OpBitwiseAnd %3 %55 %75 -%77 = OpINotEqual %53 %76 %31 -%79 = OpBitwiseAnd %3 %55 %78 -%80 = OpINotEqual %53 %79 %31 -%81 = OpLogicalAnd %53 %80 %77 -%82 = OpLogicalNot %53 %81 -%84 = OpBitwiseAnd %3 %55 %83 -%85 = OpINotEqual %53 %84 %31 -%87 = OpBitwiseAnd %3 %55 %86 -%88 = OpINotEqual %53 %87 %31 -%89 = OpLogicalAnd %53 %88 %77 -%90 = OpLogicalAnd %53 %88 %85 -%91 = OpLogicalAnd %53 %85 %77 -%92 = OpLogicalOr %53 %91 %89 -%93 = OpLogicalOr %53 %92 %90 -%94 = OpLogicalNot %53 %93 -%96 = OpBitwiseAnd %3 %55 %95 -%97 = OpINotEqual %53 %96 %31 -%99 = OpBitwiseAnd %3 %55 %98 -%100 = OpINotEqual %53 %99 %31 -%102 = OpBitwiseAnd %3 %55 %101 -%103 = OpINotEqual %53 %102 %31 -%105 = OpBitwiseAnd %3 %55 %104 -%106 = OpINotEqual %53 %105 %31 -%107 = OpLogicalAnd %53 %106 %97 -%108 = OpLogicalAnd %53 %106 %100 -%109 = OpLogicalAnd %53 %106 %103 -%110 = OpLogicalAnd %53 %103 %97 -%111 = OpLogicalAnd %53 %103 %100 -%112 = OpLogicalAnd %53 %100 %97 -%113 = OpLogicalOr %53 %112 %107 -%114 = OpLogicalOr %53 %113 %108 -%115 = OpLogicalOr %53 %114 %109 -%116 = OpLogicalOr %53 %115 %110 -%117 = OpLogicalOr %53 %116 %111 -%118 = OpLogicalNot %53 %117 -%119 = OpLogicalAnd %53 %118 %61 -%120 = OpLogicalAnd %53 %119 %62 -%121 = OpLogicalAnd %53 %120 %68 -%122 = OpLogicalAnd %53 %121 %74 -%123 = OpLogicalAnd %53 %122 %82 -%124 = OpLogicalAnd %53 %123 %94 -OpSelectionMerge %125 None -OpBranchConditional %124 %127 %126 +%10 = OpTypeVector %7 2 +%11 = OpTypeMatrix %8 4 +%13 = OpTypePointer RayPayloadKHR %4 +%12 = OpVariable %13 RayPayloadKHR +%15 = OpTypePointer UniformConstant %5 +%14 = OpVariable %15 UniformConstant +%17 = OpTypePointer IncomingRayPayloadKHR %4 +%16 = OpVariable %17 IncomingRayPayloadKHR +%20 = OpTypePointer Input %6 +%19 = OpVariable %20 Input +%22 = OpVariable %20 Input +%25 = OpTypeFunction %2 +%27 = OpConstantNull %4 +%28 = OpConstant %7 0 +%29 = OpConstant %7 2 +%30 = OpConstant %7 1 +%31 = OpConstantComposite %8 %30 %30 %30 +%32 = OpConstant %3 0 +%33 = OpConstantComposite %8 %28 %30 %28 +%34 = OpConstant %3 255 +%35 = OpConstant %7 0.01 +%36 = OpConstant %7 100 +%37 = OpConstantComposite %8 %28 %28 %28 +%49 = OpTypeFunction %2 %5 %9 +%54 = OpTypeBool +%55 = OpTypeVector %54 3 +%76 = OpConstant %3 256 +%79 = OpConstant %3 512 +%84 = OpConstant %3 16 +%87 = OpConstant %3 32 +%96 = OpConstant %3 1 +%99 = OpConstant %3 2 +%102 = OpConstant %3 64 +%105 = OpConstant %3 128 +%132 = OpTypePointer Input %8 +%131 = OpVariable %132 Input +%134 = OpVariable %132 Input +%137 = OpTypePointer Input %7 +%136 = OpVariable %137 Input +%143 = OpTypePointer Input %3 +%142 = OpVariable %143 Input +%145 = OpVariable %143 Input +%147 = OpVariable %137 Input +%149 = OpVariable %143 Input +%152 = OpTypePointer HitAttributeKHR %10 +%151 = OpVariable %152 HitAttributeKHR +%156 = OpTypePointer IncomingRayPayloadKHR %3 +%164 = OpConstant %7 4294967000 +%170 = OpVariable %132 Input +%172 = OpVariable %132 Input +%175 = OpTypePointer Input %11 +%174 = OpVariable %175 Input +%177 = OpVariable %175 Input +%179 = OpVariable %152 HitAttributeKHR +%191 = OpVariable %143 Input +%193 = OpVariable %143 Input +%50 = OpFunction %2 None %49 +%51 = OpFunctionParameter %5 +%52 = OpFunctionParameter %9 +%53 = OpLabel +%56 = OpCompositeExtract %3 %52 0 +%57 = OpCompositeExtract %3 %52 1 +%58 = OpCompositeExtract %7 %52 2 +%59 = OpCompositeExtract %7 %52 3 +%60 = OpCompositeExtract %8 %52 4 +%61 = OpCompositeExtract %8 %52 5 +%62 = OpFOrdLessThanEqual %54 %58 %59 +%63 = OpFOrdGreaterThanEqual %54 %58 %28 +%64 = OpIsInf %55 %60 +%65 = OpAny %54 %64 +%66 = OpIsNan %55 %60 +%67 = OpAny %54 %66 +%68 = OpLogicalOr %54 %67 %65 +%69 = OpLogicalNot %54 %68 +%70 = OpIsInf %55 %61 +%71 = OpAny %54 %70 +%72 = OpIsNan %55 %61 +%73 = OpAny %54 %72 +%74 = OpLogicalOr %54 %73 %71 +%75 = OpLogicalNot %54 %74 +%77 = OpBitwiseAnd %3 %56 %76 +%78 = OpINotEqual %54 %77 %32 +%80 = OpBitwiseAnd %3 %56 %79 +%81 = OpINotEqual %54 %80 %32 +%82 = OpLogicalAnd %54 %81 %78 +%83 = OpLogicalNot %54 %82 +%85 = OpBitwiseAnd %3 %56 %84 +%86 = OpINotEqual %54 %85 %32 +%88 = OpBitwiseAnd %3 %56 %87 +%89 = OpINotEqual %54 %88 %32 +%90 = OpLogicalAnd %54 %89 %78 +%91 = OpLogicalAnd %54 %89 %86 +%92 = OpLogicalAnd %54 %86 %78 +%93 = OpLogicalOr %54 %92 %90 +%94 = OpLogicalOr %54 %93 %91 +%95 = OpLogicalNot %54 %94 +%97 = OpBitwiseAnd %3 %56 %96 +%98 = OpINotEqual %54 %97 %32 +%100 = OpBitwiseAnd %3 %56 %99 +%101 = OpINotEqual %54 %100 %32 +%103 = OpBitwiseAnd %3 %56 %102 +%104 = OpINotEqual %54 %103 %32 +%106 = OpBitwiseAnd %3 %56 %105 +%107 = OpINotEqual %54 %106 %32 +%108 = OpLogicalAnd %54 %107 %98 +%109 = OpLogicalAnd %54 %107 %101 +%110 = OpLogicalAnd %54 %107 %104 +%111 = OpLogicalAnd %54 %104 %98 +%112 = OpLogicalAnd %54 %104 %101 +%113 = OpLogicalAnd %54 %101 %98 +%114 = OpLogicalOr %54 %113 %108 +%115 = OpLogicalOr %54 %114 %109 +%116 = OpLogicalOr %54 %115 %110 +%117 = OpLogicalOr %54 %116 %111 +%118 = OpLogicalOr %54 %117 %112 +%119 = OpLogicalNot %54 %118 +%120 = OpLogicalAnd %54 %119 %62 +%121 = OpLogicalAnd %54 %120 %63 +%122 = OpLogicalAnd %54 %121 %69 +%123 = OpLogicalAnd %54 %122 %75 +%124 = OpLogicalAnd %54 %123 %83 +%125 = OpLogicalAnd %54 %124 %95 +OpSelectionMerge %126 None +OpBranchConditional %125 %128 %127 +%128 = OpLabel +OpTraceRayKHR %51 %56 %57 %32 %32 %32 %60 %58 %61 %59 %12 +OpBranch %126 %127 = OpLabel -OpTraceRayKHR %50 %55 %56 %31 %31 %31 %59 %57 %60 %58 %11 -OpBranch %125 +OpBranch %126 %126 = OpLabel -OpBranch %125 -%125 = OpLabel OpReturn OpFunctionEnd -%23 = OpFunction %2 None %24 -%17 = OpLabel -%20 = OpLoad %6 %18 -%22 = OpLoad %6 %21 -%25 = OpLoad %5 %13 -OpBranch %37 -%37 = OpLabel -OpStore %11 %26 -%38 = OpConvertUToF %8 %20 -%39 = OpConvertUToF %8 %22 -%40 = OpFDiv %8 %38 %39 -%41 = OpCompositeExtract %7 %40 0 -%42 = OpCompositeExtract %7 %40 1 -%43 = OpCompositeConstruct %8 %41 %27 %42 -%44 = OpVectorTimesScalar %8 %43 %28 -%45 = OpFSub %8 %44 %30 -%46 = OpFAdd %8 %32 %45 -%47 = OpCompositeConstruct %9 %31 %33 %34 %35 %36 %46 -%128 = OpFunctionCall %2 %49 %25 %47 +%24 = OpFunction %2 None %25 +%18 = OpLabel +%21 = OpLoad %6 %19 +%23 = OpLoad %6 %22 +%26 = OpLoad %5 %14 +OpBranch %38 +%38 = OpLabel +OpStore %12 %27 +%39 = OpConvertUToF %8 %21 +%40 = OpConvertUToF %8 %23 +%41 = OpFDiv %8 %39 %40 +%42 = OpCompositeExtract %7 %41 0 +%43 = OpCompositeExtract %7 %41 1 +%44 = OpCompositeConstruct %8 %42 %28 %43 +%45 = OpVectorTimesScalar %8 %44 %29 +%46 = OpFSub %8 %45 %31 +%47 = OpFAdd %8 %33 %46 +%48 = OpCompositeConstruct %9 %32 %34 %35 %36 %37 %47 +%129 = OpFunctionCall %2 %50 %26 %48 OpReturn OpFunctionEnd -%138 = OpFunction %2 None %24 -%129 = OpLabel -%132 = OpLoad %8 %130 -%134 = OpLoad %8 %133 -%137 = OpLoad %7 %135 -OpBranch %139 -%139 = OpLabel -OpReturn -OpFunctionEnd -%150 = OpFunction %2 None %24 +%139 = OpFunction %2 None %25 +%130 = OpLabel +%133 = OpLoad %8 %131 +%135 = OpLoad %8 %134 +%138 = OpLoad %7 %136 +OpBranch %140 %140 = OpLabel -%143 = OpLoad %3 %141 -%145 = OpLoad %3 %144 -%147 = OpLoad %7 %146 -%149 = OpLoad %3 %148 -OpBranch %151 -%151 = OpLabel -%153 = OpAccessChain %152 %15 %31 -%154 = OpLoad %3 %153 -%155 = OpIAdd %3 %154 %95 -%156 = OpAccessChain %152 %15 %31 -OpStore %156 %155 -%157 = OpAccessChain %152 %15 %95 -OpStore %157 %143 OpReturn OpFunctionEnd -%168 = OpFunction %2 None %24 -%158 = OpLabel -%160 = OpLoad %8 %159 -%162 = OpLoad %8 %161 -%165 = OpLoad %10 %163 -%167 = OpLoad %10 %166 -OpBranch %169 +%154 = OpFunction %2 None %25 +%141 = OpLabel +%144 = OpLoad %3 %142 +%146 = OpLoad %3 %145 +%148 = OpLoad %7 %147 +%150 = OpLoad %3 %149 +%153 = OpLoad %10 %151 +OpBranch %155 +%155 = OpLabel +%157 = OpAccessChain %156 %16 %32 +%158 = OpLoad %3 %157 +%159 = OpIAdd %3 %158 %96 +%160 = OpAccessChain %156 %16 %32 +OpStore %160 %159 +%161 = OpCompositeExtract %7 %153 0 +%162 = OpCompositeExtract %7 %153 1 +%163 = OpFAdd %7 %161 %162 +%165 = OpExtInst %7 %1 FClamp %163 %28 %164 +%166 = OpConvertFToU %3 %165 +%167 = OpIAdd %3 %144 %166 +%168 = OpAccessChain %156 %16 %96 +OpStore %168 %167 +OpReturn +OpFunctionEnd +%181 = OpFunction %2 None %25 %169 = OpLabel +%171 = OpLoad %8 %170 +%173 = OpLoad %8 %172 +%176 = OpLoad %11 %174 +%178 = OpLoad %11 %177 +%180 = OpLoad %10 %179 +OpBranch %182 +%182 = OpLabel +%183 = OpCompositeExtract %7 %180 0 +%184 = OpFSub %7 %30 %183 +%185 = OpCompositeExtract %7 %180 1 +%186 = OpFSub %7 %184 %185 +%187 = OpExtInst %7 %1 FClamp %186 %28 %164 +%188 = OpConvertFToU %3 %187 +%189 = OpAccessChain %156 %16 %96 +OpStore %189 %188 OpReturn OpFunctionEnd -%175 = OpFunction %2 None %24 -%170 = OpLabel -%172 = OpLoad %3 %171 -%174 = OpLoad %3 %173 -OpBranch %176 -%176 = OpLabel -%177 = OpAccessChain %152 %15 %31 -OpStore %177 %174 -%178 = OpAccessChain %152 %15 %31 -OpStore %178 %172 +%195 = OpFunction %2 None %25 +%190 = OpLabel +%192 = OpLoad %3 %191 +%194 = OpLoad %3 %193 +OpBranch %196 +%196 = OpLabel +%197 = OpAccessChain %156 %16 %32 +OpStore %197 %194 +%198 = OpAccessChain %156 %16 %32 +OpStore %198 %192 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 17443a130..7262117ef 100644 --- a/naga/tests/out/spv/wgsl-ray-tracing-pipeline.spvasm.glsl +++ b/naga/tests/out/spv/wgsl-ray-tracing-pipeline.spvasm.glsl @@ -20,21 +20,21 @@ struct _9 vec3 _m5; }; -layout(location = 0) rayPayloadEXT _4 _11; -layout(set = 0, binding = 0) uniform accelerationStructureEXT _13; +layout(location = 0) rayPayloadEXT _4 _12; +layout(set = 0, binding = 0) uniform accelerationStructureEXT _14; -void _49(accelerationStructureEXT _50, _9 _51) +void _50(accelerationStructureEXT _51, _9 _52) { - bool _77 = (_51._m0 & 256u) != 0u; - bool _85 = (_51._m0 & 16u) != 0u; - bool _88 = (_51._m0 & 32u) != 0u; - bool _97 = (_51._m0 & 1u) != 0u; - bool _100 = (_51._m0 & 2u) != 0u; - bool _103 = (_51._m0 & 64u) != 0u; - bool _106 = (_51._m0 & 128u) != 0u; - if (((((((!((((((_100 && _97) || (_106 && _97)) || (_106 && _100)) || (_106 && _103)) || (_103 && _97)) || (_103 && _100))) && (_51._m2 <= _51._m3)) && (_51._m2 >= 0.0)) && (!(any(isnan(_51._m4)) || any(isinf(_51._m4))))) && (!(any(isnan(_51._m5)) || any(isinf(_51._m5))))) && (!(((_51._m0 & 512u) != 0u) && _77))) && (!(((_85 && _77) || (_88 && _77)) || (_88 && _85)))) + bool _78 = (_52._m0 & 256u) != 0u; + bool _86 = (_52._m0 & 16u) != 0u; + bool _89 = (_52._m0 & 32u) != 0u; + bool _98 = (_52._m0 & 1u) != 0u; + bool _101 = (_52._m0 & 2u) != 0u; + bool _104 = (_52._m0 & 64u) != 0u; + bool _107 = (_52._m0 & 128u) != 0u; + if (((((((!((((((_101 && _98) || (_107 && _98)) || (_107 && _101)) || (_107 && _104)) || (_104 && _98)) || (_104 && _101))) && (_52._m2 <= _52._m3)) && (_52._m2 >= 0.0)) && (!(any(isnan(_52._m4)) || any(isinf(_52._m4))))) && (!(any(isnan(_52._m5)) || any(isinf(_52._m5))))) && (!(((_52._m0 & 512u) != 0u) && _78))) && (!(((_86 && _78) || (_89 && _78)) || (_89 && _86)))) { - traceRayEXT(_50, _51._m0, _51._m1, 0u, 0u, 0u, _51._m4, _51._m2, _51._m5, _51._m3, 0); + traceRayEXT(_51, _52._m0, _52._m1, 0u, 0u, 0u, _52._m4, _52._m2, _52._m5, _52._m3, 0); } else { @@ -43,9 +43,9 @@ void _49(accelerationStructureEXT _50, _9 _51) void main() { - _11 = _4(0u, 0u); - vec3 _40 = vec3(gl_LaunchIDEXT) / vec3(gl_LaunchSizeEXT); - _49(_13, _9(0u, 255u, 0.00999999977648258209228515625, 100.0, vec3(0.0), vec3(0.0, 1.0, 0.0) + ((vec3(_40.x, 0.0, _40.y) * 2.0) - vec3(1.0)))); + _12 = _4(0u, 0u); + vec3 _41 = vec3(gl_LaunchIDEXT) / vec3(gl_LaunchSizeEXT); + _50(_14, _9(0u, 255u, 0.00999999977648258209228515625, 100.0, vec3(0.0), vec3(0.0, 1.0, 0.0) + ((vec3(_41.x, 0.0, _41.y) * 2.0) - vec3(1.0)))); } @@ -98,12 +98,13 @@ struct _9 vec3 _m5; }; -layout(location = 0) rayPayloadInEXT _4 _15; +layout(location = 0) rayPayloadInEXT _4 _16; +hitAttributeEXT vec2 _151; void main() { - _15._m0++; - _15._m1 = uint(gl_InstanceCustomIndexEXT); + _16._m0++; + _16._m1 = uint(gl_InstanceCustomIndexEXT) + uint(clamp(_151.x + _151.y, 0.0, 4294967040.0)); } @@ -129,8 +130,12 @@ struct _9 vec3 _m5; }; +layout(location = 0) rayPayloadInEXT _4 _16; +hitAttributeEXT vec2 _179; + void main() { + _16._m1 = uint(clamp((1.0 - _179.x) - _179.y, 0.0, 4294967040.0)); } @@ -156,11 +161,11 @@ struct _9 vec3 _m5; }; -layout(location = 0) rayPayloadInEXT _4 _15; +layout(location = 0) rayPayloadInEXT _4 _16; void main() { - _15._m0 = uint(gl_PrimitiveID); - _15._m0 = uint(gl_InstanceID); + _16._m0 = uint(gl_PrimitiveID); + _16._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 591687f1a..5f21d497f 100644 --- a/naga/tests/out/wgsl/wgsl-ray-tracing-pipeline.wgsl +++ b/naga/tests/out/wgsl/wgsl-ray-tracing-pipeline.wgsl @@ -35,15 +35,16 @@ fn miss(@builtin(world_ray_origin) origin: vec3, @builtin(world_ray_directi } @any_hit @incoming_payload(incoming_hit_num) -fn any_hit_main(@builtin(instance_custom_data) data: u32, @builtin(geometry_index) geo_idx: u32, @builtin(ray_t_current_max) max_: f32, @builtin(hit_kind) kind: u32) { - let _e7 = incoming_hit_num.hit_num; - incoming_hit_num.hit_num = (_e7 + 1u); - incoming_hit_num.selected_hit = data; +fn any_hit_main(@builtin(instance_custom_data) data: u32, @builtin(geometry_index) geo_idx: u32, @builtin(ray_t_current_max) max_: f32, @builtin(hit_kind) kind: u32, @builtin(hit_barycentrics) bary: vec2) { + let _e8 = incoming_hit_num.hit_num; + incoming_hit_num.hit_num = (_e8 + 1u); + incoming_hit_num.selected_hit = (data + u32((bary.x + bary.y))); return; } @closest_hit @incoming_payload(incoming_hit_num) -fn closest_hit_main(@builtin(object_ray_origin) origin_1: vec3, @builtin(object_ray_direction) dir_1: vec3, @builtin(object_to_world) obj_to_world: mat4x3, @builtin(world_to_object) world_to_obj: mat4x3) { +fn closest_hit_main(@builtin(object_ray_origin) origin_1: vec3, @builtin(object_ray_direction) dir_1: vec3, @builtin(object_to_world) obj_to_world: mat4x3, @builtin(world_to_object) world_to_obj: mat4x3, @builtin(hit_barycentrics) bary_1: vec2) { + incoming_hit_num.selected_hit = u32(((1f - bary_1.x) - bary_1.y)); return; } diff --git a/wgpu-core/src/validation.rs b/wgpu-core/src/validation.rs index 905ff7209..f6cdac80f 100644 --- a/wgpu-core/src/validation.rs +++ b/wgpu-core/src/validation.rs @@ -225,6 +225,7 @@ enum BuiltIn { ObjectToWorld, WorldToObject, HitKind, + HitBarycentrics, } impl BuiltIn { @@ -281,6 +282,7 @@ impl BuiltIn { Self::ObjectToWorld => naga::BuiltIn::ObjectToWorld, Self::WorldToObject => naga::BuiltIn::WorldToObject, Self::HitKind => naga::BuiltIn::HitKind, + Self::HitBarycentrics => naga::BuiltIn::HitBarycentrics, } } } @@ -1327,6 +1329,7 @@ impl Interface { naga::BuiltIn::ObjectToWorld => BuiltIn::ObjectToWorld, naga::BuiltIn::WorldToObject => BuiltIn::WorldToObject, naga::BuiltIn::HitKind => BuiltIn::HitKind, + naga::BuiltIn::HitBarycentrics => BuiltIn::HitBarycentrics, }), None => { log::error!("Missing binding for a varying"); diff --git a/wgpu-core/src/validation/shader_io_deductions.rs b/wgpu-core/src/validation/shader_io_deductions.rs index 140ba42bc..d0142c0a3 100644 --- a/wgpu-core/src/validation/shader_io_deductions.rs +++ b/wgpu-core/src/validation/shader_io_deductions.rs @@ -120,7 +120,8 @@ impl MaxFragmentShaderInputDeduction { | BuiltIn::RayTCurrentMax | BuiltIn::ObjectToWorld | BuiltIn::WorldToObject - | BuiltIn::HitKind => return None, + | BuiltIn::HitKind + | BuiltIn::HitBarycentrics => return None, })) } } diff --git a/wgpu-hal/src/vulkan/adapter.rs b/wgpu-hal/src/vulkan/adapter.rs index c7a0061ad..1c84bd2c8 100644 --- a/wgpu-hal/src/vulkan/adapter.rs +++ b/wgpu-hal/src/vulkan/adapter.rs @@ -1319,7 +1319,11 @@ impl PhysicalDeviceProperties { extensions.push(khr::shader_float16_int8::NAME); } - if requested_features.intersects(wgt::Features::EXPERIMENTAL_MESH_SHADER) { + // `SPV_EXT_mesh_shader` and `SPV_KHR_ray_tracing` both require SPIR-V 1.4. + if requested_features.intersects( + wgt::Features::EXPERIMENTAL_MESH_SHADER + | wgt::Features::EXPERIMENTAL_RAY_TRACING_PIPELINES, + ) { extensions.push(khr::spirv_1_4::NAME); } @@ -2903,14 +2907,23 @@ impl super::Adapter { capabilities.extend(&[spv::Capability::Int8]); } spv::Options { - lang_version: match self.phd_capabilities.device_api_version { + lang_version: { // Use maximum supported SPIR-V version according to // . - vk::API_VERSION_1_0..vk::API_VERSION_1_1 => (1, 0), - vk::API_VERSION_1_1..vk::API_VERSION_1_2 => (1, 3), - vk::API_VERSION_1_2..vk::API_VERSION_1_3 => (1, 5), - vk::API_VERSION_1_3.. => (1, 6), - _ => unreachable!(), + let version = match self.phd_capabilities.device_api_version { + vk::API_VERSION_1_0..vk::API_VERSION_1_1 => (1, 0), + vk::API_VERSION_1_1..vk::API_VERSION_1_2 => (1, 3), + vk::API_VERSION_1_2..vk::API_VERSION_1_3 => (1, 5), + vk::API_VERSION_1_3.. => (1, 6), + _ => unreachable!(), + }; + // `VK_KHR_spirv_1_4` raises that ceiling on pre-1.2 devices, which + // both mesh shaders and ray tracing pipelines need it to do. + if enabled_extensions.contains(&khr::spirv_1_4::NAME) { + version.max((1, 4)) + } else { + version + } }, flags, capabilities: Some(capabilities.iter().cloned().collect()), diff --git a/wgpu/src/documentation/extensions/ray_tracing.rs b/wgpu/src/documentation/extensions/ray_tracing.rs index 9976c7516..8688bdcf5 100644 --- a/wgpu/src/documentation/extensions/ray_tracing.rs +++ b/wgpu/src/documentation/extensions/ray_tracing.rs @@ -393,6 +393,34 @@ fn ch() {} fn miss() {} ``` +Ray Tracing pipeline-specific builtins + +| Builtin | Type | Stages | Description | +| ---------------------- | ------------- | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `ray_invocation_id` | `vec3` | all | The id of this ray within the rays created. | +| `num_ray_invocations` | `vec3` | all | The number of rays created. | +| `world_ray_origin` | `vec3` | any hit, closest hit, miss | The origin of the ray in world space. | +| `world_ray_direction` | `vec3` | any hit, closest hit, miss | The direction of the ray in world space. | +| `ray_t_min` | `f32` | any hit, closest hit, miss | The `t_min` given in the `RayDesc`. | +| `ray_t_current_max` | `f32` | any hit, closest hit, miss | The closest committed hit if there is one, otherwise the `t_max` in the `RayDesc`. | +| `object_ray_origin` | `vec3` | any hit, closest hit | The origin of the ray in the space of the hit object. | +| `object_ray_direction` | `vec3` | any hit, closest hit | The direction of the ray in the space of the hit object. | +| `instance_custom_data` | `u32` | any hit, closest hit | Corresponds to `TlasInstance::custom_data` for the intersected object. | +| `geometry_index` | `u32` | any hit, closest hit | The index of the geometry in the `Blas`. | +| `object_to_world` | `mat4x3` | any hit, closest hit | Matrix for converting from the space of the hit object to world-space. | +| `world_to_object` | `mat4x3` | any hit, closest hit | Matrix for converting from world-space to the space of the hit object. | +| `hit_kind` | `u32` | any hit, closest hit | The kind of hit: 254 (0xFE) for a front facing triangle, 255 (0xFF) for a back facing one. | +| `hit_barycentrics` | `vec2` | any hit, closest hit | Two of the barycentric coordinates of the hit point, the first is `1.0 - x - y`, followed by the two others (only allowed if the hit object was a triangle). | + +Builtins usable in ray tracing pipelines that are not specific to them + +| Builtin | Type | Stages | Description | +| ------------------------ | ----- | -------------------- | ---------------------------------------------------------------------------------------------- | +| `instance_index` | `u32` | any hit, closest hit | The index of the intersected instance in the `Tlas`. | +| `primitive_index` | `u32` | any hit, closest hit | The index of the intersected primitive within the geometry, requires `enable primitive_index`. | +| `subgroup_size` | `u32` | all | The number of invocations in the subgroup, requires `enable subgroups`. | +| `subgroup_invocation_id` | `u32` | all | The id of this invocation within the subgroup, requires `enable subgroups`. | + ### Acceleration structure tags These are tags that can be added to a acceleration structure (`acceleration_structure` ->