Skip to main content

mtl_gpu/enums/
resource.rs

1//! Resource enumerations.
2//!
3//! Corresponds to `Metal/MTLResource.hpp`.
4
5use mtl_foundation::{Integer, UInteger};
6
7/// Purgeable state of a resource.
8///
9/// C++ equivalent: `MTL::PurgeableState`
10#[repr(transparent)]
11#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
12pub struct PurgeableState(pub UInteger);
13
14impl PurgeableState {
15    pub const KEEP_CURRENT: Self = Self(1);
16    pub const NON_VOLATILE: Self = Self(2);
17    pub const VOLATILE: Self = Self(3);
18    pub const EMPTY: Self = Self(4);
19}
20
21/// CPU cache mode for resource memory.
22///
23/// C++ equivalent: `MTL::CPUCacheMode`
24#[repr(transparent)]
25#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
26pub struct CPUCacheMode(pub UInteger);
27
28impl CPUCacheMode {
29    pub const DEFAULT_CACHE: Self = Self(0);
30    pub const WRITE_COMBINED: Self = Self(1);
31}
32
33/// Storage mode for resource memory.
34///
35/// C++ equivalent: `MTL::StorageMode`
36#[repr(transparent)]
37#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
38pub struct StorageMode(pub UInteger);
39
40impl StorageMode {
41    pub const SHARED: Self = Self(0);
42    pub const MANAGED: Self = Self(1);
43    pub const PRIVATE: Self = Self(2);
44    pub const MEMORYLESS: Self = Self(3);
45}
46
47/// Hazard tracking mode for resources.
48///
49/// C++ equivalent: `MTL::HazardTrackingMode`
50#[repr(transparent)]
51#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
52pub struct HazardTrackingMode(pub UInteger);
53
54impl HazardTrackingMode {
55    pub const DEFAULT: Self = Self(0);
56    pub const UNTRACKED: Self = Self(1);
57    pub const TRACKED: Self = Self(2);
58}
59
60/// Sparse page size for sparse resources.
61///
62/// C++ equivalent: `MTL::SparsePageSize`
63#[repr(transparent)]
64#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
65pub struct SparsePageSize(pub Integer);
66
67impl SparsePageSize {
68    pub const SIZE_16: Self = Self(101);
69    pub const SIZE_64: Self = Self(102);
70    pub const SIZE_256: Self = Self(103);
71}
72
73/// Buffer sparse tier.
74///
75/// C++ equivalent: `MTL::BufferSparseTier`
76#[repr(transparent)]
77#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
78pub struct BufferSparseTier(pub Integer);
79
80impl BufferSparseTier {
81    pub const NONE: Self = Self(0);
82    pub const TIER1: Self = Self(1);
83}
84
85/// Texture sparse tier.
86///
87/// C++ equivalent: `MTL::TextureSparseTier`
88#[repr(transparent)]
89#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
90pub struct TextureSparseTier(pub Integer);
91
92impl TextureSparseTier {
93    pub const NONE: Self = Self(0);
94    pub const TIER1: Self = Self(1);
95    pub const TIER2: Self = Self(2);
96}
97
98/// Resource creation options (bitflags).
99///
100/// C++ equivalent: `MTL::ResourceOptions`
101#[repr(transparent)]
102#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
103pub struct ResourceOptions(pub UInteger);
104
105impl ResourceOptions {
106    pub const CPU_CACHE_MODE_DEFAULT_CACHE: Self = Self(0);
107    pub const CPU_CACHE_MODE_WRITE_COMBINED: Self = Self(1);
108    pub const STORAGE_MODE_SHARED: Self = Self(0);
109    pub const STORAGE_MODE_MANAGED: Self = Self(1 << 4);
110    pub const STORAGE_MODE_PRIVATE: Self = Self(1 << 5);
111    pub const STORAGE_MODE_MEMORYLESS: Self = Self(1 << 5);
112    pub const HAZARD_TRACKING_MODE_DEFAULT: Self = Self(0);
113    pub const HAZARD_TRACKING_MODE_UNTRACKED: Self = Self(1 << 8);
114    pub const HAZARD_TRACKING_MODE_TRACKED: Self = Self(1 << 9);
115    pub const OPTION_CPU_CACHE_MODE_DEFAULT: Self = Self(0);
116    pub const OPTION_CPU_CACHE_MODE_WRITE_COMBINED: Self = Self(1);
117
118    /// Returns the raw bits.
119    #[inline]
120    pub const fn bits(&self) -> UInteger {
121        self.0
122    }
123
124    /// Creates from raw bits.
125    #[inline]
126    pub const fn from_bits(bits: UInteger) -> Self {
127        Self(bits)
128    }
129
130    /// Check if empty.
131    #[inline]
132    pub const fn is_empty(&self) -> bool {
133        self.0 == 0
134    }
135
136    /// Check if contains all flags in other.
137    #[inline]
138    pub const fn contains(&self, other: Self) -> bool {
139        (self.0 & other.0) == other.0
140    }
141}
142
143impl std::ops::BitOr for ResourceOptions {
144    type Output = Self;
145    #[inline]
146    fn bitor(self, rhs: Self) -> Self {
147        Self(self.0 | rhs.0)
148    }
149}
150
151impl std::ops::BitAnd for ResourceOptions {
152    type Output = Self;
153    #[inline]
154    fn bitand(self, rhs: Self) -> Self {
155        Self(self.0 & rhs.0)
156    }
157}
158
159impl std::ops::BitOrAssign for ResourceOptions {
160    #[inline]
161    fn bitor_assign(&mut self, rhs: Self) {
162        self.0 |= rhs.0;
163    }
164}
165
166impl std::ops::BitAndAssign for ResourceOptions {
167    #[inline]
168    fn bitand_assign(&mut self, rhs: Self) {
169        self.0 &= rhs.0;
170    }
171}
172
173/// Sparse texture mapping mode.
174///
175/// C++ equivalent: `MTL::SparseTextureMappingMode`
176#[repr(transparent)]
177#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
178pub struct SparseTextureMappingMode(pub UInteger);
179
180impl SparseTextureMappingMode {
181    /// Map the sparse texture region.
182    pub const MAP: Self = Self(0);
183    /// Unmap the sparse texture region.
184    pub const UNMAP: Self = Self(1);
185}
186
187#[cfg(test)]
188mod tests {
189    use super::*;
190
191    #[test]
192    fn test_purgeable_state_values() {
193        assert_eq!(PurgeableState::KEEP_CURRENT.0, 1);
194        assert_eq!(PurgeableState::NON_VOLATILE.0, 2);
195        assert_eq!(PurgeableState::VOLATILE.0, 3);
196        assert_eq!(PurgeableState::EMPTY.0, 4);
197    }
198
199    #[test]
200    fn test_storage_mode_values() {
201        assert_eq!(StorageMode::SHARED.0, 0);
202        assert_eq!(StorageMode::MANAGED.0, 1);
203        assert_eq!(StorageMode::PRIVATE.0, 2);
204        assert_eq!(StorageMode::MEMORYLESS.0, 3);
205    }
206
207    #[test]
208    fn test_resource_options_bitor() {
209        let opts =
210            ResourceOptions::CPU_CACHE_MODE_WRITE_COMBINED | ResourceOptions::STORAGE_MODE_PRIVATE;
211        assert!(opts.contains(ResourceOptions::CPU_CACHE_MODE_WRITE_COMBINED));
212    }
213}