From f7c40b2cf96327b9d2dd2ffef73400528d2ec4f7 Mon Sep 17 00:00:00 2001 From: Sam <16504129+sagudev@users.noreply.github.com> Date: Fri, 18 Sep 2026 15:10:14 +0200 Subject: [PATCH] [remote] Add `DeviceDescriptor` (#10378) Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- wgpu-core-remote-types/src/lib.rs | 27 ++++++++++++++- wgpu-core-remote/src/global/instance.rs | 44 ++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/wgpu-core-remote-types/src/lib.rs b/wgpu-core-remote-types/src/lib.rs index c393bb5ea..767989e6d 100644 --- a/wgpu-core-remote-types/src/lib.rs +++ b/wgpu-core-remote-types/src/lib.rs @@ -48,7 +48,32 @@ pub struct RequestAdapterOptions { assert_ffi_safe!(RequestAdapterOptions); -pub type DeviceDescriptor<'a> = wgt::DeviceDescriptor>; +/// Describes a `Device`. +/// +/// Corresponds to [WebGPU `GPUDeviceDescriptor`]( +/// https://gpuweb.github.io/gpuweb/#gpudevicedescriptor). +#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)] +pub struct DeviceDescriptor<'a> { + /// Debug label for the device. + pub label: Label<'a>, + /// Specifies the features that are required by the device request. + /// The request will fail if the adapter cannot provide these features. + /// + /// Exactly the specified set of features, and no more or less, + /// will be allowed in validation of API calls on the resulting device. + pub required_features: wgt::FeaturesWebGPU, + /// Specifies the limits that are required by the device request. + /// The request will fail if the adapter cannot provide these limits. + /// + /// Exactly the specified limits, and no better or worse, + /// will be allowed in validation of API calls on the resulting device. + pub required_limits: wgt::Limits, + /// Specifies the descriptor of the default queue for the device request. + /// + /// Corresponds to [WebGPU `GPUDeviceDescriptor.defaultQueue`](https://gpuweb.github.io/gpuweb/#dom-gpudevicedescriptor-defaultqueue). + pub default_queue: QueueDescriptor<'a>, +} + pub type QueueDescriptor<'a> = wgt::QueueDescriptor>; pub type BufferDescriptor<'a> = wgt::BufferDescriptor, wgt::BufferUsagesWebGPU>; pub type TextureDescriptor<'a> = wgt::TextureDescriptor, Vec>; diff --git a/wgpu-core-remote/src/global/instance.rs b/wgpu-core-remote/src/global/instance.rs index 0348bf42f..e5257333f 100644 --- a/wgpu-core-remote/src/global/instance.rs +++ b/wgpu-core-remote/src/global/instance.rs @@ -1,6 +1,9 @@ +use alloc::borrow::Cow; + use alloc::sync::Arc; use wgpu_core_remote_types::DeviceDescriptor; +use wgpu_core_remote_types::Label; use wgpu_core_remote_types::RequestAdapterOptions; use wgpu_core_remote_types::RequestDeviceError; use wgt::Backends; @@ -126,11 +129,41 @@ fn map_request_device_error(err: wgpu_core::instance::RequestDeviceError) -> Req } } +/// Accepts a `DeviceDescriptor` from the content process +/// and accepts other parameters from trusted process. +pub fn map_device_descriptor<'a>( + desc: &'a DeviceDescriptor<'a>, + trace: wgt::Trace, + additional_features: wgt::Features, +) -> wgt::DeviceDescriptor> { + wgt::DeviceDescriptor { + label: desc.label.as_ref().map(|l| Cow::Borrowed(l.as_ref())), + required_features: wgt::Features::from_internal_flags( + wgt::FeaturesWGPU::empty(), + desc.required_features, + ) + .union(additional_features), + required_limits: desc.required_limits.clone(), + default_queue: wgt::QueueDescriptor { + label: desc + .default_queue + .label + .as_ref() + .map(|l| Cow::Borrowed(l.as_ref())), + }, + experimental_features: wgt::ExperimentalFeatures::disabled(), + memory_hints: wgt::MemoryHints::MemoryUsage, + trace, + } +} + impl Global { pub fn adapter_request_device( &self, adapter_id: AdapterId, desc: &DeviceDescriptor, + trace: wgt::Trace, + additional_features: wgt::Features, device_id_in: DeviceId, queue_id_in: QueueId, ) -> Result<(DeviceId, QueueId), RequestDeviceError> { @@ -143,8 +176,9 @@ impl Global { } = &mut *hub; let adapter = adapters.get(adapter_id); + let desc = map_device_descriptor(desc, trace, additional_features); let (device, queue) = adapter - .request_device(desc) + .request_device(&desc) .map_err(map_request_device_error)?; let device_id = devices.assign(device_id_in, device); @@ -154,10 +188,10 @@ impl Global { Ok((device_id, queue_id)) } - pub fn adapter_validate_device_descriptor( + pub fn adapter_validate_device_descriptor<'a>( &self, adapter_id: AdapterId, - desc: &mut DeviceDescriptor, + desc: &mut wgt::DeviceDescriptor>, ) -> Result<(), RequestDeviceError> { let hub = self.hub.borrow(); let adapter = hub.adapters.get(adapter_id); @@ -170,11 +204,11 @@ impl Global { /// /// - `hal_device` must be created from `adapter_id` or its internal handle. /// - `desc` must be a subset of `hal_device` features and limits. - pub unsafe fn create_device_from_hal( + pub unsafe fn create_device_from_hal<'a>( &self, adapter_id: AdapterId, hal_device: hal::DynOpenDevice, - desc: &DeviceDescriptor, + desc: &wgt::DeviceDescriptor>, device_id_in: DeviceId, queue_id_in: QueueId, ) -> Result<(DeviceId, QueueId), RequestDeviceError> {