euv-core 0.54.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Transient GPU/CPU render-target resource declared inside `RenderGraph3D` for automatic lifetime aliasing.
#[derive(Clone, Data, Debug, PartialEq)]
pub struct RenderGraphResource3D {
    /// Numeric transient resource identifier (`0..resource_count`).
    #[get(type(copy))]
    pub(crate) resource_id: usize,
    /// Human-readable resource label (e.g., `gbuffer_depth`, `lumen_probes`).
    pub(crate) name: String,
    /// Estimated byte footprint of this render target or storage buffer.
    #[get(type(copy))]
    pub(crate) size_bytes: usize,
    /// Whether this resource is an external final output that anchors pass liveness.
    #[get(type(copy))]
    pub(crate) is_external_output: bool,
}

/// Declarative render pass node inside `RenderGraph3D` declaring read/write resource dependencies.
#[derive(Clone, Data, Debug, PartialEq)]
pub struct RenderGraphPass3D {
    /// Numeric pass identifier (`0..pass_count`).
    #[get(type(copy))]
    pub(crate) pass_id: usize,
    /// Canonical UE5 render pass category.
    #[get(type(copy))]
    pub(crate) kind: RenderPassKind3D,
    /// Human-readable diagnostic pass name.
    pub(crate) name: String,
    /// Resource IDs consumed as read-only inputs by this pass.
    pub(crate) reads: Vec<usize>,
    /// Resource IDs written or produced by this pass.
    pub(crate) writes: Vec<usize>,
    /// Last measured execution time of this pass in microseconds.
    #[get(type(copy))]
    pub(crate) last_duration_us: u64,
}

/// Compiled execution schedule and transient memory aliasing report produced by `RenderGraph3D::compile`.
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub struct RenderGraphSchedule3D {
    /// Topologically ordered active pass IDs surviving dead-pass culling.
    pub(crate) ordered_passes: Vec<usize>,
    /// Total pass count eliminated by dead-pass culling.
    #[get(type(copy))]
    pub(crate) culled_pass_count: usize,
    /// Unaliased sum of all active transient resource byte sizes.
    #[get(type(copy))]
    pub(crate) unaliased_bytes: usize,
    /// Peak aliased transient memory footprint after greedy lifetime packing.
    #[get(type(copy))]
    pub(crate) aliased_peak_bytes: usize,
    /// Physical memory pool slot assigned to each transient resource ID (`usize::MAX` if unused).
    pub(crate) resource_pool_slots: Vec<usize>,
}

/// UE5 Render Dependency Graph (RDG) managing declarative pass scheduling, automatic dead-pass culling, and transient buffer aliasing.
#[derive(Clone, Data, Debug, Default)]
pub struct RenderGraph3D {
    /// Declared transient and external frame resources.
    pub(crate) resources: Vec<RenderGraphResource3D>,
    /// Declared frame render passes.
    pub(crate) passes: Vec<RenderGraphPass3D>,
}

/// 112-byte GPU-aligned instanced 3D object payload matching `WGSL`/`MSL`/`GLSL`/`HLSL` `InstanceData` layouts.
#[repr(C, align(16))]
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct GpuInstanceData3D {
    /// Column-major 4x4 model matrix elements.
    #[get(type(copy))]
    pub(crate) model_matrix: [f32; 16],
    /// Linear base albedo RGB and metallic factor in `.w`.
    #[get(type(copy))]
    pub(crate) albedo_metallic: [f32; 4],
    /// Linear HDR emissive RGB and roughness factor in `.w`.
    #[get(type(copy))]
    pub(crate) emissive_roughness: [f32; 4],
    /// Material slot, bone palette offset, morph weight offset, and instance flags `[mat_id, skin_off, morph_off, flags]`.
    #[get(type(copy))]
    pub(crate) material_skin_morph_flags: [u32; 4],
}

/// Deduplicated GPU instanced mesh draw batch emitted from `World3D` for hardware RHI submission.
#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
pub struct GpuMeshDrawBatch3D {
    /// Registered mesh identifier in `World3D::mesh_library`.
    #[get(type(copy))]
    pub(crate) mesh_id: usize,
    /// Selected LOD tier index (`0..=2`).
    #[get(type(copy))]
    pub(crate) lod_index: usize,
    /// First instance index inside `GpuFramePacket3D::instances`.
    #[get(type(copy))]
    pub(crate) first_instance: u32,
    /// Number of consecutive instanced objects sharing this mesh/LOD batch.
    #[get(type(copy))]
    pub(crate) instance_count: u32,
    /// Triangle count per single instance of this mesh LOD.
    #[get(type(copy))]
    pub(crate) triangles_per_instance: u32,
}

