Skip to main content

euv_core/game/engine3d/quality/
impl.rs

1use super::*;
2
3/// Default `High` AAA quality configuration with BVH ray-traced shadows and DRS enabled.
4impl Default for RenderQualityConfig {
5    /// Builds the default `High` runtime graphics profile.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - High quality graphics configuration.
10    fn default() -> Self {
11        Self::from_preset(QualityPreset::High)
12    }
13}
14
15/// Resolution dimension helpers for `ResolutionPreset`.
16impl ResolutionPreset {
17    /// Returns nominal `(width, height)` pixel dimensions for this target tier.
18    ///
19    /// # Arguments
20    ///
21    /// - `&Self` - Resolution tier.
22    ///
23    /// # Returns
24    ///
25    /// - `(u32, u32)` - Target output dimensions.
26    #[must_use]
27    pub const fn dimensions(&self) -> (u32, u32) {
28        match self {
29            Self::Hd720 => (1280, 720),
30            Self::Fhd1080 => (1920, 1080),
31            Self::Qhd1440 => (2560, 1440),
32            Self::Uhd4k => (3840, 2160),
33        }
34    }
35}
36
37/// Preset builders and live dynamic resolution scaling for `RenderQualityConfig`.
38impl RenderQualityConfig {
39    /// Builds a complete runtime graphics configuration tuned for `preset`.
40    ///
41    /// # Arguments
42    ///
43    /// - `QualityPreset` - Desired master quality tier.
44    ///
45    /// # Returns
46    ///
47    /// - `Self` - Tuned runtime configuration.
48    #[must_use]
49    pub fn from_preset(preset: QualityPreset) -> Self {
50        match preset {
51            QualityPreset::Low => Self {
52                preset,
53                resolution_preset: ResolutionPreset::Hd720,
54                resolution_scale: 0.55,
55                dynamic_resolution: true,
56                target_fps: 120.0,
57                ray_tracing: RayTracingMode::Off,
58                reflection_bounces: 0,
59                upscaler: UpscalerMode::SpatialFsr,
60                anti_aliasing: AntiAliasingMode::Off,
61                draw_distance: 140.0,
62                lod_bias: 1.5,
63                frustum_culling: true,
64                chunk_streaming: true,
65                volumetric_fog: false,
66                gpu_instancing: true,
67            },
68            QualityPreset::Medium => Self {
69                preset,
70                resolution_preset: ResolutionPreset::Fhd1080,
71                resolution_scale: 0.72,
72                dynamic_resolution: true,
73                target_fps: 60.0,
74                ray_tracing: RayTracingMode::ShadowsOnly,
75                reflection_bounces: 0,
76                upscaler: UpscalerMode::SpatialFsr,
77                anti_aliasing: AntiAliasingMode::Fxaa,
78                draw_distance: 220.0,
79                lod_bias: 1.15,
80                frustum_culling: true,
81                chunk_streaming: true,
82                volumetric_fog: true,
83                gpu_instancing: true,
84            },
85            QualityPreset::High => Self {
86                preset,
87                resolution_preset: ResolutionPreset::Fhd1080,
88                resolution_scale: 0.95,
89                dynamic_resolution: true,
90                target_fps: 60.0,
91                ray_tracing: RayTracingMode::ShadowsAndReflections,
92                reflection_bounces: 1,
93                upscaler: UpscalerMode::SpatialFsr,
94                anti_aliasing: AntiAliasingMode::Fxaa,
95                draw_distance: 320.0,
96                lod_bias: 1.0,
97                frustum_culling: true,
98                chunk_streaming: true,
99                volumetric_fog: true,
100                gpu_instancing: true,
101            },
102            QualityPreset::Ultra => Self {
103                preset,
104                resolution_preset: ResolutionPreset::Qhd1440,
105                resolution_scale: 1.0,
106                dynamic_resolution: false,
107                target_fps: 60.0,
108                ray_tracing: RayTracingMode::ShadowsAndReflections,
109                reflection_bounces: 2,
110                upscaler: UpscalerMode::TemporalTaa,
111                anti_aliasing: AntiAliasingMode::Taa,
112                draw_distance: 420.0,
113                lod_bias: 0.8,
114                frustum_culling: true,
115                chunk_streaming: true,
116                volumetric_fog: true,
117                gpu_instancing: true,
118            },
119            QualityPreset::Cinematic => Self {
120                preset,
121                resolution_preset: ResolutionPreset::Uhd4k,
122                resolution_scale: 1.0,
123                dynamic_resolution: false,
124                target_fps: 60.0,
125                ray_tracing: RayTracingMode::FullPathTracedGi,
126                reflection_bounces: 2,
127                upscaler: UpscalerMode::Native,
128                anti_aliasing: AntiAliasingMode::Taa,
129                draw_distance: 520.0,
130                lod_bias: 0.6,
131                frustum_culling: true,
132                chunk_streaming: true,
133                volumetric_fog: true,
134                gpu_instancing: true,
135            },
136        }
137    }
138
139    /// Builds a quality preset tuned with Dynamic Resolution Scaling targeting `target_hz` (e.g. `60.0`, `120.0`, `240.0`, `360.0`, `480.0`).
140    ///
141    /// # Arguments
142    ///
143    /// - `QualityPreset` - Base master quality tier.
144    /// - `f32` - Desired display refresh target in Hz (clamped to `30.0..=1000.0`).
145    ///
146    /// # Returns
147    ///
148    /// - `Self` - Quality configuration with DRS enabled for `target_hz`.
149    #[must_use]
150    pub fn for_target_hz(preset: QualityPreset, target_hz: f32) -> Self {
151        Self::from_preset(preset).with_drs_target_hz(target_hz)
152    }
153
154    /// Enables Dynamic Resolution Scaling targeting `target_hz` (`240.0`/`360.0`/`480.0` Hz supported up to `1000.0` Hz).
155    ///
156    /// # Arguments
157    ///
158    /// - `Self` - Base configuration.
159    /// - `f32` - Target refresh rate in Hz.
160    ///
161    /// # Returns
162    ///
163    /// - `Self` - Updated configuration with DRS enabled and target FPS configured.
164    #[must_use]
165    pub fn with_drs_target_hz(mut self, target_hz: f32) -> Self {
166        let clamped_hz: f32 = target_hz.clamp(30.0, 1000.0);
167        self.set_dynamic_resolution(true);
168        self.set_target_fps(clamped_hz);
169        self
170    }
171
172    /// Dynamically adapts `resolution_scale` based on measured `frame_ms` when DRS is enabled.
173    ///
174    /// # Arguments
175    ///
176    /// - `&Self` - Mutable configuration.
177    /// - `f32` - Measured render duration of the previous frame in milliseconds.
178    pub fn adjust_dynamic_resolution(&mut self, frame_ms: f32) {
179        if !self.get_dynamic_resolution() {
180            return;
181        }
182        let target: f32 = self.get_target_fps().clamp(30.0, 1000.0);
183        let budget_ms: f32 = 1000.0 / target;
184        let cur: f32 = self.get_resolution_scale();
185        let (down_step, up_step): (f32, f32) = if target >= 240.0 {
186            (0.05, 0.015)
187        } else {
188            (0.04, 0.02)
189        };
190        if frame_ms > budget_ms * 1.08 {
191            self.set_resolution_scale((cur - down_step).clamp(0.40, 1.25));
192        } else if frame_ms < budget_ms * 0.72 {
193            self.set_resolution_scale((cur + up_step).clamp(0.40, 1.25));
194        }
195    }
196}