1#[cfg(not(target_arch = "spirv"))]
4use core::fmt;
5use core::ops::*;
6
7#[derive(Clone, Copy, PartialEq, Eq, Hash)]
9#[repr(C, align(1))]
10pub struct BVec3 {
11 pub x: bool,
12 pub y: bool,
13 pub z: bool,
14}
15
16const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
17
18impl BVec3 {
19 pub const FALSE: Self = Self::splat(false);
21
22 pub const TRUE: Self = Self::splat(true);
24
25 #[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 #[inline]
34 #[must_use]
35 pub const fn splat(v: bool) -> Self {
36 Self::new(v, v, v)
37 }
38
39 #[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 #[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 #[inline]
58 #[must_use]
59 pub fn any(self) -> bool {
60 self.x || self.y || self.z
61 }
62
63 #[inline]
65 #[must_use]
66 pub fn all(self) -> bool {
67 self.x && self.y && self.z
68 }
69
70 #[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 #[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
114impl Default for BVec3 {
115 #[inline]
116 fn default() -> Self {
117 Self::FALSE
118 }
119}
120
121impl 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
133impl BitAndAssign for BVec3 {
134 #[inline]
135 fn bitand_assign(&mut self, rhs: Self) {
136 *self = self.bitand(rhs);
137 }
138}
139
140impl 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
152impl BitOrAssign for BVec3 {
153 #[inline]
154 fn bitor_assign(&mut self, rhs: Self) {
155 *self = self.bitor(rhs);
156 }
157}
158
159impl 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
171impl BitXorAssign for BVec3 {
172 #[inline]
173 fn bitxor_assign(&mut self, rhs: Self) {
174 *self = self.bitxor(rhs);
175 }
176}
177
178impl 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"))]
191impl 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"))]
206impl 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
213impl From<[bool; 3]> for BVec3 {
214 #[inline]
215 fn from(a: [bool; 3]) -> Self {
216 Self::from_array(a)
217 }
218}
219
220impl From<BVec3> for [bool; 3] {
221 #[inline]
222 fn from(mask: BVec3) -> Self {
223 mask.into_bool_array()
224 }
225}
226
227impl From<BVec3> for [u32; 3] {
228 #[inline]
229 fn from(mask: BVec3) -> Self {
230 mask.into_u32_array()
231 }
232}