Skip to content

Commit

Permalink
Merge pull request #2428 from iced-rs/feature/present-mode-env-var
Browse files Browse the repository at this point in the history
Introduce `ICED_PRESENT_MODE` env var to pick a `wgpu::PresentMode`
  • Loading branch information
hecrj authored May 7, 2024
2 parents db07b9b + 5b6f349 commit 18b2f88
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
26 changes: 26 additions & 0 deletions wgpu/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
25 changes: 15 additions & 10 deletions wgpu/src/window/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::graphics::color;
use crate::graphics::compositor;
use crate::graphics::error;
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`.
#[allow(missing_debug_implementations)]
Expand Down Expand Up @@ -270,15 +271,19 @@ impl graphics::Compositor for Compositor {
backend: Option<&str>,
) -> Result<Self, graphics::Error> {
match backend {
None | Some("wgpu") => Ok(new(
Settings {
backends: wgpu::util::backend_bits_from_env()
.unwrap_or(wgpu::Backends::all()),
..settings.into()
},
compatible_window,
)
.await?),
None | Some("wgpu") => {
let mut settings = Settings::from(settings);

if let Some(backends) = wgpu::util::backend_bits_from_env() {
settings.backends = backends;
}

if let Some(present_mode) = settings::present_mode_from_env() {
settings.present_mode = present_mode;
}

Ok(new(settings, compatible_window).await?)
}
Some(backend) => Err(graphics::Error::GraphicsAdapterNotFound {
backend: "wgpu",
reason: error::Reason::DidNotMatch {
Expand Down

0 comments on commit 18b2f88

Please sign in to comment.