apple-mps 0.2.5

Safe Rust bindings for Apple's MetalPerformanceShaders framework 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
436
437
438
439
use crate::ffi;
use apple_metal::{CommandBuffer, MetalBuffer, MetalDevice};
use core::ffi::c_void;
use core::ptr;

/// `MPSPolygonType` constants.
pub mod polygon_type {
    /// Wraps a `MPSPolygonType` raw value.
    pub const TRIANGLE: usize = 0;
    /// Wraps a `MPSPolygonType` raw value.
    pub const QUADRILATERAL: usize = 1;
}

/// `MPSAccelerationStructureUsage` bitflags.
pub mod acceleration_structure_usage {
    /// Wraps a `MPSAccelerationStructureUsage` raw value.
    pub const NONE: usize = 0;
    /// Wraps a `MPSAccelerationStructureUsage` raw value.
    pub const REFIT: usize = 1;
    /// Wraps a `MPSAccelerationStructureUsage` raw value.
    pub const FREQUENT_REBUILD: usize = 2;
    /// Wraps a `MPSAccelerationStructureUsage` raw value.
    pub const PREFER_GPU_BUILD: usize = 4;
    /// Wraps a `MPSAccelerationStructureUsage` raw value.
    pub const PREFER_CPU_BUILD: usize = 8;
}

/// `MPSAccelerationStructureStatus` constants.
pub mod acceleration_structure_status {
    /// Wraps a `MPSAccelerationStructureStatus` raw value.
    pub const UNBUILT: usize = 0;
    /// Wraps a `MPSAccelerationStructureStatus` raw value.
    pub const BUILT: usize = 1;
}

/// `MPSIntersectionType` constants.
pub mod intersection_type {
    /// Wraps a `MPSIntersectionType` raw value.
    pub const NEAREST: usize = 0;
    /// Wraps a `MPSIntersectionType` raw value.
    pub const ANY: usize = 1;
}

/// `MPSRayDataType` constants.
pub mod ray_data_type {
    /// Wraps a `MPSRayDataType` raw value.
    pub const ORIGIN_DIRECTION: usize = 0;
    /// Wraps a `MPSRayDataType` raw value.
    pub const ORIGIN_MIN_DISTANCE_DIRECTION_MAX_DISTANCE: usize = 1;
    /// Wraps a `MPSRayDataType` raw value.
    pub const ORIGIN_MASK_DIRECTION_MAX_DISTANCE: usize = 2;
    /// Wraps a `MPSRayDataType` raw value.
    pub const PACKED_ORIGIN_DIRECTION: usize = 3;
}

/// `MPSIntersectionDataType` constants.
pub mod intersection_data_type {
    /// Wraps a `MPSIntersectionDataType` raw value.
    pub const DISTANCE: usize = 0;
    /// Wraps a `MPSIntersectionDataType` raw value.
    pub const DISTANCE_PRIMITIVE_INDEX: usize = 1;
    /// Wraps a `MPSIntersectionDataType` raw value.
    pub const DISTANCE_PRIMITIVE_INDEX_COORDINATES: usize = 2;
    /// Wraps a `MPSIntersectionDataType` raw value.
    pub const DISTANCE_PRIMITIVE_INDEX_INSTANCE_INDEX: usize = 3;
    /// Wraps a `MPSIntersectionDataType` raw value.
    pub const DISTANCE_PRIMITIVE_INDEX_INSTANCE_INDEX_COORDINATES: usize = 4;
    /// Wraps a `MPSIntersectionDataType` raw value.
    pub const DISTANCE_PRIMITIVE_INDEX_BUFFER_INDEX: usize = 5;
    /// Wraps a `MPSIntersectionDataType` raw value.
    pub const DISTANCE_PRIMITIVE_INDEX_BUFFER_INDEX_COORDINATES: usize = 6;
    /// Wraps a `MPSIntersectionDataType` raw value.
    pub const DISTANCE_PRIMITIVE_INDEX_BUFFER_INDEX_INSTANCE_INDEX: usize = 7;
    /// Wraps a `MPSIntersectionDataType` raw value.
    pub const DISTANCE_PRIMITIVE_INDEX_BUFFER_INDEX_INSTANCE_INDEX_COORDINATES: usize = 8;
}

