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