Skip to main content

awsm_renderer/
loading.rs

1//! The load-transaction progress surface.
2//!
3//! [`LoadingStats`] is the single struct both [`crate::AwsmRenderer::commit_load`]'s
4//! `on_progress` callback and the imperative [`crate::AwsmRenderer::loading_stats`]
5//! poller report. It supersedes the ad-hoc `CompileProgress`-only progress that the
6//! old `wait_for_pipelines_ready_with_progress` surfaced: one struct carries the
7//! texture-upload phase AND the pipeline-compile phase of a commit, so a loader can
8//! drive a single progress bar across the whole transaction.
9//!
10//! (The coarser, per-load-step [`crate::load_phase::LoadPhase`] a scene loader emits
11//! while *building* a scene is a different, higher-level thing — it brackets the adds
12//! that precede the commit; `LoadingStats` describes the commit itself.)
13
14use crate::pipeline_scheduler::CompileProgress;
15
16/// Which phase of a [`crate::AwsmRenderer::commit_load`] is in flight.
17#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
18pub enum LoadPhase {
19    /// No commit has run yet (or the renderer is between commits).
20    #[default]
21    Idle,
22    /// Deriving + uploading each registered geometry's needed pass representations
23    /// (visibility / transparency) from its retained source — the first commit phase.
24    UploadingGeometry,
25    /// Finalizing the texture pool — the one batched GPU upload of every staged image.
26    FinalizingTextures,
27    /// Driving the scene's pipeline compiles to completion.
28    Compiling,
29    /// The commit landed; the scene is committed and renders this frame on.
30    Ready,
31}
32
33/// Snapshot of a load transaction's progress. Reported by `commit_load`'s
34/// `on_progress` (live, per resolution) and by `loading_stats()` (imperative poll).
35#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
36pub struct LoadingStats {
37    /// Current commit phase.
38    pub phase: LoadPhase,
39    /// Total geometries whose representations this commit will (re)build.
40    pub geometry_total: usize,
41    /// Geometries resolved (representations uploaded) so far.
42    pub geometry_uploaded: usize,
43    /// Total textures the pool will upload this commit.
44    pub textures_total: usize,
45    /// Textures uploaded so far.
46    pub textures_uploaded: usize,
47    /// Materials still compiling (`CompileProgress::materials_pending`).
48    pub pipelines_pending: usize,
49    /// Materials fully resolved (`CompileProgress::materials_ready`).
50    pub pipelines_ready: usize,
51    /// Materials whose compile failed (`CompileProgress::materials_failed`).
52    pub pipelines_failed: usize,
53    /// In-flight sub-pipeline compiles summed across pending materials.
54    pub in_flight_subcompiles: u32,
55}
56
57impl LoadingStats {
58    /// Pipelines still in flight this commit (materials pending + their summed
59    /// sub-pipeline compiles) — the single "how much compile is left" number.
60    pub fn pipelines_remaining(&self) -> usize {
61        self.pipelines_pending + self.in_flight_subcompiles as usize
62    }
63
64    /// Human-facing progress line for the active commit phase — the SHARED mapping
65    /// both viewers' loading overlays render, so geometry/texture/pipeline progress
66    /// reads identically everywhere. `None` for `Idle` /
67    /// `Ready` (no banner needed).
68    pub fn phase_label(&self) -> Option<String> {
69        match self.phase {
70            LoadPhase::Idle | LoadPhase::Ready => None,
71            LoadPhase::UploadingGeometry => Some(format!(
72                "Uploading geometry {}/{}",
73                self.geometry_uploaded, self.geometry_total
74            )),
75            LoadPhase::FinalizingTextures => Some(format!(
76                "Uploading textures {}/{}",
77                self.textures_uploaded, self.textures_total
78            )),
79            LoadPhase::Compiling => Some(format!(
80                "Compiling pipelines ({} remaining)",
81                self.pipelines_remaining()
82            )),
83        }
84    }
85
86    /// Build a snapshot from a [`CompileProgress`] plus the commit's texture counts
87    /// and phase. The compile drain calls this per resolution to map the scheduler's
88    /// `CompileProgress` into the unified `LoadingStats` shape.
89    pub(crate) fn from_parts(
90        phase: LoadPhase,
91        geometry_total: usize,
92        geometry_uploaded: usize,
93        textures_total: usize,
94        textures_uploaded: usize,
95        cp: CompileProgress,
96    ) -> Self {
97        Self {
98            phase,
99            geometry_total,
100            geometry_uploaded,
101            textures_total,
102            textures_uploaded,
103            pipelines_pending: cp.materials_pending,
104            pipelines_ready: cp.materials_ready,
105            pipelines_failed: cp.materials_failed,
106            in_flight_subcompiles: cp.in_flight_subcompiles,
107        }
108    }
109}