diff --git a/CHANGELOG.md b/CHANGELOG.md index 51dc48219..42c9c5dcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,6 +76,7 @@ By @cwfitzgerald in [#8999](https://github.com/gfx-rs/wgpu/pull/8999). #### naga - Initial wgsl-in ray tracing pipelines. By @Vecvec in [#8570](https://github.com/gfx-rs/wgpu/pull/8570). +- wgsl-out ray tracing pipelines. By @Vecvec in [#8970](https://github.com/gfx-rs/wgpu/pull/8970). - Allow parsing shaders which make use of `SPV_KHR_non_semantic_info` for debug info. Also removes `naga::front::spv::SUPPORTED_EXT_SETS`. By @inner-daemons in #8827. #### GLES diff --git a/naga/src/back/wgsl/writer.rs b/naga/src/back/wgsl/writer.rs index 039bcc156..47c0e7215 100644 --- a/naga/src/back/wgsl/writer.rs +++ b/naga/src/back/wgsl/writer.rs @@ -36,6 +36,7 @@ enum Attribute { MeshStage(String), TaskPayload(String), PerPrimitive, + IncomingRayPayload(String), } /// The WGSL form that `write_expr_with_indirection` should use to render a Naga @@ -243,10 +244,17 @@ impl Writer { Attribute::WorkGroupSize(ep.workgroup_size), ] } - ShaderStage::RayGeneration - | ShaderStage::AnyHit - | ShaderStage::ClosestHit - | ShaderStage::Miss => unreachable!(), + ShaderStage::RayGeneration => vec![Attribute::Stage(ShaderStage::RayGeneration)], + ShaderStage::AnyHit | ShaderStage::ClosestHit | ShaderStage::Miss => { + let payload_name = module.global_variables[ep.incoming_ray_payload.unwrap()] + .name + .clone() + .unwrap(); + vec![ + Attribute::Stage(ep.stage), + Attribute::IncomingRayPayload(payload_name), + ] + } }; self.write_attributes(&attributes)?; // Add a newline after attribute @@ -287,6 +295,7 @@ impl Writer { primitive_index: bool, cooperative_matrix: bool, draw_index: bool, + ray_tracing_pipeline: bool, } let mut needed = RequiredEnabled { mesh_shaders: module.uses_mesh_shaders(), @@ -313,6 +322,22 @@ impl Writer { needed.mesh_shaders = true; } crate::Binding::BuiltIn(crate::BuiltIn::DrawIndex) => needed.draw_index = true, + crate::Binding::BuiltIn( + crate::BuiltIn::RayInvocationId + | crate::BuiltIn::NumRayInvocations + | crate::BuiltIn::InstanceCustomData + | crate::BuiltIn::GeometryIndex + | crate::BuiltIn::WorldRayOrigin + | crate::BuiltIn::WorldRayDirection + | crate::BuiltIn::ObjectRayOrigin + | crate::BuiltIn::ObjectRayDirection + | crate::BuiltIn::RayTmin + | crate::BuiltIn::RayTCurrentMax + | crate::BuiltIn::ObjectToWorld + | crate::BuiltIn::WorldToObject, + ) => { + needed.ray_tracing_pipeline = true; + } _ => {} }; @@ -332,6 +357,9 @@ impl Writer { TypeInner::CooperativeMatrix { .. } => { needed.cooperative_matrix = true; } + TypeInner::AccelerationStructure { .. } => { + needed.ray_tracing_pipeline = true; + } _ => {} } } @@ -350,6 +378,44 @@ impl Writer { } } + if module.global_variables.iter().any(|gv| { + gv.1.space == crate::AddressSpace::IncomingRayPayload + || gv.1.space == crate::AddressSpace::RayPayload + }) { + needed.ray_tracing_pipeline = true; + } + + if module.entry_points.iter().any(|ep| { + matches!( + ep.stage, + ShaderStage::RayGeneration + | ShaderStage::AnyHit + | ShaderStage::ClosestHit + | ShaderStage::Miss + ) + }) { + needed.ray_tracing_pipeline = true; + } + + if module.global_variables.iter().any(|gv| { + gv.1.space == crate::AddressSpace::IncomingRayPayload + || gv.1.space == crate::AddressSpace::RayPayload + }) { + needed.ray_tracing_pipeline = true; + } + + if module.entry_points.iter().any(|ep| { + matches!( + ep.stage, + ShaderStage::RayGeneration + | ShaderStage::AnyHit + | ShaderStage::ClosestHit + | ShaderStage::Miss + ) + }) { + needed.ray_tracing_pipeline = true; + } + // Write required declarations let mut any_written = false; if needed.f16 { @@ -380,6 +446,10 @@ impl Writer { writeln!(self.out, "enable wgpu_cooperative_matrix;")?; any_written = true; } + if needed.ray_tracing_pipeline { + writeln!(self.out, "enable wgpu_ray_tracing_pipeline;")?; + any_written = true; + } if any_written { // Empty line for readability writeln!(self.out)?; @@ -501,10 +571,10 @@ impl Writer { ShaderStage::Task => "task", //Handled by another variant in the Attribute enum, so this code should never be hit. ShaderStage::Mesh => unreachable!(), - ShaderStage::RayGeneration - | ShaderStage::AnyHit - | ShaderStage::ClosestHit - | ShaderStage::Miss => unreachable!(), + ShaderStage::RayGeneration => "ray_generation", + ShaderStage::AnyHit => "any_hit", + ShaderStage::ClosestHit => "closest_hit", + ShaderStage::Miss => "miss", }; write!(self.out, "@{stage_str} ")?; @@ -542,6 +612,9 @@ impl Writer { write!(self.out, "@payload({payload_name}) ")?; } Attribute::PerPrimitive => write!(self.out, "@per_primitive ")?, + Attribute::IncomingRayPayload(ref payload_name) => { + write!(self.out, "@incoming_payload({payload_name}) ")?; + } }; } Ok(()) @@ -1103,7 +1176,21 @@ impl Writer { self.write_expr(module, data.stride, func_ctx)?; writeln!(self.out, ");")? } - Statement::RayPipelineFunction(_) => unreachable!(), + Statement::RayPipelineFunction(fun) => match fun { + crate::RayPipelineFunction::TraceRay { + acceleration_structure, + descriptor, + payload, + } => { + write!(self.out, "{level}traceRay(")?; + self.write_expr(module, acceleration_structure, func_ctx)?; + write!(self.out, ", ")?; + self.write_expr(module, descriptor, func_ctx)?; + write!(self.out, ", ")?; + self.write_expr(module, payload, func_ctx)?; + writeln!(self.out, ");")? + } + }, } Ok(()) diff --git a/naga/src/common/wgsl/to_wgsl.rs b/naga/src/common/wgsl/to_wgsl.rs index 85ba771a7..abf3fe593 100644 --- a/naga/src/common/wgsl/to_wgsl.rs +++ b/naga/src/common/wgsl/to_wgsl.rs @@ -195,25 +195,26 @@ impl TryToWgsl for crate::BuiltIn { Bi::PrimitiveCount => "primitive_count", Bi::CullPrimitive => "cull_primitive", + Bi::RayInvocationId => "ray_invocation_id", + Bi::NumRayInvocations => "num_ray_invocations", + Bi::InstanceCustomData => "instance_custom_data", + Bi::GeometryIndex => "geometry_index", + Bi::WorldRayOrigin => "world_ray_origin", + Bi::WorldRayDirection => "world_ray_direction", + Bi::ObjectRayOrigin => "object_ray_origin", + Bi::ObjectRayDirection => "object_ray_direction", + Bi::RayTmin => "ray_t_min", + Bi::RayTCurrentMax => "ray_t_current_max", + Bi::ObjectToWorld => "object_to_world", + Bi::WorldToObject => "world_to_object", + Bi::HitKind => "hit_kind", + Bi::BaseInstance | Bi::BaseVertex | Bi::CullDistance | Bi::PointSize | Bi::PointCoord - | Bi::WorkGroupSize - | Bi::RayInvocationId - | Bi::NumRayInvocations - | Bi::InstanceCustomData - | Bi::GeometryIndex - | Bi::WorldRayOrigin - | Bi::WorldRayDirection - | Bi::ObjectRayOrigin - | Bi::ObjectRayDirection - | Bi::RayTmin - | Bi::RayTCurrentMax - | Bi::ObjectToWorld - | Bi::WorldToObject - | Bi::HitKind => return None, + | Bi::WorkGroupSize => return None, }) } } @@ -387,7 +388,8 @@ pub const fn address_space_str( As::Handle => return (None, None), As::Function => "function", As::TaskPayload => "task_payload", - As::IncomingRayPayload | As::RayPayload => return (None, None), + As::IncomingRayPayload => "incoming_ray_payload", + As::RayPayload => "ray_payload", }), None, ) diff --git a/naga/tests/in/wgsl/ray-tracing-pipeline.toml b/naga/tests/in/wgsl/ray-tracing-pipeline.toml index 58be3448f..89da381fd 100644 --- a/naga/tests/in/wgsl/ray-tracing-pipeline.toml +++ b/naga/tests/in/wgsl/ray-tracing-pipeline.toml @@ -1,2 +1,2 @@ capabilities = "RAY_TRACING_PIPELINE" -targets = "IR" +targets = "WGSL" diff --git a/naga/tests/out/ir/wgsl-ray-tracing-pipeline.compact.ron b/naga/tests/out/ir/wgsl-ray-tracing-pipeline.compact.ron deleted file mode 100644 index 5a9dac09e..000000000 --- a/naga/tests/out/ir/wgsl-ray-tracing-pipeline.compact.ron +++ /dev/null @@ -1,350 +0,0 @@ -( - types: [ - ( - name: None, - inner: Scalar(( - kind: Uint, - width: 4, - )), - ), - ( - name: Some("HitCounters"), - inner: Struct( - members: [ - ( - name: Some("hit_num"), - ty: 0, - binding: None, - offset: 0, - ), - ( - name: Some("selected_hit"), - ty: 0, - binding: None, - offset: 4, - ), - ], - span: 8, - ), - ), - ( - name: None, - inner: AccelerationStructure( - vertex_return: false, - ), - ), - ( - name: None, - inner: Scalar(( - kind: Float, - width: 4, - )), - ), - ( - name: None, - inner: Vector( - size: Tri, - scalar: ( - kind: Float, - width: 4, - ), - ), - ), - ( - name: Some("RayDesc"), - inner: Struct( - members: [ - ( - name: Some("flags"), - ty: 0, - binding: None, - offset: 0, - ), - ( - name: Some("cull_mask"), - ty: 0, - binding: None, - offset: 4, - ), - ( - name: Some("tmin"), - ty: 3, - binding: None, - offset: 8, - ), - ( - name: Some("tmax"), - ty: 3, - binding: None, - offset: 12, - ), - ( - name: Some("origin"), - ty: 4, - binding: None, - offset: 16, - ), - ( - name: Some("dir"), - ty: 4, - binding: None, - offset: 32, - ), - ], - span: 48, - ), - ), - ], - special_types: ( - ray_desc: Some(5), - ray_intersection: None, - ray_vertex_return: None, - external_texture_params: None, - external_texture_transfer_function: None, - predeclared_types: {}, - ), - constants: [], - overrides: [], - global_variables: [ - ( - name: Some("hit_num"), - space: RayPayload, - binding: None, - ty: 1, - init: None, - ), - ( - name: Some("acc_struct"), - space: Handle, - binding: Some(( - group: 0, - binding: 0, - )), - ty: 2, - init: None, - ), - ( - name: Some("incoming_hit_num"), - space: IncomingRayPayload, - binding: None, - ty: 1, - init: None, - ), - ], - global_expressions: [], - functions: [], - entry_points: [ - ( - name: "ray_gen_main", - stage: RayGeneration, - early_depth_test: None, - workgroup_size: (0, 0, 0), - workgroup_size_overrides: None, - function: ( - name: Some("ray_gen_main"), - arguments: [], - result: None, - local_variables: [], - expressions: [ - GlobalVariable(0), - ZeroValue(1), - GlobalVariable(1), - Literal(U32(0)), - Literal(U32(255)), - Literal(F32(0.01)), - Literal(F32(100.0)), - Literal(F32(0.0)), - Splat( - size: Tri, - value: 7, - ), - Literal(F32(0.0)), - Literal(F32(1.0)), - Literal(F32(0.0)), - Compose( - ty: 4, - components: [ - 9, - 10, - 11, - ], - ), - Compose( - ty: 5, - components: [ - 3, - 4, - 5, - 6, - 8, - 12, - ], - ), - GlobalVariable(0), - ], - named_expressions: {}, - body: [ - Store( - pointer: 0, - value: 1, - ), - Emit(( - start: 0, - end: 0, - )), - Emit(( - start: 0, - end: 0, - )), - Emit(( - start: 8, - end: 9, - )), - Emit(( - start: 12, - end: 14, - )), - RayPipelineFunction(TraceRay( - acceleration_structure: 2, - descriptor: 13, - payload: 14, - )), - Return( - value: None, - ), - ], - diagnostic_filter_leaf: None, - ), - mesh_info: None, - task_payload: None, - incoming_ray_payload: None, - ), - ( - name: "miss", - stage: Miss, - early_depth_test: None, - workgroup_size: (0, 0, 0), - workgroup_size_overrides: None, - function: ( - name: Some("miss"), - arguments: [], - result: None, - local_variables: [], - expressions: [], - named_expressions: {}, - body: [ - Return( - value: None, - ), - ], - diagnostic_filter_leaf: None, - ), - mesh_info: None, - task_payload: None, - incoming_ray_payload: Some(2), - ), - ( - name: "any_hit_main", - stage: AnyHit, - early_depth_test: None, - workgroup_size: (0, 0, 0), - workgroup_size_overrides: None, - function: ( - name: Some("any_hit_main"), - arguments: [], - result: None, - local_variables: [], - expressions: [ - GlobalVariable(2), - AccessIndex( - base: 0, - index: 0, - ), - Literal(U32(1)), - Load( - pointer: 1, - ), - Binary( - op: Add, - left: 3, - right: 2, - ), - GlobalVariable(2), - AccessIndex( - base: 5, - index: 1, - ), - GlobalVariable(2), - AccessIndex( - base: 7, - index: 0, - ), - Load( - pointer: 8, - ), - ], - named_expressions: {}, - body: [ - Emit(( - start: 1, - end: 2, - )), - Emit(( - start: 3, - end: 5, - )), - Store( - pointer: 1, - value: 4, - ), - Emit(( - start: 6, - end: 7, - )), - Emit(( - start: 8, - end: 10, - )), - Store( - pointer: 6, - value: 9, - ), - Return( - value: None, - ), - ], - diagnostic_filter_leaf: None, - ), - mesh_info: None, - task_payload: None, - incoming_ray_payload: Some(2), - ), - ( - name: "closest_hit_main", - stage: ClosestHit, - early_depth_test: None, - workgroup_size: (0, 0, 0), - workgroup_size_overrides: None, - function: ( - name: Some("closest_hit_main"), - arguments: [], - result: None, - local_variables: [], - expressions: [], - named_expressions: {}, - body: [ - Return( - value: None, - ), - ], - diagnostic_filter_leaf: None, - ), - mesh_info: None, - task_payload: None, - incoming_ray_payload: Some(2), - ), - ], - diagnostic_filters: [], - diagnostic_filter_leaf: None, - doc_comments: None, -) \ No newline at end of file diff --git a/naga/tests/out/ir/wgsl-ray-tracing-pipeline.ron b/naga/tests/out/ir/wgsl-ray-tracing-pipeline.ron deleted file mode 100644 index 5a9dac09e..000000000 --- a/naga/tests/out/ir/wgsl-ray-tracing-pipeline.ron +++ /dev/null @@ -1,350 +0,0 @@ -( - types: [ - ( - name: None, - inner: Scalar(( - kind: Uint, - width: 4, - )), - ), - ( - name: Some("HitCounters"), - inner: Struct( - members: [ - ( - name: Some("hit_num"), - ty: 0, - binding: None, - offset: 0, - ), - ( - name: Some("selected_hit"), - ty: 0, - binding: None, - offset: 4, - ), - ], - span: 8, - ), - ), - ( - name: None, - inner: AccelerationStructure( - vertex_return: false, - ), - ), - ( - name: None, - inner: Scalar(( - kind: Float, - width: 4, - )), - ), - ( - name: None, - inner: Vector( - size: Tri, - scalar: ( - kind: Float, - width: 4, - ), - ), - ), - ( - name: Some("RayDesc"), - inner: Struct( - members: [ - ( - name: Some("flags"), - ty: 0, - binding: None, - offset: 0, - ), - ( - name: Some("cull_mask"), - ty: 0, - binding: None, - offset: 4, - ), - ( - name: Some("tmin"), - ty: 3, - binding: None, - offset: 8, - ), - ( - name: Some("tmax"), - ty: 3, - binding: None, - offset: 12, - ), - ( - name: Some("origin"), - ty: 4, - binding: None, - offset: 16, - ), - ( - name: Some("dir"), - ty: 4, - binding: None, - offset: 32, - ), - ], - span: 48, - ), - ), - ], - special_types: ( - ray_desc: Some(5), - ray_intersection: None, - ray_vertex_return: None, - external_texture_params: None, - external_texture_transfer_function: None, - predeclared_types: {}, - ), - constants: [], - overrides: [], - global_variables: [ - ( - name: Some("hit_num"), - space: RayPayload, - binding: None, - ty: 1, - init: None, - ), - ( - name: Some("acc_struct"), - space: Handle, - binding: Some(( - group: 0, - binding: 0, - )), - ty: 2, - init: None, - ), - ( - name: Some("incoming_hit_num"), - space: IncomingRayPayload, - binding: None, - ty: 1, - init: None, - ), - ], - global_expressions: [], - functions: [], - entry_points: [ - ( - name: "ray_gen_main", - stage: RayGeneration, - early_depth_test: None, - workgroup_size: (0, 0, 0), - workgroup_size_overrides: None, - function: ( - name: Some("ray_gen_main"), - arguments: [], - result: None, - local_variables: [], - expressions: [ - GlobalVariable(0), - ZeroValue(1), - GlobalVariable(1), - Literal(U32(0)), - Literal(U32(255)), - Literal(F32(0.01)), - Literal(F32(100.0)), - Literal(F32(0.0)), - Splat( - size: Tri, - value: 7, - ), - Literal(F32(0.0)), - Literal(F32(1.0)), - Literal(F32(0.0)), - Compose( - ty: 4, - components: [ - 9, - 10, - 11, - ], - ), - Compose( - ty: 5, - components: [ - 3, - 4, - 5, - 6, - 8, - 12, - ], - ), - GlobalVariable(0), - ], - named_expressions: {}, - body: [ - Store( - pointer: 0, - value: 1, - ), - Emit(( - start: 0, - end: 0, - )), - Emit(( - start: 0, - end: 0, - )), - Emit(( - start: 8, - end: 9, - )), - Emit(( - start: 12, - end: 14, - )), - RayPipelineFunction(TraceRay( - acceleration_structure: 2, - descriptor: 13, - payload: 14, - )), - Return( - value: None, - ), - ], - diagnostic_filter_leaf: None, - ), - mesh_info: None, - task_payload: None, - incoming_ray_payload: None, - ), - ( - name: "miss", - stage: Miss, - early_depth_test: None, - workgroup_size: (0, 0, 0), - workgroup_size_overrides: None, - function: ( - name: Some("miss"), - arguments: [], - result: None, - local_variables: [], - expressions: [], - named_expressions: {}, - body: [ - Return( - value: None, - ), - ], - diagnostic_filter_leaf: None, - ), - mesh_info: None, - task_payload: None, - incoming_ray_payload: Some(2), - ), - ( - name: "any_hit_main", - stage: AnyHit, - early_depth_test: None, - workgroup_size: (0, 0, 0), - workgroup_size_overrides: None, - function: ( - name: Some("any_hit_main"), - arguments: [], - result: None, - local_variables: [], - expressions: [ - GlobalVariable(2), - AccessIndex( - base: 0, - index: 0, - ), - Literal(U32(1)), - Load( - pointer: 1, - ), - Binary( - op: Add, - left: 3, - right: 2, - ), - GlobalVariable(2), - AccessIndex( - base: 5, - index: 1, - ), - GlobalVariable(2), - AccessIndex( - base: 7, - index: 0, - ), - Load( - pointer: 8, - ), - ], - named_expressions: {}, - body: [ - Emit(( - start: 1, - end: 2, - )), - Emit(( - start: 3, - end: 5, - )), - Store( - pointer: 1, - value: 4, - ), - Emit(( - start: 6, - end: 7, - )), - Emit(( - start: 8, - end: 10, - )), - Store( - pointer: 6, - value: 9, - ), - Return( - value: None, - ), - ], - diagnostic_filter_leaf: None, - ), - mesh_info: None, - task_payload: None, - incoming_ray_payload: Some(2), - ), - ( - name: "closest_hit_main", - stage: ClosestHit, - early_depth_test: None, - workgroup_size: (0, 0, 0), - workgroup_size_overrides: None, - function: ( - name: Some("closest_hit_main"), - arguments: [], - result: None, - local_variables: [], - expressions: [], - named_expressions: {}, - body: [ - Return( - value: None, - ), - ], - diagnostic_filter_leaf: None, - ), - mesh_info: None, - task_payload: None, - incoming_ray_payload: Some(2), - ), - ], - diagnostic_filters: [], - diagnostic_filter_leaf: None, - doc_comments: None, -) \ No newline at end of file diff --git a/naga/tests/out/wgsl/wgsl-ray-tracing-pipeline.wgsl b/naga/tests/out/wgsl/wgsl-ray-tracing-pipeline.wgsl new file mode 100644 index 000000000..896043ff3 --- /dev/null +++ b/naga/tests/out/wgsl/wgsl-ray-tracing-pipeline.wgsl @@ -0,0 +1,46 @@ +enable wgpu_ray_tracing_pipeline; + +struct HitCounters { + hit_num: u32, + selected_hit: u32, +} + +struct RayDesc { + flags: u32, + cull_mask: u32, + tmin: f32, + tmax: f32, + origin: vec3, + dir: vec3, +} + +var hit_num: HitCounters; +@group(0) @binding(0) +var acc_struct: acceleration_structure; +var incoming_hit_num: HitCounters; + +@ray_generation +fn ray_gen_main() { + hit_num = HitCounters(); + traceRay(acc_struct, RayDesc(0u, 255u, 0.01f, 100f, vec3(0f), vec3(0f, 1f, 0f)), (&hit_num)); + return; +} + +@miss @incoming_payload(incoming_hit_num) +fn miss() { + return; +} + +@any_hit @incoming_payload(incoming_hit_num) +fn any_hit_main() { + let _e3 = incoming_hit_num.hit_num; + incoming_hit_num.hit_num = (_e3 + 1u); + let _e9 = incoming_hit_num.hit_num; + incoming_hit_num.selected_hit = _e9; + return; +} + +@closest_hit @incoming_payload(incoming_hit_num) +fn closest_hit_main() { + return; +}