Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ by if the `Feature::MULTI_DRAW_INDIRECT_COUNT` feature is available on the devic

By @cwfitzgerald in [#8162](https://github.com/gfx-rs/wgpu/pull/8162).

#### enumerate_adapters was made async

Making `enumerate_adapters` async allows custom backends to use it along with elimnating some native/non-native distinctions

This is a breaking change

```diff
- pub fn enumerate_adapters(&self, backends: Backends) -> Vec<Adapter> {
+ pub fn enumerate_adapters(&self, backends: Backends) -> impl Future<Output = Vec<Adapter>> {

```

By @R-Cramer4 in [#8230](https://github.com/gfx-rs/wgpu/pull/8230)

#### `wgpu::PollType::Wait` has now an optional timeout

Expand Down
13 changes: 7 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/features/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ pub(crate) async fn get_adapter_with_capabilities_or_from_env(
);
adapter
} else {
let adapters = instance.enumerate_adapters(Backends::all());
let adapters = instance.enumerate_adapters(Backends::all()).await;

let mut chosen_adapter = None;
for adapter in adapters {
Expand Down
7 changes: 7 additions & 0 deletions examples/standalone/custom_backend/src/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ impl InstanceInterface for CustomInstance {
fn wgsl_language_features(&self) -> wgpu::WgslLanguageFeatures {
unimplemented!()
}

fn enumerate_adapters(
&self,
_backends: wgpu::Backends,
) -> Pin<Box<dyn wgpu::custom::EnumerateAdapterFuture>> {
unimplemented!()
}
}

#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions tests/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub async fn initialize_adapter(

cfg_if::cfg_if! {
if #[cfg(not(target_arch = "wasm32"))] {
let adapter_iter = instance.enumerate_adapters(backends);
let adapter_iter = instance.enumerate_adapters(backends).await;
let adapter = adapter_iter.into_iter()
// If we have a report, we only want to match the adapter with the same info.
//
Expand All @@ -136,7 +136,7 @@ pub async fn initialize_adapter(
panic!(
"Could not find adapter with info {:#?} in {:#?}",
adapter_report.map(|r| &r.info),
instance.enumerate_adapters(backends).into_iter().map(|a| a.get_info()).collect::<Vec<_>>(),
instance.enumerate_adapters(backends).await.into_iter().map(|a| a.get_info()).collect::<Vec<_>>(),
);
};
} else {
Expand Down
1 change: 1 addition & 0 deletions wgpu-info/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ bitflags.workspace = true
env_logger.workspace = true
hashbrown = { workspace = true, features = ["serde"] }
pico-args.workspace = true
pollster.workspace = true
serde = { workspace = true, features = ["default"] }
serde_json.workspace = true
wgpu.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion wgpu-info/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ impl GpuReport {
desc.flags = wgpu::InstanceFlags::debugging();
desc.with_env()
});
let adapters = instance.enumerate_adapters(wgpu::Backends::all());

let adapters = pollster::block_on(instance.enumerate_adapters(wgpu::Backends::all()));

let mut devices = Vec::with_capacity(adapters.len());
for adapter in adapters {
Expand Down
30 changes: 12 additions & 18 deletions wgpu/src/api/instance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[cfg(wgpu_core)]
use alloc::vec::Vec;
use core::future::Future;

Expand Down Expand Up @@ -142,23 +141,18 @@ impl Instance {
/// # Arguments
///
/// - `backends` - Backends from which to enumerate adapters.
#[cfg(wgpu_core)]
pub fn enumerate_adapters(&self, backends: Backends) -> Vec<Adapter> {
let Some(core_instance) = self.inner.as_core_opt() else {
return Vec::new();
};

core_instance
.enumerate_adapters(backends)
.into_iter()
.map(|adapter| {
let core = backend::wgpu_core::CoreAdapter {
context: core_instance.clone(),
id: adapter,
};
crate::Adapter { inner: core.into() }
})
.collect()
pub fn enumerate_adapters(&self, backends: Backends) -> impl Future<Output = Vec<Adapter>> {
let future = self.inner.enumerate_adapters(backends);

async move {
future
.await
.iter()
.map(|adapter| Adapter {
inner: adapter.clone(),
})
.collect()
}
}

/// Retrieves an [`Adapter`] which matches the given [`RequestAdapterOptions`].
Expand Down
14 changes: 14 additions & 0 deletions wgpu/src/backend/webgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,20 @@ impl dispatch::InstanceInterface for ContextWebGpu {
))))
}
}
fn enumerate_adapters(
&self,
_backends: crate::Backends,
) -> Pin<Box<dyn dispatch::EnumerateAdapterFuture>> {
let future = self.request_adapter(&crate::RequestAdapterOptions::default());
let enumerate_future = async move {
let adapter = future.await;
match adapter {
Ok(a) => vec![a],
Err(_) => vec![],
}
};
Box::pin(enumerate_future)
}

fn poll_all_devices(&self, _force_wait: bool) -> bool {
// Devices are automatically polled.
Expand Down
20 changes: 19 additions & 1 deletion wgpu/src/backend/wgpu_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ use wgt::{
WasmNotSendSync,
};

use crate::util::Mutex;
use crate::{
api,
dispatch::{self, BlasCompactCallback, BufferMappedRangeInterface},
BindingResource, Blas, BufferBinding, BufferDescriptor, CompilationInfo, CompilationMessage,
CompilationMessageType, ErrorSource, Features, Label, LoadOp, MapMode, Operations,
ShaderSource, SurfaceTargetUnsafe, TextureDescriptor, Tlas,
};
use crate::{dispatch::DispatchAdapter, util::Mutex};

#[derive(Clone)]
pub struct ContextWgpuCore(Arc<wgc::global::Global>);
Expand Down Expand Up @@ -902,6 +902,24 @@ impl dispatch::InstanceInterface for ContextWgpuCore {
},
)
}

