apple-metal 0.8.5

Safe Rust bindings for Apple's Metal framework — devices, resources, command encoding, advanced GPU objects, and IOSurface interop on macOS, backed by a Swift bridge
Documentation
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use crate::{
    ffi, util::take_optional_string, CommandBuffer, CommandQueue, ComputePipelineState,
    CounterSampleBuffer, DepthStencilState, Event, Fence, MetalBuffer, MetalTexture,
    RenderPipelineState, SamplerState,
};
use core::ffi::c_void;
use core::ops::Range;

macro_rules! opaque_encoder {
    ($(#[$meta:meta])* pub struct $name:ident;) => {
        $(#[$meta])*
/// Mirrors the `Metal` framework counterpart for this type.
        pub struct $name {
            ptr: *mut c_void,
        }

        impl Drop for $name {
            fn drop(&mut self) {
                if !self.ptr.is_null() {
                    unsafe { ffi::am_object_release(self.ptr) };
                    self.ptr = core::ptr::null_mut();
                }
            }
        }

        impl $name {
/// Mirrors the `Metal` framework constant `fn`.
            #[must_use]
            pub const fn as_ptr(&self) -> *mut c_void {
                self.ptr
            }

            fn wrap(ptr: *mut c_void) -> Option<Self> {
                if ptr.is_null() {
                    None
                } else {
                    Some(Self { ptr })
                }
            }
        }
    };
}

/// `MTLCommandBufferStatus` enum values.
pub mod command_buffer_status {
/// Mirrors the `Metal` framework constant `NOT_ENQUEUED`.
    pub const NOT_ENQUEUED: usize = 0;
/// Mirrors the `Metal` framework constant `ENQUEUED`.
    pub const ENQUEUED: usize = 1;
/// Mirrors the `Metal` framework constant `COMMITTED`.
    pub const COMMITTED: usize = 2;
/// Mirrors the `Metal` framework constant `SCHEDULED`.
    pub const SCHEDULED: usize = 3;
/// Mirrors the `Metal` framework constant `COMPLETED`.
    pub const COMPLETED: usize = 4;
/// Mirrors the `Metal` framework constant `ERROR`.
    pub const ERROR: usize = 5;
}

opaque_encoder!(
    /// Apple's `id<MTLBlitCommandEncoder>` — encodes buffer and texture copy work.
    pub struct BlitCommandEncoder;
);
opaque_encoder!(
    /// Apple's `id<MTLComputeCommandEncoder>` — encodes compute dispatches.
    pub struct ComputeCommandEncoder;
);
opaque_encoder!(
    /// Apple's `id<MTLRenderCommandEncoder>` — encodes render passes.
    pub struct RenderCommandEncoder;
);

impl CommandQueue {
    /// Create a new command buffer that does not retain resources it references.
    #[must_use]
    pub fn new_command_buffer_with_unretained_references(&self) -> Option<CommandBuffer> {
        let ptr = unsafe {
            ffi::am_command_queue_new_command_buffer_with_unretained_references(self.as_ptr())
        };
        if ptr.is_null() {
            None
        } else {
            Some(unsafe { CommandBuffer::from_retained_ptr(ptr) })
        }
    }
}

impl CommandBuffer {
    /// Enqueue the command buffer on its queue without immediately committing it.
    pub fn enqueue(&self) {
        unsafe { ffi::am_command_buffer_enqueue(self.as_ptr()) };
    }

    /// Block the current thread until Metal schedules the command buffer.
    pub fn wait_until_scheduled(&self) {
        unsafe { ffi::am_command_buffer_wait_until_scheduled(self.as_ptr()) };
    }

    /// Current `MTLCommandBufferStatus` value — see [`command_buffer_status`].
    #[must_use]
    pub fn status(&self) -> usize {
        unsafe { ffi::am_command_buffer_status(self.as_ptr()) }
    }

    /// Localized Metal error string for a failed command buffer.
    #[must_use]
    pub fn error(&self) -> Option<String> {
        unsafe { take_optional_string(ffi::am_command_buffer_error_message(self.as_ptr())) }
    }

    /// Create a standalone blit command encoder for this command buffer.
    #[must_use]
    pub fn new_blit_command_encoder(&self) -> Option<BlitCommandEncoder> {
        BlitCommandEncoder::wrap(unsafe {
            ffi::am_command_buffer_new_blit_command_encoder(self.as_ptr())
        })
    }

    /// Create a standalone compute command encoder for this command buffer.
    #[must_use]
    pub fn new_compute_command_encoder(&self) -> Option<ComputeCommandEncoder> {
        ComputeCommandEncoder::wrap(unsafe {
            ffi::am_command_buffer_new_compute_command_encoder(self.as_ptr())
        })
    }

    /// Create a render command encoder that renders into `texture`.
    #[must_use]
    pub fn new_render_command_encoder(
        &self,
        texture: &MetalTexture,
        load_action: usize,
        store_action: usize,
        clear_color: [f64; 4],
    ) -> Option<RenderCommandEncoder> {
        RenderCommandEncoder::wrap(unsafe {
            ffi::am_command_buffer_new_render_command_encoder(
                self.as_ptr(),
                texture.as_ptr(),
                load_action,
                store_action,
                clear_color[0],
                clear_color[1],
                clear_color[2],
                clear_color[3],
            )
        })
    }

    /// Encode a wait until `event` reaches at least `value`.
    pub fn encode_wait_for_event(&self, event: &Event, value: u64) {
        unsafe {
            ffi::am_command_buffer_encode_wait_for_event(self.as_ptr(), event.as_ptr(), value);
        };
    }

    /// Encode a signal that updates `event` to `value`.
    pub fn encode_signal_event(&self, event: &Event, value: u64) {
        unsafe { ffi::am_command_buffer_encode_signal_event(self.as_ptr(), event.as_ptr(), value) };
    }
}

impl BlitCommandEncoder {
    /// Copy `size` bytes from `src` into `dst`.
    #[must_use]
    pub fn copy_buffer(
        &self,
        src: &MetalBuffer,
        src_offset: usize,
        dst: &MetalBuffer,
        dst_offset: usize,
        size: usize,
    ) -> bool {
        unsafe {
            ffi::am_blit_command_encoder_copy_buffer(
                self.as_ptr(),
                src.as_ptr(),
                src_offset,
                dst.as_ptr(),
                dst_offset,
                size,
            )
        }
    }

    /// Fill a byte range of `buffer` with `value`.
    #[must_use]
    pub fn fill_buffer(&self, buffer: &MetalBuffer, range: Range<usize>, value: u8) -> bool {
        let length = range.end.saturating_sub(range.start);
        unsafe {
            ffi::am_blit_command_encoder_fill_buffer(
                self.as_ptr(),
                buffer.as_ptr(),
                range.start,
                length,
                value,
            )
        }
    }

    /// Sample hardware counters into `sample_buffer`.
    #[must_use]
    pub fn sample_counters(
        &self,
        sample_buffer: &CounterSampleBuffer,
        sample_index: usize,
        barrier: bool,
    ) -> bool {
        unsafe {
            ffi::am_blit_command_encoder_sample_counters(
                self.as_ptr(),
                sample_buffer.as_ptr(),
                sample_index,
                barrier,
            )
        }
    }

    /// Update `fence` with work encoded so far.
    pub fn update_fence(&self, fence: &Fence) {
        unsafe { ffi::am_blit_command_encoder_update_fence(self.as_ptr(), fence.as_ptr()) };
    }

    /// Wait for `fence` before executing subsequent work.
    pub fn wait_for_fence(&self, fence: &Fence) {
        unsafe { ffi::am_blit_command_encoder_wait_for_fence(self.as_ptr(), fence.as_ptr()) };
    }

    /// Finish encoding commands.
    pub fn end_encoding(&self) {
        unsafe { ffi::am_command_encoder_end_encoding(self.as_ptr()) };
    }
}

impl ComputeCommandEncoder {
    /// Bind a compute pipeline state.
    pub fn set_compute_pipeline_state(&self, pipeline: &ComputePipelineState) {
        unsafe {
            ffi::am_compute_command_encoder_set_pipeline_state(self.as_ptr(), pipeline.as_ptr());
        };
    }

    /// Bind a buffer at `index`.
    pub fn set_buffer(&self, buffer: &MetalBuffer, offset: usize, index: usize) {
        unsafe {
            ffi::am_compute_command_encoder_set_buffer(
                self.as_ptr(),
                buffer.as_ptr(),
                offset,
                index,
            );
        };
    }

    /// Bind a texture at `index`.
    pub fn set_texture(&self, texture: &MetalTexture, index: usize) {
        unsafe {
            ffi::am_compute_command_encoder_set_texture(self.as_ptr(), texture.as_ptr(), index);
        };
    }

    /// Bind a sampler state at `index`.
    pub fn set_sampler_state(&self, sampler: &SamplerState, index: usize) {
        unsafe {
            ffi::am_compute_command_encoder_set_sampler_state(
                self.as_ptr(),
                sampler.as_ptr(),
                index,
            );
        };
    }

    /// Bind a visible function table at `index`.
    pub fn set_visible_function_table(&self, table: &crate::VisibleFunctionTable, index: usize) {
        unsafe {
            ffi::am_compute_command_encoder_set_visible_function_table(
                self.as_ptr(),
                table.as_ptr(),
                index,
            );
        };
    }

    /// Bind an intersection function table at `index`.
    pub fn set_intersection_function_table(
        &self,
        table: &crate::IntersectionFunctionTable,
        index: usize,
    ) {
        unsafe {
            ffi::am_compute_command_encoder_set_intersection_function_table(
                self.as_ptr(),
                table.as_ptr(),
                index,
            );
        };
    }

    /// Bind an acceleration structure at `index`.
    pub fn set_acceleration_structure(
        &self,
        acceleration_structure: &crate::AccelerationStructure,
        index: usize,
    ) {
        unsafe {
            ffi::am_compute_command_encoder_set_acceleration_structure(
                self.as_ptr(),
                acceleration_structure.as_ptr(),
                index,
            );
        };
    }

    /// Dispatch threadgroups of fixed size.
    pub fn dispatch_threadgroups(
        &self,
        threadgroups: (usize, usize, usize),
        threads_per_threadgroup: (usize, usize, usize),
    ) {
        unsafe {
            ffi::am_compute_command_encoder_dispatch_threadgroups(
                self.as_ptr(),
                threadgroups.0,
                threadgroups.1,
                threadgroups.2,
                threads_per_threadgroup.0,
                threads_per_threadgroup.1,
                threads_per_threadgroup.2,
            );
        };
    }

    /// Dispatch an arbitrary thread grid.
    pub fn dispatch_threads(
        &self,
        threads: (usize, usize, usize),
        threads_per_threadgroup: (usize, usize, usize),
    ) {
        unsafe {
            ffi::am_compute_command_encoder_dispatch_threads(
                self.as_ptr(),
                threads.0,
                threads.1,
                threads.2,
                threads_per_threadgroup.0,
                threads_per_threadgroup.1,
                threads_per_threadgroup.2,
            );
        };
    }

    /// Update `fence` with work encoded so far.
    pub fn update_fence(&self, fence: &Fence) {
        unsafe { ffi::am_compute_command_encoder_update_fence(self.as_ptr(), fence.as_ptr()) };
    }

    /// Wait for `fence` before executing subsequent work.
    pub fn wait_for_fence(&self, fence: &Fence) {
        unsafe { ffi::am_compute_command_encoder_wait_for_fence(self.as_ptr(), fence.as_ptr()) };
    }

    /// Finish encoding commands.
    pub fn end_encoding(&self) {
        unsafe { ffi::am_command_encoder_end_encoding(self.as_ptr()) };
    }
}

impl RenderCommandEncoder {
    /// Bind a render pipeline state.
    pub fn set_render_pipeline_state(&self, pipeline: &RenderPipelineState) {
        unsafe {
            ffi::am_render_command_encoder_set_render_pipeline_state(
                self.as_ptr(),
                pipeline.as_ptr(),
            );
        };
    }

    /// Bind a vertex buffer at `index`.
    pub fn set_vertex_buffer(&self, buffer: &MetalBuffer, offset: usize, index: usize) {
        unsafe {
            ffi::am_render_command_encoder_set_vertex_buffer(
                self.as_ptr(),
                buffer.as_ptr(),
                offset,
                index,
            );
        };
    }

    /// Bind a fragment sampler state at `index`.
    pub fn set_fragment_sampler_state(&self, sampler: &SamplerState, index: usize) {
        unsafe {
            ffi::am_render_command_encoder_set_fragment_sampler_state(
                self.as_ptr(),
                sampler.as_ptr(),
                index,
            );
        };
    }

    /// Bind a depth/stencil state object.
    pub fn set_depth_stencil_state(&self, state: &DepthStencilState) {
        unsafe {
            ffi::am_render_command_encoder_set_depth_stencil_state(self.as_ptr(), state.as_ptr());
        };
    }

    /// Draw a non-indexed primitive range.
    pub fn draw_primitives(&self, primitive_type: usize, vertex_start: usize, vertex_count: usize) {
        unsafe {
            ffi::am_render_command_encoder_draw_primitives(
                self.as_ptr(),
                primitive_type,
                vertex_start,
                vertex_count,
            );
        };
    }

    /// Update `fence` with work encoded so far.
    pub fn update_fence(&self, fence: &Fence) {
        unsafe { ffi::am_render_command_encoder_update_fence(self.as_ptr(), fence.as_ptr()) };
    }

    /// Wait for `fence` before executing subsequent work.
    pub fn wait_for_fence(&self, fence: &Fence) {
        unsafe { ffi::am_render_command_encoder_wait_for_fence(self.as_ptr(), fence.as_ptr()) };
    }

    /// Finish encoding commands.
    pub fn end_encoding(&self) {
        unsafe { ffi::am_command_encoder_end_encoding(self.as_ptr()) };
    }
}