Skip to main content

euv_core/game/engine3d/bvh/
struct.rs

1use super::*;
2
3/// World-space triangle stored in the flat BVH leaf array for ray-traced shadows, reflections, and GI.
4#[derive(Clone, Copy, Data, Debug, PartialEq)]
5pub struct BvhTriangle {
6    /// First vertex position in world space.
7    #[get(type(copy))]
8    pub(crate) v0: Vec3,
9    /// Edge vector `v1 - v0`.
10    #[get(type(copy))]
11    pub(crate) edge1: Vec3,
12    /// Edge vector `v2 - v0`.
13    #[get(type(copy))]
14    pub(crate) edge2: Vec3,
15    /// Unit face normal.
16    #[get(type(copy))]
17    pub(crate) normal: Vec3,
18    /// Precomputed triangle centroid for O(N log N) BVH partitioning.
19    #[get(type(copy))]
20    pub(crate) centroid: Vec3,
21    /// Precomputed triangle bounding box.
22    #[get(type(copy))]
23    pub(crate) bounds: AxisAlignedBox3D,
24    /// Surface PBR material referenced on reflection/GI hits.
25    #[get(type(copy))]
26    pub(crate) material: PbrMaterial,
27    /// Per-triangle material slot (`0` = primary instance material, `> 0` = fixed mesh sub-material).
28    #[get(type(copy))]
29    pub(crate) material_slot: u16,
30}
31
32/// Cache-friendly flat BVH tree node (interior node or leaf triangle range).
33#[derive(Clone, Copy, Data, Debug, PartialEq)]
34pub struct BvhNode3D {
35    /// World-space axis-aligned bounding box enclosing this subtree.
36    #[get(type(copy))]
37    pub(crate) bounds: AxisAlignedBox3D,
38    /// Left child node index (`0` for leaves).
39    #[get(type(copy))]
40    pub(crate) left: u32,
41    /// Right child node index (`0` for leaves).
42    #[get(type(copy))]
43    pub(crate) right: u32,
44    /// First triangle index in `Bvh3D::triangles` for leaves.
45    #[get(type(copy))]
46    pub(crate) first_tri: u32,
47    /// Triangle count (`> 0` denotes a leaf node).
48    #[get(type(copy))]
49    pub(crate) tri_count: u32,
50}
51
52/// Closest ray-triangle hit result returned by `Bvh3D::trace_closest`.
53#[derive(Clone, Copy, Data, Debug, PartialEq)]
54pub struct RayHit3D {
55    /// Ray parametric hit distance `t`.
56    #[get(type(copy))]
57    pub(crate) distance: f32,
58    /// World-space hit position.
59    #[get(type(copy))]
60    pub(crate) point: Vec3,
61    /// Unit surface normal at the hit point.
62    #[get(type(copy))]
63    pub(crate) normal: Vec3,
64    /// Surface PBR material at the hit point.
65    #[get(type(copy))]
66    pub(crate) material: PbrMaterial,
67}
68
69/// Cache-compact 36-byte hot geometry triangle for L1-dense Möller–Trumbore ray intersections.
70#[derive(Clone, Copy, Debug, PartialEq)]
71pub(crate) struct BvhGeomTri {
72    /// First vertex `(x, y, z)`.
73    pub(crate) v0: [f32; 3],
74    /// First edge `(v1 - v0)`.
75    pub(crate) e1: [f32; 3],
76    /// Second edge `(v2 - v0)`.
77    pub(crate) e2: [f32; 3],
78}
79
80/// 4-wide Structure-of-Arrays leaf triangle packet enabling auto-vectorized 4-lane Möller–Trumbore ray tests.
81#[repr(C, align(16))]
82#[derive(Clone, Copy, Debug, PartialEq)]
83pub(crate) struct BvhTri4 {
84    /// Four triangle `v0.x` coordinates.
85    pub(crate) v0x: [f32; 4],
86    /// Four triangle `v0.y` coordinates.
87    pub(crate) v0y: [f32; 4],
88    /// Four triangle `v0.z` coordinates.
89    pub(crate) v0z: [f32; 4],
90    /// Four triangle `edge1.x` components (`0.0` for inactive padded lanes).
91    pub(crate) e1x: [f32; 4],
92    /// Four triangle `edge1.y` components (`0.0` for inactive padded lanes).
93    pub(crate) e1y: [f32; 4],
94    /// Four triangle `edge1.z` components (`0.0` for inactive padded lanes).
95    pub(crate) e1z: [f32; 4],
96    /// Four triangle `edge2.x` components (`0.0` for inactive padded lanes).
97    pub(crate) e2x: [f32; 4],
98    /// Four triangle `edge2.y` components (`0.0` for inactive padded lanes).
99    pub(crate) e2y: [f32; 4],
100    /// Four triangle `edge2.z` components (`0.0` for inactive padded lanes).
101    pub(crate) e2z: [f32; 4],
102    /// Global triangle indices inside `Bvh3D::triangles` for each lane (`u32::MAX` for padded lanes).
103    pub(crate) tri_indices: [u32; 4],
104}
105
106/// Cache-line-paired 32-byte BVH node storing sibling pairs `(2k, 2k + 1)` together in one 64-byte cache line.
107#[repr(C, align(32))]
108#[derive(Clone, Copy, Debug, PartialEq)]
109pub(crate) struct BvhFlatNode {
110    /// Minimum axis-aligned bounding box corner `(x, y, z)`.
111    pub(crate) min: [f32; 3],
112    /// Even index of the contiguous child pair `(first_child, first_child + 1)` when `tri_count == 0`, or first `BvhTri4` packet index when `tri_count > 0`.
113    pub(crate) child_or_first: u32,
114    /// Maximum axis-aligned bounding box corner `(x, y, z)`.
115    pub(crate) max: [f32; 3],
116    /// Leaf `BvhTri4` packet count (`0` for interior nodes).
117    pub(crate) tri_count: u32,
118}
119
120/// Compact 128-byte 4-wide SoA BVH node (`BVH4`) storing four child axis-aligned bounding boxes in one cache line for NEON/SSE2 4-box SIMD traversal.
121#[repr(C, align(64))]
122#[derive(Clone, Copy, Debug, PartialEq)]
123pub(crate) struct Bvh4Node {
124    /// Four child axis-aligned bounding box minimum X coordinates.
125    pub(crate) min_x: [f32; 4],
126    /// Four child axis-aligned bounding box minimum Y coordinates.
127    pub(crate) min_y: [f32; 4],
128    /// Four child axis-aligned bounding box minimum Z coordinates.
129    pub(crate) min_z: [f32; 4],
130    /// Four child axis-aligned bounding box maximum X coordinates.
131    pub(crate) max_x: [f32; 4],
132    /// Four child axis-aligned bounding box maximum Y coordinates.
133    pub(crate) max_y: [f32; 4],
134    /// Four child axis-aligned bounding box maximum Z coordinates.
135    pub(crate) max_z: [f32; 4],
136    /// Child `Bvh4Node` index when `leaf_count[c] == 0`, or first `BvhTri4` packet index when `leaf_count[c] > 0`.
137    pub(crate) child: [u32; 4],
138    /// Number of `BvhTri4` leaf packets for each child (`0` denotes an interior child node).
139    pub(crate) leaf_count: [u8; 4],
140    /// Number of populated child lanes (`1..=4`).
141    pub(crate) valid_count: u8,
142    /// Padding aligning `Bvh4Node` to 128 bytes.
143    pub(crate) _pad: [u8; 11],
144}
145
146/// Compact 256-byte 8-wide SoA BVH node (`BVH8`) storing eight child axis-aligned bounding boxes for x86_64 AVX2 8-box SIMD traversal.
147#[cfg(target_arch = "x86_64")]
148#[repr(C, align(64))]
149#[derive(Clone, Copy, Debug, PartialEq)]
150pub(crate) struct Bvh8Node {
151    /// Eight child axis-aligned bounding box minimum X coordinates.
152    pub(crate) min_x: [f32; 8],
153    /// Eight child axis-aligned bounding box minimum Y coordinates.
154    pub(crate) min_y: [f32; 8],
155    /// Eight child axis-aligned bounding box minimum Z coordinates.
156    pub(crate) min_z: [f32; 8],
157    /// Eight child axis-aligned bounding box maximum X coordinates.
158    pub(crate) max_x: [f32; 8],
159    /// Eight child axis-aligned bounding box maximum Y coordinates.
160    pub(crate) max_y: [f32; 8],
161    /// Eight child axis-aligned bounding box maximum Z coordinates.
162    pub(crate) max_z: [f32; 8],
163    /// Child `Bvh8Node` index when `leaf_count[c] == 0`, or first `BvhTri4` packet index when `leaf_count[c] > 0`.
164    pub(crate) child: [u32; 8],
165    /// Number of `BvhTri4` leaf packets for each child (`0` denotes an interior child node).
166    pub(crate) leaf_count: [u8; 8],
167    /// Number of populated child lanes (`1..=8`).
168    pub(crate) valid_count: u8,
169    /// Padding aligning `Bvh8Node` to 256 bytes.
170    pub(crate) _pad: [u8; 23],
171}
172
173/// Four-ray Structure-of-Arrays coherent ray packet for simultaneous 2x2 probe traversal.
174#[derive(Clone, Copy, Debug)]
175pub struct RayPacket4 {
176    /// Ray origins X across 4 lanes.
177    pub(crate) ox: [f32; 4],
178    /// Ray origins Y across 4 lanes.
179    pub(crate) oy: [f32; 4],
180    /// Ray origins Z across 4 lanes.
181    pub(crate) oz: [f32; 4],
182    /// Ray directions X across 4 lanes.
183    pub(crate) dx: [f32; 4],
184    /// Ray directions Y across 4 lanes.
185    pub(crate) dy: [f32; 4],
186    /// Ray directions Z across 4 lanes.
187    pub(crate) dz: [f32; 4],
188    /// Safe reciprocal directions X across 4 lanes.
189    pub(crate) ix: [f32; 4],
190    /// Safe reciprocal directions Y across 4 lanes.
191    pub(crate) iy: [f32; 4],
192    /// Safe reciprocal directions Z across 4 lanes.
193    pub(crate) iz: [f32; 4],
194}
195
196/// Flat-array Bounding Volume Hierarchy accelerating ray-traced shadows, reflections, and GI in O(log N).
197#[derive(Clone, Data, Debug, Default)]
198pub struct Bvh3D {
199    /// Contiguous BVH nodes with root at index `0`.
200    pub(crate) nodes: Vec<BvhNode3D>,
201    /// Partitioned world-space triangle metadata and shading materials.
202    pub(crate) triangles: Vec<BvhTriangle>,
203    /// Hot 36-byte packed geometry mirror aligned 1:1 with `triangles` for fast single-triangle occluder hits.
204    #[get(skip)]
205    #[get_mut(skip)]
206    #[set(skip)]
207    pub(crate) geom: Vec<BvhGeomTri>,
208    /// 4-wide SoA leaf triangle packets referenced by `Bvh4Node` and `BvhFlatNode` leaves for SIMD ray intersection.
209    #[get(skip)]
210    #[get_mut(skip)]
211    #[set(skip)]
212    pub(crate) leaf_packets: Vec<BvhTri4>,
213    /// Hot 32-byte DFS-ordered BVH mirror preserved for incremental refit consistency.
214    #[get(skip)]
215    #[get_mut(skip)]
216    #[set(skip)]
217    pub(crate) flat_nodes: Vec<BvhFlatNode>,
218    /// Compact 4-wide SoA BVH4 tree collapsed from `nodes` for SIMD 4-child traversal.
219    #[get(skip)]
220    #[get_mut(skip)]
221    #[set(skip)]
222    pub(crate) bvh4_nodes: Vec<Bvh4Node>,
223    /// Source binary `nodes` index corresponding to each `(bvh4_node, lane)` slot for zero-alloc O(N) refits.
224    #[get(skip)]
225    #[get_mut(skip)]
226    #[set(skip)]
227    pub(crate) bvh4_src_children: Vec<[u32; 4]>,
228    /// Root axis-aligned bounding box cached for O(1) miss rejection before entering `bvh4_nodes`.
229    #[get(skip)]
230    #[get_mut(skip)]
231    #[set(skip)]
232    pub(crate) root_min: [f32; 3],
233    /// Root axis-aligned bounding box maximum corner cached for O(1) miss rejection before entering `bvh4_nodes`.
234    #[get(skip)]
235    #[get_mut(skip)]
236    #[set(skip)]
237    pub(crate) root_max: [f32; 3],
238    /// Compact 8-wide SoA BVH8 tree collapsed from `nodes` for x86_64 AVX2 traversal.
239    #[cfg(target_arch = "x86_64")]
240    #[get(skip)]
241    #[get_mut(skip)]
242    #[set(skip)]
243    pub(crate) bvh8_nodes: Vec<Bvh8Node>,
244    /// Source binary `nodes` index corresponding to each `(bvh8_node, lane)` slot for zero-alloc O(N) refits.
245    #[cfg(target_arch = "x86_64")]
246    #[get(skip)]
247    #[get_mut(skip)]
248    #[set(skip)]
249    pub(crate) bvh8_src_children: Vec<[u32; 8]>,
250    /// Mapping from partitioned `triangles[slot]` index back to its original caller triangle index for O(N) incremental refit.
251    #[get(skip)]
252    #[get_mut(skip)]
253    #[set(skip)]
254    pub(crate) orig_indices: Vec<u32>,
255    /// Mapping from `nodes[src_idx]` index to its flat mirror slot in `flat_nodes` for O(N) bottom-up refit.
256    #[get(skip)]
257    #[get_mut(skip)]
258    #[set(skip)]
259    pub(crate) node_to_flat: Vec<u32>,
260    /// Baseline SAH tree surface-area cost captured at build time to detect refit degradation.
261    #[get(type(copy))]
262    pub(crate) build_sah_cost: f32,
263}