Skip to content

Commit 62cc81c

Browse files
authored
Allow wgpu::Instance to report which backends were enabled (#5167)
* Replace `Instance::any_backend_feature_enabled` with `Instance::enabled_backend_features` which reports all available backends instead of just reporting if none is available. * add changelog entry * update enabled_backend_features in doc * fix not enabling any backend on android, fix related doc issues
1 parent 4595708 commit 62cc81c

File tree

3 files changed

+55
-28
lines changed

3 files changed

+55
-28
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ Bottom level categories:
9494
- As with other instance flags, this flag can be changed in calls to `InstanceFlags::with_env` with the new `WGPU_GPU_BASED_VALIDATION` environment variable.
9595

9696
By @ErichDonGubler in [#5046](https://github.com/gfx-rs/wgpu/pull/5046).
97-
97+
- `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)
98+
```diff
99+
-wgpu::Instance::any_backend_feature_enabled()
100+
+!wgpu::Instance::enabled_backend_features().is_empty()
101+
```
98102

99103
### Bug Fixes
100104

wgpu/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ default = ["wgsl", "dx12", "metal", "webgpu"]
2929
#! ### Backends
3030
# --------------------------------------------------------------------
3131
#! ⚠️ WIP: Not all backends can be manually configured today.
32-
#! On Windows & Linux the Vulkan & GLES backends are always enabled.
32+
#! On Windows, Linux & Android the Vulkan & GLES backends are always enabled.
3333
#! See [#3514](https://github.com/gfx-rs/wgpu/issues/3514) for more details.
3434

3535
## Enables the DX12 backend on Windows.

wgpu/src/lib.rs

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,39 +1748,62 @@ impl Default for Instance {
17481748
/// # Panics
17491749
///
17501750
/// If no backend feature for the active target platform is enabled,
1751-
/// this method will panic, see [`Instance::any_backend_feature_enabled()`].
1751+
/// this method will panic, see [`Instance::enabled_backend_features()`].
17521752
fn default() -> Self {
17531753
Self::new(InstanceDescriptor::default())
17541754
}
17551755
}
17561756

17571757
impl Instance {
1758-
/// Returns `true` if any backend feature is enabled for the current build configuration.
1758+
/// Returns which backends can be picked for the current build configuration.
17591759
///
1760-
/// Which feature makes this method return true depends on the target platform:
1761-
/// * MacOS/iOS: `metal`, `vulkan-portability` or `angle`
1762-
/// * Wasm32: `webgpu`, `webgl` or Emscripten target.
1763-
/// * All other: Always returns true
1760+
/// The returned set depends on a combination of target platform and enabled features.
1761+
/// This does *not* do any runtime checks and is exclusively based on compile time information.
17641762
///
1765-
/// TODO: Right now it's otherwise not possible yet to opt-out of all features on most platforms.
1763+
/// `InstanceDescriptor::backends` does not need to be a subset of this,
1764+
/// but any backend that is not in this set, will not be picked.
1765+
///
1766+
/// TODO: Right now it's otherwise not possible yet to opt-out of all features on some platforms.
17661767
/// See <https://github.com/gfx-rs/wgpu/issues/3514>
1767-
/// * Windows: always enables Vulkan and GLES with no way to opt out
1768-
/// * Linux: always enables Vulkan and GLES with no way to opt out
1769-
pub const fn any_backend_feature_enabled() -> bool {
1770-
// Method intentionally kept verbose to keep it a bit easier to follow!
1771-
1772-
// On macOS and iOS, at least one of Metal, Vulkan or GLES backend must be enabled.
1773-
let is_mac_or_ios = cfg!(target_os = "macos") || cfg!(target_os = "ios");
1774-
if is_mac_or_ios {
1775-
cfg!(feature = "metal")
1776-
|| cfg!(feature = "vulkan-portability")
1777-
|| cfg!(feature = "angle")
1778-
// On the web, either WebGPU or WebGL must be enabled.
1779-
} else if cfg!(target_arch = "wasm32") {
1780-
cfg!(feature = "webgpu") || cfg!(feature = "webgl") || cfg!(target_os = "emscripten")
1768+
/// * Windows/Linux/Android: always enables Vulkan and GLES with no way to opt out
1769+
pub const fn enabled_backend_features() -> Backends {
1770+
let mut backends = Backends::empty();
1771+
1772+
if cfg!(native) {
1773+
if cfg!(metal) {
1774+
backends = backends.union(Backends::METAL);
1775+
}
1776+
if cfg!(dx12) {
1777+
backends = backends.union(Backends::DX12);
1778+
}
1779+
1780+
// Windows, Android, Linux currently always enable Vulkan and OpenGL.
1781+
// See <https://github.com/gfx-rs/wgpu/issues/3514>
1782+
if cfg!(target_os = "windows") || cfg!(unix) {
1783+
backends = backends.union(Backends::VULKAN).union(Backends::GL);
1784+
}
1785+
1786+
// Vulkan on Mac/iOS is only available through vulkan-portability.
1787+
if (cfg!(target_os = "ios") || cfg!(target_os = "macos"))
1788+
&& cfg!(feature = "vulkan-portability")
1789+
{
1790+
backends = backends.union(Backends::VULKAN);
1791+
}
1792+
1793+
// GL Vulkan on Mac is only available through angle.
1794+
if cfg!(target_os = "macos") && cfg!(feature = "angle") {
1795+
backends = backends.union(Backends::VULKAN);
1796+
}
17811797
} else {
1782-
true
1798+
if cfg!(webgpu) {
1799+
backends = backends.union(Backends::BROWSER_WEBGPU);
1800+
}
1801+
if cfg!(webgl) {
1802+
backends = backends.union(Backends::GL);
1803+
}
17831804
}
1805+
1806+
backends
17841807
}
17851808

17861809
/// Create an new instance of wgpu.
@@ -1802,13 +1825,13 @@ impl Instance {
18021825
/// # Panics
18031826
///
18041827
/// If no backend feature for the active target platform is enabled,
1805-
/// this method will panic, see [`Instance::any_backend_feature_enabled()`].
1828+
/// this method will panic, see [`Instance::enabled_backend_features()`].
18061829
#[allow(unreachable_code)]
18071830
pub fn new(_instance_desc: InstanceDescriptor) -> Self {
1808-
if !Self::any_backend_feature_enabled() {
1831+
if Self::enabled_backend_features().is_empty() {
18091832
panic!(
18101833
"No wgpu backend feature that is implemented for the target platform was enabled. \
1811-
See `wgpu::Instance::any_backend_feature_enabled()` for more information."
1834+
See `wgpu::Instance::enabled_backend_features()` for more information."
18121835
);
18131836
}
18141837

@@ -1834,7 +1857,7 @@ impl Instance {
18341857
}
18351858

18361859
unreachable!(
1837-
"Earlier check of `any_backend_feature_enabled` should have prevented getting here!"
1860+
"Earlier check of `enabled_backend_features` should have prevented getting here!"
18381861
);
18391862
}
18401863

0 commit comments

Comments
 (0)