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