Skip to content

Commit

Permalink
web: Implement Device::on_uncaptured_error
Browse files Browse the repository at this point in the history
  • Loading branch information
Herschel committed Oct 30, 2021
1 parent 5f1b3e5 commit 23c4465
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
pin::Pin,
task::{self, Poll},
};
use wasm_bindgen::prelude::*;
use wasm_bindgen::{prelude::*, JsCast};

// We need to make a wrapper for some of the handle types returned by the web backend to make them
// implement `Send` and `Sync` to match native.
Expand Down Expand Up @@ -1623,10 +1623,27 @@ impl crate::Context for Context {

fn device_on_uncaptured_error(
&self,
_device: &Self::DeviceId,
_handler: impl crate::UncapturedErrorHandler,
device: &Self::DeviceId,
handler: impl crate::UncapturedErrorHandler,
) {
// TODO:
let f = Closure::wrap(Box::new(move |event: web_sys::GpuUncapturedErrorEvent| {
// Convert the JS error into a wgpu error.
let js_error = event.error();
let source = Box::<dyn std::error::Error + Send + Sync>::from("<WebGPU Error>");
if let Some(js_error) = js_error.dyn_ref::<web_sys::GpuValidationError>() {
handler(crate::Error::ValidationError {
source,
description: js_error.message(),
});
} else if let Some(_) = js_error.dyn_ref::<web_sys::GpuOutOfMemoryError>() {
handler(crate::Error::OutOfMemoryError { source });
}
}) as Box<dyn FnMut(_)>);
device
.0
.set_onuncapturederror(Some(f.as_ref().unchecked_ref()));
// TODO: This will leak the memory associated with the error handler by default.
f.forget();
}

fn buffer_map_async(
Expand Down

0 comments on commit 23c4465

Please sign in to comment.