Skip to main content

euv_core/platform/backend/
struct.rs

1use super::*;
2
3/// One lit 3D origami paper boat triangle vertex in NDC space for direct GPU ocean compositing (`32` bytes).
4#[repr(C)]
5#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
6pub struct GpuOceanBoatVertex {
7    /// Clip/NDC vertex position `[ndc_x, ndc_y, depth, 1.0]`.
8    #[get(type(copy))]
9    pub(crate) pos_ndc: [f32; 4],
10    /// Lit sRGB RGBA facet color `[r, g, b, a]`.
11    #[get(type(copy))]
12    pub(crate) color: [f32; 4],
13}
14
15/// Self-contained hardware GPU ocean and origami boat scene pass descriptor (`1:1` physical resolution, zero CPU pixel rasterization).
16#[derive(Clone, Data, Debug, Default, PartialEq)]
17pub struct GpuOceanScenePass {
18    /// Target physical surface width in device pixels.
19    #[get(type(copy))]
20    pub(crate) target_width: u32,
21    /// Target physical surface height in device pixels.
22    #[get(type(copy))]
23    pub(crate) target_height: u32,
24    /// Monotonic simulation time in seconds driving Gerstner wave phases.
25    #[get(type(copy))]
26    pub(crate) sim_time: f32,
27    /// Wave agitation multiplier derived from live PBF kinetic energy.
28    #[get(type(copy))]
29    pub(crate) agitation: f32,
30    /// Normalized vertical horizon line coordinate in `[0.46, 0.62]`.
31    #[get(type(copy))]
32    pub(crate) horizon_v: f32,
33    /// Cosine of vessel roll tilt angle.
34    #[get(type(copy))]
35    pub(crate) roll_cos: f32,
36    /// Sine of vessel roll tilt angle.
37    #[get(type(copy))]
38    pub(crate) roll_sin: f32,
39    /// Exact unclamped physical aspect ratio `target_width / target_height`.
40    #[get(type(copy))]
41    pub(crate) aspect: f32,
42    /// Normalized 3D sun direction `[x, y, z]`.
43    #[get(type(copy))]
44    pub(crate) sun_dir: [f32; 3],
45    /// Normalized boat waterline UV `[u, v]` for waterline contact shadow and reflection.
46    #[get(type(copy))]
47    pub(crate) boat_uv: [f32; 2],
48    /// PBF wake texture width in texels (`96`).
49    #[get(type(copy))]
50    pub(crate) wake_width: u32,
51    /// PBF wake texture height in texels (`72`).
52    #[get(type(copy))]
53    pub(crate) wake_height: u32,
54    /// True when interactive pointer/tilt PBF wake energy is non-zero.
55    #[get(type(copy))]
56    pub(crate) has_active_wake: bool,
57    /// Packed RGBA8 PBF wake field (`R = flow_u`, `G = flow_v`, `B = foam`, `A = 255`).
58    pub(crate) wake_rgba8: Vec<u8>,
59    /// Depth-sorted back-to-front 3D origami paper boat triangle vertices (`18 * 3`).
60    pub(crate) boat_vertices: Vec<GpuOceanBoatVertex>,
61}
62
63/// Compact 48-byte `#[repr(C)]` vertex record for hardware Metal and WebGL2 3D PBR open-world rendering.
64#[repr(C)]
65#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
66pub struct GpuWorld3dVertex {
67    /// Local-space 3D vertex position `[x, y, z, 1.0]`.
68    #[get(type(copy))]
69    pub(crate) position: [f32; 4],
70    /// Local-space unit surface normal `[nx, ny, nz, 0.0]`.
71    #[get(type(copy))]
72    pub(crate) normal: [f32; 4],
73    /// Sub-material linear RGB override (`w = 1.0` overrides instance albedo, `w = 0.0` multiplies instance albedo).
74    #[get(type(copy))]
75    pub(crate) color_submat: [f32; 4],
76}
77
78/// Compact 96-byte `#[repr(C)]` instanced object payload for hardware Metal and WebGL2 3D PBR rendering.
79#[repr(C)]
80#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
81pub struct GpuWorld3dInstance {
82    /// Column-major `4x4` object-to-world model matrix.
83    #[get(type(copy))]
84    pub(crate) model: [f32; 16],
85    /// Primary linear albedo RGB and metallic factor `[r, g, b, metallic]`.
86    #[get(type(copy))]
87    pub(crate) albedo_metallic: [f32; 4],
88    /// HDR emissive glow RGB and surface roughness `[er, eg, eb, roughness]`.
89    #[get(type(copy))]
90    pub(crate) emissive_roughness: [f32; 4],
91}
92
93/// Per-mesh instanced draw batch range indexing into `GpuWorld3dMeshBuffer::vertices` and `GpuWorld3dScenePass::instances`.
94#[repr(C)]
95#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
96pub struct GpuWorld3dMeshRange {
97    /// Starting vertex index inside the resident static mesh vertex buffer.
98    #[get(type(copy))]
99    pub(crate) vertex_start: u32,
100    /// Number of non-indexed triangle vertices for this prototype mesh.
101    #[get(type(copy))]
102    pub(crate) vertex_count: u32,
103    /// Starting instance index inside the per-frame instance buffer.
104    #[get(type(copy))]
105    pub(crate) instance_start: u32,
106    /// Number of visible instances drawn in this instanced call.
107    #[get(type(copy))]
108    pub(crate) instance_count: u32,
109}
110
111/// Immutable `Arc`-shared static 3D mesh library vertex buffer uploaded once per revision to GPU VRAM.
112#[derive(Clone, Data, Debug, Default, PartialEq)]
113pub struct GpuWorld3dMeshBuffer {
114    /// Monotonic revision incremented only when meshes are built or imported.
115    #[get(type(copy))]
116    pub(crate) revision: u64,
117    /// Packed non-indexed triangle vertices across all prototype meshes in the scene library.
118    pub(crate) vertices: Vec<GpuWorld3dVertex>,
119}
120
121/// Self-contained hardware GPU 3D PBR open-world scene pass descriptor (`1:1` physical resolution, hardware Z-buffer).
122#[derive(Clone, Data, Debug, Default, PartialEq)]
123pub struct GpuWorld3dScenePass {
124    /// Target physical surface width in device pixels.
125    #[get(type(copy))]
126    pub(crate) target_width: u32,
127    /// Target physical surface height in device pixels.
128    #[get(type(copy))]
129    pub(crate) target_height: u32,
130    /// Column-major combined `projection * view` matrix.
131    #[get(type(copy))]
132    pub(crate) view_proj: [f32; 16],
133    /// Camera world position and exposure `[cx, cy, cz, exposure]`.
134    #[get(type(copy))]
135    pub(crate) camera_pos: [f32; 4],
136    /// Normalized sun direction and fog density `[sx, sy, sz, fog_density]`.
137    #[get(type(copy))]
138    pub(crate) sun_dir_fog: [f32; 4],
139    /// Linear HDR sun color and ambient strength `[sr, sg, sb, ambient]`.
140    #[get(type(copy))]
141    pub(crate) sun_color_ambient: [f32; 4],
142    /// Atmospheric horizon RGB and time-of-day `[hr, hg, hb, tod]`.
143    #[get(type(copy))]
144    pub(crate) sky_horizon_tod: [f32; 4],
145    /// Atmospheric zenith RGB and aspect ratio `[zr, zg, zb, aspect]`.
146    #[get(type(copy))]
147    pub(crate) sky_zenith_aspect: [f32; 4],
148    /// Immutable `Arc`-shared static mesh vertex buffer uploaded once per revision.
149    pub(crate) mesh_buffer: Arc<GpuWorld3dMeshBuffer>,
150    /// Frustum-culled visible per-object instance payloads for this frame.
151    pub(crate) instances: Vec<GpuWorld3dInstance>,
152    /// Ordered per-mesh instanced draw ranges referencing `mesh_buffer` and `instances`.
153    pub(crate) draw_batches: Vec<GpuWorld3dMeshRange>,
154}
155
156/// Per-frame culling, LOD distribution, upload budget, and draw-call telemetry for `GpuPbrScenePass`.
157#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
158pub struct GpuPbrCullStats {
159    /// Total meshlet clusters tested against frustum and normal-cone bounds.
160    #[get(type(copy))]
161    pub(crate) meshlets_tested: u32,
162    /// Number of meshlet clusters rejected by frustum or normal-cone backface culling.
163    #[get(type(copy))]
164    pub(crate) meshlets_culled: u32,
165    /// Visible instance counts distributed across `[LOD0, LOD1, LOD2, LOD3]`.
166    #[get(type(copy))]
167    pub(crate) lod_instance_counts: [u32; 4],
168    /// Total instanced GPU draw calls emitted for this frame.
169    #[get(type(copy))]
170    pub(crate) draw_call_count: u32,
171    /// Total bytes uploaded to GPU buffers and textures during this frame.
172    #[get(type(copy))]
173    pub(crate) uploaded_bytes: usize,
174    /// Total CPU microseconds spent staging GPU uploads during this frame.
175    #[get(type(copy))]
176    pub(crate) upload_time_us: u64,
177}
178
179/// Packed 64-byte `#[repr(C)]` GPU local light record supporting point, spot, and area lights in `GpuPbrScenePass`.
180#[repr(C)]
181#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
182pub struct GpuPackedLocalLight {
183    /// World-space position `[x, y, z]` and attenuation range `w`.
184    #[get(type(copy))]
185    pub(crate) position_range: [f32; 4],
186    /// Linear HDR RGB radiance and luminous intensity `w`.
187    #[get(type(copy))]
188    pub(crate) color_intensity: [f32; 4],
189    /// Normalized light direction `[dx, dy, dz]` and packed light type (`0=Point, 1=Spot, 2=Area`).
190    #[get(type(copy))]
191    pub(crate) direction_kind: [f32; 4],
192    /// Spot or area shape parameters `[inner_cos, outer_cos, width, height]`.
193    #[get(type(copy))]
194    pub(crate) cone_size_params: [f32; 4],
195}
196
197/// Single frame-budgeted incremental GPU texture mipmap level upload payload.
198#[derive(Clone, Data, Debug, Default, Eq, PartialEq)]
199pub struct GpuTextureMipUpload {
200    /// Target bindless texture pool slot index.
201    #[get(type(copy))]
202    pub(crate) texture_slot: u32,
203    /// Target mipmap level (`0` = full resolution, larger = coarser mip-tail).
204    #[get(type(copy))]
205    pub(crate) mip_level: u8,
206    /// Total mipmap levels allocated for this texture resource.
207    #[get(type(copy))]
208    pub(crate) total_mip_levels: u8,
209    /// True when texels are sRGB-encoded (albedo/emissive), false for linear data (normal/MR/AO).
210    #[get(type(copy))]
211    pub(crate) is_srgb: bool,
212    /// Minimum resident mip level clamp (`minLodClamp` / `TEXTURE_BASE_LEVEL`) after this upload.
213    #[get(type(copy))]
214    pub(crate) min_lod_clamp: u8,
215    /// Uploaded mip level width in texels.
216    #[get(type(copy))]
217    pub(crate) width: u32,
218    /// Uploaded mip level height in texels.
219    #[get(type(copy))]
220    pub(crate) height: u32,
221    /// Packed RGBA8 texel bytes for this mip level (`width * height * 4`).
222    pub(crate) rgba8: Vec<u8>,
223}
224
225/// Immutable `Arc`-shared quantized 24-byte vertex, `u32` index, 16-byte skin sidecar, and morph delta geometry buffer for `GpuPbrScenePass`.
226#[derive(Clone, Data, Debug, Default, PartialEq)]
227pub struct GpuPbrMeshBuffer {
228    /// Monotonic revision incremented only when mesh library geometry changes.
229    #[get(type(copy))]
230    pub(crate) revision: u64,
231    /// Packed 24-byte quantized vertices shared across all baked LOD levels.
232    pub(crate) vertices: Vec<QuantizedVertex3D>,
233    /// Contiguous triangle vertex indices referencing `vertices`.
234    pub(crate) indices: Vec<u32>,
235    /// Optional 16-byte skeletal skinning sidecars aligned with skinned vertex ranges.
236    pub(crate) skin_vertices: Vec<QuantizedSkinVertex3D>,
237    /// Optional flattened `[dx, dy, dz, dn]` morph target vertex deltas.
238    pub(crate) morph_deltas: Vec<[f32; 4]>,
239    /// Optional secondary half-precision UV (`TEXCOORD_1`) sidecar aligned with `vertices`.
240    pub(crate) uv1_f16: Vec<[u16; 2]>,
241    /// Per-mesh quantization de-normalization minimum bounds `[x, y, z, 0.0]`.
242    pub(crate) mesh_bounds_min: Vec<[f32; 4]>,
243    /// Per-mesh quantization de-normalization extent `[sx, sy, sz, 0.0]`.
244    pub(crate) mesh_bounds_extent: Vec<[f32; 4]>,
245}
246
247/// Compact `#[repr(C)]` material-sorted instanced object payload for `GpuPbrScenePass`.
248#[repr(C)]
249#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
250pub struct GpuPbrInstance3D {
251    /// Column-major 4x4 object-to-world model matrix.
252    #[get(type(copy))]
253    pub(crate) model: [f32; 16],
254    /// Local-space quantization AABB minimum `[min_x, min_y, min_z, object_id_bits]`.
255    #[get(type(copy))]
256    pub(crate) quant_origin_id: [f32; 4],
257    /// Local-space quantization AABB extent `[ext_x, ext_y, ext_z, lod_bias]`.
258    #[get(type(copy))]
259    pub(crate) quant_extent_lod: [f32; 4],
260    /// Material slot, bone matrix base offset, morph weight base offset, and instance flags `[mat, bone_off, morph_off, flags]`.
261    #[get(type(copy))]
262    pub(crate) material_skin_morph_flags: [u32; 4],
263}
264
265/// Indexed instanced draw range sorted by 64-bit `(alpha|pso|material|mesh|lod)` key for `GpuPbrScenePass`.
266#[repr(C)]
267#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, PartialEq)]
268pub struct GpuPbrDrawBatch {
269    /// Starting index offset inside `GpuPbrMeshBuffer::indices`.
270    #[get(type(copy))]
271    pub(crate) index_start: u32,
272    /// Number of triangle indices drawn per instance.
273    #[get(type(copy))]
274    pub(crate) index_count: u32,
275    /// Base vertex offset added to indices inside `GpuPbrMeshBuffer::vertices`.
276    #[get(type(copy))]
277    pub(crate) base_vertex: u32,
278    /// Starting instance index inside `GpuPbrScenePass::instances`.
279    #[get(type(copy))]
280    pub(crate) instance_start: u32,
281    /// Number of consecutive instances drawn in this batch.
282    #[get(type(copy))]
283    pub(crate) instance_count: u32,
284    /// Material table index into `GpuPbrScenePass::materials`.
285    #[get(type(copy))]
286    pub(crate) material_index: u32,
287    /// Pipeline state variant (`0=OpaquePbr, 1=SkinnedPbr, 2=AlphaBlendPbr`).
288    #[get(type(copy))]
289    pub(crate) pso_variant: u8,
290    /// Selected geometric LOD tier (`0..=3`).
291    #[get(type(copy))]
292    pub(crate) lod_level: u8,
293    /// Reserved alignment padding bytes.
294    #[get(type(copy))]
295    pub(crate) reserved: [u8; 2],
296    /// 64-bit deterministic state sort key `(alpha | pso | material | mesh | lod | depth)`.
297    #[get(type(copy))]
298    pub(crate) sort_key: u64,
299}
300
301/// Hardware Z-buffered textured Principled BSDF PBR, GPU-skinned, morph-targeted, and meshlet-culled 3D scene pass descriptor.
302#[derive(Clone, Data, Debug, Default, PartialEq)]
303pub struct GpuPbrScenePass {
304    /// Target physical surface width in device pixels.
305    #[get(type(copy))]
306    pub(crate) target_width: u32,
307    /// Target physical surface height in device pixels.
308    #[get(type(copy))]
309    pub(crate) target_height: u32,
310    /// Column-major combined `projection * view` matrix.
311    #[get(type(copy))]
312    pub(crate) view_proj: [f32; 16],
313    /// Previous frame column-major `projection * view` matrix for temporal velocity vectors.
314    #[get(type(copy))]
315    pub(crate) prev_view_proj: [f32; 16],
316    /// Camera world position and exposure `[cx, cy, cz, exposure]`.
317    #[get(type(copy))]
318    pub(crate) camera_pos_exposure: [f32; 4],
319    /// Normalized sun direction and fog density `[sx, sy, sz, fog_density]`.
320    #[get(type(copy))]
321    pub(crate) sun_dir_fog: [f32; 4],
322    /// Linear HDR sun color and ambient strength `[sr, sg, sb, ambient]`.
323    #[get(type(copy))]
324    pub(crate) sun_color_ambient: [f32; 4],
325    /// Atmospheric horizon RGB and time-of-day `[hr, hg, hb, tod]`.
326    #[get(type(copy))]
327    pub(crate) sky_horizon_tod: [f32; 4],
328    /// Atmospheric zenith RGB and AgX tonemap toggle `[zr, zg, zb, use_agx]`.
329    #[get(type(copy))]
330    pub(crate) sky_zenith_agx: [f32; 4],
331    /// L2 9-band Spherical Harmonics diffuse IBL irradiance coefficients `[r, g, b, pad]`.
332    #[get(type(copy))]
333    pub(crate) ibl_sh: [[f32; 4]; 9],
334    /// Immutable `Arc`-shared quantized 24-byte mesh geometry, skin sidecar, and morph delta buffer.
335    pub(crate) mesh_buffer: Arc<GpuPbrMeshBuffer>,
336    /// Packed 160-byte std140 Principled BSDF materials uploaded to GPU storage memory.
337    pub(crate) materials: Vec<GpuMaterialData3D>,
338    /// Material/mesh/LOD-sorted instanced object payloads for this frame.
339    pub(crate) instances: Vec<GpuPbrInstance3D>,
340    /// Indexed instanced draw ranges sorted by PSO variant, material, mesh, and LOD.
341    pub(crate) draw_batches: Vec<GpuPbrDrawBatch>,
342    /// Flattened contiguous GPU skinning bone matrices (LBS/DQS).
343    pub(crate) bone_matrices: Vec<[f32; 16]>,
344    /// Flattened per-instance morph target blend weights.
345    pub(crate) morph_weights: Vec<f32>,
346    /// Up to 32 active local point, spot, and area lights.
347    pub(crate) local_lights: Vec<GpuPackedLocalLight>,
348    /// Budget-capped (`<= 4 MiB`, `<= 1.5 ms`) incremental texture mipmap uploads for this frame.
349    pub(crate) texture_uploads: Vec<GpuTextureMipUpload>,
350    /// Active debug shading visualization mode (`0=LitPBR, 1=Meshlets, 2=LodHeatmap, 3=MipResidency`).
351    #[get(type(copy))]
352    pub(crate) debug_view_mode: u8,
353    /// Per-frame meshlet culling, LOD distribution, upload budget, and draw-call statistics.
354    #[get(type(copy))]
355    pub(crate) stats: GpuPbrCullStats,
356}
357
358/// Self-contained direct-GPU frame submission passed from `AppRunner` to `PlatformBackend::present_gpu_frame`.
359#[derive(Clone, Data, Debug, Default, PartialEq)]
360pub struct GpuFrameSubmission {
361    /// Authoritative physical surface width in pixels.
362    #[get(type(copy))]
363    pub(crate) width: u32,
364    /// Authoritative physical surface height in pixels.
365    #[get(type(copy))]
366    pub(crate) height: u32,
367    /// Active display scale factor (`[0.25, 16.0]`).
368    #[get(type(copy))]
369    pub(crate) scale_factor: f32,
370    /// Surface background clear color.
371    #[get(type(copy))]
372    pub(crate) clear_color: Color,
373    /// Order-preserving 64-byte `GpuPrimitiveInstance` draw batches.
374    pub(crate) batches: Vec<GpuBatch>,
375    /// Active Dual-Kawase backdrop blur regions for Pass 3.
376    pub(crate) blur_passes: Vec<GpuBackdropBlurPass>,
377    /// Optional external 3D/Fluid GPU texture handle (`0` when absent).
378    #[get(type(copy))]
379    pub(crate) external_texture_handle: usize,
380    /// Optional hardware GPU offscreen scene pass rendered into `external_tex` before UI compositing.
381    #[get(skip)]
382    #[get_mut(skip)]
383    #[set(skip)]
384    pub(crate) gpu_scene_pass: Option<DirectGpuScenePass>,
385    /// Optional incremental R8 glyph atlas upload payload for direct GPU text rendering.
386    #[get(skip)]
387    #[get_mut(skip)]
388    #[set(skip)]
389    pub(crate) glyph_atlas_r8: Option<DirectGpuAtlasUpload>,
390    /// Dirty sub-rectangles inside `glyph_atlas_r8` pending incremental GPU upload.
391    pub(crate) glyph_dirty_rects: Vec<PixelDirtyRect>,
392    /// Optional packed RGBA8 image atlas payload for direct GPU image quad rendering.
393    #[get(skip)]
394    #[get_mut(skip)]
395    #[set(skip)]
396    pub(crate) image_atlas_rgba8: Option<DirectGpuAtlasUpload>,
397    /// Dirty sub-rectangles inside `image_atlas_rgba8` pending incremental GPU upload.
398    pub(crate) image_dirty_rects: Vec<PixelDirtyRect>,
399}
400
401/// Hardware presentation telemetry and fault status returned by `PlatformBackend::present_gpu_frame`.
402#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
403pub struct GpuPresentReceipt {
404    /// True when the platform backend executed native direct GPU presentation without CPU RGBA mirroring.
405    #[get(type(copy))]
406    pub(crate) presented: bool,
407    /// Number of hardware GPU draw calls submitted during presentation.
408    #[get(type(copy))]
409    pub(crate) draw_calls: usize,
410    /// Number of 64-byte primitive instances uploaded to GPU memory.
411    #[get(type(copy))]
412    pub(crate) uploaded_instances: usize,
413    /// Hardware presentation timestamp in nanoseconds (`0` when unobservable).
414    #[get(type(copy))]
415    pub(crate) presented_time_ns: u64,
416    /// Measured GPU execution duration in nanoseconds (`0` when unobservable).
417    #[get(type(copy))]
418    pub(crate) gpu_exec_ns: u64,
419    /// True when the GPU device or presentation surface was lost and requires automatic recovery.
420    #[get(type(copy))]
421    pub(crate) device_lost: bool,
422}