From ba7cb20ceebc3c1becfb45893bb48144bf5ac0dd Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Sun, 14 Jan 2024 19:29:59 +0100 Subject: [PATCH 1/3] Fix cases in which instance creation may not create any context --- wgpu/src/lib.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 5a518d3c8a..e87bafd800 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1718,6 +1718,7 @@ impl Instance { /// /// Which feature makes this method return true depends on the target platform: /// * MacOS/iOS: `metal`, `vulkan-portability` or `angle` + /// * Wasm32: `webgpu`, `webgl` or target emscripten. /// * All other: Always returns true /// /// TODO: Right now it's otherwise not possible yet to opt-out of all features on most platforms. @@ -1735,7 +1736,7 @@ impl Instance { || cfg!(feature = "angle") // On the web, either WebGPU or WebGL must be enabled. } else if cfg!(target_arch = "wasm32") { - cfg!(feature = "webgpu") || cfg!(feature = "webgl") + cfg!(feature = "webgpu") || cfg!(feature = "webgl") || cfg!(target_os = "emscripten") } else { true } @@ -1771,8 +1772,10 @@ impl Instance { } #[cfg(webgpu)] - if _instance_desc.backends.contains(Backends::BROWSER_WEBGPU) - && crate::backend::get_browser_gpu_property().map_or(false, |gpu| !gpu.is_undefined()) + if !cfg!(wgpu_core) // If wgpu-core isn't enabled we can't use anything else. + || (_instance_desc.backends.contains(Backends::BROWSER_WEBGPU) + && crate::backend::get_browser_gpu_property() + .map_or(false, |gpu| !gpu.is_undefined())) { return Self { context: Arc::from(crate::backend::ContextWebGpu::init(_instance_desc)), From ab4f8629ef7b9b07f6752b771497accf29d2d999 Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Mon, 15 Jan 2024 10:34:24 +0100 Subject: [PATCH 2/3] split condition steps on picking webgpu instance --- wgpu/src/lib.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index e87bafd800..722e7c6004 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1772,14 +1772,17 @@ impl Instance { } #[cfg(webgpu)] - if !cfg!(wgpu_core) // If wgpu-core isn't enabled we can't use anything else. - || (_instance_desc.backends.contains(Backends::BROWSER_WEBGPU) - && crate::backend::get_browser_gpu_property() - .map_or(false, |gpu| !gpu.is_undefined())) { - return Self { - context: Arc::from(crate::backend::ContextWebGpu::init(_instance_desc)), - }; + let is_only_available_backend = !cfg!(wgpu_core); + let requested_webgpu = _instance_desc.backends.contains(Backends::BROWSER_WEBGPU); + let support_webgpu = + crate::backend::get_browser_gpu_property().map_or(false, |gpu| !gpu.is_undefined()); + + if is_only_available_backend || (requested_webgpu && support_webgpu) { + return Self { + context: Arc::from(crate::backend::ContextWebGpu::init(_instance_desc)), + }; + } } #[cfg(wgpu_core)] From ce17fc99b94c5218505720b52928eb55e4744b05 Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Mon, 15 Jan 2024 11:38:23 +0100 Subject: [PATCH 3/3] comment formulation fix on backend availability Co-authored-by: daxpedda --- wgpu/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 722e7c6004..4120894b9b 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1718,7 +1718,7 @@ impl Instance { /// /// Which feature makes this method return true depends on the target platform: /// * MacOS/iOS: `metal`, `vulkan-portability` or `angle` - /// * Wasm32: `webgpu`, `webgl` or target emscripten. + /// * Wasm32: `webgpu`, `webgl` or Emscripten target. /// * All other: Always returns true /// /// TODO: Right now it's otherwise not possible yet to opt-out of all features on most platforms.