Skip to main content

euv_core/game/engine3d/rhi/
struct.rs

1use super::*;
2
3/// Transient GPU/CPU render-target resource declared inside `RenderGraph3D` for automatic lifetime aliasing.
4#[derive(Clone, Data, Debug, PartialEq)]
5pub struct RenderGraphResource3D {
6    /// Numeric transient resource identifier (`0..resource_count`).
7    #[get(type(copy))]
8    pub(crate) resource_id: usize,
9    /// Human-readable resource label (e.g., `gbuffer_depth`, `lumen_probes`).
10    pub(crate) name: String,
11    /// Estimated byte footprint of this render target or storage buffer.
12    #[get(type(copy))]
13    pub(crate) size_bytes: usize,
14    /// Whether this resource is an external final output that anchors pass liveness.
15    #[get(type(copy))]
16    pub(crate) is_external_output: bool,
17}
18
19/// Declarative render pass node inside `RenderGraph3D` declaring read/write resource dependencies.
20#[derive(Clone, Data, Debug, PartialEq)]
21pub struct RenderGraphPass3D {
22    /// Numeric pass identifier (`0..pass_count`).
23    #[get(type(copy))]
24    pub(crate) pass_id: usize,
25    /// Canonical UE5 render pass category.
26    #[get(type(copy))]
27    pub(crate) kind: RenderPassKind3D,
28    /// Human-readable diagnostic pass name.
29    pub(crate) name: String,
30    /// Resource IDs consumed as read-only inputs by this pass.
31    pub(crate) reads: Vec<usize>,
32    /// Resource IDs written or produced by this pass.
33    pub(crate) writes: Vec<usize>,
34    /// Last measured execution time of this pass in microseconds.
35    #[get(type(copy))]
36    pub(crate) last_duration_us: u64,
37}
38
39/// Compiled execution schedule and transient memory aliasing report produced by `RenderGraph3D::compile`.
40#[derive(Clone, Data, Debug, Default, PartialEq)]
41pub struct RenderGraphSchedule3D {
42    /// Topologically ordered active pass IDs surviving dead-pass culling.
43    pub(crate) ordered_passes: Vec<usize>,
44    /// Total pass count eliminated by dead-pass culling.
45    #[get(type(copy))]
46    pub(crate) culled_pass_count: usize,
47    /// Unaliased sum of all active transient resource byte sizes.
48    #[get(type(copy))]
49    pub(crate) unaliased_bytes: usize,
50    /// Peak aliased transient memory footprint after greedy lifetime packing.
51    #[get(type(copy))]
52    pub(crate) aliased_peak_bytes: usize,
53    /// Physical memory pool slot assigned to each transient resource ID (`usize::MAX` if unused).
54    pub(crate) resource_pool_slots: Vec<usize>,
55}
56
57/// UE5 Render Dependency Graph (RDG) managing declarative pass scheduling, automatic dead-pass culling, and transient buffer aliasing.
58#[derive(Clone, Data, Debug, Default)]
59pub struct RenderGraph3D {
60    /// Declared transient and external frame resources.
61    pub(crate) resources: Vec<RenderGraphResource3D>,
62    /// Declared frame render passes.
63    pub(crate) passes: Vec<RenderGraphPass3D>,
64}
65
66/// 112-byte GPU-aligned instanced 3D object payload matching `WGSL`/`MSL`/`GLSL`/`HLSL` `InstanceData` layouts.
67#[repr(C, align(16))]
68#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
69pub struct GpuInstanceData3D {
70    /// Column-major 4x4 model matrix elements.
71    #[get(type(copy))]
72    pub(crate) model_matrix: [f32; 16],
73    /// Linear base albedo RGB and metallic factor in `.w`.
74    #[get(type(copy))]
75    pub(crate) albedo_metallic: [f32; 4],
76    /// Linear HDR emissive RGB and roughness factor in `.w`.
77    #[get(type(copy))]
78    pub(crate) emissive_roughness: [f32; 4],
79    /// Material slot, bone palette offset, morph weight offset, and instance flags `[mat_id, skin_off, morph_off, flags]`.
80    #[get(type(copy))]
81    pub(crate) material_skin_morph_flags: [u32; 4],
82}
83
84/// Deduplicated GPU instanced mesh draw batch emitted from `World3D` for hardware RHI submission.
85#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
86pub struct GpuMeshDrawBatch3D {
87    /// Registered mesh identifier in `World3D::mesh_library`.
88    #[get(type(copy))]
89    pub(crate) mesh_id: usize,
90    /// Selected LOD tier index (`0..=2`).
91    #[get(type(copy))]
92    pub(crate) lod_index: usize,
93    /// First instance index inside `GpuFramePacket3D::instances`.
94    #[get(type(copy))]
95    pub(crate) first_instance: u32,
96    /// Number of consecutive instanced objects sharing this mesh/LOD batch.
97    #[get(type(copy))]
98    pub(crate) instance_count: u32,
99    /// Triangle count per single instance of this mesh LOD.
100    #[get(type(copy))]
101    pub(crate) triangles_per_instance: u32,
102}
103
104/// Reusable sorting scratch arena enabling zero-allocation `GpuFramePacket3D::build_from_world_into` updates.
105#[derive(Clone, Data, Debug, Default)]
106pub struct GpuFramePacketScratch3D {
107    /// Reusable instance sort key table `(mesh_id, lod_idx, tri_count, instance_data)`.
108    #[get(skip)]
109    #[get_mut(skip)]
110    #[set(skip)]
111    pub(crate) keyed_instances: Vec<(usize, usize, u32, GpuInstanceData3D)>,
112    /// Reusable skin matrix evaluation workspace.
113    pub(crate) skin_scratch: SkinPaletteScratch3D,
114}
115
116/// Zero-readback GPU 3D frame submission packet packing camera uniforms, Principled materials, 4-cascade CSM, IBL SH9, bone palettes, and sorted instanced batches.
117#[derive(Clone, Data, Debug)]
118pub struct GpuFramePacket3D {
119    /// Active frame view-projection matrix.
120    #[get(type(copy))]
121    pub(crate) view_projection: Mat4,
122    /// Previous frame view-projection matrix for GPU TSR velocity vectors.
123    #[get(type(copy))]
124    pub(crate) prev_view_projection: Mat4,
125    /// Active camera world position.
126    #[get(type(copy))]
127    pub(crate) camera_position: Vec3,
128    /// Active directional sun light parameters.
129    #[get(type(copy))]
130    pub(crate) sun: DirectionalLight3D,
131    /// Contiguous GPU-aligned instance buffer ordered by `(mesh_id, lod_index)`.
132    pub(crate) instances: Vec<GpuInstanceData3D>,
133    /// Deduplicated instanced draw call descriptors referencing slices of `instances`.
134    pub(crate) batches: Vec<GpuMeshDrawBatch3D>,
135    /// Packed 160-byte std140 Principled BSDF material buffer uploaded to GPU storage memory.
136    pub(crate) materials: Vec<GpuMaterialData3D>,
137    /// Four Practical-Split directional CSM light view-projection matrices.
138    #[get(type(copy))]
139    pub(crate) csm_matrices: [Mat4; VSM_CASCADE_COUNT],
140    /// Camera-space far split depth bounds for the 4 CSM cascades.
141    #[get(type(copy))]
142    pub(crate) csm_split_depths: [f32; VSM_CASCADE_COUNT],
143    /// Precomputed L2 9-band Spherical Harmonics diffuse IBL irradiance coefficients.
144    #[get(type(copy))]
145    pub(crate) ibl_sh: [[f32; 3]; 9],
146    /// Flattened contiguous bone skinning matrices across all active skinned meshes.
147    pub(crate) bone_matrices: Vec<Mat4>,
148    /// Whether Blender AgX (`true`) or ACES Filmic (`false`) tonemapping is active.
149    #[get(type(copy))]
150    pub(crate) use_agx_tonemap: bool,
151    /// Number of active render-graph passes scheduled for this frame.
152    #[get(type(copy))]
153    pub(crate) active_pass_count: usize,
154    /// Total hardware instanced triangles queued for submission.
155    #[get(type(copy))]
156    pub(crate) total_triangles: usize,
157}
158
159/// 4-wide Structure-of-Arrays AABB batch packet for SIMD frustum culling across NEON, SSE2, and WASM `simd128`.
160#[repr(C, align(16))]
161#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
162pub struct SimdAabbBatch4 {
163    /// Minimum X coordinates for four bounding boxes.
164    #[get(type(copy))]
165    pub(crate) min_x: [f32; 4],
166    /// Minimum Y coordinates for four bounding boxes.
167    #[get(type(copy))]
168    pub(crate) min_y: [f32; 4],
169    /// Minimum Z coordinates for four bounding boxes.
170    #[get(type(copy))]
171    pub(crate) min_z: [f32; 4],
172    /// Maximum X coordinates for four bounding boxes.
173    #[get(type(copy))]
174    pub(crate) max_x: [f32; 4],
175    /// Maximum Y coordinates for four bounding boxes.
176    #[get(type(copy))]
177    pub(crate) max_y: [f32; 4],
178    /// Maximum Z coordinates for four bounding boxes.
179    #[get(type(copy))]
180    pub(crate) max_z: [f32; 4],
181}
182
183/// 4-wide Structure-of-Arrays meshlet normal-cone packet for SIMD backface culling across NEON, SSE2, and WASM `simd128`.
184#[repr(C, align(16))]
185#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
186pub struct SimdMeshletConeBatch4 {
187    /// Local-space bounding-box center X coordinates for four meshlets.
188    #[get(type(copy))]
189    pub(crate) center_x: [f32; 4],
190    /// Local-space bounding-box center Y coordinates for four meshlets.
191    #[get(type(copy))]
192    pub(crate) center_y: [f32; 4],
193    /// Local-space bounding-box center Z coordinates for four meshlets.
194    #[get(type(copy))]
195    pub(crate) center_z: [f32; 4],
196    /// Normalized outward cone axis X components for four meshlets.
197    #[get(type(copy))]
198    pub(crate) axis_x: [f32; 4],
199    /// Normalized outward cone axis Y components for four meshlets.
200    #[get(type(copy))]
201    pub(crate) axis_y: [f32; 4],
202    /// Normalized outward cone axis Z components for four meshlets.
203    #[get(type(copy))]
204    pub(crate) axis_z: [f32; 4],
205    /// Squared sine of each normal-cone half-angle (`> 1.0` disables backface rejection).
206    #[get(type(copy))]
207    pub(crate) sin_alpha_sq: [f32; 4],
208    /// Conservative cluster bounding extent margin along the cone axis plus radial sine term.
209    #[get(type(copy))]
210    pub(crate) extent_margin: [f32; 4],
211}
212
213/// Single sortable PBR instance record consumed by `coalesce_pbr_batches_into` for 64-bit state sorting and automatic instancing.
214#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
215pub struct PbrSortEntry3D {
216    /// 64-bit deterministic state sort key `(alpha | pso | material | mesh | lod | depth)`.
217    #[get(type(copy))]
218    pub(crate) sort_key: u64,
219    /// Starting index offset inside `GpuPbrMeshBuffer::indices`.
220    #[get(type(copy))]
221    pub(crate) index_start: u32,
222    /// Triangle index count drawn per instance.
223    #[get(type(copy))]
224    pub(crate) index_count: u32,
225    /// Base vertex offset added to indices.
226    #[get(type(copy))]
227    pub(crate) base_vertex: u32,
228    /// Material slot index inside `GpuPbrScenePass::materials`.
229    #[get(type(copy))]
230    pub(crate) material_index: u32,
231    /// Pipeline variant (`0=OpaquePbr, 1=SkinnedPbr, 2=AlphaBlendPbr`).
232    #[get(type(copy))]
233    pub(crate) pso_variant: u8,
234    /// Selected geometric LOD tier (`0..=3`).
235    #[get(type(copy))]
236    pub(crate) lod_level: u8,
237    /// Packed GPU instance payload written into `GpuPbrScenePass::instances`.
238    #[get(type(copy))]
239    pub(crate) instance: GpuPbrInstance3D,
240}
241
242/// Reusable scratch arena enabling zero-allocation 64-bit sorting and automatic hardware instancing coalescing.
243#[derive(Clone, Data, Debug, Default)]
244pub struct GpuPbrBatcherScratch3D {
245    /// Reusable flat buffer holding sortable PBR draw entries before coalescing.
246    pub(crate) entries: Vec<PbrSortEntry3D>,
247}
248
249/// 320-byte `#[repr(C)]` constant buffer shared by Metal, D3D11, and GLES3/WebGL2 `DirectGpuScenePass::PbrScene` shaders.
250#[repr(C)]
251#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
252pub struct PbrSceneUniformBlock {
253    /// Column-major combined `projection * view` matrix.
254    #[get(type(copy))]
255    pub(crate) view_proj: [f32; 16],
256    /// Camera world position and exposure `[cx, cy, cz, exposure]`.
257    #[get(type(copy))]
258    pub(crate) camera_pos_exposure: [f32; 4],
259    /// Normalized sun direction and fog density `[sx, sy, sz, fog_density]`.
260    #[get(type(copy))]
261    pub(crate) sun_dir_fog: [f32; 4],
262    /// Linear HDR sun color and ambient strength `[sr, sg, sb, ambient]`.
263    #[get(type(copy))]
264    pub(crate) sun_color_ambient: [f32; 4],
265    /// Atmospheric horizon RGB and time-of-day `[hr, hg, hb, tod]`.
266    #[get(type(copy))]
267    pub(crate) sky_horizon_tod: [f32; 4],
268    /// Atmospheric zenith RGB and AgX tonemap toggle `[zr, zg, zb, use_agx]`.
269    #[get(type(copy))]
270    pub(crate) sky_zenith_agx: [f32; 4],
271    /// L2 9-band Spherical Harmonics diffuse IBL irradiance coefficients.
272    #[get(type(copy))]
273    pub(crate) ibl_sh: [[f32; 4]; 9],
274    /// Packed per-batch state `[debug_mode, lod_mip_texmask, local_light_count, material_index]`.
275    #[get(type(copy))]
276    pub(crate) batch_params: [u32; 4],
277    /// Packed per-draw offsets `[base_vertex, instance_start, morph_delta_stride, index_start]`.
278    #[get(type(copy))]
279    pub(crate) draw_offsets: [u32; 4],
280}