Skip to content

Commit fbe005f

Browse files
kpreidnical
authored andcommitted
Make request_adapter() report which backends were tried.
This will help users determine whether the problem is: * a backend is not statically enabled * a backend is not dynamically enabled * no drivers or physical adapters are present for that backend (further distinction would be useful here) * no adapters met the required criteria There are deficiencies in the reporting of WebGPU vs. WebGL support. Those would best be fixed by also fixing the mutual exclusion of those backends.
1 parent a369bfa commit fbe005f

File tree

15 files changed

+381
-142
lines changed

15 files changed

+381
-142
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ By @wumpf in [#7144](https://github.com/gfx-rs/wgpu/pull/7144)
198198
199199
#### General
200200
201+
- `wgpu::Instance::request_adapter()` now returns `Result` instead of `Option`; the error provides information about why no suitable adapter was returned. By @kpreid in [#7330](https://github.com/gfx-rs/wgpu/pull/7330).
201202
- Support BLAS compaction in wgpu-hal. By @Vecvec in [#7101](https://github.com/gfx-rs/wgpu/pull/7101).
202203
- Avoid using default features in many dependencies, etc. By Brody in [#7031](https://github.com/gfx-rs/wgpu/pull/7031)
203204
- Use `hashbrown` to simplify no-std support. By Brody in [#6938](https://github.com/gfx-rs/wgpu/pull/6938) & [#6925](https://github.com/gfx-rs/wgpu/pull/6925).

examples/standalone/03_custom_backend/src/custom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl InstanceInterface for CustomInstance {
4343
&self,
4444
_options: &wgpu::RequestAdapterOptions<'_, '_>,
4545
) -> std::pin::Pin<Box<dyn RequestAdapterFuture>> {
46-
Box::pin(std::future::ready(Some(DispatchAdapter::custom(
46+
Box::pin(std::future::ready(Ok(DispatchAdapter::custom(
4747
CustomAdapter(self.0.clone()),
4848
))))
4949
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
mod request_adapter_error {
2+
fn id(backends: wgpu::Backends) -> wgpu::InstanceDescriptor {
3+
wgpu::InstanceDescriptor {
4+
backends,
5+
flags: wgpu::InstanceFlags::default(),
6+
backend_options: wgpu::BackendOptions::default(),
7+
}
8+
}
9+
10+
fn adapter_error(desc: &wgpu::InstanceDescriptor) -> String {
11+
let instance = wgpu::Instance::new(desc);
12+
pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
13+
power_preference: wgpu::PowerPreference::default(),
14+
force_fallback_adapter: false,
15+
compatible_surface: None,
16+
}))
17+
.unwrap_err()
18+
.to_string()
19+
}
20+
21+
#[test]
22+
fn no_backends_requested() {
23+
assert_eq!(
24+
adapter_error(&id(wgpu::Backends::empty())),
25+
"No suitable graphics adapter found; \
26+
noop not requested, \
27+
vulkan not requested, \
28+
metal not requested, \
29+
dx12 not requested, \
30+
gl not requested, \
31+
webgpu not requested"
32+
);
33+
}
34+
35+
/// This test nominally tests the noop backend, but it also exercises the logic for other backends
36+
/// that fail to return any adapter.
37+
#[test]
38+
fn noop_not_enabled() {
39+
assert_eq!(
40+
adapter_error(&id(wgpu::Backends::NOOP)),
41+
"No suitable graphics adapter found; \
42+
noop not explicitly enabled, \
43+
vulkan not requested, \
44+
metal not requested, \
45+
dx12 not requested, \
46+
gl not requested, \
47+
webgpu not requested"
48+
);
49+
}
50+
51+
#[test]
52+
fn no_compiled_support() {
53+
// Whichever platform we are on, try asking for a backend that definitely will be
54+
// cfged out regardless of feature flags. (Not that these tests run on wasm at all yet.)
55+
56+
#[cfg(target_family = "wasm")]
57+
assert_eq!(
58+
adapter_error(&id(wgpu::Backends::METAL)),
59+
"No suitable graphics adapter found; \
60+
noop not requested, \
61+
vulkan not requested, \
62+
metal support not compiled in, \
63+
dx12 not requested, \
64+
gl not requested, \
65+
webgpu not requested"
66+
);
67+
68+
#[cfg(not(target_family = "wasm"))]
69+
assert_eq!(
70+
adapter_error(&id(wgpu::Backends::BROWSER_WEBGPU)),
71+
"No suitable graphics adapter found; \
72+
noop not requested, \
73+
vulkan not requested, \
74+
metal not requested, \
75+
dx12 not requested, \
76+
gl not requested, \
77+
webgpu support not compiled in"
78+
);
79+
}
80+
}

tests/validation-tests/api/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod binding_arrays;
22
mod buffer;
33
mod buffer_slice;
4+
mod instance;
45
mod texture;

tests/validation-tests/noop.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ fn device_is_not_available_by_default() {
1010
..Default::default()
1111
});
1212

13-
assert_eq!(
14-
pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())),
15-
None,
16-
"noop backend adapter present when it should not be"
17-
);
13+
pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default()))
14+
.expect_err("noop backend adapter present when it should not be");
1815
}
1916

2017
#[test]

wgpu-core/src/global.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use alloc::{borrow::ToOwned as _, boxed::Box, sync::Arc};
2-
use core::{fmt, iter};
1+
use alloc::{borrow::ToOwned as _, sync::Arc};
2+
use core::fmt;
33

44
use crate::{
55
hal_api::HalApi,
@@ -47,13 +47,8 @@ impl Global {
4747
pub unsafe fn from_hal_instance<A: HalApi>(name: &str, hal_instance: A::Instance) -> Self {
4848
profiling::scope!("Global::new");
4949

50-
let dyn_instance: Box<dyn hal::DynInstance> = Box::new(hal_instance);
5150
Self {
52-
instance: Instance {
53-
name: name.to_owned(),
54-
instance_per_backend: iter::once((A::VARIANT, dyn_instance)).collect(),
55-
..Default::default()
56-
},
51+
instance: Instance::from_hal_instance::<A>(name.to_owned(), hal_instance),
5752
surfaces: Registry::new(),
5853
hub: Hub::new(),
5954
}

0 commit comments

Comments
 (0)