Skip to main content

mtl_gpu/pass/
compute_pass.rs

1//! Compute pass descriptor.
2
3use std::ffi::c_void;
4use std::ptr::NonNull;
5
6use mtl_foundation::Referencing;
7use mtl_sys::{msg_send_0, msg_send_1, sel};
8
9use crate::enums::DispatchType;
10
11use super::ComputePassSampleBufferAttachmentDescriptorArray;
12
13/// A compute pass descriptor that configures a compute pass.
14///
15/// C++ equivalent: `MTL::ComputePassDescriptor`
16#[repr(transparent)]
17pub struct ComputePassDescriptor(NonNull<c_void>);
18
19impl ComputePassDescriptor {
20    /// Create a ComputePassDescriptor from a raw pointer.
21    ///
22    /// # Safety
23    ///
24    /// The pointer must be a valid Metal compute pass descriptor object.
25    #[inline]
26    pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
27        NonNull::new(ptr).map(Self)
28    }
29
30    /// Get the raw pointer.
31    #[inline]
32    pub fn as_raw(&self) -> *mut c_void {
33        self.0.as_ptr()
34    }
35
36    /// Create a new compute pass descriptor.
37    ///
38    /// C++ equivalent: `ComputePassDescriptor::computePassDescriptor()`
39    pub fn new() -> Option<Self> {
40        unsafe {
41            let class = mtl_sys::class!(MTLComputePassDescriptor);
42            let ptr: *mut c_void = msg_send_0(class.as_ptr(), sel!(computePassDescriptor));
43            if ptr.is_null() {
44                return None;
45            }
46            let _: *mut c_void = msg_send_0(ptr, sel!(retain));
47            Self::from_raw(ptr)
48        }
49    }
50
51    // =========================================================================
52    // Properties
53    // =========================================================================
54
55    /// Get the dispatch type.
56    ///
57    /// C++ equivalent: `DispatchType dispatchType() const`
58    #[inline]
59    pub fn dispatch_type(&self) -> DispatchType {
60        unsafe { msg_send_0(self.as_ptr(), sel!(dispatchType)) }
61    }
62
63    /// Set the dispatch type.
64    ///
65    /// C++ equivalent: `void setDispatchType(DispatchType)`
66    #[inline]
67    pub fn set_dispatch_type(&self, dispatch_type: DispatchType) {
68        unsafe {
69            msg_send_1::<(), DispatchType>(self.as_ptr(), sel!(setDispatchType:), dispatch_type);
70        }
71    }
72
73    /// Get the sample buffer attachments array.
74    ///
75    /// C++ equivalent: `ComputePassSampleBufferAttachmentDescriptorArray* sampleBufferAttachments() const`
76    pub fn sample_buffer_attachments(
77        &self,
78    ) -> Option<ComputePassSampleBufferAttachmentDescriptorArray> {
79        unsafe {
80            let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(sampleBufferAttachments));
81            ComputePassSampleBufferAttachmentDescriptorArray::from_raw(ptr)
82        }
83    }
84}
85
86impl Default for ComputePassDescriptor {
87    fn default() -> Self {
88        Self::new().expect("failed to create ComputePassDescriptor")
89    }
90}
91
92impl Clone for ComputePassDescriptor {
93    fn clone(&self) -> Self {
94        unsafe {
95            msg_send_0::<*mut c_void>(self.as_ptr(), sel!(retain));
96        }
97        Self(self.0)
98    }
99}
100
101impl Drop for ComputePassDescriptor {
102    fn drop(&mut self) {
103        unsafe {
104            msg_send_0::<()>(self.as_ptr(), sel!(release));
105        }
106    }
107}
108
109impl Referencing for ComputePassDescriptor {
110    #[inline]
111    fn as_ptr(&self) -> *const c_void {
112        self.0.as_ptr()
113    }
114}
115
116unsafe impl Send for ComputePassDescriptor {}
117unsafe impl Sync for ComputePassDescriptor {}
118
119impl std::fmt::Debug for ComputePassDescriptor {
120    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
121        f.debug_struct("ComputePassDescriptor")
122            .field("dispatch_type", &self.dispatch_type())
123            .finish()
124    }
125}