Skip to main content

mtl_gpu/mtl4/acceleration_structure/
primitive_descriptor.rs

1//! Descriptor for creating a primitive acceleration structure.
2
3use std::ffi::c_void;
4use std::ptr::NonNull;
5
6use mtl_foundation::{Referencing, UInteger};
7use mtl_sys::{msg_send_0, msg_send_1, sel};
8
9use crate::MotionBorderMode;
10
11/// Descriptor for creating a primitive acceleration structure.
12///
13/// C++ equivalent: `MTL4::PrimitiveAccelerationStructureDescriptor`
14#[repr(transparent)]
15pub struct PrimitiveAccelerationStructureDescriptor(NonNull<c_void>);
16
17impl PrimitiveAccelerationStructureDescriptor {
18    /// Create a PrimitiveAccelerationStructureDescriptor from a raw pointer.
19    #[inline]
20    pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
21        NonNull::new(ptr).map(Self)
22    }
23
24    /// Get the raw pointer.
25    #[inline]
26    pub fn as_raw(&self) -> *mut c_void {
27        self.0.as_ptr()
28    }
29
30    /// Create a new primitive acceleration structure descriptor.
31    pub fn new() -> Option<Self> {
32        unsafe {
33            let class = mtl_sys::Class::get("MTL4PrimitiveAccelerationStructureDescriptor")?;
34            let ptr: *mut c_void = msg_send_0(class.as_ptr(), sel!(alloc));
35            if ptr.is_null() {
36                return None;
37            }
38            let ptr: *mut c_void = msg_send_0(ptr, sel!(init));
39            Self::from_raw(ptr)
40        }
41    }
42
43    /// Get the geometry descriptors (as raw pointer to NSArray).
44    ///
45    /// C++ equivalent: `NS::Array* geometryDescriptors() const`
46    pub fn geometry_descriptors_raw(&self) -> *mut c_void {
47        unsafe { msg_send_0(self.as_ptr(), sel!(geometryDescriptors)) }
48    }
49
50    /// Set the geometry descriptors (from raw pointer to NSArray).
51    ///
52    /// C++ equivalent: `void setGeometryDescriptors(const NS::Array*)`
53    pub fn set_geometry_descriptors_raw(&self, descriptors: *const c_void) {
54        unsafe {
55            let _: () = msg_send_1(self.as_ptr(), sel!(setGeometryDescriptors:), descriptors);
56        }
57    }
58
59    /// Get the motion end border mode.
60    ///
61    /// C++ equivalent: `MTL::MotionBorderMode motionEndBorderMode() const`
62    pub fn motion_end_border_mode(&self) -> MotionBorderMode {
63        unsafe { msg_send_0(self.as_ptr(), sel!(motionEndBorderMode)) }
64    }
65
66    /// Set the motion end border mode.
67    ///
68    /// C++ equivalent: `void setMotionEndBorderMode(MTL::MotionBorderMode)`
69    pub fn set_motion_end_border_mode(&self, mode: MotionBorderMode) {
70        unsafe {
71            let _: () = msg_send_1(self.as_ptr(), sel!(setMotionEndBorderMode:), mode);
72        }
73    }
74
75    /// Get the motion end time.
76    ///
77    /// C++ equivalent: `float motionEndTime() const`
78    pub fn motion_end_time(&self) -> f32 {
79        unsafe { msg_send_0(self.as_ptr(), sel!(motionEndTime)) }
80    }
81
82    /// Set the motion end time.
83    ///
84    /// C++ equivalent: `void setMotionEndTime(float)`
85    pub fn set_motion_end_time(&self, time: f32) {
86        unsafe {
87            let _: () = msg_send_1(self.as_ptr(), sel!(setMotionEndTime:), time);
88        }
89    }
90
91    /// Get the motion keyframe count.
92    ///
93    /// C++ equivalent: `NS::UInteger motionKeyframeCount() const`
94    pub fn motion_keyframe_count(&self) -> UInteger {
95        unsafe { msg_send_0(self.as_ptr(), sel!(motionKeyframeCount)) }
96    }
97
98    /// Set the motion keyframe count.
99    ///
100    /// C++ equivalent: `void setMotionKeyframeCount(NS::UInteger)`
101    pub fn set_motion_keyframe_count(&self, count: UInteger) {
102        unsafe {
103            let _: () = msg_send_1(self.as_ptr(), sel!(setMotionKeyframeCount:), count);
104        }
105    }
106
107    /// Get the motion start border mode.
108    ///
109    /// C++ equivalent: `MTL::MotionBorderMode motionStartBorderMode() const`
110    pub fn motion_start_border_mode(&self) -> MotionBorderMode {
111        unsafe { msg_send_0(self.as_ptr(), sel!(motionStartBorderMode)) }
112    }
113
114    /// Set the motion start border mode.
115    ///
116    /// C++ equivalent: `void setMotionStartBorderMode(MTL::MotionBorderMode)`
117    pub fn set_motion_start_border_mode(&self, mode: MotionBorderMode) {
118        unsafe {
119            let _: () = msg_send_1(self.as_ptr(), sel!(setMotionStartBorderMode:), mode);
120        }
121    }
122
123    /// Get the motion start time.
124    ///
125    /// C++ equivalent: `float motionStartTime() const`
126    pub fn motion_start_time(&self) -> f32 {
127        unsafe { msg_send_0(self.as_ptr(), sel!(motionStartTime)) }
128    }
129
130    /// Set the motion start time.
131    ///
132    /// C++ equivalent: `void setMotionStartTime(float)`
133    pub fn set_motion_start_time(&self, time: f32) {
134        unsafe {
135            let _: () = msg_send_1(self.as_ptr(), sel!(setMotionStartTime:), time);
136        }
137    }
138}
139
140impl Default for PrimitiveAccelerationStructureDescriptor {
141    fn default() -> Self {
142        Self::new().expect("Failed to create MTL4PrimitiveAccelerationStructureDescriptor")
143    }
144}
145
146impl Clone for PrimitiveAccelerationStructureDescriptor {
147    fn clone(&self) -> Self {
148        unsafe {
149            mtl_sys::msg_send_0::<*mut c_void>(self.as_ptr(), mtl_sys::sel!(retain));
150        }
151        Self(self.0)
152    }
153}
154
155impl Drop for PrimitiveAccelerationStructureDescriptor {
156    fn drop(&mut self) {
157        unsafe {
158            mtl_sys::msg_send_0::<()>(self.as_ptr(), mtl_sys::sel!(release));
159        }
160    }
161}
162
163impl Referencing for PrimitiveAccelerationStructureDescriptor {
164    #[inline]
165    fn as_ptr(&self) -> *const c_void {
166        self.0.as_ptr()
167    }
168}
169
170unsafe impl Send for PrimitiveAccelerationStructureDescriptor {}
171unsafe impl Sync for PrimitiveAccelerationStructureDescriptor {}
172
173impl std::fmt::Debug for PrimitiveAccelerationStructureDescriptor {
174    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
175        f.debug_struct("PrimitiveAccelerationStructureDescriptor")
176            .field("motion_keyframe_count", &self.motion_keyframe_count())
177            .finish()
178    }
179}