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 BVec4 {
11 pub x: bool,
12 pub y: bool,
13 pub z: bool,
14 pub w: bool,
15}
16
17const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
18
19impl BVec4 {
20 pub const FALSE: Self = Self::splat(false);
22
23 pub const TRUE: Self = Self::splat(true);
25
26 #[inline(always)]
28 #[must_use]
29 pub const fn new(x: bool, y: bool, z: bool, w: bool) -> Self {
30 Self { x, y, z, w }
31 }
32
33 #[inline]
35 #[must_use]
36 pub const fn splat(v: bool) -> Self {
37 Self::new(v, v, v, v)
38 }
39
40 #[inline]
42 #[must_use]
43 pub const fn from_array(a: [bool; 4]) -> Self {
44 Self::new(a[0], a[1], a[2], a[3])
45 }
46
47 #[inline]
52 #[must_use]
53 pub fn bitmask(self) -> u32 {
54 (self.x as u32) | (self.y as u32) << 1 | (self.z as u32) << 2 | (self.w as u32) << 3
55 }
56
57 #[inline]
59 #[must_use]
60 pub fn any(self) -> bool {
61 self.x || self.y || self.z || self.w
62 }
63
64 #[inline]
66 #[must_use]
67 pub fn all(self) -> bool {
68 self.x && self.y && self.z && self.w
69 }
70
71 #[inline]
75 #[must_use]
76 pub fn test(&self, index: usize) -> bool {
77 match index {
78 0 => self.x,
79 1 => self.y,
80 2 => self.z,
81 3 => self.w,
82 _ => panic!("index out of bounds"),
83 }
84 }
85
86 #[inline]
90 pub fn set(&mut self, index: usize, value: bool) {
91 match index {
92 0 => self.x = value,
93 1 => self.y = value,
94 2 => self.z = value,
95 3 => self.w = value,
96 _ => panic!("index out of bounds"),
97 }
98 }
99
100 #[inline]
101 #[must_use]
102 fn into_bool_array(self) -> [bool; 4] {
103 [self.x, self.y, self.z, self.w]
104 }
105
106 #[inline]
107 #[must_use]
108 fn into_u32_array(self) -> [u32; 4] {
109 [
110 MASK[self.x as usize],
111 MASK[self.y as usize],
112 MASK[self.z as usize],
113 MASK[self.w as usize],
114 ]
115 }
116}
117
118impl Default for BVec4 {
119 #[inline]
120 fn default() -> Self {
121 Self::FALSE
122 }
123}
124
125impl BitAnd for BVec4 {
126 type Output = Self;
127 #[inline]
128 fn bitand(self, rhs: Self) -> Self {
129 Self {
130 x: self.x & rhs.x,
131 y: self.y & rhs.y,
132 z: self.z & rhs.z,
133 w: self.w & rhs.w,
134 }
135 }
136}
137
138impl BitAndAssign for BVec4 {
139 #[inline]
140 fn bitand_assign(&mut self, rhs: Self) {
141 *self = self.bitand(rhs);
142 }
143}
144
145impl BitOr for BVec4 {
146 type Output = Self;
147 #[inline]
148 fn bitor(self, rhs: Self) -> Self {
149 Self {
150 x: self.x | rhs.x,
151 y: self.y | rhs.y,
152 z: self.z | rhs.z,
153 w: self.w | rhs.w,
154 }
155 }
156}
157
158impl BitOrAssign for BVec4 {
159 #[inline]
160 fn bitor_assign(&mut self, rhs: Self) {
161 *self = self.bitor(rhs);
162 }
163}
164
165impl BitXor for BVec4 {
166 type Output = Self;
167 #[inline]
168 fn bitxor(self, rhs: Self) -> Self {
169 Self {
170 x: self.x ^ rhs.x,
171 y: self.y ^ rhs.y,
172 z: self.z ^ rhs.z,
173 w: self.w ^ rhs.w,
174 }
175 }
176}
177
178impl BitXorAssign for BVec4 {
179 #[inline]
180 fn bitxor_assign(&mut self, rhs: Self) {
181 *self = self.bitxor(rhs);
182 }
183}
184
185impl Not for BVec4 {
186 type Output = Self;
187 #[inline]
188 fn not(self) -> Self {
189 Self {
190 x: !self.x,
191 y: !self.y,
192 z: !self.z,
193 w: !self.w,
194 }
195 }
196}
197
198#[cfg(not(target_arch = "spirv"))]
199impl fmt::Debug for BVec4 {
200 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
201 let arr = self.into_u32_array();
202 write!(
203 f,
204 "{}({:#x}, {:#x}, {:#x}, {:#x})",
205 stringify!(BVec4),
206 arr[0],
207 arr[1],
208 arr[2],
209 arr[3]
210 )
211 }
212}
213
214#[cfg(not(target_arch = "spirv"))]
215impl fmt::Display for BVec4 {
216 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
217 let arr = self.into_bool_array();
218 write!(f, "[{}, {}, {}, {}]", arr[0], arr[1], arr[2], arr[3])
219 }
220}
221
222impl From<[bool; 4]> for BVec4 {
223 #[inline]
224 fn from(a: [bool; 4]) -> Self {
225 Self::from_array(a)
226 }
227}
228
229impl From<BVec4> for [bool; 4] {
230 #[inline]
231 fn from(mask: BVec4) -> Self {
232 mask.into_bool_array()
233 }
234}
235
236impl From<BVec4> for [u32; 4] {
237 #[inline]
238 fn from(mask: BVec4) -> Self {
239 mask.into_u32_array()
240 }
241}