Skip to main content

mtl_gpu/pass/
color_attachment.rs

1//! Color attachment descriptors for render passes.
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::Texture;
10use crate::enums::{LoadAction, StoreAction};
11use crate::types::ClearColor;
12
13/// A color attachment descriptor for a render pass.
14///
15/// C++ equivalent: `MTL::RenderPassColorAttachmentDescriptor`
16#[repr(transparent)]
17pub struct RenderPassColorAttachmentDescriptor(NonNull<c_void>);
18
19impl RenderPassColorAttachmentDescriptor {
20    /// Create a RenderPassColorAttachmentDescriptor from a raw pointer.
21    ///
22    /// # Safety
23    ///
24    /// The pointer must be a valid Metal render pass color attachment 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    /// Get the clear color for this attachment.
37    ///
38    /// C++ equivalent: `ClearColor clearColor() const`
39    #[inline]
40    pub fn clear_color(&self) -> ClearColor {
41        unsafe { msg_send_0(self.as_ptr(), sel!(clearColor)) }
42    }
43
44    /// Set the clear color for this attachment.
45    ///
46    /// C++ equivalent: `void setClearColor(ClearColor)`
47    #[inline]
48    pub fn set_clear_color(&self, color: ClearColor) {
49        unsafe {
50            msg_send_1::<(), ClearColor>(self.as_ptr(), sel!(setClearColor:), color);
51        }
52    }
53
54    // Inherit all base attachment methods
55    /// Get the texture for this attachment.
56    pub fn texture(&self) -> Option<Texture> {
57        unsafe {
58            let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(texture));
59            if ptr.is_null() {
60                return None;
61            }
62            let _: *mut c_void = msg_send_0(ptr, sel!(retain));
63            Texture::from_raw(ptr)
64        }
65    }
66
67    /// Set the texture for this attachment.
68    pub fn set_texture(&self, texture: Option<&Texture>) {
69        unsafe {
70            let ptr = texture.map_or(std::ptr::null(), |t| t.as_ptr());
71            msg_send_1::<(), *const c_void>(self.as_ptr(), sel!(setTexture:), ptr);
72        }
73    }
74
75    /// Get the load action for this attachment.
76    #[inline]
77    pub fn load_action(&self) -> LoadAction {
78        unsafe { msg_send_0(self.as_ptr(), sel!(loadAction)) }
79    }
80
81    /// Set the load action for this attachment.
82    #[inline]
83    pub fn set_load_action(&self, load_action: LoadAction) {
84        unsafe {
85            msg_send_1::<(), LoadAction>(self.as_ptr(), sel!(setLoadAction:), load_action);
86        }
87    }
88
89    /// Get the store action for this attachment.
90    #[inline]
91    pub fn store_action(&self) -> StoreAction {
92        unsafe { msg_send_0(self.as_ptr(), sel!(storeAction)) }
93    }
94
95    /// Set the store action for this attachment.
96    #[inline]
97    pub fn set_store_action(&self, store_action: StoreAction) {
98        unsafe {
99            msg_send_1::<(), StoreAction>(self.as_ptr(), sel!(setStoreAction:), store_action);
100        }
101    }
102
103    /// Get the mipmap level for this attachment.
104    #[inline]
105    pub fn level(&self) -> UInteger {
106        unsafe { msg_send_0(self.as_ptr(), sel!(level)) }
107    }
108
109    /// Set the mipmap level for this attachment.
110    #[inline]
111    pub fn set_level(&self, level: UInteger) {
112        unsafe {
113            msg_send_1::<(), UInteger>(self.as_ptr(), sel!(setLevel:), level);
114        }
115    }
116
117    /// Get the slice for this attachment.
118    #[inline]
119    pub fn slice(&self) -> UInteger {
120        unsafe { msg_send_0(self.as_ptr(), sel!(slice)) }
121    }
122
123    /// Set the slice for this attachment.
124    #[inline]
125    pub fn set_slice(&self, slice: UInteger) {
126        unsafe {
127            msg_send_1::<(), UInteger>(self.as_ptr(), sel!(setSlice:), slice);
128        }
129    }
130}
131
132impl Referencing for RenderPassColorAttachmentDescriptor {
133    #[inline]
134    fn as_ptr(&self) -> *const c_void {
135        self.0.as_ptr()
136    }
137}
138
139/// An array of color attachment descriptors.
140///
141/// C++ equivalent: `MTL::RenderPassColorAttachmentDescriptorArray`
142#[repr(transparent)]
143pub struct RenderPassColorAttachmentDescriptorArray(NonNull<c_void>);
144
145impl RenderPassColorAttachmentDescriptorArray {
146    /// Create a RenderPassColorAttachmentDescriptorArray from a raw pointer.
147    ///
148    /// # Safety
149    ///
150    /// The pointer must be a valid Metal render pass color attachment descriptor array object.
151    #[inline]
152    pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
153        NonNull::new(ptr).map(Self)
154    }
155
156    /// Get the raw pointer.
157    #[inline]
158    pub fn as_raw(&self) -> *mut c_void {
159        self.0.as_ptr()
160    }
161
162    /// Get the color attachment descriptor at the specified index.
163    ///
164    /// C++ equivalent: `RenderPassColorAttachmentDescriptor* object(NS::UInteger)`
165    pub fn object_at(&self, index: UInteger) -> Option<RenderPassColorAttachmentDescriptor> {
166        unsafe {
167            let ptr: *mut c_void =
168                msg_send_1(self.as_ptr(), sel!(objectAtIndexedSubscript:), index);
169            RenderPassColorAttachmentDescriptor::from_raw(ptr)
170        }
171    }
172
173    /// Set the color attachment descriptor at the specified index.
174    ///
175    /// C++ equivalent: `void setObject(const RenderPassColorAttachmentDescriptor*, NS::UInteger)`
176    pub fn set_object_at(
177        &self,
178        attachment: Option<&RenderPassColorAttachmentDescriptor>,
179        index: UInteger,
180    ) {
181        unsafe {
182            let ptr = attachment.map_or(std::ptr::null(), |a| a.as_ptr());
183            mtl_sys::msg_send_2::<(), *const c_void, UInteger>(
184                self.as_ptr(),
185                sel!(setObject: atIndexedSubscript:),
186                ptr,
187                index,
188            );
189        }
190    }
191}
192
193impl Referencing for RenderPassColorAttachmentDescriptorArray {
194    #[inline]
195    fn as_ptr(&self) -> *const c_void {
196        self.0.as_ptr()
197    }
198}