Skip to main content

mtl_gpu/enums/
device.rs

1//! Device enumerations.
2//!
3//! Corresponds to `Metal/MTLDevice.hpp`.
4
5use mtl_foundation::{Integer, UInteger};
6
7/// IO compression method.
8///
9/// C++ equivalent: `MTL::IOCompressionMethod`
10#[repr(transparent)]
11#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
12pub struct IOCompressionMethod(pub Integer);
13
14impl IOCompressionMethod {
15    pub const ZLIB: Self = Self(0);
16    pub const LZFSE: Self = Self(1);
17    pub const LZ4: Self = Self(2);
18    pub const LZMA: Self = Self(3);
19    pub const LZ_BITMAP: Self = Self(4);
20}
21
22/// Feature set (legacy, deprecated).
23///
24/// C++ equivalent: `MTL::FeatureSet`
25#[repr(transparent)]
26#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
27pub struct FeatureSet(pub UInteger);
28
29impl FeatureSet {
30    // iOS GPU Families
31    pub const IOS_GPU_FAMILY1_V1: Self = Self(0);
32    pub const IOS_GPU_FAMILY2_V1: Self = Self(1);
33    pub const IOS_GPU_FAMILY1_V2: Self = Self(2);
34    pub const IOS_GPU_FAMILY2_V2: Self = Self(3);
35    pub const IOS_GPU_FAMILY3_V1: Self = Self(4);
36    pub const IOS_GPU_FAMILY1_V3: Self = Self(5);
37    pub const IOS_GPU_FAMILY2_V3: Self = Self(6);
38    pub const IOS_GPU_FAMILY3_V2: Self = Self(7);
39    pub const IOS_GPU_FAMILY1_V4: Self = Self(8);
40    pub const IOS_GPU_FAMILY2_V4: Self = Self(9);
41    pub const IOS_GPU_FAMILY3_V3: Self = Self(10);
42    pub const IOS_GPU_FAMILY4_V1: Self = Self(11);
43    pub const IOS_GPU_FAMILY1_V5: Self = Self(12);
44    pub const IOS_GPU_FAMILY2_V5: Self = Self(13);
45    pub const IOS_GPU_FAMILY3_V4: Self = Self(14);
46    pub const IOS_GPU_FAMILY4_V2: Self = Self(15);
47    pub const IOS_GPU_FAMILY5_V1: Self = Self(16);
48
49    // macOS GPU Families
50    pub const MACOS_GPU_FAMILY1_V1: Self = Self(10000);
51    pub const OSX_GPU_FAMILY1_V1: Self = Self(10000);
52    pub const MACOS_GPU_FAMILY1_V2: Self = Self(10001);
53    pub const OSX_GPU_FAMILY1_V2: Self = Self(10001);
54    pub const MACOS_READ_WRITE_TEXTURE_TIER2: Self = Self(10002);
55    pub const OSX_READ_WRITE_TEXTURE_TIER2: Self = Self(10002);
56    pub const MACOS_GPU_FAMILY1_V3: Self = Self(10003);
57    pub const MACOS_GPU_FAMILY1_V4: Self = Self(10004);
58    pub const MACOS_GPU_FAMILY2_V1: Self = Self(10005);
59
60    // watchOS GPU Families
61    pub const WATCHOS_GPU_FAMILY1_V1: Self = Self(20000);
62    pub const WATCHOS_GPU_FAMILY2_V1: Self = Self(20001);
63
64    // tvOS GPU Families
65    pub const TVOS_GPU_FAMILY1_V1: Self = Self(30000);
66    pub const TVOS_GPU_FAMILY1_V2: Self = Self(30001);
67    pub const TVOS_GPU_FAMILY1_V3: Self = Self(30002);
68    pub const TVOS_GPU_FAMILY2_V1: Self = Self(30003);
69    pub const TVOS_GPU_FAMILY1_V4: Self = Self(30004);
70    pub const TVOS_GPU_FAMILY2_V2: Self = Self(30005);
71}
72
73/// GPU family.
74///
75/// C++ equivalent: `MTL::GPUFamily`
76#[repr(transparent)]
77#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
78pub struct GPUFamily(pub Integer);
79
80impl GPUFamily {
81    pub const APPLE1: Self = Self(1001);
82    pub const APPLE2: Self = Self(1002);
83    pub const APPLE3: Self = Self(1003);
84    pub const APPLE4: Self = Self(1004);
85    pub const APPLE5: Self = Self(1005);
86    pub const APPLE6: Self = Self(1006);
87    pub const APPLE7: Self = Self(1007);
88    pub const APPLE8: Self = Self(1008);
89    pub const APPLE9: Self = Self(1009);
90    pub const APPLE10: Self = Self(1010);
91    pub const MAC1: Self = Self(2001);
92    pub const MAC2: Self = Self(2002);
93    pub const COMMON1: Self = Self(3001);
94    pub const COMMON2: Self = Self(3002);
95    pub const COMMON3: Self = Self(3003);
96    pub const MAC_CATALYST1: Self = Self(4001);
97    pub const MAC_CATALYST2: Self = Self(4002);
98    pub const METAL3: Self = Self(5001);
99    pub const METAL4: Self = Self(5002);
100}
101
102/// Device location.
103///
104/// C++ equivalent: `MTL::DeviceLocation`
105#[repr(transparent)]
106#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
107pub struct DeviceLocation(pub UInteger);
108
109impl DeviceLocation {
110    pub const BUILT_IN: Self = Self(0);
111    pub const SLOT: Self = Self(1);
112    pub const EXTERNAL: Self = Self(2);
113    pub const UNSPECIFIED: Self = Self(UInteger::MAX);
114}
115
116/// Read-write texture tier.
117///
118/// C++ equivalent: `MTL::ReadWriteTextureTier`
119#[repr(transparent)]
120#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
121pub struct ReadWriteTextureTier(pub UInteger);
122
123impl ReadWriteTextureTier {
124    pub const NONE: Self = Self(0);
125    pub const TIER1: Self = Self(1);
126    pub const TIER2: Self = Self(2);
127}
128
129/// Argument buffers tier.
130///
131/// C++ equivalent: `MTL::ArgumentBuffersTier`
132#[repr(transparent)]
133#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
134pub struct ArgumentBuffersTier(pub UInteger);
135
136impl ArgumentBuffersTier {
137    pub const TIER1: Self = Self(0);
138    pub const TIER2: Self = Self(1);
139}
140
141/// Sparse texture region alignment mode.
142///
143/// C++ equivalent: `MTL::SparseTextureRegionAlignmentMode`
144#[repr(transparent)]
145#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
146pub struct SparseTextureRegionAlignmentMode(pub UInteger);
147
148impl SparseTextureRegionAlignmentMode {
149    pub const OUTWARD: Self = Self(0);
150    pub const INWARD: Self = Self(1);
151}
152
153/// Counter sampling point.
154///
155/// C++ equivalent: `MTL::CounterSamplingPoint`
156#[repr(transparent)]
157#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
158pub struct CounterSamplingPoint(pub UInteger);
159
160impl CounterSamplingPoint {
161    pub const AT_STAGE_BOUNDARY: Self = Self(0);
162    pub const AT_DRAW_BOUNDARY: Self = Self(1);
163    pub const AT_DISPATCH_BOUNDARY: Self = Self(2);
164    pub const AT_TILE_DISPATCH_BOUNDARY: Self = Self(3);
165    pub const AT_BLIT_BOUNDARY: Self = Self(4);
166}
167
168/// Pipeline options (bitflags).
169///
170/// C++ equivalent: `MTL::PipelineOption`
171#[repr(transparent)]
172#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
173pub struct PipelineOption(pub UInteger);
174
175impl PipelineOption {
176    pub const NONE: Self = Self(0);
177    pub const ARGUMENT_INFO: Self = Self(1);
178    pub const BINDING_INFO: Self = Self(1);
179    pub const BUFFER_TYPE_INFO: Self = Self(1 << 1);
180    pub const FAIL_ON_BINARY_ARCHIVE_MISS: Self = Self(1 << 2);
181
182    /// Returns the raw bits.
183    #[inline]
184    pub const fn bits(&self) -> UInteger {
185        self.0
186    }
187
188    /// Creates from raw bits.
189    #[inline]
190    pub const fn from_bits(bits: UInteger) -> Self {
191        Self(bits)
192    }
193
194    /// Check if empty.
195    #[inline]
196    pub const fn is_empty(&self) -> bool {
197        self.0 == 0
198    }
199
200    /// Check if contains all flags in other.
201    #[inline]
202    pub const fn contains(&self, other: Self) -> bool {
203        (self.0 & other.0) == other.0
204    }
205}
206
207impl std::ops::BitOr for PipelineOption {
208    type Output = Self;
209    #[inline]
210    fn bitor(self, rhs: Self) -> Self {
211        Self(self.0 | rhs.0)
212    }
213}
214
215impl std::ops::BitAnd for PipelineOption {
216    type Output = Self;
217    #[inline]
218    fn bitand(self, rhs: Self) -> Self {
219        Self(self.0 & rhs.0)
220    }
221}
222
223impl std::ops::BitOrAssign for PipelineOption {
224    #[inline]
225    fn bitor_assign(&mut self, rhs: Self) {
226        self.0 |= rhs.0;
227    }
228}
229
230#[cfg(test)]
231mod tests {
232    use super::*;
233
234    #[test]
235    fn test_gpu_family_values() {
236        assert_eq!(GPUFamily::APPLE1.0, 1001);
237        assert_eq!(GPUFamily::MAC1.0, 2001);
238        assert_eq!(GPUFamily::METAL3.0, 5001);
239        assert_eq!(GPUFamily::METAL4.0, 5002);
240    }
241
242    #[test]
243    fn test_feature_set_values() {
244        assert_eq!(FeatureSet::IOS_GPU_FAMILY1_V1.0, 0);
245        assert_eq!(FeatureSet::MACOS_GPU_FAMILY1_V1.0, 10000);
246        assert_eq!(FeatureSet::TVOS_GPU_FAMILY1_V1.0, 30000);
247    }
248
249    #[test]
250    fn test_device_location_values() {
251        assert_eq!(DeviceLocation::BUILT_IN.0, 0);
252        assert_eq!(DeviceLocation::UNSPECIFIED.0, UInteger::MAX);
253    }
254
255    #[test]
256    fn test_pipeline_option_bitor() {
257        let opt = PipelineOption::ARGUMENT_INFO | PipelineOption::BUFFER_TYPE_INFO;
258        assert!(opt.contains(PipelineOption::ARGUMENT_INFO));
259        assert!(opt.contains(PipelineOption::BUFFER_TYPE_INFO));
260    }
261}