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