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
use alloc::vec::Vec;
#[cfg(any(feature = "serde", test))]
use serde::{Deserialize, Serialize};
#[cfg(doc)]
use crate::{Features, VertexFormat};
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// Descriptor for all size defining attributes of a single triangle geometry inside a bottom level acceleration structure.
pub struct BlasTriangleGeometrySizeDescriptor {
/// Format of a vertex position, must be [`VertexFormat::Float32x3`]
/// with just [`Features::EXPERIMENTAL_RAY_QUERY`]
/// but [`Features::EXTENDED_ACCELERATION_STRUCTURE_VERTEX_FORMATS`] adds more.
pub vertex_format: crate::VertexFormat,
/// Number of vertices.
pub vertex_count: u32,
/// Format of an index. Only needed if an index buffer is used.
/// If `index_format` is provided `index_count` is required.
pub index_format: Option<crate::IndexFormat>,
/// Number of indices. Only needed if an index buffer is used.
/// If `index_count` is provided `index_format` is required.
pub index_count: Option<u32>,
/// Flags for the geometry.
pub flags: AccelerationStructureGeometryFlags,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// Descriptor for size-defining attributes of one AABB geometry group in a bottom level acceleration structure.
///
/// Each primitive is one axis-aligned bounding box, typically stored as two `vec3<f32>` min/max corners.
pub struct BlasAABBGeometrySizeDescriptor {
/// Number of AABB primitives in this geometry.
pub primitive_count: u32,
/// Flags for the geometry.
pub flags: AccelerationStructureGeometryFlags,
}
/// Minimum stride for AABB geometry (24 bytes: two `vec3<f32>`).
pub const AABB_GEOMETRY_MIN_STRIDE: crate::BufferAddress = 24;
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// Descriptor for all size defining attributes of all geometries inside a bottom level acceleration structure.
pub enum BlasGeometrySizeDescriptors {
/// Triangle geometry version.
Triangles {
/// Descriptor for each triangle geometry.
descriptors: Vec<BlasTriangleGeometrySizeDescriptor>,
},
/// AABB geometry version.
AABBs {
/// Descriptor for each AABB geometry.
descriptors: Vec<BlasAABBGeometrySizeDescriptor>,
},
}
#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// Update mode for acceleration structure builds.
pub enum AccelerationStructureUpdateMode {
/// Always perform a full build.
Build,
/// If possible, perform an incremental update.
///
/// Not advised for major topology changes.
/// (Useful for e.g. skinning)
PreferUpdate,
}
#[repr(C)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// Descriptor for creating a bottom level acceleration structure.
pub struct CreateBlasDescriptor<L> {
/// Label for the bottom level acceleration structure.
pub label: L,
/// Flags for the bottom level acceleration structure.
pub flags: AccelerationStructureFlags,
/// Update mode for the bottom level acceleration structure.
pub update_mode: AccelerationStructureUpdateMode,
}
impl<L> CreateBlasDescriptor<L> {
/// Takes a closure and maps the label of the blas descriptor into another.
pub fn map_label<'a, K>(&'a self, fun: impl FnOnce(&'a L) -> K) -> CreateBlasDescriptor<K> {
CreateBlasDescriptor {
label: fun(&self.label),
flags: self.flags,
update_mode: self.update_mode,
}
}
}
#[repr(C)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// Descriptor for creating a top level acceleration structure.
pub struct CreateTlasDescriptor<L> {
/// Label for the top level acceleration structure.
pub label: L,
/// Number of instances that can be stored in the acceleration structure.
pub max_instances: u32,
/// Flags for the bottom level acceleration structure.
pub flags: AccelerationStructureFlags,
/// Update mode for the bottom level acceleration structure.
pub update_mode: AccelerationStructureUpdateMode,
}
impl<L> CreateTlasDescriptor<L> {
/// Takes a closure and maps the label of the blas descriptor into another.
pub fn map_label<'a, K>(&'a self, fun: impl FnOnce(&'a L) -> K) -> CreateTlasDescriptor<K> {
CreateTlasDescriptor {
label: fun(&self.label),
flags: self.flags,
update_mode: self.update_mode,
max_instances: self.max_instances,
}
}
}
bitflags::bitflags!(
/// Flags for acceleration structures
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct AccelerationStructureFlags: u8 {
/// Allow for incremental updates (no change in size), currently this is unimplemented
/// and will build as normal (this is fine, update vs build should be unnoticeable)
const ALLOW_UPDATE = 1 << 0;
/// Allow the acceleration structure to be compacted in a copy operation
/// (`Blas::prepare_for_compaction`, `CommandEncoder::compact_blas`).
///
/// Some backends allocate structures with this flag from memory meant for
/// short-lived resources. Only set it on structures you compact soon after building.
const ALLOW_COMPACTION = 1 << 1;
/// Optimize for fast ray tracing performance, recommended if the geometry is unlikely
/// to change (e.g. in a game: non-interactive scene geometry)
const PREFER_FAST_TRACE = 1 << 2;
/// Optimize for fast build time, recommended if geometry is likely to change frequently
/// (e.g. in a game: player model).
const PREFER_FAST_BUILD = 1 << 3;
/// Optimize for low memory footprint (both while building and in the output BLAS).
const LOW_MEMORY = 1 << 4;
/// Use `BlasTriangleGeometry::transform_buffer` when building a BLAS (only allowed in
/// BLAS creation)
const USE_TRANSFORM = 1 << 5;
/// Allow retrieval of the vertices of the triangle hit by a ray.
const ALLOW_RAY_HIT_VERTEX_RETURN = 1 << 6;
}
);
bitflags::bitflags!(
/// Flags for acceleration structure geometries
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct AccelerationStructureGeometryFlags: u8 {
/// Is OPAQUE (there is no alpha test). This disables any-hit shaders on a ray tracing pipeline
/// and automatically commits triangle intersections when using ray queries, meaning it does not
/// have to return to user code.
const OPAQUE = 1 << 0;
/// Without this flag, any primitive (triangle or AABB) may invoke multiple any-hit shaders
/// (an implementation could, for example, split primitives if they got too large), if this
/// flag in present only one hit on a primitive may invoke an any-hit shader. This is
/// recommended if the any-hit shader is expensive, but you cannot use
/// [`AccelerationStructureGeometryFlags::OPAQUE`]. With ray queries, this likewise limits
/// each primitive to returning at most one candidate intersection.
const NO_DUPLICATE_ANY_HIT_INVOCATION = 1 << 1;
}
);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
/// What a copy between acceleration structures should do
pub enum AccelerationStructureCopy {
/// Directly duplicate an acceleration structure to another
Clone,
/// Duplicate and compact an acceleration structure
Compact,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
/// What type the data of an acceleration structure is
pub enum AccelerationStructureType {
/// The types of the acceleration structure are triangles
Triangles,
/// The types of the acceleration structure are axis aligned bounding boxes
AABBs,
/// The types of the acceleration structure are instances
Instances,
}
/// Alignment requirement for transform buffers used in acceleration structure builds
pub const TRANSFORM_BUFFER_ALIGNMENT: crate::BufferAddress = 16;
/// Alignment requirement for instance buffers used in acceleration structure builds (`build_acceleration_structures_unsafe_tlas`)
pub const INSTANCE_BUFFER_ALIGNMENT: crate::BufferAddress = 16;
bitflags::bitflags! {
/// Flags for the instances in top level acceleration structures
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct AccelerationStructureInstanceFlags: u8 {
/// Changes the front face from the default of clockwise
/// to counterclockwise.
const FRONT_FACE_CCW = 1 << 0;
/// Forces the instance to be treated as if it was opaque. This overrides
/// [AccelerationStructureGeometryFlags] and is in turn overridden by the ray flags in
/// the shader.
const FORCE_OPAQUE = 1 << 1;
/// Forces the instance to be treated as if it was not opaque. This overrides
/// [AccelerationStructureGeometryFlags] and is in turn overridden by the ray flags in
/// the shader.
const FORCE_NO_OPAQUE = 1 << 2;
}
}