Skip to main content

mtl_gpu/encoder/render_encoder/
state.rs

1//! Render state configuration methods.
2//!
3//! This module contains methods for configuring render state including:
4//! - Rasterizer state (cull mode, winding, fill mode, depth clip)
5//! - Depth/stencil state
6//! - Blend color
7//! - Visibility results
8//! - Store actions
9
10use std::ffi::c_void;
11
12use mtl_foundation::{Referencing, UInteger};
13use mtl_sys::{msg_send_1, sel};
14
15use crate::enums::{
16    CullMode, DepthClipMode, StoreAction, StoreActionOptions, TriangleFillMode,
17    VisibilityResultMode, Winding,
18};
19
20use super::RenderCommandEncoder;
21
22impl RenderCommandEncoder {
23    // =========================================================================
24    // Rasterizer State
25    // =========================================================================
26
27    /// Set the cull mode.
28    ///
29    /// C++ equivalent: `void setCullMode(MTL::CullMode)`
30    #[inline]
31    pub fn set_cull_mode(&self, cull_mode: CullMode) {
32        unsafe {
33            msg_send_1::<(), CullMode>(self.as_ptr(), sel!(setCullMode:), cull_mode);
34        }
35    }
36
37    /// Set the front-facing winding order.
38    ///
39    /// C++ equivalent: `void setFrontFacingWinding(MTL::Winding)`
40    #[inline]
41    pub fn set_front_facing_winding(&self, winding: Winding) {
42        unsafe {
43            msg_send_1::<(), Winding>(self.as_ptr(), sel!(setFrontFacingWinding:), winding);
44        }
45    }
46
47    /// Set the triangle fill mode.
48    ///
49    /// C++ equivalent: `void setTriangleFillMode(MTL::TriangleFillMode)`
50    #[inline]
51    pub fn set_triangle_fill_mode(&self, fill_mode: TriangleFillMode) {
52        unsafe {
53            msg_send_1::<(), TriangleFillMode>(
54                self.as_ptr(),
55                sel!(setTriangleFillMode:),
56                fill_mode,
57            );
58        }
59    }
60
61    /// Set the depth clip mode.
62    ///
63    /// C++ equivalent: `void setDepthClipMode(MTL::DepthClipMode)`
64    #[inline]
65    pub fn set_depth_clip_mode(&self, mode: DepthClipMode) {
66        unsafe {
67            msg_send_1::<(), DepthClipMode>(self.as_ptr(), sel!(setDepthClipMode:), mode);
68        }
69    }
70
71    /// Set the depth bias.
72    ///
73    /// C++ equivalent: `void setDepthBias(float, float, float)`
74    #[inline]
75    pub fn set_depth_bias(&self, depth_bias: f32, slope_scale: f32, clamp: f32) {
76        unsafe {
77            mtl_sys::msg_send_3::<(), f32, f32, f32>(
78                self.as_ptr(),
79                sel!(setDepthBias: slopeScale: clamp:),
80                depth_bias,
81                slope_scale,
82                clamp,
83            );
84        }
85    }
86
87    /// Set the depth test bounds.
88    ///
89    /// C++ equivalent: `void setDepthTestBounds(float, float)`
90    #[inline]
91    pub fn set_depth_test_bounds(&self, min_bound: f32, max_bound: f32) {
92        unsafe {
93            mtl_sys::msg_send_2::<(), f32, f32>(
94                self.as_ptr(),
95                sel!(setDepthTestMinBound: maxBound:),
96                min_bound,
97                max_bound,
98            );
99        }
100    }
101
102    // =========================================================================
103    // Depth Stencil State
104    // =========================================================================
105
106    /// Set the depth stencil state.
107    ///
108    /// C++ equivalent: `void setDepthStencilState(const DepthStencilState*)`
109    #[inline]
110    pub fn set_depth_stencil_state(&self, state: &crate::DepthStencilState) {
111        unsafe {
112            msg_send_1::<(), *const c_void>(
113                self.as_ptr(),
114                sel!(setDepthStencilState:),
115                state.as_ptr(),
116            );
117        }
118    }
119
120    /// Set the stencil reference value (same for front and back).
121    ///
122    /// C++ equivalent: `void setStencilReferenceValue(uint32_t)`
123    #[inline]
124    pub fn set_stencil_reference_value(&self, value: u32) {
125        unsafe {
126            msg_send_1::<(), u32>(self.as_ptr(), sel!(setStencilReferenceValue:), value);
127        }
128    }
129
130    /// Set the stencil reference values (separate front and back).
131    ///
132    /// C++ equivalent: `void setStencilReferenceValues(uint32_t, uint32_t)`
133    #[inline]
134    pub fn set_stencil_reference_values(&self, front: u32, back: u32) {
135        unsafe {
136            mtl_sys::msg_send_2::<(), u32, u32>(
137                self.as_ptr(),
138                sel!(setStencilFrontReferenceValue: backReferenceValue:),
139                front,
140                back,
141            );
142        }
143    }
144
145    // =========================================================================
146    // Blend Color
147    // =========================================================================
148
149    /// Set the blend color.
150    ///
151    /// C++ equivalent: `void setBlendColor(float, float, float, float)`
152    #[inline]
153    pub fn set_blend_color(&self, red: f32, green: f32, blue: f32, alpha: f32) {
154        unsafe {
155            mtl_sys::msg_send_4::<(), f32, f32, f32, f32>(
156                self.as_ptr(),
157                sel!(setBlendColorRed: green: blue: alpha:),
158                red,
159                green,
160                blue,
161                alpha,
162            );
163        }
164    }
165
166    // =========================================================================
167    // Visibility Result
168    // =========================================================================
169
170    /// Set the visibility result mode.
171    ///
172    /// C++ equivalent: `void setVisibilityResultMode(MTL::VisibilityResultMode, NS::UInteger)`
173    #[inline]
174    pub fn set_visibility_result_mode(&self, mode: VisibilityResultMode, offset: UInteger) {
175        unsafe {
176            mtl_sys::msg_send_2::<(), VisibilityResultMode, UInteger>(
177                self.as_ptr(),
178                sel!(setVisibilityResultMode: offset:),
179                mode,
180                offset,
181            );
182        }
183    }
184
185    // =========================================================================
186    // Store Actions
187    // =========================================================================
188
189    /// Set the color store action for an attachment.
190    ///
191    /// C++ equivalent: `void setColorStoreAction(MTL::StoreAction, NS::UInteger)`
192    #[inline]
193    pub fn set_color_store_action(&self, store_action: StoreAction, attachment_index: UInteger) {
194        unsafe {
195            mtl_sys::msg_send_2::<(), StoreAction, UInteger>(
196                self.as_ptr(),
197                sel!(setColorStoreAction: atIndex:),
198                store_action,
199                attachment_index,
200            );
201        }
202    }
203
204    /// Set the color store action options for an attachment.
205    ///
206    /// C++ equivalent: `void setColorStoreActionOptions(MTL::StoreActionOptions, NS::UInteger)`
207    #[inline]
208    pub fn set_color_store_action_options(
209        &self,
210        options: StoreActionOptions,
211        attachment_index: UInteger,
212    ) {
213        unsafe {
214            mtl_sys::msg_send_2::<(), StoreActionOptions, UInteger>(
215                self.as_ptr(),
216                sel!(setColorStoreActionOptions: atIndex:),
217                options,
218                attachment_index,
219            );
220        }
221    }
222
223    /// Set the depth store action.
224    ///
225    /// C++ equivalent: `void setDepthStoreAction(MTL::StoreAction)`
226    #[inline]
227    pub fn set_depth_store_action(&self, store_action: StoreAction) {
228        unsafe {
229            msg_send_1::<(), StoreAction>(self.as_ptr(), sel!(setDepthStoreAction:), store_action);
230        }
231    }
232
233    /// Set the depth store action options.
234    ///
235    /// C++ equivalent: `void setDepthStoreActionOptions(MTL::StoreActionOptions)`
236    #[inline]
237    pub fn set_depth_store_action_options(&self, options: StoreActionOptions) {
238        unsafe {
239            msg_send_1::<(), StoreActionOptions>(
240                self.as_ptr(),
241                sel!(setDepthStoreActionOptions:),
242                options,
243            );
244        }
245    }
246
247    /// Set the stencil store action.
248    ///
249    /// C++ equivalent: `void setStencilStoreAction(MTL::StoreAction)`
250    #[inline]
251    pub fn set_stencil_store_action(&self, store_action: StoreAction) {
252        unsafe {
253            msg_send_1::<(), StoreAction>(
254                self.as_ptr(),
255                sel!(setStencilStoreAction:),
256                store_action,
257            );
258        }
259    }
260
261    /// Set the stencil store action options.
262    ///
263    /// C++ equivalent: `void setStencilStoreActionOptions(MTL::StoreActionOptions)`
264    #[inline]
265    pub fn set_stencil_store_action_options(&self, options: StoreActionOptions) {
266        unsafe {
267            msg_send_1::<(), StoreActionOptions>(
268                self.as_ptr(),
269                sel!(setStencilStoreActionOptions:),
270                options,
271            );
272        }
273    }
274}