1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
//! Ray-tracing data types
use crate::vector::Vector;
#[cfg(target_arch = "spirv")]
use core::arch::asm;

/// An acceleration structure type which is an opaque reference to an
/// acceleration structure handle as defined in the client API specification.
#[spirv(acceleration_structure)]
#[derive(Copy, Clone)]
// HACK(eddyb) avoids "transparent newtype of `_anti_zst_padding`" misinterpretation.
#[repr(C)]
pub struct AccelerationStructure {
    // HACK(eddyb) avoids the layout becoming ZST (and being elided in one way
    // or another, before `#[spirv(acceleration_structure)]` can special-case it).
    _anti_zst_padding: core::mem::MaybeUninit<u32>,
}

impl AccelerationStructure {
    /// Converts a 64-bit integer into an [`AccelerationStructure`].
    /// # Safety
    /// The 64-bit integer must point to a valid acceleration structure.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpConvertUToAccelerationStructureKHR")]
    #[inline]
    pub unsafe fn from_u64(id: u64) -> AccelerationStructure {
        // FIXME(eddyb) `let mut result = T::default()` uses (for `asm!`), with this.
        let mut result_slot = core::mem::MaybeUninit::uninit();
        asm! {
            "%ret = OpTypeAccelerationStructureKHR",
            "%result = OpConvertUToAccelerationStructureKHR %ret {id}",
            "OpStore {result_slot} %result",
            id = in(reg) id,
            result_slot = in(reg) result_slot.as_mut_ptr(),
        }
        result_slot.assume_init()
    }

    /// Converts a vector of two 32 bit integers into an [`AccelerationStructure`].
    /// # Safety
    /// The combination must point to a valid acceleration structure.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpConvertUToAccelerationStructureKHR")]
    #[inline]
    pub unsafe fn from_vec(id: impl Vector<u32, 2>) -> AccelerationStructure {
        // FIXME(eddyb) `let mut result = T::default()` uses (for `asm!`), with this.
        let mut result_slot = core::mem::MaybeUninit::uninit();
        asm! {
            "%ret = OpTypeAccelerationStructureKHR",
            "%id = OpLoad _ {id}",
            "%result = OpConvertUToAccelerationStructureKHR %ret %id",
            "OpStore {result_slot} %result",
            id = in(reg) &id,
            result_slot = in(reg) result_slot.as_mut_ptr(),
        }
        result_slot.assume_init()
    }

