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

/// Flat 64-byte `#[repr(C)]` GPU compute layout input record aligned 1:1 with `FlatBfsTopology` BFS indices.
#[repr(C)]
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct GpuLayoutNodeInput {
    /// BFS index of this node's parent (`GPU_LAYOUT_INDEX_NONE` for root).
    #[get(type(copy))]
    pub(crate) parent_bfs: u32,
    /// BFS index of this node's first direct child (`GPU_LAYOUT_INDEX_NONE` when leaf).
    #[get(type(copy))]
    pub(crate) first_child: u32,
    /// Count of contiguous direct children starting at `first_child`.
    #[get(type(copy))]
    pub(crate) child_count: u32,
    /// Packed `LayoutStyle::flags` (`bits[0..20]`), width/height mode tags (`bits[20..24]`), and `GPU_LAYOUT_FLAG_*` (`bits[24..29]`).
    #[get(type(copy))]
    pub(crate) flags_kind: u32,
    /// Preferred and minimum outer size values `[pref_w, pref_h, min_w, min_h]`.
    #[get(type(copy))]
    pub(crate) pref_min_size: [f32; 4],
    /// Padding totals, main gap, and flex-grow or column-gap parameter `[pad_h, pad_v, gap_main, flex_grow_or_col_gap]`.
    #[get(type(copy))]
    pub(crate) pad_gap_flex: [f32; 4],
    /// Grid columns, aspect ratio, and pre-measured intrinsic dimensions `[grid_cols_f32, aspect_ratio, intrinsic_w, intrinsic_h]`.
    #[get(type(copy))]
    pub(crate) extra_or_intrinsic: [f32; 4],
}

/// Flat 32-byte `#[repr(C)]` GPU compute layout output record storing resolved border-box and content-box geometry.
#[repr(C)]
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct GpuLayoutNodeOutput {
    /// Resolved outer border-box rectangle `[x, y, width, height]` in logical coordinates.
    #[get(type(copy))]
    pub(crate) rect: [f32; 4],
    /// Resolved inner content-box rectangle `[cx, cy, cw, ch]` in logical coordinates.
    #[get(type(copy))]
    pub(crate) content_rect: [f32; 4],
}

/// Flat 48-byte `#[repr(C)]` GPU visual style record consumed by compute primitive culling and indirect draw emission.
#[repr(C)]
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct GpuVisualStyleInput {
    /// Packed premultiplied RGBA8 primary fill color.
    #[get(type(copy))]
    pub(crate) color_rgba: u32,
    /// Packed premultiplied RGBA8 secondary border or gradient color.
    #[get(type(copy))]
    pub(crate) aux_color_rgba: u32,
    /// Normalized `[u0, v0, u1, v1]` atlas UV coordinates in `[0..=65535]`.
    #[get(type(copy))]
    pub(crate) uv_rect: [u16; 4],
    /// Packed Q12.4 fixed-point corner radii `[tl, tr, br, bl]` in `1/16` px units.
    #[get(type(copy))]
    pub(crate) radii_border: [u16; 4],
    /// Encoded `GpuPrimitiveInstance::kind_flags` bitfield (`PrimKind`, clip mask, texture slot, layer).
    #[get(type(copy))]
    pub(crate) kind_flags: u32,
    /// Packed IEEE-754 `f16x2` `(border_or_blur, angle_or_pad)` auxiliary parameter.
    #[get(type(copy))]
    pub(crate) extra_param: u32,
    /// BFS index of nearest clipping ancestor (`GPU_LAYOUT_INDEX_NONE` when unclipped).
    #[get(type(copy))]
    pub(crate) clip_ancestor_bfs: u32,
    /// Scroll-layer identifier bound to this node's transform/scroll uniform.
    #[get(type(copy))]
    pub(crate) scroll_layer_id: u32,
    /// Fixed-point Q16 opacity multiplier (`0..=65535`).
    #[get(type(copy))]
    pub(crate) opacity_q16: u32,
    /// Stacking order bias mirrored from `LayoutStyle::z_index`.
    #[get(type(copy))]
    pub(crate) z_order_bias: i32,
}

