[wgpu-core] change return type of device creation methods to be more rusty

This is fine from the WebGPU spec's perspective as device creation returns a promise that can reject.
This commit is contained in:
teoxoy 2024-09-05 15:03:37 +02:00 committed by Teodor Tanasoaia
parent da9afea271
commit aa280ecd44
5 changed files with 38 additions and 57 deletions

View File

@ -649,7 +649,7 @@ pub fn op_webgpu_request_device(
memory_hints: wgpu_types::MemoryHints::default(),
};
let (device, queue, maybe_err) = instance.adapter_request_device(
let res = instance.adapter_request_device(
adapter,
&descriptor,
std::env::var("DENO_WEBGPU_TRACE")
@ -660,9 +660,8 @@ pub fn op_webgpu_request_device(
None,
);
adapter_resource.close();
if let Some(err) = maybe_err {
return Err(DomExceptionOperationError::new(&err.to_string()).into());
}
let (device, queue) = res.map_err(|err| DomExceptionOperationError::new(&err.to_string()))?;
let device_features = instance.device_features(device)?;
let features = deserialize_features(&device_features);

View File

@ -82,14 +82,14 @@ fn main() {
log::info!("Picked '{}'", info.name);
let device_id = wgc::id::Id::zip(1, 0, backend);
let queue_id = wgc::id::Id::zip(1, 0, backend);
let (_, _, error) = global.adapter_request_device(
let res = global.adapter_request_device(
adapter,
&desc,
None,
Some(device_id),
Some(queue_id),
);
if let Some(e) = error {
if let Err(e) = res {
panic!("{:?}", e);
}
(device_id, queue_id)

View File

@ -108,7 +108,7 @@ impl Test<'_> {
let backend = adapter.backend();
let device_id = wgc::id::Id::zip(test_num, 0, backend);
let queue_id = wgc::id::Id::zip(test_num, 0, backend);
let (_, _, error) = global.adapter_request_device(
let res = global.adapter_request_device(
adapter,
&wgt::DeviceDescriptor {
label: None,
@ -120,7 +120,7 @@ impl Test<'_> {
Some(device_id),
Some(queue_id),
);
if let Some(e) = error {
if let Err(e) = res {
panic!("{:?}", e);
}

View File

@ -899,7 +899,7 @@ impl Global {
trace_path: Option<&std::path::Path>,
device_id_in: Option<DeviceId>,
queue_id_in: Option<QueueId>,
) -> (DeviceId, QueueId, Option<RequestDeviceError>) {
) -> Result<(DeviceId, QueueId), RequestDeviceError> {
profiling::scope!("Adapter::request_device");
api_log!("Adapter::request_device");
@ -907,26 +907,17 @@ impl Global {
let device_fid = self.hub.devices.prepare(backend, device_id_in);
let queue_fid = self.hub.queues.prepare(backend, queue_id_in);
let error = 'error: {
let adapter = self.hub.adapters.strict_get(adapter_id);
let (device, queue) =
match adapter.create_device_and_queue(desc, self.instance.flags, trace_path) {
Ok((device, queue)) => (device, queue),
Err(e) => break 'error e,
};
let adapter = self.hub.adapters.strict_get(adapter_id);
let (device, queue) =
adapter.create_device_and_queue(desc, self.instance.flags, trace_path)?;
let device_id = device_fid.assign(device);
resource_log!("Created Device {:?}", device_id);
let device_id = device_fid.assign(device);
resource_log!("Created Device {:?}", device_id);
let queue_id = queue_fid.assign(queue);
resource_log!("Created Queue {:?}", queue_id);
let queue_id = queue_fid.assign(queue);
resource_log!("Created Queue {:?}", queue_id);
return (device_id, queue_id, None);
};
let device_id = device_fid.assign_error();
let queue_id = queue_fid.assign_error();
(device_id, queue_id, Some(error))
Ok((device_id, queue_id))
}
/// # Safety
@ -941,37 +932,28 @@ impl Global {
trace_path: Option<&std::path::Path>,
device_id_in: Option<DeviceId>,
queue_id_in: Option<QueueId>,
) -> (DeviceId, QueueId, Option<RequestDeviceError>) {
) -> Result<(DeviceId, QueueId), RequestDeviceError> {
profiling::scope!("Global::create_device_from_hal");
let backend = adapter_id.backend();
let devices_fid = self.hub.devices.prepare(backend, device_id_in);
let queues_fid = self.hub.queues.prepare(backend, queue_id_in);
let error = 'error: {
let adapter = self.hub.adapters.strict_get(adapter_id);
let (device, queue) = match adapter.create_device_and_queue_from_hal(
hal_device,
desc,
self.instance.flags,
trace_path,
) {
Ok(device) => device,
Err(e) => break 'error e,
};
let adapter = self.hub.adapters.strict_get(adapter_id);
let (device, queue) = adapter.create_device_and_queue_from_hal(
hal_device,
desc,
self.instance.flags,
trace_path,
)?;
let device_id = devices_fid.assign(device);
resource_log!("Created Device {:?}", device_id);
let device_id = devices_fid.assign(device);
resource_log!("Created Device {:?}", device_id);
let queue_id = queues_fid.assign(queue);
resource_log!("Created Queue {:?}", queue_id);
let queue_id = queues_fid.assign(queue);
resource_log!("Created Queue {:?}", queue_id);
return (device_id, queue_id, None);
};
let device_id = devices_fid.assign_error();
let queue_id = queues_fid.assign_error();
(device_id, queue_id, Some(error))
Ok((device_id, queue_id))
}
}

View File

@ -108,7 +108,7 @@ impl ContextWgpuCore {
if trace_dir.is_some() {
log::error!("Feature 'trace' has been removed temporarily, see https://github.com/gfx-rs/wgpu/issues/5974");
}
let (device_id, queue_id, error) = unsafe {
let (device_id, queue_id) = unsafe {
self.0.create_device_from_hal(
*adapter,
hal_device.into(),
@ -117,10 +117,7 @@ impl ContextWgpuCore {
None,
None,
)
};
if let Some(err) = error {
self.handle_error_fatal(err, "Adapter::create_device_from_hal");
}
}?;
let error_sink = Arc::new(Mutex::new(ErrorSinkRaw::new()));
let device = Device {
id: device_id,
@ -606,16 +603,19 @@ impl crate::Context for ContextWgpuCore {
if trace_dir.is_some() {
log::error!("Feature 'trace' has been removed temporarily, see https://github.com/gfx-rs/wgpu/issues/5974");
}
let (device_id, queue_id, error) = self.0.adapter_request_device(
let res = self.0.adapter_request_device(
*adapter_data,
&desc.map_label(|l| l.map(Borrowed)),
None,
None,
None,
);
if let Some(err) = error {
return ready(Err(err.into()));
}
let (device_id, queue_id) = match res {
Ok(ids) => ids,
Err(err) => {
return ready(Err(err.into()));
}
};
let error_sink = Arc::new(Mutex::new(ErrorSinkRaw::new()));
let device = Device {
id: device_id,