From 7a655581ff7d3fd9f38e4ede2bdd9c16bfcba899 Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Thu, 30 Jul 2026 11:18:49 -0700 Subject: [PATCH] feat(core): Expose descriptor validation separately from `request_device` (#9967) --- CHANGELOG.md | 1 + wgpu-core/src/instance.rs | 42 ++++++++++++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 973a79790..7bbdeb3e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,7 @@ Bottom level categories: #### General - Support the `wasm64-unknown-unknown` target for the web backend. Building for wasm64 requires a nightly toolchain with `-Z build-std=std,panic_abort`. By @nickbabcock in [#9836](https://github.com/gfx-rs/wgpu/pull/9836). +- `wgpu-core` now exposes a `validate_device_descriptor` function that validates a device descriptor as `request_device` would. This may be useful in conjunction with `create_device_from_hal`. By @andyleiserson in [#9967](https://github.com/gfx-rs/wgpu/pull/9967). #### Hal diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 3400eb777..c9dd04451 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -1216,13 +1216,19 @@ impl Adapter { Ok((device, queue)) } - pub fn request_device( - self: &Arc, - desc: &DeviceDescriptor, - ) -> Result<(Arc, Arc), RequestDeviceError> { - profiling::scope!("Adapter::request_device"); - api_log!("Adapter::request_device"); - let mut desc = desc.clone(); + /// Validate a device descriptor. + /// + /// This validates the provided device descriptor as if it were passed to + /// [`Self::request_device`]. If [`InstanceFlags::STRICT_WEBGPU_COMPLIANCE`] is active, + /// the requested extensions in the descriptor will be filtered to remove `wgpu` + /// extensions, except for those that are included in [`limits::EXEMPT_FEATURES`]. + /// + /// This may be useful when it is necessary to obtain the device itself from a raw hal + /// API, but the rest of the `request_device` validation is still desired. + pub fn validate_device_descriptor( + &self, + desc: &mut DeviceDescriptor, + ) -> Result<(), RequestDeviceError> { filter_features_and_limits( self.instance_flags, &mut desc.required_features, @@ -1273,6 +1279,19 @@ impl Adapter { return Err(RequestDeviceError::LimitsExceeded(failed)); } + Ok(()) + } + + pub fn request_device( + self: &Arc, + desc: &DeviceDescriptor, + ) -> Result<(Arc, Arc), RequestDeviceError> { + profiling::scope!("Adapter::request_device"); + api_log!("Adapter::request_device"); + + let mut desc = desc.clone(); + self.validate_device_descriptor(&mut desc)?; + let open = unsafe { self.raw.adapter.open( desc.required_features, @@ -1605,6 +1624,15 @@ impl Global { Ok((device_id, queue_id)) } + pub fn adapter_validate_device_descriptor( + &self, + adapter_id: AdapterId, + desc: &mut DeviceDescriptor, + ) -> Result<(), RequestDeviceError> { + let adapter = self.hub.adapters.get(adapter_id); + adapter.validate_device_descriptor(desc) + } + /// # Safety /// /// - `hal_device` must be created from `adapter_id` or its internal handle.