From 041f76bbb18a0ab0d3c1db3a711b3d5d5fa924fa Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Sat, 15 Aug 2026 13:25:58 +0200 Subject: [PATCH] Move error types to wgpu-types Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- wgpu-types/src/error.rs | 109 ++++++++++++++++++++++++++++++++++++- wgpu/src/api/device.rs | 84 +--------------------------- wgpu/src/backend/webgpu.rs | 48 ++++++++-------- 3 files changed, 132 insertions(+), 109 deletions(-) diff --git a/wgpu-types/src/error.rs b/wgpu-types/src/error.rs index 96cc7453a..79509a5e8 100644 --- a/wgpu-types/src/error.rs +++ b/wgpu-types/src/error.rs @@ -1,6 +1,10 @@ //! Shared types for WebGPU errors. See also: //! +use alloc::boxed::Box; +use alloc::string::String; +use core::{error, fmt}; + /// A classification of WebGPU error for implementers of the WebGPU API to use in their own error /// layer(s). /// @@ -35,7 +39,110 @@ pub enum ErrorType { /// /// This is intended to be used as a convenience by implementations of WebGPU to classify errors /// returned by [`wgpu_core`](crate). -pub trait WebGpuError: core::error::Error + 'static { +pub trait WebGpuError: error::Error + 'static { /// Determine the classification of this error as a WebGPU [`ErrorType`]. fn webgpu_error_type(&self) -> ErrorType; } + +/// The callback of [`uncaptured_error`](https://gpuweb.github.io/gpuweb/#eventdef-gpudevice-uncapturederror) +/// +/// It must be a function with this signature. +pub trait UncapturedErrorHandler: Fn(Error) + Send + Sync + 'static {} +impl UncapturedErrorHandler for T where T: Fn(Error) + Send + Sync + 'static {} + +/// Kinds of [`Error`]s a [`push_error_scope`](https://gpuweb.github.io/gpuweb/#dom-gpudevice-pusherrorscope) may be configured to catch. +/// +/// Corresponds to the [`GPUErrorFilter`] type in the WebGPU API. +/// +/// [`GPUErrorFilter`]: https://gpuweb.github.io/gpuweb/#enumdef-gpuerrorfilter +#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd)] +pub enum ErrorFilter { + /// Catch only out-of-memory errors. + OutOfMemory, + /// Catch only validation errors. + Validation, + /// Catch only internal errors. + Internal, +} +static_assertions::assert_impl_all!(ErrorFilter: Send, Sync); + +/// Lower level source of the error. +/// +/// `Send + Sync` varies depending on configuration. +#[cfg(any( + not(target_family = "wasm"), + all( + feature = "fragile-send-sync-non-atomic-wasm", + not(target_feature = "atomics") + ) +))] +#[cfg_attr(docsrs, doc(cfg(all())))] +pub type ErrorSource = Box; +/// Lower level source of the error. +/// +/// `Send + Sync` varies depending on configuration. +#[cfg(not(any( + not(target_family = "wasm"), + all( + feature = "fragile-send-sync-non-atomic-wasm", + not(target_feature = "atomics") + ) +)))] +#[cfg_attr(docsrs, doc(cfg(all())))] +pub type ErrorSource = Box; + +/// Errors resulting from usage of GPU APIs. +#[derive(Debug)] +pub enum Error { + /// Out of memory. + OutOfMemory { + /// Lower level source of the error. + source: ErrorSource, + }, + /// Validation error, signifying a bug in code or data provided to `wgpu`. + Validation { + /// Lower level source of the error. + source: ErrorSource, + /// Description of the validation error. + description: String, + }, + /// Internal error. Used for signalling any failures not explicitly expected by WebGPU. + /// + /// These could be due to internal implementation or system limits being reached. + Internal { + /// Lower level source of the error. + source: ErrorSource, + /// Description of the internal GPU error. + description: String, + }, +} + +impl error::Error for Error { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + match self { + Error::OutOfMemory { source } => Some(source.as_ref()), + Error::Validation { source, .. } => Some(source.as_ref()), + Error::Internal { source, .. } => Some(source.as_ref()), + } + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Error::OutOfMemory { .. } => f.write_str("Out of Memory"), + Error::Validation { description, .. } => f.write_str(description), + Error::Internal { description, .. } => f.write_str(description), + } + } +} + +impl WebGpuError for Error { + fn webgpu_error_type(&self) -> ErrorType { + match self { + Error::OutOfMemory { .. } => ErrorType::OutOfMemory, + Error::Validation { .. } => ErrorType::Validation, + Error::Internal { .. } => ErrorType::Internal, + } + } +} diff --git a/wgpu/src/api/device.rs b/wgpu/src/api/device.rs index 0124fa64e..264dc9ffb 100644 --- a/wgpu/src/api/device.rs +++ b/wgpu/src/api/device.rs @@ -7,6 +7,7 @@ use crate::api::blas::{Blas, BlasGeometrySizeDescriptors, CreateBlasDescriptor}; use crate::api::tlas::{CreateTlasDescriptor, Tlas}; use crate::util::Mutex; use crate::*; +pub use wgt::error::*; /// Open connection to a graphics and/or compute device. /// @@ -844,89 +845,6 @@ impl From for RequestDeviceError { } } -/// The callback of [`Device::on_uncaptured_error()`]. -/// -/// It must be a function with this signature. -pub trait UncapturedErrorHandler: Fn(Error) + Send + Sync + 'static {} -impl UncapturedErrorHandler for T where T: Fn(Error) + Send + Sync + 'static {} - -/// Kinds of [`Error`]s a [`Device::push_error_scope()`] may be configured to catch. -#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd)] -pub enum ErrorFilter { - /// Catch only out-of-memory errors. - OutOfMemory, - /// Catch only validation errors. - Validation, - /// Catch only internal errors. - Internal, -} -static_assertions::assert_impl_all!(ErrorFilter: Send, Sync); - -/// Lower level source of the error. -/// -/// `Send + Sync` varies depending on configuration. -#[cfg(send_sync)] -#[cfg_attr(docsrs, doc(cfg(all())))] -pub type ErrorSource = Box; -/// Lower level source of the error. -/// -/// `Send + Sync` varies depending on configuration. -#[cfg(not(send_sync))] -#[cfg_attr(docsrs, doc(cfg(all())))] -pub type ErrorSource = Box; - -/// Errors resulting from usage of GPU APIs. -/// -/// By default, errors translate into panics. Depending on the backend and circumstances, -/// errors may occur synchronously or asynchronously. When errors need to be handled, use -/// [`Device::push_error_scope()`] or [`Device::on_uncaptured_error()`]. -#[derive(Debug)] -pub enum Error { - /// Out of memory. - OutOfMemory { - /// Lower level source of the error. - source: ErrorSource, - }, - /// Validation error, signifying a bug in code or data provided to `wgpu`. - Validation { - /// Lower level source of the error. - source: ErrorSource, - /// Description of the validation error. - description: String, - }, - /// Internal error. Used for signalling any failures not explicitly expected by WebGPU. - /// - /// These could be due to internal implementation or system limits being reached. - Internal { - /// Lower level source of the error. - source: ErrorSource, - /// Description of the internal GPU error. - description: String, - }, -} -#[cfg(send_sync)] -static_assertions::assert_impl_all!(Error: Send, Sync); - -impl error::Error for Error { - fn source(&self) -> Option<&(dyn error::Error + 'static)> { - match self { - Error::OutOfMemory { source } => Some(source.as_ref()), - Error::Validation { source, .. } => Some(source.as_ref()), - Error::Internal { source, .. } => Some(source.as_ref()), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Error::OutOfMemory { .. } => f.write_str("Out of Memory"), - Error::Validation { description, .. } => f.write_str(description), - Error::Internal { description, .. } => f.write_str(description), - } - } -} - /// Guard for an error scope pushed with [`Device::push_error_scope()`]. /// /// Call [`pop()`] to pop the scope and get a future for the result. If diff --git a/wgpu/src/backend/webgpu.rs b/wgpu/src/backend/webgpu.rs index 043b9a4bd..b30541aac 100644 --- a/wgpu/src/backend/webgpu.rs +++ b/wgpu/src/backend/webgpu.rs @@ -71,30 +71,28 @@ impl fmt::Debug for ContextWebGpu { } } -impl crate::Error { - fn from_js(js_error: js_sys::Object) -> Self { - let source = Box::::from(""); - if let Some(js_error) = js_error.dyn_ref::() { - crate::Error::Validation { - source, - description: js_error.message(), - } - } else if let Some(js_error) = js_error.dyn_ref::() { - crate::Error::Internal { - source, - description: js_error.message(), - } - } else if js_error.has_type::() { - crate::Error::OutOfMemory { source } - } else { - let constructor = js_error - .constructor() - .name() - .as_string() - .unwrap_or_default(); - let value = js_error.to_string().as_string().unwrap_or_default(); - panic!("Unexpected error: constructor={constructor} value={value}"); +fn error_from_js(js_error: js_sys::Object) -> crate::Error { + let source = Box::::from(""); + if let Some(js_error) = js_error.dyn_ref::() { + crate::Error::Validation { + source, + description: js_error.message(), } + } else if let Some(js_error) = js_error.dyn_ref::() { + crate::Error::Internal { + source, + description: js_error.message(), + } + } else if js_error.has_type::() { + crate::Error::OutOfMemory { source } + } else { + let constructor = js_error + .constructor() + .name() + .as_string() + .unwrap_or_default(); + let value = js_error.to_string().as_string().unwrap_or_default(); + panic!("Unexpected error: constructor={constructor} value={value}"); } } @@ -1075,7 +1073,7 @@ fn future_request_device( fn future_pop_error_scope( result: Result, wasm_bindgen::JsValue>, ) -> Option { - Some(crate::Error::from_js(result.ok()?.into_option()?.into())) + Some(error_from_js(result.ok()?.into_option()?.into())) } fn future_compilation_info( @@ -2680,7 +2678,7 @@ impl dispatch::DeviceInterface for WebDevice { fn on_uncaptured_error(&self, handler: Arc) { let f = Closure::wrap(Box::new(move |event: webgpu_sys::GpuUncapturedErrorEvent| { - let error = crate::Error::from_js(event.error().value_of()); + let error = error_from_js(event.error().value_of()); handler(error); }) as Box); self.inner