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

/// Exact 16-byte `#[repr(C)]` GPU indirect draw argument buffer matching `MTLDrawPrimitivesIndirectArguments` and WGSL/Vulkan indirect draw.
#[repr(C)]
#[derive(Clone, Copy, Data, Debug, Default, Eq, PartialEq)]
pub struct GpuDrawIndirectArgs {
    /// Vertex count per instance (`6` for two-triangle UI quad).
    #[get(type(copy))]
    pub(crate) vertex_count: u32,
    /// Compacted visible primitive instance count atomically populated by `euv_layout_cull_and_emit`.
    #[get(type(copy))]
    pub(crate) instance_count: u32,
    /// First vertex offset (`0`).
    #[get(type(copy))]
    pub(crate) vertex_start: u32,
    /// Base instance offset (`0`).
    #[get(type(copy))]
    pub(crate) base_instance: u32,
}

/// Flat 64-byte `#[repr(C)]` per-level uniform payload bound at buffer slot 5 across all 4 compute kernels.
#[repr(C)]
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct GpuLevelUniform {
    /// Starting BFS index of the active tree depth level.
    #[get(type(copy))]
    pub(crate) level_start: u32,
    /// Node count in the active tree depth level.
    #[get(type(copy))]
    pub(crate) level_count: u32,
    /// Total node count across all levels.
    #[get(type(copy))]
    pub(crate) node_count: u32,
    /// Optional pass flags bitmask.
    #[get(type(copy))]
    pub(crate) pass_flags: u32,
    /// Physical viewport dimensions `[width, height]`.
    #[get(type(copy))]
    pub(crate) viewport_size: [f32; 2],
    /// Simulation frame delta time in seconds for Spring physics layout.
    #[get(type(copy))]
    pub(crate) dt_secs: f32,
    /// Reserved alignment padding.
    #[get(type(copy))]
    pub(crate) reserved: u32,
    /// Root boundary rectangle `[x, y, width, height]` constraining subtree roots.
    #[get(type(copy))]
    pub(crate) root_boundary_rect: [f32; 4],
    /// Query pointer coordinate `[x, y]` for `euv_layout_hit_test`.
    #[get(type(copy))]
    pub(crate) pick_point: [f32; 2],
    /// Trailing 16-byte alignment padding.
    #[get(type(copy))]
    pub(crate) pad_tail: [f32; 2],
}

/// Flat BFS scene descriptor submitted to `GpuComputeLayoutEngine`.
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub struct GpuComputeSceneInput {
    /// Contiguous BFS-ordered layout node inputs.
    pub(crate) nodes: Vec<GpuLayoutNodeInput>,
    /// Contiguous BFS-ordered visual style inputs paired 1:1 with `nodes`.
    pub(crate) visuals: Vec<GpuVisualStyleInput>,
    /// Inclusive prefix offsets partitioning `nodes` by BFS depth (`depth_count + 1` entries).
    pub(crate) level_offsets: Vec<u32>,
    /// Physical viewport `[width, height]`.
    #[get(type(copy))]
    pub(crate) viewport_size: [f32; 2],
    /// Root boundary rectangle `[x, y, width, height]`.
    #[get(type(copy))]
    pub(crate) root_boundary_rect: [f32; 4],
    /// Frame time step in seconds for Spring mode.
    #[get(type(copy))]
    pub(crate) dt_secs: f32,
    /// Optional pointer hit-test coordinate `[x, y]`.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) pick_point: Option<[f32; 2]>,
    /// Clear color for optional indirect render pass.
    #[get(type(copy))]
    pub(crate) clear_color: Color,
}

