Skip to main content

euv_core/game/engine3d/quality/
impl.rs

1use super::*;
2
3/// Default `Ultra` max-quality lossless 1:1 native-resolution configuration.
4impl Default for RenderQualityConfig {
5    /// Builds the default `Ultra` runtime graphics profile at native 1:1 resolution without silent downscaling.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - Ultra max-quality lossless graphics configuration.
10    fn default() -> Self {
11        Self::from_preset(QualityPreset::Ultra)
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                occlusion_culling: true,
68                virtual_shadow_maps: false,
69                clustered_lighting: true,
70                lumen_radiance_cache: false,
71            },
72            QualityPreset::Medium => Self {
73                preset,
74                resolution_preset: ResolutionPreset::Fhd1080,
75                resolution_scale: 0.72,
76                dynamic_resolution: true,
77                target_fps: 60.0,
78                ray_tracing: RayTracingMode::ShadowsOnly,
79                reflection_bounces: 0,
80                upscaler: UpscalerMode::Tsr,
81                anti_aliasing: AntiAliasingMode::Fxaa,
82                draw_distance: 220.0,
83                lod_bias: 1.15,
84                frustum_culling: true,
85                chunk_streaming: true,
86                volumetric_fog: true,
87                gpu_instancing: true,
88                occlusion_culling: true,
89                virtual_shadow_maps: true,
90                clustered_lighting: true,
91                lumen_radiance_cache: false,
92            },
93            QualityPreset::High => Self {
94                preset,
95                resolution_preset: ResolutionPreset::Fhd1080,
96                resolution_scale: 1.0,
97                dynamic_resolution: true,
98                target_fps: 60.0,
99                ray_tracing: RayTracingMode::ShadowsAndReflections,
100                reflection_bounces: 1,
101                upscaler: UpscalerMode::Tsr,
102                anti_aliasing: AntiAliasingMode::Fxaa,
103                draw_distance: 320.0,
104                lod_bias: 1.0,
105                frustum_culling: true,
106                chunk_streaming: true,
107                volumetric_fog: true,
108                gpu_instancing: true,
109                occlusion_culling: true,
110                virtual_shadow_maps: true,
111                clustered_lighting: true,
112                lumen_radiance_cache: false,
113            },
114            QualityPreset::Ultra => Self {
115                preset,
116                resolution_preset: ResolutionPreset::Uhd4k,
117                resolution_scale: 1.0,
118                dynamic_resolution: false,
119                target_fps: 480.0,
120                ray_tracing: RayTracingMode::ShadowsAndReflections,
121                reflection_bounces: 3,
122                upscaler: UpscalerMode::Native,
123                anti_aliasing: AntiAliasingMode::Tsr,
124                draw_distance: 640.0,
125                lod_bias: 0.6,
126                frustum_culling: true,
127                chunk_streaming: true,
128                volumetric_fog: true,
129                gpu_instancing: true,
130                occlusion_culling: true,
131                virtual_shadow_maps: true,
132                clustered_lighting: true,
133                lumen_radiance_cache: true,
134            },
135            QualityPreset::Cinematic => Self {
136                preset,
137                resolution_preset: ResolutionPreset::Uhd4k,
138                resolution_scale: 1.0,
139                dynamic_resolution: false,
140                target_fps: 480.0,
141                ray_tracing: RayTracingMode::FullPathTracedGi,
142                reflection_bounces: 3,
143                upscaler: UpscalerMode::Native,
144                anti_aliasing: AntiAliasingMode::Taa,
145                draw_distance: 640.0,
146                lod_bias: 0.5,
147                frustum_culling: true,
148                chunk_streaming: true,
149                volumetric_fog: true,
150                gpu_instancing: true,
151                occlusion_culling: true,
152                virtual_shadow_maps: true,
153                clustered_lighting: true,
154                lumen_radiance_cache: true,
155            },
156        }
157    }
158
159    /// Builds a quality preset tuned with Dynamic Resolution Scaling targeting `target_hz` (e.g. `60.0`, `120.0`, `240.0`, `360.0`, `480.0`).
160    ///
161    /// # Arguments
162    ///
163    /// - `QualityPreset` - Base master quality tier.
164    /// - `f32` - Desired display refresh target in Hz (clamped to `30.0..=1000.0`).
165    ///
166    /// # Returns
167    ///
168    /// - `Self` - Quality configuration with DRS enabled for `target_hz`.
169    #[must_use]
170    pub fn for_target_hz(preset: QualityPreset, target_hz: f32) -> Self {
171        Self::from_preset(preset).with_drs_target_hz(target_hz)
172    }
173
174    /// Enables Dynamic Resolution Scaling targeting `target_hz` (`240.0`/`360.0`/`480.0` Hz supported up to `1000.0` Hz).
175    ///
176    /// # Arguments
177    ///
178    /// - `Self` - Base configuration.
179    /// - `f32` - Target refresh rate in Hz.
180    ///
181    /// # Returns
182    ///
183    /// - `Self` - Updated configuration with DRS enabled and target FPS configured.
184    #[must_use]
185    pub fn with_drs_target_hz(mut self, target_hz: f32) -> Self {
186        let clamped_hz: f32 = if target_hz.is_finite() {
187            target_hz.max(30.0)
188        } else {
189            60.0
190        };
191        self.set_dynamic_resolution(true);
192        self.set_target_fps(clamped_hz);
193        self
194    }
195
196    /// Dynamically adapts `resolution_scale` based on measured `frame_ms` when DRS is enabled.
197    ///
198    /// # Arguments
199    ///
200    /// - `&Self` - Mutable configuration.
201    /// - `f32` - Measured render duration of the previous frame in milliseconds.
202    pub fn adjust_dynamic_resolution(&mut self, frame_ms: f32) {
203        if !self.get_dynamic_resolution() {
204            return;
205        }
206        let target: f32 = self.get_target_fps().max(30.0);
207        let budget_ms: f32 = 1000.0 / target;
208        let cur: f32 = self.get_resolution_scale();
209        let (down_step, up_step): (f32, f32) = if target >= 240.0 {
210            (0.05, 0.015)
211        } else {
212            (0.04, 0.02)
213        };
214        if frame_ms > budget_ms * 1.08 {
215            self.set_resolution_scale((cur - down_step).clamp(0.40, 1.25));
216        } else if frame_ms < budget_ms * 0.72 {
217            self.set_resolution_scale((cur + up_step).clamp(0.40, 1.25));
218        }
219    }
220}