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            _ => return None,
34        },
35    )
36}
37
38/// Initialize the adapter obeying the WGPU_ADAPTER_NAME environment variable.
39#[cfg(not(target_arch = "wasm32"))]
40pub fn initialize_adapter_from_env(
41    instance: &Instance,
42    compatible_surface: Option<&Surface>,
43) -> Option<Adapter> {
44    let desired_adapter_name = std::env::var("WGPU_ADAPTER_NAME")
45        .as_deref()
46        .map(str::to_lowercase)
47        .ok()?;
48
49    let adapters = instance.enumerate_adapters(Backends::all());
50
51    let mut chosen_adapter = None;
52    for adapter in adapters {
53        let info = adapter.get_info();
54
55        if let Some(surface) = compatible_surface {
56            if !adapter.is_surface_supported(surface) {
57                continue;
58            }
59        }
60
61        if info.name.to_lowercase().contains(&desired_adapter_name) {
62            chosen_adapter = Some(adapter);
63            break;
64        }
65    }
66
67    Some(chosen_adapter.expect("WGPU_ADAPTER_NAME set but no matching adapter found!"))
68}
69
70/// Initialize the adapter obeying the WGPU_ADAPTER_NAME environment variable.
71#[cfg(target_arch = "wasm32")]
72pub fn initialize_adapter_from_env(
73    _instance: &Instance,
74    _compatible_surface: Option<&Surface>,
75) -> Option<Adapter> {
76    None
77}
78
79/// Initialize the adapter obeying the WGPU_ADAPTER_NAME environment variable and if it doesn't exist fall back on a default adapter.
80pub async fn initialize_adapter_from_env_or_default(
81    instance: &Instance,
82    compatible_surface: Option<&Surface>,
83) -> Option<Adapter> {
84    match initialize_adapter_from_env(instance, compatible_surface) {
85        Some(a) => Some(a),
86        None => {
87            instance
88                .request_adapter(&RequestAdapterOptions {
89                    power_preference: power_preference_from_env().unwrap_or_default(),
90                    force_fallback_adapter: false,
91                    compatible_surface,
92                })
93                .await
94        }
95    }
96}
97
98/// Choose which DX12 shader compiler to use from the environment variable `WGPU_DX12_COMPILER`.
99///
100/// Possible values are `dxc` and `fxc`. Case insensitive.
101pub fn dx12_shader_compiler_from_env() -> Option<wgt::Dx12Compiler> {
102    Some(
103        match std::env::var("WGPU_DX12_COMPILER")
104            .as_deref()
105            .map(str::to_lowercase)
106            .as_deref()
107        {
108            Ok("dxc") => wgt::Dx12Compiler::Dxc {
109                dxil_path: None,
110                dxc_path: None,
111            },
112            Ok("fxc") => wgt::Dx12Compiler::Fxc,
113            _ => return None,
114        },
115    )
116}