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
use core::ptr::NonNull;
use objc2::{extern_protocol, runtime::ProtocolObject};
use crate::{
MTLAccelerationStructure, MTLBuffer, MTLCommandEncoder, MTLCounterSampleBuffer, MTLDataType, MTLFence, MTLHeap,
MTLResource, MTLResourceUsage,
};
extern_protocol!(
/// Command encoder for building and managing acceleration structures.
///
/// Availability: macOS 11.0+, iOS 14.0+, tvOS 16.0+
///
/// The encoder records operations related to acceleration structures, such as
/// building, refitting, copying, compacting, and resource/fence usage. Work
/// encoded here executes when the enclosing `MTLCommandBuffer` is committed
/// and finished by the GPU.
pub unsafe trait MTLAccelerationStructureCommandEncoder: MTLCommandEncoder {
/// Encode a build of an acceleration structure into the command buffer.
///
/// All bottom-level builds must complete before a top-level build may
/// begin. The resulting acceleration structure will not retain any
/// references to input buffers (vertex, instance, etc.).
///
/// The build is not complete until the command buffer has been
/// committed and finished executing. It is safe to encode ray tracing
/// work against the acceleration structure as long as command buffers
/// are scheduled and synchronized such that this build has completed by
/// the time ray tracing starts.
///
/// The `acceleration_structure` and `scratch_buffer` must be at least
/// the sizes returned by `MTLDevice::accelerationStructureSizesWithDescriptor`.
///
/// - acceleration_structure: Storage object to build into.
/// - descriptor: Describes the acceleration structure to build.
/// - scratch_buffer: Scratch buffer used during the build. Contents
/// may be overwritten and are undefined after the build starts/completes.
/// - scratch_buffer_offset: Byte offset into the scratch buffer.
#[unsafe(method(buildAccelerationStructure:descriptor:scratchBuffer:scratchBufferOffset:))]
#[unsafe(method_family = none)]
fn build_acceleration_structure(
&self,
acceleration_structure: &ProtocolObject<dyn MTLAccelerationStructure>,
descriptor: &crate::acceleration_structure::MTLAccelerationStructureDescriptor,
scratch_buffer: &ProtocolObject<dyn MTLBuffer>,
scratch_buffer_offset: usize,
);
/// Encode a refit of an acceleration structure (basic form).
///
/// Refitting updates the acceleration structure when geometry changes
/// and is typically much faster than rebuilding. Performance and quality
/// may degrade depending on how much geometry changes. Refitting cannot
/// be used for certain changes (e.g. adding/removing geometry).
///
/// Refitting can occur in-place by specifying the same source and
/// destination or by passing `None` for the destination. If source and
/// destination are different, they must not overlap in memory.
///
/// The destination must be at least as large as the source, unless the
/// source has been compacted, in which case the destination must be at
/// least the compacted size.
///
/// The scratch buffer must be at least the size returned by
/// `MTLDevice::accelerationStructureSizesWithDescriptor`.
///
/// - source_acceleration_structure: Acceleration structure to refit from.
/// - descriptor: Describes the acceleration structure to build/refit.
/// - destination_acceleration_structure: Optional destination structure
/// to write into; `None` or same as source refits in-place.
/// - scratch_buffer: Optional scratch buffer used during refit. Contents
/// may be overwritten and are undefined after refit starts/completes.
/// - scratch_buffer_offset: Byte offset into the scratch buffer.
#[unsafe(method(refitAccelerationStructure:descriptor:destination:scratchBuffer:scratchBufferOffset:))]
#[unsafe(method_family = none)]
fn refit_acceleration_structure(
&self,
source_acceleration_structure: &ProtocolObject<dyn MTLAccelerationStructure>,
descriptor: &crate::acceleration_structure::MTLAccelerationStructureDescriptor,
destination_acceleration_structure: Option<&ProtocolObject<dyn MTLAccelerationStructure>>,
scratch_buffer: Option<&ProtocolObject<dyn MTLBuffer>>,
scratch_buffer_offset: usize,
);
/// Encode a refit of an acceleration structure with options.
///
/// Same behavior and constraints as the basic refit method, but allows
/// specifying `options` that control which elements of the acceleration
/// structure are refit.
///
/// Availability: macOS 13.0+, iOS 16.0+
///
/// - source_acceleration_structure: Acceleration structure to refit from.
/// - descriptor: Describes the acceleration structure to build/refit.
/// - destination_acceleration_structure: Optional destination structure
/// to write into; `None` or same as source refits in-place.
/// - scratch_buffer: Optional scratch buffer used during refit.
/// - scratch_buffer_offset: Byte offset into the scratch buffer.
/// - options: Refit options to apply.
#[unsafe(method(refitAccelerationStructure:descriptor:destination:scratchBuffer:scratchBufferOffset:options:))]
#[unsafe(method_family = none)]
fn refit_acceleration_structure_with_options(
&self,
source_acceleration_structure: &ProtocolObject<dyn MTLAccelerationStructure>,
descriptor: &crate::acceleration_structure::MTLAccelerationStructureDescriptor,
destination_acceleration_structure: Option<&ProtocolObject<dyn MTLAccelerationStructure>>,
scratch_buffer: Option<&ProtocolObject<dyn MTLBuffer>>,
scratch_buffer_offset: usize,
options: crate::acceleration_structure::MTLAccelerationStructureRefitOptions,
); // Availability: API_AVAILABLE(macos(13.0), ios(16.0))
/// Copy an acceleration structure from source to destination.
///
/// Source and destination must not overlap in memory. For top-level
/// acceleration structures, references to bottom-level structures are
/// preserved.
///
/// The destination must be at least as large as the source, unless the
/// source has been compacted, in which case the destination must be at
/// least the compacted size.
#[unsafe(method(copyAccelerationStructure:toAccelerationStructure:))]
#[unsafe(method_family = none)]
fn copy_acceleration_structure(
&self,
source_acceleration_structure: &ProtocolObject<dyn MTLAccelerationStructure>,
destination_acceleration_structure: &ProtocolObject<dyn MTLAccelerationStructure>,
);
/// Compute the compacted size for an acceleration structure and write
/// it into a buffer as a 32-bit unsigned integer.
///
/// Read this size after the command buffer completes, allocate a smaller
/// acceleration structure accordingly, and then call
/// [`copy_and_compact_acceleration_structure`].
///
/// - acceleration_structure: Source acceleration structure.
/// - buffer: Destination buffer to receive the 32-bit size in bytes.
/// - offset: Byte offset into the destination buffer.
#[unsafe(method(writeCompactedAccelerationStructureSize:toBuffer:offset:))]
#[unsafe(method_family = none)]
fn write_compacted_acceleration_structure_size(
&self,
acceleration_structure: &ProtocolObject<dyn MTLAccelerationStructure>,
buffer: &ProtocolObject<dyn MTLBuffer>,
offset: usize,
);
/// Compute the compacted size for an acceleration structure and write
/// it into a buffer as either a 32-bit or 64-bit value depending on
/// `size_data_type`.
///
/// Availability: macOS 12.0+, iOS 15.0+, tvOS 16.0+
///
/// - acceleration_structure: Source acceleration structure.
/// - buffer: Destination buffer to receive the size in bytes.
/// - offset: Byte offset into the destination buffer.
/// - size_data_type: `MTLDataTypeUInt` (32-bit) or `MTLDataTypeULong` (64-bit).
#[unsafe(method(writeCompactedAccelerationStructureSize:toBuffer:offset:sizeDataType:))]
#[unsafe(method_family = none)]
fn write_compacted_acceleration_structure_size_with_type(
&self,
acceleration_structure: &ProtocolObject<dyn MTLAccelerationStructure>,
buffer: &ProtocolObject<dyn MTLBuffer>,
offset: usize,
size_data_type: MTLDataType,
); // Availability: API_AVAILABLE(macos(12.0), ios(15.0), tvos(16.0))
/// Copy and compact an acceleration structure.
///
/// Source and destination must not overlap in memory. For top-level
/// acceleration structures, references to bottom-level structures are
/// preserved.
///
/// The destination must be at least as large as the compacted size of
/// the source, as computed by
/// [`write_compacted_acceleration_structure_size`] (or the typed
/// variant).
#[unsafe(method(copyAndCompactAccelerationStructure:toAccelerationStructure:))]
#[unsafe(method_family = none)]
fn copy_and_compact_acceleration_structure(
&self,
source_acceleration_structure: &ProtocolObject<dyn MTLAccelerationStructure>,
destination_acceleration_structure: &ProtocolObject<dyn MTLAccelerationStructure>,
);
/// Update the fence to capture all GPU work so far enqueued by this encoder.
///
/// The fence is updated at build submission to maintain global order and
/// prevent deadlock. Drivers may delay fence updates until the end of
/// the encoder and may also wait on fences at the beginning of an
/// encoder. It is illegal to wait on a fence after it has been updated
/// in the same encoder.
#[unsafe(method(updateFence:))]
#[unsafe(method_family = none)]
fn update_fence(
&self,
fence: &ProtocolObject<dyn MTLFence>,
);
/// Prevent further GPU work until the fence is reached.
///
/// The fence is evaluated at build submission to maintain global order
/// and prevent deadlock. Drivers may delay fence updates until the end
/// of the encoder and may also wait on fences at the beginning of an
/// encoder. It is illegal to wait on a fence after it has been updated
/// in the same encoder.
#[unsafe(method(waitForFence:))]
#[unsafe(method_family = none)]
fn wait_for_fence(
&self,
fence: &ProtocolObject<dyn MTLFence>,
);
/// Declare that a resource may be accessed by the encoder through an
/// argument buffer.
///
/// For tracked `MTLResource`s, this protects against data hazards. Call
/// before encoding any acceleration structure commands that may access
/// the resource through an argument buffer.
///
/// Warning: Prior to iOS 13 and macOS 10.15, this does not protect
/// against data hazards. Use fences to ensure hazards are resolved on
/// older OS versions.
#[unsafe(method(useResource:usage:))]
#[unsafe(method_family = none)]
fn use_resource(
&self,
resource: &ProtocolObject<dyn MTLResource>,
usage: MTLResourceUsage,
);
/// Declare that an array of resources may be accessed by the encoder
/// through an argument buffer.
///
/// For tracked resources, this protects against data hazards. Call
/// before encoding any acceleration structure commands that may access
/// the resources through an argument buffer.
///
/// Warning: Prior to iOS 13 and macOS 10.15, this does not protect
/// against data hazards. Use fences to ensure hazards are resolved on
/// older OS versions.
///
/// Safety: `resources` must be a valid, non-null pointer to an array of
/// non-null `ProtocolObject<dyn MTLResource>` pointers of length `count`.
#[unsafe(method(useResources:count:usage:))]
#[unsafe(method_family = none)]
fn use_resources(
&self,
resources: NonNull<NonNull<ProtocolObject<dyn MTLResource>>>,
count: usize,
usage: MTLResourceUsage,
);
/// Declare that the resources allocated from a heap may be accessed as
/// read-only by the encoder through an argument buffer.
///
/// For tracked `MTLHeap`s, this protects against data hazards. Call
/// before encoding any acceleration structure commands that may access
/// resources allocated from the heap through an argument buffer. This
/// may cause all color attachments allocated from the heap to become
/// decompressed; prefer `use_resource`/`use_resources` for color
/// attachments with minimal (read-only) usage.
///
/// Warning: Prior to iOS 13 and macOS 10.15, this does not protect
/// against data hazards. Use fences to ensure hazards are resolved on
/// older OS versions.
#[unsafe(method(useHeap:))]
#[unsafe(method_family = none)]
fn use_heap(
&self,
heap: &ProtocolObject<dyn MTLHeap>,
);
/// Declare that the resources allocated from an array of heaps may be
/// accessed as read-only by the encoder through an argument buffer.
///
/// For tracked heaps, this protects against data hazards. Call before
/// encoding commands that may access resources allocated from the heaps
/// through an argument buffer. This may cause all color attachments
/// allocated from the heaps to become decompressed; prefer
/// `use_resource`/`use_resources` for color attachments with minimal
/// (read-only) usage.
///
/// Warning: Prior to iOS 13 and macOS 10.15, this does not protect
/// against data hazards. Use fences to ensure hazards are resolved on
/// older OS versions.
///
/// Safety: `heaps` must be a valid, non-null pointer to an array of
/// non-null `ProtocolObject<dyn MTLHeap>` pointers of length `count`.
#[unsafe(method(useHeaps:count:))]
#[unsafe(method_family = none)]
fn use_heaps(
&self,
heaps: NonNull<NonNull<ProtocolObject<dyn MTLHeap>>>,
count: usize,
);
/// Sample hardware counters at this point in the acceleration structure
/// encoder and store the counter sample into the sample buffer at the
/// specified index.
///
/// Passing `barrier = true` inserts a barrier before taking the sample
/// ensuring all work encoded before this operation in the encoder is
/// complete, but it does not isolate the work with respect to other
/// encoders. Passing `barrier = false` allows the sample to be taken
/// concurrently with other operations in this encoder.
///
/// In general, `true` yields more repeatable results but may reduce
/// performance; `false` is higher performance but results may be less
/// repeatable.
///
/// Availability: macOS 11.0+, iOS 14.0+, tvOS 16.0+
///
/// - sample_buffer: The counter sample buffer to sample into.
/// - sample_index: Index into the counter buffer to write the sample.
/// - barrier: Whether to insert a barrier before sampling.
#[unsafe(method(sampleCountersInBuffer:atSampleIndex:withBarrier:))]
#[unsafe(method_family = none)]
fn sample_counters_in_buffer(
&self,
sample_buffer: &ProtocolObject<dyn MTLCounterSampleBuffer>,
sample_index: usize,
barrier: bool,
); // Availability: API_AVAILABLE(macos(11.0), ios(14.0), tvos(16.0))
}
);