diff --git a/tests/tests/device.rs b/tests/tests/device.rs index 87935c831..edcb3132e 100644 --- a/tests/tests/device.rs +++ b/tests/tests/device.rs @@ -625,56 +625,6 @@ static DEVICE_DESTROY_THEN_LOST: GpuTestConfiguration = GpuTestConfiguration::ne ); }); -#[gpu_test] -static DEVICE_DROP_THEN_LOST: GpuTestConfiguration = GpuTestConfiguration::new() - .parameters(TestParameters::default().expect_fail(FailureCase::webgl2())) - .run_sync(|ctx| { - // This test checks that when the device is dropped (such as in a GC), - // the provided DeviceLostClosure is called with reason DeviceLostReason::Dropped. - // Fails on webgl because webgl doesn't implement drop. - static WAS_CALLED: std::sync::atomic::AtomicBool = AtomicBool::new(false); - - // Set a LoseDeviceCallback on the device. - let callback = Box::new(|reason, message| { - WAS_CALLED.store(true, std::sync::atomic::Ordering::SeqCst); - assert_eq!(reason, wgt::DeviceLostReason::Dropped); - assert_eq!(message, "Device dropped."); - }); - ctx.device.set_device_lost_callback(callback); - - drop(ctx); - - assert!( - WAS_CALLED.load(std::sync::atomic::Ordering::SeqCst), - "Device lost callback should have been called." - ); - }); - -#[gpu_test] -static DEVICE_LOST_REPLACED_CALLBACK: GpuTestConfiguration = GpuTestConfiguration::new() - .parameters(TestParameters::default()) - .run_sync(|ctx| { - // This test checks that a device_lost_callback is called when it is - // replaced by another callback. - static WAS_CALLED: AtomicBool = AtomicBool::new(false); - - // Set a LoseDeviceCallback on the device. - let callback = Box::new(|reason, _m| { - WAS_CALLED.store(true, std::sync::atomic::Ordering::SeqCst); - assert_eq!(reason, wgt::DeviceLostReason::ReplacedCallback); - }); - ctx.device.set_device_lost_callback(callback); - - // Replace the callback. - let replacement_callback = Box::new(move |_r, _m| {}); - ctx.device.set_device_lost_callback(replacement_callback); - - assert!( - WAS_CALLED.load(std::sync::atomic::Ordering::SeqCst), - "Device lost callback should have been called." - ); - }); - #[gpu_test] static DIFFERENT_BGL_ORDER_BW_SHADER_AND_API: GpuTestConfiguration = GpuTestConfiguration::new() .parameters(TestParameters::default()) diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index 42881f1bd..358c77ff4 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -8,7 +8,7 @@ use crate::{ }, command::{self, CommandBuffer}, conv, - device::{bgl, life::WaitIdleError, DeviceError, DeviceLostClosure, DeviceLostReason}, + device::{bgl, life::WaitIdleError, DeviceError, DeviceLostClosure}, global::Global, hal_api::HalApi, id::{self, AdapterId, DeviceId, QueueId, SurfaceId}, @@ -2083,8 +2083,7 @@ impl Global { self.hub.devices.remove(device_id); } - // This closure will be called exactly once during "lose the device", - // or when it is replaced. + /// This closure will be called exactly once during "lose the device". pub fn device_set_device_lost_closure( &self, device_id: DeviceId, @@ -2092,22 +2091,15 @@ impl Global { ) { let device = self.hub.devices.get(device_id); - let old_device_lost_closure = device + device .device_lost_closure .lock() .replace(device_lost_closure); - - if let Some(old_device_lost_closure) = old_device_lost_closure { - old_device_lost_closure.call(DeviceLostReason::ReplacedCallback, "".to_string()); - } } pub fn device_unregister_device_lost_closure(&self, device_id: DeviceId) { let device = self.hub.devices.get(device_id); - let closure = device.device_lost_closure.lock().take(); - if let Some(closure) = closure { - closure.call(DeviceLostReason::ReplacedCallback, "".to_string()); - } + device.device_lost_closure.lock().take(); } pub fn device_destroy(&self, device_id: DeviceId) { diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 618cfb6ea..c9db29c43 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -149,10 +149,6 @@ impl Drop for Device { fn drop(&mut self) { resource_log!("Drop {}", self.error_ident()); - if let Some(closure) = self.device_lost_closure.lock().take() { - closure.call(DeviceLostReason::Dropped, String::from("Device dropped.")); - } - // SAFETY: We are in the Drop impl and we don't use self.zero_buffer anymore after this point. let zero_buffer = unsafe { ManuallyDrop::take(&mut self.zero_buffer) }; // SAFETY: We are in the Drop impl and we don't use self.fence anymore after this point. diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index 1d5f6ef2d..d1f9d4fef 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -7673,18 +7673,4 @@ pub enum DeviceLostReason { Unknown = 0, /// After Device::destroy Destroyed = 1, - /// After Device::drop - /// - /// WebGPU does not invoke the device lost callback when the device is - /// dropped to prevent garbage collection from being observable. In wgpu, - /// we invoke the callback on drop to help with managing memory owned by - /// the callback. - Dropped = 2, - /// After replacing the device_lost_callback - /// - /// WebGPU does not have a concept of a device lost callback, but wgpu - /// does. wgpu guarantees that any supplied callback will be invoked - /// exactly once before it is dropped, which helps with managing the - /// memory owned by the callback. - ReplacedCallback = 3, }