Skip to main content

mtl_gpu/mtl4/acceleration_structure/
bounding_box_geometry.rs

1//! Descriptor for bounding box geometry in an 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 super::BufferRange;
10
11/// Descriptor for bounding box geometry in an acceleration structure.
12///
13/// C++ equivalent: `MTL4::AccelerationStructureBoundingBoxGeometryDescriptor`
14#[repr(transparent)]
15pub struct AccelerationStructureBoundingBoxGeometryDescriptor(NonNull<c_void>);
16
17impl AccelerationStructureBoundingBoxGeometryDescriptor {
18    /// Create an AccelerationStructureBoundingBoxGeometryDescriptor 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 bounding box geometry descriptor.
31    pub fn new() -> Option<Self> {
32        unsafe {
33            let class =
34                mtl_sys::Class::get("MTL4AccelerationStructureBoundingBoxGeometryDescriptor")?;
35            let ptr: *mut c_void = msg_send_0(class.as_ptr(), sel!(alloc));
36            if ptr.is_null() {
37                return None;
38            }
39            let ptr: *mut c_void = msg_send_0(ptr, sel!(init));
40            Self::from_raw(ptr)
41        }
42    }
43
44    /// Get the bounding box buffer.
45    ///
46    /// C++ equivalent: `BufferRange boundingBoxBuffer() const`
47    pub fn bounding_box_buffer(&self) -> BufferRange {
48        unsafe { msg_send_0(self.as_ptr(), sel!(boundingBoxBuffer)) }
49    }
50
51    /// Set the bounding box buffer.
52    ///
53    /// C++ equivalent: `void setBoundingBoxBuffer(const MTL4::BufferRange)`
54    pub fn set_bounding_box_buffer(&self, buffer: BufferRange) {
55        unsafe {
56            let _: () = msg_send_1(self.as_ptr(), sel!(setBoundingBoxBuffer:), buffer);
57        }
58    }
59
60    /// Get the bounding box count.
61    ///
62    /// C++ equivalent: `NS::UInteger boundingBoxCount() const`
63    pub fn bounding_box_count(&self) -> UInteger {
64        unsafe { msg_send_0(self.as_ptr(), sel!(boundingBoxCount)) }
65    }
66
67    /// Set the bounding box count.
68    ///
69    /// C++ equivalent: `void setBoundingBoxCount(NS::UInteger)`
70    pub fn set_bounding_box_count(&self, count: UInteger) {
71        unsafe {
72            let _: () = msg_send_1(self.as_ptr(), sel!(setBoundingBoxCount:), count);
73        }
74    }
75
76    /// Get the bounding box stride.
77    ///
78    /// C++ equivalent: `NS::UInteger boundingBoxStride() const`
79    pub fn bounding_box_stride(&self) -> UInteger {
80        unsafe { msg_send_0(self.as_ptr(), sel!(boundingBoxStride)) }
81    }
82
83    /// Set the bounding box stride.
84    ///
85    /// C++ equivalent: `void setBoundingBoxStride(NS::UInteger)`
86    pub fn set_bounding_box_stride(&self, stride: UInteger) {
87        unsafe {
88            let _: () = msg_send_1(self.as_ptr(), sel!(setBoundingBoxStride:), stride);
89        }
90    }
91}
92
93impl Default for AccelerationStructureBoundingBoxGeometryDescriptor {
94    fn default() -> Self {
95        Self::new()
96            .expect("Failed to create MTL4AccelerationStructureBoundingBoxGeometryDescriptor")
97    }
98}
99
100impl Clone for AccelerationStructureBoundingBoxGeometryDescriptor {
101    fn clone(&self) -> Self {
102        unsafe {
103            mtl_sys::msg_send_0::<*mut c_void>(self.as_ptr(), mtl_sys::sel!(retain));
104        }
105        Self(self.0)
106    }
107}
108
109impl Drop for AccelerationStructureBoundingBoxGeometryDescriptor {
110    fn drop(&mut self) {
111        unsafe {
112            mtl_sys::msg_send_0::<()>(self.as_ptr(), mtl_sys::sel!(release));
113        }
114    }
115}
116
117impl Referencing for AccelerationStructureBoundingBoxGeometryDescriptor {
118    #[inline]
119    fn as_ptr(&self) -> *const c_void {
120        self.0.as_ptr()
121    }
122}
123
124unsafe impl Send for AccelerationStructureBoundingBoxGeometryDescriptor {}
125unsafe impl Sync for AccelerationStructureBoundingBoxGeometryDescriptor {}
126
127impl std::fmt::Debug for AccelerationStructureBoundingBoxGeometryDescriptor {
128    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
129        f.debug_struct("AccelerationStructureBoundingBoxGeometryDescriptor")
130            .field("bounding_box_count", &self.bounding_box_count())
131            .finish()
132    }
133}