fn enumerate_adapters(
&self,
backends: crate::Backends,
) -> Pin<Box<dyn dispatch::EnumerateAdapterFuture>> {
let adapters: Vec<DispatchAdapter> = self
.enumerate_adapters(backends)
.into_iter()
.map(|adapter| {
let core = crate::backend::wgpu_core::CoreAdapter {
context: self.clone(),
id: adapter,
};
core.into()
})
.collect();
Box::pin(ready(adapters))
}
}

impl dispatch::AdapterInterface for CoreAdapter {
Expand Down
4 changes: 4 additions & 0 deletions wgpu/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ trait_alias!(RequestAdapterFuture: Future<Output = Result<DispatchAdapter, wgt::
trait_alias!(RequestDeviceFuture: Future<Output = Result<(DispatchDevice, DispatchQueue), crate::RequestDeviceError>> + WasmNotSend + 'static);
trait_alias!(PopErrorScopeFuture: Future<Output = Option<crate::Error>> + WasmNotSend + 'static);
trait_alias!(ShaderCompilationInfoFuture: Future<Output = crate::CompilationInfo> + WasmNotSend + 'static);
trait_alias!(EnumerateAdapterFuture: Future<Output = Vec<DispatchAdapter>> + WasmNotSend + 'static);

// We can't use trait aliases here, as you can't convert from a dyn Trait to dyn Supertrait _yet_.
#[cfg(send_sync)]
Expand Down Expand Up @@ -93,6 +94,9 @@ pub trait InstanceInterface: CommonTraits {

#[cfg(feature = "wgsl")]
fn wgsl_language_features(&self) -> crate::WgslLanguageFeatures;

fn enumerate_adapters(&self, backends: crate::Backends)
-> Pin<Box<dyn EnumerateAdapterFuture>>;
}

pub trait AdapterInterface: CommonTraits {
Expand Down
8 changes: 4 additions & 4 deletions wgpu/src/util/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::Backends;
/// Initialize the adapter obeying the `WGPU_ADAPTER_NAME` environment variable.
#[cfg(wgpu_core)]
#[cfg_attr(not(std), expect(unused_variables, unreachable_code))]
pub fn initialize_adapter_from_env(
pub async fn initialize_adapter_from_env(
instance: &Instance,
compatible_surface: Option<&Surface<'_>>,
) -> Result<Adapter, wgt::RequestAdapterError> {
Expand All @@ -23,7 +23,7 @@ pub fn initialize_adapter_from_env(
}
};

let adapters = instance.enumerate_adapters(crate::Backends::all());
let adapters = instance.enumerate_adapters(crate::Backends::all()).await;

let mut chosen_adapter = None;
for adapter in adapters {
Expand All @@ -46,7 +46,7 @@ pub fn initialize_adapter_from_env(

/// Initialize the adapter obeying the `WGPU_ADAPTER_NAME` environment variable.
#[cfg(not(wgpu_core))]
pub fn initialize_adapter_from_env(
pub async fn initialize_adapter_from_env(
_instance: &Instance,
_compatible_surface: Option<&Surface<'_>>,
) -> Result<Adapter, wgt::RequestAdapterError> {
Expand All @@ -58,7 +58,7 @@ pub async fn initialize_adapter_from_env_or_default(
instance: &Instance,
compatible_surface: Option<&Surface<'_>>,
) -> Result<Adapter, wgt::RequestAdapterError> {
match initialize_adapter_from_env(instance, compatible_surface) {
match initialize_adapter_from_env(instance, compatible_surface).await {
Ok(a) => Ok(a),
Err(_) => {
instance
Expand Down
Loading