    #[spirv_std_macros::gpu_only]
    /// Trace a ray into the acceleration structure.
    ///
    /// - `structure` is the descriptor for the acceleration structure to trace into.
    /// - `ray_flags` contains one or more of the Ray Flag values.
    /// - `cull_mask` is the mask to test against the instance mask. Only the 8
    ///   least-significant bits of are used by this instruction - other bits
    ///   are ignored.
    /// - `sbt_offset` and `sbt_stride` control indexing into the SBT (Shader
    ///   Binding Table) for hit shaders called from this trace. Only the 4
    ///   least-significant bits of `sbt_offset` and `sbt_stride` are used by this
    ///   instruction - other bits are ignored.
    /// - `miss_index` is the index of the miss shader to be called from this
    ///   trace call. Only the 16 least-significant bits are used by this
    ///   instruction - other bits are ignored.
    /// - `ray_origin`, `ray_tmin`, `ray_direction`, and `ray_tmax` control the
    ///   basic parameters of the ray to be traced.
    ///
    /// - `payload` is a pointer to the ray payload structure to use for this trace.
    ///   `payload` must have a storage class of `ray_payload`
    ///   or `incoming_ray_payload`.
    ///
    /// This instruction is allowed only in `ray_generation`, `closest_hit` and
    /// `miss` execution models.
    ///
    /// This instruction is a shader call instruction which may invoke shaders with
    /// the `intersection`, `any_hit`, `closest_hit`, and `miss`
    /// execution models.
    #[doc(alias = "OpTraceRayKHR")]
    #[inline]
    #[allow(clippy::too_many_arguments)]
    pub unsafe fn trace_ray<T>(
        &self,
        ray_flags: RayFlags,
        cull_mask: i32,
        sbt_offset: i32,
        sbt_stride: i32,
        miss_index: i32,
        ray_origin: impl Vector<f32, 3>,
        ray_tmin: f32,
        ray_direction: impl Vector<f32, 3>,
        ray_tmax: f32,
        payload: &mut T,
    ) {
        asm! {
            "%acceleration_structure = OpLoad _ {acceleration_structure}",
            "%ray_origin = OpLoad _ {ray_origin}",
            "%ray_direction = OpLoad _ {ray_direction}",
            "OpTraceRayKHR \
            %acceleration_structure \
            {ray_flags} \
            {cull_mask} \
            {sbt_offset} \
            {sbt_stride} \
            {miss_index} \
            %ray_origin \
            {ray_tmin} \
            %ray_direction \
            {ray_tmax} \
            {payload}",
            acceleration_structure = in(reg) self,
            ray_flags = in(reg) ray_flags.bits(),
            cull_mask = in(reg) cull_mask,
            sbt_offset = in(reg) sbt_offset,
            sbt_stride = in(reg) sbt_stride,
            miss_index = in(reg) miss_index,
            ray_origin = in(reg) &ray_origin,
            ray_tmin = in(reg) ray_tmin,
            ray_direction = in(reg) &ray_direction,
            ray_tmax = in(reg) ray_tmax,
            payload = in(reg) payload,
        }
    }
}

bitflags::bitflags! {
    /// Flags controlling the properties of an OpTraceRayKHR instruction.
    /// Despite being a mask and allowing multiple bits to be combined, it is
    /// invalid for more than one of these four bits to be set: `OPAQUE`,
    /// `NO_OPAQUE`, `CULL_OPAQUE`, `CULL_NO_OPAQUE`, only one of
    /// `CULL_BACK_FACING_TRIANGLES` and `CULL_FRONT_FACING_TRIANGLES` may
    /// be set.
    pub struct RayFlags: u32 {
        /// No flags specified.
        const NONE = 0;
        /// Force all intersections with the trace to be opaque.
        const OPAQUE = 1;
        /// Force all intersections with the trace to be non-opaque.
        const NO_OPAQUE = 2;
        /// Accept the first hit discovered.
        const TERMINATE_ON_FIRST_HIT = 4;
        /// Do not execute a closest hit shader.
        const SKIP_CLOSEST_HIT_SHADER = 8;
        /// Do not intersect with the back face of triangles.
        const CULL_BACK_FACING_TRIANGLES = 16;
        /// Do not intersect with the front face of triangles.
        const CULL_FRONT_FACING_TRIANGLES = 32;
        /// Do not intersect with opaque geometry.
        const CULL_OPAQUE = 64;
        /// Do not intersect with non-opaque geometry.
        const CULL_NO_OPAQUE = 128;
        /// Do not intersect with any triangle geometries.
        const SKIP_TRIANGLES = 256;
        /// Do not intersect with any AABB (Axis Aligned Bounding Box) geometries.
        const SKIP_AABBS = 512;
    }
}

/// Describes the type of the intersection which is currently the candidate in a ray query,
/// returned by [`RayQuery::get_candidate_intersection_type`].
#[repr(u32)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[allow(clippy::upper_case_acronyms)]
pub enum CandidateIntersection {
    /// A potential intersection with a triangle is being considered.
    Triangle = 0,
    /// A potential intersection with an axis-aligned bounding box is being considered.
    AABB = 1,
}

/// Describes the type of the intersection currently committed in a ray query, returned by
/// [`RayQuery::get_committed_intersection_type`].
#[repr(u32)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum CommittedIntersection {
    /// No intersection is committed.
    None = 0,
    /// An intersection with a triangle has been committed.
    Triangle = 1,
    /// A user-generated intersection has been committed.
    Generated = 2,
}

