Skip to content

Commit c9fc9bf

Browse files
committed
move mod: add mod ray_tracing, with a clean split between ray_pipeline and ray_query
1 parent d785f34 commit c9fc9bf

38 files changed

+379
-306
lines changed

crates/spirv-std/src/arch.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ mod atomics;
1414
mod barrier;
1515
mod demote_to_helper_invocation_ext;
1616
mod derivative;
17-
mod ray_tracing;
1817
mod subgroup;
1918

2019
pub use atomics::*;
2120
pub use barrier::*;
2221
pub use demote_to_helper_invocation_ext::*;
2322
pub use derivative::*;
24-
pub use ray_tracing::*;
2523
pub use subgroup::*;
2624

2725
/// Result is true if any component of `vector` is true, otherwise result is

crates/spirv-std/src/arch/ray_tracing.rs

Lines changed: 0 additions & 92 deletions
This file was deleted.

crates/spirv-std/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ pub mod float;
9898
pub mod geometry;
9999
pub mod image;
100100
pub mod indirect_command;
101-
pub mod matrix;
102101
pub mod memory;
103102
pub mod mesh;
104103
pub mod ray_tracing;
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}

crates/spirv-std/src/matrix.rs renamed to crates/spirv-std/src/ray_tracing/matrix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! a set of common SPIR-V Matrices, used for intrinsics
1+
//! [`Matrix4x3`] is a matrix with a special layout for ray tracing
22
33
use core::fmt::{Debug, Display, Formatter};
44
use glam::{Affine3A, Mat3, Mat3A, Mat4, Vec3, Vec3A};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! Intrinsics for the different ray tracing related extensions.
2+
//!
3+
//! Provides the following modules:
4+
//! * [`ray_pipeline`]: intrinsics for ray **pipelines**
5+
//! * [`ray_query`]: intrinsics for ray **query**
6+
//!
7+
//! And the following types shared between them:
8+
//! * [`AccelerationStructure`]
9+
//! * [`RayFlags`]
10+
//! * [`Matrix4x3`]
11+
12+
mod acceleration_structure;
13+
mod matrix;
14+
pub mod ray_pipeline;
15+
pub mod ray_query;
16+
17+
pub use acceleration_structure::*;
18+
pub use matrix::*;

0 commit comments

Comments
 (0)