From 82210e1cdc63cbd5e53f43788f9956bb0d4a2c6a Mon Sep 17 00:00:00 2001 From: XiaoPeng Date: Tue, 25 Jun 2024 22:09:52 +0800 Subject: [PATCH] remove stale weak refs before insert view/bind_groups --- wgpu-core/src/device/global.rs | 18 ++++++++++++++++-- wgpu-core/src/device/life.rs | 28 +--------------------------- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index 3138ccf8f..1b3b89231 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -841,6 +841,10 @@ impl Global { { let mut views = texture.views.lock(); + + // Remove stale weak references + views.retain(|view| view.strong_count() > 0); + views.push(Arc::downgrade(&resource)); } @@ -1162,10 +1166,20 @@ impl Global { let weak_ref = Arc::downgrade(&resource); for range in &resource.used_texture_ranges { - range.texture.bind_groups.lock().push(weak_ref.clone()); + let mut bind_groups = range.texture.bind_groups.lock(); + + // Remove stale weak references + bind_groups.retain(|bg| bg.strong_count() > 0); + + bind_groups.push(weak_ref.clone()); } for range in &resource.used_buffer_ranges { - range.buffer.bind_groups.lock().push(weak_ref.clone()); + let mut bind_groups = range.buffer.bind_groups.lock(); + + // Remove stale weak references + bind_groups.retain(|bg| bg.strong_count() > 0); + + bind_groups.push(weak_ref.clone()); } api_log!("Device::create_bind_group -> {id:?}"); diff --git a/wgpu-core/src/device/life.rs b/wgpu-core/src/device/life.rs index 73f1e2ffc..c77063a02 100644 --- a/wgpu-core/src/device/life.rs +++ b/wgpu-core/src/device/life.rs @@ -595,7 +595,7 @@ impl LifetimeTracker { fn triage_suspected_texture_views(&mut self, trackers: &Mutex>) -> &mut Self { let mut trackers = trackers.lock(); let suspected_texture_views = &mut self.suspected_resources.texture_views; - let removed_views = Self::triage_resources( + Self::triage_resources( suspected_texture_views, self.active.as_mut_slice(), &mut trackers.views, @@ -607,12 +607,6 @@ impl LifetimeTracker { // `LifetimeTracker::suspected_resources` it remains there until it's // actually dropped, which for long-lived textures could be at the end // of execution. - for view in removed_views { - view.parent - .views - .lock() - .retain(|view| view.strong_count() > 1); - } self } @@ -625,18 +619,6 @@ impl LifetimeTracker { &mut trackers.textures, |maps| &mut maps.textures, ); - - // We may have been suspected because a texture view or bind group - // referring to us was dropped. Remove stale weak references, so that - // the backlink table doesn't grow without bound. - for texture in self.suspected_resources.textures.values() { - texture.views.lock().retain(|view| view.strong_count() > 0); - texture - .bind_groups - .lock() - .retain(|bg| bg.strong_count() > 0); - } - self } @@ -661,14 +643,6 @@ impl LifetimeTracker { &mut trackers.buffers, |maps| &mut maps.buffers, ); - - // We may have been suspected because a bind group referring to us was - // dropped. Remove stale weak references, so that the backlink table - // doesn't grow without bound. - for buffer in self.suspected_resources.buffers.values() { - buffer.bind_groups.lock().retain(|bg| bg.strong_count() > 0); - } - self }