li_wgpu/util/
init.rs

1use wgt::{Backends, PowerPreference, RequestAdapterOptions};
2
3use crate::{Adapter, Instance, Surface};
4
5#[cfg(any(not(target_arch = "wasm32"), feature = "wgc"))]
6pub use wgc::instance::parse_backends_from_comma_list;
7/// Always returns WEBGPU on wasm over webgpu.
8#[cfg(all(target_arch = "wasm32", not(feature = "wgc")))]
9pub fn parse_backends_from_comma_list(_string: &str) -> Backends {
10    Backends::BROWSER_WEBGPU
11}
12
13/// Get a set of backend bits from the environment variable WGPU_BACKEND.
14pub fn backend_bits_from_env() -> Option<Backends> {
15    std::env::var("WGPU_BACKEND")
16        .as_deref()
17        .map(str::to_lowercase)
18        .ok()
19        .as_deref()
20        .map(parse_backends_from_comma_list)
21}
22
23/// Get a power preference from the environment variable WGPU_POWER_PREF
24pub fn power_preference_from_env() -> Option<PowerPreference> {
25    Some(
26        match std::env::var("WGPU_POWER_PREF")
27            .as_deref()
28            .map(str::to_lowercase)
29            .as_deref()
30        {
31            Ok("low") => PowerPreference::LowPower,
32            Ok("high") => PowerPreference::HighPerformance,
33            Ok("none") => PowerPreference::None,
34            _ => return None,
35        },
36    )
37}
38
39/// Initialize the adapter obeying the WGPU_ADAPTER_NAME environment variable.
40#[cfg(not(target_arch = "wasm32"))]
41pub fn initialize_adapter_from_env(
42    instance: &Instance,
43    compatible_surface: Option<&Surface>,
44) -> Option<Adapter> {
45    let desired_adapter_name = std::env::var("WGPU_ADAPTER_NAME")
46        .as_deref()
47        .map(str::to_lowercase)
48        .ok()?;
49
50    let adapters = instance.enumerate_adapters(Backends::all());
51
52    let mut chosen_adapter = None;
53    for adapter in adapters {
54        let info = adapter.get_info();
55
56        if let Some(surface) = compatible_surface {
57            if !adapter.is_surface_supported(surface) {
58                continue;
59            }
60        }
61
62        if info.name.to_lowercase().contains(&desired_adapter_name) {
63            chosen_adapter = Some(adapter);
64            break;
65        }
66    }
67
68    Some(chosen_adapter.expect("WGPU_ADAPTER_NAME set but no matching adapter found!"))
69}
70
71/// Initialize the adapter obeying the WGPU_ADAPTER_NAME environment variable.
72#[cfg(target_arch = "wasm32")]
73pub fn initialize_adapter_from_env(
74    _instance: &Instance,
75    _compatible_surface: Option<&Surface>,
76) -> Option<Adapter> {
77    None
78}
79
80/// Initialize the adapter obeying the WGPU_ADAPTER_NAME environment variable and if it doesn't exist fall back on a default adapter.
81pub async fn initialize_adapter_from_env_or_default(
82    instance: &Instance,
83    compatible_surface: Option<&Surface>,
84) -> Option<Adapter> {
85    match initialize_adapter_from_env(instance, compatible_surface) {
86        Some(a) => Some(a),
87        None => {
88            instance
89                .request_adapter(&RequestAdapterOptions {
90                    power_preference: power_preference_from_env().unwrap_or_default(),
91                    force_fallback_adapter: false,
92                    compatible_surface,
93                })
94                .await
95        }
96    }
97}
98
99/// Choose which DX12 shader compiler to use from the environment variable `WGPU_DX12_COMPILER`.
100///
101/// Possible values are `dxc` and `fxc`. Case insensitive.
102pub fn dx12_shader_compiler_from_env() -> Option<wgt::Dx12Compiler> {
103    Some(
104        match std::env::var("WGPU_DX12_COMPILER")
105            .as_deref()
106            .map(str::to_lowercase)
107            .as_deref()
108        {
109            Ok("dxc") => wgt::Dx12Compiler::Dxc {
110                dxil_path: None,
111                dxc_path: None,
112            },
113            Ok("fxc") => wgt::Dx12Compiler::Fxc,
114            _ => return None,
115        },
116    )
117}
118
119/// Choose which minor OpenGL ES version to use from the environment variable `WGPU_GLES_MINOR_VERSION`.
120///
121/// Possible values are `0`, `1`, `2` or `automatic`. Case insensitive.
122pub fn gles_minor_version_from_env() -> Option<wgt::Gles3MinorVersion> {
123    Some(
124        match std::env::var("WGPU_GLES_MINOR_VERSION")
125            .as_deref()
126            .map(str::to_lowercase)
127            .as_deref()
128        {
129            Ok("automatic") => wgt::Gles3MinorVersion::Automatic,
130            Ok("0") => wgt::Gles3MinorVersion::Version0,
131            Ok("1") => wgt::Gles3MinorVersion::Version1,
132            Ok("2") => wgt::Gles3MinorVersion::Version2,
133            _ => return None,
134        },
135    )
136}