/// Reusable sorting scratch arena enabling zero-allocation `GpuFramePacket3D::build_from_world_into` updates.
#[derive(Clone, Data, Debug, Default)]
pub struct GpuFramePacketScratch3D {
    /// Reusable instance sort key table `(mesh_id, lod_idx, tri_count, instance_data)`.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) keyed_instances: Vec<(usize, usize, u32, GpuInstanceData3D)>,
    /// Reusable skin matrix evaluation workspace.
    pub(crate) skin_scratch: SkinPaletteScratch3D,
}

/// Zero-readback GPU 3D frame submission packet packing camera uniforms, Principled materials, 4-cascade CSM, IBL SH9, bone palettes, and sorted instanced batches.
#[derive(Clone, Data, Debug)]
pub struct GpuFramePacket3D {
    /// Active frame view-projection matrix.
    #[get(type(copy))]
    pub(crate) view_projection: Mat4,
    /// Previous frame view-projection matrix for GPU TSR velocity vectors.
    #[get(type(copy))]
    pub(crate) prev_view_projection: Mat4,
    /// Active camera world position.
    #[get(type(copy))]
    pub(crate) camera_position: Vec3,
    /// Active directional sun light parameters.
    #[get(type(copy))]
    pub(crate) sun: DirectionalLight3D,
    /// Contiguous GPU-aligned instance buffer ordered by `(mesh_id, lod_index)`.
    pub(crate) instances: Vec<GpuInstanceData3D>,
    /// Deduplicated instanced draw call descriptors referencing slices of `instances`.
    pub(crate) batches: Vec<GpuMeshDrawBatch3D>,
    /// Packed 160-byte std140 Principled BSDF material buffer uploaded to GPU storage memory.
    pub(crate) materials: Vec<GpuMaterialData3D>,
    /// Four Practical-Split directional CSM light view-projection matrices.
    #[get(type(copy))]
    pub(crate) csm_matrices: [Mat4; VSM_CASCADE_COUNT],
    /// Camera-space far split depth bounds for the 4 CSM cascades.
    #[get(type(copy))]
    pub(crate) csm_split_depths: [f32; VSM_CASCADE_COUNT],
    /// Precomputed L2 9-band Spherical Harmonics diffuse IBL irradiance coefficients.
    #[get(type(copy))]
    pub(crate) ibl_sh: [[f32; 3]; 9],
    /// Flattened contiguous bone skinning matrices across all active skinned meshes.
    pub(crate) bone_matrices: Vec<Mat4>,
    /// Whether Blender AgX (`true`) or ACES Filmic (`false`) tonemapping is active.
    #[get(type(copy))]
    pub(crate) use_agx_tonemap: bool,
    /// Number of active render-graph passes scheduled for this frame.
    #[get(type(copy))]
    pub(crate) active_pass_count: usize,
    /// Total hardware instanced triangles queued for submission.
    #[get(type(copy))]
    pub(crate) total_triangles: usize,
}

/// 4-wide Structure-of-Arrays AABB batch packet for SIMD frustum culling across NEON, SSE2, and WASM `simd128`.
#[repr(C, align(16))]
#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
pub struct SimdAabbBatch4 {
    /// Minimum X coordinates for four bounding boxes.
    #[get(type(copy))]
    pub(crate) min_x: [f32; 4],
    /// Minimum Y coordinates for four bounding boxes.
    #[get(type(copy))]
    pub(crate) min_y: [f32; 4],
    /// Minimum Z coordinates for four bounding boxes.
    #[get(type(copy))]
    pub(crate) min_z: [f32; 4],
    /// Maximum X coordinates for four bounding boxes.
    #[get(type(copy))]
    pub(crate) max_x: [f32; 4],
    /// Maximum Y coordinates for four bounding boxes.
    #[get(type(copy))]
    pub(crate) max_y: [f32; 4],
    /// Maximum Z coordinates for four bounding boxes.
    #[get(type(copy))]
    pub(crate) max_z: [f32; 4],
}

