Skip to content

Commit f98bd60

Browse files
authored
Update enabled_backend_features to take improved backend feature flags into account (#7195)
1 parent a26171b commit f98bd60

File tree

2 files changed

+51
-40
lines changed

2 files changed

+51
-40
lines changed

wgpu/build.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,45 @@
11
fn main() {
22
cfg_aliases::cfg_aliases! {
33
native: { not(target_arch = "wasm32") },
4-
webgl: { all(target_arch = "wasm32", not(target_os = "emscripten"), feature = "webgl") },
5-
webgpu: { all(target_arch = "wasm32", not(target_os = "emscripten"), feature = "webgpu") },
64
Emscripten: { all(target_arch = "wasm32", target_os = "emscripten") },
7-
wgpu_core: { any(native, webgl, Emscripten) },
5+
86
send_sync: { any(
9-
not(target_arch = "wasm32"),
7+
native,
108
all(feature = "fragile-send-sync-non-atomic-wasm", not(target_feature = "atomics"))
119
) },
10+
11+
// Backends - keep this in sync with `wgpu-core/Cargo.toml` & docs in `wgpu/Cargo.toml`
12+
webgpu: { all(not(native), not(Emscripten), feature = "webgpu") },
13+
webgl: { all(not(native), not(Emscripten), feature = "webgl") },
1214
dx12: { all(target_os = "windows", feature = "dx12") },
1315
metal: { all(target_vendor = "apple", feature = "metal") },
16+
vulkan: { any(
17+
// The `vulkan` feature enables the Vulkan backend only on "native Vulkan" platforms, i.e. Windows/Linux/Android
18+
all(any(windows, target_os = "linux", target_os = "android"), feature = "vulkan"),
19+
// On Apple platforms, however, we require the `vulkan-portability` feature
20+
// to explicitly opt-in to Vulkan since it's meant to be used with MoltenVK.
21+
all(target_vendor = "apple", feature = "vulkan-portability")
22+
) },
23+
gles: { any(
24+
// The `gles` feature enables the OpenGL/GLES backend only on "native OpenGL" platforms, i.e. Windows, Linux, Android, and Emscripten.
25+
// (Note that WebGL is also not included here!)
26+
all(any(windows, target_os = "linux", target_os = "android", Emscripten), feature = "gles"),
27+
// On Apple platforms, however, we require the `angle` feature to explicitly opt-in to OpenGL
28+
// since its meant to be used with ANGLE.
29+
all(target_vendor = "apple", feature = "angle")
30+
) },
31+
noop: { feature = "noop" },
32+
33+
wgpu_core: {
34+
any(
35+
// On native, wgpu_core is currently always enabled, even if there's no backend enabled at all.
36+
native,
37+
// `wgpu_core` is implied if any backend other than WebGPU is enabled.
38+
// (this is redundant except for `gles` and `noop`)
39+
webgl, dx12, metal, vulkan, gles, noop
40+
)
41+
},
42+
1443
// This alias is _only_ if _we_ need naga in the wrapper. wgpu-core provides
1544
// its own re-export of naga, which can be used in other situations
1645
naga: { any(feature = "naga-ir", feature = "spirv", feature = "glsl") },

wgpu/src/api/instance.rs

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -62,45 +62,27 @@ impl Instance {
6262
///
6363
/// `InstanceDescriptor::backends` does not need to be a subset of this,
6464
/// but any backend that is not in this set, will not be picked.
65-
///
66-
/// TODO: Right now it's otherwise not possible yet to opt-out of all features on some platforms.
67-
/// See <https://github.com/gfx-rs/wgpu/issues/3514>
68-
/// * Windows/Linux/Android: always enables Vulkan and GLES with no way to opt out
6965
pub const fn enabled_backend_features() -> Backends {
7066
let mut backends = Backends::empty();
71-
72-
if cfg!(native) {
73-
if cfg!(metal) {
74-
backends = backends.union(Backends::METAL);
75-
}
76-
if cfg!(dx12) {
77-
backends = backends.union(Backends::DX12);
78-
}
79-
80-
// Windows, Android, Linux currently always enable Vulkan and OpenGL.
81-
// See <https://github.com/gfx-rs/wgpu/issues/3514>
82-
if cfg!(target_os = "windows") || cfg!(unix) {
83-
backends = backends.union(Backends::VULKAN).union(Backends::GL);
84-
}
85-
86-
// Vulkan on Mac/iOS is only available through vulkan-portability.
87-
if cfg!(target_vendor = "apple") && cfg!(feature = "vulkan-portability") {
88-
backends = backends.union(Backends::VULKAN);
89-
}
90-
91-
// GL on Mac is only available through angle.
92-
if cfg!(target_os = "macos") && cfg!(feature = "angle") {
93-
backends = backends.union(Backends::GL);
94-
}
95-
} else {
96-
if cfg!(webgpu) {
97-
backends = backends.union(Backends::BROWSER_WEBGPU);
98-
}
99-
if cfg!(webgl) {
100-
backends = backends.union(Backends::GL);
101-
}
67+
// `.set` and `|=` don't work in a `const` context.
68+
if cfg!(noop) {
69+
backends = backends.union(Backends::NOOP);
70+
}
71+
if cfg!(vulkan) {
72+
backends = backends.union(Backends::VULKAN);
73+
}
74+
if cfg!(any(gles, webgl)) {
75+
backends = backends.union(Backends::GL);
76+
}
77+
if cfg!(metal) {
78+
backends = backends.union(Backends::METAL);
79+
}
80+
if cfg!(dx12) {
81+
backends = backends.union(Backends::DX12);
82+
}
83+
if cfg!(webgpu) {
84+
backends = backends.union(Backends::BROWSER_WEBGPU);
10285
}
103-
10486
backends
10587
}
10688

0 commit comments

Comments
 (0)