Skip to main content

molgfx_render/engine/
brick_atlas.rs

1//! Caller-driven sparse brick delivery without renderer-owned I/O.
2
3use super::Engine;
4use crate::{BrickAtlasConfig, BrickAtlasError, BrickAtlasMetrics, BrickAtlasUpload};
5use molgfx_core::{BrickCatalog, BrickId};
6use molgfx_gpu::{Device, FenceValue};
7
8impl<D: Device> Engine<D> {
9    /// Allocates a bounded atlas for one logical sparse field.
10    ///
11    /// # Errors
12    ///
13    /// Returns typed catalog, capacity or GPU limit failures.
14    pub fn install_brick_atlas(
15        &mut self,
16        catalog: &BrickCatalog,
17        config: BrickAtlasConfig,
18    ) -> Result<usize, BrickAtlasError> {
19        self.scene_gpu
20            .install_brick_atlas(&self.device, &self.queue, catalog, config)
21    }
22
23    /// Delivers one borrowed provider payload to persistent staging.
24    ///
25    /// # Errors
26    ///
27    /// Returns typed atlas index, generation or upload backpressure.
28    pub fn stage_brick(
29        &mut self,
30        atlas: usize,
31        upload: BrickAtlasUpload<'_>,
32    ) -> Result<FenceValue, BrickAtlasError> {
33        let capacity = self.scene_gpu.brick_atlas_count();
34        let Some(target) = self.scene_gpu.brick_atlas_mut(atlas) else {
35            return Err(BrickAtlasError::LifecycleCapacity { capacity });
36        };
37        target.stage(&self.device, &self.queue, upload)
38    }
39
40    /// Invalidates a page and schedules fence-safe slot release.
41    ///
42    /// # Errors
43    ///
44    /// Returns typed atlas index, page or lifecycle failures.
45    pub fn evict_brick(
46        &mut self,
47        atlas: usize,
48        brick: BrickId,
49    ) -> Result<FenceValue, BrickAtlasError> {
50        let capacity = self.scene_gpu.brick_atlas_count();
51        let Some(target) = self.scene_gpu.brick_atlas_mut(atlas) else {
52            return Err(BrickAtlasError::LifecycleCapacity { capacity });
53        };
54        target.request_eviction(&self.device, &self.queue, brick)
55    }
56
57    /// Current bounded memory and lifecycle counters for one atlas.
58    #[must_use]
59    pub fn brick_atlas_metrics(&mut self, atlas: usize) -> Option<BrickAtlasMetrics> {
60        self.scene_gpu
61            .brick_atlas_mut(atlas)
62            .map(|target| target.metrics())
63    }
64}