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

/// Completed background or cooperative decode result ready for main-thread residency admission.
#[derive(Clone, Debug)]
pub(crate) struct CompletedStreamJob {
    /// Deterministic 64-bit asset key hash.
    pub(crate) key_hash: u64,
    /// Target GPU bindless texture slot index.
    pub(crate) texture_slot: u32,
    /// Decoded payload or structured failure reason.
    pub(crate) payload: Result<DecodedAssetPayload, String>,
}

/// Resumable state for a single large asset split across multiple cooperative `<= 1.5 ms` frames.
#[derive(Clone, Debug)]
pub(crate) struct CooperativeInProgressJob {
    /// Active work item being decoded incrementally.
    pub(crate) work: StreamWorkItem,
    /// Cumulative decode microseconds spent across cooperative slices for this job.
    pub(crate) accumulated_us: u64,
    /// Expected uncompressed payload CRC32 verified upon completion.
    pub(crate) uncompressed_crc32: u32,
    /// Incremental running CRC32 state updated chunk-by-chunk during LZ4 streaming.
    pub(crate) running_uncompressed_crc: u32,
    /// Resumable LZ4 stream decoder when decompressing an LZ4 `.euvpak` block.
    pub(crate) lz4_decoder: Option<Lz4StreamDecoder>,
    /// Compressed bytes held for incremental LZ4 block decoding.
    pub(crate) compressed_bytes: Vec<u8>,
    /// Uncompressed bytes ready for raster/mesh decode stage once LZ4 finishes.
    pub(crate) uncompressed_bytes: Option<Vec<u8>>,
}

/// Shared thread-safe state accessed concurrently by native background I/O & decode workers.
#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug)]
pub(crate) struct StreamWorkerContext {
    /// 4-lane priority work-stealing queue with coalescing and atomic cancellation.
    pub(crate) queue: Arc<PriorityStreamQueue>,
    /// Shared optional `.euvpak` archive backing zero-copy worker reads.
    pub(crate) vfs: Mutex<Option<Arc<EuvPakArchive>>>,
    /// Completed decoded jobs awaiting O(1) main-thread ingestion.
    pub(crate) completed: Mutex<VecDeque<CompletedStreamJob>>,
    /// Lock-free count of completed jobs pending main-thread ingestion.
    pub(crate) completed_count: AtomicUsize,
    /// Count of workers currently executing a popped decode job.
    pub(crate) in_progress_workers: AtomicUsize,
    /// Shared lock-free telemetry counters.
    pub(crate) telemetry: Arc<StreamingTelemetryCounters>,
}

/// Unified asynchronous asset streaming coordinator orchestrating `.euvpak` VFS reads, priority decoding, Clock-LRU residency, spatial prefetch, and frame-budgeted GPU uploads.
#[derive(Debug)]
pub struct AssetStreamCoordinator {
    /// Active memory and per-frame time ceilings.
    pub(crate) budget: AssetStreamBudget,
    /// Optional mounted `.euvpak` virtual filesystem archive.
    pub(crate) vfs: Option<Arc<EuvPakArchive>>,
    /// Lock-free atomic streaming telemetry counters.
    pub(crate) telemetry: Arc<StreamingTelemetryCounters>,
    /// Shared 4-lane priority work-stealing queue.
    pub(crate) queue: Arc<PriorityStreamQueue>,
    /// Shared worker pool context for native background threads.
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) worker_ctx: Arc<StreamWorkerContext>,
    /// Dedicated background I/O & decode worker thread handles on native targets.
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) workers: Vec<JoinHandle<()>>,
    /// Configured background worker thread count on native targets.
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) target_worker_count: usize,
    /// Active execution mode (`AsyncWorkers` on native, `CooperativeTimeSliced` on WASM or test mode).
    pub(crate) execution_mode: StreamExecutionMode,
    /// Resumable in-progress job for cooperative time-sliced decoding.
    pub(crate) cooperative_job: Option<CooperativeInProgressJob>,
    /// O(1) 2-hand Clock-LRU progressive mipmap and RAM/VRAM residency pool.
    pub(crate) residency: ProgressiveResidencyPool,
    /// Authoritative `ResourceAccountant` tracking live streaming RAM and VRAM allocations.
    pub(crate) accountant: ResourceAccountant,
    /// Camera velocity-predicted 3D spatial hash prefetcher.
    pub(crate) spatial_grid: SpatialPrefetchGrid,
    /// Queued incremental GPU texture mipmap uploads awaiting budget-capped draining.
    pub(crate) pending_gpu_uploads: VecDeque<GpuTextureMipUpload>,
    /// Most recent velocity-predicted spatial prefetch focus volume.
    pub(crate) last_prefetch_volume: SpatialPrefetchVolume,
    /// Monotonic counter assigning deterministic GPU texture slot ids for auto-registered keys.
    pub(crate) next_auto_texture_slot: u32,
    /// Stable mapping from 64-bit texture key hash to GPU bindless texture slot id.
    pub(crate) texture_slots: HashMap<u64, u32>,
    /// Reusable lookahead anchor scratch buffer avoiding per-frame heap allocations.
    pub(crate) prefetch_scratch: Vec<SpatialAssetAnchor>,
    /// Reusable behind-camera cancel key scratch buffer avoiding per-frame heap allocations.
    pub(crate) cancel_scratch: Vec<u64>,
}