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/// Flat-array Bounding Volume Hierarchy accelerating ray-traced shadows, reflections, and GI in O(log N).
118#[derive(Clone, Data, Debug, Default)]
119pub struct Bvh3D {
120    /// Contiguous BVH nodes with root at index `0`.
121    pub(crate) nodes: Vec<BvhNode3D>,
122    /// Partitioned world-space triangle metadata and shading materials.
123    pub(crate) triangles: Vec<BvhTriangle>,
124    /// Hot 36-byte packed geometry mirror aligned 1:1 with `triangles` for fast single-triangle occluder hits.
125    #[get(skip)]
126    #[get_mut(skip)]
127    #[set(skip)]
128    pub(crate) geom: Vec<BvhGeomTri>,
129    /// 4-wide SoA leaf triangle packets referenced by `BvhFlatNode` leaves for SIMD ray intersection.
130    #[get(skip)]
131    #[get_mut(skip)]
132    #[set(skip)]
133    pub(crate) leaf_packets: Vec<BvhTri4>,
134    /// Hot 32-byte DFS-ordered BVH mirror for branchless cache-dense FMA traversal.
135    #[get(skip)]
136    #[get_mut(skip)]
137    #[set(skip)]
138    pub(crate) flat_nodes: Vec<BvhFlatNode>,
139}