euv-core 0.47.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::*;

/// Unified 64-byte `#[repr(C)]` instanced GPU draw primitive consumed identically across WGSL, MSL, GLSL ES 3.0, and HLSL uber-shaders.
#[repr(C)]
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct GpuPrimitiveInstance {
    /// Destination physical rectangle `[x, y, width, height]` (layer-local or post-scroll).
    #[get(type(copy))]
    pub(crate) rect: [f32; 4],
    /// Exact scissor/clip bounds `[x0, y0, x1, y1]` in physical pixels.
    #[get(type(copy))]
    pub(crate) clip_rect: [f32; 4],
    /// Packed premultiplied RGBA8 primary color (or linear gradient stop 0).
    #[get(type(copy))]
    pub(crate) color_rgba: u32,
    /// Packed premultiplied RGBA8 secondary color (border color, gradient stop 1, shadow, or packed vertex anchor).
    #[get(type(copy))]
    pub(crate) aux_color_rgba: u32,
    /// Normalized atlas/texture UV bounds `[u0, v0, u1, v1]` in `[0..=65535]` (or packed line/triangle vertex coordinates).
    #[get(type(copy))]
    pub(crate) uv_rect: [u16; 4],
    /// Packed Q12.4 fixed-point corner radii `[r_tl, r_tr, r_br, r_bl]` (`1/16` px units) or shape parameters.
    #[get(type(copy))]
    pub(crate) radii_border: [u16; 4],
    /// Bitfield: `bits[0..4] = PrimKind`, `bits[4..16] = clip_mask_id / sub_kind`, `bits[16..24] = tex_slot`, `bits[24..32] = layer_id`.
    #[get(type(copy))]
    pub(crate) kind_flags: u32,
    /// Packed IEEE-754 `f16x2` `(stroke_or_blur, angle_or_pad)` or effect-table index.
    #[get(type(copy))]
    pub(crate) extra_param: u32,
}

/// Per-layer 16-byte uniform buffer uploading physical viewport dimensions and GPU scroll offset.
#[repr(C)]
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct GpuLayerUniform {
    /// Physical viewport `[width, height]` in pixels.
    #[get(type(copy))]
    pub(crate) viewport_size: [f32; 2],
    /// Retained scroll-layer translation `[scroll_x, scroll_y]` in physical pixels.
    #[get(type(copy))]
    pub(crate) scroll_offset: [f32; 2],
}

/// Effect-dependency Dual-Kawase / separable Gaussian `BackdropBlur` pass descriptor executed on GPU.
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct GpuBackdropBlurPass {
    /// Destination blur card bounds in physical pixels.
    #[get(type(copy))]
    pub(crate) bounds: Rect,
    /// Inflated input sampling halo (`bounds.inflate(blur_radius)`) tracking exact effect dependencies.
    #[get(type(copy))]
    pub(crate) input_bounds: Rect,
    /// Active physical scissor clip rectangle.
    #[get(type(copy))]
    pub(crate) clip_rect: Rect,
    /// Per-corner physical clip radii `[tl, tr, br, bl]`.
    #[get(type(copy))]
    pub(crate) radii: [f32; 4],
    /// Full configured blur radius in physical pixels (never degraded).
    #[get(type(copy))]
    pub(crate) blur_radius: f32,
    /// Owning retained layer identifier.
    #[get(type(copy))]
    pub(crate) layer_id: u8,
}

/// Tightly packed GPU instance payload uploaded directly to Metal, Vulkan, D3D12, WebGPU, or WebGL2 buffers.
#[repr(C)]
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct GpuQuadInstance {
    /// Destination bounds `[x, y, width, height]` in physical pixels.
    #[get(type(copy))]
    pub(crate) rect: [f32; 4],
    /// Clip bounds `[x0, y0, x1, y1]` in physical pixels.
    #[get(type(copy))]
    pub(crate) clip: [f32; 4],
    /// Primary normalized RGBA color.
    #[get(type(copy))]
    pub(crate) color: [f32; 4],
    /// Secondary color (border color or gradient end color).
    #[get(type(copy))]
    pub(crate) secondary_color: [f32; 4],
    /// Corner radii `[tl, tr, br, bl]` in physical pixels.
    #[get(type(copy))]
    pub(crate) radii: [f32; 4],
    /// Border width or blur radius in physical pixels.
    #[get(type(copy))]
    pub(crate) stroke_or_blur: f32,
    /// Gradient angle in radians.
    #[get(type(copy))]
    pub(crate) angle_rad: f32,
    /// Encoded `GpuPrimitiveKind` discriminant.
    #[get(type(copy))]
    pub(crate) kind: u32,
    /// Padding for 16-byte GPU std140/std430 alignment.
    #[get(type(copy))]
    pub(crate) reserved: u32,
}

/// Coalesced instanced GPU draw batch submitted as a single hardware draw call.
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub struct GpuBatch {
    /// Active scissor rectangle in physical pixels.
    #[get(type(copy))]
    pub(crate) scissor: Rect,
    /// Instanced legacy 96-byte quad stream belonging to this batch.
    pub(crate) instances: Vec<GpuQuadInstance>,
    /// Unified 64-byte `GpuPrimitiveInstance` stream uploaded directly to GPU instance buffers.
    pub(crate) primitives: Vec<GpuPrimitiveInstance>,
    /// Retained scroll-layer offset `[scroll_x, scroll_y]` bound as vertex uniform for this batch.
    #[get(type(copy))]
    pub(crate) scroll_offset: [f32; 2],
    /// Bound texture array/slot identifier for this batch (`0` = default R8/RGBA8 glyph atlases).
    #[get(type(copy))]
    pub(crate) texture_slot: u8,
    /// Owning retained layer identifier (`0` = root layer).
    #[get(type(copy))]
    pub(crate) layer_id: u8,
}

