From bbcba4fbc27e6591931b3d08462a5895cf9924de Mon Sep 17 00:00:00 2001 From: ComfyFluffy <24245520+ComfyFluffy@users.noreply.github.com> Date: Tue, 17 Dec 2024 20:36:32 +0800 Subject: [PATCH 1/4] miss -> rmiss --- examples/ray-tracing-auto/{raytrace.miss => raytrace.rmiss} | 0 examples/ray-tracing-auto/scene.rs | 2 +- examples/ray-tracing/{raytrace.miss => raytrace.rmiss} | 0 examples/ray-tracing/scene.rs | 2 +- 4 files changed, 2 insertions(+), 2 deletions(-) rename examples/ray-tracing-auto/{raytrace.miss => raytrace.rmiss} (100%) rename examples/ray-tracing/{raytrace.miss => raytrace.rmiss} (100%) diff --git a/examples/ray-tracing-auto/raytrace.miss b/examples/ray-tracing-auto/raytrace.rmiss similarity index 100% rename from examples/ray-tracing-auto/raytrace.miss rename to examples/ray-tracing-auto/raytrace.rmiss diff --git a/examples/ray-tracing-auto/scene.rs b/examples/ray-tracing-auto/scene.rs index 019b61e631..37784788d4 100644 --- a/examples/ray-tracing-auto/scene.rs +++ b/examples/ray-tracing-auto/scene.rs @@ -52,7 +52,7 @@ mod closest_hit { mod miss { vulkano_shaders::shader! { ty: "miss", - path: "raytrace.miss", + path: "raytrace.rmiss", vulkan_version: "1.2" } } diff --git a/examples/ray-tracing/raytrace.miss b/examples/ray-tracing/raytrace.rmiss similarity index 100% rename from examples/ray-tracing/raytrace.miss rename to examples/ray-tracing/raytrace.rmiss diff --git a/examples/ray-tracing/scene.rs b/examples/ray-tracing/scene.rs index b01eec3ed0..577d1d463d 100644 --- a/examples/ray-tracing/scene.rs +++ b/examples/ray-tracing/scene.rs @@ -56,7 +56,7 @@ mod closest_hit { mod miss { vulkano_shaders::shader! { ty: "miss", - path: "raytrace.miss", + path: "raytrace.rmiss", vulkan_version: "1.2" } } From 13c1e068a6a57d0482df89d5aeed73d34292b010 Mon Sep 17 00:00:00 2001 From: ComfyFluffy <24245520+ComfyFluffy@users.noreply.github.com> Date: Tue, 17 Dec 2024 20:38:06 +0800 Subject: [PATCH 2/4] perspective_rh instead of perspective_rh_gl --- examples/ray-tracing-auto/scene.rs | 2 +- examples/ray-tracing/scene.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/ray-tracing-auto/scene.rs b/examples/ray-tracing-auto/scene.rs index 37784788d4..4e9a5c87ee 100644 --- a/examples/ray-tracing-auto/scene.rs +++ b/examples/ray-tracing-auto/scene.rs @@ -187,7 +187,7 @@ impl Scene { ) }; - let proj = Mat4::perspective_rh_gl(std::f32::consts::FRAC_PI_2, 4.0 / 3.0, 0.01, 100.0); + let proj = Mat4::perspective_rh(std::f32::consts::FRAC_PI_2, 4.0 / 3.0, 0.01, 100.0); let view = Mat4::look_at_rh( Vec3::new(0.0, 0.0, 1.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/ray-tracing/scene.rs b/examples/ray-tracing/scene.rs index 577d1d463d..6aabc9c353 100644 --- a/examples/ray-tracing/scene.rs +++ b/examples/ray-tracing/scene.rs @@ -184,7 +184,7 @@ impl SceneTask { ) }; - let proj = Mat4::perspective_rh_gl(std::f32::consts::FRAC_PI_2, 4.0 / 3.0, 0.01, 100.0); + let proj = Mat4::perspective_rh(std::f32::consts::FRAC_PI_2, 4.0 / 3.0, 0.01, 100.0); let view = Mat4::look_at_rh( Vec3::new(0.0, 0.0, 1.0), Vec3::new(0.0, 0.0, 0.0), From 2efe76a596d6b8a463a067b913bcf23e3f75f271 Mon Sep 17 00:00:00 2001 From: ComfyFluffy <24245520+ComfyFluffy@users.noreply.github.com> Date: Tue, 17 Dec 2024 20:47:16 +0800 Subject: [PATCH 3/4] refactor copy handles --- vulkano/src/pipeline/ray_tracing.rs | 77 ++++++++++++++++++----------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/vulkano/src/pipeline/ray_tracing.rs b/vulkano/src/pipeline/ray_tracing.rs index abee90419c..f799484e5d 100644 --- a/vulkano/src/pipeline/ray_tracing.rs +++ b/vulkano/src/pipeline/ray_tracing.rs @@ -393,6 +393,16 @@ impl RayTracingPipelineCreateInfo { ..Default::default() })); } + let has_raygen = stages.iter().any(|stage| { + stage.entry_point.info().execution_model == ExecutionModel::RayGenerationKHR + }); + if !has_raygen { + return Err(Box::new(ValidationError { + problem: "No ray generation shader stage found".into(), + vuids: &["VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425"], + ..Default::default() + })); + } for stage in stages { stage.validate(device).map_err(|err| { err.add_context("stages") @@ -819,11 +829,25 @@ impl ShaderBindingTable { allocator: Arc, ray_tracing_pipeline: &RayTracingPipeline, ) -> Result> { - let mut miss_shader_count: u64 = 0; - let mut hit_shader_count: u64 = 0; - let mut callable_shader_count: u64 = 0; + // VUID-vkCmdTraceRaysKHR-size-04023 + // There should be exactly one raygen shader group. + let mut raygen_shader_handle = None; + let mut miss_shader_handles = Vec::new(); + let mut hit_shader_handles = Vec::new(); + let mut callable_shader_handles = Vec::new(); + + let handle_data = ray_tracing_pipeline + .device() + .ray_tracing_shader_group_handles( + ray_tracing_pipeline, + 0, + ray_tracing_pipeline.groups().len() as u32, + )?; + + let mut handles_iter = handle_data.iter(); for group in ray_tracing_pipeline.groups() { + let handle = handles_iter.next().unwrap(); match group { RayTracingShaderGroupCreateInfo::General { general_shader } => { match ray_tracing_pipeline.stages()[*general_shader as usize] @@ -831,9 +855,15 @@ impl ShaderBindingTable { .info() .execution_model { - ExecutionModel::RayGenerationKHR => {} - ExecutionModel::MissKHR => miss_shader_count += 1, - ExecutionModel::CallableKHR => callable_shader_count += 1, + ExecutionModel::RayGenerationKHR => { + raygen_shader_handle = Some(handle); + } + ExecutionModel::MissKHR => { + miss_shader_handles.push(handle); + } + ExecutionModel::CallableKHR => { + callable_shader_handles.push(handle); + } _ => { panic!("Unexpected shader type in general shader group"); } @@ -841,18 +871,12 @@ impl ShaderBindingTable { } RayTracingShaderGroupCreateInfo::ProceduralHit { .. } | RayTracingShaderGroupCreateInfo::TrianglesHit { .. } => { - hit_shader_count += 1; + hit_shader_handles.push(handle); } } } - let handle_data = ray_tracing_pipeline - .device() - .ray_tracing_shader_group_handles( - ray_tracing_pipeline, - 0, - ray_tracing_pipeline.groups().len() as u32, - )?; + let raygen_shader_handle = raygen_shader_handle.expect("Missing raygen shader"); let properties = ray_tracing_pipeline.device().physical_device().properties(); let handle_size_aligned = align_up( @@ -873,7 +897,7 @@ impl ShaderBindingTable { let mut miss = StridedDeviceAddressRegion { stride: handle_size_aligned, size: align_up( - handle_size_aligned * miss_shader_count, + handle_size_aligned * miss_shader_handles.len() as u64, shader_group_base_alignment, ), device_address: 0, @@ -881,7 +905,7 @@ impl ShaderBindingTable { let mut hit = StridedDeviceAddressRegion { stride: handle_size_aligned, size: align_up( - handle_size_aligned * hit_shader_count, + handle_size_aligned * hit_shader_handles.len() as u64, shader_group_base_alignment, ), device_address: 0, @@ -889,7 +913,7 @@ impl ShaderBindingTable { let mut callable = StridedDeviceAddressRegion { stride: handle_size_aligned, size: align_up( - handle_size_aligned * callable_shader_count, + handle_size_aligned * callable_shader_handles.len() as u64, shader_group_base_alignment, ), device_address: 0, @@ -920,26 +944,21 @@ impl ShaderBindingTable { { let mut sbt_buffer_write = sbt_buffer.write().unwrap(); - let mut handle_iter = handle_data.iter(); - let handle_size = handle_data.handle_size() as usize; - sbt_buffer_write[..handle_size].copy_from_slice(handle_iter.next().unwrap()); + sbt_buffer_write[..handle_size].copy_from_slice(raygen_shader_handle); let mut offset = raygen.size as usize; - for _ in 0..miss_shader_count { - sbt_buffer_write[offset..offset + handle_size] - .copy_from_slice(handle_iter.next().unwrap()); + for handle in miss_shader_handles { + sbt_buffer_write[offset..offset + handle_size].copy_from_slice(handle); offset += miss.stride as usize; } offset = (raygen.size + miss.size) as usize; - for _ in 0..hit_shader_count { - sbt_buffer_write[offset..offset + handle_size] - .copy_from_slice(handle_iter.next().unwrap()); + for handle in hit_shader_handles { + sbt_buffer_write[offset..offset + handle_size].copy_from_slice(handle); offset += hit.stride as usize; } offset = (raygen.size + miss.size + hit.size) as usize; - for _ in 0..callable_shader_count { - sbt_buffer_write[offset..offset + handle_size] - .copy_from_slice(handle_iter.next().unwrap()); + for handle in callable_shader_handles { + sbt_buffer_write[offset..offset + handle_size].copy_from_slice(handle); offset += callable.stride as usize; } } From 474c5566b0c83f0d29851f0ec439661decf3f6ba Mon Sep 17 00:00:00 2001 From: ComfyFluffy <24245520+ComfyFluffy@users.noreply.github.com> Date: Tue, 17 Dec 2024 21:05:19 +0800 Subject: [PATCH 4/4] refactor tlas builder --- examples/ray-tracing-auto/scene.rs | 17 ++++++++--------- examples/ray-tracing/scene.rs | 17 ++++++++--------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/examples/ray-tracing-auto/scene.rs b/examples/ray-tracing-auto/scene.rs index 4e9a5c87ee..d787968dc6 100644 --- a/examples/ray-tracing-auto/scene.rs +++ b/examples/ray-tracing-auto/scene.rs @@ -179,7 +179,10 @@ impl Scene { let tlas = unsafe { build_top_level_acceleration_structure( - blas.clone(), + vec![AccelerationStructureInstance { + acceleration_structure_reference: blas.device_address().into(), + ..Default::default() + }], memory_allocator.clone(), command_buffer_allocator.clone(), app.device.clone(), @@ -436,17 +439,13 @@ unsafe fn build_acceleration_structure_triangles( } unsafe fn build_top_level_acceleration_structure( - acceleration_structure: Arc, + as_instances: Vec, allocator: Arc, command_buffer_allocator: Arc, device: Arc, queue: Arc, ) -> Arc { - let as_instance = AccelerationStructureInstance { - acceleration_structure_reference: acceleration_structure.device_address().into(), - ..Default::default() - }; - + let primitive_count = as_instances.len() as u32; let instance_buffer = Buffer::from_iter( allocator.clone(), BufferCreateInfo { @@ -459,7 +458,7 @@ unsafe fn build_top_level_acceleration_structure( | MemoryTypeFilter::HOST_SEQUENTIAL_WRITE, ..Default::default() }, - [as_instance], + as_instances, ) .unwrap(); @@ -471,7 +470,7 @@ unsafe fn build_top_level_acceleration_structure( build_acceleration_structure_common( geometries, - 1, + primitive_count, AccelerationStructureType::TopLevel, allocator, command_buffer_allocator, diff --git a/examples/ray-tracing/scene.rs b/examples/ray-tracing/scene.rs index 6aabc9c353..66ecc64a91 100644 --- a/examples/ray-tracing/scene.rs +++ b/examples/ray-tracing/scene.rs @@ -176,7 +176,10 @@ impl SceneTask { let tlas = unsafe { build_top_level_acceleration_structure( - blas.clone(), + vec![AccelerationStructureInstance { + acceleration_structure_reference: blas.device_address().into(), + ..Default::default() + }], memory_allocator.clone(), command_buffer_allocator.clone(), app.device.clone(), @@ -454,17 +457,13 @@ unsafe fn build_acceleration_structure_triangles( } unsafe fn build_top_level_acceleration_structure( - acceleration_structure: Arc, + as_instances: Vec, allocator: Arc, command_buffer_allocator: Arc, device: Arc, queue: Arc, ) -> Arc { - let as_instance = AccelerationStructureInstance { - acceleration_structure_reference: acceleration_structure.device_address().into(), - ..Default::default() - }; - + let primitive_count = as_instances.len() as u32; let instance_buffer = Buffer::from_iter( allocator.clone(), BufferCreateInfo { @@ -477,7 +476,7 @@ unsafe fn build_top_level_acceleration_structure( | MemoryTypeFilter::HOST_SEQUENTIAL_WRITE, ..Default::default() }, - [as_instance], + as_instances, ) .unwrap(); @@ -489,7 +488,7 @@ unsafe fn build_top_level_acceleration_structure( build_acceleration_structure_common( geometries, - 1, + primitive_count, AccelerationStructureType::TopLevel, allocator, command_buffer_allocator,