Skip to main content

mtl_gpu/pass/
stencil_attachment.rs

1//! Stencil attachment descriptor for render passes.
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::Texture;
10use crate::enums::{LoadAction, MultisampleStencilResolveFilter, StoreAction};
11
12/// A stencil attachment descriptor for a render pass.
13///
14/// C++ equivalent: `MTL::RenderPassStencilAttachmentDescriptor`
15#[repr(transparent)]
16pub struct RenderPassStencilAttachmentDescriptor(NonNull<c_void>);
17
18impl RenderPassStencilAttachmentDescriptor {
19    /// Create a RenderPassStencilAttachmentDescriptor from a raw pointer.
20    ///
21    /// # Safety
22    ///
23    /// The pointer must be a valid Metal render pass stencil attachment descriptor object.
24    #[inline]
25    pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
26        NonNull::new(ptr).map(Self)
27    }
28
29    /// Get the raw pointer.
30    #[inline]
31    pub fn as_raw(&self) -> *mut c_void {
32        self.0.as_ptr()
33    }
34
35    /// Get the clear stencil value for this attachment.
36    ///
37    /// C++ equivalent: `uint32_t clearStencil() const`
38    #[inline]
39    pub fn clear_stencil(&self) -> u32 {
40        unsafe { msg_send_0(self.as_ptr(), sel!(clearStencil)) }
41    }
42
43    /// Set the clear stencil value for this attachment.
44    ///
45    /// C++ equivalent: `void setClearStencil(uint32_t)`
46    #[inline]
47    pub fn set_clear_stencil(&self, stencil: u32) {
48        unsafe {
49            msg_send_1::<(), u32>(self.as_ptr(), sel!(setClearStencil:), stencil);
50        }
51    }
52
53    /// Get the stencil resolve filter for this attachment.
54    ///
55    /// C++ equivalent: `MultisampleStencilResolveFilter stencilResolveFilter() const`
56    #[inline]
57    pub fn stencil_resolve_filter(&self) -> MultisampleStencilResolveFilter {
58        unsafe { msg_send_0(self.as_ptr(), sel!(stencilResolveFilter)) }
59    }
60
61    /// Set the stencil resolve filter for this attachment.
62    ///
63    /// C++ equivalent: `void setStencilResolveFilter(MultisampleStencilResolveFilter)`
64    #[inline]
65    pub fn set_stencil_resolve_filter(&self, filter: MultisampleStencilResolveFilter) {
66        unsafe {
67            msg_send_1::<(), MultisampleStencilResolveFilter>(
68                self.as_ptr(),
69                sel!(setStencilResolveFilter:),
70                filter,
71            );
72        }
73    }
74
75    // Base attachment methods
76    /// Get the texture for this attachment.
77    pub fn texture(&self) -> Option<Texture> {
78        unsafe {
79            let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(texture));
80            if ptr.is_null() {
81                return None;
82            }
83            let _: *mut c_void = msg_send_0(ptr, sel!(retain));
84            Texture::from_raw(ptr)
85        }
86    }
87
88    /// Set the texture for this attachment.
89    pub fn set_texture(&self, texture: Option<&Texture>) {
90        unsafe {
91            let ptr = texture.map_or(std::ptr::null(), |t| t.as_ptr());
92            msg_send_1::<(), *const c_void>(self.as_ptr(), sel!(setTexture:), ptr);
93        }
94    }
95
96    /// Get the load action for this attachment.
97    #[inline]
98    pub fn load_action(&self) -> LoadAction {
99        unsafe { msg_send_0(self.as_ptr(), sel!(loadAction)) }
100    }
101
102    /// Set the load action for this attachment.
103    #[inline]
104    pub fn set_load_action(&self, load_action: LoadAction) {
105        unsafe {
106            msg_send_1::<(), LoadAction>(self.as_ptr(), sel!(setLoadAction:), load_action);
107        }
108    }
109
110    /// Get the store action for this attachment.
111    #[inline]
112    pub fn store_action(&self) -> StoreAction {
113        unsafe { msg_send_0(self.as_ptr(), sel!(storeAction)) }
114    }
115
116    /// Set the store action for this attachment.
117    #[inline]
118    pub fn set_store_action(&self, store_action: StoreAction) {
119        unsafe {
120            msg_send_1::<(), StoreAction>(self.as_ptr(), sel!(setStoreAction:), store_action);
121        }
122    }
123}
124
125impl Referencing for RenderPassStencilAttachmentDescriptor {
126    #[inline]
127    fn as_ptr(&self) -> *const c_void {
128        self.0.as_ptr()
129    }
130}