euv-core 0.46.1

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::*;

/// Shared GPU texture/render-target handle composited directly with the EUV HUD on the same device without CPU readback.
#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, PartialEq)]
pub struct GpuTextureHandle {
    /// Backend-opaque GPU texture or image view identifier (`0` indicates invalid/unbound).
    #[get(type(copy))]
    pub(crate) texture_id: u64,
    /// Owning GPU logical device identifier verifying same-device compositing compatibility.
    #[get(type(copy))]
    pub(crate) device_id: u64,
    /// Monotonic resource generation rejecting stale handles after resize or device loss.
    #[get(type(copy))]
    pub(crate) generation: u64,
    /// Texture width in physical pixels.
    #[get(type(copy))]
    pub(crate) width: u32,
    /// Texture height in physical pixels.
    #[get(type(copy))]
    pub(crate) height: u32,
    /// Pixel format of the GPU render target.
    #[get(type(copy))]
    pub(crate) format: GpuTextureFormat,
    /// Timeline semaphore or fence value signaling completion of 3D/2D GPU rendering into this texture.
    #[get(type(copy))]
    pub(crate) fence_value: u64,
    /// Whether UV `(0, 0)` corresponds to the top-left corner (`false` for bottom-left OpenGL/WebGL targets).
    #[get(type(copy))]
    pub(crate) origin_top_left: bool,
}

/// Descriptor emitted onto `GameRenderContext` binding a GPU render target into the single-present HUD compositor.
#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
pub struct GpuCompositeDescriptor {
    /// Destination bounds in root logical coordinates.
    #[get(type(copy))]
    pub(crate) rect: Rect,
    /// Shared same-device GPU texture handle rendered by the game engine.
    #[get(type(copy))]
    pub(crate) handle: GpuTextureHandle,
    /// Layer ordering index relative to overlaid EUV HUD elements.
    #[get(type(copy))]
    pub(crate) z_layer: i32,
    /// Whether premultiplied alpha blending is enabled when compositing over underlying UI.
    #[get(type(copy))]
    pub(crate) preserve_alpha: bool,
}

/// Stateful controller coordinating same-device GPU viewport compositing and independent world vs HUD dirty tracking.
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct GpuCompositeTarget {
    /// Currently attached persistent GPU texture handle.
    #[get(type(copy))]
    pub(crate) handle: GpuTextureHandle,
    /// Active viewport destination rectangle in root logical coordinates.
    #[get(type(copy))]
    pub(crate) viewport_rect: Rect,
    /// Monotonic revision counter bumped only when 2D/3D game world content changes.
    #[get(type(copy))]
    pub(crate) world_revision: u64,
    /// Last presented game world revision.
    #[get(type(copy))]
    pub(crate) presented_world_revision: u64,
    /// Monotonic revision counter bumped only when overlaid EUV HUD content changes.
    #[get(type(copy))]
    pub(crate) hud_revision: u64,
    /// Last presented HUD revision.
    #[get(type(copy))]
    pub(crate) presented_hud_revision: u64,
    /// Highest GPU fence submitted for this target.
    #[get(type(copy))]
    pub(crate) submitted_fence: u64,
    /// Highest GPU fence confirmed complete by the device queue.
    #[get(type(copy))]
    pub(crate) completed_fence: u64,
    /// Number of frames composited directly on GPU without CPU pixel readback.
    #[get(type(copy))]
    pub(crate) zero_readback_presents: u64,
    /// Number of HUD-only composite frames that reused the existing 3D/2D world target without re-rendering the world.
    #[get(type(copy))]
    pub(crate) hud_only_reuses: u64,
}