/// `MTLCullMode` constants.
pub mod cull_mode {
    /// Wraps a `MTLCullMode` raw value.
    pub const NONE: usize = 0;
    /// Wraps a `MTLCullMode` raw value.
    pub const FRONT: usize = 1;
    /// Wraps a `MTLCullMode` raw value.
    pub const BACK: usize = 2;
}

/// `MTLWinding` constants.
pub mod winding {
    /// Wraps a `MTLWinding` raw value.
    pub const CLOCKWISE: usize = 0;
    /// Wraps a `MTLWinding` raw value.
    pub const COUNTER_CLOCKWISE: usize = 1;
}

#[doc(hidden)]
pub use crate::generated::ray::*;

macro_rules! opaque_handle {
    ($name:ident, $doc:expr) => {
        #[doc = $doc]
        pub struct $name {
            ptr: *mut c_void,
        }

        // SAFETY: MPS handles are opaque pointers to thread-safe Swift/ObjC objects.
        unsafe impl Send for $name {}
        // SAFETY: MPS handles are opaque pointers to thread-safe Swift/ObjC objects.
        unsafe impl Sync for $name {}

        impl Drop for $name {
            fn drop(&mut self) {
                if !self.ptr.is_null() {
                    // SAFETY: `ptr` is a +1 retained MPS object owned by this wrapper.
                    unsafe { ffi::mps_object_release(self.ptr) };
                    self.ptr = ptr::null_mut();
                }
            }
        }

        impl $name {
            /// Returns the retained Objective-C pointer backing this wrapper.
            #[must_use]
            pub const fn as_ptr(&self) -> *mut c_void {
                self.ptr
            }
        }
    };
}

