From 96799cd291ddc6f43a2d2eb58a574cd5fc0dcd27 Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Tue, 30 Jan 2024 22:15:30 +0100 Subject: [PATCH 1/4] Replace `Instance::any_backend_feature_enabled` with `Instance::enabled_backend_features` which reports all available backends instead of just reporting if none is available. --- wgpu/src/lib.rs | 66 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 6becc7cde5..8ddcddf0ee 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1748,39 +1748,63 @@ impl Default for Instance { /// # Panics /// /// If no backend feature for the active target platform is enabled, - /// this method will panic, see [`Instance::any_backend_feature_enabled()`]. + /// this method will panic, see [`Instance::enabled_backend_features()`]. fn default() -> Self { Self::new(InstanceDescriptor::default()) } } impl Instance { - /// Returns `true` if any backend feature is enabled for the current build configuration. + /// Returns which backends can be picked for the current build configuration. + /// + /// The returned set depends on a combination of target platform and enabled features. + /// This does *not* do any runtime checks and is exclusively based on compile time information. /// - /// Which feature makes this method return true depends on the target platform: - /// * MacOS/iOS: `metal`, `vulkan-portability` or `angle` - /// * Wasm32: `webgpu`, `webgl` or Emscripten target. - /// * All other: Always returns true + /// `InstanceDescriptor::backends` does not need to be a subset of this, + /// but any backend that is not in this set, will not be picked. /// - /// TODO: Right now it's otherwise not possible yet to opt-out of all features on most platforms. + /// TODO: Right now it's otherwise not possible yet to opt-out of all features on some platforms. /// See /// * Windows: always enables Vulkan and GLES with no way to opt out /// * Linux: always enables Vulkan and GLES with no way to opt out - pub const fn any_backend_feature_enabled() -> bool { - // Method intentionally kept verbose to keep it a bit easier to follow! - - // On macOS and iOS, at least one of Metal, Vulkan or GLES backend must be enabled. - let is_mac_or_ios = cfg!(target_os = "macos") || cfg!(target_os = "ios"); - if is_mac_or_ios { - cfg!(feature = "metal") - || cfg!(feature = "vulkan-portability") - || 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!(target_os = "emscripten") + pub const fn enabled_backend_features() -> Backends { + let mut backends = Backends::empty(); + + if cfg!(native) { + if cfg!(metal) { + backends = backends.union(Backends::METAL); + } + if cfg!(dx12) { + backends = backends.union(Backends::DX12); + } + + // Windows & Linux currently always enable Vulkan and OpenGL. + // See + if cfg!(target_os = "windows") || cfg!(target_os = "linux") { + backends = backends.union(Backends::VULKAN).union(Backends::GL); + } + + // Vulkan on Mac/iOS is only available through vulkan-portability. + if (cfg!(target_os = "ios") || cfg!(target_os = "macos")) + && cfg!(feature = "vulkan-portability") + { + backends = backends.union(Backends::VULKAN); + } + + // GL Vulkan on Mac is only available through angle. + if cfg!(target_os = "macos") && cfg!(feature = "angle") { + backends = backends.union(Backends::VULKAN); + } } else { - true + if cfg!(webgpu) { + backends = backends.union(Backends::BROWSER_WEBGPU); + } + if cfg!(webgl) { + backends = backends.union(Backends::GL); + } } + + backends } /// Create an new instance of wgpu. @@ -1805,7 +1829,7 @@ impl Instance { /// this method will panic, see [`Instance::any_backend_feature_enabled()`]. #[allow(unreachable_code)] pub fn new(_instance_desc: InstanceDescriptor) -> Self { - if !Self::any_backend_feature_enabled() { + if Self::enabled_backend_features().is_empty() { panic!( "No wgpu backend feature that is implemented for the target platform was enabled. \ See `wgpu::Instance::any_backend_feature_enabled()` for more information." From 684168099449a09d957b09188f7f33058981575d Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Tue, 30 Jan 2024 22:22:05 +0100 Subject: [PATCH 2/4] add changelog entry --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58a01f23eb..3f30c69247 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -89,6 +89,11 @@ Bottom level categories: - Eager release of GPU resources comes from device.trackers. By @bradwerth in [#5075](https://github.com/gfx-rs/wgpu/pull/5075) - `wgpu-types`'s `trace` and `replay` features have been replaced by the `serde` feature. By @KirmesBude in [#5149](https://github.com/gfx-rs/wgpu/pull/5149) - `wgpu-core`'s `serial-pass` feature has been removed. Use `serde` instead. By @KirmesBude in [#5149](https://github.com/gfx-rs/wgpu/pull/5149) +- `wgpu::Instance` can now report which `wgpu::Backends` are available based on the build configuration. By @wumpf [#5167](https://github.com/gfx-rs/wgpu/pull/5167) +```diff +-wgpu::Instance::any_backend_feature_enabled() ++!wgpu::Instance::enabled_backend_features().is_empty() +``` ### Bug Fixes From 02577ce44c12315fe4175b55e9feeb6d372b5ae0 Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Tue, 30 Jan 2024 22:24:40 +0100 Subject: [PATCH 3/4] update enabled_backend_features in doc --- wgpu/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 8ddcddf0ee..28cbf4b823 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1826,13 +1826,13 @@ impl Instance { /// # Panics /// /// If no backend feature for the active target platform is enabled, - /// this method will panic, see [`Instance::any_backend_feature_enabled()`]. + /// this method will panic, see [`Instance::enabled_backend_features()`]. #[allow(unreachable_code)] pub fn new(_instance_desc: InstanceDescriptor) -> Self { if Self::enabled_backend_features().is_empty() { panic!( "No wgpu backend feature that is implemented for the target platform was enabled. \ - See `wgpu::Instance::any_backend_feature_enabled()` for more information." + See `wgpu::Instance::enabled_backend_features()` for more information." ); } @@ -1858,7 +1858,7 @@ impl Instance { } unreachable!( - "Earlier check of `any_backend_feature_enabled` should have prevented getting here!" + "Earlier check of `enabled_backend_features` should have prevented getting here!" ); } From 90c1096090530bd04d2a9a17665db27a7c0dc539 Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Wed, 31 Jan 2024 09:59:33 +0100 Subject: [PATCH 4/4] fix not enabling any backend on android, fix related doc issues --- wgpu/Cargo.toml | 2 +- wgpu/src/lib.rs | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index a0c1926713..2c2b8af2e0 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -29,7 +29,7 @@ default = ["wgsl", "dx12", "metal", "webgpu"] #! ### Backends # -------------------------------------------------------------------- #! ⚠️ WIP: Not all backends can be manually configured today. -#! On Windows & Linux the Vulkan & GLES backends are always enabled. +#! On Windows, Linux & Android the Vulkan & GLES backends are always enabled. #! See [#3514](https://github.com/gfx-rs/wgpu/issues/3514) for more details. ## Enables the DX12 backend on Windows. diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 28cbf4b823..296693ee64 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1765,8 +1765,7 @@ impl Instance { /// /// TODO: Right now it's otherwise not possible yet to opt-out of all features on some platforms. /// See - /// * Windows: always enables Vulkan and GLES with no way to opt out - /// * Linux: always enables Vulkan and GLES with no way to opt out + /// * Windows/Linux/Android: always enables Vulkan and GLES with no way to opt out pub const fn enabled_backend_features() -> Backends { let mut backends = Backends::empty(); @@ -1778,9 +1777,9 @@ impl Instance { backends = backends.union(Backends::DX12); } - // Windows & Linux currently always enable Vulkan and OpenGL. + // Windows, Android, Linux currently always enable Vulkan and OpenGL. // See - if cfg!(target_os = "windows") || cfg!(target_os = "linux") { + if cfg!(target_os = "windows") || cfg!(unix) { backends = backends.union(Backends::VULKAN).union(Backends::GL); }