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