/// Complete telemetry and output snapshot returned by one `GpuComputeLayoutEngine` execution.
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub struct GpuComputeDispatchResult {
    /// Backend that executed the compute layout and optional indirect draw pass.
    #[get(type(copy))]
    pub(crate) backend_kind: GpuComputeBackendKind,
    /// Resolved per-node physical rects and content boxes.
    pub(crate) outputs: Vec<GpuLayoutNodeOutput>,
    /// Compacted visible 64-byte `GpuPrimitiveInstance` stream emitted by `euv_layout_cull_and_emit`.
    pub(crate) primitives: Vec<GpuPrimitiveInstance>,
    /// Indirect draw arguments written by the GPU compute compaction pass.
    #[get(type(copy))]
    pub(crate) indirect_args: GpuDrawIndirectArgs,
    /// Hit-tested BFS node index (`GPU_HIT_TEST_MISS` when missed or skipped).
    #[get(type(copy))]
    pub(crate) hit_node_bfs: u32,
    /// Measured GPU or reference compute duration in nanoseconds.
    #[get(type(copy))]
    pub(crate) gpu_compute_ns: u64,
    /// Measured GPU indirect render duration in nanoseconds (`0` when compute-only).
    #[get(type(copy))]
    pub(crate) gpu_render_ns: u64,
    /// Number of indirect GPU draw calls executed.
    #[get(type(copy))]
    pub(crate) draw_calls: usize,
    /// Number of non-zero pixels verified in offscreen GPU render target readback (`0` when not read back).
    #[get(type(copy))]
    pub(crate) offscreen_non_zero_pixels: usize,
    /// Total upload bytes copied to GPU buffers during this dispatch.
    #[get(type(copy))]
    pub(crate) upload_bytes: usize,
}

/// Process-wide cached Apple Metal compute pipeline states for all 4 layout compute kernels.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct MetalComputePipelineCache {
    /// Retained `id<MTLDevice>` handle.
    pub(crate) device: usize,
    /// Retained `id<MTLCommandQueue>` handle.
    pub(crate) queue: usize,
    /// Retained `id<MTLComputePipelineState>` for `euv_layout_measure_bottom_up`.
    pub(crate) measure_pso: usize,
    /// Retained `id<MTLComputePipelineState>` for `euv_layout_measure_flat_leaves`.
    pub(crate) measure_leaves_pso: usize,
    /// Retained `id<MTLComputePipelineState>` for `euv_layout_solve_top_down`.
    pub(crate) solve_pso: usize,
    /// Retained `id<MTLComputePipelineState>` for `euv_layout_solve_flat_children`.
    pub(crate) solve_flat_pso: usize,
    /// Retained `id<MTLComputePipelineState>` for `euv_layout_cull_and_emit`.
    pub(crate) cull_emit_pso: usize,
    /// Retained `id<MTLComputePipelineState>` for `euv_layout_hit_test`.
    pub(crate) hit_test_pso: usize,
    /// Retained UI uber-shader `MTLRenderPipelineState` for indirect draw execution.
    pub(crate) ui_render_pso: usize,
    /// Retained 1x1 fallback R8 texture handle.
    pub(crate) dummy_r8_tex: usize,
    /// Retained 1x1 fallback RGBA8 texture handle.
    pub(crate) dummy_rgba8_tex: usize,
    /// True when the Metal device uses unified CPU/GPU shared memory.
    pub(crate) has_unified_memory: bool,
}

/// Cached Objective-C selector pointers for Metal compute, indirect draw, and offscreen texture readback.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct MetalComputeSelectors {
    /// Selector `newComputePipelineStateWithFunction:error:`.
    pub(crate) new_compute_pso: usize,
    /// Selector `computeCommandEncoder`.
    pub(crate) compute_encoder: usize,
    /// Selector `setComputePipelineState:`.
    pub(crate) set_compute_pso: usize,
    /// Selector `setBuffer:offset:atIndex:`.
    pub(crate) set_compute_buffer: usize,
    /// Selector `setBytes:length:atIndex:`.
    pub(crate) set_compute_bytes: usize,
    /// Selector `dispatchThreadgroups:threadsPerThreadgroup:`.
    pub(crate) dispatch_threadgroups: usize,
    /// Selector `drawPrimitives:indirectBuffer:indirectBufferOffset:`.
    pub(crate) draw_primitives_indirect: usize,
    /// Selector `getBytes:bytesPerRow:fromRegion:mipmapLevel:`.
    pub(crate) get_bytes_region: usize,
    /// Selector `setStorageMode:`.
    pub(crate) set_storage_mode: usize,
    /// Selector `hasUnifiedMemory`.
    pub(crate) has_unified_memory: usize,
    /// Selector `blitCommandEncoder`.
    pub(crate) blit_encoder: usize,
    /// Selector `synchronizeResource:`.
    pub(crate) sync_resource: usize,
    /// Selector `copyFromBuffer:sourceOffset:toBuffer:destinationOffset:size:`.
    pub(crate) copy_from_buffer: usize,
}

