Skip to main content

cvkg_render_gpu/subsystems/
config.rs

1//! P1-1 (phase 6): RendererConfig -- the renderer's tunable parameters.
2//!
3//! Extracted from types.rs to give the configuration its own
4//! module. The struct and its impl blocks were previously defined
5//! at the top of cvkg-render-gpu/src/renderer.rs.
6
7use std::num::NonZeroUsize;
8
9/// Configurable parameters for the GpuRenderer.
10///
11/// The 5220-line GpuRenderer monolith hardcoded six LRU cache
12/// sizes plus the Mega-Heim atlas dimensions. This struct
13/// extracts those tunables so they can be adjusted at runtime via
14/// `GpuRenderer::set_config()` (e.g., to use a low_vram preset
15/// after detecting a device with limited VRAM).
16#[derive(Debug, Clone)]
17pub struct RendererConfig {
18    /// Capacity of the text glyph cache (keyed by glyph hash).
19    pub text_cache_capacity: NonZeroUsize,
20    /// Capacity of the SVG tessellated model cache.
21    pub svg_cache_capacity: NonZeroUsize,
22    /// Capacity of the parsed usvg::Tree cache.
23    pub svg_trees_capacity: NonZeroUsize,
24    /// Capacity of the shared element (Lottie) cache.
25    pub shared_elements_capacity: NonZeroUsize,
26    /// Capacity of the image UV registry.
27    pub image_uv_capacity: NonZeroUsize,
28    /// Capacity of the texture name -> handle registry.
29    pub texture_registry_capacity: NonZeroUsize,
30    /// Width of the Mega-Heim shared texture atlas.
31    pub mega_heim_width: u32,
32    /// Height of the Mega-Heim shared texture atlas.
33    pub mega_heim_height: u32,
34}
35
36impl Default for RendererConfig {
37    fn default() -> Self {
38        // Defaults match the previously hardcoded values so
39        // behavior is preserved.
40        Self {
41            text_cache_capacity: NonZeroUsize::new(8192).unwrap(),
42            svg_cache_capacity: NonZeroUsize::new(512).unwrap(),
43            svg_trees_capacity: NonZeroUsize::new(512).unwrap(),
44            shared_elements_capacity: NonZeroUsize::new(1024).unwrap(),
45            image_uv_capacity: NonZeroUsize::new(256).unwrap(),
46            texture_registry_capacity: NonZeroUsize::new(31).unwrap(),
47            mega_heim_width: 4096,
48            mega_heim_height: 4096,
49        }
50    }
51}
52
53impl RendererConfig {
54    /// Low-VRAM preset: smaller caches and atlas for mobile GPUs.
55    pub fn low_vram() -> Self {
56        Self {
57            text_cache_capacity: NonZeroUsize::new(2048).unwrap(),
58            svg_cache_capacity: NonZeroUsize::new(128).unwrap(),
59            svg_trees_capacity: NonZeroUsize::new(128).unwrap(),
60            shared_elements_capacity: NonZeroUsize::new(256).unwrap(),
61            image_uv_capacity: NonZeroUsize::new(64).unwrap(),
62            texture_registry_capacity: NonZeroUsize::new(15).unwrap(),
63            mega_heim_width: 2048,
64            mega_heim_height: 2048,
65        }
66    }
67
68    /// High-end preset: larger caches and atlas for desktop GPUs.
69    pub fn high_end() -> Self {
70        Self {
71            text_cache_capacity: NonZeroUsize::new(16384).unwrap(),
72            svg_cache_capacity: NonZeroUsize::new(1024).unwrap(),
73            svg_trees_capacity: NonZeroUsize::new(1024).unwrap(),
74            shared_elements_capacity: NonZeroUsize::new(4096).unwrap(),
75            image_uv_capacity: NonZeroUsize::new(1024).unwrap(),
76            texture_registry_capacity: NonZeroUsize::new(127).unwrap(),
77            mega_heim_width: 8192,
78            mega_heim_height: 8192,
79        }
80    }
81
82    /// Total VRAM cost of the Mega-Heim texture in bytes
83    /// (RGBA8 = 4 bytes/pixel).
84    pub fn mega_heim_vram_bytes(&self) -> u64 {
85        self.mega_heim_width as u64 * self.mega_heim_height as u64 * 4
86    }
87}