From 89d70cc932fd0c80d40ae79887dff2fbe6e8d018 Mon Sep 17 00:00:00 2001 From: Isse Date: Sat, 27 Jan 2024 22:57:29 +0100 Subject: [PATCH] cache present modes, add check for present modes in `set_render_mode` and move present mode localization to hud --- voxygen/src/hud/settings_window/video.rs | 11 ++++++-- voxygen/src/render/mod.rs | 11 -------- voxygen/src/render/renderer.rs | 32 ++++++++++++++++-------- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/voxygen/src/hud/settings_window/video.rs b/voxygen/src/hud/settings_window/video.rs index 577b98051a..8ef6de96af 100644 --- a/voxygen/src/hud/settings_window/video.rs +++ b/voxygen/src/hud/settings_window/video.rs @@ -7,7 +7,7 @@ use crate::{ }, render::{ AaMode, BloomConfig, BloomFactor, BloomMode, CloudMode, FluidMode, LightingMode, - ReflectionMode, RenderMode, ShadowMapMode, ShadowMode, UpscaleMode, + PresentMode, ReflectionMode, RenderMode, ShadowMapMode, ShadowMode, UpscaleMode, }, session::settings_change::Graphics as GraphicsChange, settings::{Fps, GraphicsSettings}, @@ -662,7 +662,14 @@ impl<'a> Widget for Video<'a> { let mode_list = self.global_state.window.renderer().present_modes(); let mode_label_list = mode_list .iter() - .map(|mode| self.localized_strings.get_msg(mode.localize())) + .map(|mode| { + self.localized_strings.get_msg(match mode { + PresentMode::Fifo => "hud-settings-present_mode-vsync_capped", + PresentMode::FifoRelaxed => "hud-settings-present_mode-vsync_adaptive", + PresentMode::Mailbox => "hud-settings-present_mode-vsync_uncapped", + PresentMode::Immediate => "hud-settings-present_mode-vsync_off", + }) + }) .collect::>(); // Get which present mode is currently active diff --git a/voxygen/src/render/mod.rs b/voxygen/src/render/mod.rs index 222b7f1dd8..5bbc74ffe1 100644 --- a/voxygen/src/render/mod.rs +++ b/voxygen/src/render/mod.rs @@ -298,17 +298,6 @@ pub enum PresentMode { Fifo, // has to be last for `#[serde(other)]` } -impl PresentMode { - pub fn localize(&self) -> &'static str { - match self { - Self::Fifo => "hud-settings-present_mode-vsync_capped", - Self::FifoRelaxed => "hud-settings-present_mode-vsync_adaptive", - Self::Mailbox => "hud-settings-present_mode-vsync_uncapped", - Self::Immediate => "hud-settings-present_mode-vsync_off", - } - } -} - impl From for wgpu::PresentMode { fn from(mode: PresentMode) -> Self { match mode { diff --git a/voxygen/src/render/renderer.rs b/voxygen/src/render/renderer.rs index 954c4462bf..13ceeafc12 100644 --- a/voxygen/src/render/renderer.rs +++ b/voxygen/src/render/renderer.rs @@ -135,7 +135,6 @@ enum State { pub struct Renderer { device: Arc, queue: wgpu::Queue, - adapter: wgpu::Adapter, surface: wgpu::Surface, surface_config: wgpu::SurfaceConfiguration, @@ -185,6 +184,8 @@ pub struct Renderer { /// The texture format used for the intermediate rendering passes intermediate_format: wgpu::TextureFormat, + + present_modes: Vec, } impl Renderer { @@ -562,10 +563,16 @@ impl Renderer { #[cfg(feature = "egui-ui")] let egui_renderpass = egui_wgpu_backend::RenderPass::new(&device, format, 1); + let present_modes = surface + .get_capabilities(&adapter) + .present_modes + .into_iter() + .filter_map(|present_mode| PresentMode::try_from(present_mode).ok()) + .collect(); + Ok(Self { device, queue, - adapter, surface, surface_config, @@ -606,6 +613,7 @@ impl Renderer { graphics_backend, intermediate_format, + present_modes, }) } @@ -642,7 +650,16 @@ impl Renderer { self.other_modes = other_modes; // Update present mode in swap chain descriptor - self.surface_config.present_mode = self.other_modes.present_mode.into(); + self.surface_config.present_mode = + if self.present_modes.contains(&self.other_modes.present_mode) { + self.other_modes.present_mode + } else { + *self + .present_modes + .first() + .expect("There should never be no supported present modes") + } + .into(); // Only enable profiling if the wgpu features are enabled self.other_modes.profiler_enabled &= self.profiler_features_enabled; @@ -687,14 +704,7 @@ impl Renderer { pub fn pipeline_modes(&self) -> &PipelineModes { &self.pipeline_modes } /// Get the supported present modes. - pub fn present_modes(&self) -> Vec { - self.surface - .get_capabilities(&self.adapter) - .present_modes - .into_iter() - .filter_map(|present_mode| PresentMode::try_from(present_mode).ok()) - .collect() - } + pub fn present_modes(&self) -> &[PresentMode] { &self.present_modes } /// Get the current profiling times /// Nested timings immediately follow their parent