Skip to main content

mtl_gpu/enums/
pipeline.rs

1//! Pipeline enumerations.
2//!
3//! Corresponds to `Metal/MTLRenderPipeline.hpp` and `Metal/MTLPipeline.hpp`.
4
5use mtl_foundation::{Integer, UInteger};
6
7/// Blend factor for color blending.
8///
9/// C++ equivalent: `MTL::BlendFactor`
10#[repr(transparent)]
11#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
12pub struct BlendFactor(pub UInteger);
13
14impl BlendFactor {
15    pub const ZERO: Self = Self(0);
16    pub const ONE: Self = Self(1);
17    pub const SOURCE_COLOR: Self = Self(2);
18    pub const ONE_MINUS_SOURCE_COLOR: Self = Self(3);
19    pub const SOURCE_ALPHA: Self = Self(4);
20    pub const ONE_MINUS_SOURCE_ALPHA: Self = Self(5);
21    pub const DESTINATION_COLOR: Self = Self(6);
22    pub const ONE_MINUS_DESTINATION_COLOR: Self = Self(7);
23    pub const DESTINATION_ALPHA: Self = Self(8);
24    pub const ONE_MINUS_DESTINATION_ALPHA: Self = Self(9);
25    pub const SOURCE_ALPHA_SATURATED: Self = Self(10);
26    pub const BLEND_COLOR: Self = Self(11);
27    pub const ONE_MINUS_BLEND_COLOR: Self = Self(12);
28    pub const BLEND_ALPHA: Self = Self(13);
29    pub const ONE_MINUS_BLEND_ALPHA: Self = Self(14);
30    pub const SOURCE1_COLOR: Self = Self(15);
31    pub const ONE_MINUS_SOURCE1_COLOR: Self = Self(16);
32    pub const SOURCE1_ALPHA: Self = Self(17);
33    pub const ONE_MINUS_SOURCE1_ALPHA: Self = Self(18);
34    pub const UNSPECIALIZED: Self = Self(19);
35}
36
37/// Blend operation.
38///
39/// C++ equivalent: `MTL::BlendOperation`
40#[repr(transparent)]
41#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
42pub struct BlendOperation(pub UInteger);
43
44impl BlendOperation {
45    pub const ADD: Self = Self(0);
46    pub const SUBTRACT: Self = Self(1);
47    pub const REVERSE_SUBTRACT: Self = Self(2);
48    pub const MIN: Self = Self(3);
49    pub const MAX: Self = Self(4);
50    pub const UNSPECIALIZED: Self = Self(5);
51}
52
53/// Primitive topology class.
54///
55/// C++ equivalent: `MTL::PrimitiveTopologyClass`
56#[repr(transparent)]
57#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
58pub struct PrimitiveTopologyClass(pub UInteger);
59
60impl PrimitiveTopologyClass {
61    pub const UNSPECIFIED: Self = Self(0);
62    pub const POINT: Self = Self(1);
63    pub const LINE: Self = Self(2);
64    pub const TRIANGLE: Self = Self(3);
65}
66
67/// Tessellation partition mode.
68///
69/// C++ equivalent: `MTL::TessellationPartitionMode`
70#[repr(transparent)]
71#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
72pub struct TessellationPartitionMode(pub UInteger);
73
74impl TessellationPartitionMode {
75    pub const POW2: Self = Self(0);
76    pub const INTEGER: Self = Self(1);
77    pub const FRACTIONAL_ODD: Self = Self(2);
78    pub const FRACTIONAL_EVEN: Self = Self(3);
79}
80
81/// Tessellation factor step function.
82///
83/// C++ equivalent: `MTL::TessellationFactorStepFunction`
84#[repr(transparent)]
85#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
86pub struct TessellationFactorStepFunction(pub UInteger);
87
88impl TessellationFactorStepFunction {
89    pub const CONSTANT: Self = Self(0);
90    pub const PER_PATCH: Self = Self(1);
91    pub const PER_INSTANCE: Self = Self(2);
92    pub const PER_PATCH_AND_PER_INSTANCE: Self = Self(3);
93}
94
95/// Tessellation factor format.
96///
97/// C++ equivalent: `MTL::TessellationFactorFormat`
98#[repr(transparent)]
99#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
100pub struct TessellationFactorFormat(pub UInteger);
101
102impl TessellationFactorFormat {
103    pub const HALF: Self = Self(0);
104}
105
106/// Tessellation control point index type.
107///
108/// C++ equivalent: `MTL::TessellationControlPointIndexType`
109#[repr(transparent)]
110#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
111pub struct TessellationControlPointIndexType(pub UInteger);
112
113impl TessellationControlPointIndexType {
114    pub const NONE: Self = Self(0);
115    pub const UINT16: Self = Self(1);
116    pub const UINT32: Self = Self(2);
117}
118
119/// Color write mask (bitflags).
120///
121/// C++ equivalent: `MTL::ColorWriteMask`
122#[repr(transparent)]
123#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
124pub struct ColorWriteMask(pub UInteger);
125
126impl ColorWriteMask {
127    pub const NONE: Self = Self(0);
128    pub const RED: Self = Self(1 << 3);
129    pub const GREEN: Self = Self(1 << 2);
130    pub const BLUE: Self = Self(1 << 1);
131    pub const ALPHA: Self = Self(1);
132    pub const ALL: Self = Self(15);
133    pub const UNSPECIALIZED: Self = Self(1 << 4);
134
135    /// Returns the raw bits.
136    #[inline]
137    pub const fn bits(&self) -> UInteger {
138        self.0
139    }
140
141    /// Creates from raw bits.
142    #[inline]
143    pub const fn from_bits(bits: UInteger) -> Self {
144        Self(bits)
145    }
146
147    /// Check if empty.
148    #[inline]
149    pub const fn is_empty(&self) -> bool {
150        self.0 == 0
151    }
152
153    /// Check if contains all flags in other.
154    #[inline]
155    pub const fn contains(&self, other: Self) -> bool {
156        (self.0 & other.0) == other.0
157    }
158}
159
160impl std::ops::BitOr for ColorWriteMask {
161    type Output = Self;
162    #[inline]
163    fn bitor(self, rhs: Self) -> Self {
164        Self(self.0 | rhs.0)
165    }
166}
167
168impl std::ops::BitAnd for ColorWriteMask {
169    type Output = Self;
170    #[inline]
171    fn bitand(self, rhs: Self) -> Self {
172        Self(self.0 & rhs.0)
173    }
174}
175
176impl std::ops::BitOrAssign for ColorWriteMask {
177    #[inline]
178    fn bitor_assign(&mut self, rhs: Self) {
179        self.0 |= rhs.0;
180    }
181}
182
183/// Buffer mutability for pipeline state.
184///
185/// C++ equivalent: `MTL::Mutability`
186#[repr(transparent)]
187#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
188pub struct Mutability(pub UInteger);
189
190impl Mutability {
191    pub const DEFAULT: Self = Self(0);
192    pub const MUTABLE: Self = Self(1);
193    pub const IMMUTABLE: Self = Self(2);
194}
195
196/// Shader validation mode.
197///
198/// C++ equivalent: `MTL::ShaderValidation`
199#[repr(transparent)]
200#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
201pub struct ShaderValidation(pub Integer);
202
203impl ShaderValidation {
204    pub const DEFAULT: Self = Self(0);
205    pub const ENABLED: Self = Self(1);
206    pub const DISABLED: Self = Self(2);
207}
208
209#[cfg(test)]
210mod tests {
211    use super::*;
212
213    #[test]
214    fn test_blend_factor_values() {
215        assert_eq!(BlendFactor::ZERO.0, 0);
216        assert_eq!(BlendFactor::ONE.0, 1);
217        assert_eq!(BlendFactor::SOURCE_ALPHA.0, 4);
218        assert_eq!(BlendFactor::UNSPECIALIZED.0, 19);
219    }
220
221    #[test]
222    fn test_blend_operation_values() {
223        assert_eq!(BlendOperation::ADD.0, 0);
224        assert_eq!(BlendOperation::MAX.0, 4);
225    }
226
227    #[test]
228    fn test_color_write_mask_values() {
229        assert_eq!(ColorWriteMask::NONE.0, 0);
230        assert_eq!(ColorWriteMask::ALL.0, 15);
231        assert_eq!(ColorWriteMask::RED.0, 8);
232        assert_eq!(ColorWriteMask::GREEN.0, 4);
233        assert_eq!(ColorWriteMask::BLUE.0, 2);
234        assert_eq!(ColorWriteMask::ALPHA.0, 1);
235    }
236
237    #[test]
238    fn test_color_write_mask_bitor() {
239        let mask = ColorWriteMask::RED | ColorWriteMask::GREEN;
240        assert!(mask.contains(ColorWriteMask::RED));
241        assert!(mask.contains(ColorWriteMask::GREEN));
242    }
243}