Skip to main content

euv_core/runtime/streaming/decoder/
struct.rs

1use super::*;
2
3/// Completed background or cooperative decode result ready for main-thread residency admission.
4#[derive(Clone, Debug)]
5pub(crate) struct CompletedStreamJob {
6    /// Deterministic 64-bit asset key hash.
7    pub(crate) key_hash: u64,
8    /// Target GPU bindless texture slot index.
9    pub(crate) texture_slot: u32,
10    /// Decoded payload or structured failure reason.
11    pub(crate) payload: Result<DecodedAssetPayload, String>,
12}
13
14/// Resumable state for a single large asset split across multiple cooperative `<= 1.5 ms` frames.
15#[derive(Clone, Debug)]
16pub(crate) struct CooperativeInProgressJob {
17    /// Active work item being decoded incrementally.
18    pub(crate) work: StreamWorkItem,
19    /// Cumulative decode microseconds spent across cooperative slices for this job.
20    pub(crate) accumulated_us: u64,
21    /// Expected uncompressed payload CRC32 verified upon completion.
22    pub(crate) uncompressed_crc32: u32,
23    /// Incremental running CRC32 state updated chunk-by-chunk during LZ4 streaming.
24    pub(crate) running_uncompressed_crc: u32,
25    /// Resumable LZ4 stream decoder when decompressing an LZ4 `.euvpak` block.
26    pub(crate) lz4_decoder: Option<Lz4StreamDecoder>,
27    /// Compressed bytes held for incremental LZ4 block decoding.
28    pub(crate) compressed_bytes: Vec<u8>,
29    /// Uncompressed bytes ready for raster/mesh decode stage once LZ4 finishes.
30    pub(crate) uncompressed_bytes: Option<Vec<u8>>,
31}
32
33/// Shared thread-safe state accessed concurrently by native background I/O & decode workers.
34#[cfg(not(target_arch = "wasm32"))]
35#[derive(Debug)]
36pub(crate) struct StreamWorkerContext {
37    /// 4-lane priority work-stealing queue with coalescing and atomic cancellation.
38    pub(crate) queue: Arc<PriorityStreamQueue>,
39    /// Shared optional `.euvpak` archive backing zero-copy worker reads.
40    pub(crate) vfs: Mutex<Option<Arc<EuvPakArchive>>>,
41    /// Completed decoded jobs awaiting O(1) main-thread ingestion.
42    pub(crate) completed: Mutex<VecDeque<CompletedStreamJob>>,
43    /// Lock-free count of completed jobs pending main-thread ingestion.
44    pub(crate) completed_count: AtomicUsize,
45    /// Count of workers currently executing a popped decode job.
46    pub(crate) in_progress_workers: AtomicUsize,
47    /// Shared lock-free telemetry counters.
48    pub(crate) telemetry: Arc<StreamingTelemetryCounters>,
49}
50
51/// Unified asynchronous asset streaming coordinator orchestrating `.euvpak` VFS reads, priority decoding, Clock-LRU residency, spatial prefetch, and frame-budgeted GPU uploads.
52#[derive(Debug)]
53pub struct AssetStreamCoordinator {
54    /// Active memory and per-frame time ceilings.
55    pub(crate) budget: AssetStreamBudget,
56    /// Optional mounted `.euvpak` virtual filesystem archive.
57    pub(crate) vfs: Option<Arc<EuvPakArchive>>,
58    /// Lock-free atomic streaming telemetry counters.
59    pub(crate) telemetry: Arc<StreamingTelemetryCounters>,
60    /// Shared 4-lane priority work-stealing queue.
61    pub(crate) queue: Arc<PriorityStreamQueue>,
62    /// Shared worker pool context for native background threads.
63    #[cfg(not(target_arch = "wasm32"))]
64    pub(crate) worker_ctx: Arc<StreamWorkerContext>,
65    /// Dedicated background I/O & decode worker thread handles on native targets.
66    #[cfg(not(target_arch = "wasm32"))]
67    pub(crate) workers: Vec<JoinHandle<()>>,
68    /// Configured background worker thread count on native targets.
69    #[cfg(not(target_arch = "wasm32"))]
70    pub(crate) target_worker_count: usize,
71    /// Active execution mode (`AsyncWorkers` on native, `CooperativeTimeSliced` on WASM or test mode).
72    pub(crate) execution_mode: StreamExecutionMode,
73    /// Resumable in-progress job for cooperative time-sliced decoding.
74    pub(crate) cooperative_job: Option<CooperativeInProgressJob>,
75    /// O(1) 2-hand Clock-LRU progressive mipmap and RAM/VRAM residency pool.
76    pub(crate) residency: ProgressiveResidencyPool,
77    /// Authoritative `ResourceAccountant` tracking live streaming RAM and VRAM allocations.
78    pub(crate) accountant: ResourceAccountant,
79    /// Camera velocity-predicted 3D spatial hash prefetcher.
80    pub(crate) spatial_grid: SpatialPrefetchGrid,
81    /// Queued incremental GPU texture mipmap uploads awaiting budget-capped draining.
82    pub(crate) pending_gpu_uploads: VecDeque<GpuTextureMipUpload>,
83    /// Most recent velocity-predicted spatial prefetch focus volume.
84    pub(crate) last_prefetch_volume: SpatialPrefetchVolume,
85    /// Monotonic counter assigning deterministic GPU texture slot ids for auto-registered keys.
86    pub(crate) next_auto_texture_slot: u32,
87    /// Stable mapping from 64-bit texture key hash to GPU bindless texture slot id.
88    pub(crate) texture_slots: HashMap<u64, u32>,
89    /// Reusable lookahead anchor scratch buffer avoiding per-frame heap allocations.
90    pub(crate) prefetch_scratch: Vec<SpatialAssetAnchor>,
91    /// Reusable behind-camera cancel key scratch buffer avoiding per-frame heap allocations.
92    pub(crate) cancel_scratch: Vec<u64>,
93}