/// Contiguous dirty byte range queued for incremental GPU storage or instance buffer flushing.
#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, Ord, PartialEq, PartialOrd)]
pub struct DirtyBufferSpan {
    /// Inclusive starting byte offset inside the target GPU buffer.
    #[get(type(copy))]
    pub(crate) byte_offset: usize,
    /// Number of dirty bytes starting at `byte_offset`.
    #[get(type(copy))]
    pub(crate) byte_len: usize,
}

/// Reusable dirty-range coalescer and retained primitive diff buffer merging spans within `merge_gap_bytes`.
#[derive(Clone, Data, Debug, PartialEq)]
pub struct DirtySpanCoalescer {
    /// Maximum inter-span byte gap bridged when coalescing (`DIRTY_SPAN_MERGE_GAP_BYTES` by default).
    #[get(type(copy))]
    pub(crate) merge_gap_bytes: usize,
    /// Unsorted raw dirty spans collected since the last `clear()`.
    pub(crate) pending: Vec<DirtyBufferSpan>,
    /// Sorted, non-overlapping coalesced dirty spans ready for GPU upload.
    pub(crate) coalesced: Vec<DirtyBufferSpan>,
    /// Whether `pending` contains uncoalesced entries requiring a merge pass.
    #[get(type(copy))]
    pub(crate) needs_coalesce: bool,
    /// Retained previous-frame primitive stream used to detect changed primitive slots.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) retained_primitives: Vec<GpuPrimitiveInstance>,
    /// Coalesced dirty bytes uploaded in the most recent primitive diff frame.
    #[get(type(copy))]
    pub(crate) last_dirty_bytes: usize,
    /// Full-frame primitive byte size in the most recent primitive diff frame.
    #[get(type(copy))]
    pub(crate) last_full_bytes: usize,
    /// Cumulative bytes saved across all frames by incremental dirty-span flushing.
    #[get(type(copy))]
    pub(crate) cumulative_bytes_saved: u64,
}

/// Level-contiguous Breadth-First Search (BFS) topology consumed identically by parallel CPU layout and GPU compute shaders.
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub struct FlatBfsTopology {
    /// `SceneTree::topology_revision` captured when this BFS layout was constructed.
    #[get(type(copy))]
    pub(crate) topology_revision: u64,
    /// Arena slot index of the root node (`GPU_LAYOUT_INDEX_NONE` when empty).
    #[get(type(copy))]
    pub(crate) root_arena_index: u32,
    /// Mapping from BFS node index `0..node_count` to `SceneTree` arena slot index.
    pub(crate) bfs_to_arena: Vec<u32>,
    /// Mapping from `SceneTree` arena slot index to BFS index (`GPU_LAYOUT_INDEX_NONE` when detached).
    pub(crate) arena_to_bfs: Vec<u32>,
    /// Parent BFS index for each BFS node (`GPU_LAYOUT_INDEX_NONE` at root).
    pub(crate) parent_bfs: Vec<u32>,
    /// Starting BFS index of each node's contiguous direct children slice (`GPU_LAYOUT_INDEX_NONE` when leaf).
    pub(crate) child_first: Vec<u32>,
    /// Number of direct children for each BFS node.
    pub(crate) child_count: Vec<u32>,
    /// Prefix offsets partitioning `bfs_to_arena` into tree depth levels (`level_offsets[d]..level_offsets[d + 1]`).
    pub(crate) level_offsets: Vec<u32>,
    /// Total node count inside each BFS node's subtree (including self) for work granularity and partitioning.
    pub(crate) subtree_sizes: Vec<u32>,
}

/// Compact byte-range slice into `SceneLayoutSoa::text_bytes` enabling zero-allocation `Send + Sync` text reads.
#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
pub struct SoaTextSlice {
    /// Starting byte offset inside `SceneLayoutSoa::text_bytes`.
    #[get(type(copy))]
    pub(crate) byte_start: u32,
    /// UTF-8 byte length of the node's text payload (`0` for non-text nodes).
    #[get(type(copy))]
    pub(crate) byte_len: u32,
    /// True when every byte in the slice is ASCII (`< 128`), enabling SIMD advance fast-paths.
    #[get(type(copy))]
    pub(crate) is_ascii: bool,
}

