2022-01-20 02:10:16 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-03-01 05:31:13 -05:00
|
|
|
use deno_core::error::AnyError;
|
2021-04-05 12:40:24 -04:00
|
|
|
use deno_core::ResourceId;
|
2021-03-01 05:31:13 -05:00
|
|
|
use serde::Serialize;
|
2021-04-05 12:40:24 -04:00
|
|
|
use std::convert::From;
|
2022-07-19 20:22:26 -04:00
|
|
|
use std::error::Error;
|
2021-03-01 05:31:13 -05:00
|
|
|
use std::fmt;
|
2022-07-19 20:22:26 -04:00
|
|
|
use std::fmt::Write;
|
2021-03-01 05:31:13 -05:00
|
|
|
use wgpu_core::binding_model::CreateBindGroupError;
|
|
|
|
use wgpu_core::binding_model::CreateBindGroupLayoutError;
|
|
|
|
use wgpu_core::binding_model::CreatePipelineLayoutError;
|
|
|
|
use wgpu_core::binding_model::GetBindGroupLayoutError;
|
2022-01-24 17:47:05 -05:00
|
|
|
use wgpu_core::command::ClearError;
|
2021-03-01 05:31:13 -05:00
|
|
|
use wgpu_core::command::CommandEncoderError;
|
|
|
|
use wgpu_core::command::ComputePassError;
|
|
|
|
use wgpu_core::command::CopyError;
|
|
|
|
use wgpu_core::command::CreateRenderBundleError;
|
|
|
|
use wgpu_core::command::QueryError;
|
|
|
|
use wgpu_core::command::RenderBundleError;
|
|
|
|
use wgpu_core::command::RenderPassError;
|
|
|
|
use wgpu_core::device::queue::QueueSubmitError;
|
|
|
|
use wgpu_core::device::queue::QueueWriteError;
|
|
|
|
use wgpu_core::device::DeviceError;
|
|
|
|
use wgpu_core::pipeline::CreateComputePipelineError;
|
|
|
|
use wgpu_core::pipeline::CreateRenderPipelineError;
|
|
|
|
use wgpu_core::pipeline::CreateShaderModuleError;
|
|
|
|
use wgpu_core::resource::BufferAccessError;
|
|
|
|
use wgpu_core::resource::CreateBufferError;
|
|
|
|
use wgpu_core::resource::CreateQuerySetError;
|
|
|
|
use wgpu_core::resource::CreateSamplerError;
|
|
|
|
use wgpu_core::resource::CreateTextureError;
|
|
|
|
use wgpu_core::resource::CreateTextureViewError;
|
|
|
|
|
2022-07-19 20:22:26 -04:00
|
|
|
fn fmt_err(err: &(dyn Error + 'static)) -> String {
|
|
|
|
let mut output = err.to_string();
|
|
|
|
|
|
|
|
let mut e = err.source();
|
|
|
|
while let Some(source) = e {
|
|
|
|
// No error possible, unwrap is fine here.
|
|
|
|
// https://github.com/rust-lang/rust/blob/1.47.0/library/alloc/src/string.rs#L2414-L2427
|
|
|
|
write!(output, ": {source}").unwrap();
|
|
|
|
e = source.source();
|
|
|
|
}
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct WebGpuResult {
|
|
|
|
pub rid: Option<ResourceId>,
|
|
|
|
pub err: Option<WebGpuError>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WebGpuResult {
|
|
|
|
pub fn rid(rid: ResourceId) -> Self {
|
|
|
|
Self {
|
|
|
|
rid: Some(rid),
|
|
|
|
err: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rid_err<T: Into<WebGpuError>>(
|
|
|
|
rid: ResourceId,
|
|
|
|
err: Option<T>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
rid: Some(rid),
|
2022-07-19 20:22:26 -04:00
|
|
|
err: err.map(Into::into),
|
2021-04-05 12:40:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maybe_err<T: Into<WebGpuError>>(err: Option<T>) -> Self {
|
|
|
|
Self {
|
|
|
|
rid: None,
|
2022-07-19 20:22:26 -04:00
|
|
|
err: err.map(Into::into),
|
2021-04-05 12:40:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn empty() -> Self {
|
|
|
|
Self {
|
|
|
|
rid: None,
|
|
|
|
err: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 05:31:13 -05:00
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(tag = "type", content = "value")]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
2021-03-25 14:17:37 -04:00
|
|
|
pub enum WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
Lost,
|
|
|
|
OutOfMemory,
|
|
|
|
Validation(String),
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<CreateBufferError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: CreateBufferError) -> Self {
|
|
|
|
match err {
|
|
|
|
CreateBufferError::Device(err) => err.into(),
|
|
|
|
CreateBufferError::AccessError(err) => err.into(),
|
2022-07-19 20:22:26 -04:00
|
|
|
err => WebGpuError::Validation(fmt_err(&err)),
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<DeviceError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: DeviceError) -> Self {
|
|
|
|
match err {
|
2021-03-25 14:17:37 -04:00
|
|
|
DeviceError::Lost => WebGpuError::Lost,
|
|
|
|
DeviceError::OutOfMemory => WebGpuError::OutOfMemory,
|
2022-07-19 20:22:26 -04:00
|
|
|
DeviceError::Invalid => WebGpuError::Validation(fmt_err(&err)),
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<BufferAccessError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: BufferAccessError) -> Self {
|
|
|
|
match err {
|
|
|
|
BufferAccessError::Device(err) => err.into(),
|
2022-07-19 20:22:26 -04:00
|
|
|
err => WebGpuError::Validation(fmt_err(&err)),
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<CreateBindGroupLayoutError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: CreateBindGroupLayoutError) -> Self {
|
|
|
|
match err {
|
|
|
|
CreateBindGroupLayoutError::Device(err) => err.into(),
|
2022-07-19 20:22:26 -04:00
|
|
|
err => WebGpuError::Validation(fmt_err(&err)),
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<CreatePipelineLayoutError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: CreatePipelineLayoutError) -> Self {
|
|
|
|
match err {
|
|
|
|
CreatePipelineLayoutError::Device(err) => err.into(),
|
2022-07-19 20:22:26 -04:00
|
|
|
err => WebGpuError::Validation(fmt_err(&err)),
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<CreateBindGroupError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: CreateBindGroupError) -> Self {
|
|
|
|
match err {
|
|
|
|
CreateBindGroupError::Device(err) => err.into(),
|
2022-07-19 20:22:26 -04:00
|
|
|
err => WebGpuError::Validation(fmt_err(&err)),
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<RenderBundleError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: RenderBundleError) -> Self {
|
2022-07-19 20:22:26 -04:00
|
|
|
WebGpuError::Validation(fmt_err(&err))
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<CreateRenderBundleError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: CreateRenderBundleError) -> Self {
|
2022-07-19 20:22:26 -04:00
|
|
|
WebGpuError::Validation(fmt_err(&err))
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<CopyError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: CopyError) -> Self {
|
2022-07-19 20:22:26 -04:00
|
|
|
WebGpuError::Validation(fmt_err(&err))
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<CommandEncoderError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: CommandEncoderError) -> Self {
|
2022-07-19 20:22:26 -04:00
|
|
|
WebGpuError::Validation(fmt_err(&err))
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<QueryError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: QueryError) -> Self {
|
2022-07-19 20:22:26 -04:00
|
|
|
WebGpuError::Validation(fmt_err(&err))
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<ComputePassError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: ComputePassError) -> Self {
|
2022-07-19 20:22:26 -04:00
|
|
|
WebGpuError::Validation(fmt_err(&err))
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<CreateComputePipelineError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: CreateComputePipelineError) -> Self {
|
|
|
|
match err {
|
|
|
|
CreateComputePipelineError::Device(err) => err.into(),
|
2022-07-19 20:22:26 -04:00
|
|
|
err => WebGpuError::Validation(fmt_err(&err)),
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<GetBindGroupLayoutError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: GetBindGroupLayoutError) -> Self {
|
2022-07-19 20:22:26 -04:00
|
|
|
WebGpuError::Validation(fmt_err(&err))
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<CreateRenderPipelineError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: CreateRenderPipelineError) -> Self {
|
|
|
|
match err {
|
|
|
|
CreateRenderPipelineError::Device(err) => err.into(),
|
2022-07-19 20:22:26 -04:00
|
|
|
err => WebGpuError::Validation(fmt_err(&err)),
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<RenderPassError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: RenderPassError) -> Self {
|
2022-07-19 20:22:26 -04:00
|
|
|
WebGpuError::Validation(fmt_err(&err))
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<CreateSamplerError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: CreateSamplerError) -> Self {
|
|
|
|
match err {
|
|
|
|
CreateSamplerError::Device(err) => err.into(),
|
2022-07-19 20:22:26 -04:00
|
|
|
err => WebGpuError::Validation(fmt_err(&err)),
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<CreateShaderModuleError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: CreateShaderModuleError) -> Self {
|
|
|
|
match err {
|
|
|
|
CreateShaderModuleError::Device(err) => err.into(),
|
2022-07-19 20:22:26 -04:00
|
|
|
err => WebGpuError::Validation(fmt_err(&err)),
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<CreateTextureError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: CreateTextureError) -> Self {
|
|
|
|
match err {
|
|
|
|
CreateTextureError::Device(err) => err.into(),
|
2022-07-19 20:22:26 -04:00
|
|
|
err => WebGpuError::Validation(fmt_err(&err)),
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<CreateTextureViewError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: CreateTextureViewError) -> Self {
|
2022-07-19 20:22:26 -04:00
|
|
|
WebGpuError::Validation(fmt_err(&err))
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<CreateQuerySetError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: CreateQuerySetError) -> Self {
|
|
|
|
match err {
|
|
|
|
CreateQuerySetError::Device(err) => err.into(),
|
2022-07-19 20:22:26 -04:00
|
|
|
err => WebGpuError::Validation(fmt_err(&err)),
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<QueueSubmitError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: QueueSubmitError) -> Self {
|
|
|
|
match err {
|
|
|
|
QueueSubmitError::Queue(err) => err.into(),
|
2022-07-19 20:22:26 -04:00
|
|
|
err => WebGpuError::Validation(fmt_err(&err)),
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl From<QueueWriteError> for WebGpuError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn from(err: QueueWriteError) -> Self {
|
|
|
|
match err {
|
|
|
|
QueueWriteError::Queue(err) => err.into(),
|
2022-07-19 20:22:26 -04:00
|
|
|
err => WebGpuError::Validation(fmt_err(&err)),
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 17:47:05 -05:00
|
|
|
impl From<ClearError> for WebGpuError {
|
|
|
|
fn from(err: ClearError) -> Self {
|
2022-07-19 20:22:26 -04:00
|
|
|
WebGpuError::Validation(fmt_err(&err))
|
2022-01-24 17:47:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 05:31:13 -05:00
|
|
|
#[derive(Debug)]
|
2021-03-25 14:17:37 -04:00
|
|
|
pub struct DomExceptionOperationError {
|
2021-03-01 05:31:13 -05:00
|
|
|
pub msg: String,
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl DomExceptionOperationError {
|
2021-03-01 05:31:13 -05:00
|
|
|
pub fn new(msg: &str) -> Self {
|
2021-03-25 14:17:37 -04:00
|
|
|
DomExceptionOperationError {
|
2021-03-01 05:31:13 -05:00
|
|
|
msg: msg.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl fmt::Display for DomExceptionOperationError {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.pad(&self.msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
impl std::error::Error for DomExceptionOperationError {}
|
2021-03-01 05:31:13 -05:00
|
|
|
|
|
|
|
pub fn get_error_class_name(e: &AnyError) -> Option<&'static str> {
|
2021-03-25 14:17:37 -04:00
|
|
|
e.downcast_ref::<DomExceptionOperationError>()
|
2021-03-01 05:31:13 -05:00
|
|
|
.map(|_| "DOMExceptionOperationError")
|
|
|
|
}
|