spirv_std/arch/
ray_tracing.rs

1#[cfg(target_arch = "spirv")]
2use core::arch::asm;
3
4/// Reports an intersection back to the traversal infrastructure.
5///
6/// If the intersection occurred within the current ray interval, the
7/// intersection confirmation is performed (see the API specification for more
8/// details). If the value of Hit falls outside the current ray interval, the
9/// hit is rejected.
10///
11/// Returns True if the hit was accepted by the ray interval and the intersection was confirmed. Returns False otherwise.
12///
13/// - `hit` is the floating point parametric value along ray for the intersection.
14/// - `hit_kind` is the integer hit kind reported back to other shaders and
15///   accessible by the `hit kind` builtin.
16///
17/// This instruction is allowed only in `IntersectionKHR` execution model.
18///
19/// This instruction is a shader call instruction which may invoke shaders with
20/// the `any_hit` execution model.
21#[spirv_std_macros::gpu_only]
22#[doc(alias = "OpReportIntersectionKHR")]
23#[inline]
24pub unsafe fn report_intersection(hit: f32, hit_kind: u32) -> bool {
25    unsafe {
26        let mut result = false;
27
28        asm! {
29            "%bool = OpTypeBool",
30            "%result = OpReportIntersectionKHR %bool {hit} {hit_kind}",
31            "OpStore {result} %result",
32            result = in(reg) &mut result,
33            hit = in(reg) hit,
34            hit_kind = in(reg) hit_kind,
35        };
36
37        result
38    }
39}
40
41/// Ignores the current potential intersection, terminating the invocation that
42/// executes it, and continues the ray traversal.  This instruction is allowed
43/// only in `any_hit` execution model.
44#[spirv_std_macros::gpu_only]
45#[doc(alias = "OpIgnoreIntersectionKHR")]
46#[inline]
47pub unsafe fn ignore_intersection() -> ! {
48    unsafe {
49        asm!("OpIgnoreIntersectionKHR", options(noreturn));
50    }
51}
52
53/// Terminates the invocation that executes it, stops the ray traversal, accepts
54/// the current hit, and invokes the `closest_hit` execution model
55/// (if active). This instruction is allowed only in the `any_hit`
56/// execution model.
57#[spirv_std_macros::gpu_only]
58#[doc(alias = "OpTerminateRayKHR")]
59#[inline]
60pub unsafe fn terminate_ray() -> ! {
61    unsafe {
62        asm!("OpTerminateRayKHR", options(noreturn));
63    }
64}
65
66/// Invoke a callable shader.
67///
68/// - `INDEX` is the index into the SBT table to select callable shader
69///   to execute.
70/// - `data` is a pointer to the callable data to pass into the called shader.
71///   `data` must have a storage class of `callable_data`
72///   or `incoming_callable_data`.
73///
74/// This instruction is allowed only in `ray_generation`, `closest_hit`,
75/// `miss` and `callable` execution models.
76///
77/// This instruction is a shader call instruction which will invoke a shader
78/// with the `callable` execution model.
79#[spirv_std_macros::gpu_only]
80#[doc(alias = "OpExecuteCallableKHR")]
81#[inline]
82pub unsafe fn execute_callable<T, const ID: usize>(data: &T) {
83    unsafe {
84        asm! {
85            "%u32 = OpTypeInt 32 0",
86            "%id = OpConstant %u32 {id}",
87            "OpExecuteCallableKHR %id {data}",
88            id = const ID,
89            data = in(reg) data,
90        };
91    }
92}