Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,11 @@ By @cwfitzgerald in [#8388](https://github.com/gfx-rs/wgpu/pull/8388).
- Fix `Device::on_uncaptured_error` never firing in browsers that do not implement `GPUDevice.onuncapturederror`, such as Safari, by listening for the `uncapturederror` event instead of assigning to that attribute. By @zemse in [#10270](https://github.com/gfx-rs/wgpu/pull/10270).
- Pass `RenderPassColorAttachment::depth_slice` to the browser, so a render pass into a 3D texture no longer fails validation with "depthSlice is required for a 3D attachment". By @jheising in [#10693](https://github.com/gfx-rs/wgpu/pull/10693).

#### deno_webgpu

- Fix panics when a device is lost: `mapAsync()` and `onSubmittedWorkDone()` unwrapped the result of polling the device, and the task that resolves `device.lost` unwrapped a resolver that the device's finalizer may already have removed. By @matthargett in [#XXXXX](https://github.com/gfx-rs/wgpu/pull/XXXXX).
- Fix `device.lost` never resolving after `device.destroy()` when a queue submission was still in flight. `cts_runner` then exited with status 0 and without reporting any result, as it did for `webgpu:api,operation,uncapturederror:iff_uncaptured:*`. By @matthargett in [#XXXXX](https://github.com/gfx-rs/wgpu/pull/XXXXX).

### Performance

- GLES: `Queue::submit` no longer resets GL state for command buffers that record no commands (e.g. the internal transit and pre-pass encoders), removing two full reset sequences (about 20 GL calls) per frame for a typical single-pass WebGL2 renderer. By @chinqan in [#10913](https://github.com/gfx-rs/wgpu/pull/10913).
Expand Down
18 changes: 15 additions & 3 deletions deno_webgpu/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,14 @@ impl GPUAdapter {
let error_handler =
Rc::new(super::error::DeviceErrorHandler::new(wgpu_device.clone()));

let wgpu_device_id = Arc::as_ptr(&wgpu_device) as usize;
// Key the per-device tables by a number that is never reused. The
// address of the device was used before, but a stale device-lost task or
// finalizer for a freed device could then act on a newer device that
// happened to be allocated at the same address.
static NEXT_DEVICE_ID: std::sync::atomic::AtomicUsize =
std::sync::atomic::AtomicUsize::new(1);
let wgpu_device_id =
NEXT_DEVICE_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);

let lost_resolver = v8::PromiseResolver::new(scope).unwrap();
let lost_promise = lost_resolver.get_promise(scope);
Expand All @@ -201,11 +208,16 @@ impl GPUAdapter {
};
let wake = Arc::new(Mutex::new(move |reason: DeviceLostReason| {
cross_thread_spawner.spawn(move |scope| {
let lost_resolver = JsRuntime::op_state_from(scope)
// The finalizer of the JS device object removes the resolver too.
// If the object was collected between the device being lost and
// this task running, there is nobody left to tell.
let Some(lost_resolver) = JsRuntime::op_state_from(scope)
.borrow_mut()
.borrow_mut::<LostPromiseResolverHM>()
.remove(&wgpu_device_id)
.unwrap();
else {
return;
};
let lost_resolver = v8::Local::new(scope, lost_resolver);
let info = make_cppgc_object(
scope,
Expand Down
9 changes: 7 additions & 2 deletions deno_webgpu/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ impl GPUBuffer {

{
let callback = Box::new(move |status| {
sender.send(status).unwrap();
// The receiver is gone if the mapping was already rejected because
// the device was lost; there is nobody left to tell.
let _ = sender.send(status);
});

self.wgpu_buffer.map_async(
Expand Down Expand Up @@ -270,10 +272,13 @@ impl GPUBuffer {
let device_poll_fut = async move {
while !*done.borrow() {
{
// A failed poll means the device is gone (the driver reported
// a lost device or ran out of memory). Reject the mapping rather
// than aborting the process.
self
.wgpu_device
.poll(wgpu_types::PollType::wait_indefinitely())
.unwrap();
.map_err(|e| BufferError::Aborted(e.to_string()))?;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
Expand Down
11 changes: 8 additions & 3 deletions deno_webgpu/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,14 @@ impl GPUDevice {
#[undefined]
fn destroy(&self) {
self.wgpu_device.destroy();
// Poll the device to encourage it to process any pending operations
// and release resources associated with the destroyed device (and thus calling lost closure).
let _ = self.wgpu_device.poll(wgpu_types::PollType::Poll);
// Wait for the device to finish any pending submissions and release the
// resources associated with the destroyed device, which is when wgpu
// calls the lost closure. A single non-blocking poll is not enough: if a
// submission is still in flight, the closure is not called, nothing else
// polls a destroyed device, and `device.lost` never resolves.
let _ = self
.wgpu_device
.poll(wgpu_types::PollType::wait_indefinitely());
}

#[reentrant]
Expand Down
13 changes: 10 additions & 3 deletions deno_webgpu/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ impl GPUQueue {
let (sender, receiver) = oneshot::channel::<()>();

let callback = Box::new(move || {
sender.send(()).unwrap();
// The receiver is gone if the device was lost while waiting.
let _ = sender.send(());
});

self.wgpu_queue.on_submitted_work_done(callback);
Expand All @@ -88,10 +89,16 @@ impl GPUQueue {
let device_poll_fut = async move {
while !*done.borrow() {
{
self
// A failed poll means the device is gone, so the submitted work
// will never complete. The specification resolves this promise
// for a lost device; do not abort the process.
if self
.wgpu_device
.poll(wgpu_types::PollType::wait_indefinitely())
.unwrap();
.is_err()
{
return Ok(());
Comment on lines +98 to +100

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Complete the entire queue wait when polling fails

If the device is lost while submissions are outstanding, returning Ok(()) only completes device_poll_fut; the tokio::try_join! below still waits for receiver_fut. In wgpu-core/src/device/resource.rs, a HAL wait or fence-query error returns from Device::maintain before queue maintenance collects submission callbacks, and Device::lose does not invoke those callbacks either. Consequently, the sender can remain stored in the queue while this method stops polling, leaving onSubmittedWorkDone() pending indefinitely. Make the poll-error path complete the overall operation without continuing to await the callback.

Useful? React with 👍 / 👎.

}
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
Expand Down
Loading