use super::*;
impl BvhGeomTri {
#[must_use]
pub(crate) fn from_triangle(tri: &BvhTriangle) -> Self {
let v0: Vec3 = tri.get_v0();
let e1: Vec3 = tri.get_edge1();
let e2: Vec3 = tri.get_edge2();
Self {
v0: [v0.get_x(), v0.get_y(), v0.get_z()],
e1: [e1.get_x(), e1.get_y(), e1.get_z()],
e2: [e2.get_x(), e2.get_y(), e2.get_z()],
}
}
#[inline(always)]
#[must_use]
pub(crate) fn intersect_raw(
&self,
origin: Vec3,
dir: Vec3,
t_min: f32,
t_max: f32,
) -> Option<f32> {
let Self { v0, e1, e2 } = self;
let dx: f32 = dir.get_x();
let dy: f32 = dir.get_y();
let dz: f32 = dir.get_z();
let hx: f32 = dy * e2[2] - dz * e2[1];
let hy: f32 = dz * e2[0] - dx * e2[2];
let hz: f32 = dx * e2[1] - dy * e2[0];
let det: f32 = e1[0] * hx + e1[1] * hy + e1[2] * hz;
let sx: f32 = origin.get_x() - v0[0];
let sy: f32 = origin.get_y() - v0[1];
let sz: f32 = origin.get_z() - v0[2];
if det > 1e-6 {
let u_raw: f32 = sx * hx + sy * hy + sz * hz;
if u_raw < 0.0 || u_raw > det {
return None;
}
let qx: f32 = sy * e1[2] - sz * e1[1];
let qy: f32 = sz * e1[0] - sx * e1[2];
let qz: f32 = sx * e1[1] - sy * e1[0];
let v_raw: f32 = dx * qx + dy * qy + dz * qz;
if v_raw < 0.0 || u_raw + v_raw > det {
return None;
}
let t_raw: f32 = e2[0] * qx + e2[1] * qy + e2[2] * qz;
if t_raw <= t_min * det || t_raw >= t_max * det {
return None;
}
Some(t_raw / det)
} else if det < -1e-6 {
let u_raw: f32 = sx * hx + sy * hy + sz * hz;
if u_raw > 0.0 || u_raw < det {
return None;
}
let qx: f32 = sy * e1[2] - sz * e1[1];
let qy: f32 = sz * e1[0] - sx * e1[2];
let qz: f32 = sx * e1[1] - sy * e1[0];
let v_raw: f32 = dx * qx + dy * qy + dz * qz;
if v_raw > 0.0 || u_raw + v_raw < det {
return None;
}
let t_raw: f32 = e2[0] * qx + e2[1] * qy + e2[2] * qz;
if t_raw >= t_min * det || t_raw <= t_max * det {
return None;
}
Some(t_raw / det)
} else {
None
}
}
#[inline(always)]
#[must_use]
pub(crate) fn intersect_any_raw(
&self,
origin: Vec3,
dir: Vec3,
t_min: f32,
t_max: f32,
) -> bool {
let Self { v0, e1, e2 } = self;
let dx: f32 = dir.get_x();
let dy: f32 = dir.get_y();
let dz: f32 = dir.get_z();
let hx: f32 = dy * e2[2] - dz * e2[1];
let hy: f32 = dz * e2[0] - dx * e2[2];
let hz: f32 = dx * e2[1] - dy * e2[0];
let det: f32 = e1[0] * hx + e1[1] * hy + e1[2] * hz;
let sx: f32 = origin.get_x() - v0[0];
let sy: f32 = origin.get_y() - v0[1];
let sz: f32 = origin.get_z() - v0[2];
if det > 1e-6 {
let u_raw: f32 = sx * hx + sy * hy + sz * hz;
if u_raw < 0.0 || u_raw > det {
return false;
}
let qx: f32 = sy * e1[2] - sz * e1[1];
let qy: f32 = sz * e1[0] - sx * e1[2];
let qz: f32 = sx * e1[1] - sy * e1[0];
let v_raw: f32 = dx * qx + dy * qy + dz * qz;
if v_raw < 0.0 || u_raw + v_raw > det {
return false;
}
let t_raw: f32 = e2[0] * qx + e2[1] * qy + e2[2] * qz;
t_raw > t_min * det && t_raw < t_max * det
} else if det < -1e-6 {
let u_raw: f32 = sx * hx + sy * hy + sz * hz;
if u_raw > 0.0 || u_raw < det {
return false;
}
let qx: f32 = sy * e1[2] - sz * e1[1];
let qy: f32 = sz * e1[0] - sx * e1[2];
let qz: f32 = sx * e1[1] - sy * e1[0];
let v_raw: f32 = dx * qx + dy * qy + dz * qz;
if v_raw > 0.0 || u_raw + v_raw < det {
return false;
}
let t_raw: f32 = e2[0] * qx + e2[1] * qy + e2[2] * qz;
t_raw < t_min * det && t_raw > t_max * det
} else {
false
}
}
#[inline(always)]
#[must_use]
pub(crate) fn intersect_any_packet4(&self, pkt: &RayPacket4, t_min: f32, t_max: f32) -> u8 {
#[cfg(target_arch = "aarch64")]
{
let Self { v0, e1, e2 } = self;
unsafe {
let ox: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(pkt.ox.as_ptr());
let oy: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(pkt.oy.as_ptr());
let oz: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(pkt.oz.as_ptr());
let dx: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(pkt.dx.as_ptr());
let dy: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(pkt.dy.as_ptr());
let dz: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(pkt.dz.as_ptr());
let e1x: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(e1[0]);
let e1y: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(e1[1]);
let e1z: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(e1[2]);
let e2x: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(e2[0]);
let e2y: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(e2[1]);
let e2z: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(e2[2]);
let hx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(dy, e2z),
dz,
e2y,
);
let hy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(dz, e2x),
dx,
e2z,
);
let hz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(dx, e2y),
dy,
e2x,
);
let det: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vmulq_f32(e1z, hz),
e1y,
hy,
),
e1x,
hx,
);
let sign_bits: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
core::arch::aarch64::vreinterpretq_u32_f32(det),
core::arch::aarch64::vdupq_n_u32(0x8000_0000),
);
let ad: core::arch::aarch64::float32x4_t = core::arch::aarch64::vabsq_f32(det);
let sx: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vsubq_f32(ox, core::arch::aarch64::vdupq_n_f32(v0[0]));
let sy: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vsubq_f32(oy, core::arch::aarch64::vdupq_n_f32(v0[1]));
let sz: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vsubq_f32(oz, core::arch::aarch64::vdupq_n_f32(v0[2]));
let u_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vfmaq_f32(core::arch::aarch64::vmulq_f32(sz, hz), sy, hy),
sx,
hx,
);
let su: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
core::arch::aarch64::vreinterpretq_u32_f32(u_raw),
sign_bits,
));
let qx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(sy, e1z),
sz,
e1y,
);
let qy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(sz, e1x),
sx,
e1z,
);
let qz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(sx, e1y),
sy,
e1x,
);
let v_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vfmaq_f32(core::arch::aarch64::vmulq_f32(dz, qz), dy, qy),
dx,
qx,
);
let sv: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
core::arch::aarch64::vreinterpretq_u32_f32(v_raw),
sign_bits,
));
let t_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vmulq_f32(e2z, qz),
e2y,
qy,
),
e2x,
qx,
);
let st: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
core::arch::aarch64::vreinterpretq_u32_f32(t_raw),
sign_bits,
));
let zero: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(0.0);
let m0: core::arch::aarch64::uint32x4_t =
core::arch::aarch64::vcgtq_f32(ad, core::arch::aarch64::vdupq_n_f32(1e-6));
let m1: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgeq_f32(su, zero);
let m2: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgeq_f32(sv, zero);
let m3: core::arch::aarch64::uint32x4_t =
core::arch::aarch64::vcleq_f32(core::arch::aarch64::vaddq_f32(su, sv), ad);
let m4: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgtq_f32(
st,
core::arch::aarch64::vmulq_f32(core::arch::aarch64::vdupq_n_f32(t_min), ad),
);
let m5: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcltq_f32(
st,
core::arch::aarch64::vmulq_f32(core::arch::aarch64::vdupq_n_f32(t_max), ad),
);
let hit_v: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
core::arch::aarch64::vandq_u32(
core::arch::aarch64::vandq_u32(m0, m1),
core::arch::aarch64::vandq_u32(m2, m3),
),
core::arch::aarch64::vandq_u32(m4, m5),
);
let lane_bits: [u32; 4] = [1, 2, 4, 8];
let mask: u32 = core::arch::aarch64::vaddvq_u32(core::arch::aarch64::vandq_u32(
hit_v,
core::arch::aarch64::vld1q_u32(lane_bits.as_ptr()),
));
mask as u8
}
}
#[cfg(not(target_arch = "aarch64"))]
{
let mut mask: u8 = 0;
for lane in 0..4 {
if self.intersect_any_raw(
Vec3::new(pkt.ox[lane], pkt.oy[lane], pkt.oz[lane]),
Vec3::new(pkt.dx[lane], pkt.dy[lane], pkt.dz[lane]),
t_min,
t_max,
) {
mask |= 1_u8 << lane;
}
}
mask
}
}
}
impl BvhTri4 {
#[must_use]
pub(crate) fn from_geom_chunk(slice: &[BvhGeomTri], base_tri_idx: u32) -> Self {
let mut v0x: [f32; 4] = [0.0; 4];
let mut v0y: [f32; 4] = [0.0; 4];
let mut v0z: [f32; 4] = [0.0; 4];
let mut e1x: [f32; 4] = [0.0; 4];
let mut e1y: [f32; 4] = [0.0; 4];
let mut e1z: [f32; 4] = [0.0; 4];
let mut e2x: [f32; 4] = [0.0; 4];
let mut e2y: [f32; 4] = [0.0; 4];
let mut e2z: [f32; 4] = [0.0; 4];
let mut tri_indices: [u32; 4] = [u32::MAX; 4];
for (lane, tri) in slice.iter().take(4).enumerate() {
v0x[lane] = tri.v0[0];
v0y[lane] = tri.v0[1];
v0z[lane] = tri.v0[2];
e1x[lane] = tri.e1[0];
e1y[lane] = tri.e1[1];
e1z[lane] = tri.e1[2];
e2x[lane] = tri.e2[0];
e2y[lane] = tri.e2[1];
e2z[lane] = tri.e2[2];
tri_indices[lane] = base_tri_idx.wrapping_add(lane as u32);
}
Self {
v0x,
v0y,
v0z,
e1x,
e1y,
e1z,
e2x,
e2y,
e2z,
tri_indices,
}
}
#[inline(always)]
#[must_use]
pub(crate) fn intersect_any_4(
&self,
orig: [f32; 3],
dir: [f32; 3],
t_min: f32,
t_max: f32,
) -> u32 {
let Self {
v0x,
v0y,
v0z,
e1x,
e1y,
e1z,
e2x,
e2y,
e2z,
tri_indices,
} = self;
#[cfg(target_arch = "aarch64")]
{
let mask: u32 = unsafe {
let ox: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vdupq_n_f32(orig[0]);
let oy: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vdupq_n_f32(orig[1]);
let oz: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vdupq_n_f32(orig[2]);
let dx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(dir[0]);
let dy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(dir[1]);
let dz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(dir[2]);
let ve2x: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(e2x.as_ptr());
let ve2y: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(e2y.as_ptr());
let ve2z: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(e2z.as_ptr());
let hx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(dy, ve2z),
dz,
ve2y,
);
let hy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(dz, ve2x),
dx,
ve2z,
);
let hz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(dx, ve2y),
dy,
ve2x,
);
let ve1x: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(e1x.as_ptr());
let ve1y: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(e1y.as_ptr());
let ve1z: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(e1z.as_ptr());
let det: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vmulq_f32(ve1z, hz),
ve1y,
hy,
),
ve1x,
hx,
);
let sign_bits: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
core::arch::aarch64::vreinterpretq_u32_f32(det),
core::arch::aarch64::vdupq_n_u32(0x8000_0000),
);
let ad: core::arch::aarch64::float32x4_t = core::arch::aarch64::vabsq_f32(det);
let sx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vsubq_f32(
ox,
core::arch::aarch64::vld1q_f32(v0x.as_ptr()),
);
let sy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vsubq_f32(
oy,
core::arch::aarch64::vld1q_f32(v0y.as_ptr()),
);
let sz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vsubq_f32(
oz,
core::arch::aarch64::vld1q_f32(v0z.as_ptr()),
);
let u_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vfmaq_f32(core::arch::aarch64::vmulq_f32(sz, hz), sy, hy),
sx,
hx,
);
let su: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
core::arch::aarch64::vreinterpretq_u32_f32(u_raw),
sign_bits,
));
let qx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(sy, ve1z),
sz,
ve1y,
);
let qy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(sz, ve1x),
sx,
ve1z,
);
let qz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(sx, ve1y),
sy,
ve1x,
);
let v_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vfmaq_f32(core::arch::aarch64::vmulq_f32(dz, qz), dy, qy),
dx,
qx,
);
let sv: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
core::arch::aarch64::vreinterpretq_u32_f32(v_raw),
sign_bits,
));
let t_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vmulq_f32(ve2z, qz),
ve2y,
qy,
),
ve2x,
qx,
);
let st: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
core::arch::aarch64::vreinterpretq_u32_f32(t_raw),
sign_bits,
));
let zero: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(0.0);
let m0: core::arch::aarch64::uint32x4_t =
core::arch::aarch64::vcgtq_f32(ad, core::arch::aarch64::vdupq_n_f32(1e-6));
let m1: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgeq_f32(su, zero);
let m2: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgeq_f32(sv, zero);
let m3: core::arch::aarch64::uint32x4_t =
core::arch::aarch64::vcleq_f32(core::arch::aarch64::vaddq_f32(su, sv), ad);
let m4: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgtq_f32(
st,
core::arch::aarch64::vmulq_f32(core::arch::aarch64::vdupq_n_f32(t_min), ad),
);
let m5: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcltq_f32(
st,
core::arch::aarch64::vmulq_f32(core::arch::aarch64::vdupq_n_f32(t_max), ad),
);
let hit_v: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
core::arch::aarch64::vandq_u32(
core::arch::aarch64::vandq_u32(m0, m1),
core::arch::aarch64::vandq_u32(m2, m3),
),
core::arch::aarch64::vandq_u32(m4, m5),
);
let lane_bits: [u32; 4] = [1, 2, 4, 8];
core::arch::aarch64::vaddvq_u32(core::arch::aarch64::vandq_u32(
hit_v,
core::arch::aarch64::vld1q_u32(lane_bits.as_ptr()),
))
};
if mask != 0 {
tri_indices[mask.trailing_zeros() as usize]
} else {
u32::MAX
}
}
#[cfg(target_arch = "x86_64")]
{
let mask: i32 = unsafe {
let ox: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(orig[0]);
let oy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(orig[1]);
let oz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(orig[2]);
let dx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(dir[0]);
let dy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(dir[1]);
let dz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(dir[2]);
let ve2x: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_load_ps(e2x.as_ptr());
let ve2y: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_load_ps(e2y.as_ptr());
let ve2z: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_load_ps(e2z.as_ptr());
let hx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
core::arch::x86_64::_mm_mul_ps(dy, ve2z),
core::arch::x86_64::_mm_mul_ps(dz, ve2y),
);
let hy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
core::arch::x86_64::_mm_mul_ps(dz, ve2x),
core::arch::x86_64::_mm_mul_ps(dx, ve2z),
);
let hz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
core::arch::x86_64::_mm_mul_ps(dx, ve2y),
core::arch::x86_64::_mm_mul_ps(dy, ve2x),
);
let ve1x: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_load_ps(e1x.as_ptr());
let ve1y: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_load_ps(e1y.as_ptr());
let ve1z: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_load_ps(e1z.as_ptr());
let det: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(ve1x, hx),
core::arch::x86_64::_mm_mul_ps(ve1y, hy),
),
core::arch::x86_64::_mm_mul_ps(ve1z, hz),
);
let sign_mask: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(-0.0);
let sign_bits: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_and_ps(det, sign_mask);
let ad: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_andnot_ps(sign_mask, det);
let sx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
ox,
core::arch::x86_64::_mm_load_ps(v0x.as_ptr()),
);
let sy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
oy,
core::arch::x86_64::_mm_load_ps(v0y.as_ptr()),
);
let sz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
oz,
core::arch::x86_64::_mm_load_ps(v0z.as_ptr()),
);
let u_raw: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(sx, hx),
core::arch::x86_64::_mm_mul_ps(sy, hy),
),
core::arch::x86_64::_mm_mul_ps(sz, hz),
);
let su: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_xor_ps(u_raw, sign_bits);
let qx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
core::arch::x86_64::_mm_mul_ps(sy, ve1z),
core::arch::x86_64::_mm_mul_ps(sz, ve1y),
);
let qy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
core::arch::x86_64::_mm_mul_ps(sz, ve1x),
core::arch::x86_64::_mm_mul_ps(sx, ve1z),
);
let qz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
core::arch::x86_64::_mm_mul_ps(sx, ve1y),
core::arch::x86_64::_mm_mul_ps(sy, ve1x),
);
let v_raw: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(dx, qx),
core::arch::x86_64::_mm_mul_ps(dy, qy),
),
core::arch::x86_64::_mm_mul_ps(dz, qz),
);
let sv: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_xor_ps(v_raw, sign_bits);
let t_raw: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(ve2x, qx),
core::arch::x86_64::_mm_mul_ps(ve2y, qy),
),
core::arch::x86_64::_mm_mul_ps(ve2z, qz),
);
let st: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_xor_ps(t_raw, sign_bits);
let zero: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_setzero_ps();
let m0: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_cmpgt_ps(ad, core::arch::x86_64::_mm_set1_ps(1e-6));
let m1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmpge_ps(su, zero);
let m2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmpge_ps(sv, zero);
let m3: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_cmple_ps(core::arch::x86_64::_mm_add_ps(su, sv), ad);
let m4: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmpgt_ps(
st,
core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(t_min), ad),
);
let m5: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmplt_ps(
st,
core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(t_max), ad),
);
let hit_v: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_and_ps(
core::arch::x86_64::_mm_and_ps(
core::arch::x86_64::_mm_and_ps(m0, m1),
core::arch::x86_64::_mm_and_ps(m2, m3),
),
core::arch::x86_64::_mm_and_ps(m4, m5),
);
core::arch::x86_64::_mm_movemask_ps(hit_v)
};
if mask != 0 {
tri_indices[(mask as u32).trailing_zeros() as usize]
} else {
u32::MAX
}
}
#[cfg(not(any(target_arch = "aarch64", target_arch = "x86_64")))]
{
let ox: f32 = orig[0];
let oy: f32 = orig[1];
let oz: f32 = orig[2];
let dx: f32 = dir[0];
let dy: f32 = dir[1];
let dz: f32 = dir[2];
for i in 0..4 {
let hx: f32 = dy.mul_add(e2z[i], -(dz * e2y[i]));
let hy: f32 = dz.mul_add(e2x[i], -(dx * e2z[i]));
let hz: f32 = dx.mul_add(e2y[i], -(dy * e2x[i]));
let det: f32 = e1x[i].mul_add(hx, e1y[i].mul_add(hy, e1z[i] * hz));
let sign: f32 = 1.0_f32.copysign(det);
let ad: f32 = det.abs();
let sx: f32 = ox - v0x[i];
let sy: f32 = oy - v0y[i];
let sz: f32 = oz - v0z[i];
let su: f32 = sx.mul_add(hx, sy.mul_add(hy, sz * hz)) * sign;
let qx: f32 = sy.mul_add(e1z[i], -(sz * e1y[i]));
let qy: f32 = sz.mul_add(e1x[i], -(sx * e1z[i]));
let qz: f32 = sx.mul_add(e1y[i], -(sy * e1x[i]));
let sv: f32 = dx.mul_add(qx, dy.mul_add(qy, dz * qz)) * sign;
let st: f32 = e2x[i].mul_add(qx, e2y[i].mul_add(qy, e2z[i] * qz)) * sign;
let uv_ok: bool = su >= 0.0 && sv >= 0.0 && (su + sv) <= ad;
let t_ok: bool = st > t_min * ad && st < t_max * ad;
if ad > 1e-6 && uv_ok && t_ok {
return tri_indices[i];
}
}
u32::MAX
}
}
#[inline(always)]
pub(crate) fn intersect_closest_4(
&self,
orig: [f32; 3],
dir: [f32; 3],
t_min: f32,
best_t: &mut f32,
best_idx: &mut usize,
) {
let Self {
v0x,
v0y,
v0z,
e1x,
e1y,
e1z,
e2x,
e2y,
e2z,
tri_indices,
} = self;
let cur_max: f32 = *best_t;
#[cfg(target_arch = "aarch64")]
{
let mut ad_arr: [f32; 4] = [0.0; 4];
let mut st_arr: [f32; 4] = [0.0; 4];
let mask: u32 = unsafe {
let ox: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vdupq_n_f32(orig[0]);
let oy: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vdupq_n_f32(orig[1]);
let oz: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vdupq_n_f32(orig[2]);
let dx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(dir[0]);
let dy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(dir[1]);
let dz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(dir[2]);
let ve2x: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(e2x.as_ptr());
let ve2y: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(e2y.as_ptr());
let ve2z: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(e2z.as_ptr());
let hx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(dy, ve2z),
dz,
ve2y,
);
let hy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(dz, ve2x),
dx,
ve2z,
);
let hz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(dx, ve2y),
dy,
ve2x,
);
let ve1x: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(e1x.as_ptr());
let ve1y: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(e1y.as_ptr());
let ve1z: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(e1z.as_ptr());
let det: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vmulq_f32(ve1z, hz),
ve1y,
hy,
),
ve1x,
hx,
);
let sign_bits: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
core::arch::aarch64::vreinterpretq_u32_f32(det),
core::arch::aarch64::vdupq_n_u32(0x8000_0000),
);
let ad: core::arch::aarch64::float32x4_t = core::arch::aarch64::vabsq_f32(det);
let sx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vsubq_f32(
ox,
core::arch::aarch64::vld1q_f32(v0x.as_ptr()),
);
let sy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vsubq_f32(
oy,
core::arch::aarch64::vld1q_f32(v0y.as_ptr()),
);
let sz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vsubq_f32(
oz,
core::arch::aarch64::vld1q_f32(v0z.as_ptr()),
);
let u_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vfmaq_f32(core::arch::aarch64::vmulq_f32(sz, hz), sy, hy),
sx,
hx,
);
let su: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
core::arch::aarch64::vreinterpretq_u32_f32(u_raw),
sign_bits,
));
let qx: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(sy, ve1z),
sz,
ve1y,
);
let qy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(sz, ve1x),
sx,
ve1z,
);
let qz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmsq_f32(
core::arch::aarch64::vmulq_f32(sx, ve1y),
sy,
ve1x,
);
let v_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vfmaq_f32(core::arch::aarch64::vmulq_f32(dz, qz), dy, qy),
dx,
qx,
);
let sv: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
core::arch::aarch64::vreinterpretq_u32_f32(v_raw),
sign_bits,
));
let t_raw: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vmulq_f32(ve2z, qz),
ve2y,
qy,
),
ve2x,
qx,
);
let st: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vreinterpretq_f32_u32(core::arch::aarch64::veorq_u32(
core::arch::aarch64::vreinterpretq_u32_f32(t_raw),
sign_bits,
));
let zero: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(0.0);
let m0: core::arch::aarch64::uint32x4_t =
core::arch::aarch64::vcgtq_f32(ad, core::arch::aarch64::vdupq_n_f32(1e-6));
let m1: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgeq_f32(su, zero);
let m2: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgeq_f32(sv, zero);
let m3: core::arch::aarch64::uint32x4_t =
core::arch::aarch64::vcleq_f32(core::arch::aarch64::vaddq_f32(su, sv), ad);
let m4: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcgtq_f32(
st,
core::arch::aarch64::vmulq_f32(core::arch::aarch64::vdupq_n_f32(t_min), ad),
);
let m5: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vcltq_f32(
st,
core::arch::aarch64::vmulq_f32(core::arch::aarch64::vdupq_n_f32(cur_max), ad),
);
let hit_v: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
core::arch::aarch64::vandq_u32(
core::arch::aarch64::vandq_u32(m0, m1),
core::arch::aarch64::vandq_u32(m2, m3),
),
core::arch::aarch64::vandq_u32(m4, m5),
);
let lane_bits: [u32; 4] = [1, 2, 4, 8];
let hmask: u32 = core::arch::aarch64::vaddvq_u32(core::arch::aarch64::vandq_u32(
hit_v,
core::arch::aarch64::vld1q_u32(lane_bits.as_ptr()),
));
if hmask != 0 {
core::arch::aarch64::vst1q_f32(ad_arr.as_mut_ptr(), ad);
core::arch::aarch64::vst1q_f32(st_arr.as_mut_ptr(), st);
}
hmask
};
let mut rem: u32 = mask;
while rem != 0 {
let i: usize = rem.trailing_zeros() as usize;
rem &= rem - 1;
let t: f32 = st_arr[i] / ad_arr[i];
if t < *best_t {
*best_t = t;
*best_idx = tri_indices[i] as usize;
}
}
}
#[cfg(target_arch = "x86_64")]
{
let mut ad_arr: [f32; 4] = [0.0; 4];
let mut st_arr: [f32; 4] = [0.0; 4];
let mask: i32 = unsafe {
let ox: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(orig[0]);
let oy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(orig[1]);
let oz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(orig[2]);
let dx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(dir[0]);
let dy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(dir[1]);
let dz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(dir[2]);
let ve2x: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_load_ps(e2x.as_ptr());
let ve2y: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_load_ps(e2y.as_ptr());
let ve2z: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_load_ps(e2z.as_ptr());
let hx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
core::arch::x86_64::_mm_mul_ps(dy, ve2z),
core::arch::x86_64::_mm_mul_ps(dz, ve2y),
);
let hy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
core::arch::x86_64::_mm_mul_ps(dz, ve2x),
core::arch::x86_64::_mm_mul_ps(dx, ve2z),
);
let hz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
core::arch::x86_64::_mm_mul_ps(dx, ve2y),
core::arch::x86_64::_mm_mul_ps(dy, ve2x),
);
let ve1x: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_load_ps(e1x.as_ptr());
let ve1y: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_load_ps(e1y.as_ptr());
let ve1z: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_load_ps(e1z.as_ptr());
let det: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(ve1x, hx),
core::arch::x86_64::_mm_mul_ps(ve1y, hy),
),
core::arch::x86_64::_mm_mul_ps(ve1z, hz),
);
let sign_mask: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(-0.0);
let sign_bits: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_and_ps(det, sign_mask);
let ad: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_andnot_ps(sign_mask, det);
let sx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
ox,
core::arch::x86_64::_mm_load_ps(v0x.as_ptr()),
);
let sy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
oy,
core::arch::x86_64::_mm_load_ps(v0y.as_ptr()),
);
let sz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
oz,
core::arch::x86_64::_mm_load_ps(v0z.as_ptr()),
);
let u_raw: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(sx, hx),
core::arch::x86_64::_mm_mul_ps(sy, hy),
),
core::arch::x86_64::_mm_mul_ps(sz, hz),
);
let su: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_xor_ps(u_raw, sign_bits);
let qx: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
core::arch::x86_64::_mm_mul_ps(sy, ve1z),
core::arch::x86_64::_mm_mul_ps(sz, ve1y),
);
let qy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
core::arch::x86_64::_mm_mul_ps(sz, ve1x),
core::arch::x86_64::_mm_mul_ps(sx, ve1z),
);
let qz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_sub_ps(
core::arch::x86_64::_mm_mul_ps(sx, ve1y),
core::arch::x86_64::_mm_mul_ps(sy, ve1x),
);
let v_raw: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(dx, qx),
core::arch::x86_64::_mm_mul_ps(dy, qy),
),
core::arch::x86_64::_mm_mul_ps(dz, qz),
);
let sv: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_xor_ps(v_raw, sign_bits);
let t_raw: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(ve2x, qx),
core::arch::x86_64::_mm_mul_ps(ve2y, qy),
),
core::arch::x86_64::_mm_mul_ps(ve2z, qz),
);
let st: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_xor_ps(t_raw, sign_bits);
let zero: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_setzero_ps();
let m0: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_cmpgt_ps(ad, core::arch::x86_64::_mm_set1_ps(1e-6));
let m1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmpge_ps(su, zero);
let m2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmpge_ps(sv, zero);
let m3: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_cmple_ps(core::arch::x86_64::_mm_add_ps(su, sv), ad);
let m4: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmpgt_ps(
st,
core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(t_min), ad),
);
let m5: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_cmplt_ps(
st,
core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(cur_max), ad),
);
let hit_v: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_and_ps(
core::arch::x86_64::_mm_and_ps(
core::arch::x86_64::_mm_and_ps(m0, m1),
core::arch::x86_64::_mm_and_ps(m2, m3),
),
core::arch::x86_64::_mm_and_ps(m4, m5),
);
let hmask: i32 = core::arch::x86_64::_mm_movemask_ps(hit_v);
if hmask != 0 {
core::arch::x86_64::_mm_storeu_ps(ad_arr.as_mut_ptr(), ad);
core::arch::x86_64::_mm_storeu_ps(st_arr.as_mut_ptr(), st);
}
hmask
};
let mut rem: u32 = mask as u32;
while rem != 0 {
let i: usize = rem.trailing_zeros() as usize;
rem &= rem - 1;
let t: f32 = st_arr[i] / ad_arr[i];
if t < *best_t {
*best_t = t;
*best_idx = tri_indices[i] as usize;
}
}
}
#[cfg(not(any(target_arch = "aarch64", target_arch = "x86_64")))]
{
let ox: f32 = orig[0];
let oy: f32 = orig[1];
let oz: f32 = orig[2];
let dx: f32 = dir[0];
let dy: f32 = dir[1];
let dz: f32 = dir[2];
let mut ad_arr: [f32; 4] = [0.0; 4];
let mut st_arr: [f32; 4] = [0.0; 4];
let mut hits: [bool; 4] = [false; 4];
for i in 0..4 {
let hx: f32 = dy.mul_add(e2z[i], -(dz * e2y[i]));
let hy: f32 = dz.mul_add(e2x[i], -(dx * e2z[i]));
let hz: f32 = dx.mul_add(e2y[i], -(dy * e2x[i]));
let det: f32 = e1x[i].mul_add(hx, e1y[i].mul_add(hy, e1z[i] * hz));
let sign: f32 = 1.0_f32.copysign(det);
let ad: f32 = det.abs();
let sx: f32 = ox - v0x[i];
let sy: f32 = oy - v0y[i];
let sz: f32 = oz - v0z[i];
let su: f32 = sx.mul_add(hx, sy.mul_add(hy, sz * hz)) * sign;
let qx: f32 = sy.mul_add(e1z[i], -(sz * e1y[i]));
let qy: f32 = sz.mul_add(e1x[i], -(sx * e1z[i]));
let qz: f32 = sx.mul_add(e1y[i], -(sy * e1x[i]));
let sv: f32 = dx.mul_add(qx, dy.mul_add(qy, dz * qz)) * sign;
let st: f32 = e2x[i].mul_add(qx, e2y[i].mul_add(qy, e2z[i] * qz)) * sign;
ad_arr[i] = ad;
st_arr[i] = st;
let uv_ok: bool = su >= 0.0 && sv >= 0.0 && (su + sv) <= ad;
let t_ok: bool = st > t_min * ad && st < cur_max * ad;
hits[i] = ad > 1e-6 && uv_ok && t_ok;
}
for i in 0..4 {
if hits[i] {
let t: f32 = st_arr[i] / ad_arr[i];
if t < *best_t {
*best_t = t;
*best_idx = tri_indices[i] as usize;
}
}
}
}
}
}
impl BvhFlatNode {
#[inline(always)]
#[must_use]
pub(crate) const fn empty() -> Self {
Self {
min: [0.0; 3],
child_or_first: 0,
max: [0.0; 3],
tri_count: 0,
}
}
#[inline(always)]
#[must_use]
pub(crate) fn intersect_box_fma(
min: [f32; 3],
max: [f32; 3],
inv: [f32; 3],
neg_orig_inv: [f32; 3],
t_min: f32,
t_max: f32,
) -> f32 {
let tx1: f32 = min[0].mul_add(inv[0], neg_orig_inv[0]);
let tx2: f32 = max[0].mul_add(inv[0], neg_orig_inv[0]);
let mut tmin: f32 = t_min.max(tx1.min(tx2));
let mut tmax: f32 = t_max.min(tx1.max(tx2));
let ty1: f32 = min[1].mul_add(inv[1], neg_orig_inv[1]);
let ty2: f32 = max[1].mul_add(inv[1], neg_orig_inv[1]);
tmin = tmin.max(ty1.min(ty2));
tmax = tmax.min(ty1.max(ty2));
let tz1: f32 = min[2].mul_add(inv[2], neg_orig_inv[2]);
let tz2: f32 = max[2].mul_add(inv[2], neg_orig_inv[2]);
tmin = tmin.max(tz1.min(tz2));
tmax = tmax.min(tz1.max(tz2));
if tmax >= tmin && tmax >= 0.0 {
tmin
} else {
f32::INFINITY
}
}
}
impl Bvh4Node {
#[inline(always)]
#[must_use]
pub(crate) const fn empty() -> Self {
Self {
min_x: [f32::INFINITY; 4],
min_y: [f32::INFINITY; 4],
min_z: [f32::INFINITY; 4],
max_x: [f32::INFINITY; 4],
max_y: [f32::INFINITY; 4],
max_z: [f32::INFINITY; 4],
child: [u32::MAX; 4],
leaf_count: [0; 4],
valid_count: 0,
_pad: [0; 11],
}
}
#[inline(always)]
#[must_use]
pub(crate) fn intersect_4(
&self,
inv: [f32; 3],
neg_orig_inv: [f32; 3],
t_min: f32,
t_max: f32,
) -> ([f32; 4], u32) {
let Self {
min_x,
min_y,
min_z,
max_x,
max_y,
max_z,
valid_count,
..
} = self;
let valid_mask: u32 = (1_u32 << (*valid_count as u32)) - 1;
#[cfg(target_arch = "aarch64")]
{
let mut t_out: [f32; 4] = [f32::INFINITY; 4];
let mask: u32 = unsafe {
let ix: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(inv[0]);
let iy: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(inv[1]);
let iz: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(inv[2]);
let nx: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vdupq_n_f32(neg_orig_inv[0]);
let ny: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vdupq_n_f32(neg_orig_inv[1]);
let nz: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vdupq_n_f32(neg_orig_inv[2]);
let tx1: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
nx,
core::arch::aarch64::vld1q_f32(min_x.as_ptr()),
ix,
);
let tx2: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
nx,
core::arch::aarch64::vld1q_f32(max_x.as_ptr()),
ix,
);
let mut vmin: core::arch::aarch64::float32x4_t = core::arch::aarch64::vmaxq_f32(
core::arch::aarch64::vdupq_n_f32(t_min),
core::arch::aarch64::vminq_f32(tx1, tx2),
);
let mut vmax: core::arch::aarch64::float32x4_t = core::arch::aarch64::vminq_f32(
core::arch::aarch64::vdupq_n_f32(t_max),
core::arch::aarch64::vmaxq_f32(tx1, tx2),
);
let ty1: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
ny,
core::arch::aarch64::vld1q_f32(min_y.as_ptr()),
iy,
);
let ty2: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
ny,
core::arch::aarch64::vld1q_f32(max_y.as_ptr()),
iy,
);
vmin =
core::arch::aarch64::vmaxq_f32(vmin, core::arch::aarch64::vminq_f32(ty1, ty2));
vmax =
core::arch::aarch64::vminq_f32(vmax, core::arch::aarch64::vmaxq_f32(ty1, ty2));
let tz1: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
nz,
core::arch::aarch64::vld1q_f32(min_z.as_ptr()),
iz,
);
let tz2: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
nz,
core::arch::aarch64::vld1q_f32(max_z.as_ptr()),
iz,
);
vmin =
core::arch::aarch64::vmaxq_f32(vmin, core::arch::aarch64::vminq_f32(tz1, tz2));
vmax =
core::arch::aarch64::vminq_f32(vmax, core::arch::aarch64::vmaxq_f32(tz1, tz2));
core::arch::aarch64::vst1q_f32(t_out.as_mut_ptr(), vmin);
let hit_v: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
core::arch::aarch64::vcgeq_f32(vmax, vmin),
core::arch::aarch64::vcgeq_f32(vmax, core::arch::aarch64::vdupq_n_f32(0.0)),
);
let lane_bits: [u32; 4] = [1, 2, 4, 8];
core::arch::aarch64::vaddvq_u32(core::arch::aarch64::vandq_u32(
hit_v,
core::arch::aarch64::vld1q_u32(lane_bits.as_ptr()),
))
};
(t_out, mask & valid_mask)
}
#[cfg(target_arch = "x86_64")]
{
let mut t_out: [f32; 4] = [f32::INFINITY; 4];
let mask: i32 = unsafe {
let ix: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(inv[0]);
let iy: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(inv[1]);
let iz: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(inv[2]);
let nx: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_set1_ps(neg_orig_inv[0]);
let ny: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_set1_ps(neg_orig_inv[1]);
let nz: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_set1_ps(neg_orig_inv[2]);
let tx1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(
core::arch::x86_64::_mm_load_ps(min_x.as_ptr()),
ix,
),
nx,
);
let tx2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(
core::arch::x86_64::_mm_load_ps(max_x.as_ptr()),
ix,
),
nx,
);
let mut vmin: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_max_ps(
core::arch::x86_64::_mm_set1_ps(t_min),
core::arch::x86_64::_mm_min_ps(tx1, tx2),
);
let mut vmax: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_min_ps(
core::arch::x86_64::_mm_set1_ps(t_max),
core::arch::x86_64::_mm_max_ps(tx1, tx2),
);
let ty1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(
core::arch::x86_64::_mm_load_ps(min_y.as_ptr()),
iy,
),
ny,
);
let ty2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(
core::arch::x86_64::_mm_load_ps(max_y.as_ptr()),
iy,
),
ny,
);
vmin =
core::arch::x86_64::_mm_max_ps(vmin, core::arch::x86_64::_mm_min_ps(ty1, ty2));
vmax =
core::arch::x86_64::_mm_min_ps(vmax, core::arch::x86_64::_mm_max_ps(ty1, ty2));
let tz1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(
core::arch::x86_64::_mm_load_ps(min_z.as_ptr()),
iz,
),
nz,
);
let tz2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(
core::arch::x86_64::_mm_load_ps(max_z.as_ptr()),
iz,
),
nz,
);
vmin =
core::arch::x86_64::_mm_max_ps(vmin, core::arch::x86_64::_mm_min_ps(tz1, tz2));
vmax =
core::arch::x86_64::_mm_min_ps(vmax, core::arch::x86_64::_mm_max_ps(tz1, tz2));
core::arch::x86_64::_mm_storeu_ps(t_out.as_mut_ptr(), vmin);
let hit_v: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_and_ps(
core::arch::x86_64::_mm_cmpge_ps(vmax, vmin),
core::arch::x86_64::_mm_cmpge_ps(vmax, core::arch::x86_64::_mm_setzero_ps()),
);
core::arch::x86_64::_mm_movemask_ps(hit_v)
};
(t_out, (mask as u32) & valid_mask)
}
#[cfg(not(any(target_arch = "aarch64", target_arch = "x86_64")))]
{
let mut t_out: [f32; 4] = [f32::INFINITY; 4];
let mut mask: u32 = 0;
let count: usize = *valid_count as usize;
for c in 0..count {
let t: f32 = BvhFlatNode::intersect_box_fma(
[min_x[c], min_y[c], min_z[c]],
[max_x[c], max_y[c], max_z[c]],
inv,
neg_orig_inv,
t_min,
t_max,
);
t_out[c] = t;
if t.is_finite() {
mask |= 1_u32 << c;
}
}
(t_out, mask & valid_mask)
}
}
#[inline(always)]
#[must_use]
pub(crate) fn intersect_aabb_packet4(
bmin: [f32; 3],
bmax: [f32; 3],
pkt: &RayPacket4,
neg_oi: &[[f32; 4]; 3],
t_min: f32,
t_max_4: [f32; 4],
) -> u8 {
let bx0: f32 = bmin[0];
let by0: f32 = bmin[1];
let bz0: f32 = bmin[2];
let bx1: f32 = bmax[0];
let by1: f32 = bmax[1];
let bz1: f32 = bmax[2];
#[cfg(target_arch = "aarch64")]
{
unsafe {
let ix: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(pkt.ix.as_ptr());
let iy: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(pkt.iy.as_ptr());
let iz: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(pkt.iz.as_ptr());
let nx: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(neg_oi[0].as_ptr());
let ny: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(neg_oi[1].as_ptr());
let nz: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(neg_oi[2].as_ptr());
let tx1: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vfmaq_f32(nx, core::arch::aarch64::vdupq_n_f32(bx0), ix);
let tx2: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vfmaq_f32(nx, core::arch::aarch64::vdupq_n_f32(bx1), ix);
let mut vmin: core::arch::aarch64::float32x4_t = core::arch::aarch64::vmaxq_f32(
core::arch::aarch64::vdupq_n_f32(t_min),
core::arch::aarch64::vminq_f32(tx1, tx2),
);
let mut vmax: core::arch::aarch64::float32x4_t = core::arch::aarch64::vminq_f32(
core::arch::aarch64::vld1q_f32(t_max_4.as_ptr()),
core::arch::aarch64::vmaxq_f32(tx1, tx2),
);
let ty1: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vfmaq_f32(ny, core::arch::aarch64::vdupq_n_f32(by0), iy);
let ty2: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vfmaq_f32(ny, core::arch::aarch64::vdupq_n_f32(by1), iy);
vmin =
core::arch::aarch64::vmaxq_f32(vmin, core::arch::aarch64::vminq_f32(ty1, ty2));
vmax =
core::arch::aarch64::vminq_f32(vmax, core::arch::aarch64::vmaxq_f32(ty1, ty2));
let tz1: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vfmaq_f32(nz, core::arch::aarch64::vdupq_n_f32(bz0), iz);
let tz2: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vfmaq_f32(nz, core::arch::aarch64::vdupq_n_f32(bz1), iz);
vmin =
core::arch::aarch64::vmaxq_f32(vmin, core::arch::aarch64::vminq_f32(tz1, tz2));
vmax =
core::arch::aarch64::vminq_f32(vmax, core::arch::aarch64::vmaxq_f32(tz1, tz2));
let hit_v: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
core::arch::aarch64::vcgeq_f32(vmax, vmin),
core::arch::aarch64::vcgeq_f32(vmax, core::arch::aarch64::vdupq_n_f32(0.0)),
);
let lane_bits: [u32; 4] = [1, 2, 4, 8];
core::arch::aarch64::vaddvq_u32(core::arch::aarch64::vandq_u32(
hit_v,
core::arch::aarch64::vld1q_u32(lane_bits.as_ptr()),
)) as u8
}
}
#[cfg(target_arch = "x86_64")]
{
unsafe {
let ix: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_loadu_ps(pkt.ix.as_ptr());
let iy: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_loadu_ps(pkt.iy.as_ptr());
let iz: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_loadu_ps(pkt.iz.as_ptr());
let nx: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_loadu_ps(neg_oi[0].as_ptr());
let ny: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_loadu_ps(neg_oi[1].as_ptr());
let nz: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_loadu_ps(neg_oi[2].as_ptr());
let tx1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(bx0), ix),
nx,
);
let tx2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(bx1), ix),
nx,
);
let mut vmin: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_max_ps(
core::arch::x86_64::_mm_set1_ps(t_min),
core::arch::x86_64::_mm_min_ps(tx1, tx2),
);
let mut vmax: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_min_ps(
core::arch::x86_64::_mm_loadu_ps(t_max_4.as_ptr()),
core::arch::x86_64::_mm_max_ps(tx1, tx2),
);
let ty1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(by0), iy),
ny,
);
let ty2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(by1), iy),
ny,
);
vmin =
core::arch::x86_64::_mm_max_ps(vmin, core::arch::x86_64::_mm_min_ps(ty1, ty2));
vmax =
core::arch::x86_64::_mm_min_ps(vmax, core::arch::x86_64::_mm_max_ps(ty1, ty2));
let tz1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(bz0), iz),
nz,
);
let tz2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(bz1), iz),
nz,
);
vmin =
core::arch::x86_64::_mm_max_ps(vmin, core::arch::x86_64::_mm_min_ps(tz1, tz2));
vmax =
core::arch::x86_64::_mm_min_ps(vmax, core::arch::x86_64::_mm_max_ps(tz1, tz2));
let hit_v: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_and_ps(
core::arch::x86_64::_mm_cmpge_ps(vmax, vmin),
core::arch::x86_64::_mm_cmpge_ps(vmax, core::arch::x86_64::_mm_setzero_ps()),
);
core::arch::x86_64::_mm_movemask_ps(hit_v) as u8
}
}
#[cfg(not(any(target_arch = "aarch64", target_arch = "x86_64")))]
{
let mut mask: u8 = 0;
for r in 0..4 {
let t: f32 = BvhFlatNode::intersect_box_fma(
[bx0, by0, bz0],
[bx1, by1, bz1],
[pkt.ix[r], pkt.iy[r], pkt.iz[r]],
[neg_oi[0][r], neg_oi[1][r], neg_oi[2][r]],
t_min,
t_max_4[r],
);
if t.is_finite() {
mask |= 1_u8 << r;
}
}
mask
}
}
#[inline(always)]
#[must_use]
pub(crate) fn intersect_child_packet4(
&self,
lane: usize,
pkt: &RayPacket4,
neg_oi: &[[f32; 4]; 3],
t_min: f32,
t_max_4: [f32; 4],
) -> u8 {
let Self {
min_x,
min_y,
min_z,
max_x,
max_y,
max_z,
..
} = self;
Self::intersect_aabb_packet4(
[min_x[lane], min_y[lane], min_z[lane]],
[max_x[lane], max_y[lane], max_z[lane]],
pkt,
neg_oi,
t_min,
t_max_4,
)
}
#[inline(always)]
#[must_use]
pub(crate) fn intersect_child_packet4_near(
&self,
lane: usize,
pkt: &RayPacket4,
neg_oi: &[[f32; 4]; 3],
t_min: f32,
t_max_4: [f32; 4],
) -> (u8, f32) {
let Self {
min_x,
min_y,
min_z,
max_x,
max_y,
max_z,
..
} = self;
let bx0: f32 = min_x[lane];
let by0: f32 = min_y[lane];
let bz0: f32 = min_z[lane];
let bx1: f32 = max_x[lane];
let by1: f32 = max_y[lane];
let bz1: f32 = max_z[lane];
#[cfg(target_arch = "aarch64")]
{
unsafe {
let ix: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(pkt.ix.as_ptr());
let iy: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(pkt.iy.as_ptr());
let iz: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(pkt.iz.as_ptr());
let nx: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(neg_oi[0].as_ptr());
let ny: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(neg_oi[1].as_ptr());
let nz: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vld1q_f32(neg_oi[2].as_ptr());
let tx1: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vfmaq_f32(nx, core::arch::aarch64::vdupq_n_f32(bx0), ix);
let tx2: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vfmaq_f32(nx, core::arch::aarch64::vdupq_n_f32(bx1), ix);
let mut vmin: core::arch::aarch64::float32x4_t = core::arch::aarch64::vmaxq_f32(
core::arch::aarch64::vdupq_n_f32(t_min),
core::arch::aarch64::vminq_f32(tx1, tx2),
);
let mut vmax: core::arch::aarch64::float32x4_t = core::arch::aarch64::vminq_f32(
core::arch::aarch64::vld1q_f32(t_max_4.as_ptr()),
core::arch::aarch64::vmaxq_f32(tx1, tx2),
);
let ty1: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vfmaq_f32(ny, core::arch::aarch64::vdupq_n_f32(by0), iy);
let ty2: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vfmaq_f32(ny, core::arch::aarch64::vdupq_n_f32(by1), iy);
vmin =
core::arch::aarch64::vmaxq_f32(vmin, core::arch::aarch64::vminq_f32(ty1, ty2));
vmax =
core::arch::aarch64::vminq_f32(vmax, core::arch::aarch64::vmaxq_f32(ty1, ty2));
let tz1: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vfmaq_f32(nz, core::arch::aarch64::vdupq_n_f32(bz0), iz);
let tz2: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vfmaq_f32(nz, core::arch::aarch64::vdupq_n_f32(bz1), iz);
vmin =
core::arch::aarch64::vmaxq_f32(vmin, core::arch::aarch64::vminq_f32(tz1, tz2));
vmax =
core::arch::aarch64::vminq_f32(vmax, core::arch::aarch64::vmaxq_f32(tz1, tz2));
let hit_v: core::arch::aarch64::uint32x4_t = core::arch::aarch64::vandq_u32(
core::arch::aarch64::vcgeq_f32(vmax, vmin),
core::arch::aarch64::vcgeq_f32(vmax, core::arch::aarch64::vdupq_n_f32(0.0)),
);
let lane_bits: [u32; 4] = [1, 2, 4, 8];
let mask: u8 = core::arch::aarch64::vaddvq_u32(core::arch::aarch64::vandq_u32(
hit_v,
core::arch::aarch64::vld1q_u32(lane_bits.as_ptr()),
)) as u8;
if mask == 0 {
return (0, f32::INFINITY);
}
let masked_t: core::arch::aarch64::float32x4_t = core::arch::aarch64::vbslq_f32(
hit_v,
vmin,
core::arch::aarch64::vdupq_n_f32(f32::INFINITY),
);
let min_t: f32 = core::arch::aarch64::vminvq_f32(masked_t);
(mask, min_t)
}
}
#[cfg(target_arch = "x86_64")]
{
unsafe {
let ix: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_loadu_ps(pkt.ix.as_ptr());
let iy: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_loadu_ps(pkt.iy.as_ptr());
let iz: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_loadu_ps(pkt.iz.as_ptr());
let nx: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_loadu_ps(neg_oi[0].as_ptr());
let ny: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_loadu_ps(neg_oi[1].as_ptr());
let nz: core::arch::x86_64::__m128 =
core::arch::x86_64::_mm_loadu_ps(neg_oi[2].as_ptr());
let tx1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(bx0), ix),
nx,
);
let tx2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(bx1), ix),
nx,
);
let mut vmin: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_max_ps(
core::arch::x86_64::_mm_set1_ps(t_min),
core::arch::x86_64::_mm_min_ps(tx1, tx2),
);
let mut vmax: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_min_ps(
core::arch::x86_64::_mm_loadu_ps(t_max_4.as_ptr()),
core::arch::x86_64::_mm_max_ps(tx1, tx2),
);
let ty1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(by0), iy),
ny,
);
let ty2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(by1), iy),
ny,
);
vmin =
core::arch::x86_64::_mm_max_ps(vmin, core::arch::x86_64::_mm_min_ps(ty1, ty2));
vmax =
core::arch::x86_64::_mm_min_ps(vmax, core::arch::x86_64::_mm_max_ps(ty1, ty2));
let tz1: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(bz0), iz),
nz,
);
let tz2: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(core::arch::x86_64::_mm_set1_ps(bz1), iz),
nz,
);
vmin =
core::arch::x86_64::_mm_max_ps(vmin, core::arch::x86_64::_mm_min_ps(tz1, tz2));
vmax =
core::arch::x86_64::_mm_min_ps(vmax, core::arch::x86_64::_mm_max_ps(tz1, tz2));
let hit_v: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_and_ps(
core::arch::x86_64::_mm_cmpge_ps(vmax, vmin),
core::arch::x86_64::_mm_cmpge_ps(vmax, core::arch::x86_64::_mm_setzero_ps()),
);
let mask: u8 = core::arch::x86_64::_mm_movemask_ps(hit_v) as u8;
if mask == 0 {
return (0, f32::INFINITY);
}
let mut t_arr: [f32; 4] = [0.0; 4];
core::arch::x86_64::_mm_storeu_ps(t_arr.as_mut_ptr(), vmin);
let mut min_t: f32 = f32::INFINITY;
let mut m: u8 = mask;
while m != 0 {
let r: usize = m.trailing_zeros() as usize;
m &= m - 1;
min_t = min_t.min(t_arr[r]);
}
(mask, min_t)
}
}
#[cfg(not(any(target_arch = "aarch64", target_arch = "x86_64")))]
{
let mut mask: u8 = 0;
let mut min_t: f32 = f32::INFINITY;
for r in 0..4 {
let t: f32 = BvhFlatNode::intersect_box_fma(
[bx0, by0, bz0],
[bx1, by1, bz1],
[pkt.ix[r], pkt.iy[r], pkt.iz[r]],
[neg_oi[0][r], neg_oi[1][r], neg_oi[2][r]],
t_min,
t_max_4[r],
);
if t.is_finite() {
mask |= 1_u8 << r;
min_t = min_t.min(t);
}
}
(mask, min_t)
}
}
}
#[cfg(target_arch = "x86_64")]
impl Bvh8Node {
#[target_feature(enable = "xsave")]
#[must_use]
unsafe fn read_xcr0() -> u64 {
unsafe { core::arch::x86_64::_xgetbv(0) }
}
#[must_use]
pub(crate) fn has_avx2_fma() -> bool {
*X86_AVX2_FMA_AVAILABLE.get_or_init(|| -> bool {
let max_leaf: u32 = core::arch::x86_64::__cpuid(0).eax;
if max_leaf < 7 {
return false;
}
let leaf1: core::arch::x86_64::CpuidResult = core::arch::x86_64::__cpuid(1);
let req_ecx: u32 = (1_u32 << 27) | (1_u32 << 28) | (1_u32 << 12);
if (leaf1.ecx & req_ecx) != req_ecx {
return false;
}
let xcr0: u64 = unsafe { Self::read_xcr0() };
if (xcr0 & 0x6) != 0x6 {
return false;
}
let leaf7: core::arch::x86_64::CpuidResult = core::arch::x86_64::__cpuid_count(7, 0);
(leaf7.ebx & (1_u32 << 5)) != 0
})
}
#[inline(always)]
#[must_use]
pub(crate) const fn empty() -> Self {
Self {
min_x: [f32::INFINITY; 8],
min_y: [f32::INFINITY; 8],
min_z: [f32::INFINITY; 8],
max_x: [f32::INFINITY; 8],
max_y: [f32::INFINITY; 8],
max_z: [f32::INFINITY; 8],
child: [u32::MAX; 8],
leaf_count: [0; 8],
valid_count: 0,
_pad: [0; 23],
}
}
#[target_feature(enable = "avx2", enable = "fma")]
#[must_use]
pub(crate) unsafe fn intersect_8_avx2(
&self,
inv: [f32; 3],
neg_orig_inv: [f32; 3],
t_min: f32,
t_max: f32,
) -> ([f32; 8], u32) {
let Self {
min_x,
min_y,
min_z,
max_x,
max_y,
max_z,
valid_count,
..
} = self;
let valid_mask: u32 = (1_u32 << (*valid_count as u32)) - 1;
let mut t_out: [f32; 8] = [f32::INFINITY; 8];
let mask: u32 = unsafe {
let ix: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_set1_ps(inv[0]);
let iy: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_set1_ps(inv[1]);
let iz: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_set1_ps(inv[2]);
let nx: core::arch::x86_64::__m256 =
core::arch::x86_64::_mm256_set1_ps(neg_orig_inv[0]);
let ny: core::arch::x86_64::__m256 =
core::arch::x86_64::_mm256_set1_ps(neg_orig_inv[1]);
let nz: core::arch::x86_64::__m256 =
core::arch::x86_64::_mm256_set1_ps(neg_orig_inv[2]);
let tx1: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_fmadd_ps(
core::arch::x86_64::_mm256_load_ps(min_x.as_ptr()),
ix,
nx,
);
let tx2: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_fmadd_ps(
core::arch::x86_64::_mm256_load_ps(max_x.as_ptr()),
ix,
nx,
);
let mut vmin: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_max_ps(
core::arch::x86_64::_mm256_set1_ps(t_min),
core::arch::x86_64::_mm256_min_ps(tx1, tx2),
);
let mut vmax: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_min_ps(
core::arch::x86_64::_mm256_set1_ps(t_max),
core::arch::x86_64::_mm256_max_ps(tx1, tx2),
);
let ty1: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_fmadd_ps(
core::arch::x86_64::_mm256_load_ps(min_y.as_ptr()),
iy,
ny,
);
let ty2: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_fmadd_ps(
core::arch::x86_64::_mm256_load_ps(max_y.as_ptr()),
iy,
ny,
);
vmin = core::arch::x86_64::_mm256_max_ps(
vmin,
core::arch::x86_64::_mm256_min_ps(ty1, ty2),
);
vmax = core::arch::x86_64::_mm256_min_ps(
vmax,
core::arch::x86_64::_mm256_max_ps(ty1, ty2),
);
let tz1: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_fmadd_ps(
core::arch::x86_64::_mm256_load_ps(min_z.as_ptr()),
iz,
nz,
);
let tz2: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_fmadd_ps(
core::arch::x86_64::_mm256_load_ps(max_z.as_ptr()),
iz,
nz,
);
vmin = core::arch::x86_64::_mm256_max_ps(
vmin,
core::arch::x86_64::_mm256_min_ps(tz1, tz2),
);
vmax = core::arch::x86_64::_mm256_min_ps(
vmax,
core::arch::x86_64::_mm256_max_ps(tz1, tz2),
);
core::arch::x86_64::_mm256_storeu_ps(t_out.as_mut_ptr(), vmin);
let hit_v: core::arch::x86_64::__m256 = core::arch::x86_64::_mm256_and_ps(
core::arch::x86_64::_mm256_cmp_ps::<{ core::arch::x86_64::_CMP_GE_OQ }>(vmax, vmin),
core::arch::x86_64::_mm256_cmp_ps::<{ core::arch::x86_64::_CMP_GE_OQ }>(
vmax,
core::arch::x86_64::_mm256_setzero_ps(),
),
);
core::arch::x86_64::_mm256_movemask_ps(hit_v) as u32
};
(t_out, mask & valid_mask)
}
}
impl BvhTriangle {
#[must_use]
pub fn from_points(v0: Vec3, v1: Vec3, v2: Vec3, material: PbrMaterial) -> Self {
let edge1: Vec3 = v1.sub(v0);
let edge2: Vec3 = v2.sub(v0);
let normal: Vec3 = edge1.cross(edge2).normalize();
let centroid: Vec3 = v0.add(v1).add(v2).scale(1.0 / 3.0);
let mut bounds: Aabb3D = Aabb3D::empty();
bounds.expand_point(v0);
bounds.expand_point(v1);
bounds.expand_point(v2);
Self {
v0,
edge1,
edge2,
normal,
centroid,
bounds,
material,
}
}
#[must_use]
pub fn intersect(&self, ray: &Ray3D, t_min: f32, t_max: f32) -> Option<f32> {
let g: BvhGeomTri = BvhGeomTri::from_triangle(self);
g.intersect_raw(ray.get_origin(), ray.get_direction(), t_min, t_max)
}
}
impl RayPacket4 {
#[must_use]
pub fn from_rays(rays: &[Ray3D; 4]) -> Self {
let o0: Vec3 = rays[0].get_origin();
let o1: Vec3 = rays[1].get_origin();
let o2: Vec3 = rays[2].get_origin();
let o3: Vec3 = rays[3].get_origin();
let d0: Vec3 = rays[0].get_direction();
let d1: Vec3 = rays[1].get_direction();
let d2: Vec3 = rays[2].get_direction();
let d3: Vec3 = rays[3].get_direction();
let i0: Vec3 = rays[0].get_inv_direction();
let i1: Vec3 = rays[1].get_inv_direction();
let i2: Vec3 = rays[2].get_inv_direction();
let i3: Vec3 = rays[3].get_inv_direction();
Self {
ox: [o0.get_x(), o1.get_x(), o2.get_x(), o3.get_x()],
oy: [o0.get_y(), o1.get_y(), o2.get_y(), o3.get_y()],
oz: [o0.get_z(), o1.get_z(), o2.get_z(), o3.get_z()],
dx: [d0.get_x(), d1.get_x(), d2.get_x(), d3.get_x()],
dy: [d0.get_y(), d1.get_y(), d2.get_y(), d3.get_y()],
dz: [d0.get_z(), d1.get_z(), d2.get_z(), d3.get_z()],
ix: [i0.get_x(), i1.get_x(), i2.get_x(), i3.get_x()],
iy: [i0.get_y(), i1.get_y(), i2.get_y(), i3.get_y()],
iz: [i0.get_z(), i1.get_z(), i2.get_z(), i3.get_z()],
}
}
}
impl Bvh3D {
fn flatten_sibling_pair(
nodes: &[BvhNode3D],
geom: &[BvhGeomTri],
leaf_packets: &mut Vec<BvhTri4>,
flat_nodes: &mut Vec<BvhFlatNode>,
node_to_flat: &mut [u32],
src_idx: usize,
dst_idx: usize,
) {
if src_idx < node_to_flat.len() {
node_to_flat[src_idx] = dst_idx as u32;
}
let n: &BvhNode3D = &nodes[src_idx];
let mn: Vec3 = n.get_bounds().get_min();
let mx: Vec3 = n.get_bounds().get_max();
let tc: u32 = n.get_tri_count();
if tc > 0 {
let first_tri: usize = n.get_first_tri() as usize;
let end_tri: usize = first_tri + (tc as usize);
let pkt_first: u32 = leaf_packets.len() as u32;
let mut cursor: usize = first_tri;
while cursor < end_tri {
let next_end: usize = (cursor + 4).min(end_tri);
leaf_packets.push(BvhTri4::from_geom_chunk(
&geom[cursor..next_end],
cursor as u32,
));
cursor = next_end;
}
let pkt_count: u32 = (leaf_packets.len() as u32) - pkt_first;
flat_nodes[dst_idx] = BvhFlatNode {
min: [mn.get_x(), mn.get_y(), mn.get_z()],
child_or_first: pkt_first,
max: [mx.get_x(), mx.get_y(), mx.get_z()],
tri_count: pkt_count,
};
} else {
let first_child: usize = flat_nodes.len();
flat_nodes.push(BvhFlatNode::empty());
flat_nodes.push(BvhFlatNode::empty());
flat_nodes[dst_idx] = BvhFlatNode {
min: [mn.get_x(), mn.get_y(), mn.get_z()],
child_or_first: first_child as u32,
max: [mx.get_x(), mx.get_y(), mx.get_z()],
tri_count: 0,
};
Self::flatten_sibling_pair(
nodes,
geom,
leaf_packets,
flat_nodes,
node_to_flat,
n.get_left() as usize,
first_child,
);
Self::flatten_sibling_pair(
nodes,
geom,
leaf_packets,
flat_nodes,
node_to_flat,
n.get_right() as usize,
first_child + 1,
);
}
}
fn collapse_to_bvh4(
nodes: &[BvhNode3D],
flat_nodes: &[BvhFlatNode],
node_to_flat: &[u32],
bvh4_nodes: &mut Vec<Bvh4Node>,
bvh4_src: &mut Vec<[u32; 4]>,
src_idx: usize,
dst_idx: usize,
) {
let root_n: &BvhNode3D = &nodes[src_idx];
let mut children: [usize; 4] = [0; 4];
let mut len: usize;
if root_n.get_tri_count() > 0 {
children[0] = src_idx;
len = 1;
} else {
children[0] = root_n.get_left() as usize;
children[1] = root_n.get_right() as usize;
len = 2;
while len < 4 {
let mut best_slot: Option<usize> = None;
let mut best_area: f32 = -1.0;
for (slot, &c_idx) in children.iter().take(len).enumerate() {
if nodes[c_idx].get_tri_count() == 0 {
let area: f32 = nodes[c_idx].get_bounds().surface_area();
if area > best_area {
best_area = area;
best_slot = Some(slot);
}
}
}
let Some(expand_slot) = best_slot else {
break;
};
let expand_node: &BvhNode3D = &nodes[children[expand_slot]];
children[expand_slot] = expand_node.get_left() as usize;
children[len] = expand_node.get_right() as usize;
len += 1;
}
}
let mut node4: Bvh4Node = Bvh4Node::empty();
let mut src_ids: [u32; 4] = [u32::MAX; 4];
node4.valid_count = len as u8;
let mut interior_recur: [(usize, usize); 4] = [(0, 0); 4];
let mut interior_len: usize = 0;
for (lane, &c_idx) in children.iter().take(len).enumerate() {
let cn: &BvhNode3D = &nodes[c_idx];
let mn: Vec3 = cn.get_bounds().get_min();
let mx: Vec3 = cn.get_bounds().get_max();
node4.min_x[lane] = mn.get_x();
node4.min_y[lane] = mn.get_y();
node4.min_z[lane] = mn.get_z();
node4.max_x[lane] = mx.get_x();
node4.max_y[lane] = mx.get_y();
node4.max_z[lane] = mx.get_z();
src_ids[lane] = c_idx as u32;
if cn.get_tri_count() > 0 {
let flat: &BvhFlatNode = &flat_nodes[node_to_flat[c_idx] as usize];
node4.child[lane] = flat.child_or_first;
node4.leaf_count[lane] = flat.tri_count.min(255) as u8;
} else {
let next_dst: usize = bvh4_nodes.len();
bvh4_nodes.push(Bvh4Node::empty());
bvh4_src.push([u32::MAX; 4]);
node4.child[lane] = next_dst as u32;
node4.leaf_count[lane] = 0;
interior_recur[interior_len] = (c_idx, next_dst);
interior_len += 1;
}
}
bvh4_nodes[dst_idx] = node4;
bvh4_src[dst_idx] = src_ids;
for &(c_src, c_dst) in interior_recur.iter().take(interior_len) {
Self::collapse_to_bvh4(
nodes,
flat_nodes,
node_to_flat,
bvh4_nodes,
bvh4_src,
c_src,
c_dst,
);
}
}
#[cfg(target_arch = "x86_64")]
fn collapse_to_bvh8(
nodes: &[BvhNode3D],
flat_nodes: &[BvhFlatNode],
node_to_flat: &[u32],
bvh8_nodes: &mut Vec<Bvh8Node>,
bvh8_src: &mut Vec<[u32; 8]>,
src_idx: usize,
dst_idx: usize,
) {
let root_n: &BvhNode3D = &nodes[src_idx];
let mut children: [usize; 8] = [0; 8];
let mut len: usize;
if root_n.get_tri_count() > 0 {
children[0] = src_idx;
len = 1;
} else {
children[0] = root_n.get_left() as usize;
children[1] = root_n.get_right() as usize;
len = 2;
while len < 8 {
let mut best_slot: Option<usize> = None;
let mut best_area: f32 = -1.0;
for (slot, &c_idx) in children.iter().take(len).enumerate() {
if nodes[c_idx].get_tri_count() == 0 {
let area: f32 = nodes[c_idx].get_bounds().surface_area();
if area > best_area {
best_area = area;
best_slot = Some(slot);
}
}
}
let Some(expand_slot) = best_slot else {
break;
};
let expand_node: &BvhNode3D = &nodes[children[expand_slot]];
children[expand_slot] = expand_node.get_left() as usize;
children[len] = expand_node.get_right() as usize;
len += 1;
}
}
let mut node8: Bvh8Node = Bvh8Node::empty();
let mut src_ids: [u32; 8] = [u32::MAX; 8];
node8.valid_count = len as u8;
let mut interior_recur: [(usize, usize); 8] = [(0, 0); 8];
let mut interior_len: usize = 0;
for (lane, &c_idx) in children.iter().take(len).enumerate() {
let cn: &BvhNode3D = &nodes[c_idx];
let mn: Vec3 = cn.get_bounds().get_min();
let mx: Vec3 = cn.get_bounds().get_max();
node8.min_x[lane] = mn.get_x();
node8.min_y[lane] = mn.get_y();
node8.min_z[lane] = mn.get_z();
node8.max_x[lane] = mx.get_x();
node8.max_y[lane] = mx.get_y();
node8.max_z[lane] = mx.get_z();
src_ids[lane] = c_idx as u32;
if cn.get_tri_count() > 0 {
let flat: &BvhFlatNode = &flat_nodes[node_to_flat[c_idx] as usize];
node8.child[lane] = flat.child_or_first;
node8.leaf_count[lane] = flat.tri_count.min(255) as u8;
} else {
let next_dst: usize = bvh8_nodes.len();
bvh8_nodes.push(Bvh8Node::empty());
bvh8_src.push([u32::MAX; 8]);
node8.child[lane] = next_dst as u32;
node8.leaf_count[lane] = 0;
interior_recur[interior_len] = (c_idx, next_dst);
interior_len += 1;
}
}
bvh8_nodes[dst_idx] = node8;
bvh8_src[dst_idx] = src_ids;
for &(c_src, c_dst) in interior_recur.iter().take(interior_len) {
Self::collapse_to_bvh8(
nodes,
flat_nodes,
node_to_flat,
bvh8_nodes,
bvh8_src,
c_src,
c_dst,
);
}
}
#[must_use]
pub fn build(triangles: Vec<BvhTriangle>) -> Self {
let tri_len: usize = triangles.len();
if tri_len == 0 {
return Self::default();
}
let mut orig_indices: Vec<u32> = Vec::with_capacity(tri_len);
for idx in 0..tri_len {
orig_indices.push(idx as u32);
}
let mut bvh: Self = Self {
nodes: Vec::with_capacity(tri_len * 2 + 1),
triangles,
geom: Vec::with_capacity(tri_len),
leaf_packets: Vec::with_capacity(tri_len.div_ceil(4) + 16),
flat_nodes: Vec::with_capacity(tri_len * 2 + 2),
bvh4_nodes: Vec::with_capacity(tri_len.div_ceil(2) + 4),
bvh4_src_children: Vec::with_capacity(tri_len.div_ceil(2) + 4),
root_min: [0.0; 3],
root_max: [0.0; 3],
#[cfg(target_arch = "x86_64")]
bvh8_nodes: Vec::with_capacity(tri_len.div_ceil(4) + 4),
#[cfg(target_arch = "x86_64")]
bvh8_src_children: Vec::with_capacity(tri_len.div_ceil(4) + 4),
orig_indices,
node_to_flat: Vec::new(),
build_sah_cost: 0.0,
};
if !bvh.get_triangles().is_empty() {
bvh.rebuild_current();
}
bvh
}
pub fn rebuild_in_place(&mut self, source: &[BvhTriangle]) {
let tri_len: usize = source.len();
let Self {
nodes,
triangles,
geom,
leaf_packets,
flat_nodes,
bvh4_nodes,
bvh4_src_children,
root_min,
root_max,
#[cfg(target_arch = "x86_64")]
bvh8_nodes,
#[cfg(target_arch = "x86_64")]
bvh8_src_children,
orig_indices,
node_to_flat,
build_sah_cost,
} = self;
nodes.clear();
triangles.clear();
geom.clear();
leaf_packets.clear();
flat_nodes.clear();
bvh4_nodes.clear();
bvh4_src_children.clear();
#[cfg(target_arch = "x86_64")]
{
bvh8_nodes.clear();
bvh8_src_children.clear();
}
orig_indices.clear();
node_to_flat.clear();
*root_min = [0.0; 3];
*root_max = [0.0; 3];
*build_sah_cost = 0.0;
if tri_len == 0 {
return;
}
triangles.extend_from_slice(source);
let max_nodes: usize = tri_len.saturating_mul(2).saturating_add(8);
if nodes.capacity() < max_nodes {
nodes.reserve(max_nodes - nodes.capacity());
}
if geom.capacity() < tri_len {
geom.reserve(tri_len - geom.capacity());
}
let max_leaves: usize = tri_len.div_ceil(4).saturating_add(16);
if leaf_packets.capacity() < max_leaves {
leaf_packets.reserve(max_leaves - leaf_packets.capacity());
}
if flat_nodes.capacity() < max_nodes {
flat_nodes.reserve(max_nodes - flat_nodes.capacity());
}
let max_bvh4: usize = tri_len.div_ceil(2).saturating_add(8);
if bvh4_nodes.capacity() < max_bvh4 {
bvh4_nodes.reserve(max_bvh4 - bvh4_nodes.capacity());
}
if bvh4_src_children.capacity() < max_bvh4 {
bvh4_src_children.reserve(max_bvh4 - bvh4_src_children.capacity());
}
if node_to_flat.capacity() < max_nodes {
node_to_flat.reserve(max_nodes - node_to_flat.capacity());
}
if orig_indices.capacity() < tri_len {
orig_indices.reserve(tri_len - orig_indices.capacity());
}
for idx in 0..tri_len {
orig_indices.push(idx as u32);
}
self.rebuild_current();
}
fn rebuild_current(&mut self) {
let total: usize = self.get_triangles().len();
if total == 0 {
return;
}
let _: u32 = self.build_node(0, total);
let Self {
nodes,
triangles,
geom,
leaf_packets,
flat_nodes,
bvh4_nodes,
bvh4_src_children,
root_min,
root_max,
#[cfg(target_arch = "x86_64")]
bvh8_nodes,
#[cfg(target_arch = "x86_64")]
bvh8_src_children,
node_to_flat,
build_sah_cost,
..
} = self;
geom.clear();
for tri in triangles.iter() {
geom.push(BvhGeomTri::from_triangle(tri));
}
leaf_packets.clear();
flat_nodes.clear();
bvh4_nodes.clear();
bvh4_src_children.clear();
node_to_flat.clear();
node_to_flat.resize(nodes.len(), 0);
if !nodes.is_empty() {
let rmin: Vec3 = nodes[0].get_bounds().get_min();
let rmax: Vec3 = nodes[0].get_bounds().get_max();
*root_min = [rmin.get_x(), rmin.get_y(), rmin.get_z()];
*root_max = [rmax.get_x(), rmax.get_y(), rmax.get_z()];
flat_nodes.push(BvhFlatNode::empty());
flat_nodes.push(BvhFlatNode::empty());
Self::flatten_sibling_pair(nodes, geom, leaf_packets, flat_nodes, node_to_flat, 0, 0);
bvh4_nodes.push(Bvh4Node::empty());
bvh4_src_children.push([u32::MAX; 4]);
Self::collapse_to_bvh4(
nodes,
flat_nodes,
node_to_flat,
bvh4_nodes,
bvh4_src_children,
0,
0,
);
#[cfg(target_arch = "x86_64")]
{
bvh8_nodes.clear();
bvh8_src_children.clear();
if Bvh8Node::has_avx2_fma() {
bvh8_nodes.push(Bvh8Node::empty());
bvh8_src_children.push([u32::MAX; 8]);
Self::collapse_to_bvh8(
nodes,
flat_nodes,
node_to_flat,
bvh8_nodes,
bvh8_src_children,
0,
0,
);
}
}
let mut sah_sum: f32 = 0.0;
for n in nodes.iter() {
let area: f32 = n.get_bounds().surface_area();
if n.get_tri_count() > 0 {
sah_sum += area * (n.get_tri_count() as f32);
} else {
sah_sum += area * 0.5;
}
}
*build_sah_cost = sah_sum.max(1e-5);
}
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.get_triangles().is_empty()
}
#[must_use]
pub fn surface_area_cost(&self) -> f32 {
let mut cost: f32 = 0.0;
for n in self.get_nodes().iter() {
let area: f32 = n.get_bounds().surface_area();
if n.get_tri_count() > 0 {
cost += area * (n.get_tri_count() as f32);
} else {
cost += area * 0.5;
}
}
cost
}
#[must_use]
pub fn needs_rebuild_after_refit(&self, max_cost_ratio: f32) -> bool {
if self.is_empty() {
return false;
}
let base: f32 = self.get_build_sah_cost().max(1e-5);
self.surface_area_cost() > base * max_cost_ratio.max(1.05)
}
pub fn refit_from_source(&mut self, source_triangles: &[BvhTriangle]) -> bool {
let Self {
nodes,
triangles,
geom,
leaf_packets,
flat_nodes,
bvh4_nodes,
bvh4_src_children,
root_min,
root_max,
#[cfg(target_arch = "x86_64")]
bvh8_nodes,
#[cfg(target_arch = "x86_64")]
bvh8_src_children,
orig_indices,
node_to_flat,
..
} = self;
let count: usize = source_triangles.len();
if count == 0
|| triangles.len() != count
|| orig_indices.len() != count
|| nodes.is_empty()
|| node_to_flat.len() != nodes.len()
{
return false;
}
for slot in 0..count {
let src_idx: usize = orig_indices[slot] as usize;
if src_idx >= count {
return false;
}
let updated: BvhTriangle = source_triangles[src_idx];
triangles[slot] = updated;
geom[slot] = BvhGeomTri::from_triangle(&updated);
}
for node_idx in (0..nodes.len()).rev() {
let tc: u32 = nodes[node_idx].get_tri_count();
let flat_idx: usize = node_to_flat[node_idx] as usize;
if tc > 0 {
let first: usize = nodes[node_idx].get_first_tri() as usize;
let end: usize = (first + (tc as usize)).min(count);
let mut bounds: Aabb3D = Aabb3D::empty();
for tri in triangles[first..end].iter() {
bounds.expand_box(tri.get_bounds());
}
nodes[node_idx].set_bounds(bounds);
if let Some(flat_node) = flat_nodes.get_mut(flat_idx) {
let mn: Vec3 = bounds.get_min();
let mx: Vec3 = bounds.get_max();
flat_node.min = [mn.get_x(), mn.get_y(), mn.get_z()];
flat_node.max = [mx.get_x(), mx.get_y(), mx.get_z()];
let mut pkt_idx: usize = flat_node.child_or_first as usize;
let mut cursor: usize = first;
while cursor < end && pkt_idx < leaf_packets.len() {
let next_end: usize = (cursor + 4).min(end);
leaf_packets[pkt_idx] =
BvhTri4::from_geom_chunk(&geom[cursor..next_end], cursor as u32);
cursor = next_end;
pkt_idx += 1;
}
}
} else {
let left_idx: usize = nodes[node_idx].get_left() as usize;
let right_idx: usize = nodes[node_idx].get_right() as usize;
let mut bounds: Aabb3D = nodes[left_idx].get_bounds();
bounds.expand_box(nodes[right_idx].get_bounds());
nodes[node_idx].set_bounds(bounds);
if let Some(flat_node) = flat_nodes.get_mut(flat_idx) {
let mn: Vec3 = bounds.get_min();
let mx: Vec3 = bounds.get_max();
flat_node.min = [mn.get_x(), mn.get_y(), mn.get_z()];
flat_node.max = [mx.get_x(), mx.get_y(), mx.get_z()];
}
}
}
let rmin: Vec3 = nodes[0].get_bounds().get_min();
let rmax: Vec3 = nodes[0].get_bounds().get_max();
*root_min = [rmin.get_x(), rmin.get_y(), rmin.get_z()];
*root_max = [rmax.get_x(), rmax.get_y(), rmax.get_z()];
for (n4, srcs) in bvh4_nodes.iter_mut().zip(bvh4_src_children.iter()) {
let vcnt: usize = n4.valid_count as usize;
for lane in 0..vcnt {
let sidx: usize = srcs[lane] as usize;
if let Some(cn) = nodes.get(sidx) {
let mn: Vec3 = cn.get_bounds().get_min();
let mx: Vec3 = cn.get_bounds().get_max();
n4.min_x[lane] = mn.get_x();
n4.min_y[lane] = mn.get_y();
n4.min_z[lane] = mn.get_z();
n4.max_x[lane] = mx.get_x();
n4.max_y[lane] = mx.get_y();
n4.max_z[lane] = mx.get_z();
}
}
}
#[cfg(target_arch = "x86_64")]
for (n8, srcs) in bvh8_nodes.iter_mut().zip(bvh8_src_children.iter()) {
let vcnt: usize = n8.valid_count as usize;
for lane in 0..vcnt {
let sidx: usize = srcs[lane] as usize;
if let Some(cn) = nodes.get(sidx) {
let mn: Vec3 = cn.get_bounds().get_min();
let mx: Vec3 = cn.get_bounds().get_max();
n8.min_x[lane] = mn.get_x();
n8.min_y[lane] = mn.get_y();
n8.min_z[lane] = mn.get_z();
n8.max_x[lane] = mx.get_x();
n8.max_y[lane] = mx.get_y();
n8.max_z[lane] = mx.get_z();
}
}
}
true
}
#[must_use]
fn vec_axis(v: Vec3, axis: usize) -> f32 {
match axis {
0 => v.get_x(),
1 => v.get_y(),
_ => v.get_z(),
}
}
#[must_use]
fn find_sah_split(
tris: &[BvhTriangle],
cent_bounds: Aabb3D,
parent_area: f32,
) -> Option<(usize, f32)> {
let bins: usize = 12;
let extent: Vec3 = cent_bounds.get_max().sub(cent_bounds.get_min());
let exts: [f32; 3] = [extent.get_x(), extent.get_y(), extent.get_z()];
let cmins: [f32; 3] = [
cent_bounds.get_min().get_x(),
cent_bounds.get_min().get_y(),
cent_bounds.get_min().get_z(),
];
let active: [bool; 3] = [exts[0] >= 1e-5, exts[1] >= 1e-5, exts[2] >= 1e-5];
let scales: [f32; 3] = [
if active[0] {
(bins as f32) / exts[0]
} else {
0.0
},
if active[1] {
(bins as f32) / exts[1]
} else {
0.0
},
if active[2] {
(bins as f32) / exts[2]
} else {
0.0
},
];
let mut bin_bounds: [[Aabb3D; 12]; 3] = [[Aabb3D::empty(); 12]; 3];
let mut bin_counts: [[u32; 12]; 3] = [[0; 12]; 3];
for tri in tris.iter() {
let c: Vec3 = tri.get_centroid();
let tb: Aabb3D = tri.get_bounds();
if active[0] {
let b0: usize = (((c.get_x() - cmins[0]) * scales[0]) as usize).min(bins - 1);
bin_counts[0][b0] += 1;
bin_bounds[0][b0].expand_box(tb);
}
if active[1] {
let b1: usize = (((c.get_y() - cmins[1]) * scales[1]) as usize).min(bins - 1);
bin_counts[1][b1] += 1;
bin_bounds[1][b1].expand_box(tb);
}
if active[2] {
let b2: usize = (((c.get_z() - cmins[2]) * scales[2]) as usize).min(bins - 1);
bin_counts[2][b2] += 1;
bin_bounds[2][b2].expand_box(tb);
}
}
let mut best_cost: f32 = (tris.len() as f32) * parent_area;
let mut best_split: Option<(usize, f32)> = None;
for axis in 0..3 {
if !active[axis] {
continue;
}
let counts: &[u32; 12] = &bin_counts[axis];
let bounds_arr: &[Aabb3D; 12] = &bin_bounds[axis];
let mut left_area: [f32; 11] = [0.0; 11];
let mut left_count: [u32; 11] = [0; 11];
let mut acc_box: Aabb3D = Aabb3D::empty();
let mut acc_cnt: u32 = 0;
for i in 0..(bins - 1) {
acc_cnt += counts[i];
if counts[i] > 0 {
acc_box.expand_box(bounds_arr[i]);
}
left_count[i] = acc_cnt;
left_area[i] = if acc_cnt > 0 {
acc_box.surface_area()
} else {
0.0
};
}
let mut r_box: Aabb3D = Aabb3D::empty();
let mut r_cnt: u32 = 0;
let step: f32 = exts[axis] / (bins as f32);
for i in (1..bins).rev() {
r_cnt += counts[i];
if counts[i] > 0 {
r_box.expand_box(bounds_arr[i]);
}
let l_cnt: u32 = left_count[i - 1];
if l_cnt == 0 || r_cnt == 0 {
continue;
}
let r_area: f32 = r_box.surface_area();
let cost: f32 =
0.5 * parent_area + (l_cnt as f32) * left_area[i - 1] + (r_cnt as f32) * r_area;
if cost < best_cost {
best_cost = cost;
let split_pos: f32 = cmins[axis] + (i as f32) * step;
best_split = Some((axis, split_pos));
}
}
}
best_split
}
fn build_node(&mut self, start: usize, end: usize) -> u32 {
let count: usize = end - start;
let mut bounds: Aabb3D = Aabb3D::empty();
let mut cent_bounds: Aabb3D = Aabb3D::empty();
for tri in self.get_triangles()[start..end].iter() {
bounds.expand_box(tri.get_bounds());
cent_bounds.expand_point(tri.get_centroid());
}
let node_idx: u32 = self.get_nodes().len() as u32;
self.get_mut_nodes().push(BvhNode3D {
bounds,
left: 0,
right: 0,
first_tri: start as u32,
tri_count: count as u32,
});
if count <= 4 {
return node_idx;
}
let parent_area: f32 = bounds.surface_area().max(1e-6);
let split_opt: Option<(usize, f32)> =
Self::find_sah_split(&self.get_triangles()[start..end], cent_bounds, parent_area);
let mid: usize = if let Some((axis, split_pos)) = split_opt {
let Self {
triangles,
orig_indices,
..
} = self;
let slice: &mut [BvhTriangle] = &mut triangles[start..end];
let idx_slice: &mut [u32] = &mut orig_indices[start..end];
let mut i: usize = 0;
let mut j: usize = slice.len();
while i < j {
if Self::vec_axis(slice[i].get_centroid(), axis) < split_pos {
i += 1;
} else {
j -= 1;
slice.swap(i, j);
idx_slice.swap(i, j);
}
}
if i == 0 || i == count {
start + count / 2
} else {
start + i
}
} else if count <= 6 {
return node_idx;
} else {
let extent: Vec3 = cent_bounds.get_max().sub(cent_bounds.get_min());
let axis: usize =
if extent.get_x() >= extent.get_y() && extent.get_x() >= extent.get_z() {
0
} else if extent.get_y() >= extent.get_z() {
1
} else {
2
};
let mid_pos: f32 =
Self::vec_axis(cent_bounds.get_min(), axis) + Self::vec_axis(extent, axis) * 0.5;
let Self {
triangles,
orig_indices,
..
} = self;
let slice: &mut [BvhTriangle] = &mut triangles[start..end];
let idx_slice: &mut [u32] = &mut orig_indices[start..end];
let mut i: usize = 0;
let mut j: usize = slice.len();
while i < j {
if Self::vec_axis(slice[i].get_centroid(), axis) < mid_pos {
i += 1;
} else {
j -= 1;
slice.swap(i, j);
idx_slice.swap(i, j);
}
}
if i == 0 || i == count {
start + count / 2
} else {
start + i
}
};
let left_idx: u32 = self.build_node(start, mid);
let right_idx: u32 = self.build_node(mid, end);
let node: &mut BvhNode3D = &mut self.get_mut_nodes()[node_idx as usize];
node.set_left(left_idx);
node.set_right(right_idx);
node.set_tri_count(0);
node_idx
}
#[inline(always)]
#[must_use]
pub fn trace_any_shadow_cached(
&self,
origin: Vec3,
dir: Vec3,
inv_dir: Vec3,
t_min: f32,
t_max: f32,
cached_tri: &mut u32,
) -> bool {
let Self {
bvh4_nodes,
#[cfg(target_arch = "x86_64")]
bvh8_nodes,
root_min,
root_max,
geom,
leaf_packets,
..
} = self;
if bvh4_nodes.is_empty() || leaf_packets.is_empty() {
return false;
}
let hint: usize = *cached_tri as usize;
if hint < geom.len() && geom[hint].intersect_any_raw(origin, dir, t_min, t_max) {
return true;
}
let orig_arr: [f32; 3] = [origin.get_x(), origin.get_y(), origin.get_z()];
let dir_arr: [f32; 3] = [dir.get_x(), dir.get_y(), dir.get_z()];
let inv_arr: [f32; 3] = [inv_dir.get_x(), inv_dir.get_y(), inv_dir.get_z()];
let neg_orig_inv: [f32; 3] = [
-orig_arr[0] * inv_arr[0],
-orig_arr[1] * inv_arr[1],
-orig_arr[2] * inv_arr[2],
];
if !BvhFlatNode::intersect_box_fma(
*root_min,
*root_max,
inv_arr,
neg_orig_inv,
t_min,
t_max,
)
.is_finite()
{
return false;
}
#[cfg(target_arch = "x86_64")]
if !bvh8_nodes.is_empty() && Bvh8Node::has_avx2_fma() {
return unsafe {
self.trace_any_shadow_bvh8_avx2(
orig_arr,
dir_arr,
inv_arr,
neg_orig_inv,
(t_min, t_max),
cached_tri,
)
};
}
let mut stack: [u32; 64] = [0; 64];
let mut sp: usize = 0;
let mut cur_idx: usize = 0;
loop {
let n: &Bvh4Node = unsafe { bvh4_nodes.get_unchecked(cur_idx) };
let (_, mut mask): ([f32; 4], u32) = n.intersect_4(inv_arr, neg_orig_inv, t_min, t_max);
let mut next_interior: u32 = u32::MAX;
while mask != 0 {
let c: usize = mask.trailing_zeros() as usize;
mask &= mask - 1;
let lc: usize = n.leaf_count[c] as usize;
if lc > 0 {
let first_pkt: usize = n.child[c] as usize;
for pkt_i in first_pkt..(first_pkt + lc) {
let pkt: &BvhTri4 = unsafe { leaf_packets.get_unchecked(pkt_i) };
let hit_tri: u32 = pkt.intersect_any_4(orig_arr, dir_arr, t_min, t_max);
if hit_tri != u32::MAX {
*cached_tri = hit_tri;
return true;
}
}
} else if next_interior == u32::MAX {
next_interior = n.child[c];
} else if sp < 64 {
stack[sp] = n.child[c];
sp += 1;
}
}
if next_interior != u32::MAX {
cur_idx = next_interior as usize;
continue;
}
if sp == 0 {
break;
}
sp -= 1;
cur_idx = stack[sp] as usize;
}
false
}
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2", enable = "fma")]
#[must_use]
unsafe fn trace_any_shadow_bvh8_avx2(
&self,
orig_arr: [f32; 3],
dir_arr: [f32; 3],
inv_arr: [f32; 3],
neg_orig_inv: [f32; 3],
t_range: (f32, f32),
cached_tri: &mut u32,
) -> bool {
let (t_min, t_max): (f32, f32) = t_range;
let Self {
bvh8_nodes,
leaf_packets,
..
} = self;
if bvh8_nodes.is_empty() {
return false;
}
let mut stack: [u32; 96] = [0; 96];
let mut sp: usize = 0;
let mut cur_idx: usize = 0;
loop {
let n: &Bvh8Node = unsafe { bvh8_nodes.get_unchecked(cur_idx) };
let (_, mut mask): ([f32; 8], u32) =
unsafe { n.intersect_8_avx2(inv_arr, neg_orig_inv, t_min, t_max) };
let mut next_interior: u32 = u32::MAX;
while mask != 0 {
let c: usize = mask.trailing_zeros() as usize;
mask &= mask - 1;
let lc: usize = n.leaf_count[c] as usize;
if lc > 0 {
let first_pkt: usize = n.child[c] as usize;
for pkt_i in first_pkt..(first_pkt + lc) {
let pkt: &BvhTri4 = unsafe { leaf_packets.get_unchecked(pkt_i) };
let hit_tri: u32 = pkt.intersect_any_4(orig_arr, dir_arr, t_min, t_max);
if hit_tri != u32::MAX {
*cached_tri = hit_tri;
return true;
}
}
} else if next_interior == u32::MAX {
next_interior = n.child[c];
} else if sp < 96 {
stack[sp] = n.child[c];
sp += 1;
}
}
if next_interior != u32::MAX {
cur_idx = next_interior as usize;
continue;
}
if sp == 0 {
break;
}
sp -= 1;
cur_idx = stack[sp] as usize;
}
false
}
#[inline(always)]
#[must_use]
pub fn trace_any_shadow_packet4(
&self,
pkt: &RayPacket4,
mut active_mask: u8,
t_min: f32,
t_max: f32,
cached_tri: &mut u32,
) -> u8 {
let Self {
bvh4_nodes,
geom,
leaf_packets,
..
} = self;
if active_mask == 0 || bvh4_nodes.is_empty() || leaf_packets.is_empty() {
return 0;
}
let mut occluded: u8 = 0;
let hint: usize = *cached_tri as usize;
if hint < geom.len() {
let hit_mask: u8 = geom[hint].intersect_any_packet4(pkt, t_min, t_max) & active_mask;
occluded |= hit_mask;
active_mask &= !hit_mask;
if active_mask == 0 {
return occluded;
}
}
if active_mask.count_ones() == 1 {
let lane: usize = active_mask.trailing_zeros() as usize;
if self.trace_any_shadow_cached(
Vec3::new(pkt.ox[lane], pkt.oy[lane], pkt.oz[lane]),
Vec3::new(pkt.dx[lane], pkt.dy[lane], pkt.dz[lane]),
Vec3::new(pkt.ix[lane], pkt.iy[lane], pkt.iz[lane]),
t_min,
t_max,
cached_tri,
) {
occluded |= 1_u8 << lane;
}
return occluded;
}
let neg_oi: [[f32; 4]; 3] = [
[
-pkt.ox[0] * pkt.ix[0],
-pkt.ox[1] * pkt.ix[1],
-pkt.ox[2] * pkt.ix[2],
-pkt.ox[3] * pkt.ix[3],
],
[
-pkt.oy[0] * pkt.iy[0],
-pkt.oy[1] * pkt.iy[1],
-pkt.oy[2] * pkt.iy[2],
-pkt.oy[3] * pkt.iy[3],
],
[
-pkt.oz[0] * pkt.iz[0],
-pkt.oz[1] * pkt.iz[1],
-pkt.oz[2] * pkt.iz[2],
-pkt.oz[3] * pkt.iz[3],
],
];
let t_max_4: [f32; 4] = [t_max; 4];
let mut stack: [(u32, u8); 64] = [(0, 0); 64];
let mut sp: usize = 0;
let mut cur_idx: usize = 0;
let mut cur_mask: u8 = active_mask;
loop {
cur_mask &= active_mask;
if cur_mask != 0 {
let n: &Bvh4Node = unsafe { bvh4_nodes.get_unchecked(cur_idx) };
let vcnt: usize = n.valid_count as usize;
let mut next_interior: (u32, u8) = (u32::MAX, 0);
for c in 0..vcnt {
let hit_rays: u8 =
n.intersect_child_packet4(c, pkt, &neg_oi, t_min, t_max_4) & cur_mask;
if hit_rays == 0 {
continue;
}
let lc: usize = n.leaf_count[c] as usize;
if lc > 0 {
let first_pkt: usize = n.child[c] as usize;
let mut rem_rays: u8 = hit_rays & active_mask;
while rem_rays != 0 {
let r: usize = rem_rays.trailing_zeros() as usize;
rem_rays &= rem_rays - 1;
let orig_r: [f32; 3] = [pkt.ox[r], pkt.oy[r], pkt.oz[r]];
let dir_r: [f32; 3] = [pkt.dx[r], pkt.dy[r], pkt.dz[r]];
for pkt_i in first_pkt..(first_pkt + lc) {
let tp: &BvhTri4 = unsafe { leaf_packets.get_unchecked(pkt_i) };
let hit_tri: u32 = tp.intersect_any_4(orig_r, dir_r, t_min, t_max);
if hit_tri != u32::MAX {
*cached_tri = hit_tri;
let bit: u8 = 1_u8 << r;
occluded |= bit;
active_mask &= !bit;
cur_mask &= !bit;
break;
}
}
}
if active_mask == 0 {
return occluded;
}
} else if next_interior.0 == u32::MAX {
next_interior = (n.child[c], hit_rays);
} else if sp < 64 {
stack[sp] = (n.child[c], hit_rays);
sp += 1;
}
}
if next_interior.0 != u32::MAX && (next_interior.1 & active_mask) != 0 {
cur_idx = next_interior.0 as usize;
cur_mask = next_interior.1 & active_mask;
continue;
}
}
let mut popped: bool = false;
while sp > 0 {
sp -= 1;
let (nid, nmask): (u32, u8) = stack[sp];
let live: u8 = nmask & active_mask;
if live != 0 {
cur_idx = nid as usize;
cur_mask = live;
popped = true;
break;
}
}
if !popped {
break;
}
}
occluded
}
#[inline(always)]
pub fn trace_closest_packet4(
&self,
pkt: &RayPacket4,
active_mask: u8,
t_min: f32,
best_t: &mut [f32; 4],
best_idx: &mut [usize; 4],
) {
let Self {
bvh4_nodes,
leaf_packets,
..
} = self;
if active_mask == 0 || bvh4_nodes.is_empty() || leaf_packets.is_empty() {
return;
}
if active_mask.count_ones() == 1 {
let r: usize = active_mask.trailing_zeros() as usize;
if let Some((t, idx)) = self.trace_closest_raw(
Vec3::new(pkt.ox[r], pkt.oy[r], pkt.oz[r]),
Vec3::new(pkt.dx[r], pkt.dy[r], pkt.dz[r]),
Vec3::new(pkt.ix[r], pkt.iy[r], pkt.iz[r]),
t_min,
best_t[r],
) {
best_t[r] = t;
best_idx[r] = idx;
}
return;
}
let neg_oi: [[f32; 4]; 3] = [
[
-pkt.ox[0] * pkt.ix[0],
-pkt.ox[1] * pkt.ix[1],
-pkt.ox[2] * pkt.ix[2],
-pkt.ox[3] * pkt.ix[3],
],
[
-pkt.oy[0] * pkt.iy[0],
-pkt.oy[1] * pkt.iy[1],
-pkt.oy[2] * pkt.iy[2],
-pkt.oy[3] * pkt.iy[3],
],
[
-pkt.oz[0] * pkt.iz[0],
-pkt.oz[1] * pkt.iz[1],
-pkt.oz[2] * pkt.iz[2],
-pkt.oz[3] * pkt.iz[3],
],
];
let mut stack: [(u32, u8, f32); 64] = [(0, 0, 0.0); 64];
let mut s_stack: [(u32, f32); 64] = [(0, 0.0); 64];
let mut sp: usize = 0;
let mut cur_idx: usize = 0;
let mut cur_mask: u8 = active_mask;
loop {
if cur_mask.count_ones() == 1 {
let r: usize = cur_mask.trailing_zeros() as usize;
let orig_r: [f32; 3] = [pkt.ox[r], pkt.oy[r], pkt.oz[r]];
let dir_r: [f32; 3] = [pkt.dx[r], pkt.dy[r], pkt.dz[r]];
let inv_r: [f32; 3] = [pkt.ix[r], pkt.iy[r], pkt.iz[r]];
let neg_r: [f32; 3] = [neg_oi[0][r], neg_oi[1][r], neg_oi[2][r]];
let mut r_best_t: f32 = best_t[r];
let mut r_best_idx: usize = best_idx[r];
let mut s_sp: usize = 0;
let mut s_cur: usize = cur_idx;
loop {
let sn: &Bvh4Node = unsafe { bvh4_nodes.get_unchecked(s_cur) };
let (t_near, mut smask): ([f32; 4], u32) =
sn.intersect_4(inv_r, neg_r, t_min, r_best_t);
let mut sh_hits: [(u32, f32); 4] = [(0, 0.0); 4];
let mut sh_len: usize = 0;
while smask != 0 {
let c: usize = smask.trailing_zeros() as usize;
smask &= smask - 1;
let tc: f32 = t_near[c];
if tc >= r_best_t {
continue;
}
let lc: usize = sn.leaf_count[c] as usize;
if lc > 0 {
let first_pkt: usize = sn.child[c] as usize;
for pkt_i in first_pkt..(first_pkt + lc) {
let tp: &BvhTri4 = unsafe { leaf_packets.get_unchecked(pkt_i) };
tp.intersect_closest_4(
orig_r,
dir_r,
t_min,
&mut r_best_t,
&mut r_best_idx,
);
}
} else {
sh_hits[sh_len] = (sn.child[c], tc);
sh_len += 1;
}
}
if sh_len > 0 {
if sh_len > 1 {
for i in 1..sh_len {
let key: (u32, f32) = sh_hits[i];
let mut j: usize = i;
while j > 0 && sh_hits[j - 1].1 < key.1 {
sh_hits[j] = sh_hits[j - 1];
j -= 1;
}
sh_hits[j] = key;
}
}
for hit in sh_hits.iter().take(sh_len - 1) {
if s_sp < 64 {
s_stack[s_sp] = *hit;
s_sp += 1;
}
}
s_cur = sh_hits[sh_len - 1].0 as usize;
continue;
}
let mut popped_single: bool = false;
while s_sp > 0 {
s_sp -= 1;
let (nid, nt): (u32, f32) = s_stack[s_sp];
if nt < r_best_t {
s_cur = nid as usize;
popped_single = true;
break;
}
}
if !popped_single {
break;
}
}
best_t[r] = r_best_t;
best_idx[r] = r_best_idx;
} else {
let n: &Bvh4Node = unsafe { bvh4_nodes.get_unchecked(cur_idx) };
let vcnt: usize = n.valid_count as usize;
let mut hits: [(u32, u8, f32); 4] = [(0, 0, 0.0); 4];
let mut hit_len: usize = 0;
for c in 0..vcnt {
let (raw_rays, min_t): (u8, f32) =
n.intersect_child_packet4_near(c, pkt, &neg_oi, t_min, *best_t);
let hit_rays: u8 = raw_rays & cur_mask;
if hit_rays == 0 {
continue;
}
let lc: usize = n.leaf_count[c] as usize;
if lc > 0 {
let first_pkt: usize = n.child[c] as usize;
let mut rem_rays: u8 = hit_rays;
while rem_rays != 0 {
let r: usize = rem_rays.trailing_zeros() as usize;
rem_rays &= rem_rays - 1;
let orig_r: [f32; 3] = [pkt.ox[r], pkt.oy[r], pkt.oz[r]];
let dir_r: [f32; 3] = [pkt.dx[r], pkt.dy[r], pkt.dz[r]];
for pkt_i in first_pkt..(first_pkt + lc) {
let tp: &BvhTri4 = unsafe { leaf_packets.get_unchecked(pkt_i) };
tp.intersect_closest_4(
orig_r,
dir_r,
t_min,
&mut best_t[r],
&mut best_idx[r],
);
}
}
} else {
hits[hit_len] = (n.child[c], hit_rays, min_t);
hit_len += 1;
}
}
if hit_len > 0 {
if hit_len > 1 {
for i in 1..hit_len {
let key: (u32, u8, f32) = hits[i];
let mut j: usize = i;
while j > 0 && hits[j - 1].2 < key.2 {
hits[j] = hits[j - 1];
j -= 1;
}
hits[j] = key;
}
}
for hit in hits.iter().take(hit_len - 1) {
if sp < 64 {
stack[sp] = *hit;
sp += 1;
}
}
let nearest: (u32, u8, f32) = hits[hit_len - 1];
cur_idx = nearest.0 as usize;
cur_mask = nearest.1;
continue;
}
}
let mut found_next: bool = false;
while sp > 0 {
sp -= 1;
let (nid, nmask, nt): (u32, u8, f32) = stack[sp];
let mut max_bt: f32 = 0.0;
let mut m: u8 = nmask;
while m != 0 {
let r: usize = m.trailing_zeros() as usize;
m &= m - 1;
max_bt = max_bt.max(best_t[r]);
}
if nt < max_bt {
cur_idx = nid as usize;
cur_mask = nmask;
found_next = true;
break;
}
}
if !found_next {
break;
}
}
}
#[must_use]
pub fn trace_any_shadow(&self, ray: &Ray3D, t_min: f32, t_max: f32) -> bool {
let mut cache: u32 = u32::MAX;
self.trace_any_shadow_cached(
ray.get_origin(),
ray.get_direction(),
ray.get_inv_direction(),
t_min,
t_max,
&mut cache,
)
}
#[inline(always)]
#[must_use]
pub fn trace_closest_raw(
&self,
origin: Vec3,
dir: Vec3,
inv_dir: Vec3,
t_min: f32,
t_max: f32,
) -> Option<(f32, usize)> {
let Self {
bvh4_nodes,
#[cfg(target_arch = "x86_64")]
bvh8_nodes,
root_min,
root_max,
leaf_packets,
..
} = self;
if bvh4_nodes.is_empty() || leaf_packets.is_empty() {
return None;
}
let orig_arr: [f32; 3] = [origin.get_x(), origin.get_y(), origin.get_z()];
let dir_arr: [f32; 3] = [dir.get_x(), dir.get_y(), dir.get_z()];
let inv_arr: [f32; 3] = [inv_dir.get_x(), inv_dir.get_y(), inv_dir.get_z()];
let neg_orig_inv: [f32; 3] = [
-orig_arr[0] * inv_arr[0],
-orig_arr[1] * inv_arr[1],
-orig_arr[2] * inv_arr[2],
];
if !BvhFlatNode::intersect_box_fma(
*root_min,
*root_max,
inv_arr,
neg_orig_inv,
t_min,
t_max,
)
.is_finite()
{
return None;
}
#[cfg(target_arch = "x86_64")]
if !bvh8_nodes.is_empty() && Bvh8Node::has_avx2_fma() {
return unsafe {
self.trace_closest_bvh8_avx2(orig_arr, dir_arr, inv_arr, neg_orig_inv, t_min, t_max)
};
}
let mut stack: [(u32, f32); 64] = [(0, 0.0); 64];
let mut sp: usize = 0;
let mut cur_idx: usize = 0;
let mut best_t: f32 = t_max;
let mut best_idx: usize = usize::MAX;
loop {
let n: &Bvh4Node = unsafe { bvh4_nodes.get_unchecked(cur_idx) };
let (t_near, mut mask): ([f32; 4], u32) =
n.intersect_4(inv_arr, neg_orig_inv, t_min, best_t);
let mut hits: [(u32, f32); 4] = [(0, 0.0); 4];
let mut hit_len: usize = 0;
while mask != 0 {
let c: usize = mask.trailing_zeros() as usize;
mask &= mask - 1;
let tc: f32 = t_near[c];
if tc >= best_t {
continue;
}
let lc: usize = n.leaf_count[c] as usize;
if lc > 0 {
let first_pkt: usize = n.child[c] as usize;
for pkt_i in first_pkt..(first_pkt + lc) {
let pkt: &BvhTri4 = unsafe { leaf_packets.get_unchecked(pkt_i) };
pkt.intersect_closest_4(
orig_arr,
dir_arr,
t_min,
&mut best_t,
&mut best_idx,
);
}
} else {
hits[hit_len] = (n.child[c], tc);
hit_len += 1;
}
}
if hit_len > 0 {
if hit_len > 1 {
for i in 1..hit_len {
let key: (u32, f32) = hits[i];
let mut j: usize = i;
while j > 0 && hits[j - 1].1 < key.1 {
hits[j] = hits[j - 1];
j -= 1;
}
hits[j] = key;
}
}
let near_last: usize = hit_len - 1;
for item in hits.iter().take(near_last) {
if item.1 < best_t && sp < 64 {
stack[sp] = *item;
sp += 1;
}
}
if hits[near_last].1 < best_t {
cur_idx = hits[near_last].0 as usize;
continue;
}
}
let mut found_next: bool = false;
while sp > 0 {
sp -= 1;
let (next_id, next_tmin): (u32, f32) = stack[sp];
if next_tmin < best_t {
cur_idx = next_id as usize;
found_next = true;
break;
}
}
if !found_next {
break;
}
}
if best_idx == usize::MAX {
None
} else {
Some((best_t, best_idx))
}
}
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2", enable = "fma")]
#[must_use]
unsafe fn trace_closest_bvh8_avx2(
&self,
orig_arr: [f32; 3],
dir_arr: [f32; 3],
inv_arr: [f32; 3],
neg_orig_inv: [f32; 3],
t_min: f32,
t_max: f32,
) -> Option<(f32, usize)> {
let Self {
bvh8_nodes,
leaf_packets,
..
} = self;
if bvh8_nodes.is_empty() {
return None;
}
let mut stack: [(u32, f32); 96] = [(0, 0.0); 96];
let mut sp: usize = 0;
let mut cur_idx: usize = 0;
let mut best_t: f32 = t_max;
let mut best_idx: usize = usize::MAX;
loop {
let n: &Bvh8Node = unsafe { bvh8_nodes.get_unchecked(cur_idx) };
let (t_near, mut mask): ([f32; 8], u32) =
unsafe { n.intersect_8_avx2(inv_arr, neg_orig_inv, t_min, best_t) };
let mut hits: [(u32, f32); 8] = [(0, 0.0); 8];
let mut hit_len: usize = 0;
while mask != 0 {
let c: usize = mask.trailing_zeros() as usize;
mask &= mask - 1;
let tc: f32 = t_near[c];
if tc >= best_t {
continue;
}
let lc: usize = n.leaf_count[c] as usize;
if lc > 0 {
let first_pkt: usize = n.child[c] as usize;
for pkt_i in first_pkt..(first_pkt + lc) {
let pkt: &BvhTri4 = unsafe { leaf_packets.get_unchecked(pkt_i) };
pkt.intersect_closest_4(
orig_arr,
dir_arr,
t_min,
&mut best_t,
&mut best_idx,
);
}
} else {
hits[hit_len] = (n.child[c], tc);
hit_len += 1;
}
}
if hit_len > 0 {
if hit_len > 1 {
for i in 1..hit_len {
let key: (u32, f32) = hits[i];
let mut j: usize = i;
while j > 0 && hits[j - 1].1 < key.1 {
hits[j] = hits[j - 1];
j -= 1;
}
hits[j] = key;
}
}
let near_last: usize = hit_len - 1;
for item in hits.iter().take(near_last) {
if item.1 < best_t && sp < 96 {
stack[sp] = *item;
sp += 1;
}
}
if hits[near_last].1 < best_t {
cur_idx = hits[near_last].0 as usize;
continue;
}
}
let mut found_next: bool = false;
while sp > 0 {
sp -= 1;
let (next_id, next_tmin): (u32, f32) = stack[sp];
if next_tmin < best_t {
cur_idx = next_id as usize;
found_next = true;
break;
}
}
if !found_next {
break;
}
}
if best_idx == usize::MAX {
None
} else {
Some((best_t, best_idx))
}
}
#[must_use]
pub fn trace_closest(&self, ray: &Ray3D, t_min: f32, t_max: f32) -> Option<RayHit3D> {
let (t, idx): (f32, usize) = self.trace_closest_raw(
ray.get_origin(),
ray.get_direction(),
ray.get_inv_direction(),
t_min,
t_max,
)?;
let tri: &BvhTriangle = &self.get_triangles()[idx];
let pt: Vec3 = ray.get_origin().add(ray.get_direction().scale(t));
Some(RayHit3D {
distance: t,
point: pt,
normal: tri.get_normal(),
material: tri.get_material(),
})
}
}