/// Persistent GPU compute layout and zero-readback indirect draw engine supporting Apple Metal hardware and bit-exact CPU parity.
#[derive(Debug, Getter)]
pub struct GpuComputeLayoutEngine {
    /// Active compute backend kind (`MetalHardware` on physical Apple GPUs, else `CpuReferenceParity`).
    #[get(type(copy))]
    pub(crate) backend_kind: GpuComputeBackendKind,
    /// Retained native Metal device handle (`0` on CPU reference parity).
    #[get(type(copy))]
    pub(crate) device_handle: usize,
    /// Retained native Metal command queue handle (`0` on CPU reference parity).
    #[get(type(copy))]
    pub(crate) queue_handle: usize,
    /// Retained persistent input `MTLBuffer` handle.
    #[get(type(copy))]
    pub(crate) input_buffer_handle: usize,
    /// Mapped CPU pointer for `input_buffer_handle`.
    #[get(type(copy))]
    pub(crate) input_contents_ptr: usize,
    /// Retained persistent output `MTLBuffer` handle.
    #[get(type(copy))]
    pub(crate) output_buffer_handle: usize,
    /// Mapped CPU pointer for `output_buffer_handle`.
    #[get(type(copy))]
    pub(crate) output_contents_ptr: usize,
    /// Retained persistent visual style `MTLBuffer` handle.
    #[get(type(copy))]
    pub(crate) visual_buffer_handle: usize,
    /// Mapped CPU pointer for `visual_buffer_handle`.
    #[get(type(copy))]
    pub(crate) visual_contents_ptr: usize,
    /// Retained persistent indirect primitive output `MTLBuffer` handle.
    #[get(type(copy))]
    pub(crate) primitive_buffer_handle: usize,
    /// Mapped CPU pointer for `primitive_buffer_handle`.
    #[get(type(copy))]
    pub(crate) primitive_contents_ptr: usize,
    /// Retained 16-byte `GpuDrawIndirectArgs` `MTLBuffer` handle.
    #[get(type(copy))]
    pub(crate) indirect_buffer_handle: usize,
    /// Mapped CPU pointer for `indirect_buffer_handle`.
    #[get(type(copy))]
    pub(crate) indirect_contents_ptr: usize,
    /// Retained 4-byte hit-test result `MTLBuffer` handle.
    #[get(type(copy))]
    pub(crate) hit_buffer_handle: usize,
    /// Mapped CPU pointer for `hit_buffer_handle`.
    #[get(type(copy))]
    pub(crate) hit_contents_ptr: usize,
    /// Capacity in nodes currently allocated across the persistent GPU compute buffers.
    #[get(type(copy))]
    pub(crate) allocated_node_capacity: usize,
    /// Retained reusable offscreen `BGRA8Unorm` `MTLTexture` handle for hardware render verification.
    #[get(type(copy))]
    pub(crate) offscreen_texture_handle: usize,
    /// Width of `offscreen_texture_handle` in pixels.
    #[get(type(copy))]
    pub(crate) offscreen_width: u32,
    /// Height of `offscreen_texture_handle` in pixels.
    #[get(type(copy))]
    pub(crate) offscreen_height: u32,
    /// Cumulative hardware or reference compute dispatches executed.
    #[get(type(copy))]
    pub(crate) total_dispatches: u64,
    /// Cached uniform grid row height (`> 0.0` when the uploaded flat grid root has uniform child row heights).
    #[get(type(copy))]
    pub(crate) cached_uniform_row_h: f32,
    /// Ping-pong `MTLStorageModeShared` staging buffer handles for `in_flight = 2` dirty-span GPU blits.
    #[get(type(copy))]
    pub(crate) upload_staging_handles: [usize; 2],
    /// Mapped CPU write pointers for `upload_staging_handles`.
    #[get(type(copy))]
    pub(crate) upload_staging_ptrs: [usize; 2],
    /// GPU-private `MTLStorageModePrivate` destination buffer handle receiving coalesced dirty spans.
    #[get(type(copy))]
    pub(crate) upload_private_handle: usize,
    /// Allocated byte capacity of `upload_staging_handles` and `upload_private_handle`.
    #[get(type(copy))]
    pub(crate) upload_capacity_bytes: usize,
}