diff --git a/CHANGELOG.md b/CHANGELOG.md index 512c96504..25bad6f05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -166,6 +166,7 @@ By @sagudev in [#10109](https://github.com/gfx-rs/wgpu/pull/10109). - `wgpu::Error::Validation::description` from `Device::create_shader_module` no longer include the shader source text and detailed compiler messages, per the WebGPU specification. These details remain accessible via `ShaderModule::get_compilation_info`. By @beicause and @sagudev in [#10173](https://github.com/gfx-rs/wgpu/pull/10173). - `BufferUsages` is now composed of `BufferUsagesWebGPU` and `BufferUsagesWGPU`, similarly to `Features`. In most cases this is not expected to be a breaking change, but changes might be required for converting `BufferUsages` to/from `u32`. By @sagudev in [#10195](https://github.com/gfx-rs/wgpu/pull/10195). - `ShaderStages` is now composed of `ShaderStagesWebGPU` and `ShaderStagesWGPU`, similarly to `Features`. In most cases this is not expected to be a breaking change, but changes might be required for converting `ShaderStages` to/from `u32`. By @sagudev in [#10313](https://github.com/gfx-rs/wgpu/pull/10313). +- `request_adapter` will return error on `InstanceFlags::STRICT_WEBGPU_COMPLIANCE` if the adapter has already been consumed by a previous `request_device`. By @sagudev in [#10444](https://github.com/gfx-rs/wgpu/pull/10444). #### naga diff --git a/tests/tests/wgpu-gpu/device.rs b/tests/tests/wgpu-gpu/device.rs index 4cd5b60e5..666d6b506 100644 --- a/tests/tests/wgpu-gpu/device.rs +++ b/tests/tests/wgpu-gpu/device.rs @@ -1,5 +1,6 @@ use std::sync::atomic::AtomicBool; +use wgpu::InstanceFlags; use wgpu_test::{ apply, gpu_test, FailureCase, GpuTestConfiguration, GpuTestInitializer, TestParameters, TestingContext, @@ -52,6 +53,34 @@ static CROSS_DEVICE_BIND_GROUP_USAGE: GpuTestConfiguration = GpuTestConfiguratio ctx.async_poll(wgpu::PollType::Poll).await.unwrap(); }); +#[apply(gpu_test!)] +static SINGLE_DEVICES: GpuTestConfiguration = GpuTestConfiguration::new() + .parameters( + TestParameters::default() + .instance_flags(InstanceFlags::STRICT_WEBGPU_COMPLIANCE) + .enable_noop(), + ) + .run_sync(|ctx| { + use pollster::FutureExt as _; + ctx.adapter + .request_device(&wgpu::DeviceDescriptor { + required_features: wgpu::Features::empty(), + required_limits: wgpu::Limits::downlevel_webgl2_defaults(), + ..Default::default() + }) + .block_on() + .expect("failed to create device"); + assert!(ctx + .adapter + .request_device(&wgpu::DeviceDescriptor { + required_features: wgpu::Features::empty(), + required_limits: wgpu::Limits::downlevel_webgl2_defaults(), + ..Default::default() + }) + .block_on() + .is_err()); + }); + #[cfg(not(wasm_test))] #[apply(gpu_test!)] static MULTIPLE_DEVICES: GpuTestConfiguration = GpuTestConfiguration::new() diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 3340b64d9..e7c3d1127 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -1,5 +1,8 @@ use alloc::{borrow::ToOwned as _, boxed::Box, string::String, sync::Arc, vec, vec::Vec}; -use core::fmt; +use core::{ + fmt, + sync::atomic::{AtomicBool, Ordering}, +}; use hashbrown::HashMap; use thiserror::Error; @@ -1047,11 +1050,16 @@ impl Drop for Surface { pub struct Adapter { pub(crate) raw: hal::DynExposedAdapter, pub(crate) instance: Arc, + consumed: AtomicBool, } impl Adapter { pub(crate) fn new(raw: hal::DynExposedAdapter, instance: Arc) -> Arc { - Arc::new(Self { raw, instance }) + Arc::new(Self { + raw, + instance, + consumed: AtomicBool::new(false), + }) } /// Returns the backend this adapter is using. @@ -1193,6 +1201,15 @@ impl Adapter { profiling::scope!("Adapter::create_device_and_queue_from_hal"); api_log!("Adapter::create_device_and_queue_from_hal"); + if self + .instance + .flags + .contains(InstanceFlags::STRICT_WEBGPU_COMPLIANCE) + && self.consumed.load(Ordering::SeqCst) + { + return Err(RequestDeviceError::AdapterConsumed); + } + let default_queue_desc = desc.default_queue.clone(); let device = Device::new(hal_device.device, self, desc, self.instance.flags)?; @@ -1214,6 +1231,8 @@ impl Adapter { self.instance.devices.push(&device); + self.consumed.store(true, Ordering::SeqCst); + Ok((device, queue)) } @@ -1344,6 +1363,8 @@ pub enum RequestDeviceError { "Some experimental features, {0}, were requested, but experimental features are not enabled" )] ExperimentalFeaturesNotEnabled(wgt::Features), + #[error("The adapter has already been consumed.")] + AdapterConsumed, } #[derive(Clone, Debug, Error)]