| 1 | // Generated from vec_mask.rs.tera template. Edit the template, not the generated file. |
| 2 | |
| 3 | #[cfg(not(target_arch = "spirv"))] |
| 4 | use core::fmt; |
| 5 | use core::ops::*; |
| 6 | |
| 7 | /// A 2-dimensional `bool` vector mask. |
| 8 | #[derive(Clone, Copy, PartialEq, Eq, Hash)] |
| 9 | #[repr(C, align(1))] |
| 10 | pub struct BVec2 { |
| 11 | pub x: bool, |
| 12 | pub y: bool, |
| 13 | } |
| 14 | |
| 15 | const MASK: [u32; 2] = [0, 0xff_ff_ff_ff]; |
| 16 | |
| 17 | impl BVec2 { |
| 18 | /// All false. |
| 19 | pub const FALSE: Self = Self::splat(false); |
| 20 | |
| 21 | /// All true. |
| 22 | pub const TRUE: Self = Self::splat(true); |
| 23 | |
| 24 | /// Creates a new vector mask. |
| 25 | #[inline(always)] |
| 26 | #[must_use] |
| 27 | pub const fn new(x: bool, y: bool) -> Self { |
| 28 | Self { x, y } |
| 29 | } |
| 30 | |
| 31 | /// Creates a vector with all elements set to `v`. |
| 32 | #[inline] |
| 33 | #[must_use] |
| 34 | pub const fn splat(v: bool) -> Self { |
| 35 | Self::new(v, v) |
| 36 | } |
| 37 | |
| 38 | /// Returns a bitmask with the lowest 2 bits set from the elements of `self`. |
| 39 | /// |
| 40 | /// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes |
| 41 | /// into the first lowest bit, element `y` into the second, etc. |
| 42 | #[inline] |
| 43 | #[must_use] |
| 44 | pub fn bitmask(self) -> u32 { |
| 45 | (self.x as u32) | (self.y as u32) << 1 |
| 46 | } |
| 47 | |
| 48 | /// Returns true if any of the elements are true, false otherwise. |
| 49 | #[inline] |
| 50 | #[must_use] |
| 51 | pub fn any(self) -> bool { |
| 52 | self.x || self.y |
| 53 | } |
| 54 | |
| 55 | /// Returns true if all the elements are true, false otherwise. |
| 56 | #[inline] |
| 57 | #[must_use] |
| 58 | pub fn all(self) -> bool { |
| 59 | self.x && self.y |
| 60 | } |
| 61 | |
| 62 | /// Tests the value at `index`. |
| 63 | /// |
| 64 | /// Panics if `index` is greater than 1. |
| 65 | #[inline] |
| 66 | #[must_use] |
| 67 | pub fn test(&self, index: usize) -> bool { |
| 68 | match index { |
| 69 | 0 => self.x, |
| 70 | 1 => self.y, |
| 71 | _ => panic!("index out of bounds"), |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /// Sets the element at `index`. |
| 76 | /// |
| 77 | /// Panics if `index` is greater than 1. |
| 78 | #[inline] |
| 79 | pub fn set(&mut self, index: usize, value: bool) { |
| 80 | match index { |
| 81 | 0 => self.x = value, |
| 82 | 1 => self.y = value, |
| 83 | _ => panic!("index out of bounds"), |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | #[inline] |
| 88 | #[must_use] |
| 89 | fn into_bool_array(self) -> [bool; 2] { |
| 90 | [self.x, self.y] |
| 91 | } |
| 92 | |
| 93 | #[inline] |
| 94 | #[must_use] |
| 95 | fn into_u32_array(self) -> [u32; 2] { |
| 96 | [MASK[self.x as usize], MASK[self.y as usize]] |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | impl Default for BVec2 { |
| 101 | #[inline] |
| 102 | fn default() -> Self { |
| 103 | Self::FALSE |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl BitAnd for BVec2 { |
| 108 | type Output = Self; |
| 109 | #[inline] |
| 110 | fn bitand(self, rhs: Self) -> Self { |
| 111 | Self { |
| 112 | x: self.x & rhs.x, |
| 113 | y: self.y & rhs.y, |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | impl BitAndAssign for BVec2 { |
| 119 | #[inline] |
| 120 | fn bitand_assign(&mut self, rhs: Self) { |
| 121 | *self = self.bitand(rhs); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | impl BitOr for BVec2 { |
| 126 | type Output = Self; |
| 127 | #[inline] |
| 128 | fn bitor(self, rhs: Self) -> Self { |
| 129 | Self { |
| 130 | x: self.x | rhs.x, |
| 131 | y: self.y | rhs.y, |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | impl BitOrAssign for BVec2 { |
| 137 | #[inline] |
| 138 | fn bitor_assign(&mut self, rhs: Self) { |
| 139 | *self = self.bitor(rhs); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | impl BitXor for BVec2 { |
| 144 | type Output = Self; |
| 145 | #[inline] |
| 146 | fn bitxor(self, rhs: Self) -> Self { |
| 147 | Self { |
| 148 | x: self.x ^ rhs.x, |
| 149 | y: self.y ^ rhs.y, |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | impl BitXorAssign for BVec2 { |
| 155 | #[inline] |
| 156 | fn bitxor_assign(&mut self, rhs: Self) { |
| 157 | *self = self.bitxor(rhs); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | impl Not for BVec2 { |
| 162 | type Output = Self; |
| 163 | #[inline] |
| 164 | fn not(self) -> Self { |
| 165 | Self { |
| 166 | x: !self.x, |
| 167 | y: !self.y, |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | #[cfg(not(target_arch = "spirv"))] |
| 173 | impl fmt::Debug for BVec2 { |
| 174 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 175 | let arr = self.into_u32_array(); |
| 176 | write!(f, "{}({:#x}, {:#x})", stringify!(BVec2), arr[0], arr[1]) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | #[cfg(not(target_arch = "spirv"))] |
| 181 | impl fmt::Display for BVec2 { |
| 182 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 183 | let arr = self.into_bool_array(); |
| 184 | write!(f, "[{}, {}]", arr[0], arr[1]) |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | impl From<BVec2> for [bool; 2] { |
| 189 | #[inline] |
| 190 | fn from(mask: BVec2) -> Self { |
| 191 | mask.into_bool_array() |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | impl From<BVec2> for [u32; 2] { |
| 196 | #[inline] |
| 197 | fn from(mask: BVec2) -> Self { |
| 198 | mask.into_u32_array() |
| 199 | } |
| 200 | } |