/// A ray query type which is an opaque object representing a ray traversal.
#[spirv(ray_query)]
// HACK(eddyb) avoids "transparent newtype of `_anti_zst_padding`" misinterpretation.
#[repr(C)]
pub struct RayQuery {
    // HACK(eddyb) avoids the layout becoming ZST (and being elided in one way
    // or another, before `#[spirv(ray_query)]` can special-case it).
    _anti_zst_padding: core::mem::MaybeUninit<u32>,
}

/// Constructs an uninitialized ray query variable. Using the syntax
/// `let (mut)? <name>`. Where `name` is the name of the ray query variable.
#[macro_export]
macro_rules! ray_query {
    (let $name:ident) => {
        $crate::ray_query!(@inner $name)
    };
    (let mut $name:ident) => {
        $crate::ray_query!(@inner $name, mut)
    };
    (@inner $name:ident $(, $mut:tt)?) => {
        let $name: &$($mut)? RayQuery = unsafe {
            let $name : *mut RayQuery;
            ::core::arch::asm! {
                "%ray_query = OpTypeRayQueryKHR",
                "%ray_query_ptr = OpTypePointer Generic %ray_query",
                "{name} = OpVariable %ray_query_ptr Function",
                name = out(reg) $name,
            }

            &$($mut)? *$name
        };
    }
}

impl RayQuery {
    /// Initialize a ray query object, defining parameters of traversal. After this
    /// call, a new ray trace can be performed with [`Self::proceed`]. Any
    /// previous traversal state stored in the object is lost.
    ///
    /// - `ray_query` is a pointer to the ray query to initialize.
    /// - `acceleration_structure` is the descriptor for the acceleration structure
    ///   to trace into.
    /// - `ray_flags` contains one or more of the Ray Flag values.
    /// - `cull_mask` is the mask to test against the instance mask.  Only the 8
    ///   least-significant bits of `cull_mask` are used by this instruction - other
    ///   bits are ignored.
    /// - `ray_origin`, `ray_tmin`, `ray_direction`, and `ray_tmax` control the
    ///   basic parameters of the ray to be traced.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryInitializeKHR")]
    #[inline]
    #[allow(clippy::too_many_arguments)]
    pub unsafe fn initialize(
        &mut self,
        acceleration_structure: &AccelerationStructure,
        ray_flags: RayFlags,
        cull_mask: u32,
        ray_origin: impl Vector<f32, 3>,
        ray_tmin: f32,
        ray_direction: impl Vector<f32, 3>,
        ray_tmax: f32,
    ) {
        asm! {
            "%acceleration_structure = OpLoad _ {acceleration_structure}",
            "%origin = OpLoad _ {ray_origin}",
            "%direction = OpLoad _ {ray_direction}",
            "OpRayQueryInitializeKHR \
                {ray_query} \
                %acceleration_structure \
                {ray_flags} \
                {cull_mask} \
                %origin \
                {ray_tmin} \
                %direction \
                {ray_tmax}",
            ray_query = in(reg) self,
            acceleration_structure = in(reg) acceleration_structure,
            ray_flags = in(reg) ray_flags.bits(),
            cull_mask = in(reg) cull_mask,
            ray_origin = in(reg) &ray_origin,
            ray_tmin = in(reg) ray_tmin,
            ray_direction = in(reg) &ray_direction,
            ray_tmax = in(reg) ray_tmax,
        }
    }

    /// Allow traversal to proceed. Returns `true` if traversal is incomplete,
    /// and `false` when it has completed. A previous call to [`Self::proceed`]
    /// with the same ray query object must not have already returned `false`.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryProceedKHR")]
    #[inline]
    pub unsafe fn proceed(&self) -> bool {
        let mut result = false;

        asm! {
            "%bool = OpTypeBool",
            "%result = OpRayQueryProceedKHR %bool {ray_query}",
            "OpStore {result} %result",
            ray_query = in(reg) self,
            result = in(reg) &mut result,
        }

        result
    }

