1use crate::{Adapter, Instance, RequestAdapterOptions, Surface};
23#[cfg(doc)]
4use crate::Backends;
56/// Initialize the adapter obeying the `WGPU_ADAPTER_NAME` environment variable.
7#[cfg(wgpu_core)]
8#[cfg_attr(not(std), expect(unused_variables, unreachable_code))]
9pub fn initialize_adapter_from_env(
10 instance: &Instance,
11 compatible_surface: Option<&Surface<'_>>,
12) -> Result<Adapter, wgt::RequestAdapterError> {
13let desired_adapter_name: alloc::string::String = {
14cfg_if::cfg_if! {
15if #[cfg(std)] {
16 std::env::var("WGPU_ADAPTER_NAME")
17 .as_deref()
18 .map(str::to_lowercase)
19 .map_err(|_| wgt::RequestAdapterError::EnvNotSet)?
20} else {
21return Err(wgt::RequestAdapterError::EnvNotSet)
22 }
23 }
24 };
2526let adapters = instance.enumerate_adapters(crate::Backends::all());
2728let mut chosen_adapter = None;
29for adapter in adapters {
30let info = adapter.get_info();
3132if let Some(surface) = compatible_surface {
33if !adapter.is_surface_supported(surface) {
34continue;
35 }
36 }
3738if info.name.to_lowercase().contains(&desired_adapter_name) {
39 chosen_adapter = Some(adapter);
40break;
41 }
42 }
4344Ok(chosen_adapter.expect("WGPU_ADAPTER_NAME set but no matching adapter found!"))
45}
4647/// Initialize the adapter obeying the `WGPU_ADAPTER_NAME` environment variable.
48#[cfg(not(wgpu_core))]
49pub fn initialize_adapter_from_env(
50 _instance: &Instance,
51 _compatible_surface: Option<&Surface<'_>>,
52) -> Result<Adapter, wgt::RequestAdapterError> {
53Err(wgt::RequestAdapterError::EnvNotSet)
54}
5556/// Initialize the adapter obeying the `WGPU_ADAPTER_NAME` environment variable and if it doesn't exist fall back on a default adapter.
57pub async fn initialize_adapter_from_env_or_default(
58 instance: &Instance,
59 compatible_surface: Option<&Surface<'_>>,
60) -> Result<Adapter, wgt::RequestAdapterError> {
61match initialize_adapter_from_env(instance, compatible_surface) {
62Ok(a) => Ok(a),
63Err(_) => {
64 instance
65 .request_adapter(&RequestAdapterOptions {
66 power_preference: crate::PowerPreference::from_env().unwrap_or_default(),
67 force_fallback_adapter: false,
68 compatible_surface,
69 })
70 .await
71}
72 }
73}
7475/// Determines whether the [`Backends::BROWSER_WEBGPU`] backend is supported.
76///
77/// The result can only be true if this is called from the main thread or a dedicated worker.
78/// For convenience, this is also supported on non-wasm targets, always returning false there.
79pub async fn is_browser_webgpu_supported() -> bool {
80#[cfg(webgpu)]
81{
82// In theory it should be enough to check for the presence of the `gpu` property...
83let gpu = crate::backend::get_browser_gpu_property();
84let Ok(Some(gpu)) = gpu else {
85return false;
86 };
8788// ...but in practice, we also have to try to create an adapter, since as of writing
89 // Chrome on Linux has the `gpu` property but doesn't support WebGPU.
90let adapter_promise = gpu.request_adapter();
91 wasm_bindgen_futures::JsFuture::from(adapter_promise)
92 .await
93.is_ok_and(|adapter| !adapter.is_undefined() && !adapter.is_null())
94 }
95#[cfg(not(webgpu))]
96{
97false
98}
99}
100101/// Create an new instance of wgpu, but disabling [`Backends::BROWSER_WEBGPU`] if no WebGPU support was detected.
102///
103/// If the instance descriptor enables [`Backends::BROWSER_WEBGPU`],
104/// this checks via [`is_browser_webgpu_supported`] for WebGPU support before forwarding
105/// the descriptor with or without [`Backends::BROWSER_WEBGPU`] respecitively to [`Instance::new`].
106///
107/// You should prefer this method over [`Instance::new`] if you want to target WebGPU and automatically
108/// fall back to WebGL if WebGPU is not available.
109/// This is because WebGPU support has to be decided upon instance creation and [`Instance::new`]
110/// (being a `sync` function) can't establish WebGPU support (details see [`is_browser_webgpu_supported`]).
111///
112/// # Panics
113///
114/// If no backend feature for the active target platform is enabled,
115/// this method will panic, see [`Instance::enabled_backend_features()`].
116pub async fn new_instance_with_webgpu_detection(
117 instance_desc: &wgt::InstanceDescriptor,
118) -> crate::Instance {
119let mut instance_desc = instance_desc.clone();
120if instance_desc
121 .backends
122 .contains(wgt::Backends::BROWSER_WEBGPU)
123 && !is_browser_webgpu_supported().await
124{
125 instance_desc.backends.remove(wgt::Backends::BROWSER_WEBGPU);
126 }
127128crate::Instance::new(&instance_desc)
129}