opaque_handle!(PolygonAccelerationStructure, "Wraps `MPSPolygonAccelerationStructure`.");
impl PolygonAccelerationStructure {
    /// Wraps a constructor on `MPSPolygonAccelerationStructure`.
    #[must_use]
    pub fn new(device: &MetalDevice) -> Option<Self> {
        let ptr = unsafe { ffi::mps_polygon_acceleration_structure_new(device.as_ptr()) };
        if ptr.is_null() {
            None
        } else {
            Some(Self { ptr })
        }
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` method.
    #[must_use]
    pub fn polygon_type(&self) -> usize {
        unsafe { ffi::mps_polygon_acceleration_structure_polygon_type(self.ptr) }
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` setter.
    pub fn set_polygon_type(&self, polygon_type: usize) {
        unsafe { ffi::mps_polygon_acceleration_structure_set_polygon_type(self.ptr, polygon_type) };
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` method.
    #[must_use]
    pub fn vertex_stride(&self) -> usize {
        unsafe { ffi::mps_polygon_acceleration_structure_vertex_stride(self.ptr) }
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` setter.
    pub fn set_vertex_stride(&self, vertex_stride: usize) {
        unsafe {
            ffi::mps_polygon_acceleration_structure_set_vertex_stride(self.ptr, vertex_stride);
        };
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` method.
    #[must_use]
    pub fn index_type(&self) -> u32 {
        unsafe { ffi::mps_polygon_acceleration_structure_index_type(self.ptr) }
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` setter.
    pub fn set_index_type(&self, index_type: u32) {
        unsafe { ffi::mps_polygon_acceleration_structure_set_index_type(self.ptr, index_type) };
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` setter.
    pub fn set_vertex_buffer(&self, buffer: Option<&MetalBuffer>) {
        let buffer_ptr = buffer.map_or(ptr::null_mut(), MetalBuffer::as_ptr);
        unsafe { ffi::mps_polygon_acceleration_structure_set_vertex_buffer(self.ptr, buffer_ptr) };
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` method.
    #[must_use]
    pub fn vertex_buffer_offset(&self) -> usize {
        unsafe { ffi::mps_polygon_acceleration_structure_vertex_buffer_offset(self.ptr) }
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` setter.
    pub fn set_vertex_buffer_offset(&self, offset: usize) {
        unsafe {
            ffi::mps_polygon_acceleration_structure_set_vertex_buffer_offset(self.ptr, offset);
        };
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` setter.
    pub fn set_index_buffer(&self, buffer: Option<&MetalBuffer>) {
        let buffer_ptr = buffer.map_or(ptr::null_mut(), MetalBuffer::as_ptr);
        unsafe { ffi::mps_polygon_acceleration_structure_set_index_buffer(self.ptr, buffer_ptr) };
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` method.
    #[must_use]
    pub fn index_buffer_offset(&self) -> usize {
        unsafe { ffi::mps_polygon_acceleration_structure_index_buffer_offset(self.ptr) }
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` setter.
    pub fn set_index_buffer_offset(&self, offset: usize) {
        unsafe {
            ffi::mps_polygon_acceleration_structure_set_index_buffer_offset(self.ptr, offset);
        };
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` method.
    #[must_use]
    pub fn polygon_count(&self) -> usize {
        unsafe { ffi::mps_polygon_acceleration_structure_polygon_count(self.ptr) }
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` setter.
    pub fn set_polygon_count(&self, count: usize) {
        unsafe { ffi::mps_polygon_acceleration_structure_set_polygon_count(self.ptr, count) };
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` method.
    #[must_use]
    pub fn usage(&self) -> usize {
        unsafe { ffi::mps_polygon_acceleration_structure_usage(self.ptr) }
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` setter.
    pub fn set_usage(&self, usage: usize) {
        unsafe { ffi::mps_polygon_acceleration_structure_set_usage(self.ptr, usage) };
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` method.
    #[must_use]
    pub fn status(&self) -> usize {
        unsafe { ffi::mps_polygon_acceleration_structure_status(self.ptr) }
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` method.
    pub fn rebuild(&self) {
        unsafe { ffi::mps_polygon_acceleration_structure_rebuild(self.ptr) };
    }

    /// Wraps the corresponding `MPSPolygonAccelerationStructure` encode entry point.
    pub fn encode_refit(&self, command_buffer: &CommandBuffer) {
        unsafe {
            ffi::mps_polygon_acceleration_structure_encode_refit(self.ptr, command_buffer.as_ptr());
        };
    }
}

opaque_handle!(RayIntersector, "Wraps `MPSRayIntersector`.");
impl RayIntersector {
    /// Wraps a constructor on `MPSRayIntersector`.
    #[must_use]
    pub fn new(device: &MetalDevice) -> Option<Self> {
        let ptr = unsafe { ffi::mps_ray_intersector_new(device.as_ptr()) };
        if ptr.is_null() {
            None
        } else {
            Some(Self { ptr })
        }
    }

    /// Wraps the corresponding `MPSRayIntersector` method.
    #[must_use]
    pub fn cull_mode(&self) -> usize {
        unsafe { ffi::mps_ray_intersector_cull_mode(self.ptr) }
    }

    /// Wraps the corresponding `MPSRayIntersector` setter.
    pub fn set_cull_mode(&self, cull_mode: usize) {
        unsafe { ffi::mps_ray_intersector_set_cull_mode(self.ptr, cull_mode) };
    }

    /// Wraps the corresponding `MPSRayIntersector` method.
    #[must_use]
    pub fn front_facing_winding(&self) -> usize {
        unsafe { ffi::mps_ray_intersector_front_facing_winding(self.ptr) }
    }

    /// Wraps the corresponding `MPSRayIntersector` setter.
    pub fn set_front_facing_winding(&self, winding: usize) {
        unsafe { ffi::mps_ray_intersector_set_front_facing_winding(self.ptr, winding) };
    }

    /// Wraps the corresponding `MPSRayIntersector` method.
    #[must_use]
    pub fn ray_stride(&self) -> usize {
        unsafe { ffi::mps_ray_intersector_ray_stride(self.ptr) }
    }

    /// Wraps the corresponding `MPSRayIntersector` setter.
    pub fn set_ray_stride(&self, stride: usize) {
        unsafe { ffi::mps_ray_intersector_set_ray_stride(self.ptr, stride) };
    }

    /// Wraps the corresponding `MPSRayIntersector` method.
    #[must_use]
    pub fn intersection_stride(&self) -> usize {
        unsafe { ffi::mps_ray_intersector_intersection_stride(self.ptr) }
    }

    /// Wraps the corresponding `MPSRayIntersector` setter.
    pub fn set_intersection_stride(&self, stride: usize) {
        unsafe { ffi::mps_ray_intersector_set_intersection_stride(self.ptr, stride) };
    }

    /// Wraps the corresponding `MPSRayIntersector` method.
    #[must_use]
    pub fn ray_data_type(&self) -> usize {
        unsafe { ffi::mps_ray_intersector_ray_data_type(self.ptr) }
    }

    /// Wraps the corresponding `MPSRayIntersector` setter.
    pub fn set_ray_data_type(&self, data_type: usize) {
        unsafe { ffi::mps_ray_intersector_set_ray_data_type(self.ptr, data_type) };
    }

    /// Wraps the corresponding `MPSRayIntersector` method.
    #[must_use]
    pub fn intersection_data_type(&self) -> usize {
        unsafe { ffi::mps_ray_intersector_intersection_data_type(self.ptr) }
    }

    /// Wraps the corresponding `MPSRayIntersector` setter.
    pub fn set_intersection_data_type(&self, data_type: usize) {
        unsafe { ffi::mps_ray_intersector_set_intersection_data_type(self.ptr, data_type) };
    }

    /// Wraps the corresponding `MPSRayIntersector` method.
    #[must_use]
    pub fn recommended_minimum_ray_batch_size(&self, ray_count: usize) -> usize {
        unsafe { ffi::mps_ray_intersector_recommended_minimum_ray_batch_size(self.ptr, ray_count) }
    }

    /// Wraps the corresponding `MPSRayIntersector` encode entry point.
    #[allow(clippy::too_many_arguments)]
    pub fn encode_intersection(
        &self,
        command_buffer: &CommandBuffer,
        intersection_type: usize,
        ray_buffer: &MetalBuffer,
        ray_buffer_offset: usize,
        intersection_buffer: &MetalBuffer,
        intersection_buffer_offset: usize,
        ray_count: usize,
        acceleration_structure: &PolygonAccelerationStructure,
    ) {
        unsafe {
            ffi::mps_ray_intersector_encode_intersection(
                self.ptr,
                command_buffer.as_ptr(),
                intersection_type,
                ray_buffer.as_ptr(),
                ray_buffer_offset,
                intersection_buffer.as_ptr(),
                intersection_buffer_offset,
                ray_count,
                acceleration_structure.as_ptr(),
            );
        };
    }
}

opaque_handle!(SVGF, "Wraps `MPSSVGF`.");
impl SVGF {
    /// Wraps a constructor on `MPSSVGF`.
    #[must_use]
    pub fn new(device: &MetalDevice) -> Option<Self> {
        let ptr = unsafe { ffi::mps_svgf_new(device.as_ptr()) };
        if ptr.is_null() {
            None
        } else {
            Some(Self { ptr })
        }
    }

    /// Wraps the corresponding `MPSSVGF` method.
    #[must_use]
    pub fn depth_weight(&self) -> f32 {
        unsafe { ffi::mps_svgf_depth_weight(self.ptr) }
    }

    /// Wraps the corresponding `MPSSVGF` setter.
    pub fn set_depth_weight(&self, value: f32) {
        unsafe { ffi::mps_svgf_set_depth_weight(self.ptr, value) };
    }

    /// Wraps the corresponding `MPSSVGF` method.
    #[must_use]
    pub fn normal_weight(&self) -> f32 {
        unsafe { ffi::mps_svgf_normal_weight(self.ptr) }
    }

    /// Wraps the corresponding `MPSSVGF` setter.
    pub fn set_normal_weight(&self, value: f32) {
        unsafe { ffi::mps_svgf_set_normal_weight(self.ptr, value) };
    }

    /// Wraps the corresponding `MPSSVGF` method.
    #[must_use]
    pub fn luminance_weight(&self) -> f32 {
        unsafe { ffi::mps_svgf_luminance_weight(self.ptr) }
    }

    /// Wraps the corresponding `MPSSVGF` setter.
    pub fn set_luminance_weight(&self, value: f32) {
        unsafe { ffi::mps_svgf_set_luminance_weight(self.ptr, value) };
    }

    /// Wraps the corresponding `MPSSVGF` method.
    #[must_use]
    pub fn channel_count(&self) -> usize {
        unsafe { ffi::mps_svgf_channel_count(self.ptr) }
    }

    /// Wraps the corresponding `MPSSVGF` setter.
    pub fn set_channel_count(&self, value: usize) {
        unsafe { ffi::mps_svgf_set_channel_count(self.ptr, value) };
    }

    /// Wraps the corresponding `MPSSVGF` method.
    #[must_use]
    pub fn channel_count2(&self) -> usize {
        unsafe { ffi::mps_svgf_channel_count2(self.ptr) }
    }

    /// Wraps the corresponding `MPSSVGF` setter.
    pub fn set_channel_count2(&self, value: usize) {
        unsafe { ffi::mps_svgf_set_channel_count2(self.ptr, value) };
    }
}