Merge pull request #2428 from iced-rs/feature/present-mode-env-var

Introduce `ICED_PRESENT_MODE` env var to pick a `wgpu::PresentMode`
This commit is contained in:
Héctor Ramón 2024-05-07 21:22:29 +02:00 committed by GitHub
commit 18b2f8845e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 10 deletions

View file

@ -51,3 +51,29 @@ impl From<graphics::Settings> for Settings {
} }
} }
} }
/// Obtains a [`wgpu::PresentMode`] from the current environment
/// configuration, if set.
///
/// The value returned by this function can be changed by setting
/// the `ICED_PRESENT_MODE` env variable. The possible values are:
///
/// - `vsync` → [`wgpu::PresentMode::AutoVsync`]
/// - `no_vsync` → [`wgpu::PresentMode::AutoNoVsync`]
/// - `immediate` → [`wgpu::PresentMode::Immediate`]
/// - `fifo` → [`wgpu::PresentMode::Fifo`]
/// - `fifo_relaxed` → [`wgpu::PresentMode::FifoRelaxed`]
/// - `mailbox` → [`wgpu::PresentMode::Mailbox`]
pub fn present_mode_from_env() -> Option<wgpu::PresentMode> {
let present_mode = std::env::var("ICED_PRESENT_MODE").ok()?;
match present_mode.to_lowercase().as_str() {
"vsync" => Some(wgpu::PresentMode::AutoVsync),
"no_vsync" => Some(wgpu::PresentMode::AutoNoVsync),
"immediate" => Some(wgpu::PresentMode::Immediate),
"fifo" => Some(wgpu::PresentMode::Fifo),
"fifo_relaxed" => Some(wgpu::PresentMode::FifoRelaxed),
"mailbox" => Some(wgpu::PresentMode::Mailbox),
_ => None,
}
}

View file

@ -4,7 +4,8 @@ use crate::graphics::color;
use crate::graphics::compositor; use crate::graphics::compositor;
use crate::graphics::error; use crate::graphics::error;
use crate::graphics::{self, Viewport}; use crate::graphics::{self, Viewport};
use crate::{Engine, Renderer, Settings}; use crate::settings::{self, Settings};
use crate::{Engine, Renderer};
/// A window graphics backend for iced powered by `wgpu`. /// A window graphics backend for iced powered by `wgpu`.
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
@ -270,15 +271,19 @@ impl graphics::Compositor for Compositor {
backend: Option<&str>, backend: Option<&str>,
) -> Result<Self, graphics::Error> { ) -> Result<Self, graphics::Error> {
match backend { match backend {
None | Some("wgpu") => Ok(new( None | Some("wgpu") => {
Settings { let mut settings = Settings::from(settings);
backends: wgpu::util::backend_bits_from_env()
.unwrap_or(wgpu::Backends::all()), if let Some(backends) = wgpu::util::backend_bits_from_env() {
..settings.into() settings.backends = backends;
}, }
compatible_window,
) if let Some(present_mode) = settings::present_mode_from_env() {
.await?), settings.present_mode = present_mode;
}
Ok(new(settings, compatible_window).await?)
}
Some(backend) => Err(graphics::Error::GraphicsAdapterNotFound { Some(backend) => Err(graphics::Error::GraphicsAdapterNotFound {
backend: "wgpu", backend: "wgpu",
reason: error::Reason::DidNotMatch { reason: error::Reason::DidNotMatch {