Skip to main content

mtl_gpu/enums/
function.rs

1//! Function descriptor enumerations.
2//!
3//! Corresponds to `Metal/MTLFunctionDescriptor.hpp`.
4
5use mtl_foundation::UInteger;
6
7/// Function options (bitflags).
8///
9/// C++ equivalent: `MTL::FunctionOptions`
10#[repr(transparent)]
11#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
12pub struct FunctionOptions(pub UInteger);
13
14impl FunctionOptions {
15    pub const NONE: Self = Self(0);
16    pub const COMPILE_TO_BINARY: Self = Self(1);
17    /// Alias for STORE_FUNCTION_IN_METAL_SCRIPT
18    pub const STORE_FUNCTION_IN_METAL_PIPELINES_SCRIPT: Self = Self(1 << 1);
19    pub const STORE_FUNCTION_IN_METAL_SCRIPT: Self = Self(1 << 1);
20    pub const FAIL_ON_BINARY_ARCHIVE_MISS: Self = Self(1 << 2);
21    pub const PIPELINE_INDEPENDENT: Self = Self(1 << 3);
22
23    /// Returns the raw bits.
24    #[inline]
25    pub const fn bits(&self) -> UInteger {
26        self.0
27    }
28
29    /// Creates from raw bits.
30    #[inline]
31    pub const fn from_bits(bits: UInteger) -> Self {
32        Self(bits)
33    }
34
35    /// Check if empty.
36    #[inline]
37    pub const fn is_empty(&self) -> bool {
38        self.0 == 0
39    }
40
41    /// Check if contains all flags in other.
42    #[inline]
43    pub const fn contains(&self, other: Self) -> bool {
44        (self.0 & other.0) == other.0
45    }
46}
47
48impl std::ops::BitOr for FunctionOptions {
49    type Output = Self;
50    #[inline]
51    fn bitor(self, rhs: Self) -> Self {
52        Self(self.0 | rhs.0)
53    }
54}
55
56impl std::ops::BitAnd for FunctionOptions {
57    type Output = Self;
58    #[inline]
59    fn bitand(self, rhs: Self) -> Self {
60        Self(self.0 & rhs.0)
61    }
62}
63
64impl std::ops::BitOrAssign for FunctionOptions {
65    #[inline]
66    fn bitor_assign(&mut self, rhs: Self) {
67        self.0 |= rhs.0;
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn test_function_options_values() {
77        assert_eq!(FunctionOptions::NONE.0, 0);
78        assert_eq!(FunctionOptions::COMPILE_TO_BINARY.0, 1);
79        assert_eq!(FunctionOptions::STORE_FUNCTION_IN_METAL_SCRIPT.0, 2);
80        assert_eq!(FunctionOptions::FAIL_ON_BINARY_ARCHIVE_MISS.0, 4);
81        assert_eq!(FunctionOptions::PIPELINE_INDEPENDENT.0, 8);
82    }
83
84    #[test]
85    fn test_function_options_bitor() {
86        let opt = FunctionOptions::COMPILE_TO_BINARY | FunctionOptions::FAIL_ON_BINARY_ARCHIVE_MISS;
87        assert!(opt.contains(FunctionOptions::COMPILE_TO_BINARY));
88        assert!(opt.contains(FunctionOptions::FAIL_ON_BINARY_ARCHIVE_MISS));
89    }
90}