From fc1aa76bad61d56f4c4ff27700babc4a6e2d5e7b Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Fri, 22 May 2026 10:25:48 -0700 Subject: [PATCH] fix(core): Don't use `{buffer,texture}_init_actions` for usage tracking Fixes #9576 --- wgpu-core/src/binding_model.rs | 10 ++++------ wgpu-core/src/device/resource.rs | 9 +++++---- wgpu-core/src/resource.rs | 2 ++ wgpu-core/src/track/buffer.rs | 10 +++------- wgpu-core/src/track/mod.rs | 7 +++++-- wgpu-core/src/track/texture.rs | 6 ++++++ 6 files changed, 25 insertions(+), 19 deletions(-) diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index a459dc0c0..e50b9d1b7 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -1277,13 +1277,11 @@ impl BindGroup { &'a self, guard: &'a SnatchGuard, ) -> Result<&'a dyn hal::DynBindGroup, DestroyedResourceError> { - // Clippy insist on writing it this way. The idea is to return None - // if any of the raw buffer is not valid anymore. - for buffer in &self.buffer_init_actions { - buffer.buffer.try_raw(guard)?; + for buffer in self.used.buffers.used_resources() { + buffer.try_raw(guard)?; } - for texture in &self.texture_init_actions { - texture.texture.try_raw(guard)?; + for texture in self.used.views.used_textures() { + texture.try_raw(guard)?; } self.raw diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index d08923140..399270e44 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -728,6 +728,7 @@ impl Device { /// implementation of a reference-counted structure). /// The snatch lock must not be held while this function is called. pub(crate) fn deferred_resource_destruction(&self) { + // Note that the deferred_destroy list may contain duplicate entries. let deferred_destroy = mem::take(&mut *self.deferred_destroy.lock()); for item in deferred_destroy { match item { @@ -3499,12 +3500,12 @@ impl Device { let bind_group = Arc::new(bind_group); let weak_ref = Arc::downgrade(&bind_group); - for range in &bind_group.texture_init_actions { - let mut bind_groups = range.texture.bind_groups.lock(); + for texture in bind_group.used.views.used_textures() { + let mut bind_groups = texture.bind_groups.lock(); bind_groups.push(weak_ref.clone()); } - for range in &bind_group.buffer_init_actions { - let mut bind_groups = range.buffer.bind_groups.lock(); + for buffer in bind_group.used.buffers.used_resources() { + let mut bind_groups = buffer.bind_groups.lock(); bind_groups.push(weak_ref.clone()); } diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index aa672dc26..276542ed1 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -455,6 +455,7 @@ pub struct Buffer { pub(crate) label: String, pub(crate) tracking_data: TrackingData, pub(crate) map_state: Mutex, + // Bind groups that reference this buffer. May contain duplicates. pub(crate) bind_groups: Mutex>, pub(crate) timestamp_normalization_bind_group: Snatchable, pub(crate) indirect_validation_bind_groups: Snatchable, @@ -1366,6 +1367,7 @@ pub struct Texture { pub(crate) tracking_data: TrackingData, pub(crate) clear_mode: RwLock, pub(crate) views: Mutex>, + // Bind groups that reference this texture. May contain duplicates. pub(crate) bind_groups: Mutex>, } diff --git a/wgpu-core/src/track/buffer.rs b/wgpu-core/src/track/buffer.rs index afa389abb..8fdbe8101 100644 --- a/wgpu-core/src/track/buffer.rs +++ b/wgpu-core/src/track/buffer.rs @@ -57,13 +57,9 @@ impl BufferBindGroupState { .sort_unstable_by_key(|(b, _)| b.tracker_index()); } - /// Returns a list of all buffers tracked. May contain duplicates. - pub fn used_tracker_indices(&self) -> impl Iterator + '_ { - self.buffers - .iter() - .map(|(b, _)| b.tracker_index()) - .collect::>() - .into_iter() + /// Returns an iterator over the tracked buffers. May contain duplicates. + pub fn used_resources(&self) -> impl Iterator> { + self.buffers.iter().map(|(b, _)| b) } /// Adds the given resource with the given state. diff --git a/wgpu-core/src/track/mod.rs b/wgpu-core/src/track/mod.rs index 7abb8e1a6..79701020d 100644 --- a/wgpu-core/src/track/mod.rs +++ b/wgpu-core/src/track/mod.rs @@ -106,7 +106,7 @@ use crate::{ binding_model, command, lock::{rank, Mutex}, pipeline, - resource::{self, Labeled, RawResourceAccess, ResourceErrorIdent}, + resource::{self, Labeled, RawResourceAccess, ResourceErrorIdent, Trackable}, snatch::SnatchGuard, track::blas::BlasTracker, }; @@ -697,7 +697,10 @@ impl Tracker { ) { self.buffers.set_and_remove_from_usage_scope_sparse( &mut scope.buffers, - bind_group.buffers.used_tracker_indices(), + bind_group + .buffers + .used_resources() + .map(|b| b.tracker_index()), ); self.textures .set_and_remove_from_usage_scope_sparse(&mut scope.textures, &bind_group.views); diff --git a/wgpu-core/src/track/texture.rs b/wgpu-core/src/track/texture.rs index aede04287..78f60fdb2 100644 --- a/wgpu-core/src/track/texture.rs +++ b/wgpu-core/src/track/texture.rs @@ -160,6 +160,12 @@ impl TextureViewBindGroupState { pub fn insert_single(&mut self, view: Arc, usage: TextureUses) { self.views.push((view, usage)); } + + /// Returns an iterator over the parent textures of the tracked views. May contain + /// duplicates. + pub fn used_textures(&self) -> impl Iterator> { + self.views.iter().map(|(v, _)| &v.parent) + } } /// Container for corresponding simple and complex texture states.