#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::ops::*;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[repr(C, align(1))]
pub struct BVec3 {
pub x: bool,
pub y: bool,
pub z: bool,
}
const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
impl BVec3 {
pub const FALSE: Self = Self::splat(false);
pub const TRUE: Self = Self::splat(true);
#[inline(always)]
pub const fn new(x: bool, y: bool, z: bool) -> Self {
Self { x, y, z }
}
#[inline]
pub const fn splat(v: bool) -> Self {
Self::new(v, v, v)
}
#[inline]
pub fn bitmask(self) -> u32 {
(self.x as u32) | (self.y as u32) << 1 | (self.z as u32) << 2
}
#[inline]
pub fn any(self) -> bool {
self.x || self.y || self.z
}
#[inline]
pub fn all(self) -> bool {
self.x && self.y && self.z
}
#[inline]
pub fn test(&self, index: usize) -> bool {
match index {
0 => self.x,
1 => self.y,
2 => self.z,
_ => panic!("index out of bounds"),
}
}
#[inline]
pub fn set(&mut self, index: usize, value: bool) {
match index {
0 => self.x = value,
1 => self.y = value,
2 => self.z = value,
_ => panic!("index out of bounds"),
}
}
#[inline]
fn into_bool_array(self) -> [bool; 3] {
[self.x, self.y, self.z]
}
#[inline]
fn into_u32_array(self) -> [u32; 3] {
[
MASK[self.x as usize],
MASK[self.y as usize],
MASK[self.z as usize],
]
}
}
impl Default for BVec3 {
#[inline]
fn default() -> Self {
Self::FALSE
}
}
impl BitAnd for BVec3 {
type Output = Self;
#[inline]
fn bitand(self, rhs: Self) -> Self {
Self {
x: self.x & rhs.x,
y: self.y & rhs.y,
z: self.z & rhs.z,
}
}
}
impl BitAndAssign for BVec3 {
#[inline]
fn bitand_assign(&mut self, rhs: Self) {
*self = self.bitand(rhs);
}
}
impl BitOr for BVec3 {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self {
Self {
x: self.x | rhs.x,
y: self.y | rhs.y,
z: self.z | rhs.z,
}
}
}
impl BitOrAssign for BVec3 {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
*self = self.bitor(rhs);
}
}
impl BitXor for BVec3 {
type Output = Self;
#[inline]
fn bitxor(self, rhs: Self) -> Self {
Self {
x: self.x ^ rhs.x,
y: self.y ^ rhs.y,
z: self.z ^ rhs.z,
}
}
}
impl BitXorAssign for BVec3 {
#[inline]
fn bitxor_assign(&mut self, rhs: Self) {
*self = self.bitxor(rhs);
}
}
impl Not for BVec3 {
type Output = Self;
#[inline]
fn not(self) -> Self {
Self {
x: !self.x,
y: !self.y,
z: !self.z,
}
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for BVec3 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let arr = self.into_u32_array();
write!(
f,
"{}({:#x}, {:#x}, {:#x})",
stringify!(BVec3),
arr[0],
arr[1],
arr[2]
)
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for BVec3 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let arr = self.into_bool_array();
write!(f, "[{}, {}, {}]", arr[0], arr[1], arr[2])
}
}
impl From<BVec3> for [bool; 3] {
#[inline]
fn from(mask: BVec3) -> Self {
mask.into_bool_array()
}
}
impl From<BVec3> for [u32; 3] {
#[inline]
fn from(mask: BVec3) -> Self {
mask.into_u32_array()
}
}