/// Recording GPU driver used on headless targets and as a hardware-contract validator.
#[derive(Clone, Data, Debug, Default)]
pub struct RecordingGpuDriver {
    /// Emulated or bound graphics backend tag.
    #[get(type(copy))]
    pub(crate) backend_kind: GraphicsBackend,
    /// Total draw batches submitted across frames.
    #[get(type(copy))]
    pub(crate) total_draw_calls: usize,
    /// Total GPU quad instances submitted in the most recent frame.
    #[get(type(copy))]
    pub(crate) last_instance_count: usize,
    /// Last clear color submitted.
    #[get(type(copy))]
    pub(crate) last_clear: Color,
}

/// Per-batch coalescing metadata tracking whether all queued instances are unclipped within `bounds`.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct GpuBatchCoalesceState {
    /// True only while every instance in the active tail batch lies strictly inside its safe unclipped region.
    pub(crate) all_unclipped: bool,
    /// True when the active tail batch contains a backdrop-sampling primitive requiring strict scissor isolation.
    pub(crate) has_backdrop_blur: bool,
    /// Conservative physical bounding box covering all instances in the active tail batch.
    pub(crate) bounds: Rect,
    /// Active retained-layer scroll offset propagated into batch uniforms.
    pub(crate) scroll_offset: [f32; 2],
    /// Active retained-layer identifier encoded in `GpuPrimitiveInstance::kind_flags`.
    pub(crate) layer_id: u8,
}

/// Active physical clip scope pairing the scissor rectangle with its corner-free interior safe rectangle.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct GpuClipEntry {
    /// Intersected physical scissor rectangle.
    pub(crate) scissor: Rect,
    /// Conservative interior safe rectangle strictly clear of any active rounded-clip corners.
    pub(crate) safe_inner: Rect,
}

/// Hardware GPU renderer compiling `DisplayList` streams into order-preserving coalesced instanced `GpuBatch` calls.
#[derive(Data, Debug, Default)]
pub struct HardwareRenderer {
    /// Active physical OS GPU driver handling shader compilation and batch submission.
    pub(crate) driver: NativeGpuDriver,
    /// Reusable batch scratch buffer avoiding per-frame allocations.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) batches: Vec<GpuBatch>,
    /// Count of active `GpuBatch` slots populated for the latest frame so inner `Vec` capacities persist across frames.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(pub(crate))]
    pub(crate) active_batches: usize,
    /// Persistent physical clip-rectangle and safe-inner stack reused across frames without heap reallocation.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) clip_stack: Vec<GpuClipEntry>,
    /// Number of draw batches submitted in the most recent frame.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) last_submitted_batches: usize,
    /// Number of adjacent cross-clip batch splits eliminated via order-preserving coalescing in the latest frame.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) last_coalesced_merges: usize,
    /// Number of completely off-clip instances culled prior to GPU upload in the latest frame.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) last_culled_instances: usize,
    /// Number of `GpuQuadInstance` records uploaded in the latest frame.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) last_uploaded_instances: usize,
    /// Total instance payload bytes uploaded in the latest frame (`last_uploaded_instances * GPU_QUAD_INSTANCE_BYTES`).
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) last_uploaded_bytes: usize,
    /// Optional software readback mirror used for deterministic pixel-parity testing.
    pub(crate) readback: SoftwareRenderer,
    /// Whether readback mirroring is enabled.
    #[get(type(copy))]
    pub(crate) enable_readback: bool,
    /// Bounded GPU staging buffer ring, persistent scroll tile cache, and fence-deferred reclamation manager.
    pub(crate) resource_ring: GpuResourceRing,
    /// Cumulative count of frames rendered directly on the GPU path with zero CPU RGBA mirror generation.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) direct_gpu_frames: u64,
    /// Cumulative count of frames that also executed the CPU software readback mirror.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) cpu_readback_frames: u64,
    /// CPU RGBA framebuffer bytes generated on the most recent frame (`0` in direct GPU mode).
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) last_cpu_rgba_upload_bytes: usize,
    /// True when the most recently compiled frame contained an effect requiring full CPU reference fallback.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) has_unsupported_effect: bool,
    /// Monotonic GPU submission fence identifier assigned to the latest rendered frame.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) last_fence_id: u64,
    /// Active GPU effect-dependency `BackdropBlur` pass queue populated for the latest frame.
    pub(crate) blur_passes: Vec<GpuBackdropBlurPass>,
    /// Active retained-layer scroll offset `[scroll_x, scroll_y]` propagated to GPU layer uniforms.
    #[get(type(copy))]
    pub(crate) active_scroll_offset: [f32; 2],
    /// Active retained layer identifier (`0` = root).
    #[get(type(copy))]
    pub(crate) active_layer_id: u8,
    /// Packed 64-byte `GpuPrimitiveInstance` byte payload uploaded in the latest frame.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) last_uploaded_primitive_bytes: usize,
}