1use super::*;
2
3impl BvhGeomTri {
5 #[must_use]
15 pub(crate) fn from_triangle(tri: &BvhTriangle) -> Self {
16 let v0: Vec3 = tri.get_v0();
17 let e1: Vec3 = tri.get_edge1();
18 let e2: Vec3 = tri.get_edge2();
19 Self {
20 v0: [v0.get_x(), v0.get_y(), v0.get_z()],
21 e1: [e1.get_x(), e1.get_y(), e1.get_z()],
22 e2: [e2.get_x(), e2.get_y(), e2.get_z()],
23 }
24 }
25
26 #[inline(always)]
40 #[must_use]
41 pub(crate) fn intersect_raw(
42 &self,
43 origin: Vec3,
44 dir: Vec3,
45 t_min: f32,
46 t_max: f32,
47 ) -> Option<f32> {
48 let Self { v0, e1, e2 } = self;
49 let dx: f32 = dir.get_x();
50 let dy: f32 = dir.get_y();
51 let dz: f32 = dir.get_z();
52 let hx: f32 = dy * e2[2] - dz * e2[1];
53 let hy: f32 = dz * e2[0] - dx * e2[2];
54 let hz: f32 = dx * e2[1] - dy * e2[0];
55 let det: f32 = e1[0] * hx + e1[1] * hy + e1[2] * hz;
56 let sx: f32 = origin.get_x() - v0[0];
57 let sy: f32 = origin.get_y() - v0[1];
58 let sz: f32 = origin.get_z() - v0[2];
59 if det > 1e-6 {
60 let u_raw: f32 = sx * hx + sy * hy + sz * hz;
61 if u_raw < 0.0 || u_raw > det {
62 return None;
63 }
64 let qx: f32 = sy * e1[2] - sz * e1[1];
65 let qy: f32 = sz * e1[0] - sx * e1[2];
66 let qz: f32 = sx * e1[1] - sy * e1[0];
67 let v_raw: f32 = dx * qx + dy * qy + dz * qz;
68 if v_raw < 0.0 || u_raw + v_raw > det {
69 return None;
70 }
71 let t_raw: f32 = e2[0] * qx + e2[1] * qy + e2[2] * qz;
72 if t_raw <= t_min * det || t_raw >= t_max * det {
73 return None;
74 }
75 Some(t_raw / det)
76 } else if det < -1e-6 {
77 let u_raw: f32 = sx * hx + sy * hy + sz * hz;
78 if u_raw > 0.0 || u_raw < det {
79 return None;
80 }
81 let qx: f32 = sy * e1[2] - sz * e1[1];
82 let qy: f32 = sz * e1[0] - sx * e1[2];
83 let qz: f32 = sx * e1[1] - sy * e1[0];
84 let v_raw: f32 = dx * qx + dy * qy + dz * qz;
85 if v_raw > 0.0 || u_raw + v_raw < det {
86 return None;
87 }
88 let t_raw: f32 = e2[0] * qx + e2[1] * qy + e2[2] * qz;
89 if t_raw >= t_min * det || t_raw <= t_max * det {
90 return None;
91 }
92 Some(t_raw / det)
93 } else {
94 None
95 }
96 }
97
98 #[inline(always)]
112 #[must_use]
113 pub(crate) fn intersect_any_raw(
114 &self,
115 origin: Vec3,
116 dir: Vec3,
117 t_min: f32,
118 t_max: f32,
119 ) -> bool {
120 let Self { v0, e1, e2 } = self;
121 let dx: f32 = dir.get_x();
122 let dy: f32 = dir.get_y();
123 let dz: f32 = dir.get_z();
124 let hx: f32 = dy * e2[2] - dz * e2[1];
125 let hy: f32 = dz * e2[0] - dx * e2[2];
126 let hz: f32 = dx * e2[1] - dy * e2[0];
127 let det: f32 = e1[0] * hx + e1[1] * hy + e1[2] * hz;
128 let sx: f32 = origin.get_x() - v0[0];
129 let sy: f32 = origin.get_y() - v0[1];
130 let sz: f32 = origin.get_z() - v0[2];
131 if det > 1e-6 {
132 let u_raw: f32 = sx * hx + sy * hy + sz * hz;
133 if u_raw < 0.0 || u_raw > det {
134 return false;
135 }
136 let qx: f32 = sy * e1[2] - sz * e1[1];
137 let qy: f32 = sz * e1[0] - sx * e1[2];
138 let qz: f32 = sx * e1[1] - sy * e1[0];
139 let v_raw: f32 = dx * qx + dy * qy + dz * qz;
140 if v_raw < 0.0 || u_raw + v_raw > det {
141 return false;
142 }
143 let t_raw: f32 = e2[0] * qx + e2[1] * qy + e2[2] * qz;
144 t_raw > t_min * det && t_raw < t_max * det
145 } else if det < -1e-6 {
146 let u_raw: f32 = sx * hx + sy * hy + sz * hz;
147 if u_raw > 0.0 || u_raw < det {
148 return false;
149 }
150 let qx: f32 = sy * e1[2] - sz * e1[1];
151 let qy: f32 = sz * e1[0] - sx * e1[2];
152 let qz: f32 = sx * e1[1] - sy * e1[0];
153 let v_raw: f32 = dx * qx + dy * qy + dz * qz;
154 if v_raw > 0.0 || u_raw + v_raw < det {
155 return false;
156 }
157 let t_raw: f32 = e2[0] * qx + e2[1] * qy + e2[2] * qz;
158 t_raw < t_min * det && t_raw > t_max * det
159 } else {
160 false
161 }
162 }
163
164 #[inline(always)]
177 #[must_use]
178 pub(crate) fn intersect_any_packet4(&self, pkt: &RayPacket4, t_min: f32, t_max: f32) -> u8 {
179 #[cfg(target_arch = "aarch64")]
180 {
181 let Self { v0, e1, e2 } = self;
182 unsafe {
184 let ox: core::arch::aarch64::float32x4_t =
185 core::arch::aarch64::vld1q_f32(pkt.ox.as_ptr());
186 let oy: core::arch::aarch64::float32x4_t =
187 core::arch::aarch64::vld1q_f32(pkt.oy.as_ptr());
188 let oz: core::arch::aarch64::float32x4_t =
189 core::arch::aarch64::vld1q_f32(pkt.oz.as_ptr());
190 let dx: core::arch::aarch64::float32x4_t =
191 core::arch::aarch64::vld1q_f32(pkt.dx.as_ptr());
192 let dy: core::arch::aarch64::float32x4_t =
193 core::arch::aarch64::vld1q_f32(pkt.dy.as_ptr());
194 let dz: core::arch::aarch64::float32x4_t =
195 core::arch::aarch64::vld1q_f32(pkt.dz.as_ptr());
196 let e1x: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(e1[0]);
197 let e1y: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(e1[1]);
198 let e1z: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(e1[2]);
199 let e2x: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(e2[0]);
200 let e2y: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(e2[1]);
201 let e2z: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(e2[2]);
202 let hx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
203 core::arch::aarch64::vmulq_f32(dy, e2z),
204 dz,
205 e2y,
206 );
207 let hy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
208 core::arch::aarch64::vmulq_f32(dz, e2x),
209 dx,
210 e2z,
211 );
212 let hz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
213 core::arch::aarch64::vmulq_f32(dx, e2y),
214 dy,
215 e2x,
216 );
217 let det: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
218 core::arch::aarch64::vfmaq_f32(
219 core::arch::aarch64::vmulq_f32(e1z, hz),
220 e1y,
221 hy,
222 ),
223 e1x,
224 hx,
225 );
226 let sign_bits: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
227 core::arch::aarch64::vreinterpretq_u32_f32(det),
228 core::arch::aarch64::vdupq_n_u32(0x8000_0000),
229 );
230 let ad: core::arch::aarch64::float32x4_t = core::arch::aarch64::vabsq_f32(det);
231 let sx: core::arch::aarch64::float32x4_t =
232 core::arch::aarch64::vsubq_f32(ox, core::arch::aarch64::vdupq_n_f32(v0[0]));
233 let sy: core::arch::aarch64::float32x4_t =
234 core::arch::aarch64::vsubq_f32(oy, core::arch::aarch64::vdupq_n_f32(v0[1]));
235 let sz: core::arch::aarch64::float32x4_t =
236 core::arch::aarch64::vsubq_f32(oz, core::arch::aarch64::vdupq_n_f32(v0[2]));
237 let u_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
238 core::arch::aarch64::vfmaq_f32(core::arch::aarch64::vmulq_f32(sz, hz), sy, hy),
239 sx,
240 hx,
241 );
242 let su: core::arch::aarch64::float32x4_t =
243 core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
244 core::arch::aarch64::vreinterpretq_u32_f32(u_raw),
245 sign_bits,
246 ));
247 let qx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
248 core::arch::aarch64::vmulq_f32(sy, e1z),
249 sz,
250 e1y,
251 );
252 let qy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
253 core::arch::aarch64::vmulq_f32(sz, e1x),
254 sx,
255 e1z,
256 );
257 let qz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
258 core::arch::aarch64::vmulq_f32(sx, e1y),
259 sy,
260 e1x,
261 );
262 let v_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
263 core::arch::aarch64::vfmaq_f32(core::arch::aarch64::vmulq_f32(dz, qz), dy, qy),
264 dx,
265 qx,
266 );
267 let sv: core::arch::aarch64::float32x4_t =
268 core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
269 core::arch::aarch64::vreinterpretq_u32_f32(v_raw),
270 sign_bits,
271 ));
272 let t_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
273 core::arch::aarch64::vfmaq_f32(
274 core::arch::aarch64::vmulq_f32(e2z, qz),
275 e2y,
276 qy,
277 ),
278 e2x,
279 qx,
280 );
281 let node_style: core::arch::aarch64::float32x4_t =
282 core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
283 core::arch::aarch64::vreinterpretq_u32_f32(t_raw),
284 sign_bits,
285 ));
286 let zero: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(0.0);
287 let m0: core::arch::aarch64::uint32x4_t =
288 core::arch::aarch64::vcgtq_f32(ad, core::arch::aarch64::vdupq_n_f32(1e-6));
289 let m1: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgeq_f32(su, zero);
290 let m2: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgeq_f32(sv, zero);
291 let m3: core::arch::aarch64::uint32x4_t =
292 core::arch::aarch64::vcleq_f32(core::arch::aarch64::vaddq_f32(su, sv), ad);
293 let m4: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgtq_f32(
294 node_style,
295 core::arch::aarch64::vmulq_f32(core::arch::aarch64::vdupq_n_f32(t_min), ad),
296 );
297 let m5: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcltq_f32(
298 node_style,
299 core::arch::aarch64::vmulq_f32(core::arch::aarch64::vdupq_n_f32(t_max), ad),
300 );
301 let hit_v: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
302 core::arch::aarch64::vandq_u32(
303 core::arch::aarch64::vandq_u32(m0, m1),
304 core::arch::aarch64::vandq_u32(m2, m3),
305 ),
306 core::arch::aarch64::vandq_u32(m4, m5),
307 );
308 let lane_bits: [u32; 4] = [1, 2, 4, 8];
309 let mask: u32 = core::arch::aarch64::vaddvq_u32(core::arch::aarch64::vandq_u32(
310 hit_v,
311 core::arch::aarch64::vld1q_u32(lane_bits.as_ptr()),
312 ));
313 mask as u8
314 }
315 }
316 #[cfg(target_arch = "x86_64")]
317 {
318 let Self { v0, e1, e2 } = self;
319 unsafe {
321 let ox: core::arch::x86_64::__m128 =
322 core::arch::x86_64::_mm_loadu_ps(pkt.ox.as_ptr());
323 let oy: core::arch::x86_64::__m128 =
324 core::arch::x86_64::_mm_loadu_ps(pkt.oy.as_ptr());
325 let oz: core::arch::x86_64::__m128 =
326 core::arch::x86_64::_mm_loadu_ps(pkt.oz.as_ptr());
327 let dx: core::arch::x86_64::__m128 =
328 core::arch::x86_64::_mm_loadu_ps(pkt.dx.as_ptr());
329 let dy: core::arch::x86_64::__m128 =
330 core::arch::x86_64::_mm_loadu_ps(pkt.dy.as_ptr());
331 let dz: core::arch::x86_64::__m128 =
332 core::arch::x86_64::_mm_loadu_ps(pkt.dz.as_ptr());
333 let e1x: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(e1[0]);
334 let e1y: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(e1[1]);
335 let e1z: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(e1[2]);
336 let e2x: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(e2[0]);
337 let e2y: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(e2[1]);
338 let e2z: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(e2[2]);
339 let hx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
340 core::arch::x86_64::_mm_mul_ps(dy, e2z),
341 core::arch::x86_64::_mm_mul_ps(dz, e2y),
342 );
343 let hy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
344 core::arch::x86_64::_mm_mul_ps(dz, e2x),
345 core::arch::x86_64::_mm_mul_ps(dx, e2z),
346 );
347 let hz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
348 core::arch::x86_64::_mm_mul_ps(dx, e2y),
349 core::arch::x86_64::_mm_mul_ps(dy, e2x),
350 );
351 let det: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
352 core::arch::x86_64::_mm_add_ps(
353 core::arch::x86_64::_mm_mul_ps(e1x, hx),
354 core::arch::x86_64::_mm_mul_ps(e1y, hy),
355 ),
356 core::arch::x86_64::_mm_mul_ps(e1z, hz),
357 );
358 let sign_mask: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(-0.0);
359 let sign_bits: core::arch::x86_64::__m128 =
360 core::arch::x86_64::_mm_and_ps(det, sign_mask);
361 let ad: core::arch::x86_64::__m128 =
362 core::arch::x86_64::_mm_andnot_ps(sign_mask, det);
363 let sx: core::arch::x86_64::__m128 =
364 core::arch::x86_64::_mm_sub_ps(ox, core::arch::x86_64::_mm_set1_ps(v0[0]));
365 let sy: core::arch::x86_64::__m128 =
366 core::arch::x86_64::_mm_sub_ps(oy, core::arch::x86_64::_mm_set1_ps(v0[1]));
367 let sz: core::arch::x86_64::__m128 =
368 core::arch::x86_64::_mm_sub_ps(oz, core::arch::x86_64::_mm_set1_ps(v0[2]));
369 let u_raw: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
370 core::arch::x86_64::_mm_add_ps(
371 core::arch::x86_64::_mm_mul_ps(sx, hx),
372 core::arch::x86_64::_mm_mul_ps(sy, hy),
373 ),
374 core::arch::x86_64::_mm_mul_ps(sz, hz),
375 );
376 let su: core::arch::x86_64::__m128 =
377 core::arch::x86_64::_mm_xor_ps(u_raw, sign_bits);
378 let qx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
379 core::arch::x86_64::_mm_mul_ps(sy, e1z),
380 core::arch::x86_64::_mm_mul_ps(sz, e1y),
381 );
382 let qy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
383 core::arch::x86_64::_mm_mul_ps(sz, e1x),
384 core::arch::x86_64::_mm_mul_ps(sx, e1z),
385 );
386 let qz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
387 core::arch::x86_64::_mm_mul_ps(sx, e1y),
388 core::arch::x86_64::_mm_mul_ps(sy, e1x),
389 );
390 let v_raw: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
391 core::arch::x86_64::_mm_add_ps(
392 core::arch::x86_64::_mm_mul_ps(dx, qx),
393 core::arch::x86_64::_mm_mul_ps(dy, qy),
394 ),
395 core::arch::x86_64::_mm_mul_ps(dz, qz),
396 );
397 let sv: core::arch::x86_64::__m128 =
398 core::arch::x86_64::_mm_xor_ps(v_raw, sign_bits);
399 let t_raw: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
400 core::arch::x86_64::_mm_add_ps(
401 core::arch::x86_64::_mm_mul_ps(e2x, qx),
402 core::arch::x86_64::_mm_mul_ps(e2y, qy),
403 ),
404 core::arch::x86_64::_mm_mul_ps(e2z, qz),
405 );
406 let active_style: core::arch::x86_64::__m128 =
407 core::arch::x86_64::_mm_xor_ps(t_raw, sign_bits);
408 let zero: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_setzero_ps();
409 let m0: core::arch::x86_64::__m128 =
410 core::arch::x86_64::_mm_cmpgt_ps(ad, core::arch::x86_64::_mm_set1_ps(1e-6));
411 let m1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmpge_ps(su, zero);
412 let m2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmpge_ps(sv, zero);
413 let m3: core::arch::x86_64::__m128 =
414 core::arch::x86_64::_mm_cmple_ps(core::arch::x86_64::_mm_add_ps(su, sv), ad);
415 let m4: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmpgt_ps(
416 active_style,
417 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(t_min), ad),
418 );
419 let m5: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmplt_ps(
420 active_style,
421 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(t_max), ad),
422 );
423 let hit_v: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_and_ps(
424 core::arch::x86_64::_mm_and_ps(
425 core::arch::x86_64::_mm_and_ps(m0, m1),
426 core::arch::x86_64::_mm_and_ps(m2, m3),
427 ),
428 core::arch::x86_64::_mm_and_ps(m4, m5),
429 );
430 core::arch::x86_64::_mm_movemask_ps(hit_v) as u8
431 }
432 }
433 #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
434 {
435 let Self { v0, e1, e2 } = self;
436 unsafe {
438 let ox: core::arch::wasm32::v128 =
439 core::arch::wasm32::v128_load(pkt.ox.as_ptr().cast());
440 let oy: core::arch::wasm32::v128 =
441 core::arch::wasm32::v128_load(pkt.oy.as_ptr().cast());
442 let oz: core::arch::wasm32::v128 =
443 core::arch::wasm32::v128_load(pkt.oz.as_ptr().cast());
444 let dx: core::arch::wasm32::v128 =
445 core::arch::wasm32::v128_load(pkt.dx.as_ptr().cast());
446 let dy: core::arch::wasm32::v128 =
447 core::arch::wasm32::v128_load(pkt.dy.as_ptr().cast());
448 let dz: core::arch::wasm32::v128 =
449 core::arch::wasm32::v128_load(pkt.dz.as_ptr().cast());
450 let e1x: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(e1[0]);
451 let e1y: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(e1[1]);
452 let e1z: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(e1[2]);
453 let e2x: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(e2[0]);
454 let e2y: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(e2[1]);
455 let e2z: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(e2[2]);
456 let hx: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
457 core::arch::wasm32::f32x4_mul(dy, e2z),
458 core::arch::wasm32::f32x4_mul(dz, e2y),
459 );
460 let hy: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
461 core::arch::wasm32::f32x4_mul(dz, e2x),
462 core::arch::wasm32::f32x4_mul(dx, e2z),
463 );
464 let hz: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
465 core::arch::wasm32::f32x4_mul(dx, e2y),
466 core::arch::wasm32::f32x4_mul(dy, e2x),
467 );
468 let det: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
469 core::arch::wasm32::f32x4_add(
470 core::arch::wasm32::f32x4_mul(e1x, hx),
471 core::arch::wasm32::f32x4_mul(e1y, hy),
472 ),
473 core::arch::wasm32::f32x4_mul(e1z, hz),
474 );
475 let sign_bits: core::arch::wasm32::v128 =
476 core::arch::wasm32::v128_and(det, core::arch::wasm32::u32x4_splat(0x8000_0000));
477 let ad: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_abs(det);
478 let sx: core::arch::wasm32::v128 =
479 core::arch::wasm32::f32x4_sub(ox, core::arch::wasm32::f32x4_splat(v0[0]));
480 let sy: core::arch::wasm32::v128 =
481 core::arch::wasm32::f32x4_sub(oy, core::arch::wasm32::f32x4_splat(v0[1]));
482 let sz: core::arch::wasm32::v128 =
483 core::arch::wasm32::f32x4_sub(oz, core::arch::wasm32::f32x4_splat(v0[2]));
484 let u_raw: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
485 core::arch::wasm32::f32x4_add(
486 core::arch::wasm32::f32x4_mul(sx, hx),
487 core::arch::wasm32::f32x4_mul(sy, hy),
488 ),
489 core::arch::wasm32::f32x4_mul(sz, hz),
490 );
491 let su: core::arch::wasm32::v128 = core::arch::wasm32::v128_xor(u_raw, sign_bits);
492 let qx: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
493 core::arch::wasm32::f32x4_mul(sy, e1z),
494 core::arch::wasm32::f32x4_mul(sz, e1y),
495 );
496 let qy: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
497 core::arch::wasm32::f32x4_mul(sz, e1x),
498 core::arch::wasm32::f32x4_mul(sx, e1z),
499 );
500 let qz: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
501 core::arch::wasm32::f32x4_mul(sx, e1y),
502 core::arch::wasm32::f32x4_mul(sy, e1x),
503 );
504 let v_raw: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
505 core::arch::wasm32::f32x4_add(
506 core::arch::wasm32::f32x4_mul(dx, qx),
507 core::arch::wasm32::f32x4_mul(dy, qy),
508 ),
509 core::arch::wasm32::f32x4_mul(dz, qz),
510 );
511 let sv: core::arch::wasm32::v128 = core::arch::wasm32::v128_xor(v_raw, sign_bits);
512 let t_raw: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
513 core::arch::wasm32::f32x4_add(
514 core::arch::wasm32::f32x4_mul(e2x, qx),
515 core::arch::wasm32::f32x4_mul(e2y, qy),
516 ),
517 core::arch::wasm32::f32x4_mul(e2z, qz),
518 );
519 let state_val: core::arch::wasm32::v128 =
520 core::arch::wasm32::v128_xor(t_raw, sign_bits);
521 let zero: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(0.0);
522 let m0: core::arch::wasm32::v128 =
523 core::arch::wasm32::f32x4_gt(ad, core::arch::wasm32::f32x4_splat(1e-6));
524 let m1: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_ge(su, zero);
525 let m2: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_ge(sv, zero);
526 let m3: core::arch::wasm32::v128 =
527 core::arch::wasm32::f32x4_le(core::arch::wasm32::f32x4_add(su, sv), ad);
528 let m4: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_gt(
529 state_val,
530 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(t_min), ad),
531 );
532 let m5: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_lt(
533 state_val,
534 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(t_max), ad),
535 );
536 let hit_v: core::arch::wasm32::v128 = core::arch::wasm32::v128_and(
537 core::arch::wasm32::v128_and(
538 core::arch::wasm32::v128_and(m0, m1),
539 core::arch::wasm32::v128_and(m2, m3),
540 ),
541 core::arch::wasm32::v128_and(m4, m5),
542 );
543 core::arch::wasm32::i32x4_bitmask(hit_v) as u8
544 }
545 }
546 #[cfg(not(target_arch = "aarch64"))]
547 #[cfg(not(target_arch = "x86_64"))]
548 #[cfg(not(all(target_arch = "wasm32", target_feature = "simd128")))]
549 {
550 let mut mask: u8 = 0;
551 for lane in 0..4 {
552 if self.intersect_any_raw(
553 Vec3::new(pkt.ox[lane], pkt.oy[lane], pkt.oz[lane]),
554 Vec3::new(pkt.dx[lane], pkt.dy[lane], pkt.dz[lane]),
555 t_min,
556 t_max,
557 ) {
558 mask |= 1_u8 << lane;
559 }
560 }
561 mask
562 }
563 }
564}
565
566impl BvhTri4 {
568 #[must_use]
579 pub(crate) fn from_geom_chunk(slice: &[BvhGeomTri], base_tri_idx: u32) -> Self {
580 let mut v0x: [f32; 4] = [0.0; 4];
581 let mut v0y: [f32; 4] = [0.0; 4];
582 let mut v0z: [f32; 4] = [0.0; 4];
583 let mut e1x: [f32; 4] = [0.0; 4];
584 let mut e1y: [f32; 4] = [0.0; 4];
585 let mut e1z: [f32; 4] = [0.0; 4];
586 let mut e2x: [f32; 4] = [0.0; 4];
587 let mut e2y: [f32; 4] = [0.0; 4];
588 let mut e2z: [f32; 4] = [0.0; 4];
589 let mut tri_indices: [u32; 4] = [u32::MAX; 4];
590 for (lane, tri) in slice.iter().take(4).enumerate() {
591 v0x[lane] = tri.v0[0];
592 v0y[lane] = tri.v0[1];
593 v0z[lane] = tri.v0[2];
594 e1x[lane] = tri.e1[0];
595 e1y[lane] = tri.e1[1];
596 e1z[lane] = tri.e1[2];
597 e2x[lane] = tri.e2[0];
598 e2y[lane] = tri.e2[1];
599 e2z[lane] = tri.e2[2];
600 tri_indices[lane] = base_tri_idx.wrapping_add(lane as u32);
601 }
602 Self {
603 v0x,
604 v0y,
605 v0z,
606 e1x,
607 e1y,
608 e1z,
609 e2x,
610 e2y,
611 e2z,
612 tri_indices,
613 }
614 }
615
616 #[inline(always)]
630 #[must_use]
631 pub(crate) fn intersect_any_4(
632 &self,
633 orig: [f32; 3],
634 dir: [f32; 3],
635 t_min: f32,
636 t_max: f32,
637 ) -> u32 {
638 let Self {
639 v0x,
640 v0y,
641 v0z,
642 e1x,
643 e1y,
644 e1z,
645 e2x,
646 e2y,
647 e2z,
648 tri_indices,
649 } = self;
650 #[cfg(target_arch = "aarch64")]
651 {
652 let mask: u32 = unsafe {
654 let ox: core::arch::aarch64::float32x4_t =
655 core::arch::aarch64::vdupq_n_f32(orig[0]);
656 let oy: core::arch::aarch64::float32x4_t =
657 core::arch::aarch64::vdupq_n_f32(orig[1]);
658 let oz: core::arch::aarch64::float32x4_t =
659 core::arch::aarch64::vdupq_n_f32(orig[2]);
660 let dx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(dir[0]);
661 let dy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(dir[1]);
662 let dz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(dir[2]);
663 let ve2x: core::arch::aarch64::float32x4_t =
664 core::arch::aarch64::vld1q_f32(e2x.as_ptr());
665 let ve2y: core::arch::aarch64::float32x4_t =
666 core::arch::aarch64::vld1q_f32(e2y.as_ptr());
667 let ve2z: core::arch::aarch64::float32x4_t =
668 core::arch::aarch64::vld1q_f32(e2z.as_ptr());
669 let hx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
670 core::arch::aarch64::vmulq_f32(dy, ve2z),
671 dz,
672 ve2y,
673 );
674 let hy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
675 core::arch::aarch64::vmulq_f32(dz, ve2x),
676 dx,
677 ve2z,
678 );
679 let hz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
680 core::arch::aarch64::vmulq_f32(dx, ve2y),
681 dy,
682 ve2x,
683 );
684 let ve1x: core::arch::aarch64::float32x4_t =
685 core::arch::aarch64::vld1q_f32(e1x.as_ptr());
686 let ve1y: core::arch::aarch64::float32x4_t =
687 core::arch::aarch64::vld1q_f32(e1y.as_ptr());
688 let ve1z: core::arch::aarch64::float32x4_t =
689 core::arch::aarch64::vld1q_f32(e1z.as_ptr());
690 let det: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
691 core::arch::aarch64::vfmaq_f32(
692 core::arch::aarch64::vmulq_f32(ve1z, hz),
693 ve1y,
694 hy,
695 ),
696 ve1x,
697 hx,
698 );
699 let sign_bits: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
700 core::arch::aarch64::vreinterpretq_u32_f32(det),
701 core::arch::aarch64::vdupq_n_u32(0x8000_0000),
702 );
703 let ad: core::arch::aarch64::float32x4_t = core::arch::aarch64::vabsq_f32(det);
704 let sx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vsubq_f32(
705 ox,
706 core::arch::aarch64::vld1q_f32(v0x.as_ptr()),
707 );
708 let sy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vsubq_f32(
709 oy,
710 core::arch::aarch64::vld1q_f32(v0y.as_ptr()),
711 );
712 let sz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vsubq_f32(
713 oz,
714 core::arch::aarch64::vld1q_f32(v0z.as_ptr()),
715 );
716 let u_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
717 core::arch::aarch64::vfmaq_f32(core::arch::aarch64::vmulq_f32(sz, hz), sy, hy),
718 sx,
719 hx,
720 );
721 let su: core::arch::aarch64::float32x4_t =
722 core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
723 core::arch::aarch64::vreinterpretq_u32_f32(u_raw),
724 sign_bits,
725 ));
726 let qx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
727 core::arch::aarch64::vmulq_f32(sy, ve1z),
728 sz,
729 ve1y,
730 );
731 let qy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
732 core::arch::aarch64::vmulq_f32(sz, ve1x),
733 sx,
734 ve1z,
735 );
736 let qz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
737 core::arch::aarch64::vmulq_f32(sx, ve1y),
738 sy,
739 ve1x,
740 );
741 let v_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
742 core::arch::aarch64::vfmaq_f32(core::arch::aarch64::vmulq_f32(dz, qz), dy, qy),
743 dx,
744 qx,
745 );
746 let sv: core::arch::aarch64::float32x4_t =
747 core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
748 core::arch::aarch64::vreinterpretq_u32_f32(v_raw),
749 sign_bits,
750 ));
751 let t_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
752 core::arch::aarch64::vfmaq_f32(
753 core::arch::aarch64::vmulq_f32(ve2z, qz),
754 ve2y,
755 qy,
756 ),
757 ve2x,
758 qx,
759 );
760 let node_style: core::arch::aarch64::float32x4_t =
761 core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
762 core::arch::aarch64::vreinterpretq_u32_f32(t_raw),
763 sign_bits,
764 ));
765 let zero: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(0.0);
766 let m0: core::arch::aarch64::uint32x4_t =
767 core::arch::aarch64::vcgtq_f32(ad, core::arch::aarch64::vdupq_n_f32(1e-6));
768 let m1: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgeq_f32(su, zero);
769 let m2: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgeq_f32(sv, zero);
770 let m3: core::arch::aarch64::uint32x4_t =
771 core::arch::aarch64::vcleq_f32(core::arch::aarch64::vaddq_f32(su, sv), ad);
772 let m4: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgtq_f32(
773 node_style,
774 core::arch::aarch64::vmulq_f32(core::arch::aarch64::vdupq_n_f32(t_min), ad),
775 );
776 let m5: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcltq_f32(
777 node_style,
778 core::arch::aarch64::vmulq_f32(core::arch::aarch64::vdupq_n_f32(t_max), ad),
779 );
780 let hit_v: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
781 core::arch::aarch64::vandq_u32(
782 core::arch::aarch64::vandq_u32(m0, m1),
783 core::arch::aarch64::vandq_u32(m2, m3),
784 ),
785 core::arch::aarch64::vandq_u32(m4, m5),
786 );
787 let lane_bits: [u32; 4] = [1, 2, 4, 8];
788 core::arch::aarch64::vaddvq_u32(core::arch::aarch64::vandq_u32(
789 hit_v,
790 core::arch::aarch64::vld1q_u32(lane_bits.as_ptr()),
791 ))
792 };
793 if mask != 0 {
794 tri_indices[mask.trailing_zeros() as usize]
795 } else {
796 u32::MAX
797 }
798 }
799 #[cfg(target_arch = "x86_64")]
800 {
801 let mask: i32 = unsafe {
803 let ox: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(orig[0]);
804 let oy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(orig[1]);
805 let oz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(orig[2]);
806 let dx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(dir[0]);
807 let dy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(dir[1]);
808 let dz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(dir[2]);
809 let ve2x: core::arch::x86_64::__m128 =
810 core::arch::x86_64::_mm_load_ps(e2x.as_ptr());
811 let ve2y: core::arch::x86_64::__m128 =
812 core::arch::x86_64::_mm_load_ps(e2y.as_ptr());
813 let ve2z: core::arch::x86_64::__m128 =
814 core::arch::x86_64::_mm_load_ps(e2z.as_ptr());
815 let hx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
816 core::arch::x86_64::_mm_mul_ps(dy, ve2z),
817 core::arch::x86_64::_mm_mul_ps(dz, ve2y),
818 );
819 let hy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
820 core::arch::x86_64::_mm_mul_ps(dz, ve2x),
821 core::arch::x86_64::_mm_mul_ps(dx, ve2z),
822 );
823 let hz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
824 core::arch::x86_64::_mm_mul_ps(dx, ve2y),
825 core::arch::x86_64::_mm_mul_ps(dy, ve2x),
826 );
827 let ve1x: core::arch::x86_64::__m128 =
828 core::arch::x86_64::_mm_load_ps(e1x.as_ptr());
829 let ve1y: core::arch::x86_64::__m128 =
830 core::arch::x86_64::_mm_load_ps(e1y.as_ptr());
831 let ve1z: core::arch::x86_64::__m128 =
832 core::arch::x86_64::_mm_load_ps(e1z.as_ptr());
833 let det: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
834 core::arch::x86_64::_mm_add_ps(
835 core::arch::x86_64::_mm_mul_ps(ve1x, hx),
836 core::arch::x86_64::_mm_mul_ps(ve1y, hy),
837 ),
838 core::arch::x86_64::_mm_mul_ps(ve1z, hz),
839 );
840 let sign_mask: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(-0.0);
841 let sign_bits: core::arch::x86_64::__m128 =
842 core::arch::x86_64::_mm_and_ps(det, sign_mask);
843 let ad: core::arch::x86_64::__m128 =
844 core::arch::x86_64::_mm_andnot_ps(sign_mask, det);
845 let sx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
846 ox,
847 core::arch::x86_64::_mm_load_ps(v0x.as_ptr()),
848 );
849 let sy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
850 oy,
851 core::arch::x86_64::_mm_load_ps(v0y.as_ptr()),
852 );
853 let sz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
854 oz,
855 core::arch::x86_64::_mm_load_ps(v0z.as_ptr()),
856 );
857 let u_raw: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
858 core::arch::x86_64::_mm_add_ps(
859 core::arch::x86_64::_mm_mul_ps(sx, hx),
860 core::arch::x86_64::_mm_mul_ps(sy, hy),
861 ),
862 core::arch::x86_64::_mm_mul_ps(sz, hz),
863 );
864 let su: core::arch::x86_64::__m128 =
865 core::arch::x86_64::_mm_xor_ps(u_raw, sign_bits);
866 let qx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
867 core::arch::x86_64::_mm_mul_ps(sy, ve1z),
868 core::arch::x86_64::_mm_mul_ps(sz, ve1y),
869 );
870 let qy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
871 core::arch::x86_64::_mm_mul_ps(sz, ve1x),
872 core::arch::x86_64::_mm_mul_ps(sx, ve1z),
873 );
874 let qz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
875 core::arch::x86_64::_mm_mul_ps(sx, ve1y),
876 core::arch::x86_64::_mm_mul_ps(sy, ve1x),
877 );
878 let v_raw: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
879 core::arch::x86_64::_mm_add_ps(
880 core::arch::x86_64::_mm_mul_ps(dx, qx),
881 core::arch::x86_64::_mm_mul_ps(dy, qy),
882 ),
883 core::arch::x86_64::_mm_mul_ps(dz, qz),
884 );
885 let sv: core::arch::x86_64::__m128 =
886 core::arch::x86_64::_mm_xor_ps(v_raw, sign_bits);
887 let t_raw: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
888 core::arch::x86_64::_mm_add_ps(
889 core::arch::x86_64::_mm_mul_ps(ve2x, qx),
890 core::arch::x86_64::_mm_mul_ps(ve2y, qy),
891 ),
892 core::arch::x86_64::_mm_mul_ps(ve2z, qz),
893 );
894 let active_style: core::arch::x86_64::__m128 =
895 core::arch::x86_64::_mm_xor_ps(t_raw, sign_bits);
896 let zero: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_setzero_ps();
897 let m0: core::arch::x86_64::__m128 =
898 core::arch::x86_64::_mm_cmpgt_ps(ad, core::arch::x86_64::_mm_set1_ps(1e-6));
899 let m1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmpge_ps(su, zero);
900 let m2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmpge_ps(sv, zero);
901 let m3: core::arch::x86_64::__m128 =
902 core::arch::x86_64::_mm_cmple_ps(core::arch::x86_64::_mm_add_ps(su, sv), ad);
903 let m4: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmpgt_ps(
904 active_style,
905 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(t_min), ad),
906 );
907 let m5: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmplt_ps(
908 active_style,
909 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(t_max), ad),
910 );
911 let hit_v: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_and_ps(
912 core::arch::x86_64::_mm_and_ps(
913 core::arch::x86_64::_mm_and_ps(m0, m1),
914 core::arch::x86_64::_mm_and_ps(m2, m3),
915 ),
916 core::arch::x86_64::_mm_and_ps(m4, m5),
917 );
918 core::arch::x86_64::_mm_movemask_ps(hit_v)
919 };
920 if mask != 0 {
921 tri_indices[(mask as u32).trailing_zeros() as usize]
922 } else {
923 u32::MAX
924 }
925 }
926 #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
927 {
928 let mask: u32 = unsafe {
930 let ox: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(orig[0]);
931 let oy: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(orig[1]);
932 let oz: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(orig[2]);
933 let dx: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(dir[0]);
934 let dy: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(dir[1]);
935 let dz: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(dir[2]);
936 let ve2x: core::arch::wasm32::v128 =
937 core::arch::wasm32::v128_load(e2x.as_ptr().cast());
938 let ve2y: core::arch::wasm32::v128 =
939 core::arch::wasm32::v128_load(e2y.as_ptr().cast());
940 let ve2z: core::arch::wasm32::v128 =
941 core::arch::wasm32::v128_load(e2z.as_ptr().cast());
942 let hx: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
943 core::arch::wasm32::f32x4_mul(dy, ve2z),
944 core::arch::wasm32::f32x4_mul(dz, ve2y),
945 );
946 let hy: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
947 core::arch::wasm32::f32x4_mul(dz, ve2x),
948 core::arch::wasm32::f32x4_mul(dx, ve2z),
949 );
950 let hz: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
951 core::arch::wasm32::f32x4_mul(dx, ve2y),
952 core::arch::wasm32::f32x4_mul(dy, ve2x),
953 );
954 let ve1x: core::arch::wasm32::v128 =
955 core::arch::wasm32::v128_load(e1x.as_ptr().cast());
956 let ve1y: core::arch::wasm32::v128 =
957 core::arch::wasm32::v128_load(e1y.as_ptr().cast());
958 let ve1z: core::arch::wasm32::v128 =
959 core::arch::wasm32::v128_load(e1z.as_ptr().cast());
960 let det: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
961 core::arch::wasm32::f32x4_add(
962 core::arch::wasm32::f32x4_mul(ve1x, hx),
963 core::arch::wasm32::f32x4_mul(ve1y, hy),
964 ),
965 core::arch::wasm32::f32x4_mul(ve1z, hz),
966 );
967 let sign_bits: core::arch::wasm32::v128 =
968 core::arch::wasm32::v128_and(det, core::arch::wasm32::u32x4_splat(0x8000_0000));
969 let ad: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_abs(det);
970 let sx: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
971 ox,
972 core::arch::wasm32::v128_load(v0x.as_ptr().cast()),
973 );
974 let sy: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
975 oy,
976 core::arch::wasm32::v128_load(v0y.as_ptr().cast()),
977 );
978 let sz: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
979 oz,
980 core::arch::wasm32::v128_load(v0z.as_ptr().cast()),
981 );
982 let u_raw: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
983 core::arch::wasm32::f32x4_add(
984 core::arch::wasm32::f32x4_mul(sx, hx),
985 core::arch::wasm32::f32x4_mul(sy, hy),
986 ),
987 core::arch::wasm32::f32x4_mul(sz, hz),
988 );
989 let su: core::arch::wasm32::v128 = core::arch::wasm32::v128_xor(u_raw, sign_bits);
990 let qx: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
991 core::arch::wasm32::f32x4_mul(sy, ve1z),
992 core::arch::wasm32::f32x4_mul(sz, ve1y),
993 );
994 let qy: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
995 core::arch::wasm32::f32x4_mul(sz, ve1x),
996 core::arch::wasm32::f32x4_mul(sx, ve1z),
997 );
998 let qz: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
999 core::arch::wasm32::f32x4_mul(sx, ve1y),
1000 core::arch::wasm32::f32x4_mul(sy, ve1x),
1001 );
1002 let v_raw: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
1003 core::arch::wasm32::f32x4_add(
1004 core::arch::wasm32::f32x4_mul(dx, qx),
1005 core::arch::wasm32::f32x4_mul(dy, qy),
1006 ),
1007 core::arch::wasm32::f32x4_mul(dz, qz),
1008 );
1009 let sv: core::arch::wasm32::v128 = core::arch::wasm32::v128_xor(v_raw, sign_bits);
1010 let t_raw: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
1011 core::arch::wasm32::f32x4_add(
1012 core::arch::wasm32::f32x4_mul(ve2x, qx),
1013 core::arch::wasm32::f32x4_mul(ve2y, qy),
1014 ),
1015 core::arch::wasm32::f32x4_mul(ve2z, qz),
1016 );
1017 let state_val: core::arch::wasm32::v128 =
1018 core::arch::wasm32::v128_xor(t_raw, sign_bits);
1019 let zero: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(0.0);
1020 let m0: core::arch::wasm32::v128 =
1021 core::arch::wasm32::f32x4_gt(ad, core::arch::wasm32::f32x4_splat(1e-6));
1022 let m1: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_ge(su, zero);
1023 let m2: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_ge(sv, zero);
1024 let m3: core::arch::wasm32::v128 =
1025 core::arch::wasm32::f32x4_le(core::arch::wasm32::f32x4_add(su, sv), ad);
1026 let m4: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_gt(
1027 state_val,
1028 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(t_min), ad),
1029 );
1030 let m5: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_lt(
1031 state_val,
1032 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(t_max), ad),
1033 );
1034 let hit_v: core::arch::wasm32::v128 = core::arch::wasm32::v128_and(
1035 core::arch::wasm32::v128_and(
1036 core::arch::wasm32::v128_and(m0, m1),
1037 core::arch::wasm32::v128_and(m2, m3),
1038 ),
1039 core::arch::wasm32::v128_and(m4, m5),
1040 );
1041 core::arch::wasm32::i32x4_bitmask(hit_v) as u32
1042 };
1043 if mask != 0 {
1044 tri_indices[mask.trailing_zeros() as usize]
1045 } else {
1046 u32::MAX
1047 }
1048 }
1049 #[cfg(not(target_arch = "aarch64"))]
1050 #[cfg(not(target_arch = "x86_64"))]
1051 #[cfg(not(all(target_arch = "wasm32", target_feature = "simd128")))]
1052 {
1053 let ox: f32 = orig[0];
1054 let oy: f32 = orig[1];
1055 let oz: f32 = orig[2];
1056 let dx: f32 = dir[0];
1057 let dy: f32 = dir[1];
1058 let dz: f32 = dir[2];
1059 for item_index in 0..4 {
1060 let hx: f32 = dy.mul_add(e2z[item_index], -(dz * e2y[item_index]));
1061 let hy: f32 = dz.mul_add(e2x[item_index], -(dx * e2z[item_index]));
1062 let hz: f32 = dx.mul_add(e2y[item_index], -(dy * e2x[item_index]));
1063 let det: f32 =
1064 e1x[item_index].mul_add(hx, e1y[item_index].mul_add(hy, e1z[item_index] * hz));
1065 let sign: f32 = 1.0_f32.copysign(det);
1066 let ad: f32 = det.abs();
1067 let sx: f32 = ox - v0x[item_index];
1068 let sy: f32 = oy - v0y[item_index];
1069 let sz: f32 = oz - v0z[item_index];
1070 let su: f32 = sx.mul_add(hx, sy.mul_add(hy, sz * hz)) * sign;
1071 let qx: f32 = sy.mul_add(e1z[item_index], -(sz * e1y[item_index]));
1072 let qy: f32 = sz.mul_add(e1x[item_index], -(sx * e1z[item_index]));
1073 let qz: f32 = sx.mul_add(e1y[item_index], -(sy * e1x[item_index]));
1074 let sv: f32 = dx.mul_add(qx, dy.mul_add(qy, dz * qz)) * sign;
1075 let node_style_primary: f32 = e2x[item_index]
1076 .mul_add(qx, e2y[item_index].mul_add(qy, e2z[item_index] * qz))
1077 * sign;
1078 let uv_ok: bool = su >= 0.0 && sv >= 0.0 && (su + sv) <= ad;
1079 let t_ok: bool = node_style_primary > t_min * ad && node_style_primary < t_max * ad;
1080 if ad > 1e-6 && uv_ok && t_ok {
1081 return tri_indices[item_index];
1082 }
1083 }
1084 u32::MAX
1085 }
1086 }
1087
1088 #[inline(always)]
1099 pub(crate) fn intersect_closest_4(
1100 &self,
1101 orig: [f32; 3],
1102 dir: [f32; 3],
1103 t_min: f32,
1104 best_t: &mut f32,
1105 best_idx: &mut usize,
1106 ) {
1107 let Self {
1108 v0x,
1109 v0y,
1110 v0z,
1111 e1x,
1112 e1y,
1113 e1z,
1114 e2x,
1115 e2y,
1116 e2z,
1117 tri_indices,
1118 } = self;
1119 let cur_max: f32 = *best_t;
1120 #[cfg(target_arch = "aarch64")]
1121 {
1122 let mut ad_arr: [f32; 4] = [0.0; 4];
1123 let mut st_arr: [f32; 4] = [0.0; 4];
1124 let mask: u32 = unsafe {
1126 let ox: core::arch::aarch64::float32x4_t =
1127 core::arch::aarch64::vdupq_n_f32(orig[0]);
1128 let oy: core::arch::aarch64::float32x4_t =
1129 core::arch::aarch64::vdupq_n_f32(orig[1]);
1130 let oz: core::arch::aarch64::float32x4_t =
1131 core::arch::aarch64::vdupq_n_f32(orig[2]);
1132 let dx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(dir[0]);
1133 let dy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(dir[1]);
1134 let dz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(dir[2]);
1135 let ve2x: core::arch::aarch64::float32x4_t =
1136 core::arch::aarch64::vld1q_f32(e2x.as_ptr());
1137 let ve2y: core::arch::aarch64::float32x4_t =
1138 core::arch::aarch64::vld1q_f32(e2y.as_ptr());
1139 let ve2z: core::arch::aarch64::float32x4_t =
1140 core::arch::aarch64::vld1q_f32(e2z.as_ptr());
1141 let hx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
1142 core::arch::aarch64::vmulq_f32(dy, ve2z),
1143 dz,
1144 ve2y,
1145 );
1146 let hy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
1147 core::arch::aarch64::vmulq_f32(dz, ve2x),
1148 dx,
1149 ve2z,
1150 );
1151 let hz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
1152 core::arch::aarch64::vmulq_f32(dx, ve2y),
1153 dy,
1154 ve2x,
1155 );
1156 let ve1x: core::arch::aarch64::float32x4_t =
1157 core::arch::aarch64::vld1q_f32(e1x.as_ptr());
1158 let ve1y: core::arch::aarch64::float32x4_t =
1159 core::arch::aarch64::vld1q_f32(e1y.as_ptr());
1160 let ve1z: core::arch::aarch64::float32x4_t =
1161 core::arch::aarch64::vld1q_f32(e1z.as_ptr());
1162 let det: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
1163 core::arch::aarch64::vfmaq_f32(
1164 core::arch::aarch64::vmulq_f32(ve1z, hz),
1165 ve1y,
1166 hy,
1167 ),
1168 ve1x,
1169 hx,
1170 );
1171 let sign_bits: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
1172 core::arch::aarch64::vreinterpretq_u32_f32(det),
1173 core::arch::aarch64::vdupq_n_u32(0x8000_0000),
1174 );
1175 let ad: core::arch::aarch64::float32x4_t = core::arch::aarch64::vabsq_f32(det);
1176 let sx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vsubq_f32(
1177 ox,
1178 core::arch::aarch64::vld1q_f32(v0x.as_ptr()),
1179 );
1180 let sy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vsubq_f32(
1181 oy,
1182 core::arch::aarch64::vld1q_f32(v0y.as_ptr()),
1183 );
1184 let sz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vsubq_f32(
1185 oz,
1186 core::arch::aarch64::vld1q_f32(v0z.as_ptr()),
1187 );
1188 let u_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
1189 core::arch::aarch64::vfmaq_f32(core::arch::aarch64::vmulq_f32(sz, hz), sy, hy),
1190 sx,
1191 hx,
1192 );
1193 let su: core::arch::aarch64::float32x4_t =
1194 core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
1195 core::arch::aarch64::vreinterpretq_u32_f32(u_raw),
1196 sign_bits,
1197 ));
1198 let qx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
1199 core::arch::aarch64::vmulq_f32(sy, ve1z),
1200 sz,
1201 ve1y,
1202 );
1203 let qy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
1204 core::arch::aarch64::vmulq_f32(sz, ve1x),
1205 sx,
1206 ve1z,
1207 );
1208 let qz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
1209 core::arch::aarch64::vmulq_f32(sx, ve1y),
1210 sy,
1211 ve1x,
1212 );
1213 let v_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
1214 core::arch::aarch64::vfmaq_f32(core::arch::aarch64::vmulq_f32(dz, qz), dy, qy),
1215 dx,
1216 qx,
1217 );
1218 let sv: core::arch::aarch64::float32x4_t =
1219 core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
1220 core::arch::aarch64::vreinterpretq_u32_f32(v_raw),
1221 sign_bits,
1222 ));
1223 let t_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
1224 core::arch::aarch64::vfmaq_f32(
1225 core::arch::aarch64::vmulq_f32(ve2z, qz),
1226 ve2y,
1227 qy,
1228 ),
1229 ve2x,
1230 qx,
1231 );
1232 let node_style: core::arch::aarch64::float32x4_t =
1233 core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
1234 core::arch::aarch64::vreinterpretq_u32_f32(t_raw),
1235 sign_bits,
1236 ));
1237 let zero: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(0.0);
1238 let m0: core::arch::aarch64::uint32x4_t =
1239 core::arch::aarch64::vcgtq_f32(ad, core::arch::aarch64::vdupq_n_f32(1e-6));
1240 let m1: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgeq_f32(su, zero);
1241 let m2: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgeq_f32(sv, zero);
1242 let m3: core::arch::aarch64::uint32x4_t =
1243 core::arch::aarch64::vcleq_f32(core::arch::aarch64::vaddq_f32(su, sv), ad);
1244 let m4: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgtq_f32(
1245 node_style,
1246 core::arch::aarch64::vmulq_f32(core::arch::aarch64::vdupq_n_f32(t_min), ad),
1247 );
1248 let m5: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcltq_f32(
1249 node_style,
1250 core::arch::aarch64::vmulq_f32(core::arch::aarch64::vdupq_n_f32(cur_max), ad),
1251 );
1252 let hit_v: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
1253 core::arch::aarch64::vandq_u32(
1254 core::arch::aarch64::vandq_u32(m0, m1),
1255 core::arch::aarch64::vandq_u32(m2, m3),
1256 ),
1257 core::arch::aarch64::vandq_u32(m4, m5),
1258 );
1259 let lane_bits: [u32; 4] = [1, 2, 4, 8];
1260 let hmask: u32 = core::arch::aarch64::vaddvq_u32(core::arch::aarch64::vandq_u32(
1261 hit_v,
1262 core::arch::aarch64::vld1q_u32(lane_bits.as_ptr()),
1263 ));
1264 if hmask != 0 {
1265 core::arch::aarch64::vst1q_f32(ad_arr.as_mut_ptr(), ad);
1266 core::arch::aarch64::vst1q_f32(st_arr.as_mut_ptr(), node_style);
1267 }
1268 hmask
1269 };
1270 let mut rem: u32 = mask;
1271 while rem != 0 {
1272 let item_index: usize = rem.trailing_zeros() as usize;
1273 rem &= rem - 1;
1274 let blend_factor: f32 = st_arr[item_index] / ad_arr[item_index];
1275 if blend_factor < *best_t {
1276 *best_t = blend_factor;
1277 *best_idx = tri_indices[item_index] as usize;
1278 }
1279 }
1280 }
1281 #[cfg(target_arch = "x86_64")]
1282 {
1283 let mut ad_arr: [f32; 4] = [0.0; 4];
1284 let mut st_arr: [f32; 4] = [0.0; 4];
1285 let mask: i32 = unsafe {
1287 let ox: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(orig[0]);
1288 let oy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(orig[1]);
1289 let oz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(orig[2]);
1290 let dx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(dir[0]);
1291 let dy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(dir[1]);
1292 let dz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(dir[2]);
1293 let ve2x: core::arch::x86_64::__m128 =
1294 core::arch::x86_64::_mm_load_ps(e2x.as_ptr());
1295 let ve2y: core::arch::x86_64::__m128 =
1296 core::arch::x86_64::_mm_load_ps(e2y.as_ptr());
1297 let ve2z: core::arch::x86_64::__m128 =
1298 core::arch::x86_64::_mm_load_ps(e2z.as_ptr());
1299 let hx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
1300 core::arch::x86_64::_mm_mul_ps(dy, ve2z),
1301 core::arch::x86_64::_mm_mul_ps(dz, ve2y),
1302 );
1303 let hy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
1304 core::arch::x86_64::_mm_mul_ps(dz, ve2x),
1305 core::arch::x86_64::_mm_mul_ps(dx, ve2z),
1306 );
1307 let hz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
1308 core::arch::x86_64::_mm_mul_ps(dx, ve2y),
1309 core::arch::x86_64::_mm_mul_ps(dy, ve2x),
1310 );
1311 let ve1x: core::arch::x86_64::__m128 =
1312 core::arch::x86_64::_mm_load_ps(e1x.as_ptr());
1313 let ve1y: core::arch::x86_64::__m128 =
1314 core::arch::x86_64::_mm_load_ps(e1y.as_ptr());
1315 let ve1z: core::arch::x86_64::__m128 =
1316 core::arch::x86_64::_mm_load_ps(e1z.as_ptr());
1317 let det: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
1318 core::arch::x86_64::_mm_add_ps(
1319 core::arch::x86_64::_mm_mul_ps(ve1x, hx),
1320 core::arch::x86_64::_mm_mul_ps(ve1y, hy),
1321 ),
1322 core::arch::x86_64::_mm_mul_ps(ve1z, hz),
1323 );
1324 let sign_mask: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(-0.0);
1325 let sign_bits: core::arch::x86_64::__m128 =
1326 core::arch::x86_64::_mm_and_ps(det, sign_mask);
1327 let ad: core::arch::x86_64::__m128 =
1328 core::arch::x86_64::_mm_andnot_ps(sign_mask, det);
1329 let sx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
1330 ox,
1331 core::arch::x86_64::_mm_load_ps(v0x.as_ptr()),
1332 );
1333 let sy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
1334 oy,
1335 core::arch::x86_64::_mm_load_ps(v0y.as_ptr()),
1336 );
1337 let sz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
1338 oz,
1339 core::arch::x86_64::_mm_load_ps(v0z.as_ptr()),
1340 );
1341 let u_raw: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
1342 core::arch::x86_64::_mm_add_ps(
1343 core::arch::x86_64::_mm_mul_ps(sx, hx),
1344 core::arch::x86_64::_mm_mul_ps(sy, hy),
1345 ),
1346 core::arch::x86_64::_mm_mul_ps(sz, hz),
1347 );
1348 let su: core::arch::x86_64::__m128 =
1349 core::arch::x86_64::_mm_xor_ps(u_raw, sign_bits);
1350 let qx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
1351 core::arch::x86_64::_mm_mul_ps(sy, ve1z),
1352 core::arch::x86_64::_mm_mul_ps(sz, ve1y),
1353 );
1354 let qy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
1355 core::arch::x86_64::_mm_mul_ps(sz, ve1x),
1356 core::arch::x86_64::_mm_mul_ps(sx, ve1z),
1357 );
1358 let qz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
1359 core::arch::x86_64::_mm_mul_ps(sx, ve1y),
1360 core::arch::x86_64::_mm_mul_ps(sy, ve1x),
1361 );
1362 let v_raw: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
1363 core::arch::x86_64::_mm_add_ps(
1364 core::arch::x86_64::_mm_mul_ps(dx, qx),
1365 core::arch::x86_64::_mm_mul_ps(dy, qy),
1366 ),
1367 core::arch::x86_64::_mm_mul_ps(dz, qz),
1368 );
1369 let sv: core::arch::x86_64::__m128 =
1370 core::arch::x86_64::_mm_xor_ps(v_raw, sign_bits);
1371 let t_raw: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
1372 core::arch::x86_64::_mm_add_ps(
1373 core::arch::x86_64::_mm_mul_ps(ve2x, qx),
1374 core::arch::x86_64::_mm_mul_ps(ve2y, qy),
1375 ),
1376 core::arch::x86_64::_mm_mul_ps(ve2z, qz),
1377 );
1378 let active_style: core::arch::x86_64::__m128 =
1379 core::arch::x86_64::_mm_xor_ps(t_raw, sign_bits);
1380 let zero: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_setzero_ps();
1381 let m0: core::arch::x86_64::__m128 =
1382 core::arch::x86_64::_mm_cmpgt_ps(ad, core::arch::x86_64::_mm_set1_ps(1e-6));
1383 let m1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmpge_ps(su, zero);
1384 let m2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmpge_ps(sv, zero);
1385 let m3: core::arch::x86_64::__m128 =
1386 core::arch::x86_64::_mm_cmple_ps(core::arch::x86_64::_mm_add_ps(su, sv), ad);
1387 let m4: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmpgt_ps(
1388 active_style,
1389 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(t_min), ad),
1390 );
1391 let m5: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmplt_ps(
1392 active_style,
1393 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(cur_max), ad),
1394 );
1395 let hit_v: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_and_ps(
1396 core::arch::x86_64::_mm_and_ps(
1397 core::arch::x86_64::_mm_and_ps(m0, m1),
1398 core::arch::x86_64::_mm_and_ps(m2, m3),
1399 ),
1400 core::arch::x86_64::_mm_and_ps(m4, m5),
1401 );
1402 let hmask: i32 = core::arch::x86_64::_mm_movemask_ps(hit_v);
1403 if hmask != 0 {
1404 core::arch::x86_64::_mm_storeu_ps(ad_arr.as_mut_ptr(), ad);
1405 core::arch::x86_64::_mm_storeu_ps(st_arr.as_mut_ptr(), active_style);
1406 }
1407 hmask
1408 };
1409 let mut rem: u32 = mask as u32;
1410 while rem != 0 {
1411 let outer_index: usize = rem.trailing_zeros() as usize;
1412 rem &= rem - 1;
1413 let time_step: f32 = st_arr[outer_index] / ad_arr[outer_index];
1414 if time_step < *best_t {
1415 *best_t = time_step;
1416 *best_idx = tri_indices[outer_index] as usize;
1417 }
1418 }
1419 }
1420 #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
1421 {
1422 let mut ad_arr: [f32; 4] = [0.0; 4];
1423 let mut st_arr: [f32; 4] = [0.0; 4];
1424 let mask: u32 = unsafe {
1426 let ox: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(orig[0]);
1427 let oy: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(orig[1]);
1428 let oz: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(orig[2]);
1429 let dx: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(dir[0]);
1430 let dy: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(dir[1]);
1431 let dz: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(dir[2]);
1432 let ve2x: core::arch::wasm32::v128 =
1433 core::arch::wasm32::v128_load(e2x.as_ptr().cast());
1434 let ve2y: core::arch::wasm32::v128 =
1435 core::arch::wasm32::v128_load(e2y.as_ptr().cast());
1436 let ve2z: core::arch::wasm32::v128 =
1437 core::arch::wasm32::v128_load(e2z.as_ptr().cast());
1438 let hx: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
1439 core::arch::wasm32::f32x4_mul(dy, ve2z),
1440 core::arch::wasm32::f32x4_mul(dz, ve2y),
1441 );
1442 let hy: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
1443 core::arch::wasm32::f32x4_mul(dz, ve2x),
1444 core::arch::wasm32::f32x4_mul(dx, ve2z),
1445 );
1446 let hz: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
1447 core::arch::wasm32::f32x4_mul(dx, ve2y),
1448 core::arch::wasm32::f32x4_mul(dy, ve2x),
1449 );
1450 let ve1x: core::arch::wasm32::v128 =
1451 core::arch::wasm32::v128_load(e1x.as_ptr().cast());
1452 let ve1y: core::arch::wasm32::v128 =
1453 core::arch::wasm32::v128_load(e1y.as_ptr().cast());
1454 let ve1z: core::arch::wasm32::v128 =
1455 core::arch::wasm32::v128_load(e1z.as_ptr().cast());
1456 let det: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
1457 core::arch::wasm32::f32x4_add(
1458 core::arch::wasm32::f32x4_mul(ve1x, hx),
1459 core::arch::wasm32::f32x4_mul(ve1y, hy),
1460 ),
1461 core::arch::wasm32::f32x4_mul(ve1z, hz),
1462 );
1463 let sign_bits: core::arch::wasm32::v128 =
1464 core::arch::wasm32::v128_and(det, core::arch::wasm32::u32x4_splat(0x8000_0000));
1465 let ad: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_abs(det);
1466 let sx: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
1467 ox,
1468 core::arch::wasm32::v128_load(v0x.as_ptr().cast()),
1469 );
1470 let sy: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
1471 oy,
1472 core::arch::wasm32::v128_load(v0y.as_ptr().cast()),
1473 );
1474 let sz: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
1475 oz,
1476 core::arch::wasm32::v128_load(v0z.as_ptr().cast()),
1477 );
1478 let u_raw: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
1479 core::arch::wasm32::f32x4_add(
1480 core::arch::wasm32::f32x4_mul(sx, hx),
1481 core::arch::wasm32::f32x4_mul(sy, hy),
1482 ),
1483 core::arch::wasm32::f32x4_mul(sz, hz),
1484 );
1485 let su: core::arch::wasm32::v128 = core::arch::wasm32::v128_xor(u_raw, sign_bits);
1486 let qx: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
1487 core::arch::wasm32::f32x4_mul(sy, ve1z),
1488 core::arch::wasm32::f32x4_mul(sz, ve1y),
1489 );
1490 let qy: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
1491 core::arch::wasm32::f32x4_mul(sz, ve1x),
1492 core::arch::wasm32::f32x4_mul(sx, ve1z),
1493 );
1494 let qz: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_sub(
1495 core::arch::wasm32::f32x4_mul(sx, ve1y),
1496 core::arch::wasm32::f32x4_mul(sy, ve1x),
1497 );
1498 let v_raw: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
1499 core::arch::wasm32::f32x4_add(
1500 core::arch::wasm32::f32x4_mul(dx, qx),
1501 core::arch::wasm32::f32x4_mul(dy, qy),
1502 ),
1503 core::arch::wasm32::f32x4_mul(dz, qz),
1504 );
1505 let sv: core::arch::wasm32::v128 = core::arch::wasm32::v128_xor(v_raw, sign_bits);
1506 let t_raw: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
1507 core::arch::wasm32::f32x4_add(
1508 core::arch::wasm32::f32x4_mul(ve2x, qx),
1509 core::arch::wasm32::f32x4_mul(ve2y, qy),
1510 ),
1511 core::arch::wasm32::f32x4_mul(ve2z, qz),
1512 );
1513 let state_val: core::arch::wasm32::v128 =
1514 core::arch::wasm32::v128_xor(t_raw, sign_bits);
1515 let zero: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(0.0);
1516 let m0: core::arch::wasm32::v128 =
1517 core::arch::wasm32::f32x4_gt(ad, core::arch::wasm32::f32x4_splat(1e-6));
1518 let m1: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_ge(su, zero);
1519 let m2: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_ge(sv, zero);
1520 let m3: core::arch::wasm32::v128 =
1521 core::arch::wasm32::f32x4_le(core::arch::wasm32::f32x4_add(su, sv), ad);
1522 let m4: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_gt(
1523 state_val,
1524 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(t_min), ad),
1525 );
1526 let m5: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_lt(
1527 state_val,
1528 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(cur_max), ad),
1529 );
1530 let hit_v: core::arch::wasm32::v128 = core::arch::wasm32::v128_and(
1531 core::arch::wasm32::v128_and(
1532 core::arch::wasm32::v128_and(m0, m1),
1533 core::arch::wasm32::v128_and(m2, m3),
1534 ),
1535 core::arch::wasm32::v128_and(m4, m5),
1536 );
1537 let hmask: u32 = core::arch::wasm32::i32x4_bitmask(hit_v) as u32;
1538 if hmask != 0 {
1539 core::arch::wasm32::v128_store(ad_arr.as_mut_ptr().cast(), ad);
1540 core::arch::wasm32::v128_store(st_arr.as_mut_ptr().cast(), state_val);
1541 }
1542 hmask
1543 };
1544 let mut rem: u32 = mask;
1545 while rem != 0 {
1546 let step_index: usize = rem.trailing_zeros() as usize;
1547 rem &= rem - 1;
1548 let token_val: f32 = st_arr[step_index] / ad_arr[step_index];
1549 if token_val < *best_t {
1550 *best_t = token_val;
1551 *best_idx = tri_indices[step_index] as usize;
1552 }
1553 }
1554 }
1555 #[cfg(not(target_arch = "aarch64"))]
1556 #[cfg(not(target_arch = "x86_64"))]
1557 #[cfg(not(all(target_arch = "wasm32", target_feature = "simd128")))]
1558 {
1559 let ox: f32 = orig[0];
1560 let oy: f32 = orig[1];
1561 let oz: f32 = orig[2];
1562 let dx: f32 = dir[0];
1563 let dy: f32 = dir[1];
1564 let dz: f32 = dir[2];
1565 let mut ad_arr: [f32; 4] = [0.0; 4];
1566 let mut st_arr: [f32; 4] = [0.0; 4];
1567 let mut hits: [bool; 4] = [false; 4];
1568 for cursor_index in 0..4 {
1569 let hx: f32 = dy.mul_add(e2z[cursor_index], -(dz * e2y[cursor_index]));
1570 let hy: f32 = dz.mul_add(e2x[cursor_index], -(dx * e2z[cursor_index]));
1571 let hz: f32 = dx.mul_add(e2y[cursor_index], -(dy * e2x[cursor_index]));
1572 let det: f32 = e1x[cursor_index]
1573 .mul_add(hx, e1y[cursor_index].mul_add(hy, e1z[cursor_index] * hz));
1574 let sign: f32 = 1.0_f32.copysign(det);
1575 let ad: f32 = det.abs();
1576 let sx: f32 = ox - v0x[cursor_index];
1577 let sy: f32 = oy - v0y[cursor_index];
1578 let sz: f32 = oz - v0z[cursor_index];
1579 let su: f32 = sx.mul_add(hx, sy.mul_add(hy, sz * hz)) * sign;
1580 let qx: f32 = sy.mul_add(e1z[cursor_index], -(sz * e1y[cursor_index]));
1581 let qy: f32 = sz.mul_add(e1x[cursor_index], -(sx * e1z[cursor_index]));
1582 let qz: f32 = sx.mul_add(e1y[cursor_index], -(sy * e1x[cursor_index]));
1583 let sv: f32 = dx.mul_add(qx, dy.mul_add(qy, dz * qz)) * sign;
1584 let node_style_primary: f32 = e2x[cursor_index]
1585 .mul_add(qx, e2y[cursor_index].mul_add(qy, e2z[cursor_index] * qz))
1586 * sign;
1587 ad_arr[cursor_index] = ad;
1588 st_arr[cursor_index] = node_style_primary;
1589 let uv_ok: bool = su >= 0.0 && sv >= 0.0 && (su + sv) <= ad;
1590 let t_ok: bool =
1591 node_style_primary > t_min * ad && node_style_primary < cur_max * ad;
1592 hits[cursor_index] = ad > 1e-6 && uv_ok && t_ok;
1593 }
1594 for item_index_primary in 0..4 {
1595 if hits[item_index_primary] {
1596 let target_val: f32 = st_arr[item_index_primary] / ad_arr[item_index_primary];
1597 if target_val < *best_t {
1598 *best_t = target_val;
1599 *best_idx = tri_indices[item_index_primary] as usize;
1600 }
1601 }
1602 }
1603 }
1604 }
1605}
1606
1607impl BvhFlatNode {
1609 #[inline(always)]
1615 #[must_use]
1616 pub(crate) const fn empty() -> Self {
1617 Self {
1618 min: [0.0; 3],
1619 child_or_first: 0,
1620 max: [0.0; 3],
1621 tri_count: 0,
1622 }
1623 }
1624
1625 #[inline(always)]
1640 #[must_use]
1641 pub(crate) fn intersect_box_fma(
1642 min: [f32; 3],
1643 max: [f32; 3],
1644 inv: [f32; 3],
1645 neg_orig_inv: [f32; 3],
1646 t_min: f32,
1647 t_max: f32,
1648 ) -> f32 {
1649 let tx1: f32 = min[0].mul_add(inv[0], neg_orig_inv[0]);
1650 let tx2: f32 = max[0].mul_add(inv[0], neg_orig_inv[0]);
1651 let mut tmin: f32 = t_min.max(tx1.min(tx2));
1652 let mut tmax: f32 = t_max.min(tx1.max(tx2));
1653 let ty1: f32 = min[1].mul_add(inv[1], neg_orig_inv[1]);
1654 let ty2: f32 = max[1].mul_add(inv[1], neg_orig_inv[1]);
1655 tmin = tmin.max(ty1.min(ty2));
1656 tmax = tmax.min(ty1.max(ty2));
1657 let tz1: f32 = min[2].mul_add(inv[2], neg_orig_inv[2]);
1658 let tz2: f32 = max[2].mul_add(inv[2], neg_orig_inv[2]);
1659 tmin = tmin.max(tz1.min(tz2));
1660 tmax = tmax.min(tz1.max(tz2));
1661 if tmax >= tmin && tmax >= 0.0 {
1662 tmin
1663 } else {
1664 f32::INFINITY
1665 }
1666 }
1667}
1668
1669impl Bvh4Node {
1671 #[inline(always)]
1677 #[must_use]
1678 pub(crate) const fn empty() -> Self {
1679 Self {
1680 min_x: [f32::INFINITY; 4],
1681 min_y: [f32::INFINITY; 4],
1682 min_z: [f32::INFINITY; 4],
1683 max_x: [f32::INFINITY; 4],
1684 max_y: [f32::INFINITY; 4],
1685 max_z: [f32::INFINITY; 4],
1686 child: [u32::MAX; 4],
1687 leaf_count: [0; 4],
1688 valid_count: 0,
1689 _pad: [0; 11],
1690 }
1691 }
1692
1693 #[inline(always)]
1707 #[must_use]
1708 pub(crate) fn intersect_4(
1709 &self,
1710 inv: [f32; 3],
1711 neg_orig_inv: [f32; 3],
1712 t_min: f32,
1713 t_max: f32,
1714 ) -> ([f32; 4], u32) {
1715 let Self {
1716 min_x,
1717 min_y,
1718 min_z,
1719 max_x,
1720 max_y,
1721 max_z,
1722 valid_count,
1723 ..
1724 } = self;
1725 let valid_mask: u32 = (1_u32 << (*valid_count as u32)) - 1;
1726 #[cfg(target_arch = "aarch64")]
1727 {
1728 let mut t_out: [f32; 4] = [f32::INFINITY; 4];
1729 let mask: u32 = unsafe {
1731 let ix: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(inv[0]);
1732 let iy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(inv[1]);
1733 let iz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(inv[2]);
1734 let nx: core::arch::aarch64::float32x4_t =
1735 core::arch::aarch64::vdupq_n_f32(neg_orig_inv[0]);
1736 let ny: core::arch::aarch64::float32x4_t =
1737 core::arch::aarch64::vdupq_n_f32(neg_orig_inv[1]);
1738 let nz: core::arch::aarch64::float32x4_t =
1739 core::arch::aarch64::vdupq_n_f32(neg_orig_inv[2]);
1740 let tx1: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
1741 nx,
1742 core::arch::aarch64::vld1q_f32(min_x.as_ptr()),
1743 ix,
1744 );
1745 let tx2: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
1746 nx,
1747 core::arch::aarch64::vld1q_f32(max_x.as_ptr()),
1748 ix,
1749 );
1750 let mut vmin: core::arch::aarch64::float32x4_t = core::arch::aarch64::vmaxq_f32(
1751 core::arch::aarch64::vdupq_n_f32(t_min),
1752 core::arch::aarch64::vminq_f32(tx1, tx2),
1753 );
1754 let mut vmax: core::arch::aarch64::float32x4_t = core::arch::aarch64::vminq_f32(
1755 core::arch::aarch64::vdupq_n_f32(t_max),
1756 core::arch::aarch64::vmaxq_f32(tx1, tx2),
1757 );
1758 let ty1: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
1759 ny,
1760 core::arch::aarch64::vld1q_f32(min_y.as_ptr()),
1761 iy,
1762 );
1763 let ty2: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
1764 ny,
1765 core::arch::aarch64::vld1q_f32(max_y.as_ptr()),
1766 iy,
1767 );
1768 vmin =
1769 core::arch::aarch64::vmaxq_f32(vmin, core::arch::aarch64::vminq_f32(ty1, ty2));
1770 vmax =
1771 core::arch::aarch64::vminq_f32(vmax, core::arch::aarch64::vmaxq_f32(ty1, ty2));
1772 let tz1: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
1773 nz,
1774 core::arch::aarch64::vld1q_f32(min_z.as_ptr()),
1775 iz,
1776 );
1777 let tz2: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
1778 nz,
1779 core::arch::aarch64::vld1q_f32(max_z.as_ptr()),
1780 iz,
1781 );
1782 vmin =
1783 core::arch::aarch64::vmaxq_f32(vmin, core::arch::aarch64::vminq_f32(tz1, tz2));
1784 vmax =
1785 core::arch::aarch64::vminq_f32(vmax, core::arch::aarch64::vmaxq_f32(tz1, tz2));
1786 core::arch::aarch64::vst1q_f32(t_out.as_mut_ptr(), vmin);
1787 let hit_v: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
1788 core::arch::aarch64::vcgeq_f32(vmax, vmin),
1789 core::arch::aarch64::vcgeq_f32(vmax, core::arch::aarch64::vdupq_n_f32(0.0)),
1790 );
1791 let lane_bits: [u32; 4] = [1, 2, 4, 8];
1792 core::arch::aarch64::vaddvq_u32(core::arch::aarch64::vandq_u32(
1793 hit_v,
1794 core::arch::aarch64::vld1q_u32(lane_bits.as_ptr()),
1795 ))
1796 };
1797 (t_out, mask & valid_mask)
1798 }
1799 #[cfg(target_arch = "x86_64")]
1800 {
1801 let mut t_out: [f32; 4] = [f32::INFINITY; 4];
1802 let mask: i32 = unsafe {
1804 let ix: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(inv[0]);
1805 let iy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(inv[1]);
1806 let iz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(inv[2]);
1807 let nx: core::arch::x86_64::__m128 =
1808 core::arch::x86_64::_mm_set1_ps(neg_orig_inv[0]);
1809 let ny: core::arch::x86_64::__m128 =
1810 core::arch::x86_64::_mm_set1_ps(neg_orig_inv[1]);
1811 let nz: core::arch::x86_64::__m128 =
1812 core::arch::x86_64::_mm_set1_ps(neg_orig_inv[2]);
1813 let tx1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
1814 core::arch::x86_64::_mm_mul_ps(
1815 core::arch::x86_64::_mm_load_ps(min_x.as_ptr()),
1816 ix,
1817 ),
1818 nx,
1819 );
1820 let tx2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
1821 core::arch::x86_64::_mm_mul_ps(
1822 core::arch::x86_64::_mm_load_ps(max_x.as_ptr()),
1823 ix,
1824 ),
1825 nx,
1826 );
1827 let mut vmin: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_max_ps(
1828 core::arch::x86_64::_mm_set1_ps(t_min),
1829 core::arch::x86_64::_mm_min_ps(tx1, tx2),
1830 );
1831 let mut vmax: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_min_ps(
1832 core::arch::x86_64::_mm_set1_ps(t_max),
1833 core::arch::x86_64::_mm_max_ps(tx1, tx2),
1834 );
1835 let ty1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
1836 core::arch::x86_64::_mm_mul_ps(
1837 core::arch::x86_64::_mm_load_ps(min_y.as_ptr()),
1838 iy,
1839 ),
1840 ny,
1841 );
1842 let ty2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
1843 core::arch::x86_64::_mm_mul_ps(
1844 core::arch::x86_64::_mm_load_ps(max_y.as_ptr()),
1845 iy,
1846 ),
1847 ny,
1848 );
1849 vmin =
1850 core::arch::x86_64::_mm_max_ps(vmin, core::arch::x86_64::_mm_min_ps(ty1, ty2));
1851 vmax =
1852 core::arch::x86_64::_mm_min_ps(vmax, core::arch::x86_64::_mm_max_ps(ty1, ty2));
1853 let tz1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
1854 core::arch::x86_64::_mm_mul_ps(
1855 core::arch::x86_64::_mm_load_ps(min_z.as_ptr()),
1856 iz,
1857 ),
1858 nz,
1859 );
1860 let tz2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
1861 core::arch::x86_64::_mm_mul_ps(
1862 core::arch::x86_64::_mm_load_ps(max_z.as_ptr()),
1863 iz,
1864 ),
1865 nz,
1866 );
1867 vmin =
1868 core::arch::x86_64::_mm_max_ps(vmin, core::arch::x86_64::_mm_min_ps(tz1, tz2));
1869 vmax =
1870 core::arch::x86_64::_mm_min_ps(vmax, core::arch::x86_64::_mm_max_ps(tz1, tz2));
1871 core::arch::x86_64::_mm_storeu_ps(t_out.as_mut_ptr(), vmin);
1872 let hit_v: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_and_ps(
1873 core::arch::x86_64::_mm_cmpge_ps(vmax, vmin),
1874 core::arch::x86_64::_mm_cmpge_ps(vmax, core::arch::x86_64::_mm_setzero_ps()),
1875 );
1876 core::arch::x86_64::_mm_movemask_ps(hit_v)
1877 };
1878 (t_out, (mask as u32) & valid_mask)
1879 }
1880 #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
1881 {
1882 let mut t_out: [f32; 4] = [f32::INFINITY; 4];
1883 let mask: u32 = unsafe {
1885 let ix: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(inv[0]);
1886 let iy: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(inv[1]);
1887 let iz: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(inv[2]);
1888 let nx: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(neg_orig_inv[0]);
1889 let ny: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(neg_orig_inv[1]);
1890 let nz: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(neg_orig_inv[2]);
1891 let tx1: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
1892 core::arch::wasm32::f32x4_mul(
1893 core::arch::wasm32::v128_load(min_x.as_ptr().cast()),
1894 ix,
1895 ),
1896 nx,
1897 );
1898 let tx2: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
1899 core::arch::wasm32::f32x4_mul(
1900 core::arch::wasm32::v128_load(max_x.as_ptr().cast()),
1901 ix,
1902 ),
1903 nx,
1904 );
1905 let mut vmin: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_pmax(
1906 core::arch::wasm32::f32x4_splat(t_min),
1907 core::arch::wasm32::f32x4_pmin(tx1, tx2),
1908 );
1909 let mut vmax: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_pmin(
1910 core::arch::wasm32::f32x4_splat(t_max),
1911 core::arch::wasm32::f32x4_pmax(tx1, tx2),
1912 );
1913 let ty1: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
1914 core::arch::wasm32::f32x4_mul(
1915 core::arch::wasm32::v128_load(min_y.as_ptr().cast()),
1916 iy,
1917 ),
1918 ny,
1919 );
1920 let ty2: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
1921 core::arch::wasm32::f32x4_mul(
1922 core::arch::wasm32::v128_load(max_y.as_ptr().cast()),
1923 iy,
1924 ),
1925 ny,
1926 );
1927 vmin =
1928 core::arch::wasm32::f32x4_pmax(vmin, core::arch::wasm32::f32x4_pmin(ty1, ty2));
1929 vmax =
1930 core::arch::wasm32::f32x4_pmin(vmax, core::arch::wasm32::f32x4_pmax(ty1, ty2));
1931 let tz1: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
1932 core::arch::wasm32::f32x4_mul(
1933 core::arch::wasm32::v128_load(min_z.as_ptr().cast()),
1934 iz,
1935 ),
1936 nz,
1937 );
1938 let tz2: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
1939 core::arch::wasm32::f32x4_mul(
1940 core::arch::wasm32::v128_load(max_z.as_ptr().cast()),
1941 iz,
1942 ),
1943 nz,
1944 );
1945 vmin =
1946 core::arch::wasm32::f32x4_pmax(vmin, core::arch::wasm32::f32x4_pmin(tz1, tz2));
1947 vmax =
1948 core::arch::wasm32::f32x4_pmin(vmax, core::arch::wasm32::f32x4_pmax(tz1, tz2));
1949 core::arch::wasm32::v128_store(t_out.as_mut_ptr().cast(), vmin);
1950 let hit_v: core::arch::wasm32::v128 = core::arch::wasm32::v128_and(
1951 core::arch::wasm32::f32x4_ge(vmax, vmin),
1952 core::arch::wasm32::f32x4_ge(vmax, core::arch::wasm32::f32x4_splat(0.0)),
1953 );
1954 core::arch::wasm32::i32x4_bitmask(hit_v) as u32
1955 };
1956 (t_out, mask & valid_mask)
1957 }
1958 #[cfg(not(target_arch = "aarch64"))]
1959 #[cfg(not(target_arch = "x86_64"))]
1960 #[cfg(not(all(target_arch = "wasm32", target_feature = "simd128")))]
1961 {
1962 let mut t_out: [f32; 4] = [f32::INFINITY; 4];
1963 let mut mask: u32 = 0;
1964 let count: usize = *valid_count as usize;
1965 for char_item in 0..count {
1966 let blend_factor: f32 = BvhFlatNode::intersect_box_fma(
1967 [min_x[char_item], min_y[char_item], min_z[char_item]],
1968 [max_x[char_item], max_y[char_item], max_z[char_item]],
1969 inv,
1970 neg_orig_inv,
1971 t_min,
1972 t_max,
1973 );
1974 t_out[char_item] = blend_factor;
1975 if blend_factor.is_finite() {
1976 mask |= 1_u32 << char_item;
1977 }
1978 }
1979 (t_out, mask & valid_mask)
1980 }
1981 }
1982
1983 #[inline(always)]
2001 #[must_use]
2002 pub(crate) fn intersect_bounds_packet4(
2003 bmin: [f32; 3],
2004 bmax: [f32; 3],
2005 pkt: &RayPacket4,
2006 neg_oi: &[[f32; 4]; 3],
2007 t_min: f32,
2008 t_max_4: [f32; 4],
2009 ) -> u8 {
2010 let bx0: f32 = bmin[0];
2011 let by0: f32 = bmin[1];
2012 let bz0: f32 = bmin[2];
2013 let bx1: f32 = bmax[0];
2014 let by1: f32 = bmax[1];
2015 let bz1: f32 = bmax[2];
2016 #[cfg(target_arch = "aarch64")]
2017 {
2018 unsafe {
2020 let ix: core::arch::aarch64::float32x4_t =
2021 core::arch::aarch64::vld1q_f32(pkt.ix.as_ptr());
2022 let iy: core::arch::aarch64::float32x4_t =
2023 core::arch::aarch64::vld1q_f32(pkt.iy.as_ptr());
2024 let iz: core::arch::aarch64::float32x4_t =
2025 core::arch::aarch64::vld1q_f32(pkt.iz.as_ptr());
2026 let nx: core::arch::aarch64::float32x4_t =
2027 core::arch::aarch64::vld1q_f32(neg_oi[0].as_ptr());
2028 let ny: core::arch::aarch64::float32x4_t =
2029 core::arch::aarch64::vld1q_f32(neg_oi[1].as_ptr());
2030 let nz: core::arch::aarch64::float32x4_t =
2031 core::arch::aarch64::vld1q_f32(neg_oi[2].as_ptr());
2032 let tx1: core::arch::aarch64::float32x4_t =
2033 core::arch::aarch64::vfmaq_f32(nx, core::arch::aarch64::vdupq_n_f32(bx0), ix);
2034 let tx2: core::arch::aarch64::float32x4_t =
2035 core::arch::aarch64::vfmaq_f32(nx, core::arch::aarch64::vdupq_n_f32(bx1), ix);
2036 let mut vmin: core::arch::aarch64::float32x4_t = core::arch::aarch64::vmaxq_f32(
2037 core::arch::aarch64::vdupq_n_f32(t_min),
2038 core::arch::aarch64::vminq_f32(tx1, tx2),
2039 );
2040 let mut vmax: core::arch::aarch64::float32x4_t = core::arch::aarch64::vminq_f32(
2041 core::arch::aarch64::vld1q_f32(t_max_4.as_ptr()),
2042 core::arch::aarch64::vmaxq_f32(tx1, tx2),
2043 );
2044 let ty1: core::arch::aarch64::float32x4_t =
2045 core::arch::aarch64::vfmaq_f32(ny, core::arch::aarch64::vdupq_n_f32(by0), iy);
2046 let ty2: core::arch::aarch64::float32x4_t =
2047 core::arch::aarch64::vfmaq_f32(ny, core::arch::aarch64::vdupq_n_f32(by1), iy);
2048 vmin =
2049 core::arch::aarch64::vmaxq_f32(vmin, core::arch::aarch64::vminq_f32(ty1, ty2));
2050 vmax =
2051 core::arch::aarch64::vminq_f32(vmax, core::arch::aarch64::vmaxq_f32(ty1, ty2));
2052 let tz1: core::arch::aarch64::float32x4_t =
2053 core::arch::aarch64::vfmaq_f32(nz, core::arch::aarch64::vdupq_n_f32(bz0), iz);
2054 let tz2: core::arch::aarch64::float32x4_t =
2055 core::arch::aarch64::vfmaq_f32(nz, core::arch::aarch64::vdupq_n_f32(bz1), iz);
2056 vmin =
2057 core::arch::aarch64::vmaxq_f32(vmin, core::arch::aarch64::vminq_f32(tz1, tz2));
2058 vmax =
2059 core::arch::aarch64::vminq_f32(vmax, core::arch::aarch64::vmaxq_f32(tz1, tz2));
2060 let hit_v: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
2061 core::arch::aarch64::vcgeq_f32(vmax, vmin),
2062 core::arch::aarch64::vcgeq_f32(vmax, core::arch::aarch64::vdupq_n_f32(0.0)),
2063 );
2064 let lane_bits: [u32; 4] = [1, 2, 4, 8];
2065 core::arch::aarch64::vaddvq_u32(core::arch::aarch64::vandq_u32(
2066 hit_v,
2067 core::arch::aarch64::vld1q_u32(lane_bits.as_ptr()),
2068 )) as u8
2069 }
2070 }
2071 #[cfg(target_arch = "x86_64")]
2072 {
2073 unsafe {
2075 let ix: core::arch::x86_64::__m128 =
2076 core::arch::x86_64::_mm_loadu_ps(pkt.ix.as_ptr());
2077 let iy: core::arch::x86_64::__m128 =
2078 core::arch::x86_64::_mm_loadu_ps(pkt.iy.as_ptr());
2079 let iz: core::arch::x86_64::__m128 =
2080 core::arch::x86_64::_mm_loadu_ps(pkt.iz.as_ptr());
2081 let nx: core::arch::x86_64::__m128 =
2082 core::arch::x86_64::_mm_loadu_ps(neg_oi[0].as_ptr());
2083 let ny: core::arch::x86_64::__m128 =
2084 core::arch::x86_64::_mm_loadu_ps(neg_oi[1].as_ptr());
2085 let nz: core::arch::x86_64::__m128 =
2086 core::arch::x86_64::_mm_loadu_ps(neg_oi[2].as_ptr());
2087 let tx1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
2088 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(bx0), ix),
2089 nx,
2090 );
2091 let tx2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
2092 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(bx1), ix),
2093 nx,
2094 );
2095 let mut vmin: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_max_ps(
2096 core::arch::x86_64::_mm_set1_ps(t_min),
2097 core::arch::x86_64::_mm_min_ps(tx1, tx2),
2098 );
2099 let mut vmax: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_min_ps(
2100 core::arch::x86_64::_mm_loadu_ps(t_max_4.as_ptr()),
2101 core::arch::x86_64::_mm_max_ps(tx1, tx2),
2102 );
2103 let ty1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
2104 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(by0), iy),
2105 ny,
2106 );
2107 let ty2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
2108 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(by1), iy),
2109 ny,
2110 );
2111 vmin =
2112 core::arch::x86_64::_mm_max_ps(vmin, core::arch::x86_64::_mm_min_ps(ty1, ty2));
2113 vmax =
2114 core::arch::x86_64::_mm_min_ps(vmax, core::arch::x86_64::_mm_max_ps(ty1, ty2));
2115 let tz1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
2116 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(bz0), iz),
2117 nz,
2118 );
2119 let tz2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
2120 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(bz1), iz),
2121 nz,
2122 );
2123 vmin =
2124 core::arch::x86_64::_mm_max_ps(vmin, core::arch::x86_64::_mm_min_ps(tz1, tz2));
2125 vmax =
2126 core::arch::x86_64::_mm_min_ps(vmax, core::arch::x86_64::_mm_max_ps(tz1, tz2));
2127 let hit_v: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_and_ps(
2128 core::arch::x86_64::_mm_cmpge_ps(vmax, vmin),
2129 core::arch::x86_64::_mm_cmpge_ps(vmax, core::arch::x86_64::_mm_setzero_ps()),
2130 );
2131 core::arch::x86_64::_mm_movemask_ps(hit_v) as u8
2132 }
2133 }
2134 #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
2135 {
2136 unsafe {
2138 let ix: core::arch::wasm32::v128 =
2139 core::arch::wasm32::v128_load(pkt.ix.as_ptr().cast());
2140 let iy: core::arch::wasm32::v128 =
2141 core::arch::wasm32::v128_load(pkt.iy.as_ptr().cast());
2142 let iz: core::arch::wasm32::v128 =
2143 core::arch::wasm32::v128_load(pkt.iz.as_ptr().cast());
2144 let nx: core::arch::wasm32::v128 =
2145 core::arch::wasm32::v128_load(neg_oi[0].as_ptr().cast());
2146 let ny: core::arch::wasm32::v128 =
2147 core::arch::wasm32::v128_load(neg_oi[1].as_ptr().cast());
2148 let nz: core::arch::wasm32::v128 =
2149 core::arch::wasm32::v128_load(neg_oi[2].as_ptr().cast());
2150 let tx1: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
2151 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(bx0), ix),
2152 nx,
2153 );
2154 let tx2: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
2155 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(bx1), ix),
2156 nx,
2157 );
2158 let mut vmin: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_pmax(
2159 core::arch::wasm32::f32x4_splat(t_min),
2160 core::arch::wasm32::f32x4_pmin(tx1, tx2),
2161 );
2162 let mut vmax: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_pmin(
2163 core::arch::wasm32::v128_load(t_max_4.as_ptr().cast()),
2164 core::arch::wasm32::f32x4_pmax(tx1, tx2),
2165 );
2166 let ty1: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
2167 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(by0), iy),
2168 ny,
2169 );
2170 let ty2: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
2171 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(by1), iy),
2172 ny,
2173 );
2174 vmin =
2175 core::arch::wasm32::f32x4_pmax(vmin, core::arch::wasm32::f32x4_pmin(ty1, ty2));
2176 vmax =
2177 core::arch::wasm32::f32x4_pmin(vmax, core::arch::wasm32::f32x4_pmax(ty1, ty2));
2178 let tz1: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
2179 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(bz0), iz),
2180 nz,
2181 );
2182 let tz2: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
2183 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(bz1), iz),
2184 nz,
2185 );
2186 vmin =
2187 core::arch::wasm32::f32x4_pmax(vmin, core::arch::wasm32::f32x4_pmin(tz1, tz2));
2188 vmax =
2189 core::arch::wasm32::f32x4_pmin(vmax, core::arch::wasm32::f32x4_pmax(tz1, tz2));
2190 let hit_v: core::arch::wasm32::v128 = core::arch::wasm32::v128_and(
2191 core::arch::wasm32::f32x4_ge(vmax, vmin),
2192 core::arch::wasm32::f32x4_ge(vmax, core::arch::wasm32::f32x4_splat(0.0)),
2193 );
2194 core::arch::wasm32::i32x4_bitmask(hit_v) as u8
2195 }
2196 }
2197 #[cfg(not(target_arch = "aarch64"))]
2198 #[cfg(not(target_arch = "x86_64"))]
2199 #[cfg(not(all(target_arch = "wasm32", target_feature = "simd128")))]
2200 {
2201 let mut mask: u8 = 0;
2202 for red_val in 0..4 {
2203 let blend_factor: f32 = BvhFlatNode::intersect_box_fma(
2204 [bx0, by0, bz0],
2205 [bx1, by1, bz1],
2206 [pkt.ix[red_val], pkt.iy[red_val], pkt.iz[red_val]],
2207 [neg_oi[0][red_val], neg_oi[1][red_val], neg_oi[2][red_val]],
2208 t_min,
2209 t_max_4[red_val],
2210 );
2211 if blend_factor.is_finite() {
2212 mask |= 1_u8 << red_val;
2213 }
2214 }
2215 mask
2216 }
2217 }
2218
2219 #[inline(always)]
2234 #[must_use]
2235 pub(crate) fn intersect_child_packet4(
2236 &self,
2237 lane: usize,
2238 pkt: &RayPacket4,
2239 neg_oi: &[[f32; 4]; 3],
2240 t_min: f32,
2241 t_max_4: [f32; 4],
2242 ) -> u8 {
2243 let Self {
2244 min_x,
2245 min_y,
2246 min_z,
2247 max_x,
2248 max_y,
2249 max_z,
2250 ..
2251 } = self;
2252 Self::intersect_bounds_packet4(
2253 [min_x[lane], min_y[lane], min_z[lane]],
2254 [max_x[lane], max_y[lane], max_z[lane]],
2255 pkt,
2256 neg_oi,
2257 t_min,
2258 t_max_4,
2259 )
2260 }
2261
2262 #[inline(always)]
2277 #[must_use]
2278 pub(crate) fn intersect_child_packet4_near(
2279 &self,
2280 lane: usize,
2281 pkt: &RayPacket4,
2282 neg_oi: &[[f32; 4]; 3],
2283 t_min: f32,
2284 t_max_4: [f32; 4],
2285 ) -> (u8, f32) {
2286 let Self {
2287 min_x,
2288 min_y,
2289 min_z,
2290 max_x,
2291 max_y,
2292 max_z,
2293 ..
2294 } = self;
2295 let bx0: f32 = min_x[lane];
2296 let by0: f32 = min_y[lane];
2297 let bz0: f32 = min_z[lane];
2298 let bx1: f32 = max_x[lane];
2299 let by1: f32 = max_y[lane];
2300 let bz1: f32 = max_z[lane];
2301 #[cfg(target_arch = "aarch64")]
2302 {
2303 unsafe {
2305 let ix: core::arch::aarch64::float32x4_t =
2306 core::arch::aarch64::vld1q_f32(pkt.ix.as_ptr());
2307 let iy: core::arch::aarch64::float32x4_t =
2308 core::arch::aarch64::vld1q_f32(pkt.iy.as_ptr());
2309 let iz: core::arch::aarch64::float32x4_t =
2310 core::arch::aarch64::vld1q_f32(pkt.iz.as_ptr());
2311 let nx: core::arch::aarch64::float32x4_t =
2312 core::arch::aarch64::vld1q_f32(neg_oi[0].as_ptr());
2313 let ny: core::arch::aarch64::float32x4_t =
2314 core::arch::aarch64::vld1q_f32(neg_oi[1].as_ptr());
2315 let nz: core::arch::aarch64::float32x4_t =
2316 core::arch::aarch64::vld1q_f32(neg_oi[2].as_ptr());
2317 let tx1: core::arch::aarch64::float32x4_t =
2318 core::arch::aarch64::vfmaq_f32(nx, core::arch::aarch64::vdupq_n_f32(bx0), ix);
2319 let tx2: core::arch::aarch64::float32x4_t =
2320 core::arch::aarch64::vfmaq_f32(nx, core::arch::aarch64::vdupq_n_f32(bx1), ix);
2321 let mut vmin: core::arch::aarch64::float32x4_t = core::arch::aarch64::vmaxq_f32(
2322 core::arch::aarch64::vdupq_n_f32(t_min),
2323 core::arch::aarch64::vminq_f32(tx1, tx2),
2324 );
2325 let mut vmax: core::arch::aarch64::float32x4_t = core::arch::aarch64::vminq_f32(
2326 core::arch::aarch64::vld1q_f32(t_max_4.as_ptr()),
2327 core::arch::aarch64::vmaxq_f32(tx1, tx2),
2328 );
2329 let ty1: core::arch::aarch64::float32x4_t =
2330 core::arch::aarch64::vfmaq_f32(ny, core::arch::aarch64::vdupq_n_f32(by0), iy);
2331 let ty2: core::arch::aarch64::float32x4_t =
2332 core::arch::aarch64::vfmaq_f32(ny, core::arch::aarch64::vdupq_n_f32(by1), iy);
2333 vmin =
2334 core::arch::aarch64::vmaxq_f32(vmin, core::arch::aarch64::vminq_f32(ty1, ty2));
2335 vmax =
2336 core::arch::aarch64::vminq_f32(vmax, core::arch::aarch64::vmaxq_f32(ty1, ty2));
2337 let tz1: core::arch::aarch64::float32x4_t =
2338 core::arch::aarch64::vfmaq_f32(nz, core::arch::aarch64::vdupq_n_f32(bz0), iz);
2339 let tz2: core::arch::aarch64::float32x4_t =
2340 core::arch::aarch64::vfmaq_f32(nz, core::arch::aarch64::vdupq_n_f32(bz1), iz);
2341 vmin =
2342 core::arch::aarch64::vmaxq_f32(vmin, core::arch::aarch64::vminq_f32(tz1, tz2));
2343 vmax =
2344 core::arch::aarch64::vminq_f32(vmax, core::arch::aarch64::vmaxq_f32(tz1, tz2));
2345 let hit_v: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
2346 core::arch::aarch64::vcgeq_f32(vmax, vmin),
2347 core::arch::aarch64::vcgeq_f32(vmax, core::arch::aarch64::vdupq_n_f32(0.0)),
2348 );
2349 let lane_bits: [u32; 4] = [1, 2, 4, 8];
2350 let mask: u8 = core::arch::aarch64::vaddvq_u32(core::arch::aarch64::vandq_u32(
2351 hit_v,
2352 core::arch::aarch64::vld1q_u32(lane_bits.as_ptr()),
2353 )) as u8;
2354 if mask == 0 {
2355 return (0, f32::INFINITY);
2356 }
2357 let masked_t: core::arch::aarch64::float32x4_t = core::arch::aarch64::vbslq_f32(
2358 hit_v,
2359 vmin,
2360 core::arch::aarch64::vdupq_n_f32(f32::INFINITY),
2361 );
2362 let min_t: f32 = core::arch::aarch64::vminvq_f32(masked_t);
2363 (mask, min_t)
2364 }
2365 }
2366 #[cfg(target_arch = "x86_64")]
2367 {
2368 unsafe {
2370 let ix: core::arch::x86_64::__m128 =
2371 core::arch::x86_64::_mm_loadu_ps(pkt.ix.as_ptr());
2372 let iy: core::arch::x86_64::__m128 =
2373 core::arch::x86_64::_mm_loadu_ps(pkt.iy.as_ptr());
2374 let iz: core::arch::x86_64::__m128 =
2375 core::arch::x86_64::_mm_loadu_ps(pkt.iz.as_ptr());
2376 let nx: core::arch::x86_64::__m128 =
2377 core::arch::x86_64::_mm_loadu_ps(neg_oi[0].as_ptr());
2378 let ny: core::arch::x86_64::__m128 =
2379 core::arch::x86_64::_mm_loadu_ps(neg_oi[1].as_ptr());
2380 let nz: core::arch::x86_64::__m128 =
2381 core::arch::x86_64::_mm_loadu_ps(neg_oi[2].as_ptr());
2382 let tx1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
2383 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(bx0), ix),
2384 nx,
2385 );
2386 let tx2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
2387 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(bx1), ix),
2388 nx,
2389 );
2390 let mut vmin: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_max_ps(
2391 core::arch::x86_64::_mm_set1_ps(t_min),
2392 core::arch::x86_64::_mm_min_ps(tx1, tx2),
2393 );
2394 let mut vmax: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_min_ps(
2395 core::arch::x86_64::_mm_loadu_ps(t_max_4.as_ptr()),
2396 core::arch::x86_64::_mm_max_ps(tx1, tx2),
2397 );
2398 let ty1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
2399 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(by0), iy),
2400 ny,
2401 );
2402 let ty2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
2403 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(by1), iy),
2404 ny,
2405 );
2406 vmin =
2407 core::arch::x86_64::_mm_max_ps(vmin, core::arch::x86_64::_mm_min_ps(ty1, ty2));
2408 vmax =
2409 core::arch::x86_64::_mm_min_ps(vmax, core::arch::x86_64::_mm_max_ps(ty1, ty2));
2410 let tz1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
2411 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(bz0), iz),
2412 nz,
2413 );
2414 let tz2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
2415 core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(bz1), iz),
2416 nz,
2417 );
2418 vmin =
2419 core::arch::x86_64::_mm_max_ps(vmin, core::arch::x86_64::_mm_min_ps(tz1, tz2));
2420 vmax =
2421 core::arch::x86_64::_mm_min_ps(vmax, core::arch::x86_64::_mm_max_ps(tz1, tz2));
2422 let hit_v: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_and_ps(
2423 core::arch::x86_64::_mm_cmpge_ps(vmax, vmin),
2424 core::arch::x86_64::_mm_cmpge_ps(vmax, core::arch::x86_64::_mm_setzero_ps()),
2425 );
2426 let mask: u8 = core::arch::x86_64::_mm_movemask_ps(hit_v) as u8;
2427 if mask == 0 {
2428 return (0, f32::INFINITY);
2429 }
2430 let mut t_arr: [f32; 4] = [0.0; 4];
2431 core::arch::x86_64::_mm_storeu_ps(t_arr.as_mut_ptr(), vmin);
2432 let mut min_t: f32 = f32::INFINITY;
2433 let mut matrix_elems: u8 = mask;
2434 while matrix_elems != 0 {
2435 let red_val: usize = matrix_elems.trailing_zeros() as usize;
2436 matrix_elems &= matrix_elems - 1;
2437 min_t = min_t.min(t_arr[red_val]);
2438 }
2439 (mask, min_t)
2440 }
2441 }
2442 #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
2443 {
2444 unsafe {
2446 let ix: core::arch::wasm32::v128 =
2447 core::arch::wasm32::v128_load(pkt.ix.as_ptr().cast());
2448 let iy: core::arch::wasm32::v128 =
2449 core::arch::wasm32::v128_load(pkt.iy.as_ptr().cast());
2450 let iz: core::arch::wasm32::v128 =
2451 core::arch::wasm32::v128_load(pkt.iz.as_ptr().cast());
2452 let nx: core::arch::wasm32::v128 =
2453 core::arch::wasm32::v128_load(neg_oi[0].as_ptr().cast());
2454 let ny: core::arch::wasm32::v128 =
2455 core::arch::wasm32::v128_load(neg_oi[1].as_ptr().cast());
2456 let nz: core::arch::wasm32::v128 =
2457 core::arch::wasm32::v128_load(neg_oi[2].as_ptr().cast());
2458 let tx1: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
2459 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(bx0), ix),
2460 nx,
2461 );
2462 let tx2: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
2463 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(bx1), ix),
2464 nx,
2465 );
2466 let mut vmin: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_pmax(
2467 core::arch::wasm32::f32x4_splat(t_min),
2468 core::arch::wasm32::f32x4_pmin(tx1, tx2),
2469 );
2470 let mut vmax: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_pmin(
2471 core::arch::wasm32::v128_load(t_max_4.as_ptr().cast()),
2472 core::arch::wasm32::f32x4_pmax(tx1, tx2),
2473 );
2474 let ty1: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
2475 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(by0), iy),
2476 ny,
2477 );
2478 let ty2: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
2479 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(by1), iy),
2480 ny,
2481 );
2482 vmin =
2483 core::arch::wasm32::f32x4_pmax(vmin, core::arch::wasm32::f32x4_pmin(ty1, ty2));
2484 vmax =
2485 core::arch::wasm32::f32x4_pmin(vmax, core::arch::wasm32::f32x4_pmax(ty1, ty2));
2486 let tz1: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
2487 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(bz0), iz),
2488 nz,
2489 );
2490 let tz2: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
2491 core::arch::wasm32::f32x4_mul(core::arch::wasm32::f32x4_splat(bz1), iz),
2492 nz,
2493 );
2494 vmin =
2495 core::arch::wasm32::f32x4_pmax(vmin, core::arch::wasm32::f32x4_pmin(tz1, tz2));
2496 vmax =
2497 core::arch::wasm32::f32x4_pmin(vmax, core::arch::wasm32::f32x4_pmax(tz1, tz2));
2498 let hit_v: core::arch::wasm32::v128 = core::arch::wasm32::v128_and(
2499 core::arch::wasm32::f32x4_ge(vmax, vmin),
2500 core::arch::wasm32::f32x4_ge(vmax, core::arch::wasm32::f32x4_splat(0.0)),
2501 );
2502 let mask: u8 = core::arch::wasm32::i32x4_bitmask(hit_v) as u8;
2503 if mask == 0 {
2504 return (0, f32::INFINITY);
2505 }
2506 let masked_t: core::arch::wasm32::v128 = core::arch::wasm32::v128_bitselect(
2507 vmin,
2508 core::arch::wasm32::f32x4_splat(f32::INFINITY),
2509 hit_v,
2510 );
2511 let t0: f32 = core::arch::wasm32::f32x4_extract_lane::<0>(masked_t);
2512 let t1: f32 = core::arch::wasm32::f32x4_extract_lane::<1>(masked_t);
2513 let t2: f32 = core::arch::wasm32::f32x4_extract_lane::<2>(masked_t);
2514 let t3: f32 = core::arch::wasm32::f32x4_extract_lane::<3>(masked_t);
2515 (mask, t0.min(t1).min(t2.min(t3)))
2516 }
2517 }
2518 #[cfg(not(target_arch = "aarch64"))]
2519 #[cfg(not(target_arch = "x86_64"))]
2520 #[cfg(not(all(target_arch = "wasm32", target_feature = "simd128")))]
2521 {
2522 let mut mask: u8 = 0;
2523 let mut min_t: f32 = f32::INFINITY;
2524 for radius_val in 0..4 {
2525 let blend_factor: f32 = BvhFlatNode::intersect_box_fma(
2526 [bx0, by0, bz0],
2527 [bx1, by1, bz1],
2528 [pkt.ix[radius_val], pkt.iy[radius_val], pkt.iz[radius_val]],
2529 [
2530 neg_oi[0][radius_val],
2531 neg_oi[1][radius_val],
2532 neg_oi[2][radius_val],
2533 ],
2534 t_min,
2535 t_max_4[radius_val],
2536 );
2537 if blend_factor.is_finite() {
2538 mask |= 1_u8 << radius_val;
2539 min_t = min_t.min(blend_factor);
2540 }
2541 }
2542 (mask, min_t)
2543 }
2544 }
2545}
2546
2547#[cfg(target_arch = "x86_64")]
2549impl Bvh8Node {
2550 #[target_feature(enable = "xsave")]
2556 #[must_use]
2557 unsafe fn read_xcr0() -> u64 {
2558 unsafe { core::arch::x86_64::_xgetbv(0) }
2560 }
2561
2562 #[must_use]
2568 pub(crate) fn has_avx2_fma() -> bool {
2569 *X86_AVX2_FMA_AVAILABLE.get_or_init(|| -> bool {
2570 let max_leaf: u32 = core::arch::x86_64::__cpuid(0).eax;
2571 if max_leaf < 7 {
2572 return false;
2573 }
2574 let leaf1: core::arch::x86_64::CpuidResult = core::arch::x86_64::__cpuid(1);
2575 let req_ecx: u32 = (1_u32 << 27) | (1_u32 << 28) | (1_u32 << 12);
2576 if (leaf1.ecx & req_ecx) != req_ecx {
2577 return false;
2578 }
2579 let xcr0: u64 = unsafe { Self::read_xcr0() };
2581 if (xcr0 & 0x6) != 0x6 {
2582 return false;
2583 }
2584 let leaf7: core::arch::x86_64::CpuidResult = core::arch::x86_64::__cpuid_count(7, 0);
2585 (leaf7.ebx & (1_u32 << 5)) != 0
2586 })
2587 }
2588
2589 #[inline(always)]
2595 #[must_use]
2596 pub(crate) const fn empty() -> Self {
2597 Self {
2598 min_x: [f32::INFINITY; 8],
2599 min_y: [f32::INFINITY; 8],
2600 min_z: [f32::INFINITY; 8],
2601 max_x: [f32::INFINITY; 8],
2602 max_y: [f32::INFINITY; 8],
2603 max_z: [f32::INFINITY; 8],
2604 child: [u32::MAX; 8],
2605 leaf_count: [0; 8],
2606 valid_count: 0,
2607 _pad: [0; 23],
2608 }
2609 }
2610
2611 #[target_feature(enable = "avx2", enable = "fma")]
2625 #[must_use]
2626 pub(crate) unsafe fn intersect_8_avx2(
2627 &self,
2628 inv: [f32; 3],
2629 neg_orig_inv: [f32; 3],
2630 t_min: f32,
2631 t_max: f32,
2632 ) -> ([f32; 8], u32) {
2633 let Self {
2634 min_x,
2635 min_y,
2636 min_z,
2637 max_x,
2638 max_y,
2639 max_z,
2640 valid_count,
2641 ..
2642 } = self;
2643 let valid_mask: u32 = (1_u32 << (*valid_count as u32)) - 1;
2644 let mut t_out: [f32; 8] = [f32::INFINITY; 8];
2645 let mask: u32 = unsafe {
2648 let ix: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_set1_ps(inv[0]);
2649 let iy: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_set1_ps(inv[1]);
2650 let iz: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_set1_ps(inv[2]);
2651 let nx: core::arch::x86_64::__m256 =
2652 core::arch::x86_64::_mm256_set1_ps(neg_orig_inv[0]);
2653 let ny: core::arch::x86_64::__m256 =
2654 core::arch::x86_64::_mm256_set1_ps(neg_orig_inv[1]);
2655 let nz: core::arch::x86_64::__m256 =
2656 core::arch::x86_64::_mm256_set1_ps(neg_orig_inv[2]);
2657 let tx1: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_fmadd_ps(
2658 core::arch::x86_64::_mm256_load_ps(min_x.as_ptr()),
2659 ix,
2660 nx,
2661 );
2662 let tx2: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_fmadd_ps(
2663 core::arch::x86_64::_mm256_load_ps(max_x.as_ptr()),
2664 ix,
2665 nx,
2666 );
2667 let mut vmin: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_max_ps(
2668 core::arch::x86_64::_mm256_set1_ps(t_min),
2669 core::arch::x86_64::_mm256_min_ps(tx1, tx2),
2670 );
2671 let mut vmax: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_min_ps(
2672 core::arch::x86_64::_mm256_set1_ps(t_max),
2673 core::arch::x86_64::_mm256_max_ps(tx1, tx2),
2674 );
2675 let ty1: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_fmadd_ps(
2676 core::arch::x86_64::_mm256_load_ps(min_y.as_ptr()),
2677 iy,
2678 ny,
2679 );
2680 let ty2: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_fmadd_ps(
2681 core::arch::x86_64::_mm256_load_ps(max_y.as_ptr()),
2682 iy,
2683 ny,
2684 );
2685 vmin = core::arch::x86_64::_mm256_max_ps(
2686 vmin,
2687 core::arch::x86_64::_mm256_min_ps(ty1, ty2),
2688 );
2689 vmax = core::arch::x86_64::_mm256_min_ps(
2690 vmax,
2691 core::arch::x86_64::_mm256_max_ps(ty1, ty2),
2692 );
2693 let tz1: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_fmadd_ps(
2694 core::arch::x86_64::_mm256_load_ps(min_z.as_ptr()),
2695 iz,
2696 nz,
2697 );
2698 let tz2: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_fmadd_ps(
2699 core::arch::x86_64::_mm256_load_ps(max_z.as_ptr()),
2700 iz,
2701 nz,
2702 );
2703 vmin = core::arch::x86_64::_mm256_max_ps(
2704 vmin,
2705 core::arch::x86_64::_mm256_min_ps(tz1, tz2),
2706 );
2707 vmax = core::arch::x86_64::_mm256_min_ps(
2708 vmax,
2709 core::arch::x86_64::_mm256_max_ps(tz1, tz2),
2710 );
2711 core::arch::x86_64::_mm256_storeu_ps(t_out.as_mut_ptr(), vmin);
2712 let hit_v: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_and_ps(
2713 core::arch::x86_64::_mm256_cmp_ps::<{ core::arch::x86_64::_CMP_GE_OQ }>(vmax, vmin),
2714 core::arch::x86_64::_mm256_cmp_ps::<{ core::arch::x86_64::_CMP_GE_OQ }>(
2715 vmax,
2716 core::arch::x86_64::_mm256_setzero_ps(),
2717 ),
2718 );
2719 core::arch::x86_64::_mm256_movemask_ps(hit_v) as u32
2720 };
2721 (t_out, mask & valid_mask)
2722 }
2723}
2724
2725impl BvhTriangle {
2727 #[must_use]
2740 pub fn from_points(v0: Vec3, v1: Vec3, v2: Vec3, material: PbrMaterial) -> Self {
2741 let edge1: Vec3 = v1.sub(v0);
2742 let edge2: Vec3 = v2.sub(v0);
2743 let normal: Vec3 = edge1.cross(edge2).normalize();
2744 let centroid: Vec3 = v0.add(v1).add(v2).scale(1.0 / 3.0);
2745 let mut bounds: AxisAlignedBox3D = AxisAlignedBox3D::empty();
2746 bounds.expand_point(v0);
2747 bounds.expand_point(v1);
2748 bounds.expand_point(v2);
2749 Self {
2750 v0,
2751 edge1,
2752 edge2,
2753 normal,
2754 centroid,
2755 bounds,
2756 material,
2757 }
2758 }
2759
2760 #[must_use]
2773 pub fn intersect(&self, ray: &Ray3D, t_min: f32, t_max: f32) -> Option<f32> {
2774 let green_val: BvhGeomTri = BvhGeomTri::from_triangle(self);
2775 green_val.intersect_raw(ray.get_origin(), ray.get_direction(), t_min, t_max)
2776 }
2777}
2778
2779impl RayPacket4 {
2781 #[must_use]
2791 pub fn from_rays(rays: &[Ray3D; 4]) -> Self {
2792 let o0: Vec3 = rays[0].get_origin();
2793 let o1: Vec3 = rays[1].get_origin();
2794 let o2: Vec3 = rays[2].get_origin();
2795 let o3: Vec3 = rays[3].get_origin();
2796 let d0: Vec3 = rays[0].get_direction();
2797 let d1: Vec3 = rays[1].get_direction();
2798 let d2: Vec3 = rays[2].get_direction();
2799 let d3: Vec3 = rays[3].get_direction();
2800 let i0: Vec3 = rays[0].get_inv_direction();
2801 let i1: Vec3 = rays[1].get_inv_direction();
2802 let i2: Vec3 = rays[2].get_inv_direction();
2803 let i3: Vec3 = rays[3].get_inv_direction();
2804 Self {
2805 ox: [o0.get_x(), o1.get_x(), o2.get_x(), o3.get_x()],
2806 oy: [o0.get_y(), o1.get_y(), o2.get_y(), o3.get_y()],
2807 oz: [o0.get_z(), o1.get_z(), o2.get_z(), o3.get_z()],
2808 dx: [d0.get_x(), d1.get_x(), d2.get_x(), d3.get_x()],
2809 dy: [d0.get_y(), d1.get_y(), d2.get_y(), d3.get_y()],
2810 dz: [d0.get_z(), d1.get_z(), d2.get_z(), d3.get_z()],
2811 ix: [i0.get_x(), i1.get_x(), i2.get_x(), i3.get_x()],
2812 iy: [i0.get_y(), i1.get_y(), i2.get_y(), i3.get_y()],
2813 iz: [i0.get_z(), i1.get_z(), i2.get_z(), i3.get_z()],
2814 }
2815 }
2816}
2817
2818impl Bvh3D {
2820 fn flatten_sibling_pair(
2832 nodes: &[BvhNode3D],
2833 geom: &[BvhGeomTri],
2834 leaf_packets: &mut Vec<BvhTri4>,
2835 flat_nodes: &mut Vec<BvhFlatNode>,
2836 node_to_flat: &mut [u32],
2837 src_idx: usize,
2838 dst_idx: usize,
2839 ) {
2840 if src_idx < node_to_flat.len() {
2841 node_to_flat[src_idx] = dst_idx as u32;
2842 }
2843 let item_count: &BvhNode3D = &nodes[src_idx];
2844 let mn: Vec3 = item_count.get_bounds().get_min();
2845 let mx: Vec3 = item_count.get_bounds().get_max();
2846 let tc: u32 = item_count.get_tri_count();
2847 if tc > 0 {
2848 let first_tri: usize = item_count.get_first_tri() as usize;
2849 let end_tri: usize = first_tri + (tc as usize);
2850 let pkt_first: u32 = leaf_packets.len() as u32;
2851 let mut cursor: usize = first_tri;
2852 while cursor < end_tri {
2853 let next_end: usize = (cursor + 4).min(end_tri);
2854 leaf_packets.push(BvhTri4::from_geom_chunk(
2855 &geom[cursor..next_end],
2856 cursor as u32,
2857 ));
2858 cursor = next_end;
2859 }
2860 let pkt_count: u32 = (leaf_packets.len() as u32) - pkt_first;
2861 flat_nodes[dst_idx] = BvhFlatNode {
2862 min: [mn.get_x(), mn.get_y(), mn.get_z()],
2863 child_or_first: pkt_first,
2864 max: [mx.get_x(), mx.get_y(), mx.get_z()],
2865 tri_count: pkt_count,
2866 };
2867 } else {
2868 let first_child: usize = flat_nodes.len();
2869 flat_nodes.push(BvhFlatNode::empty());
2870 flat_nodes.push(BvhFlatNode::empty());
2871 flat_nodes[dst_idx] = BvhFlatNode {
2872 min: [mn.get_x(), mn.get_y(), mn.get_z()],
2873 child_or_first: first_child as u32,
2874 max: [mx.get_x(), mx.get_y(), mx.get_z()],
2875 tri_count: 0,
2876 };
2877 Self::flatten_sibling_pair(
2878 nodes,
2879 geom,
2880 leaf_packets,
2881 flat_nodes,
2882 node_to_flat,
2883 item_count.get_left() as usize,
2884 first_child,
2885 );
2886 Self::flatten_sibling_pair(
2887 nodes,
2888 geom,
2889 leaf_packets,
2890 flat_nodes,
2891 node_to_flat,
2892 item_count.get_right() as usize,
2893 first_child + 1,
2894 );
2895 }
2896 }
2897
2898 fn collapse_to_bvh4(
2910 nodes: &[BvhNode3D],
2911 flat_nodes: &[BvhFlatNode],
2912 node_to_flat: &[u32],
2913 bvh4_nodes: &mut Vec<Bvh4Node>,
2914 bvh4_src: &mut Vec<[u32; 4]>,
2915 src_idx: usize,
2916 dst_idx: usize,
2917 ) {
2918 let root_n: &BvhNode3D = &nodes[src_idx];
2919 let mut children: [usize; 4] = [0; 4];
2920 let mut len: usize;
2921 if root_n.get_tri_count() > 0 {
2922 children[0] = src_idx;
2923 len = 1;
2924 } else {
2925 children[0] = root_n.get_left() as usize;
2926 children[1] = root_n.get_right() as usize;
2927 len = 2;
2928 while len < 4 {
2929 let mut best_slot: Option<usize> = None;
2930 let mut best_area: f32 = -1.0;
2931 for (slot, &c_idx) in children.iter().take(len).enumerate() {
2932 if nodes[c_idx].get_tri_count() == 0 {
2933 let area: f32 = nodes[c_idx].get_bounds().surface_area();
2934 if area > best_area {
2935 best_area = area;
2936 best_slot = Some(slot);
2937 }
2938 }
2939 }
2940 let Some(expand_slot) = best_slot else {
2941 break;
2942 };
2943 let expand_node: &BvhNode3D = &nodes[children[expand_slot]];
2944 children[expand_slot] = expand_node.get_left() as usize;
2945 children[len] = expand_node.get_right() as usize;
2946 len += 1;
2947 }
2948 }
2949 let mut node4: Bvh4Node = Bvh4Node::empty();
2950 let mut src_ids: [u32; 4] = [u32::MAX; 4];
2951 node4.valid_count = len as u8;
2952 let mut interior_recur: [(usize, usize); 4] = [(0, 0); 4];
2953 let mut interior_len: usize = 0;
2954 for (lane, &c_idx) in children.iter().take(len).enumerate() {
2955 let cn: &BvhNode3D = &nodes[c_idx];
2956 let mn: Vec3 = cn.get_bounds().get_min();
2957 let mx: Vec3 = cn.get_bounds().get_max();
2958 node4.min_x[lane] = mn.get_x();
2959 node4.min_y[lane] = mn.get_y();
2960 node4.min_z[lane] = mn.get_z();
2961 node4.max_x[lane] = mx.get_x();
2962 node4.max_y[lane] = mx.get_y();
2963 node4.max_z[lane] = mx.get_z();
2964 src_ids[lane] = c_idx as u32;
2965 if cn.get_tri_count() > 0 {
2966 let flat: &BvhFlatNode = &flat_nodes[node_to_flat[c_idx] as usize];
2967 node4.child[lane] = flat.child_or_first;
2968 node4.leaf_count[lane] = flat.tri_count.min(255) as u8;
2969 } else {
2970 let next_dst: usize = bvh4_nodes.len();
2971 bvh4_nodes.push(Bvh4Node::empty());
2972 bvh4_src.push([u32::MAX; 4]);
2973 node4.child[lane] = next_dst as u32;
2974 node4.leaf_count[lane] = 0;
2975 interior_recur[interior_len] = (c_idx, next_dst);
2976 interior_len += 1;
2977 }
2978 }
2979 bvh4_nodes[dst_idx] = node4;
2980 bvh4_src[dst_idx] = src_ids;
2981 for &(c_src, c_dst) in interior_recur.iter().take(interior_len) {
2982 Self::collapse_to_bvh4(
2983 nodes,
2984 flat_nodes,
2985 node_to_flat,
2986 bvh4_nodes,
2987 bvh4_src,
2988 c_src,
2989 c_dst,
2990 );
2991 }
2992 }
2993
2994 #[cfg(target_arch = "x86_64")]
3006 fn collapse_to_bvh8(
3007 nodes: &[BvhNode3D],
3008 flat_nodes: &[BvhFlatNode],
3009 node_to_flat: &[u32],
3010 bvh8_nodes: &mut Vec<Bvh8Node>,
3011 bvh8_src: &mut Vec<[u32; 8]>,
3012 src_idx: usize,
3013 dst_idx: usize,
3014 ) {
3015 let root_n: &BvhNode3D = &nodes[src_idx];
3016 let mut children: [usize; 8] = [0; 8];
3017 let mut len: usize;
3018 if root_n.get_tri_count() > 0 {
3019 children[0] = src_idx;
3020 len = 1;
3021 } else {
3022 children[0] = root_n.get_left() as usize;
3023 children[1] = root_n.get_right() as usize;
3024 len = 2;
3025 while len < 8 {
3026 let mut best_slot: Option<usize> = None;
3027 let mut best_area: f32 = -1.0;
3028 for (slot, &c_idx) in children.iter().take(len).enumerate() {
3029 if nodes[c_idx].get_tri_count() == 0 {
3030 let area: f32 = nodes[c_idx].get_bounds().surface_area();
3031 if area > best_area {
3032 best_area = area;
3033 best_slot = Some(slot);
3034 }
3035 }
3036 }
3037 let Some(expand_slot) = best_slot else {
3038 break;
3039 };
3040 let expand_node: &BvhNode3D = &nodes[children[expand_slot]];
3041 children[expand_slot] = expand_node.get_left() as usize;
3042 children[len] = expand_node.get_right() as usize;
3043 len += 1;
3044 }
3045 }
3046 let mut node8: Bvh8Node = Bvh8Node::empty();
3047 let mut src_ids: [u32; 8] = [u32::MAX; 8];
3048 node8.valid_count = len as u8;
3049 let mut interior_recur: [(usize, usize); 8] = [(0, 0); 8];
3050 let mut interior_len: usize = 0;
3051 for (lane, &c_idx) in children.iter().take(len).enumerate() {
3052 let cn: &BvhNode3D = &nodes[c_idx];
3053 let mn: Vec3 = cn.get_bounds().get_min();
3054 let mx: Vec3 = cn.get_bounds().get_max();
3055 node8.min_x[lane] = mn.get_x();
3056 node8.min_y[lane] = mn.get_y();
3057 node8.min_z[lane] = mn.get_z();
3058 node8.max_x[lane] = mx.get_x();
3059 node8.max_y[lane] = mx.get_y();
3060 node8.max_z[lane] = mx.get_z();
3061 src_ids[lane] = c_idx as u32;
3062 if cn.get_tri_count() > 0 {
3063 let flat: &BvhFlatNode = &flat_nodes[node_to_flat[c_idx] as usize];
3064 node8.child[lane] = flat.child_or_first;
3065 node8.leaf_count[lane] = flat.tri_count.min(255) as u8;
3066 } else {
3067 let next_dst: usize = bvh8_nodes.len();
3068 bvh8_nodes.push(Bvh8Node::empty());
3069 bvh8_src.push([u32::MAX; 8]);
3070 node8.child[lane] = next_dst as u32;
3071 node8.leaf_count[lane] = 0;
3072 interior_recur[interior_len] = (c_idx, next_dst);
3073 interior_len += 1;
3074 }
3075 }
3076 bvh8_nodes[dst_idx] = node8;
3077 bvh8_src[dst_idx] = src_ids;
3078 for &(c_src, c_dst) in interior_recur.iter().take(interior_len) {
3079 Self::collapse_to_bvh8(
3080 nodes,
3081 flat_nodes,
3082 node_to_flat,
3083 bvh8_nodes,
3084 bvh8_src,
3085 c_src,
3086 c_dst,
3087 );
3088 }
3089 }
3090
3091 #[must_use]
3101 pub fn build(triangles: Vec<BvhTriangle>) -> Self {
3102 let tri_len: usize = triangles.len();
3103 if tri_len == 0 {
3104 return Self::default();
3105 }
3106 let mut orig_indices: Vec<u32> = Vec::with_capacity(tri_len);
3107 for idx in 0..tri_len {
3108 orig_indices.push(idx as u32);
3109 }
3110 let mut bvh: Self = Self {
3111 nodes: Vec::with_capacity(tri_len * 2 + 1),
3112 triangles,
3113 geom: Vec::with_capacity(tri_len),
3114 leaf_packets: Vec::with_capacity(tri_len.div_ceil(4) + 16),
3115 flat_nodes: Vec::with_capacity(tri_len * 2 + 2),
3116 bvh4_nodes: Vec::with_capacity(tri_len.div_ceil(2) + 4),
3117 bvh4_src_children: Vec::with_capacity(tri_len.div_ceil(2) + 4),
3118 root_min: [0.0; 3],
3119 root_max: [0.0; 3],
3120 #[cfg(target_arch = "x86_64")]
3121 bvh8_nodes: Vec::with_capacity(tri_len.div_ceil(4) + 4),
3122 #[cfg(target_arch = "x86_64")]
3123 bvh8_src_children: Vec::with_capacity(tri_len.div_ceil(4) + 4),
3124 orig_indices,
3125 node_to_flat: Vec::new(),
3126 build_sah_cost: 0.0,
3127 };
3128 if !bvh.get_triangles().is_empty() {
3129 bvh.rebuild_current();
3130 }
3131 bvh
3132 }
3133
3134 pub fn rebuild_in_place(&mut self, source: &[BvhTriangle]) {
3141 let tri_len: usize = source.len();
3142 let Self {
3143 nodes,
3144 triangles,
3145 geom,
3146 leaf_packets,
3147 flat_nodes,
3148 bvh4_nodes,
3149 bvh4_src_children,
3150 root_min,
3151 root_max,
3152 #[cfg(target_arch = "x86_64")]
3153 bvh8_nodes,
3154 #[cfg(target_arch = "x86_64")]
3155 bvh8_src_children,
3156 orig_indices,
3157 node_to_flat,
3158 build_sah_cost,
3159 } = self;
3160 nodes.clear();
3161 triangles.clear();
3162 geom.clear();
3163 leaf_packets.clear();
3164 flat_nodes.clear();
3165 bvh4_nodes.clear();
3166 bvh4_src_children.clear();
3167 #[cfg(target_arch = "x86_64")]
3168 {
3169 bvh8_nodes.clear();
3170 bvh8_src_children.clear();
3171 }
3172 orig_indices.clear();
3173 node_to_flat.clear();
3174 *root_min = [0.0; 3];
3175 *root_max = [0.0; 3];
3176 *build_sah_cost = 0.0;
3177 if tri_len == 0 {
3178 return;
3179 }
3180 triangles.extend_from_slice(source);
3181 let max_nodes: usize = tri_len.saturating_mul(2).saturating_add(8);
3182 if nodes.capacity() < max_nodes {
3183 nodes.reserve(max_nodes - nodes.capacity());
3184 }
3185 if geom.capacity() < tri_len {
3186 geom.reserve(tri_len - geom.capacity());
3187 }
3188 let max_leaves: usize = tri_len.div_ceil(4).saturating_add(16);
3189 if leaf_packets.capacity() < max_leaves {
3190 leaf_packets.reserve(max_leaves - leaf_packets.capacity());
3191 }
3192 if flat_nodes.capacity() < max_nodes {
3193 flat_nodes.reserve(max_nodes - flat_nodes.capacity());
3194 }
3195 let max_bvh4: usize = tri_len.div_ceil(2).saturating_add(8);
3196 if bvh4_nodes.capacity() < max_bvh4 {
3197 bvh4_nodes.reserve(max_bvh4 - bvh4_nodes.capacity());
3198 }
3199 if bvh4_src_children.capacity() < max_bvh4 {
3200 bvh4_src_children.reserve(max_bvh4 - bvh4_src_children.capacity());
3201 }
3202 if node_to_flat.capacity() < max_nodes {
3203 node_to_flat.reserve(max_nodes - node_to_flat.capacity());
3204 }
3205 if orig_indices.capacity() < tri_len {
3206 orig_indices.reserve(tri_len - orig_indices.capacity());
3207 }
3208 for idx in 0..tri_len {
3209 orig_indices.push(idx as u32);
3210 }
3211 self.rebuild_current();
3212 }
3213
3214 fn rebuild_current(&mut self) {
3220 let total: usize = self.get_triangles().len();
3221 if total == 0 {
3222 return;
3223 }
3224 let _: u32 = self.build_node(0, total);
3225 let Self {
3226 nodes,
3227 triangles,
3228 geom,
3229 leaf_packets,
3230 flat_nodes,
3231 bvh4_nodes,
3232 bvh4_src_children,
3233 root_min,
3234 root_max,
3235 #[cfg(target_arch = "x86_64")]
3236 bvh8_nodes,
3237 #[cfg(target_arch = "x86_64")]
3238 bvh8_src_children,
3239 node_to_flat,
3240 build_sah_cost,
3241 ..
3242 } = self;
3243 geom.clear();
3244 for tri in triangles.iter() {
3245 geom.push(BvhGeomTri::from_triangle(tri));
3246 }
3247 leaf_packets.clear();
3248 flat_nodes.clear();
3249 bvh4_nodes.clear();
3250 bvh4_src_children.clear();
3251 node_to_flat.clear();
3252 node_to_flat.resize(nodes.len(), 0);
3253 if !nodes.is_empty() {
3254 let rmin: Vec3 = nodes[0].get_bounds().get_min();
3255 let rmax: Vec3 = nodes[0].get_bounds().get_max();
3256 *root_min = [rmin.get_x(), rmin.get_y(), rmin.get_z()];
3257 *root_max = [rmax.get_x(), rmax.get_y(), rmax.get_z()];
3258 flat_nodes.push(BvhFlatNode::empty());
3259 flat_nodes.push(BvhFlatNode::empty());
3260 Self::flatten_sibling_pair(nodes, geom, leaf_packets, flat_nodes, node_to_flat, 0, 0);
3261 bvh4_nodes.push(Bvh4Node::empty());
3262 bvh4_src_children.push([u32::MAX; 4]);
3263 Self::collapse_to_bvh4(
3264 nodes,
3265 flat_nodes,
3266 node_to_flat,
3267 bvh4_nodes,
3268 bvh4_src_children,
3269 0,
3270 0,
3271 );
3272 #[cfg(target_arch = "x86_64")]
3273 {
3274 bvh8_nodes.clear();
3275 bvh8_src_children.clear();
3276 if Bvh8Node::has_avx2_fma() {
3277 bvh8_nodes.push(Bvh8Node::empty());
3278 bvh8_src_children.push([u32::MAX; 8]);
3279 Self::collapse_to_bvh8(
3280 nodes,
3281 flat_nodes,
3282 node_to_flat,
3283 bvh8_nodes,
3284 bvh8_src_children,
3285 0,
3286 0,
3287 );
3288 }
3289 }
3290 let mut sah_sum: f32 = 0.0;
3291 for item_count in nodes.iter() {
3292 let area: f32 = item_count.get_bounds().surface_area();
3293 if item_count.get_tri_count() > 0 {
3294 sah_sum += area * (item_count.get_tri_count() as f32);
3295 } else {
3296 sah_sum += area * 0.5;
3297 }
3298 }
3299 *build_sah_cost = sah_sum.max(1e-5);
3300 }
3301 }
3302
3303 #[must_use]
3313 pub fn is_empty(&self) -> bool {
3314 self.get_triangles().is_empty()
3315 }
3316
3317 #[must_use]
3327 pub fn surface_area_cost(&self) -> f32 {
3328 let mut cost: f32 = 0.0;
3329 for item_count in self.get_nodes().iter() {
3330 let area: f32 = item_count.get_bounds().surface_area();
3331 if item_count.get_tri_count() > 0 {
3332 cost += area * (item_count.get_tri_count() as f32);
3333 } else {
3334 cost += area * 0.5;
3335 }
3336 }
3337 cost
3338 }
3339
3340 #[must_use]
3351 pub fn needs_rebuild_after_refit(&self, max_cost_ratio: f32) -> bool {
3352 if self.is_empty() {
3353 return false;
3354 }
3355 let base: f32 = self.get_build_sah_cost().max(1e-5);
3356 self.surface_area_cost() > base * max_cost_ratio.max(1.05)
3357 }
3358
3359 pub fn refit_from_source(&mut self, source_triangles: &[BvhTriangle]) -> bool {
3370 let Self {
3371 nodes,
3372 triangles,
3373 geom,
3374 leaf_packets,
3375 flat_nodes,
3376 bvh4_nodes,
3377 bvh4_src_children,
3378 root_min,
3379 root_max,
3380 #[cfg(target_arch = "x86_64")]
3381 bvh8_nodes,
3382 #[cfg(target_arch = "x86_64")]
3383 bvh8_src_children,
3384 orig_indices,
3385 node_to_flat,
3386 ..
3387 } = self;
3388 let count: usize = source_triangles.len();
3389 if count == 0
3390 || triangles.len() != count
3391 || orig_indices.len() != count
3392 || nodes.is_empty()
3393 || node_to_flat.len() != nodes.len()
3394 {
3395 return false;
3396 }
3397 for slot in 0..count {
3398 let src_idx: usize = orig_indices[slot] as usize;
3399 if src_idx >= count {
3400 return false;
3401 }
3402 let updated: BvhTriangle = source_triangles[src_idx];
3403 triangles[slot] = updated;
3404 geom[slot] = BvhGeomTri::from_triangle(&updated);
3405 }
3406 for node_idx in (0..nodes.len()).rev() {
3407 let tc: u32 = nodes[node_idx].get_tri_count();
3408 let flat_idx: usize = node_to_flat[node_idx] as usize;
3409 if tc > 0 {
3410 let first: usize = nodes[node_idx].get_first_tri() as usize;
3411 let end: usize = (first + (tc as usize)).min(count);
3412 let mut bounds: AxisAlignedBox3D = AxisAlignedBox3D::empty();
3413 for tri in triangles[first..end].iter() {
3414 bounds.expand_box(tri.get_bounds());
3415 }
3416 nodes[node_idx].set_bounds(bounds);
3417 if let Some(flat_node) = flat_nodes.get_mut(flat_idx) {
3418 let mn: Vec3 = bounds.get_min();
3419 let mx: Vec3 = bounds.get_max();
3420 flat_node.min = [mn.get_x(), mn.get_y(), mn.get_z()];
3421 flat_node.max = [mx.get_x(), mx.get_y(), mx.get_z()];
3422 let mut pkt_idx: usize = flat_node.child_or_first as usize;
3423 let mut cursor: usize = first;
3424 while cursor < end && pkt_idx < leaf_packets.len() {
3425 let next_end: usize = (cursor + 4).min(end);
3426 leaf_packets[pkt_idx] =
3427 BvhTri4::from_geom_chunk(&geom[cursor..next_end], cursor as u32);
3428 cursor = next_end;
3429 pkt_idx += 1;
3430 }
3431 }
3432 } else {
3433 let left_idx: usize = nodes[node_idx].get_left() as usize;
3434 let right_idx: usize = nodes[node_idx].get_right() as usize;
3435 let mut bounds: AxisAlignedBox3D = nodes[left_idx].get_bounds();
3436 bounds.expand_box(nodes[right_idx].get_bounds());
3437 nodes[node_idx].set_bounds(bounds);
3438 if let Some(flat_node) = flat_nodes.get_mut(flat_idx) {
3439 let mn: Vec3 = bounds.get_min();
3440 let mx: Vec3 = bounds.get_max();
3441 flat_node.min = [mn.get_x(), mn.get_y(), mn.get_z()];
3442 flat_node.max = [mx.get_x(), mx.get_y(), mx.get_z()];
3443 }
3444 }
3445 }
3446 let rmin: Vec3 = nodes[0].get_bounds().get_min();
3447 let rmax: Vec3 = nodes[0].get_bounds().get_max();
3448 *root_min = [rmin.get_x(), rmin.get_y(), rmin.get_z()];
3449 *root_max = [rmax.get_x(), rmax.get_y(), rmax.get_z()];
3450 for (n4, srcs) in bvh4_nodes.iter_mut().zip(bvh4_src_children.iter()) {
3451 let vcnt: usize = n4.valid_count as usize;
3452 for lane in 0..vcnt {
3453 let sidx: usize = srcs[lane] as usize;
3454 if let Some(cn) = nodes.get(sidx) {
3455 let mn: Vec3 = cn.get_bounds().get_min();
3456 let mx: Vec3 = cn.get_bounds().get_max();
3457 n4.min_x[lane] = mn.get_x();
3458 n4.min_y[lane] = mn.get_y();
3459 n4.min_z[lane] = mn.get_z();
3460 n4.max_x[lane] = mx.get_x();
3461 n4.max_y[lane] = mx.get_y();
3462 n4.max_z[lane] = mx.get_z();
3463 }
3464 }
3465 }
3466 #[cfg(target_arch = "x86_64")]
3467 for (n8, srcs) in bvh8_nodes.iter_mut().zip(bvh8_src_children.iter()) {
3468 let vcnt: usize = n8.valid_count as usize;
3469 for lane in 0..vcnt {
3470 let sidx: usize = srcs[lane] as usize;
3471 if let Some(cn) = nodes.get(sidx) {
3472 let mn: Vec3 = cn.get_bounds().get_min();
3473 let mx: Vec3 = cn.get_bounds().get_max();
3474 n8.min_x[lane] = mn.get_x();
3475 n8.min_y[lane] = mn.get_y();
3476 n8.min_z[lane] = mn.get_z();
3477 n8.max_x[lane] = mx.get_x();
3478 n8.max_y[lane] = mx.get_y();
3479 n8.max_z[lane] = mx.get_z();
3480 }
3481 }
3482 }
3483 true
3484 }
3485
3486 #[must_use]
3497 fn vec_axis(vec_value: Vec3, axis: usize) -> f32 {
3498 match axis {
3499 0 => vec_value.get_x(),
3500 1 => vec_value.get_y(),
3501 _ => vec_value.get_z(),
3502 }
3503 }
3504
3505 #[must_use]
3517 fn find_sah_split(
3518 tris: &[BvhTriangle],
3519 cent_bounds: AxisAlignedBox3D,
3520 parent_area: f32,
3521 ) -> Option<(usize, f32)> {
3522 let bins: usize = 12;
3523 let extent: Vec3 = cent_bounds.get_max().sub(cent_bounds.get_min());
3524 let exts: [f32; 3] = [extent.get_x(), extent.get_y(), extent.get_z()];
3525 let cmins: [f32; 3] = [
3526 cent_bounds.get_min().get_x(),
3527 cent_bounds.get_min().get_y(),
3528 cent_bounds.get_min().get_z(),
3529 ];
3530 let active: [bool; 3] = [exts[0] >= 1e-5, exts[1] >= 1e-5, exts[2] >= 1e-5];
3531 let scales: [f32; 3] = [
3532 if active[0] {
3533 (bins as f32) / exts[0]
3534 } else {
3535 0.0
3536 },
3537 if active[1] {
3538 (bins as f32) / exts[1]
3539 } else {
3540 0.0
3541 },
3542 if active[2] {
3543 (bins as f32) / exts[2]
3544 } else {
3545 0.0
3546 },
3547 ];
3548 let mut bin_bounds: [[AxisAlignedBox3D; 12]; 3] = [[AxisAlignedBox3D::empty(); 12]; 3];
3549 let mut bin_counts: [[u32; 12]; 3] = [[0; 12]; 3];
3550 for tri in tris.iter() {
3551 let vec_value: Vec3 = tri.get_centroid();
3552 let tb: AxisAlignedBox3D = tri.get_bounds();
3553 if active[0] {
3554 let b0: usize =
3555 (((vec_value.get_x() - cmins[0]) * scales[0]) as usize).min(bins - 1);
3556 bin_counts[0][b0] += 1;
3557 bin_bounds[0][b0].expand_box(tb);
3558 }
3559 if active[1] {
3560 let b1: usize =
3561 (((vec_value.get_y() - cmins[1]) * scales[1]) as usize).min(bins - 1);
3562 bin_counts[1][b1] += 1;
3563 bin_bounds[1][b1].expand_box(tb);
3564 }
3565 if active[2] {
3566 let b2: usize =
3567 (((vec_value.get_z() - cmins[2]) * scales[2]) as usize).min(bins - 1);
3568 bin_counts[2][b2] += 1;
3569 bin_bounds[2][b2].expand_box(tb);
3570 }
3571 }
3572 let mut best_cost: f32 = (tris.len() as f32) * parent_area;
3573 let mut best_split: Option<(usize, f32)> = None;
3574 for axis in 0..3 {
3575 if !active[axis] {
3576 continue;
3577 }
3578 let counts: &[u32; 12] = &bin_counts[axis];
3579 let bounds_arr: &[AxisAlignedBox3D; 12] = &bin_bounds[axis];
3580 let mut left_area: [f32; 11] = [0.0; 11];
3581 let mut left_count: [u32; 11] = [0; 11];
3582 let mut acc_box: AxisAlignedBox3D = AxisAlignedBox3D::empty();
3583 let mut acc_cnt: u32 = 0;
3584 for item_index in 0..(bins - 1) {
3585 acc_cnt += counts[item_index];
3586 if counts[item_index] > 0 {
3587 acc_box.expand_box(bounds_arr[item_index]);
3588 }
3589 left_count[item_index] = acc_cnt;
3590 left_area[item_index] = if acc_cnt > 0 {
3591 acc_box.surface_area()
3592 } else {
3593 0.0
3594 };
3595 }
3596 let mut r_box: AxisAlignedBox3D = AxisAlignedBox3D::empty();
3597 let mut r_cnt: u32 = 0;
3598 let step: f32 = exts[axis] / (bins as f32);
3599 for outer_index in (1..bins).rev() {
3600 r_cnt += counts[outer_index];
3601 if counts[outer_index] > 0 {
3602 r_box.expand_box(bounds_arr[outer_index]);
3603 }
3604 let l_cnt: u32 = left_count[outer_index - 1];
3605 if l_cnt == 0 || r_cnt == 0 {
3606 continue;
3607 }
3608 let r_area: f32 = r_box.surface_area();
3609 let cost: f32 = 0.5 * parent_area
3610 + (l_cnt as f32) * left_area[outer_index - 1]
3611 + (r_cnt as f32) * r_area;
3612 if cost < best_cost {
3613 best_cost = cost;
3614 let split_pos: f32 = cmins[axis] + (outer_index as f32) * step;
3615 best_split = Some((axis, split_pos));
3616 }
3617 }
3618 }
3619 best_split
3620 }
3621
3622 fn build_node(&mut self, start: usize, end: usize) -> u32 {
3634 let count: usize = end - start;
3635 let mut bounds: AxisAlignedBox3D = AxisAlignedBox3D::empty();
3636 let mut cent_bounds: AxisAlignedBox3D = AxisAlignedBox3D::empty();
3637 for tri in self.get_triangles()[start..end].iter() {
3638 bounds.expand_box(tri.get_bounds());
3639 cent_bounds.expand_point(tri.get_centroid());
3640 }
3641 let node_idx: u32 = self.get_nodes().len() as u32;
3642 self.get_mut_nodes().push(BvhNode3D {
3643 bounds,
3644 left: 0,
3645 right: 0,
3646 first_tri: start as u32,
3647 tri_count: count as u32,
3648 });
3649 if count <= 4 {
3650 return node_idx;
3651 }
3652 let parent_area: f32 = bounds.surface_area().max(1e-6);
3653 let split_opt: Option<(usize, f32)> =
3654 Self::find_sah_split(&self.get_triangles()[start..end], cent_bounds, parent_area);
3655 let mid: usize = if let Some((axis, split_pos)) = split_opt {
3656 let Self {
3657 triangles,
3658 orig_indices,
3659 ..
3660 } = self;
3661 let slice: &mut [BvhTriangle] = &mut triangles[start..end];
3662 let idx_slice: &mut [u32] = &mut orig_indices[start..end];
3663 let mut item_index: usize = 0;
3664 let mut inner_index: usize = slice.len();
3665 while item_index < inner_index {
3666 if Self::vec_axis(slice[item_index].get_centroid(), axis) < split_pos {
3667 item_index += 1;
3668 } else {
3669 inner_index -= 1;
3670 slice.swap(item_index, inner_index);
3671 idx_slice.swap(item_index, inner_index);
3672 }
3673 }
3674 if item_index == 0 || item_index == count {
3675 start + count / 2
3676 } else {
3677 start + item_index
3678 }
3679 } else if count <= 6 {
3680 return node_idx;
3681 } else {
3682 let extent: Vec3 = cent_bounds.get_max().sub(cent_bounds.get_min());
3683 let axis: usize =
3684 if extent.get_x() >= extent.get_y() && extent.get_x() >= extent.get_z() {
3685 0
3686 } else if extent.get_y() >= extent.get_z() {
3687 1
3688 } else {
3689 2
3690 };
3691 let mid_pos: f32 =
3692 Self::vec_axis(cent_bounds.get_min(), axis) + Self::vec_axis(extent, axis) * 0.5;
3693 let Self {
3694 triangles,
3695 orig_indices,
3696 ..
3697 } = self;
3698 let slice: &mut [BvhTriangle] = &mut triangles[start..end];
3699 let idx_slice: &mut [u32] = &mut orig_indices[start..end];
3700 let mut outer_index: usize = 0;
3701 let mut col_index: usize = slice.len();
3702 while outer_index < col_index {
3703 if Self::vec_axis(slice[outer_index].get_centroid(), axis) < mid_pos {
3704 outer_index += 1;
3705 } else {
3706 col_index -= 1;
3707 slice.swap(outer_index, col_index);
3708 idx_slice.swap(outer_index, col_index);
3709 }
3710 }
3711 if outer_index == 0 || outer_index == count {
3712 start + count / 2
3713 } else {
3714 start + outer_index
3715 }
3716 };
3717 let left_idx: u32 = self.build_node(start, mid);
3718 let right_idx: u32 = self.build_node(mid, end);
3719 let node: &mut BvhNode3D = &mut self.get_mut_nodes()[node_idx as usize];
3720 node.set_left(left_idx);
3721 node.set_right(right_idx);
3722 node.set_tri_count(0);
3723 node_idx
3724 }
3725
3726 #[inline(always)]
3742 #[must_use]
3743 pub fn trace_any_shadow_cached(
3744 &self,
3745 origin: Vec3,
3746 dir: Vec3,
3747 inv_dir: Vec3,
3748 t_min: f32,
3749 t_max: f32,
3750 cached_tri: &mut u32,
3751 ) -> bool {
3752 let Self {
3753 bvh4_nodes,
3754 #[cfg(target_arch = "x86_64")]
3755 bvh8_nodes,
3756 root_min,
3757 root_max,
3758 geom,
3759 leaf_packets,
3760 ..
3761 } = self;
3762 if bvh4_nodes.is_empty() || leaf_packets.is_empty() {
3763 return false;
3764 }
3765 let hint: usize = *cached_tri as usize;
3766 if hint < geom.len() && geom[hint].intersect_any_raw(origin, dir, t_min, t_max) {
3767 return true;
3768 }
3769 let orig_arr: [f32; 3] = [origin.get_x(), origin.get_y(), origin.get_z()];
3770 let dir_arr: [f32; 3] = [dir.get_x(), dir.get_y(), dir.get_z()];
3771 let inv_arr: [f32; 3] = [inv_dir.get_x(), inv_dir.get_y(), inv_dir.get_z()];
3772 let neg_orig_inv: [f32; 3] = [
3773 -orig_arr[0] * inv_arr[0],
3774 -orig_arr[1] * inv_arr[1],
3775 -orig_arr[2] * inv_arr[2],
3776 ];
3777 if !BvhFlatNode::intersect_box_fma(
3778 *root_min,
3779 *root_max,
3780 inv_arr,
3781 neg_orig_inv,
3782 t_min,
3783 t_max,
3784 )
3785 .is_finite()
3786 {
3787 return false;
3788 }
3789 #[cfg(target_arch = "x86_64")]
3790 if !bvh8_nodes.is_empty() && Bvh8Node::has_avx2_fma() {
3791 return unsafe {
3793 self.trace_any_shadow_bvh8_avx2(
3794 orig_arr,
3795 dir_arr,
3796 inv_arr,
3797 neg_orig_inv,
3798 (t_min, t_max),
3799 cached_tri,
3800 )
3801 };
3802 }
3803 let mut stack: [u32; 64] = [0; 64];
3804 let mut sp: usize = 0;
3805 let mut cur_idx: usize = 0;
3806 loop {
3807 let item_count: &Bvh4Node = unsafe { bvh4_nodes.get_unchecked(cur_idx) };
3809 let (_, mut mask): ([f32; 4], u32) =
3810 item_count.intersect_4(inv_arr, neg_orig_inv, t_min, t_max);
3811 let mut next_interior: u32 = u32::MAX;
3812 while mask != 0 {
3813 let char_item: usize = mask.trailing_zeros() as usize;
3814 mask &= mask - 1;
3815 let lc: usize = item_count.leaf_count[char_item] as usize;
3816 if lc > 0 {
3817 let first_pkt: usize = item_count.child[char_item] as usize;
3818 for pkt_i in first_pkt..(first_pkt + lc) {
3819 let pkt: &BvhTri4 = unsafe { leaf_packets.get_unchecked(pkt_i) };
3821 let hit_tri: u32 = pkt.intersect_any_4(orig_arr, dir_arr, t_min, t_max);
3822 if hit_tri != u32::MAX {
3823 *cached_tri = hit_tri;
3824 return true;
3825 }
3826 }
3827 } else if next_interior == u32::MAX {
3828 next_interior = item_count.child[char_item];
3829 } else if sp < 64 {
3830 stack[sp] = item_count.child[char_item];
3831 sp += 1;
3832 }
3833 }
3834 if next_interior != u32::MAX {
3835 cur_idx = next_interior as usize;
3836 continue;
3837 }
3838 if sp == 0 {
3839 break;
3840 }
3841 sp -= 1;
3842 cur_idx = stack[sp] as usize;
3843 }
3844 false
3845 }
3846
3847 #[cfg(target_arch = "x86_64")]
3863 #[target_feature(enable = "avx2", enable = "fma")]
3864 #[must_use]
3865 unsafe fn trace_any_shadow_bvh8_avx2(
3866 &self,
3867 orig_arr: [f32; 3],
3868 dir_arr: [f32; 3],
3869 inv_arr: [f32; 3],
3870 neg_orig_inv: [f32; 3],
3871 t_range: (f32, f32),
3872 cached_tri: &mut u32,
3873 ) -> bool {
3874 let (t_min, t_max): (f32, f32) = t_range;
3875 let Self {
3876 bvh8_nodes,
3877 leaf_packets,
3878 ..
3879 } = self;
3880 if bvh8_nodes.is_empty() {
3881 return false;
3882 }
3883 let mut stack: [u32; 96] = [0; 96];
3884 let mut sp: usize = 0;
3885 let mut cur_idx: usize = 0;
3886 loop {
3887 let item_count: &Bvh8Node = unsafe { bvh8_nodes.get_unchecked(cur_idx) };
3889 let (_, mut mask): ([f32; 8], u32) =
3891 unsafe { item_count.intersect_8_avx2(inv_arr, neg_orig_inv, t_min, t_max) };
3892 let mut next_interior: u32 = u32::MAX;
3893 while mask != 0 {
3894 let char_item: usize = mask.trailing_zeros() as usize;
3895 mask &= mask - 1;
3896 let lc: usize = item_count.leaf_count[char_item] as usize;
3897 if lc > 0 {
3898 let first_pkt: usize = item_count.child[char_item] as usize;
3899 for pkt_i in first_pkt..(first_pkt + lc) {
3900 let pkt: &BvhTri4 = unsafe { leaf_packets.get_unchecked(pkt_i) };
3902 let hit_tri: u32 = pkt.intersect_any_4(orig_arr, dir_arr, t_min, t_max);
3903 if hit_tri != u32::MAX {
3904 *cached_tri = hit_tri;
3905 return true;
3906 }
3907 }
3908 } else if next_interior == u32::MAX {
3909 next_interior = item_count.child[char_item];
3910 } else if sp < 96 {
3911 stack[sp] = item_count.child[char_item];
3912 sp += 1;
3913 }
3914 }
3915 if next_interior != u32::MAX {
3916 cur_idx = next_interior as usize;
3917 continue;
3918 }
3919 if sp == 0 {
3920 break;
3921 }
3922 sp -= 1;
3923 cur_idx = stack[sp] as usize;
3924 }
3925 false
3926 }
3927
3928 #[inline(always)]
3943 #[must_use]
3944 pub fn trace_any_shadow_packet4(
3945 &self,
3946 pkt: &RayPacket4,
3947 mut active_mask: u8,
3948 t_min: f32,
3949 t_max: f32,
3950 cached_tri: &mut u32,
3951 ) -> u8 {
3952 let Self {
3953 bvh4_nodes,
3954 geom,
3955 leaf_packets,
3956 ..
3957 } = self;
3958 if active_mask == 0 || bvh4_nodes.is_empty() || leaf_packets.is_empty() {
3959 return 0;
3960 }
3961 let mut occluded: u8 = 0;
3962 let hint: usize = *cached_tri as usize;
3963 if hint < geom.len() {
3964 let hit_mask: u8 = geom[hint].intersect_any_packet4(pkt, t_min, t_max) & active_mask;
3965 occluded |= hit_mask;
3966 active_mask &= !hit_mask;
3967 if active_mask == 0 {
3968 return occluded;
3969 }
3970 }
3971 if active_mask.count_ones() == 1 {
3972 let lane: usize = active_mask.trailing_zeros() as usize;
3973 if self.trace_any_shadow_cached(
3974 Vec3::new(pkt.ox[lane], pkt.oy[lane], pkt.oz[lane]),
3975 Vec3::new(pkt.dx[lane], pkt.dy[lane], pkt.dz[lane]),
3976 Vec3::new(pkt.ix[lane], pkt.iy[lane], pkt.iz[lane]),
3977 t_min,
3978 t_max,
3979 cached_tri,
3980 ) {
3981 occluded |= 1_u8 << lane;
3982 }
3983 return occluded;
3984 }
3985 let neg_oi: [[f32; 4]; 3] = [
3986 [
3987 -pkt.ox[0] * pkt.ix[0],
3988 -pkt.ox[1] * pkt.ix[1],
3989 -pkt.ox[2] * pkt.ix[2],
3990 -pkt.ox[3] * pkt.ix[3],
3991 ],
3992 [
3993 -pkt.oy[0] * pkt.iy[0],
3994 -pkt.oy[1] * pkt.iy[1],
3995 -pkt.oy[2] * pkt.iy[2],
3996 -pkt.oy[3] * pkt.iy[3],
3997 ],
3998 [
3999 -pkt.oz[0] * pkt.iz[0],
4000 -pkt.oz[1] * pkt.iz[1],
4001 -pkt.oz[2] * pkt.iz[2],
4002 -pkt.oz[3] * pkt.iz[3],
4003 ],
4004 ];
4005 let t_max_4: [f32; 4] = [t_max; 4];
4006 let mut stack: [(u32, u8); 64] = [(0, 0); 64];
4007 let mut sp: usize = 0;
4008 let mut cur_idx: usize = 0;
4009 let mut cur_mask: u8 = active_mask;
4010 loop {
4011 cur_mask &= active_mask;
4012 if cur_mask != 0 {
4013 let item_count: &Bvh4Node = unsafe { bvh4_nodes.get_unchecked(cur_idx) };
4015 let vcnt: usize = item_count.valid_count as usize;
4016 let mut next_interior: (u32, u8) = (u32::MAX, 0);
4017 for char_item in 0..vcnt {
4018 let hit_rays: u8 = item_count
4019 .intersect_child_packet4(char_item, pkt, &neg_oi, t_min, t_max_4)
4020 & cur_mask;
4021 if hit_rays == 0 {
4022 continue;
4023 }
4024 let lc: usize = item_count.leaf_count[char_item] as usize;
4025 if lc > 0 {
4026 let first_pkt: usize = item_count.child[char_item] as usize;
4027 let mut rem_rays: u8 = hit_rays & active_mask;
4028 while rem_rays != 0 {
4029 let red_val: usize = rem_rays.trailing_zeros() as usize;
4030 rem_rays &= rem_rays - 1;
4031 let orig_r: [f32; 3] =
4032 [pkt.ox[red_val], pkt.oy[red_val], pkt.oz[red_val]];
4033 let dir_r: [f32; 3] =
4034 [pkt.dx[red_val], pkt.dy[red_val], pkt.dz[red_val]];
4035 for pkt_i in first_pkt..(first_pkt + lc) {
4036 let tp: &BvhTri4 = unsafe { leaf_packets.get_unchecked(pkt_i) };
4038 let hit_tri: u32 = tp.intersect_any_4(orig_r, dir_r, t_min, t_max);
4039 if hit_tri != u32::MAX {
4040 *cached_tri = hit_tri;
4041 let bit: u8 = 1_u8 << red_val;
4042 occluded |= bit;
4043 active_mask &= !bit;
4044 cur_mask &= !bit;
4045 break;
4046 }
4047 }
4048 }
4049 if active_mask == 0 {
4050 return occluded;
4051 }
4052 } else if next_interior.0 == u32::MAX {
4053 next_interior = (item_count.child[char_item], hit_rays);
4054 } else if sp < 64 {
4055 stack[sp] = (item_count.child[char_item], hit_rays);
4056 sp += 1;
4057 }
4058 }
4059 if next_interior.0 != u32::MAX && (next_interior.1 & active_mask) != 0 {
4060 cur_idx = next_interior.0 as usize;
4061 cur_mask = next_interior.1 & active_mask;
4062 continue;
4063 }
4064 }
4065 let mut popped: bool = false;
4066 while sp > 0 {
4067 sp -= 1;
4068 let (nid, nmask): (u32, u8) = stack[sp];
4069 let live: u8 = nmask & active_mask;
4070 if live != 0 {
4071 cur_idx = nid as usize;
4072 cur_mask = live;
4073 popped = true;
4074 break;
4075 }
4076 }
4077 if !popped {
4078 break;
4079 }
4080 }
4081 occluded
4082 }
4083
4084 #[inline(always)]
4095 pub fn trace_closest_packet4(
4096 &self,
4097 pkt: &RayPacket4,
4098 active_mask: u8,
4099 t_min: f32,
4100 best_t: &mut [f32; 4],
4101 best_idx: &mut [usize; 4],
4102 ) {
4103 let Self {
4104 bvh4_nodes,
4105 leaf_packets,
4106 ..
4107 } = self;
4108 if active_mask == 0 || bvh4_nodes.is_empty() || leaf_packets.is_empty() {
4109 return;
4110 }
4111 if active_mask.count_ones() == 1 {
4112 let red_val: usize = active_mask.trailing_zeros() as usize;
4113 if let Some((blend_factor, idx)) = self.trace_closest_raw(
4114 Vec3::new(pkt.ox[red_val], pkt.oy[red_val], pkt.oz[red_val]),
4115 Vec3::new(pkt.dx[red_val], pkt.dy[red_val], pkt.dz[red_val]),
4116 Vec3::new(pkt.ix[red_val], pkt.iy[red_val], pkt.iz[red_val]),
4117 t_min,
4118 best_t[red_val],
4119 ) {
4120 best_t[red_val] = blend_factor;
4121 best_idx[red_val] = idx;
4122 }
4123 return;
4124 }
4125 let neg_oi: [[f32; 4]; 3] = [
4126 [
4127 -pkt.ox[0] * pkt.ix[0],
4128 -pkt.ox[1] * pkt.ix[1],
4129 -pkt.ox[2] * pkt.ix[2],
4130 -pkt.ox[3] * pkt.ix[3],
4131 ],
4132 [
4133 -pkt.oy[0] * pkt.iy[0],
4134 -pkt.oy[1] * pkt.iy[1],
4135 -pkt.oy[2] * pkt.iy[2],
4136 -pkt.oy[3] * pkt.iy[3],
4137 ],
4138 [
4139 -pkt.oz[0] * pkt.iz[0],
4140 -pkt.oz[1] * pkt.iz[1],
4141 -pkt.oz[2] * pkt.iz[2],
4142 -pkt.oz[3] * pkt.iz[3],
4143 ],
4144 ];
4145 let mut stack: [(u32, u8, f32); 64] = [(0, 0, 0.0); 64];
4146 let mut s_stack: [(u32, f32); 64] = [(0, 0.0); 64];
4147 let mut sp: usize = 0;
4148 let mut cur_idx: usize = 0;
4149 let mut cur_mask: u8 = active_mask;
4150 loop {
4151 if cur_mask.count_ones() == 1 {
4152 let radius_val: usize = cur_mask.trailing_zeros() as usize;
4153 let orig_r: [f32; 3] = [pkt.ox[radius_val], pkt.oy[radius_val], pkt.oz[radius_val]];
4154 let dir_r: [f32; 3] = [pkt.dx[radius_val], pkt.dy[radius_val], pkt.dz[radius_val]];
4155 let inv_r: [f32; 3] = [pkt.ix[radius_val], pkt.iy[radius_val], pkt.iz[radius_val]];
4156 let neg_r: [f32; 3] = [
4157 neg_oi[0][radius_val],
4158 neg_oi[1][radius_val],
4159 neg_oi[2][radius_val],
4160 ];
4161 let mut r_best_t: f32 = best_t[radius_val];
4162 let mut r_best_idx: usize = best_idx[radius_val];
4163 let mut s_sp: usize = 0;
4164 let mut s_cur: usize = cur_idx;
4165 loop {
4166 let sn: &Bvh4Node = unsafe { bvh4_nodes.get_unchecked(s_cur) };
4168 let (t_near, mut smask): ([f32; 4], u32) =
4169 sn.intersect_4(inv_r, neg_r, t_min, r_best_t);
4170 let mut sh_hits: [(u32, f32); 4] = [(0, 0.0); 4];
4171 let mut sh_len: usize = 0;
4172 while smask != 0 {
4173 let char_item: usize = smask.trailing_zeros() as usize;
4174 smask &= smask - 1;
4175 let tc: f32 = t_near[char_item];
4176 if tc >= r_best_t {
4177 continue;
4178 }
4179 let lc: usize = sn.leaf_count[char_item] as usize;
4180 if lc > 0 {
4181 let first_pkt: usize = sn.child[char_item] as usize;
4182 for pkt_i in first_pkt..(first_pkt + lc) {
4183 let tp: &BvhTri4 = unsafe { leaf_packets.get_unchecked(pkt_i) };
4185 tp.intersect_closest_4(
4186 orig_r,
4187 dir_r,
4188 t_min,
4189 &mut r_best_t,
4190 &mut r_best_idx,
4191 );
4192 }
4193 } else {
4194 sh_hits[sh_len] = (sn.child[char_item], tc);
4195 sh_len += 1;
4196 }
4197 }
4198 if sh_len > 0 {
4199 if sh_len > 1 {
4200 for item_index in 1..sh_len {
4201 let key: (u32, f32) = sh_hits[item_index];
4202 let mut inner_index: usize = item_index;
4203 while inner_index > 0 && sh_hits[inner_index - 1].1 < key.1 {
4204 sh_hits[inner_index] = sh_hits[inner_index - 1];
4205 inner_index -= 1;
4206 }
4207 sh_hits[inner_index] = key;
4208 }
4209 }
4210 for hit in sh_hits.iter().take(sh_len - 1) {
4211 if s_sp < 64 {
4212 s_stack[s_sp] = *hit;
4213 s_sp += 1;
4214 }
4215 }
4216 s_cur = sh_hits[sh_len - 1].0 as usize;
4217 continue;
4218 }
4219 let mut popped_single: bool = false;
4220 while s_sp > 0 {
4221 s_sp -= 1;
4222 let (nid, nt): (u32, f32) = s_stack[s_sp];
4223 if nt < r_best_t {
4224 s_cur = nid as usize;
4225 popped_single = true;
4226 break;
4227 }
4228 }
4229 if !popped_single {
4230 break;
4231 }
4232 }
4233 best_t[radius_val] = r_best_t;
4234 best_idx[radius_val] = r_best_idx;
4235 } else {
4236 let item_count: &Bvh4Node = unsafe { bvh4_nodes.get_unchecked(cur_idx) };
4238 let vcnt: usize = item_count.valid_count as usize;
4239 let mut hits: [(u32, u8, f32); 4] = [(0, 0, 0.0); 4];
4240 let mut hit_len: usize = 0;
4241 for color_item in 0..vcnt {
4242 let (raw_rays, min_t): (u8, f32) = item_count
4243 .intersect_child_packet4_near(color_item, pkt, &neg_oi, t_min, *best_t);
4244 let hit_rays: u8 = raw_rays & cur_mask;
4245 if hit_rays == 0 {
4246 continue;
4247 }
4248 let lc: usize = item_count.leaf_count[color_item] as usize;
4249 if lc > 0 {
4250 let first_pkt: usize = item_count.child[color_item] as usize;
4251 let mut rem_rays: u8 = hit_rays;
4252 while rem_rays != 0 {
4253 let rect_val: usize = rem_rays.trailing_zeros() as usize;
4254 rem_rays &= rem_rays - 1;
4255 let orig_r: [f32; 3] =
4256 [pkt.ox[rect_val], pkt.oy[rect_val], pkt.oz[rect_val]];
4257 let dir_r: [f32; 3] =
4258 [pkt.dx[rect_val], pkt.dy[rect_val], pkt.dz[rect_val]];
4259 for pkt_i in first_pkt..(first_pkt + lc) {
4260 let tp: &BvhTri4 = unsafe { leaf_packets.get_unchecked(pkt_i) };
4262 tp.intersect_closest_4(
4263 orig_r,
4264 dir_r,
4265 t_min,
4266 &mut best_t[rect_val],
4267 &mut best_idx[rect_val],
4268 );
4269 }
4270 }
4271 } else {
4272 hits[hit_len] = (item_count.child[color_item], hit_rays, min_t);
4273 hit_len += 1;
4274 }
4275 }
4276 if hit_len > 0 {
4277 if hit_len > 1 {
4278 for outer_index in 1..hit_len {
4279 let key: (u32, u8, f32) = hits[outer_index];
4280 let mut col_index: usize = outer_index;
4281 while col_index > 0 && hits[col_index - 1].2 < key.2 {
4282 hits[col_index] = hits[col_index - 1];
4283 col_index -= 1;
4284 }
4285 hits[col_index] = key;
4286 }
4287 }
4288 for hit in hits.iter().take(hit_len - 1) {
4289 if sp < 64 {
4290 stack[sp] = *hit;
4291 sp += 1;
4292 }
4293 }
4294 let nearest: (u32, u8, f32) = hits[hit_len - 1];
4295 cur_idx = nearest.0 as usize;
4296 cur_mask = nearest.1;
4297 continue;
4298 }
4299 }
4300 let mut found_next: bool = false;
4301 while sp > 0 {
4302 sp -= 1;
4303 let (nid, nmask, nt): (u32, u8, f32) = stack[sp];
4304 let mut max_bt: f32 = 0.0;
4305 let mut matrix_elems: u8 = nmask;
4306 while matrix_elems != 0 {
4307 let row_val: usize = matrix_elems.trailing_zeros() as usize;
4308 matrix_elems &= matrix_elems - 1;
4309 max_bt = max_bt.max(best_t[row_val]);
4310 }
4311 if nt < max_bt {
4312 cur_idx = nid as usize;
4313 cur_mask = nmask;
4314 found_next = true;
4315 break;
4316 }
4317 }
4318 if !found_next {
4319 break;
4320 }
4321 }
4322 }
4323
4324 #[must_use]
4337 pub fn trace_any_shadow(&self, ray: &Ray3D, t_min: f32, t_max: f32) -> bool {
4338 let mut cache: u32 = u32::MAX;
4339 self.trace_any_shadow_cached(
4340 ray.get_origin(),
4341 ray.get_direction(),
4342 ray.get_inv_direction(),
4343 t_min,
4344 t_max,
4345 &mut cache,
4346 )
4347 }
4348
4349 #[inline(always)]
4364 #[must_use]
4365 pub fn trace_closest_raw(
4366 &self,
4367 origin: Vec3,
4368 dir: Vec3,
4369 inv_dir: Vec3,
4370 t_min: f32,
4371 t_max: f32,
4372 ) -> Option<(f32, usize)> {
4373 let Self {
4374 bvh4_nodes,
4375 #[cfg(target_arch = "x86_64")]
4376 bvh8_nodes,
4377 root_min,
4378 root_max,
4379 leaf_packets,
4380 ..
4381 } = self;
4382 if bvh4_nodes.is_empty() || leaf_packets.is_empty() {
4383 return None;
4384 }
4385 let orig_arr: [f32; 3] = [origin.get_x(), origin.get_y(), origin.get_z()];
4386 let dir_arr: [f32; 3] = [dir.get_x(), dir.get_y(), dir.get_z()];
4387 let inv_arr: [f32; 3] = [inv_dir.get_x(), inv_dir.get_y(), inv_dir.get_z()];
4388 let neg_orig_inv: [f32; 3] = [
4389 -orig_arr[0] * inv_arr[0],
4390 -orig_arr[1] * inv_arr[1],
4391 -orig_arr[2] * inv_arr[2],
4392 ];
4393 if !BvhFlatNode::intersect_box_fma(
4394 *root_min,
4395 *root_max,
4396 inv_arr,
4397 neg_orig_inv,
4398 t_min,
4399 t_max,
4400 )
4401 .is_finite()
4402 {
4403 return None;
4404 }
4405 #[cfg(target_arch = "x86_64")]
4406 if !bvh8_nodes.is_empty() && Bvh8Node::has_avx2_fma() {
4407 return unsafe {
4409 self.trace_closest_bvh8_avx2(orig_arr, dir_arr, inv_arr, neg_orig_inv, t_min, t_max)
4410 };
4411 }
4412 let mut stack: [(u32, f32); 64] = [(0, 0.0); 64];
4413 let mut sp: usize = 0;
4414 let mut cur_idx: usize = 0;
4415 let mut best_t: f32 = t_max;
4416 let mut best_idx: usize = usize::MAX;
4417 loop {
4418 let item_count: &Bvh4Node = unsafe { bvh4_nodes.get_unchecked(cur_idx) };
4420 let (t_near, mut mask): ([f32; 4], u32) =
4421 item_count.intersect_4(inv_arr, neg_orig_inv, t_min, best_t);
4422 let mut hits: [(u32, f32); 4] = [(0, 0.0); 4];
4423 let mut hit_len: usize = 0;
4424 while mask != 0 {
4425 let char_item: usize = mask.trailing_zeros() as usize;
4426 mask &= mask - 1;
4427 let tc: f32 = t_near[char_item];
4428 if tc >= best_t {
4429 continue;
4430 }
4431 let lc: usize = item_count.leaf_count[char_item] as usize;
4432 if lc > 0 {
4433 let first_pkt: usize = item_count.child[char_item] as usize;
4434 for pkt_i in first_pkt..(first_pkt + lc) {
4435 let pkt: &BvhTri4 = unsafe { leaf_packets.get_unchecked(pkt_i) };
4437 pkt.intersect_closest_4(
4438 orig_arr,
4439 dir_arr,
4440 t_min,
4441 &mut best_t,
4442 &mut best_idx,
4443 );
4444 }
4445 } else {
4446 hits[hit_len] = (item_count.child[char_item], tc);
4447 hit_len += 1;
4448 }
4449 }
4450 if hit_len > 0 {
4451 if hit_len > 1 {
4452 for item_index in 1..hit_len {
4453 let key: (u32, f32) = hits[item_index];
4454 let mut inner_index: usize = item_index;
4455 while inner_index > 0 && hits[inner_index - 1].1 < key.1 {
4456 hits[inner_index] = hits[inner_index - 1];
4457 inner_index -= 1;
4458 }
4459 hits[inner_index] = key;
4460 }
4461 }
4462 let near_last: usize = hit_len - 1;
4463 for item in hits.iter().take(near_last) {
4464 if item.1 < best_t && sp < 64 {
4465 stack[sp] = *item;
4466 sp += 1;
4467 }
4468 }
4469 if hits[near_last].1 < best_t {
4470 cur_idx = hits[near_last].0 as usize;
4471 continue;
4472 }
4473 }
4474 let mut found_next: bool = false;
4475 while sp > 0 {
4476 sp -= 1;
4477 let (next_id, next_tmin): (u32, f32) = stack[sp];
4478 if next_tmin < best_t {
4479 cur_idx = next_id as usize;
4480 found_next = true;
4481 break;
4482 }
4483 }
4484 if !found_next {
4485 break;
4486 }
4487 }
4488 if best_idx == usize::MAX {
4489 None
4490 } else {
4491 Some((best_t, best_idx))
4492 }
4493 }
4494
4495 #[cfg(target_arch = "x86_64")]
4511 #[target_feature(enable = "avx2", enable = "fma")]
4512 #[must_use]
4513 unsafe fn trace_closest_bvh8_avx2(
4514 &self,
4515 orig_arr: [f32; 3],
4516 dir_arr: [f32; 3],
4517 inv_arr: [f32; 3],
4518 neg_orig_inv: [f32; 3],
4519 t_min: f32,
4520 t_max: f32,
4521 ) -> Option<(f32, usize)> {
4522 let Self {
4523 bvh8_nodes,
4524 leaf_packets,
4525 ..
4526 } = self;
4527 if bvh8_nodes.is_empty() {
4528 return None;
4529 }
4530 let mut stack: [(u32, f32); 96] = [(0, 0.0); 96];
4531 let mut sp: usize = 0;
4532 let mut cur_idx: usize = 0;
4533 let mut best_t: f32 = t_max;
4534 let mut best_idx: usize = usize::MAX;
4535 loop {
4536 let item_count: &Bvh8Node = unsafe { bvh8_nodes.get_unchecked(cur_idx) };
4538 let (t_near, mut mask): ([f32; 8], u32) =
4540 unsafe { item_count.intersect_8_avx2(inv_arr, neg_orig_inv, t_min, best_t) };
4541 let mut hits: [(u32, f32); 8] = [(0, 0.0); 8];
4542 let mut hit_len: usize = 0;
4543 while mask != 0 {
4544 let char_item: usize = mask.trailing_zeros() as usize;
4545 mask &= mask - 1;
4546 let tc: f32 = t_near[char_item];
4547 if tc >= best_t {
4548 continue;
4549 }
4550 let lc: usize = item_count.leaf_count[char_item] as usize;
4551 if lc > 0 {
4552 let first_pkt: usize = item_count.child[char_item] as usize;
4553 for pkt_i in first_pkt..(first_pkt + lc) {
4554 let pkt: &BvhTri4 = unsafe { leaf_packets.get_unchecked(pkt_i) };
4556 pkt.intersect_closest_4(
4557 orig_arr,
4558 dir_arr,
4559 t_min,
4560 &mut best_t,
4561 &mut best_idx,
4562 );
4563 }
4564 } else {
4565 hits[hit_len] = (item_count.child[char_item], tc);
4566 hit_len += 1;
4567 }
4568 }
4569 if hit_len > 0 {
4570 if hit_len > 1 {
4571 for item_index in 1..hit_len {
4572 let key: (u32, f32) = hits[item_index];
4573 let mut inner_index: usize = item_index;
4574 while inner_index > 0 && hits[inner_index - 1].1 < key.1 {
4575 hits[inner_index] = hits[inner_index - 1];
4576 inner_index -= 1;
4577 }
4578 hits[inner_index] = key;
4579 }
4580 }
4581 let near_last: usize = hit_len - 1;
4582 for item in hits.iter().take(near_last) {
4583 if item.1 < best_t && sp < 96 {
4584 stack[sp] = *item;
4585 sp += 1;
4586 }
4587 }
4588 if hits[near_last].1 < best_t {
4589 cur_idx = hits[near_last].0 as usize;
4590 continue;
4591 }
4592 }
4593 let mut found_next: bool = false;
4594 while sp > 0 {
4595 sp -= 1;
4596 let (next_id, next_tmin): (u32, f32) = stack[sp];
4597 if next_tmin < best_t {
4598 cur_idx = next_id as usize;
4599 found_next = true;
4600 break;
4601 }
4602 }
4603 if !found_next {
4604 break;
4605 }
4606 }
4607 if best_idx == usize::MAX {
4608 None
4609 } else {
4610 Some((best_t, best_idx))
4611 }
4612 }
4613
4614 #[must_use]
4627 pub fn trace_closest(&self, ray: &Ray3D, t_min: f32, t_max: f32) -> Option<RayHit3D> {
4628 let (blend_factor, idx): (f32, usize) = self.trace_closest_raw(
4629 ray.get_origin(),
4630 ray.get_direction(),
4631 ray.get_inv_direction(),
4632 t_min,
4633 t_max,
4634 )?;
4635 let tri: &BvhTriangle = &self.get_triangles()[idx];
4636 let vec_value: Vec3 = ray
4637 .get_origin()
4638 .add(ray.get_direction().scale(blend_factor));
4639 Some(RayHit3D {
4640 distance: blend_factor,
4641 point: vec_value,
4642 normal: tri.get_normal(),
4643 material: tri.get_material(),
4644 })
4645 }
4646}