/// Single reusable RGBA8 backing slot inside a bounded `RenderBufferPool`.
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub struct PooledRenderBufferSlot {
    /// Persistent pixel buffer reused across frames without per-frame heap allocation.
    pub(crate) pixels: Vec<u8>,
    /// Current logical frame width stored in `pixels`.
    #[get(type(copy))]
    pub(crate) width: u32,
    /// Current logical frame height stored in `pixels`.
    #[get(type(copy))]
    pub(crate) height: u32,
    /// Fence value guarding reclamation while this slot is in flight with a compositor/uploader.
    #[get(type(copy))]
    pub(crate) fence_id: u64,
    /// Whether this slot is currently leased or awaiting fence completion.
    #[get(type(copy))]
    pub(crate) in_flight: bool,
    /// Whether the pixel buffer is currently checked out inside a live `PooledFrameLease`.
    #[get(type(copy))]
    pub(crate) checked_out: bool,
    /// Generation counter incremented each time this slot is acquired.
    #[get(type(copy))]
    pub(crate) generation: u64,
}

/// Observable allocation, reuse, and backpressure counters for `RenderBufferPool`.
#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
pub struct RenderBufferPoolStats {
    /// Maximum bounded slot capacity.
    #[get(type(copy))]
    pub(crate) capacity: usize,
    /// Slots currently leased or awaiting fence retirement.
    #[get(type(copy))]
    pub(crate) in_flight_slots: usize,
    /// Free slots immediately available for zero-allocation acquisition.
    #[get(type(copy))]
    pub(crate) free_slots: usize,
    /// Total acquisitions satisfied without growing backing heap capacity.
    #[get(type(copy))]
    pub(crate) reuse_hits: u64,
    /// Total backing buffer capacity growths (expected to plateau after warm-up).
    #[get(type(copy))]
    pub(crate) backing_allocations: u64,
    /// Acquisitions rejected due to all bounded slots being in flight (backpressure).
    #[get(type(copy))]
    pub(crate) backpressure_rejections: u64,
}

/// Internal mutable state shared between `RenderBufferPool` and active `PooledFrameLease` handles.
#[derive(Clone, Data, Debug, Default)]
pub(crate) struct RenderBufferPoolState {
    /// Fixed-length ring of reusable pixel slots.
    pub(crate) slots: Vec<PooledRenderBufferSlot>,
    /// Last retired compositor/GPU fence value.
    #[get(type(copy))]
    pub(crate) completed_fence: u64,
    /// Total zero-allocation reuse hits.
    #[get(type(copy))]
    pub(crate) reuse_hits: u64,
    /// Total backing buffer capacity allocations.
    #[get(type(copy))]
    pub(crate) backing_allocations: u64,
    /// Total rejections when every bounded slot was in flight.
    #[get(type(copy))]
    pub(crate) backpressure_rejections: u64,
}

/// Zero-allocation bounded CPU RGBA8 render output buffer pool with fence-gated reclamation.
#[derive(Clone, CustomDebug, Data)]
pub struct RenderBufferPool {
    /// Shared bounded pool state coordinating slot leases and fence retirement.
    #[debug(skip)]
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) state: Rc<RefCell<RenderBufferPoolState>>,
}

/// RAII lease holding exclusive mutable access to a pooled RGBA8 buffer slot, returning it on drop or fence commit.
#[derive(CustomDebug, Data)]
pub struct PooledFrameLease {
    /// Owning pool state receiving the pixel buffer back on release or drop.
    #[debug(skip)]
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) state: Rc<RefCell<RenderBufferPoolState>>,
    /// Bound slot index inside `RenderBufferPoolState::slots`.
    #[get(type(copy))]
    pub(crate) slot_index: usize,
    /// Rendered frame width in pixels.
    #[get(type(copy))]
    pub(crate) width: u32,
    /// Rendered frame height in pixels.
    #[get(type(copy))]
    pub(crate) height: u32,
    /// Assigned submission fence value.
    #[get(type(copy))]
    pub(crate) fence_id: u64,
    /// Slot acquisition generation.
    #[get(type(copy))]
    pub(crate) generation: u64,
    /// Checked-out pixel storage buffer swapped back into the pool slot upon release.
    pub(crate) pixels: Vec<u8>,
    /// Whether this lease has already been returned to the pool.
    #[get(type(copy))]
    pub(crate) released: bool,
}