Skip to main content

mtl_gpu/pass/
render_sample_buffer.rs

1//! Render pass sample buffer attachment descriptors.
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
9/// A sample buffer attachment for a render pass.
10///
11/// C++ equivalent: `MTL::RenderPassSampleBufferAttachmentDescriptor`
12///
13/// Note: This type has separate indices for vertex and fragment stages.
14#[repr(transparent)]
15pub struct RenderPassSampleBufferAttachmentDescriptor(pub(crate) NonNull<c_void>);
16
17impl RenderPassSampleBufferAttachmentDescriptor {
18    /// Create a new render pass sample buffer attachment descriptor.
19    ///
20    /// C++ equivalent: `static RenderPassSampleBufferAttachmentDescriptor* alloc()->init()`
21    pub fn new() -> Option<Self> {
22        unsafe {
23            let class = mtl_sys::Class::get("MTLRenderPassSampleBufferAttachmentDescriptor")?;
24            let ptr: *mut c_void = msg_send_0(class.as_ptr(), sel!(alloc));
25            if ptr.is_null() {
26                return None;
27            }
28            let ptr: *mut c_void = msg_send_0(ptr, sel!(init));
29            Self::from_raw(ptr)
30        }
31    }
32
33    /// Create from a raw pointer.
34    #[inline]
35    pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
36        NonNull::new(ptr).map(Self)
37    }
38
39    /// Get the raw pointer.
40    #[inline]
41    pub fn as_raw(&self) -> *mut c_void {
42        self.0.as_ptr()
43    }
44
45    /// Get the sample buffer.
46    ///
47    /// C++ equivalent: `CounterSampleBuffer* sampleBuffer() const`
48    pub fn sample_buffer(&self) -> Option<crate::CounterSampleBuffer> {
49        unsafe {
50            let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(sampleBuffer));
51            if ptr.is_null() {
52                return None;
53            }
54            msg_send_0::<*mut c_void>(ptr, sel!(retain));
55            crate::CounterSampleBuffer::from_raw(ptr)
56        }
57    }
58
59    /// Set the sample buffer.
60    ///
61    /// C++ equivalent: `void setSampleBuffer(const MTL::CounterSampleBuffer* sampleBuffer)`
62    pub fn set_sample_buffer(&self, sample_buffer: Option<&crate::CounterSampleBuffer>) {
63        unsafe {
64            let ptr = sample_buffer.map_or(std::ptr::null(), |s| s.as_ptr());
65            let _: () = msg_send_1(self.as_ptr(), sel!(setSampleBuffer:), ptr);
66        }
67    }
68
69    /// Get the start of vertex sample index.
70    ///
71    /// C++ equivalent: `NS::UInteger startOfVertexSampleIndex() const`
72    #[inline]
73    pub fn start_of_vertex_sample_index(&self) -> UInteger {
74        unsafe { msg_send_0(self.as_ptr(), sel!(startOfVertexSampleIndex)) }
75    }
76
77    /// Set the start of vertex sample index.
78    ///
79    /// C++ equivalent: `void setStartOfVertexSampleIndex(NS::UInteger startOfVertexSampleIndex)`
80    #[inline]
81    pub fn set_start_of_vertex_sample_index(&self, index: UInteger) {
82        unsafe {
83            let _: () = msg_send_1(self.as_ptr(), sel!(setStartOfVertexSampleIndex:), index);
84        }
85    }
86
87    /// Get the end of vertex sample index.
88    ///
89    /// C++ equivalent: `NS::UInteger endOfVertexSampleIndex() const`
90    #[inline]
91    pub fn end_of_vertex_sample_index(&self) -> UInteger {
92        unsafe { msg_send_0(self.as_ptr(), sel!(endOfVertexSampleIndex)) }
93    }
94
95    /// Set the end of vertex sample index.
96    ///
97    /// C++ equivalent: `void setEndOfVertexSampleIndex(NS::UInteger endOfVertexSampleIndex)`
98    #[inline]
99    pub fn set_end_of_vertex_sample_index(&self, index: UInteger) {
100        unsafe {
101            let _: () = msg_send_1(self.as_ptr(), sel!(setEndOfVertexSampleIndex:), index);
102        }
103    }
104
105    /// Get the start of fragment sample index.
106    ///
107    /// C++ equivalent: `NS::UInteger startOfFragmentSampleIndex() const`
108    #[inline]
109    pub fn start_of_fragment_sample_index(&self) -> UInteger {
110        unsafe { msg_send_0(self.as_ptr(), sel!(startOfFragmentSampleIndex)) }
111    }
112
113    /// Set the start of fragment sample index.
114    ///
115    /// C++ equivalent: `void setStartOfFragmentSampleIndex(NS::UInteger startOfFragmentSampleIndex)`
116    #[inline]
117    pub fn set_start_of_fragment_sample_index(&self, index: UInteger) {
118        unsafe {
119            let _: () = msg_send_1(self.as_ptr(), sel!(setStartOfFragmentSampleIndex:), index);
120        }
121    }
122
123    /// Get the end of fragment sample index.
124    ///
125    /// C++ equivalent: `NS::UInteger endOfFragmentSampleIndex() const`
126    #[inline]
127    pub fn end_of_fragment_sample_index(&self) -> UInteger {
128        unsafe { msg_send_0(self.as_ptr(), sel!(endOfFragmentSampleIndex)) }
129    }
130
131    /// Set the end of fragment sample index.
132    ///
133    /// C++ equivalent: `void setEndOfFragmentSampleIndex(NS::UInteger endOfFragmentSampleIndex)`
134    #[inline]
135    pub fn set_end_of_fragment_sample_index(&self, index: UInteger) {
136        unsafe {
137            let _: () = msg_send_1(self.as_ptr(), sel!(setEndOfFragmentSampleIndex:), index);
138        }
139    }
140}
141
142impl Clone for RenderPassSampleBufferAttachmentDescriptor {
143    fn clone(&self) -> Self {
144        unsafe {
145            msg_send_0::<*mut c_void>(self.as_ptr(), sel!(retain));
146        }
147        Self(self.0)
148    }
149}
150
151impl Drop for RenderPassSampleBufferAttachmentDescriptor {
152    fn drop(&mut self) {
153        unsafe {
154            msg_send_0::<()>(self.as_ptr(), sel!(release));
155        }
156    }
157}
158
159impl Referencing for RenderPassSampleBufferAttachmentDescriptor {
160    #[inline]
161    fn as_ptr(&self) -> *const c_void {
162        self.0.as_ptr()
163    }
164}
165
166unsafe impl Send for RenderPassSampleBufferAttachmentDescriptor {}
167unsafe impl Sync for RenderPassSampleBufferAttachmentDescriptor {}
168
169impl std::fmt::Debug for RenderPassSampleBufferAttachmentDescriptor {
170    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
171        f.debug_struct("RenderPassSampleBufferAttachmentDescriptor")
172            .field(
173                "start_of_vertex_sample_index",
174                &self.start_of_vertex_sample_index(),
175            )
176            .field(
177                "end_of_vertex_sample_index",
178                &self.end_of_vertex_sample_index(),
179            )
180            .field(
181                "start_of_fragment_sample_index",
182                &self.start_of_fragment_sample_index(),
183            )
184            .field(
185                "end_of_fragment_sample_index",
186                &self.end_of_fragment_sample_index(),
187            )
188            .finish()
189    }
190}
191
192/// An array of render pass sample buffer attachment descriptors.
193///
194/// C++ equivalent: `MTL::RenderPassSampleBufferAttachmentDescriptorArray`
195#[repr(transparent)]
196pub struct RenderPassSampleBufferAttachmentDescriptorArray(pub(crate) NonNull<c_void>);
197
198impl RenderPassSampleBufferAttachmentDescriptorArray {
199    /// Create a new array.
200    ///
201    /// C++ equivalent: `static RenderPassSampleBufferAttachmentDescriptorArray* alloc()->init()`
202    pub fn new() -> Option<Self> {
203        unsafe {
204            let class =
205                mtl_sys::Class::get("MTLRenderPassSampleBufferAttachmentDescriptorArray")?;
206            let ptr: *mut c_void = msg_send_0(class.as_ptr(), sel!(alloc));
207            if ptr.is_null() {
208                return None;
209            }
210            let ptr: *mut c_void = msg_send_0(ptr, sel!(init));
211            Self::from_raw(ptr)
212        }
213    }
214
215    /// Create from a raw pointer.
216    #[inline]
217    pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
218        NonNull::new(ptr).map(Self)
219    }
220
221    /// Get the raw pointer.
222    #[inline]
223    pub fn as_raw(&self) -> *mut c_void {
224        self.0.as_ptr()
225    }
226
227    /// Get the descriptor at the specified index.
228    ///
229    /// C++ equivalent: `RenderPassSampleBufferAttachmentDescriptor* object(NS::UInteger attachmentIndex)`
230    pub fn object(&self, index: UInteger) -> Option<RenderPassSampleBufferAttachmentDescriptor> {
231        unsafe {
232            let ptr: *mut c_void =
233                msg_send_1(self.as_ptr(), sel!(objectAtIndexedSubscript:), index);
234            if ptr.is_null() {
235                return None;
236            }
237            msg_send_0::<*mut c_void>(ptr, sel!(retain));
238            RenderPassSampleBufferAttachmentDescriptor::from_raw(ptr)
239        }
240    }
241
242    /// Set the descriptor at the specified index.
243    ///
244    /// C++ equivalent: `void setObject(const MTL::RenderPassSampleBufferAttachmentDescriptor* attachment, NS::UInteger attachmentIndex)`
245    pub fn set_object(
246        &self,
247        attachment: Option<&RenderPassSampleBufferAttachmentDescriptor>,
248        index: UInteger,
249    ) {
250        unsafe {
251            let ptr = attachment.map_or(std::ptr::null(), |a| a.as_ptr());
252            let _: () = mtl_sys::msg_send_2(
253                self.as_ptr(),
254                sel!(setObject:atIndexedSubscript:),
255                ptr,
256                index,
257            );
258        }
259    }
260}
261
262impl Clone for RenderPassSampleBufferAttachmentDescriptorArray {
263    fn clone(&self) -> Self {
264        unsafe {
265            msg_send_0::<*mut c_void>(self.as_ptr(), sel!(retain));
266        }
267        Self(self.0)
268    }
269}
270
271impl Drop for RenderPassSampleBufferAttachmentDescriptorArray {
272    fn drop(&mut self) {
273        unsafe {
274            msg_send_0::<()>(self.as_ptr(), sel!(release));
275        }
276    }
277}
278
279impl Referencing for RenderPassSampleBufferAttachmentDescriptorArray {
280    #[inline]
281    fn as_ptr(&self) -> *const c_void {
282        self.0.as_ptr()
283    }
284}
285
286unsafe impl Send for RenderPassSampleBufferAttachmentDescriptorArray {}
287unsafe impl Sync for RenderPassSampleBufferAttachmentDescriptorArray {}
288
289impl std::fmt::Debug for RenderPassSampleBufferAttachmentDescriptorArray {
290    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
291        f.debug_struct("RenderPassSampleBufferAttachmentDescriptorArray")
292            .finish()
293    }
294}