From b34219ca214caea2af86dbb64a2a2b613ea5198c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E7=A5=A5=E7=85=9C?= Date: Tue, 26 Mar 2024 23:12:50 +0800 Subject: [PATCH] Implement the `device_set_device_lost_callback` method for `ContextWebGpu` (#5438) * impl device_set_device_lost_callback for ContextWebGpu * merge changelog --------- Co-authored-by: lixiangyu.ava --- CHANGELOG.md | 1 + wgpu/src/backend/webgpu.rs | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24d61fce9c..b98734c5e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -121,6 +121,7 @@ Bottom level categories: #### WebGPU +- Implement the `device_set_device_lost_callback` method for `ContextWebGpu`. By @suti in [#5438](https://github.com/gfx-rs/wgpu/pull/5438) - Add support for storage texture access modes `ReadOnly` and `ReadWrite`. By @JolifantoBambla in [#5434](https://github.com/gfx-rs/wgpu/pull/5434) ### Bug Fixes diff --git a/wgpu/src/backend/webgpu.rs b/wgpu/src/backend/webgpu.rs index 52a4d2931e..acd3561461 100644 --- a/wgpu/src/backend/webgpu.rs +++ b/wgpu/src/backend/webgpu.rs @@ -1979,10 +1979,23 @@ impl crate::context::Context for ContextWebGpu { fn device_set_device_lost_callback( &self, _device: &Self::DeviceId, - _device_data: &Self::DeviceData, - _device_lost_callback: crate::context::DeviceLostCallback, + device_data: &Self::DeviceData, + device_lost_callback: crate::context::DeviceLostCallback, ) { - unimplemented!(); + use webgpu_sys::{GpuDeviceLostInfo, GpuDeviceLostReason}; + + let closure = Closure::once(move |info: JsValue| { + let info = info.dyn_into::().unwrap(); + device_lost_callback( + match info.reason() { + GpuDeviceLostReason::Destroyed => crate::DeviceLostReason::Destroyed, + GpuDeviceLostReason::Unknown => crate::DeviceLostReason::Unknown, + _ => crate::DeviceLostReason::Unknown, + }, + info.message(), + ); + }); + let _ = device_data.0.lost().then(&closure); } fn device_poll(