Skip to content

Commit dabb2d3

Browse files
committed
Introduce internal_backend to iced_wgpu::Settings
1 parent 7d2a998 commit dabb2d3

4 files changed

Lines changed: 52 additions & 41 deletions

File tree

glow/src/settings.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,12 @@ impl Default for Settings {
2929
}
3030
}
3131
}
32+
33+
impl Settings {
34+
/// Creates new [`Settings`] using environment configuration.
35+
///
36+
/// Currently, this is equivalent to calling [`Settings::default`].
37+
pub fn from_env() -> Self {
38+
Self::default()
39+
}
40+
}

src/application.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub trait Application: Sized {
206206
} else {
207207
None
208208
},
209-
..crate::renderer::Settings::default()
209+
..crate::renderer::Settings::from_env()
210210
};
211211

212212
Ok(crate::runtime::application::run::<

wgpu/src/settings.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ pub struct Settings {
1616
/// [`Backend`]: crate::Backend
1717
pub present_mode: wgpu::PresentMode,
1818

19+
/// The internal graphics backend to use.
20+
pub internal_backend: wgpu::BackendBit,
21+
1922
/// The bytes of the font that will be used by default.
2023
///
2124
/// If `None` is provided, a default system font will be chosen.
@@ -30,14 +33,52 @@ pub struct Settings {
3033
pub antialiasing: Option<Antialiasing>,
3134
}
3235

36+
impl Settings {
37+
/// Creates new [`Settings`] using environment configuration.
38+
///
39+
/// Specifically:
40+
///
41+
/// - The `internal_backend` can be configured using the `WGPU_BACKEND`
42+
/// environment variable. If the variable is not set, the primary backend
43+
/// will be used. The following values are allowed:
44+
/// - `vulkan`
45+
/// - `metal`
46+
/// - `dx12`
47+
/// - `dx11`
48+
/// - `gl`
49+
/// - `webgpu`
50+
pub fn from_env() -> Self {
51+
Settings {
52+
internal_backend: backend_from_env()
53+
.unwrap_or(wgpu::BackendBit::PRIMARY),
54+
..Self::default()
55+
}
56+
}
57+
}
58+
3359
impl Default for Settings {
3460
fn default() -> Settings {
3561
Settings {
3662
format: wgpu::TextureFormat::Bgra8UnormSrgb,
3763
present_mode: wgpu::PresentMode::Mailbox,
64+
internal_backend: wgpu::BackendBit::PRIMARY,
3865
default_font: None,
3966
default_text_size: 20,
4067
antialiasing: None,
4168
}
4269
}
4370
}
71+
72+
fn backend_from_env() -> Option<wgpu::BackendBit> {
73+
std::env::var("WGPU_BACKEND").ok().map(|backend| {
74+
match backend.to_lowercase().as_str() {
75+
"vulkan" => wgpu::BackendBit::VULKAN,
76+
"metal" => wgpu::BackendBit::METAL,
77+
"dx12" => wgpu::BackendBit::DX12,
78+
"dx11" => wgpu::BackendBit::DX11,
79+
"gl" => wgpu::BackendBit::GL,
80+
"webgpu" => wgpu::BackendBit::BROWSER_WEBGPU,
81+
other => panic!("Unknown backend: {}", other),
82+
}
83+
})
84+
}

wgpu/src/window/compositor.rs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,14 @@ pub struct Compositor {
1515
local_pool: futures::executor::LocalPool,
1616
}
1717

18-
#[derive(Clone)]
19-
pub enum WgpuBackend {
20-
Auto,
21-
Vulkan,
22-
Metal,
23-
Dx12,
24-
Dx11,
25-
Gl,
26-
BrowserWgpu,
27-
}
28-
29-
impl WgpuBackend {
30-
fn from_env() -> Self {
31-
if let Ok(backend) = std::env::var("WGPU_BACKEND") {
32-
match backend.to_lowercase().as_str() {
33-
"vulkan" => WgpuBackend::Vulkan,
34-
"metal" => WgpuBackend::Metal,
35-
"dx12" => WgpuBackend::Dx12,
36-
"dx11" => WgpuBackend::Dx11,
37-
"gl" => WgpuBackend::Gl,
38-
"webgpu" => WgpuBackend::BrowserWgpu,
39-
other => panic!("Unknown backend: {}", other),
40-
}
41-
} else {
42-
WgpuBackend::Auto
43-
}
44-
}
45-
}
46-
4718
impl Compositor {
4819
const CHUNK_SIZE: u64 = 10 * 1024;
4920

5021
/// Requests a new [`Compositor`] with the given [`Settings`].
5122
///
5223
/// Returns `None` if no compatible graphics adapter could be found.
5324
pub async fn request(settings: Settings) -> Option<Self> {
54-
let backend = match WgpuBackend::from_env() {
55-
WgpuBackend::Auto => wgpu::BackendBit::PRIMARY,
56-
WgpuBackend::Vulkan => wgpu::BackendBit::VULKAN,
57-
WgpuBackend::Metal => wgpu::BackendBit::METAL,
58-
WgpuBackend::Dx12 => wgpu::BackendBit::DX12,
59-
WgpuBackend::Dx11 => wgpu::BackendBit::DX11,
60-
WgpuBackend::Gl => wgpu::BackendBit::GL,
61-
WgpuBackend::BrowserWgpu => wgpu::BackendBit::BROWSER_WEBGPU,
62-
};
63-
64-
let instance = wgpu::Instance::new(backend);
25+
let instance = wgpu::Instance::new(settings.internal_backend);
6526

6627
let adapter = instance
6728
.request_adapter(&wgpu::RequestAdapterOptions {

0 commit comments

Comments
 (0)