/// Dense `Send + Sync` Structure-of-Arrays (SoA) layout, text, visual, and GPU compute staging snapshot extracted from `SceneTree`.
#[derive(Clone, Data, Debug, Default)]
pub struct SceneLayoutSoa {
    /// Last `SceneTree::revision` synchronized into this SoA snapshot.
    #[get(type(copy))]
    pub(crate) synced_revision: u64,
    /// Last `SceneTree::topology_revision` synchronized into `topology`.
    #[get(type(copy))]
    pub(crate) synced_topology_revision: u64,
    /// Last `SceneTree::serial_layout_epoch` synchronized into `layout_boxes` and `gpu_outputs`.
    #[get(type(copy))]
    pub(crate) synced_serial_epoch: u64,
    /// Synchronization strategy executed during the most recent `sync_from_tree`.
    #[get(type(copy))]
    pub(crate) last_sync_mode: SoaSyncMode,
    /// Level-ordered BFS topology mirroring the live tree structure.
    pub(crate) topology: FlatBfsTopology,
    /// Arena-indexed live `NodeId` handles parallel to `SceneTree::nodes`.
    pub(crate) node_ids: Vec<NodeId>,
    /// Arena-indexed `SoaNodeKindTag` values parallel to `SceneTree::nodes`.
    pub(crate) node_kinds: Vec<SoaNodeKindTag>,
    /// Arena-indexed `LayoutStyle` records parallel to `SceneTree::nodes`.
    pub(crate) layout_styles: Vec<LayoutStyle>,
    /// Arena-indexed `TextStyle` records parallel to `SceneTree::nodes`.
    pub(crate) text_styles: Vec<TextStyle>,
    /// Arena-indexed border widths in logical pixels parallel to `SceneTree::nodes`.
    pub(crate) border_widths: Vec<f32>,
    /// Arena-indexed 2D paint transforms parallel to `SceneTree::nodes`.
    pub(crate) transforms: Vec<Transform2D>,
    /// Arena-indexed `LayoutBox` geometry mirrored from and written back to `SceneTree`.
    pub(crate) layout_boxes: Vec<LayoutBox>,
    /// Arena-indexed dirty bitmasks mirrored from `SceneTree::meta`.
    pub(crate) dirty_masks: Vec<u16>,
    /// Arena-indexed monotonic mutation epochs mirrored from `SceneTree::node_mutation_epochs`.
    pub(crate) synced_node_epochs: Vec<u32>,
    /// Arena-indexed text slices referencing `text_bytes`.
    pub(crate) text_slices: Vec<SoaTextSlice>,
    /// Contiguous UTF-8 text byte slab shared read-only across parallel CPU layout workers.
    pub(crate) text_bytes: Vec<u8>,
    /// Arena-indexed intrinsic sizes for `NodeKind::Image` leaves.
    pub(crate) image_sizes: Vec<Size>,
    /// BFS-ordered 64-byte GPU compute layout input buffer ready for incremental upload.
    pub(crate) gpu_inputs: Vec<GpuLayoutNodeInput>,
    /// BFS-ordered 32-byte GPU compute layout output buffer mirroring computed node rects.
    pub(crate) gpu_outputs: Vec<GpuLayoutNodeOutput>,
    /// BFS-ordered 48-byte GPU visual style input buffer for indirect draw culling and emission.
    pub(crate) gpu_visuals: Vec<GpuVisualStyleInput>,
    /// Coalesced dirty byte spans covering modified ranges of `gpu_inputs`.
    pub(crate) input_dirty_spans: DirtySpanCoalescer,
    /// Coalesced dirty byte spans covering modified ranges of `gpu_visuals`.
    pub(crate) visual_dirty_spans: DirtySpanCoalescer,
}