Skip to main content

spirv_std/
ray_tracing.rs

1//! Ray-tracing data types
2
3// NOTE(eddyb) "&-masking with zero", likely due to `NONE = 0` in `bitflags!`.
4#![allow(clippy::bad_bit_mask)]
5
6use crate::glam::{UVec2, Vec2, Vec3};
7use crate::matrix::Matrix4x3;
8#[cfg(target_arch = "spirv")]
9use core::arch::asm;
10
11/// An acceleration structure type which is an opaque reference to an
12/// acceleration structure handle as defined in the client API specification.
13#[spirv(acceleration_structure)]
14#[derive(Copy, Clone)]
15// HACK(eddyb) avoids "transparent newtype of `_anti_zst_padding`" misinterpretation.
16#[repr(C)]
17pub struct AccelerationStructure {
18    // HACK(eddyb) avoids the layout becoming ZST (and being elided in one way
19    // or another, before `#[spirv(acceleration_structure)]` can special-case it).
20    _anti_zst_padding: core::mem::MaybeUninit<u32>,
21}
22
23impl AccelerationStructure {
24    /// Converts a 64-bit integer into an [`AccelerationStructure`].
25    /// # Safety
26    /// The 64-bit integer must point to a valid acceleration structure.
27    #[spirv_std_macros::gpu_only]
28    #[doc(alias = "OpConvertUToAccelerationStructureKHR")]
29    #[inline]
30    pub unsafe fn from_u64(id: u64) -> AccelerationStructure {
31        unsafe {
32            // FIXME(eddyb) `let mut result = T::default()` uses (for `asm!`), with this.
33            let mut result_slot = core::mem::MaybeUninit::uninit();
34            asm! {
35                "%ret = OpTypeAccelerationStructureKHR",
36                "%result = OpConvertUToAccelerationStructureKHR %ret {id}",
37                "OpStore {result_slot} %result",
38                id = in(reg) id,
39                result_slot = in(reg) result_slot.as_mut_ptr(),
40            }
41            result_slot.assume_init()
42        }
43    }
44
45    /// Converts a vector of two 32 bit integers into an [`AccelerationStructure`].
46    /// # Safety
47    /// The combination must point to a valid acceleration structure.
48    #[spirv_std_macros::gpu_only]
49    #[doc(alias = "OpConvertUToAccelerationStructureKHR")]
50    #[inline]
51    pub unsafe fn from_vec(id: UVec2) -> AccelerationStructure {
52        unsafe {
53            // FIXME(eddyb) `let mut result = T::default()` uses (for `asm!`), with this.
54            let mut result_slot = core::mem::MaybeUninit::uninit();
55            asm! {
56                "%ret = OpTypeAccelerationStructureKHR",
57                "%id = OpLoad _ {id}",
58                "%result = OpConvertUToAccelerationStructureKHR %ret %id",
59                "OpStore {result_slot} %result",
60                id = in(reg) &id,
61                result_slot = in(reg) result_slot.as_mut_ptr(),
62            }
63            result_slot.assume_init()
64        }
65    }
66
67    #[spirv_std_macros::gpu_only]
68    /// Trace a ray into the acceleration structure.
69    ///
70    /// - `structure` is the descriptor for the acceleration structure to trace into.
71    /// - `ray_flags` contains one or more of the Ray Flag values.
72    /// - `cull_mask` is the mask to test against the instance mask. Only the 8
73    ///   least-significant bits of are used by this instruction - other bits
74    ///   are ignored.
75    /// - `sbt_offset` and `sbt_stride` control indexing into the SBT (Shader
76    ///   Binding Table) for hit shaders called from this trace. Only the 4
77    ///   least-significant bits of `sbt_offset` and `sbt_stride` are used by this
78    ///   instruction - other bits are ignored.
79    /// - `miss_index` is the index of the miss shader to be called from this
80    ///   trace call. Only the 16 least-significant bits are used by this
81    ///   instruction - other bits are ignored.
82    /// - `ray_origin`, `ray_tmin`, `ray_direction`, and `ray_tmax` control the
83    ///   basic parameters of the ray to be traced.
84    ///
85    /// - `payload` is a pointer to the ray payload structure to use for this trace.
86    ///   `payload` must have a storage class of `ray_payload`
87    ///   or `incoming_ray_payload`.
88    ///
89    /// This instruction is allowed only in `ray_generation`, `closest_hit` and
90    /// `miss` execution models.
91    ///
92    /// This instruction is a shader call instruction which may invoke shaders with
93    /// the `intersection`, `any_hit`, `closest_hit`, and `miss`
94    /// execution models.
95    #[doc(alias = "OpTraceRayKHR")]
96    #[inline]
97    #[allow(clippy::too_many_arguments)]
98    pub unsafe fn trace_ray<T>(
99        &self,
100        ray_flags: RayFlags,
101        cull_mask: i32,
102        sbt_offset: i32,
103        sbt_stride: i32,
104        miss_index: i32,
105        ray_origin: Vec3,
106        ray_tmin: f32,
107        ray_direction: Vec3,
108        ray_tmax: f32,
109        payload: &mut T,
110    ) {
111        unsafe {
112            asm! {
113                "%acceleration_structure = OpLoad _ {acceleration_structure}",
114                "%ray_origin = OpLoad _ {ray_origin}",
115                "%ray_direction = OpLoad _ {ray_direction}",
116                "OpTraceRayKHR \
117                %acceleration_structure \
118                {ray_flags} \
119                {cull_mask} \
120                {sbt_offset} \
121                {sbt_stride} \
122                {miss_index} \
123                %ray_origin \
124                {ray_tmin} \
125                %ray_direction \
126                {ray_tmax} \
127                {payload}",
128                acceleration_structure = in(reg) self,
129                ray_flags = in(reg) ray_flags.bits(),
130                cull_mask = in(reg) cull_mask,
131                sbt_offset = in(reg) sbt_offset,
132                sbt_stride = in(reg) sbt_stride,
133                miss_index = in(reg) miss_index,
134                ray_origin = in(reg) &ray_origin,
135                ray_tmin = in(reg) ray_tmin,
136                ray_direction = in(reg) &ray_direction,
137                ray_tmax = in(reg) ray_tmax,
138                payload = in(reg) payload,
139            }
140        }
141    }
142}
143
144bitflags::bitflags! {
145    /// Flags controlling the properties of an OpTraceRayKHR instruction.
146    /// Despite being a mask and allowing multiple bits to be combined, it is
147    /// invalid for more than one of these four bits to be set: `OPAQUE`,
148    /// `NO_OPAQUE`, `CULL_OPAQUE`, `CULL_NO_OPAQUE`, only one of
149    /// `CULL_BACK_FACING_TRIANGLES` and `CULL_FRONT_FACING_TRIANGLES` may
150    /// be set.
151    #[repr(transparent)]
152    #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
153    #[cfg_attr(feature = "bytemuck", derive(bytemuck::Zeroable, bytemuck::Pod))]
154    pub struct RayFlags: u32 {
155        /// No flags specified.
156        const NONE = 0;
157        /// Force all intersections with the trace to be opaque.
158        const OPAQUE = 1;
159        /// Force all intersections with the trace to be non-opaque.
160        const NO_OPAQUE = 2;
161        /// Accept the first hit discovered.
162        const TERMINATE_ON_FIRST_HIT = 4;
163        /// Do not execute a closest hit shader.
164        const SKIP_CLOSEST_HIT_SHADER = 8;
165        /// Do not intersect with the back face of triangles.
166        const CULL_BACK_FACING_TRIANGLES = 16;
167        /// Do not intersect with the front face of triangles.
168        const CULL_FRONT_FACING_TRIANGLES = 32;
169        /// Do not intersect with opaque geometry.
170        const CULL_OPAQUE = 64;
171        /// Do not intersect with non-opaque geometry.
172        const CULL_NO_OPAQUE = 128;
173        /// Do not intersect with any triangle geometries.
174        const SKIP_TRIANGLES = 256;
175        /// Do not intersect with any AABB (Axis Aligned Bounding Box) geometries.
176        const SKIP_AABBS = 512;
177    }
178}
179
180/// Describes the type of the intersection which is currently the candidate in a ray query,
181/// returned by [`RayQuery::get_candidate_intersection_type`].
182#[repr(u32)]
183#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
184#[allow(clippy::upper_case_acronyms)]
185pub enum CandidateIntersection {
186    /// A potential intersection with a triangle is being considered.
187    Triangle = 0,
188    /// A potential intersection with an axis-aligned bounding box is being considered.
189    AABB = 1,
190}
191
192/// Describes the type of the intersection currently committed in a ray query, returned by
193/// [`RayQuery::get_committed_intersection_type`].
194#[repr(u32)]
195#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
196pub enum CommittedIntersection {
197    /// No intersection is committed.
198    None = 0,
199    /// An intersection with a triangle has been committed.
200    Triangle = 1,
201    /// A user-generated intersection has been committed.
202    Generated = 2,
203}
204
205/// A ray query type which is an opaque object representing a ray traversal.
206#[spirv(ray_query)]
207// HACK(eddyb) avoids "transparent newtype of `_anti_zst_padding`" misinterpretation.
208#[repr(C)]
209// HACK(eddyb) false positive due to `rustc` not understanding e.g. `ray_query!`.
210#[allow(dead_code)]
211pub struct RayQuery {
212    // HACK(eddyb) avoids the layout becoming ZST (and being elided in one way
213    // or another, before `#[spirv(ray_query)]` can special-case it).
214    _anti_zst_padding: core::mem::MaybeUninit<u32>,
215}
216
217/// Constructs an uninitialized ray query variable. Using the syntax
218/// `let (mut)? <name>`. Where `name` is the name of the ray query variable.
219///
220/// # Example
221/// ```
222/// use spirv_std::glam::Vec3;
223/// use spirv_std::ray_query;
224/// use spirv_std::ray_tracing::{AccelerationStructure, CommittedIntersection, RayFlags, RayQuery};
225///
226/// fn is_hit(accel: &AccelerationStructure, origin: Vec3, direction: Vec3) -> bool {
227///     ray_query!(let mut query);
228///     unsafe {
229///         query.initialize(accel, RayFlags::OPAQUE, 0xff, origin, 0.0, direction, 1000.0);
230///         while query.proceed() {}
231///         query.get_committed_intersection_type() != CommittedIntersection::None
232///     }
233/// }
234/// # std::hint::black_box(is_hit as fn(_, _, _) -> _);
235/// ```
236#[macro_export]
237macro_rules! ray_query {
238    (let $name:ident) => {
239        $crate::ray_query!(@inner $name)
240    };
241    (let mut $name:ident) => {
242        $crate::ray_query!(@inner $name, mut)
243    };
244    (@inner $name:ident $(, $mut:tt)?) => {
245        #[cfg(target_arch = "spirv")]
246        let $name: &$($mut)? RayQuery = unsafe {
247            let $name : *mut RayQuery;
248            ::core::arch::asm! {
249                "%ray_query = OpTypeRayQueryKHR",
250                "%ray_query_ptr = OpTypePointer Generic %ray_query",
251                "{name} = OpVariable %ray_query_ptr Function",
252                name = out(reg) $name,
253            }
254
255            &$($mut)? *$name
256        };
257        #[cfg(not(target_arch = "spirv"))]
258        let $name: &$($mut)? RayQuery = {
259            fn gpu_only<T>() -> T {
260                ::core::unimplemented!("`ray_query!` is only available on SPIR-V platforms.")
261            }
262            gpu_only()
263        };
264    }
265}
266
267impl RayQuery {
268    /// Initialize a ray query object, defining parameters of traversal. After this
269    /// call, a new ray trace can be performed with [`Self::proceed`]. Any
270    /// previous traversal state stored in the object is lost.
271    ///
272    /// - `ray_query` is a pointer to the ray query to initialize.
273    /// - `acceleration_structure` is the descriptor for the acceleration structure
274    ///   to trace into.
275    /// - `ray_flags` contains one or more of the Ray Flag values.
276    /// - `cull_mask` is the mask to test against the instance mask.  Only the 8
277    ///   least-significant bits of `cull_mask` are used by this instruction - other
278    ///   bits are ignored.
279    /// - `ray_origin`, `ray_tmin`, `ray_direction`, and `ray_tmax` control the
280    ///   basic parameters of the ray to be traced.
281    #[spirv_std_macros::gpu_only]
282    #[doc(alias = "OpRayQueryInitializeKHR")]
283    #[inline]
284    #[allow(clippy::too_many_arguments)]
285    pub unsafe fn initialize(
286        &mut self,
287        acceleration_structure: &AccelerationStructure,
288        ray_flags: RayFlags,
289        cull_mask: u32,
290        ray_origin: Vec3,
291        ray_tmin: f32,
292        ray_direction: Vec3,
293        ray_tmax: f32,
294    ) {
295        unsafe {
296            asm! {
297                "%acceleration_structure = OpLoad _ {acceleration_structure}",
298                "%origin = OpLoad _ {ray_origin}",
299                "%direction = OpLoad _ {ray_direction}",
300                "OpRayQueryInitializeKHR \
301                    {ray_query} \
302                    %acceleration_structure \
303                    {ray_flags} \
304                    {cull_mask} \
305                    %origin \
306                    {ray_tmin} \
307                    %direction \
308                    {ray_tmax}",
309                ray_query = in(reg) self,
310                acceleration_structure = in(reg) acceleration_structure,
311                ray_flags = in(reg) ray_flags.bits(),
312                cull_mask = in(reg) cull_mask,
313                ray_origin = in(reg) &ray_origin,
314                ray_tmin = in(reg) ray_tmin,
315                ray_direction = in(reg) &ray_direction,
316                ray_tmax = in(reg) ray_tmax,
317            }
318        }
319    }
320
321    /// Allow traversal to proceed. Returns `true` if traversal is incomplete,
322    /// and `false` when it has completed. A previous call to [`Self::proceed`]
323    /// with the same ray query object must not have already returned `false`.
324    #[spirv_std_macros::gpu_only]
325    #[doc(alias = "OpRayQueryProceedKHR")]
326    #[inline]
327    pub unsafe fn proceed(&self) -> bool {
328        unsafe {
329            let mut result = false;
330
331            asm! {
332                "%bool = OpTypeBool",
333                "%result = OpRayQueryProceedKHR %bool {ray_query}",
334                "OpStore {result} %result",
335                ray_query = in(reg) self,
336                result = in(reg) &mut result,
337            }
338
339            result
340        }
341    }
342
343    /// Terminates further execution of a ray query; further calls to
344    /// [`Self::proceed`] will return `false`. The value returned by any prior
345    /// execution of [`Self::proceed`] with the same ray query object must have
346    /// been true.
347    #[spirv_std_macros::gpu_only]
348    #[doc(alias = "OpRayQueryTerminateKHR")]
349    #[inline]
350    pub unsafe fn terminate(&self) {
351        unsafe { asm!("OpRayQueryTerminateKHR {}", in(reg) self) }
352    }
353
354    /// Confirms a triangle intersection to be included in the determination
355    /// of the closest hit for a ray query.
356    ///
357    /// [`Self::proceed()`] must have been called on this object, and it must
358    /// have returned true. The current intersection candidate must have a
359    /// [`Self::get_candidate_intersection_type()`] of
360    /// [`CandidateIntersection::Triangle`].
361    #[spirv_std_macros::gpu_only]
362    #[doc(alias = "OpRayQueryConfirmIntersectionKHR")]
363    #[inline]
364    pub unsafe fn confirm_intersection(&self) {
365        unsafe { asm!("OpRayQueryConfirmIntersectionKHR {}", in(reg) self) }
366    }
367
368    /// Returns the type of the current candidate intersection.
369    ///
370    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
371    #[spirv_std_macros::gpu_only]
372    #[doc(alias = "OpRayQueryGetIntersectionTypeKHR")]
373    #[inline]
374    pub unsafe fn get_candidate_intersection_type(&self) -> CandidateIntersection {
375        unsafe {
376            let result: u32;
377
378            asm! {
379                "%u32 = OpTypeInt 32 0",
380                "%intersection = OpConstant %u32 0",
381                "{result} = OpRayQueryGetIntersectionTypeKHR %u32 {ray_query} %intersection",
382                ray_query = in(reg) self,
383                result = out(reg) result,
384            }
385
386            match result {
387                0 => CandidateIntersection::Triangle,
388                1 => CandidateIntersection::AABB,
389                _ => CandidateIntersection::Triangle,
390            }
391        }
392    }
393
394    /// Returns the type of the current candidate intersection.
395    #[spirv_std_macros::gpu_only]
396    #[doc(alias = "OpRayQueryGetIntersectionTypeKHR")]
397    #[inline]
398    pub unsafe fn get_committed_intersection_type(&self) -> CommittedIntersection {
399        unsafe {
400            let result: u32;
401
402            asm! {
403                "%u32 = OpTypeInt 32 0",
404                "%intersection = OpConstant %u32 1",
405                "{result} = OpRayQueryGetIntersectionTypeKHR %u32 {ray_query} %intersection",
406                ray_query = in(reg) self,
407                result = out(reg) result,
408            }
409
410            match result {
411                0 => CommittedIntersection::None,
412                1 => CommittedIntersection::Triangle,
413                2 => CommittedIntersection::Generated,
414                _ => CommittedIntersection::None,
415            }
416        }
417    }
418
419    /// Returns the "Ray Tmin" value used by the ray query.
420    #[spirv_std_macros::gpu_only]
421    #[doc(alias = "OpRayQueryGetRayTMinKHR")]
422    #[inline]
423    pub unsafe fn get_ray_t_min(&self) -> f32 {
424        unsafe {
425            let result;
426
427            asm! {
428                "%f32 = OpTypeFloat 32",
429                "{result} = OpRayQueryGetRayTMinKHR %f32 {ray_query}",
430                ray_query = in(reg) self,
431                result = out(reg) result,
432            }
433
434            result
435        }
436    }
437
438    /// Returns the "Ray Flags" value used by the ray query.
439    #[spirv_std_macros::gpu_only]
440    #[doc(alias = "OpRayQueryGetRayFlagsKHR")]
441    #[inline]
442    pub unsafe fn get_ray_flags(&self) -> RayFlags {
443        unsafe {
444            let result;
445
446            asm! {
447                "{result} = OpRayQueryGetRayFlagsKHR typeof{result} {ray_query}",
448                ray_query = in(reg) self,
449                result = out(reg) result,
450            }
451
452            // NOTE: In bitflags 2.x `from_bits_truncate`'s default impl iterates `Flags::FLAGS` at
453            // runtime (via `all()`), which involves pointer arithmetic our backend doesn't support.
454            // `result` always comes straight from `OpRayQueryGetRayFlagsKHR`, so there are no
455            // unknown bits to truncate anyway.
456            RayFlags::from_bits_retain(result)
457        }
458    }
459
460    /// Gets the "T" value for the current or previous intersection considered
461    /// in a ray query.
462    ///
463    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
464    /// The current intersection candidate must have a [`Self::get_candidate_intersection_type()`]
465    /// of [`CandidateIntersection::Triangle`].
466    #[spirv_std_macros::gpu_only]
467    #[doc(alias = "OpRayQueryGetIntersectionTKHR")]
468    #[inline]
469    pub unsafe fn get_candidate_intersection_t(&self) -> f32 {
470        unsafe {
471            let result;
472
473            asm! {
474                "%u32 = OpTypeInt 32 0",
475                "%intersection = OpConstant %u32 0",
476                "{result} = OpRayQueryGetIntersectionTKHR typeof{result} {ray_query} %intersection",
477                ray_query = in(reg) self,
478                result = out(reg) result,
479            }
480
481            result
482        }
483    }
484
485    /// Gets the "T" value for the current or previous intersection considered
486    /// in a ray query.
487    ///
488    /// There must be a current committed intersection.
489    ///
490    /// TODO: Improve docs. Can't right now due to
491    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
492    #[spirv_std_macros::gpu_only]
493    #[doc(alias = "OpRayQueryGetIntersectionTKHR")]
494    #[inline]
495    pub unsafe fn get_committed_intersection_t(&self) -> f32 {
496        unsafe {
497            let result;
498
499            asm! {
500                "%u32 = OpTypeInt 32 0",
501                "%intersection = OpConstant %u32 1",
502                "{result} = OpRayQueryGetIntersectionTKHR typeof{result} {ray_query} %intersection",
503                ray_query = in(reg) self,
504                result = out(reg) result,
505            }
506
507            result
508        }
509    }
510
511    /// Gets the custom index of the instance for the current intersection
512    /// considered in a ray query.
513    ///
514    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
515    #[spirv_std_macros::gpu_only]
516    #[doc(alias = "OpRayQueryGetIntersectionInstanceCustomIndexKHR")]
517    #[inline]
518    pub unsafe fn get_candidate_intersection_instance_custom_index(&self) -> u32 {
519        unsafe {
520            let result;
521
522            asm! {
523                "%u32 = OpTypeInt 32 0",
524                "%intersection = OpConstant %u32 0",
525                "{result} = OpRayQueryGetIntersectionInstanceCustomIndexKHR %u32 {ray_query} %intersection",
526                ray_query = in(reg) self,
527                result = out(reg) result,
528            }
529
530            result
531        }
532    }
533
534    /// Gets the custom index of the instance for the current intersection
535    /// considered in a ray query.
536    ///
537    /// There must be a current committed intersection.
538    ///
539    /// TODO: Improve docs. Can't right now due to
540    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
541    #[spirv_std_macros::gpu_only]
542    #[doc(alias = "OpRayQueryGetIntersectionInstanceCustomIndexKHR")]
543    #[inline]
544    pub unsafe fn get_committed_intersection_instance_custom_index(&self) -> u32 {
545        unsafe {
546            let result;
547
548            asm! {
549                "%u32 = OpTypeInt 32 0",
550                "%intersection = OpConstant %u32 1",
551                "{result} = OpRayQueryGetIntersectionInstanceCustomIndexKHR %u32 {ray_query} %intersection",
552                ray_query = in(reg) self,
553                result = out(reg) result,
554            }
555
556            result
557        }
558    }
559
560    /// Gets the id of the instance for the current intersection considered in a
561    /// ray query.
562    ///
563    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
564    #[spirv_std_macros::gpu_only]
565    #[doc(alias = "OpRayQueryGetIntersectionInstanceIdKHR")]
566    #[inline]
567    pub unsafe fn get_candidate_intersection_instance_id(&self) -> u32 {
568        unsafe {
569            let result;
570
571            asm! {
572                "%u32 = OpTypeInt 32 0",
573                "%intersection = OpConstant %u32 0",
574                "{result} = OpRayQueryGetIntersectionInstanceIdKHR %u32 {ray_query} %intersection",
575                ray_query = in(reg) self,
576                result = out(reg) result,
577            }
578
579            result
580        }
581    }
582
583    /// Gets the id of the instance for the current intersection considered in a
584    /// ray query.
585    ///
586    /// There must be a current committed intersection.
587    ///
588    /// TODO: Improve docs. Can't right now due to
589    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
590    #[spirv_std_macros::gpu_only]
591    #[doc(alias = "OpRayQueryGetIntersectionInstanceIdKHR")]
592    #[inline]
593    pub unsafe fn get_committed_intersection_instance_id(&self) -> u32 {
594        unsafe {
595            let result;
596
597            asm! {
598                "%u32 = OpTypeInt 32 0",
599                "%intersection = OpConstant %u32 1",
600                "{result} = OpRayQueryGetIntersectionInstanceIdKHR %u32 {ray_query} %intersection",
601                ray_query = in(reg) self,
602                result = out(reg) result,
603            }
604
605            result
606        }
607    }
608
609    /// Gets the shader binding table record offset for the current intersection
610    /// considered in a ray query.
611    ///
612    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
613    #[spirv_std_macros::gpu_only]
614    #[doc(alias = "OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR")]
615    #[inline]
616    pub unsafe fn get_candidate_intersection_shader_binding_table_record_offset(&self) -> u32 {
617        unsafe {
618            let result;
619
620            asm! {
621                "%u32 = OpTypeInt 32 0",
622                "%intersection = OpConstant %u32 0",
623                "{result} = OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR %u32 {ray_query} %intersection",
624                ray_query = in(reg) self,
625                result = out(reg) result,
626            }
627
628            result
629        }
630    }
631
632    /// Gets the shader binding table record offset for the current intersection
633    /// considered in a ray query.
634    ///
635    /// There must be a current committed intersection.
636    ///
637    /// TODO: Improve docs. Can't right now due to
638    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
639    #[spirv_std_macros::gpu_only]
640    #[doc(alias = "OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR")]
641    #[inline]
642    pub unsafe fn get_committed_intersection_shader_binding_table_record_offset(&self) -> u32 {
643        unsafe {
644            let result;
645
646            asm! {
647                "%u32 = OpTypeInt 32 0",
648                "%intersection = OpConstant %u32 1",
649                "{result} = OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR %u32 {ray_query} %intersection",
650                ray_query = in(reg) self,
651                result = out(reg) result,
652            }
653
654            result
655        }
656    }
657
658    /// Gets the geometry index for the current intersection considered in a
659    /// ray query.
660    ///
661    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
662    #[spirv_std_macros::gpu_only]
663    #[doc(alias = "OpRayQueryGetIntersectionGeometryIndexKHR")]
664    #[inline]
665    pub unsafe fn get_candidate_intersection_geometry_index(&self) -> u32 {
666        unsafe {
667            let result;
668
669            asm! {
670                "%u32 = OpTypeInt 32 0",
671                "%intersection = OpConstant %u32 0",
672                "{result} = OpRayQueryGetIntersectionGeometryIndexKHR %u32 {ray_query} %intersection",
673                ray_query = in(reg) self,
674                result = out(reg) result,
675            }
676
677            result
678        }
679    }
680
681    /// Gets the geometry index for the current intersection considered in a
682    /// ray query.
683    ///
684    /// There must be a current committed intersection.
685    ///
686    /// TODO: Improve docs. Can't right now due to
687    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
688    #[spirv_std_macros::gpu_only]
689    #[doc(alias = "OpRayQueryGetIntersectionGeometryIndexKHR")]
690    #[inline]
691    pub unsafe fn get_committed_intersection_geometry_index(&self) -> u32 {
692        unsafe {
693            let result;
694
695            asm! {
696                "%u32 = OpTypeInt 32 0",
697                "%intersection = OpConstant %u32 1",
698                "{result} = OpRayQueryGetIntersectionGeometryIndexKHR %u32 {ray_query} %intersection",
699                ray_query = in(reg) self,
700                result = out(reg) result,
701            }
702
703            result
704        }
705    }
706
707    /// Gets the primitive index for the current intersection considered in a
708    /// ray query.
709    ///
710    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
711    #[spirv_std_macros::gpu_only]
712    #[doc(alias = "OpRayQueryGetIntersectionPrimitiveIndexKHR")]
713    #[inline]
714    pub unsafe fn get_candidate_intersection_primitive_index(&self) -> u32 {
715        unsafe {
716            let result;
717
718            asm! {
719                "%u32 = OpTypeInt 32 0",
720                "%intersection = OpConstant %u32 0",
721                "{result} = OpRayQueryGetIntersectionPrimitiveIndexKHR %u32 {ray_query} %intersection",
722                ray_query = in(reg) self,
723                result = out(reg) result,
724            }
725
726            result
727        }
728    }
729
730    /// Gets the primitive index for the current intersection considered in a
731    /// ray query.
732    ///
733    /// There must be a current committed intersection.
734    ///
735    /// TODO: Improve docs. Can't right now due to
736    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
737    #[spirv_std_macros::gpu_only]
738    #[doc(alias = "OpRayQueryGetIntersectionPrimitiveIndexKHR")]
739    #[inline]
740    pub unsafe fn get_committed_intersection_primitive_index(&self) -> u32 {
741        unsafe {
742            let result;
743
744            asm! {
745                "%u32 = OpTypeInt 32 0",
746                "%intersection = OpConstant %u32 1",
747                "{result} = OpRayQueryGetIntersectionPrimitiveIndexKHR %u32 {ray_query} %intersection",
748                ray_query = in(reg) self,
749                result = out(reg) result,
750            }
751
752            result
753        }
754    }
755
756    /// Gets the second and third barycentric coordinates of the current
757    /// intersection considered in a ray query against the primitive it hit.
758    ///
759    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
760    /// The current intersection candidate must have a [`Self::get_candidate_intersection_type()`]
761    /// of [`CandidateIntersection::Triangle`].
762    #[spirv_std_macros::gpu_only]
763    #[doc(alias = "OpRayQueryGetIntersectionBarycentricsKHR")]
764    #[inline]
765    pub unsafe fn get_candidate_intersection_barycentrics(&self) -> Vec2 {
766        unsafe {
767            let mut result = Default::default();
768
769            asm! {
770                "%u32 = OpTypeInt 32 0",
771                "%intersection = OpConstant %u32 0",
772                "%result = OpRayQueryGetIntersectionBarycentricsKHR typeof*{result} {ray_query} %intersection",
773                "OpStore {result} %result",
774                ray_query = in(reg) self,
775                result = in(reg) &mut result,
776            }
777
778            result
779        }
780    }
781
782    /// Gets the second and third barycentric coordinates of the current
783    /// intersection considered in a ray query against the primitive it hit.
784    ///
785    /// There must be a current committed intersection. Its
786    /// [`Self::get_committed_intersection_type()`] must be [`CommittedIntersection::Triangle`].
787    ///
788    /// TODO: Improve docs. Can't right now due to
789    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
790    #[spirv_std_macros::gpu_only]
791    #[doc(alias = "OpRayQueryGetIntersectionBarycentricsKHR")]
792    #[inline]
793    pub unsafe fn get_committed_intersection_barycentrics(&self) -> Vec2 {
794        unsafe {
795            let mut result = Default::default();
796
797            asm! {
798                "%u32 = OpTypeInt 32 0",
799                "%intersection = OpConstant %u32 1",
800                "%result = OpRayQueryGetIntersectionBarycentricsKHR typeof*{result} {ray_query} %intersection",
801                "OpStore {result} %result",
802                ray_query = in(reg) self,
803                result = in(reg) &mut result,
804            }
805
806            result
807        }
808    }
809
810    /// Returns whether the current intersection considered in a ray query was with
811    /// the front face (`true`) or back face (`false`) of a primitive.
812    ///
813    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
814    /// The current intersection candidate must have a [`Self::get_candidate_intersection_type()`]
815    /// of [`CandidateIntersection::Triangle`].
816    #[spirv_std_macros::gpu_only]
817    #[doc(alias = "OpRayQueryGetIntersectionFrontFaceKHR")]
818    #[inline]
819    pub unsafe fn get_candidate_intersection_front_face(&self) -> bool {
820        unsafe {
821            let mut result = false;
822
823            asm! {
824                "%bool = OpTypeBool",
825                "%u32 = OpTypeInt 32 0",
826                "%intersection = OpConstant %u32 0",
827                "%result = OpRayQueryGetIntersectionFrontFaceKHR %bool {ray_query} %intersection",
828                "OpStore {result} %result",
829                ray_query = in(reg) self,
830                result = in(reg) &mut result,
831            }
832
833            result
834        }
835    }
836
837    /// Returns whether the current intersection considered in a ray query was with
838    /// the front face (`true`) or back face (`false`) of a primitive.
839    ///
840    /// There must be a current committed intersection. Its
841    /// [`Self::get_committed_intersection_type()`] must be [`CommittedIntersection::Triangle`].
842    ///
843    /// TODO: Improve docs. Can't right now due to
844    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
845    #[spirv_std_macros::gpu_only]
846    #[doc(alias = "OpRayQueryGetIntersectionFrontFaceKHR")]
847    #[inline]
848    pub unsafe fn get_committed_intersection_front_face(&self) -> bool {
849        unsafe {
850            let mut result = false;
851
852            asm! {
853                "%bool = OpTypeBool",
854                "%u32 = OpTypeInt 32 0",
855                "%intersection = OpConstant %u32 1",
856                "%result = OpRayQueryGetIntersectionFrontFaceKHR %bool {ray_query} %intersection",
857                "OpStore {result} %result",
858                ray_query = in(reg) self,
859                result = in(reg) &mut result,
860            }
861
862            result
863        }
864    }
865
866    /// Returns whether a candidate intersection considered in a ray query was with
867    /// an opaque AABB (Axis Aligned Bounding Box) or not.
868    #[spirv_std_macros::gpu_only]
869    #[doc(alias = "OpRayQueryGetIntersectionCandidateAABBOpaqueKHR")]
870    #[inline]
871    pub unsafe fn get_intersection_candidate_aabb_opaque(&self) -> bool {
872        unsafe {
873            let mut result = false;
874
875            asm! {
876                "%bool = OpTypeBool",
877                "%result = OpRayQueryGetIntersectionCandidateAABBOpaqueKHR %bool {ray_query}",
878                "OpStore {result} %result",
879                ray_query = in(reg) self,
880                result = in(reg) &mut result,
881            }
882
883            result
884        }
885    }
886
887    /// Gets the object-space ray direction for the current intersection considered
888    /// in a ray query.
889    ///
890    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
891    #[spirv_std_macros::gpu_only]
892    #[doc(alias = "OpRayQueryGetIntersectionObjectRayDirectionKHR")]
893    #[inline]
894    pub unsafe fn get_candidate_intersection_object_ray_direction(&self) -> Vec3 {
895        unsafe {
896            let mut result = Default::default();
897
898            asm! {
899                "%u32 = OpTypeInt 32 0",
900                "%intersection = OpConstant %u32 0",
901                "%result = OpRayQueryGetIntersectionObjectRayDirectionKHR typeof*{result} {ray_query} %intersection",
902                "OpStore {result} %result",
903                ray_query = in(reg) self,
904                result = in(reg) &mut result,
905            }
906
907            result
908        }
909    }
910
911    /// Gets the object-space ray direction for the current intersection considered
912    /// in a ray query.
913    ///
914    /// There must be a current committed intersection.
915    ///
916    /// TODO: Improve docs. Can't right now due to
917    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
918    #[spirv_std_macros::gpu_only]
919    #[doc(alias = "OpRayQueryGetIntersectionObjectRayDirectionKHR")]
920    #[inline]
921    pub unsafe fn get_committed_intersection_object_ray_direction(&self) -> Vec3 {
922        unsafe {
923            let mut result = Default::default();
924
925            asm! {
926                "%u32 = OpTypeInt 32 0",
927                "%intersection = OpConstant %u32 1",
928                "%result = OpRayQueryGetIntersectionObjectRayDirectionKHR typeof*{result} {ray_query} %intersection",
929                "OpStore {result} %result",
930                ray_query = in(reg) self,
931                result = in(reg) &mut result,
932            }
933
934            result
935        }
936    }
937
938    /// Gets the object-space ray origin for the current intersection considered in
939    /// a ray query.
940    ///
941    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
942    #[spirv_std_macros::gpu_only]
943    #[doc(alias = "OpRayQueryGetIntersectionObjectRayOriginKHR")]
944    #[inline]
945    pub unsafe fn get_candidate_intersection_object_ray_origin(&self) -> Vec3 {
946        unsafe {
947            let mut result = Default::default();
948
949            asm! {
950                "%u32 = OpTypeInt 32 0",
951                "%intersection = OpConstant %u32 0",
952                "%result = OpRayQueryGetIntersectionObjectRayOriginKHR typeof*{result} {ray_query} %intersection",
953                "OpStore {result} %result",
954                ray_query = in(reg) self,
955                result = in(reg) &mut result,
956            }
957
958            result
959        }
960    }
961
962    /// Gets the object-space ray origin for the current intersection considered in
963    /// a ray query.
964    ///
965    /// There must be a current committed intersection.
966    ///
967    /// TODO: Improve docs. Can't right now due to
968    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
969    #[spirv_std_macros::gpu_only]
970    #[doc(alias = "OpRayQueryGetIntersectionObjectRayOriginKHR")]
971    #[inline]
972    pub unsafe fn get_committed_intersection_object_ray_origin(&self) -> Vec3 {
973        unsafe {
974            let mut result = Default::default();
975
976            asm! {
977                "%u32 = OpTypeInt 32 0",
978                "%intersection = OpConstant %u32 1",
979                "%result = OpRayQueryGetIntersectionObjectRayOriginKHR typeof*{result} {ray_query} %intersection",
980                "OpStore {result} %result",
981                ray_query = in(reg) self,
982                result = in(reg) &mut result,
983            }
984
985            result
986        }
987    }
988
989    /// Gets the world-space direction for the ray traced in a ray query.
990    #[spirv_std_macros::gpu_only]
991    #[doc(alias = "OpRayQueryGetWorldRayDirectionKHR")]
992    #[inline]
993    pub unsafe fn get_world_ray_direction(&self) -> Vec3 {
994        unsafe {
995            let mut result = Default::default();
996
997            asm! {
998                "%u32 = OpTypeInt 32 0",
999                "%result = OpRayQueryGetWorldRayDirectionKHR typeof*{result} {ray_query}",
1000                "OpStore {result} %result",
1001                ray_query = in(reg) self,
1002                result = in(reg) &mut result,
1003            }
1004
1005            result
1006        }
1007    }
1008
1009    /// Gets the world-space origin for the ray traced in a ray query.
1010    #[spirv_std_macros::gpu_only]
1011    #[doc(alias = "OpRayQueryGetWorldRayOriginKHR")]
1012    #[inline]
1013    pub unsafe fn get_world_ray_origin(&self) -> Vec3 {
1014        unsafe {
1015            let mut result = Default::default();
1016
1017            asm! {
1018                "%u32 = OpTypeInt 32 0",
1019                "%result = OpRayQueryGetWorldRayOriginKHR typeof*{result} {ray_query}",
1020                "OpStore {result} %result",
1021                ray_query = in(reg) self,
1022                result = in(reg) &mut result,
1023            }
1024
1025            result
1026        }
1027    }
1028
1029    /// Gets a matrix that transforms values to world-space from the object-space of
1030    /// the current intersection considered in a ray query.
1031    ///
1032    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
1033    #[spirv_std_macros::gpu_only]
1034    #[doc(alias = "OpRayQueryGetIntersectionObjectToWorldKHR")]
1035    #[inline]
1036    pub unsafe fn get_candidate_intersection_object_to_world(&self) -> Matrix4x3 {
1037        unsafe {
1038            let mut result = Default::default();
1039
1040            asm! {
1041                "%u32 = OpTypeInt 32 0",
1042                "%intersection = OpConstant %u32 0",
1043                "%result = OpRayQueryGetIntersectionObjectToWorldKHR typeof*{result} {ray_query} %intersection",
1044                "OpStore {result} %result",
1045                ray_query = in(reg) self,
1046                result = in(reg) &mut result,
1047            }
1048
1049            result
1050        }
1051    }
1052
1053    /// Gets a matrix that transforms values to world-space from the object-space of
1054    /// the current intersection considered in a ray query.
1055    ///
1056    /// There must be a current committed intersection.
1057    ///
1058    /// TODO: Improve docs. Can't right now due to
1059    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
1060    #[spirv_std_macros::gpu_only]
1061    #[doc(alias = "OpRayQueryGetIntersectionObjectToWorldKHR")]
1062    #[inline]
1063    pub unsafe fn get_committed_intersection_object_to_world(&self) -> Matrix4x3 {
1064        unsafe {
1065            let mut result = Default::default();
1066
1067            asm! {
1068                "%u32 = OpTypeInt 32 0",
1069                "%intersection = OpConstant %u32 1",
1070                "%result = OpRayQueryGetIntersectionObjectToWorldKHR typeof*{result} {ray_query} %intersection",
1071                "OpStore {result} %result",
1072                ray_query = in(reg) self,
1073                result = in(reg) &mut result,
1074            }
1075
1076            result
1077        }
1078    }
1079
1080    /// Gets the vertex positions for the triangle at the current intersection.
1081    ///
1082    /// Requires Capability `RayQueryPositionFetchKHR` and extension `SPV_KHR_ray_tracing_position_fetch`
1083    #[spirv_std_macros::gpu_only]
1084    #[doc(alias = "OpRayQueryGetIntersectionTriangleVertexPositionsKHR")]
1085    #[inline]
1086    pub unsafe fn get_intersection_triangle_vertex_positions(&self) -> [Vec3; 3] {
1087        unsafe {
1088            let mut result = Default::default();
1089
1090            asm! {
1091                "%u32 = OpTypeInt 32 0",
1092                "%intersection = OpConstant %u32 0",
1093                "%result = OpRayQueryGetIntersectionTriangleVertexPositionsKHR typeof*{result} {ray_query} %intersection",
1094                "OpStore {result} %result",
1095                ray_query = in(reg) self,
1096                result = in(reg) &mut result,
1097            }
1098
1099            result
1100        }
1101    }
1102}