|
| 1 | +//! Provides the [`AccelerationStructure`] type and [`RayFlags`], as defined by the [`ray_pipeline`] and [`ray_query`] |
| 2 | +//! extensions. |
| 3 | +//! |
| 4 | +//! Unlike the vulkan [`VK_KHR_acceleration_structure`] extension, there is no shared SPIR-V extension for acceleration |
| 5 | +//! structures. Instead, both [`SPV_KHR_ray_tracing`] and [`SPV_KHR_ray_query`] specify (slightly adjusted): |
| 6 | +//! > [`AccelerationStructure`], [`RayFlags`] and the `RayTraversalPrimitiveCullingKHR` capability are added by both |
| 7 | +//! > [`ray_pipeline`] and [`ray_query`]; they are intended to have identical definitions, and can be enabled by either |
| 8 | +//! > extension’s capability, for use with the instructions under that same capability. |
| 9 | +//! |
| 10 | +//! [`ray_pipeline`]: super::ray_pipeline |
| 11 | +//! [`ray_query`]: super::ray_query |
| 12 | +//! [`VK_KHR_acceleration_structure`]: https://docs.vulkan.org/refpages/latest/refpages/source/VK_KHR_acceleration_structure.html |
| 13 | +//! [`SPV_KHR_ray_tracing`]: https://github.khronos.org/SPIRV-Registry/extensions/KHR/SPV_KHR_ray_tracing.html |
| 14 | +//! [`SPV_KHR_ray_query`]: https://github.khronos.org/SPIRV-Registry/extensions/KHR/SPV_KHR_ray_query.html |
| 15 | +
|
| 16 | +#[cfg(target_arch = "spirv")] |
| 17 | +use core::arch::asm; |
| 18 | +use glam::UVec2; |
| 19 | + |
| 20 | +/// An acceleration structure type which is an opaque reference to an |
| 21 | +/// acceleration structure handle as defined in the client API specification. |
| 22 | +#[spirv(acceleration_structure)] |
| 23 | +#[derive(Copy, Clone)] |
| 24 | +// HACK(eddyb) avoids "transparent newtype of `_anti_zst_padding`" misinterpretation. |
| 25 | +#[repr(C)] |
| 26 | +pub struct AccelerationStructure { |
| 27 | + // HACK(eddyb) avoids the layout becoming ZST (and being elided in one way |
| 28 | + // or another, before `#[spirv(acceleration_structure)]` can special-case it). |
| 29 | + _anti_zst_padding: core::mem::MaybeUninit<u32>, |
| 30 | +} |
| 31 | + |
| 32 | +impl AccelerationStructure { |
| 33 | + /// Converts a 64-bit integer into an [`AccelerationStructure`]. |
| 34 | + /// # Safety |
| 35 | + /// The 64-bit integer must point to a valid acceleration structure. |
| 36 | + #[spirv_std_macros::gpu_only] |
| 37 | + #[doc(alias = "OpConvertUToAccelerationStructureKHR")] |
| 38 | + #[inline] |
| 39 | + pub unsafe fn from_u64(id: u64) -> AccelerationStructure { |
| 40 | + unsafe { |
| 41 | + // FIXME(eddyb) `let mut result = T::default()` uses (for `asm!`), with this. |
| 42 | + let mut result_slot = core::mem::MaybeUninit::uninit(); |
| 43 | + asm! { |
| 44 | + "%ret = OpTypeAccelerationStructureKHR", |
| 45 | + "%result = OpConvertUToAccelerationStructureKHR %ret {id}", |
| 46 | + "OpStore {result_slot} %result", |
| 47 | + id = in(reg) id, |
| 48 | + result_slot = in(reg) result_slot.as_mut_ptr(), |
| 49 | + } |
| 50 | + result_slot.assume_init() |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// Converts a vector of two 32 bit integers into an [`AccelerationStructure`]. |
| 55 | + /// # Safety |
| 56 | + /// The combination must point to a valid acceleration structure. |
| 57 | + #[spirv_std_macros::gpu_only] |
| 58 | + #[doc(alias = "OpConvertUToAccelerationStructureKHR")] |
| 59 | + #[inline] |
| 60 | + pub unsafe fn from_vec(id: UVec2) -> AccelerationStructure { |
| 61 | + unsafe { |
| 62 | + // FIXME(eddyb) `let mut result = T::default()` uses (for `asm!`), with this. |
| 63 | + let mut result_slot = core::mem::MaybeUninit::uninit(); |
| 64 | + asm! { |
| 65 | + "%ret = OpTypeAccelerationStructureKHR", |
| 66 | + "%id = OpLoad _ {id}", |
| 67 | + "%result = OpConvertUToAccelerationStructureKHR %ret %id", |
| 68 | + "OpStore {result_slot} %result", |
| 69 | + id = in(reg) &id, |
| 70 | + result_slot = in(reg) result_slot.as_mut_ptr(), |
| 71 | + } |
| 72 | + result_slot.assume_init() |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +bitflags::bitflags! { |
| 78 | + /// Flags controlling the properties of an OpTraceRayKHR instruction. |
| 79 | + /// Despite being a mask and allowing multiple bits to be combined, it is |
| 80 | + /// invalid for more than one of these four bits to be set: `OPAQUE`, |
| 81 | + /// `NO_OPAQUE`, `CULL_OPAQUE`, `CULL_NO_OPAQUE`, only one of |
| 82 | + /// `CULL_BACK_FACING_TRIANGLES` and `CULL_FRONT_FACING_TRIANGLES` may |
| 83 | + /// be set. |
| 84 | + #[repr(transparent)] |
| 85 | + #[cfg_attr(feature = "bytemuck", derive(bytemuck::Zeroable, bytemuck::Pod))] |
| 86 | + pub struct RayFlags: u32 { |
| 87 | + /// No flags specified. |
| 88 | + const NONE = 0; |
| 89 | + /// Force all intersections with the trace to be opaque. |
| 90 | + const OPAQUE = 1; |
| 91 | + /// Force all intersections with the trace to be non-opaque. |
| 92 | + const NO_OPAQUE = 2; |
| 93 | + /// Accept the first hit discovered. |
| 94 | + const TERMINATE_ON_FIRST_HIT = 4; |
| 95 | + /// Do not execute a closest hit shader. |
| 96 | + const SKIP_CLOSEST_HIT_SHADER = 8; |
| 97 | + /// Do not intersect with the back face of triangles. |
| 98 | + const CULL_BACK_FACING_TRIANGLES = 16; |
| 99 | + /// Do not intersect with the front face of triangles. |
| 100 | + const CULL_FRONT_FACING_TRIANGLES = 32; |
| 101 | + /// Do not intersect with opaque geometry. |
| 102 | + const CULL_OPAQUE = 64; |
| 103 | + /// Do not intersect with non-opaque geometry. |
| 104 | + const CULL_NO_OPAQUE = 128; |
| 105 | + /// Do not intersect with any triangle geometries. |
| 106 | + /// Requires `RayTraversalPrimitiveCullingKHR`. |
| 107 | + const SKIP_TRIANGLES = 256; |
| 108 | + /// Do not intersect with any AABB (Axis Aligned Bounding Box) geometries. |
| 109 | + /// Requires `RayTraversalPrimitiveCullingKHR`. |
| 110 | + const SKIP_AABBS = 512; |
| 111 | + } |
| 112 | +} |
0 commit comments