wgpu/api/
blas.rs

1#[cfg(wgpu_core)]
2use core::ops::Deref;
3
4use alloc::{boxed::Box, vec::Vec};
5
6use wgt::{WasmNotSend, WasmNotSendSync};
7
8use crate::dispatch;
9use crate::{Buffer, Label};
10
11/// Descriptor for the size defining attributes of a triangle geometry, for a bottom level acceleration structure.
12pub type BlasTriangleGeometrySizeDescriptor = wgt::BlasTriangleGeometrySizeDescriptor;
13static_assertions::assert_impl_all!(BlasTriangleGeometrySizeDescriptor: Send, Sync);
14
15/// Descriptor for the size defining attributes, for a bottom level acceleration structure.
16pub type BlasGeometrySizeDescriptors = wgt::BlasGeometrySizeDescriptors;
17static_assertions::assert_impl_all!(BlasGeometrySizeDescriptors: Send, Sync);
18
19/// Flags for an acceleration structure.
20pub type AccelerationStructureFlags = wgt::AccelerationStructureFlags;
21static_assertions::assert_impl_all!(AccelerationStructureFlags: Send, Sync);
22
23/// Flags for a geometry inside a bottom level acceleration structure.
24pub type AccelerationStructureGeometryFlags = wgt::AccelerationStructureGeometryFlags;
25static_assertions::assert_impl_all!(AccelerationStructureGeometryFlags: Send, Sync);
26
27/// Update mode for acceleration structure builds.
28pub type AccelerationStructureUpdateMode = wgt::AccelerationStructureUpdateMode;
29static_assertions::assert_impl_all!(AccelerationStructureUpdateMode: Send, Sync);
30
31/// Descriptor to create bottom level acceleration structures.
32pub type CreateBlasDescriptor<'a> = wgt::CreateBlasDescriptor<Label<'a>>;
33static_assertions::assert_impl_all!(CreateBlasDescriptor<'_>: Send, Sync);
34
35/// Safe instance for a [Tlas].
36///
37/// A TlasInstance may be made invalid, if a TlasInstance is invalid, any attempt to build a [Tlas] containing an
38/// invalid TlasInstance will generate a validation error
39///
40/// Each one contains:
41/// - A reference to a BLAS, this ***must*** be interacted with using [TlasInstance::new] or [TlasInstance::set_blas], a
42///   TlasInstance that references a BLAS keeps that BLAS from being dropped
43/// - A user accessible transformation matrix
44/// - A user accessible mask
45/// - A user accessible custom index
46///
47/// [Tlas]: crate::Tlas
48#[derive(Debug, Clone)]
49pub struct TlasInstance {
50    pub(crate) blas: dispatch::DispatchBlas,
51    /// Affine transform matrix 3x4 (rows x columns, row major order).
52    pub transform: [f32; 12],
53    /// Custom index for the instance used inside the shader.
54    ///
55    /// This must only use the lower 24 bits, if any bits are outside that range (byte 4 does not equal 0) the TlasInstance becomes
56    /// invalid and generates a validation error when built
57    pub custom_data: u32,
58    /// Mask for the instance used inside the shader to filter instances.
59    /// Reports hit only if `(shader_cull_mask & tlas_instance.mask) != 0u`.
60    pub mask: u8,
61}
62
63impl TlasInstance {
64    /// Construct TlasInstance.
65    /// - blas: Reference to the bottom level acceleration structure
66    /// - transform: Transform buffer offset in bytes (optional, required if transform buffer is present)
67    /// - custom_data: Custom index for the instance used inside the shader (max 24 bits)
68    /// - mask: Mask for the instance used inside the shader to filter instances
69    ///
70    /// Note: while one of these contains a reference to a BLAS that BLAS will not be dropped,
71    /// but it can still be destroyed. Destroying a BLAS that is referenced by one or more
72    /// TlasInstance(s) will immediately make them invalid. If one or more of those invalid
73    /// TlasInstances is inside a TlasPackage that is attempted to be built, the build will
74    /// generate a validation error.
75    pub fn new(blas: &Blas, transform: [f32; 12], custom_data: u32, mask: u8) -> Self {
76        Self {
77            blas: blas.inner.clone(),
78            transform,
79            custom_data,
80            mask,
81        }
82    }
83
84    /// Set the bottom level acceleration structure.
85    ///
86    /// See the note on [TlasInstance] about the
87    /// guarantees of keeping a BLAS alive.
88    pub fn set_blas(&mut self, blas: &Blas) {
89        self.blas = blas.inner.clone();
90    }
91}
92
93#[derive(Debug)]
94/// Definition for a triangle geometry for a Bottom Level Acceleration Structure (BLAS).
95///
96/// The size must match the rest of the structures fields, otherwise the build will fail.
97/// (e.g. if index count is present in the size, the index buffer must be present as well.)
98pub struct BlasTriangleGeometry<'a> {
99    /// Sub descriptor for the size defining attributes of a triangle geometry.
100    pub size: &'a BlasTriangleGeometrySizeDescriptor,
101    /// Vertex buffer.
102    pub vertex_buffer: &'a Buffer,
103    /// Offset into the vertex buffer as a factor of the vertex stride.
104    pub first_vertex: u32,
105    /// Vertex stride, must be greater than [`wgpu_types::VertexFormat::min_acceleration_structure_vertex_stride`]
106    /// of the format and must be a multiple of [`wgpu_types::VertexFormat::acceleration_structure_stride_alignment`].
107    pub vertex_stride: wgt::BufferAddress,
108    /// Index buffer (optional).
109    pub index_buffer: Option<&'a Buffer>,
110    /// Number of indexes to skip in the index buffer (optional, required if index buffer is present).
111    pub first_index: Option<u32>,
112    /// Transform buffer containing 3x4 (rows x columns, row major) affine transform matrices `[f32; 12]` (optional).
113    pub transform_buffer: Option<&'a Buffer>,
114    /// Transform buffer offset in bytes (optional, required if transform buffer is present).
115    pub transform_buffer_offset: Option<wgt::BufferAddress>,
116}
117static_assertions::assert_impl_all!(BlasTriangleGeometry<'_>: WasmNotSendSync);
118
119/// Contains the sets of geometry that go into a [Blas].
120pub enum BlasGeometries<'a> {
121    /// Triangle geometry variant.
122    TriangleGeometries(Vec<BlasTriangleGeometry<'a>>),
123}
124static_assertions::assert_impl_all!(BlasGeometries<'_>: WasmNotSendSync);
125
126/// Builds the given sets of geometry into the given [Blas].
127pub struct BlasBuildEntry<'a> {
128    /// Reference to the acceleration structure.
129    pub blas: &'a Blas,
130    /// Geometries.
131    pub geometry: BlasGeometries<'a>,
132}
133static_assertions::assert_impl_all!(BlasBuildEntry<'_>: WasmNotSendSync);
134
135#[derive(Debug, Clone)]
136/// Bottom Level Acceleration Structure (BLAS).
137///
138/// A BLAS is a device-specific raytracing acceleration structure that contains geometry data.
139///
140/// These BLASes are combined with transform in a [TlasInstance] to create a [Tlas].
141///
142/// [Tlas]: crate::Tlas
143pub struct Blas {
144    pub(crate) handle: Option<u64>,
145    pub(crate) inner: dispatch::DispatchBlas,
146}
147static_assertions::assert_impl_all!(Blas: WasmNotSendSync);
148
149crate::cmp::impl_eq_ord_hash_proxy!(Blas => .inner);
150
151impl Blas {
152    /// Raw handle to the acceleration structure, used inside raw instance buffers.
153    pub fn handle(&self) -> Option<u64> {
154        self.handle
155    }
156
157    /// Get the [`wgpu_hal`] acceleration structure from this `Blas`.
158    ///
159    /// Find the Api struct corresponding to the active backend in [`wgpu_hal::api`],
160    /// and pass that struct to the to the `A` type parameter.
161    ///
162    /// Returns a guard that dereferences to the type of the hal backend
163    /// which implements [`A::AccelerationStructure`].
164    ///
165    /// # Deadlocks
166    ///
167    /// - The returned guard holds a read-lock on a device-local "destruction"
168    ///   lock, which will cause all calls to `destroy` to block until the
169    ///   guard is released.
170    ///
171    /// # Errors
172    ///
173    /// This method will return None if:
174    /// - The acceleration structure is not from the backend specified by `A`.
175    /// - The acceleration structure is from the `webgpu` or `custom` backend.
176    ///
177    /// # Safety
178    ///
179    /// - The returned resource must not be destroyed unless the guard
180    ///   is the last reference to it and it is not in use by the GPU.
181    ///   The guard and handle may be dropped at any time however.
182    /// - All the safety requirements of wgpu-hal must be upheld.
183    ///
184    /// [`A::AccelerationStructure`]: hal::Api::AccelerationStructure
185    #[cfg(wgpu_core)]
186    pub unsafe fn as_hal<A: wgc::hal_api::HalApi>(
187        &mut self,
188    ) -> Option<impl Deref<Target = A::AccelerationStructure> + WasmNotSendSync> {
189        let blas = self.inner.as_core_opt()?;
190        unsafe { blas.context.blas_as_hal::<A>(blas) }
191    }
192
193    #[cfg(custom)]
194    /// Returns custom implementation of Blas (if custom backend and is internally T)
195    pub fn as_custom<T: crate::custom::BlasInterface>(&self) -> Option<&T> {
196        self.inner.as_custom()
197    }
198}
199
200/// Context version of [BlasTriangleGeometry].
201pub struct ContextBlasTriangleGeometry<'a> {
202    #[expect(dead_code)]
203    pub(crate) size: &'a BlasTriangleGeometrySizeDescriptor,
204    #[expect(dead_code)]
205    pub(crate) vertex_buffer: &'a dispatch::DispatchBuffer,
206    #[expect(dead_code)]
207    pub(crate) index_buffer: Option<&'a dispatch::DispatchBuffer>,
208    #[expect(dead_code)]
209    pub(crate) transform_buffer: Option<&'a dispatch::DispatchBuffer>,
210    #[expect(dead_code)]
211    pub(crate) first_vertex: u32,
212    #[expect(dead_code)]
213    pub(crate) vertex_stride: wgt::BufferAddress,
214    #[expect(dead_code)]
215    pub(crate) index_buffer_offset: Option<wgt::BufferAddress>,
216    #[expect(dead_code)]
217    pub(crate) transform_buffer_offset: Option<wgt::BufferAddress>,
218}
219
220/// Context version of [BlasGeometries].
221pub enum ContextBlasGeometries<'a> {
222    /// Triangle geometries.
223    TriangleGeometries(Box<dyn Iterator<Item = ContextBlasTriangleGeometry<'a>> + 'a>),
224}
225
226/// Context version see [BlasBuildEntry].
227pub struct ContextBlasBuildEntry<'a> {
228    #[expect(dead_code)]
229    pub(crate) blas: &'a dispatch::DispatchBlas,
230    #[expect(dead_code)]
231    pub(crate) geometries: ContextBlasGeometries<'a>,
232}
233
234/// Error occurred when trying to asynchronously prepare a blas for compaction.
235#[derive(Clone, PartialEq, Eq, Debug)]
236pub struct BlasAsyncError;
237static_assertions::assert_impl_all!(BlasAsyncError: Send, Sync);
238
239impl core::fmt::Display for BlasAsyncError {
240    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
241        write!(
242            f,
243            "Error occurred when trying to asynchronously prepare a blas for compaction"
244        )
245    }
246}
247
248impl core::error::Error for BlasAsyncError {}
249
250impl Blas {
251    /// Asynchronously prepares this BLAS for compaction. The callback is called once all builds
252    /// using this BLAS are finished and the BLAS is compactable. This can be checked using
253    /// [`Blas::ready_for_compaction`]. Rebuilding this BLAS will reset its compacted state, and it
254    /// will need to be prepared again.
255    ///
256    /// ### Interaction with other functions
257    /// On native, `queue.submit(..)` and polling devices (that is calling `instance.poll_all` or
258    /// `device.poll`) with [`PollType::Poll`] may call the callback. On native, polling devices with
259    /// [`PollType::Wait`] (or [`PollType::WaitForSubmissionIndex`] with a submission index greater
260    /// than the last submit the BLAS was used in) will guarantee callback is called.
261    ///
262    /// [`PollType::Poll`]: wgpu_types::PollType::Poll
263    /// [`PollType::Wait`]: wgpu_types::PollType::Wait
264    /// [`PollType::WaitForSubmissionIndex`]: wgpu_types::PollType::WaitForSubmissionIndex
265    pub fn prepare_compaction_async(
266        &self,
267        callback: impl FnOnce(Result<(), BlasAsyncError>) + WasmNotSend + 'static,
268    ) {
269        self.inner.prepare_compact_async(Box::new(callback));
270    }
271
272    /// Checks whether this BLAS is ready for compaction. The returned value is `true` if
273    /// [`Blas::prepare_compaction_async`]'s callback was called with a non-error value, otherwise
274    /// this is `false`.
275    pub fn ready_for_compaction(&self) -> bool {
276        self.inner.ready_for_compaction()
277    }
278}