    /// Terminates further execution of a ray query; further calls to
    /// [`Self::proceed`] will return `false`. The value returned by any prior
    /// execution of [`Self::proceed`] with the same ray query object must have
    /// been true.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryTerminateKHR")]
    #[inline]
    pub unsafe fn terminate(&self) {
        asm!("OpRayQueryTerminateKHR {}", in(reg) self)
    }

    /// Confirms a triangle intersection to be included in the determination
    /// of the closest hit for a ray query.
    ///
    /// [`Self::proceed()`] must have been called on this object, and it must
    /// have returned true. The current intersection candidate must have a
    /// [`Self::get_candidate_intersection_type()`] of
    /// [`CandidateIntersection::Triangle`].
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryConfirmIntersectionKHR")]
    #[inline]
    pub unsafe fn confirm_intersection(&self) {
        asm!("OpRayQueryConfirmIntersectionKHR {}", in(reg) self)
    }

    /// Returns the type of the current candidate intersection.
    ///
    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionTypeKHR")]
    #[inline]
    pub unsafe fn get_candidate_intersection_type(&self) -> CandidateIntersection {
        let result: u32;

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 0",
            "{result} = OpRayQueryGetIntersectionTypeKHR %u32 {ray_query} %intersection",
            ray_query = in(reg) self,
            result = out(reg) result,
        }