/// 4-wide Structure-of-Arrays meshlet normal-cone packet for SIMD backface culling across NEON, SSE2, and WASM `simd128`.
#[repr(C, align(16))]
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct SimdMeshletConeBatch4 {
    /// Local-space bounding-box center X coordinates for four meshlets.
    #[get(type(copy))]
    pub(crate) center_x: [f32; 4],
    /// Local-space bounding-box center Y coordinates for four meshlets.
    #[get(type(copy))]
    pub(crate) center_y: [f32; 4],
    /// Local-space bounding-box center Z coordinates for four meshlets.
    #[get(type(copy))]
    pub(crate) center_z: [f32; 4],
    /// Normalized outward cone axis X components for four meshlets.
    #[get(type(copy))]
    pub(crate) axis_x: [f32; 4],
    /// Normalized outward cone axis Y components for four meshlets.
    #[get(type(copy))]
    pub(crate) axis_y: [f32; 4],
    /// Normalized outward cone axis Z components for four meshlets.
    #[get(type(copy))]
    pub(crate) axis_z: [f32; 4],
    /// Squared sine of each normal-cone half-angle (`> 1.0` disables backface rejection).
    #[get(type(copy))]
    pub(crate) sin_alpha_sq: [f32; 4],
    /// Conservative cluster bounding extent margin along the cone axis plus radial sine term.
    #[get(type(copy))]
    pub(crate) extent_margin: [f32; 4],
}

/// Single sortable PBR instance record consumed by `coalesce_pbr_batches_into` for 64-bit state sorting and automatic instancing.
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct PbrSortEntry3D {
    /// 64-bit deterministic state sort key `(alpha | pso | material | mesh | lod | depth)`.
    #[get(type(copy))]
    pub(crate) sort_key: u64,
    /// Starting index offset inside `GpuPbrMeshBuffer::indices`.
    #[get(type(copy))]
    pub(crate) index_start: u32,
    /// Triangle index count drawn per instance.
    #[get(type(copy))]
    pub(crate) index_count: u32,
    /// Base vertex offset added to indices.
    #[get(type(copy))]
    pub(crate) base_vertex: u32,
    /// Material slot index inside `GpuPbrScenePass::materials`.
    #[get(type(copy))]
    pub(crate) material_index: u32,
    /// Pipeline variant (`0=OpaquePbr, 1=SkinnedPbr, 2=AlphaBlendPbr`).
    #[get(type(copy))]
    pub(crate) pso_variant: u8,
    /// Selected geometric LOD tier (`0..=3`).
    #[get(type(copy))]
    pub(crate) lod_level: u8,
    /// Packed GPU instance payload written into `GpuPbrScenePass::instances`.
    #[get(type(copy))]
    pub(crate) instance: GpuPbrInstance3D,
}

/// Reusable scratch arena enabling zero-allocation 64-bit sorting and automatic hardware instancing coalescing.
#[derive(Clone, Data, Debug, Default)]
pub struct GpuPbrBatcherScratch3D {
    /// Reusable flat buffer holding sortable PBR draw entries before coalescing.
    pub(crate) entries: Vec<PbrSortEntry3D>,
}

/// 320-byte `#[repr(C)]` constant buffer shared by Metal, D3D11, and GLES3/WebGL2 `DirectGpuScenePass::PbrScene` shaders.
#[repr(C)]
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct PbrSceneUniformBlock {
    /// Column-major combined `projection * view` matrix.
    #[get(type(copy))]
    pub(crate) view_proj: [f32; 16],
    /// Camera world position and exposure `[cx, cy, cz, exposure]`.
    #[get(type(copy))]
    pub(crate) camera_pos_exposure: [f32; 4],
    /// Normalized sun direction and fog density `[sx, sy, sz, fog_density]`.
    #[get(type(copy))]
    pub(crate) sun_dir_fog: [f32; 4],
    /// Linear HDR sun color and ambient strength `[sr, sg, sb, ambient]`.
    #[get(type(copy))]
    pub(crate) sun_color_ambient: [f32; 4],
    /// Atmospheric horizon RGB and time-of-day `[hr, hg, hb, tod]`.
    #[get(type(copy))]
    pub(crate) sky_horizon_tod: [f32; 4],
    /// Atmospheric zenith RGB and AgX tonemap toggle `[zr, zg, zb, use_agx]`.
    #[get(type(copy))]
    pub(crate) sky_zenith_agx: [f32; 4],
    /// L2 9-band Spherical Harmonics diffuse IBL irradiance coefficients.
    #[get(type(copy))]
    pub(crate) ibl_sh: [[f32; 4]; 9],
    /// Packed per-batch state `[debug_mode, lod_mip_texmask, local_light_count, material_index]`.
    #[get(type(copy))]
    pub(crate) batch_params: [u32; 4],
    /// Packed per-draw offsets `[base_vertex, instance_start, morph_delta_stride, index_start]`.
    #[get(type(copy))]
    pub(crate) draw_offsets: [u32; 4],
}