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