Skip to main content

molgfx_render/engine/derived_cache/
model.rs

1//! Value types for deterministic derived-resource accounting.
2
3/// Independent hard limits for recomputable CPU and GPU data.
4#[derive(Clone, Copy, PartialEq, Eq, Debug)]
5pub struct DerivedCacheBudget {
6    /// Maximum retained host bytes.
7    pub cpu_bytes: u64,
8    /// Maximum retained device bytes.
9    pub gpu_bytes: u64,
10}
11
12impl Default for DerivedCacheBudget {
13    fn default() -> Self {
14        Self {
15            cpu_bytes: 128 * 1024 * 1024,
16            gpu_bytes: 256 * 1024 * 1024,
17        }
18    }
19}
20
21/// Current and peak derived-cache charges.
22#[derive(Clone, Copy, Default, PartialEq, Eq, Debug)]
23pub struct DerivedCacheUsage {
24    /// Retained host bytes.
25    pub cpu_bytes: u64,
26    /// Retained device bytes.
27    pub gpu_bytes: u64,
28    /// Highest retained host charge since engine construction.
29    pub peak_cpu_bytes: u64,
30    /// Highest retained device charge since engine construction.
31    pub peak_gpu_bytes: u64,
32}
33
34/// Eviction priority, lowest first: declaration order *is* the order the
35/// ledger evicts in, so the cheapest-to-rebuild classes come first.
36#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
37pub(crate) enum DerivedCacheClass {
38    /// Shared visible sets: recomputed by the next cull pass, so cheapest first.
39    Visibility,
40    /// Packed instance records: a repack of one selection.
41    RecordSet,
42    /// Shared quality hierarchies, rebuilt from the packed records.
43    Acceleration,
44    /// Shared surface fields: rebuilt by re-running the field generation and
45    /// erosion dispatches over the whole grid, which is the most expensive of
46    /// the five, so they evict last among the non-timeline classes.
47    SurfaceField,
48    TimelineMaterialization,
49}
50
51/// Collision-free identity for one recomputable resource owner.
52#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
53pub(crate) enum DerivedCacheKey {
54    #[cfg(test)]
55    Test(u64),
56    /// One shared packed record set, named by the key that produced it.
57    RecordSet(crate::scene_gpu::RecordKey),
58    /// One shared visible set.
59    Visibility(crate::scene_gpu::VisibilityKey),
60    /// One shared surface field.
61    SurfaceField(crate::scene_gpu::SurfaceFieldKey),
62    SceneAttribute(molgfx_core::AttributeHandle),
63    ScenePoint(molgfx_core::PointBatchHandle),
64    SceneInstance(molgfx_core::InstanceBatchHandle),
65    PagedAttribute(molgfx_core::ResidencyTicket),
66    PagedInstance(molgfx_core::ChunkOccurrenceId),
67}
68
69impl From<crate::scene_gpu::RecordKey> for DerivedCacheKey {
70    fn from(value: crate::scene_gpu::RecordKey) -> Self {
71        Self::RecordSet(value)
72    }
73}
74
75impl From<crate::scene_gpu::VisibilityKey> for DerivedCacheKey {
76    fn from(value: crate::scene_gpu::VisibilityKey) -> Self {
77        Self::Visibility(value)
78    }
79}
80
81#[cfg(test)]
82impl From<u64> for DerivedCacheKey {
83    fn from(value: u64) -> Self {
84        Self::Test(value)
85    }
86}
87
88/// Whether repeated consumers should share one derived GPU result.
89#[derive(Clone, Copy, PartialEq, Eq, Debug)]
90pub(crate) enum MaterializationPlan {
91    /// Both frames are sampled by each consumer without a third allocation.
92    Direct,
93    /// One interpolated result is retained and shared by all consumers.
94    Materialized,
95}
96
97#[derive(Clone, Copy, PartialEq, Eq, Debug)]
98pub(crate) struct DerivedFootprint {
99    pub(crate) cpu_bytes: u64,
100    pub(crate) gpu_bytes: u64,
101}
102
103#[derive(Clone, Copy, PartialEq, Eq, Debug)]
104pub(super) struct Entry {
105    pub(super) key: DerivedCacheKey,
106    pub(super) class: DerivedCacheClass,
107    pub(super) footprint: DerivedFootprint,
108    pub(super) last_used_frame: u64,
109}