        match result {
            0 => CandidateIntersection::Triangle,
            1 => CandidateIntersection::AABB,
            _ => CandidateIntersection::Triangle,
        }
    }

    /// Returns the type of the current candidate intersection.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionTypeKHR")]
    #[inline]
    pub unsafe fn get_committed_intersection_type(&self) -> CommittedIntersection {
        let result: u32;

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 1",
            "{result} = OpRayQueryGetIntersectionTypeKHR %u32 {ray_query} %intersection",
            ray_query = in(reg) self,
            result = out(reg) result,
        }

        match result {
            0 => CommittedIntersection::None,
            1 => CommittedIntersection::Triangle,
            2 => CommittedIntersection::Generated,
            _ => CommittedIntersection::None,
        }
    }

    /// Returns the "Ray Tmin" value used by the ray query.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetRayTMinKHR")]
    #[inline]
    pub unsafe fn get_ray_t_min(&self) -> f32 {
        let result;

        asm! {
            "%f32 = OpTypeFloat 32",
            "{result} = OpRayQueryGetRayTMinKHR %f32 {ray_query}",
            ray_query = in(reg) self,
            result = out(reg) result,
        }

        result
    }

    /// Returns the "Ray Flags" value used by the ray query.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetRayFlagsKHR")]
    #[inline]
    pub unsafe fn get_ray_flags(&self) -> RayFlags {
        let result;

        asm! {
            "{result} = OpRayQueryGetRayFlagsKHR typeof{result} {ray_query}",
            ray_query = in(reg) self,
            result = out(reg) result,
        }

        RayFlags::from_bits_truncate(result)
    }

    /// Gets the "T" value for the current or previous intersection considered
    /// in a ray query.
    ///
    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
    /// The current intersection candidate must have a [`Self::get_candidate_intersection_type()`]
    /// of [`CandidateIntersection::Triangle`].
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionTKHR")]
    #[inline]
    pub unsafe fn get_candidate_intersection_t(&self) -> f32 {
        let result;

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 0",
            "{result} = OpRayQueryGetIntersectionTKHR typeof{result} {ray_query} %intersection",
            ray_query = in(reg) self,
            result = out(reg) result,
        }

        result
    }

    /// Gets the "T" value for the current or previous intersection considered
    /// in a ray query.
    ///
    /// There must be a current committed intersection.
    ///
    /// TODO: Improve docs. Can't right now due to
    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionTKHR")]
    #[inline]
    pub unsafe fn get_committed_intersection_t(&self) -> f32 {
        let result;

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 1",
            "{result} = OpRayQueryGetIntersectionTKHR typeof{result} {ray_query} %intersection",
            ray_query = in(reg) self,
            result = out(reg) result,
        }

        result
    }

    /// Gets the custom index of the instance for the current intersection
    /// considered in a ray query.
    ///
    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionInstanceCustomIndexKHR")]
    #[inline]
    pub unsafe fn get_candidate_intersection_instance_custom_index(&self) -> u32 {
        let result;

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 0",
            "{result} = OpRayQueryGetIntersectionInstanceCustomIndexKHR %u32 {ray_query} %intersection",
            ray_query = in(reg) self,
            result = out(reg) result,
        }

        result
    }

    /// Gets the custom index of the instance for the current intersection
    /// considered in a ray query.
    ///
    /// There must be a current committed intersection.
    ///
    /// TODO: Improve docs. Can't right now due to
    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionInstanceCustomIndexKHR")]
    #[inline]
    pub unsafe fn get_committed_intersection_instance_custom_index(&self) -> u32 {
        let result;

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 1",
            "{result} = OpRayQueryGetIntersectionInstanceCustomIndexKHR %u32 {ray_query} %intersection",
            ray_query = in(reg) self,
            result = out(reg) result,
        }

        result
    }

    /// Gets the id of the instance for the current intersection considered in a
    /// ray query.
    ///
    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionInstanceIdKHR")]
    #[inline]
    pub unsafe fn get_candidate_intersection_instance_id(&self) -> u32 {
        let result;

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 0",
            "{result} = OpRayQueryGetIntersectionInstanceIdKHR %u32 {ray_query} %intersection",
            ray_query = in(reg) self,
            result = out(reg) result,
        }

        result
    }

    /// Gets the id of the instance for the current intersection considered in a
    /// ray query.
    ///
    /// There must be a current committed intersection.
    ///
    /// TODO: Improve docs. Can't right now due to
    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionInstanceIdKHR")]
    #[inline]
    pub unsafe fn get_committed_intersection_instance_id(&self) -> u32 {
        let result;

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 1",
            "{result} = OpRayQueryGetIntersectionInstanceIdKHR %u32 {ray_query} %intersection",
            ray_query = in(reg) self,
            result = out(reg) result,
        }

        result
    }

    /// Gets the shader binding table record offset for the current intersection
    /// considered in a ray query.
    ///
    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR")]
    #[inline]
    pub unsafe fn get_candidate_intersection_shader_binding_table_record_offset(&self) -> u32 {
        let result;

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 0",
            "{result} = OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR %u32 {ray_query} %intersection",
            ray_query = in(reg) self,
            result = out(reg) result,
        }

        result
    }

    /// Gets the shader binding table record offset for the current intersection
    /// considered in a ray query.
    ///
    /// There must be a current committed intersection.
    ///
    /// TODO: Improve docs. Can't right now due to
    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR")]
    #[inline]
    pub unsafe fn get_committed_intersection_shader_binding_table_record_offset(&self) -> u32 {
        let result;

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 1",
            "{result} = OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR %u32 {ray_query} %intersection",
            ray_query = in(reg) self,
            result = out(reg) result,
        }

        result
    }

    /// Gets the geometry index for the current intersection considered in a
    /// ray query.
    ///
    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionGeometryIndexKHR")]
    #[inline]
    pub unsafe fn get_candidate_intersection_geometry_index(&self) -> u32 {
        let result;

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 0",
            "{result} = OpRayQueryGetIntersectionGeometryIndexKHR %u32 {ray_query} %intersection",
            ray_query = in(reg) self,
            result = out(reg) result,
        }

        result
    }

    /// Gets the geometry index for the current intersection considered in a
    /// ray query.
    ///
    /// There must be a current committed intersection.
    ///
    /// TODO: Improve docs. Can't right now due to
    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionGeometryIndexKHR")]
    #[inline]
    pub unsafe fn get_committed_intersection_geometry_index(&self) -> u32 {
        let result;

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 1",
            "{result} = OpRayQueryGetIntersectionGeometryIndexKHR %u32 {ray_query} %intersection",
            ray_query = in(reg) self,
            result = out(reg) result,
        }

        result
    }

    /// Gets the primitive index for the current intersection considered in a
    /// ray query.
    ///
    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionPrimitiveIndexKHR")]
    #[inline]
    pub unsafe fn get_candidate_intersection_primitive_index(&self) -> u32 {
        let result;

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 0",
            "{result} = OpRayQueryGetIntersectionPrimitiveIndexKHR %u32 {ray_query} %intersection",
            ray_query = in(reg) self,
            result = out(reg) result,
        }

        result
    }

    /// Gets the primitive index for the current intersection considered in a
    /// ray query.
    ///
    /// There must be a current committed intersection.
    ///
    /// TODO: Improve docs. Can't right now due to
    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionPrimitiveIndexKHR")]
    #[inline]
    pub unsafe fn get_committed_intersection_primitive_index(&self) -> u32 {
        let result;

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 1",
            "{result} = OpRayQueryGetIntersectionPrimitiveIndexKHR %u32 {ray_query} %intersection",
            ray_query = in(reg) self,
            result = out(reg) result,
        }

        result
    }

    /// Gets the second and third barycentric coordinates of the current
    /// intersection considered in a ray query against the primitive it hit.
    ///
    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
    /// The current intersection candidate must have a [`Self::get_candidate_intersection_type()`]
    /// of [`CandidateIntersection::Triangle`].
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionBarycentricsKHR")]
    #[inline]
    pub unsafe fn get_candidate_intersection_barycentrics<V: Vector<f32, 2>>(&self) -> V {
        let mut result = Default::default();

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 0",
            "%result = OpRayQueryGetIntersectionBarycentricsKHR typeof*{result} {ray_query} %intersection",
            "OpStore {result} %result",
            ray_query = in(reg) self,
            result = in(reg) &mut result,
        }

        result
    }

    /// Gets the second and third barycentric coordinates of the current
    /// intersection considered in a ray query against the primitive it hit.
    ///
    /// There must be a current committed intersection. Its
    /// [`Self::get_committed_intersection_type()`] must be [`CommittedIntersection::Triangle`].
    ///
    /// TODO: Improve docs. Can't right now due to
    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionBarycentricsKHR")]
    #[inline]
    pub unsafe fn get_committed_intersection_barycentrics<V: Vector<f32, 2>>(&self) -> V {
        let mut result = Default::default();

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 1",
            "%result = OpRayQueryGetIntersectionBarycentricsKHR typeof*{result} {ray_query} %intersection",
            "OpStore {result} %result",
            ray_query = in(reg) self,
            result = in(reg) &mut result,
        }

        result
    }

    /// Returns whether the current intersection considered in a ray query was with
    /// the front face (`true`) or back face (`false`) of a primitive.
    ///
    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
    /// The current intersection candidate must have a [`Self::get_candidate_intersection_type()`]
    /// of [`CandidateIntersection::Triangle`].
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionFrontFaceKHR")]
    #[inline]
    pub unsafe fn get_candidate_intersection_front_face(&self) -> bool {
        let mut result = false;

        asm! {
            "%bool = OpTypeBool",
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 0",
            "%result = OpRayQueryGetIntersectionFrontFaceKHR %bool {ray_query} %intersection",
            "OpStore {result} %result",
            ray_query = in(reg) self,
            result = in(reg) &mut result,
        }

        result
    }

    /// Returns whether the current intersection considered in a ray query was with
    /// the front face (`true`) or back face (`false`) of a primitive.
    ///
    /// There must be a current committed intersection. Its
    /// [`Self::get_committed_intersection_type()`] must be [`CommittedIntersection::Triangle`].
    ///
    /// TODO: Improve docs. Can't right now due to
    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionFrontFaceKHR")]
    #[inline]
    pub unsafe fn get_committed_intersection_front_face(&self) -> bool {
        let mut result = false;

        asm! {
            "%bool = OpTypeBool",
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 1",
            "%result = OpRayQueryGetIntersectionFrontFaceKHR %bool {ray_query} %intersection",
            "OpStore {result} %result",
            ray_query = in(reg) self,
            result = in(reg) &mut result,
        }

        result
    }

    /// Returns whether a candidate intersection considered in a ray query was with
    /// an opaque AABB (Axis Aligned Bounding Box) or not.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionCandidateAABBOpaqueKHR")]
    #[inline]
    pub unsafe fn get_intersection_candidate_aabb_opaque(&self) -> bool {
        let mut result = false;

        asm! {
            "%bool = OpTypeBool",
            "%result = OpRayQueryGetIntersectionCandidateAABBOpaqueKHR %bool {ray_query}",
            "OpStore {result} %result",
            ray_query = in(reg) self,
            result = in(reg) &mut result,
        }

        result
    }

    /// Gets the object-space ray direction for the current intersection considered
    /// in a ray query.
    ///
    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionObjectRayDirectionKHR")]
    #[inline]
    pub unsafe fn get_candidate_intersection_object_ray_direction<V: Vector<f32, 3>>(&self) -> V {
        let mut result = Default::default();

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 0",
            "%result = OpRayQueryGetIntersectionObjectRayDirectionKHR typeof*{result} {ray_query} %intersection",
            "OpStore {result} %result",
            ray_query = in(reg) self,
            result = in(reg) &mut result,
        }

        result
    }

    /// Gets the object-space ray direction for the current intersection considered
    /// in a ray query.
    ///
    /// There must be a current committed intersection.
    ///
    /// TODO: Improve docs. Can't right now due to
    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionObjectRayDirectionKHR")]
    #[inline]
    pub unsafe fn get_committed_intersection_object_ray_direction<V: Vector<f32, 3>>(&self) -> V {
        let mut result = Default::default();

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 1",
            "%result = OpRayQueryGetIntersectionObjectRayDirectionKHR typeof*{result} {ray_query} %intersection",
            "OpStore {result} %result",
            ray_query = in(reg) self,
            result = in(reg) &mut result,
        }

        result
    }

    /// Gets the object-space ray origin for the current intersection considered in
    /// a ray query.
    ///
    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionObjectRayOriginKHR")]
    #[inline]
    pub unsafe fn get_candidate_intersection_object_ray_origin<V: Vector<f32, 3>>(&self) -> V {
        let mut result = Default::default();

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 0",
            "%result = OpRayQueryGetIntersectionObjectRayOriginKHR typeof*{result} {ray_query} %intersection",
            "OpStore {result} %result",
            ray_query = in(reg) self,
            result = in(reg) &mut result,
        }

        result
    }

    /// Gets the object-space ray origin for the current intersection considered in
    /// a ray query.
    ///
    /// There must be a current committed intersection.
    ///
    /// TODO: Improve docs. Can't right now due to
    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionObjectRayOriginKHR")]
    #[inline]
    pub unsafe fn get_committed_intersection_object_ray_origin<V: Vector<f32, 3>>(&self) -> V {
        let mut result = Default::default();

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%intersection = OpConstant %u32 1",
            "%result = OpRayQueryGetIntersectionObjectRayOriginKHR typeof*{result} {ray_query} %intersection",
            "OpStore {result} %result",
            ray_query = in(reg) self,
            result = in(reg) &mut result,
        }

        result
    }

    /// Gets the world-space direction for the ray traced in a ray query.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetWorldRayDirectionKHR")]
    #[inline]
    pub unsafe fn get_world_ray_direction<V: Vector<f32, 3>>(&self) -> V {
        let mut result = Default::default();

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%result = OpRayQueryGetWorldRayDirectionKHR typeof*{result} {ray_query}",
            "OpStore {result} %result",
            ray_query = in(reg) self,
            result = in(reg) &mut result,
        }

        result
    }

    /// Gets the world-space origin for the ray traced in a ray query.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetWorldRayOriginKHR")]
    #[inline]
    pub unsafe fn get_world_ray_origin<V: Vector<f32, 3>>(&self) -> V {
        let mut result = Default::default();

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%result = OpRayQueryGetWorldRayOriginKHR typeof*{result} {ray_query}",
            "OpStore {result} %result",
            ray_query = in(reg) self,
            result = in(reg) &mut result,
        }

        result
    }

    /// Gets a matrix that transforms values to world-space from the object-space of
    /// the current intersection considered in a ray query.
    ///
    /// [`Self::proceed()`] must have been called on this object, and it must have returned true.
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionObjectToWorldKHR")]
    #[inline]
    pub unsafe fn get_candidate_intersection_object_to_world<V: Vector<f32, 3>>(&self) -> [V; 4] {
        let mut result = Default::default();

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%f32 = OpTypeFloat 32",
            "%f32x3 = OpTypeVector %f32 3",
            "%f32x3x4 = OpTypeMatrix %f32x3 4",
            "%intersection = OpConstant %u32 0",
            "%matrix = OpRayQueryGetIntersectionObjectToWorldKHR %f32x3x4 {ray_query} %intersection",
            "%col0 = OpCompositeExtract %f32x3 %matrix 0",
            "%col1 = OpCompositeExtract %f32x3 %matrix 1",
            "%col2 = OpCompositeExtract %f32x3 %matrix 2",
            "%col3 = OpCompositeExtract %f32x3 %matrix 3",
            "%result = OpCompositeConstruct typeof*{result} %col0 %col1 %col2 %col3",
            "OpStore {result} %result",
            ray_query = in(reg) self,
            result = in(reg) &mut result,
        }

        result
    }

    /// Gets a matrix that transforms values to world-space from the object-space of
    /// the current intersection considered in a ray query.
    ///
    /// There must be a current committed intersection.
    ///
    /// TODO: Improve docs. Can't right now due to
    /// <https://github.com/KhronosGroup/SPIRV-Registry/issues/128>
    #[spirv_std_macros::gpu_only]
    #[doc(alias = "OpRayQueryGetIntersectionObjectToWorldKHR")]
    #[inline]
    pub unsafe fn get_committed_intersection_object_to_world<V: Vector<f32, 3>>(&self) -> [V; 4] {
        let mut result = Default::default();

        asm! {
            "%u32 = OpTypeInt 32 0",
            "%f32 = OpTypeFloat 32",
            "%f32x3 = OpTypeVector %f32 3",
            "%f32x3x4 = OpTypeMatrix %f32x3 4",
            "%intersection = OpConstant %u32 1",
            "%matrix = OpRayQueryGetIntersectionObjectToWorldKHR %f32x3x4 {ray_query} %intersection",
            "%col0 = OpCompositeExtract %f32x3 %matrix 0",
            "%col1 = OpCompositeExtract %f32x3 %matrix 1",
            "%col2 = OpCompositeExtract %f32x3 %matrix 2",
            "%col3 = OpCompositeExtract %f32x3 %matrix 3",
            "%result = OpCompositeConstruct typeof*{result} %col0 %col1 %col2 %col3",
            "OpStore {result} %result",
            ray_query = in(reg) self,
            result = in(reg) &mut result,
        }

        result
    }
}