From efb35d4fa1059a514942d375fa5239c6d27cf35a Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Thu, 25 Jan 2024 06:11:43 +0100 Subject: [PATCH] Snatch bind groups associated with destroyed textures and buffers (#5136) * Make bind groups snatchable * Snatch bindgroups when destroying associated textures and buffers. --- .github/workflows/ci.yml | 4 ++- wgpu-core/src/binding_model.rs | 6 ++--- wgpu-core/src/device/global.rs | 9 +++++++ wgpu-core/src/device/resource.rs | 5 +++- wgpu-core/src/present.rs | 1 + wgpu-core/src/resource.rs | 44 ++++++++++++++++++++++++++++++++ 6 files changed, 64 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6bb8d3824..df275a43a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -76,7 +76,9 @@ env: jobs: check: # runtime is normally 2-8 minutes - timeout-minutes: 15 + # + # currently high due to documentation time problems on mac. + timeout-minutes: 30 strategy: fail-fast: false diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index 39a21f7a1..833920ea3 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -13,7 +13,7 @@ use crate::{ init_tracker::{BufferInitTrackerAction, TextureInitTrackerAction}, resource::{Resource, ResourceInfo, ResourceType}, resource_log, - snatch::SnatchGuard, + snatch::{SnatchGuard, Snatchable}, track::{BindGroupStates, UsageConflict}, validation::{MissingBufferUsageError, MissingTextureUsageError}, Label, @@ -833,7 +833,7 @@ pub(crate) fn buffer_binding_type_alignment( #[derive(Debug)] pub struct BindGroup { - pub(crate) raw: Option, + pub(crate) raw: Snatchable, pub(crate) device: Arc>, pub(crate) layout: Arc>, pub(crate) info: ResourceInfo, @@ -874,7 +874,7 @@ impl BindGroup { for texture in &self.used_texture_ranges { let _ = texture.texture.raw(guard)?; } - self.raw.as_ref() + self.raw.get(guard) } pub(crate) fn validate_dynamic_bindings( &self, diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index 9435993ea..1872159fc 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -1130,6 +1130,15 @@ impl Global { }; let (id, resource) = fid.assign(bind_group); + + let weak_ref = Arc::downgrade(&resource); + for range in &resource.used_texture_ranges { + range.texture.bind_groups.lock().push(weak_ref.clone()); + } + for range in &resource.used_buffer_ranges { + range.buffer.bind_groups.lock().push(weak_ref.clone()); + } + api_log!("Device::create_bind_group -> {id:?}"); device diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 68f5b6d0c..06aedc00f 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -566,6 +566,7 @@ impl Device { sync_mapped_writes: Mutex::new(None), map_state: Mutex::new(resource::BufferMapState::Idle), info: ResourceInfo::new(desc.label.borrow_or_default()), + bind_groups: Mutex::new(Vec::new()), }) } @@ -596,6 +597,7 @@ impl Device { info: ResourceInfo::new(desc.label.borrow_or_default()), clear_mode: RwLock::new(clear_mode), views: Mutex::new(Vec::new()), + bind_groups: Mutex::new(Vec::new()), } } @@ -615,6 +617,7 @@ impl Device { sync_mapped_writes: Mutex::new(None), map_state: Mutex::new(resource::BufferMapState::Idle), info: ResourceInfo::new(desc.label.borrow_or_default()), + bind_groups: Mutex::new(Vec::new()), } } @@ -2199,7 +2202,7 @@ impl Device { }; Ok(binding_model::BindGroup { - raw: Some(raw), + raw: Snatchable::new(raw), device: self.clone(), layout: layout.clone(), info: ResourceInfo::new(desc.label.borrow_or_default()), diff --git a/wgpu-core/src/present.rs b/wgpu-core/src/present.rs index 44b741b8c..00a2953ea 100644 --- a/wgpu-core/src/present.rs +++ b/wgpu-core/src/present.rs @@ -232,6 +232,7 @@ impl Global { clear_view: Some(clear_view), }), views: Mutex::new(Vec::new()), + bind_groups: Mutex::new(Vec::new()), }; let (id, resource) = fid.assign(texture); diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index ceab4b232..b08216eee 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -1,6 +1,7 @@ #[cfg(feature = "trace")] use crate::device::trace; use crate::{ + binding_model::BindGroup, device::{ queue, BufferMapPendingClosure, Device, DeviceError, HostMap, MissingDownlevelFlags, MissingFeatures, @@ -378,6 +379,7 @@ pub struct Buffer { pub(crate) sync_mapped_writes: Mutex>, pub(crate) info: ResourceInfo, pub(crate) map_state: Mutex>, + pub(crate) bind_groups: Mutex>>>, } impl Drop for Buffer { @@ -541,12 +543,18 @@ impl Buffer { } }; + let bind_groups = { + let mut guard = self.bind_groups.lock(); + std::mem::take(&mut *guard) + }; + queue::TempResource::DestroyedBuffer(Arc::new(DestroyedBuffer { raw: Some(raw), device: Arc::clone(&self.device), submission_index: self.info.submission_index(), id: self.info.id.unwrap(), label: self.info.label.clone(), + bind_groups, })) }; @@ -596,6 +604,29 @@ impl Resource for Buffer { } } +fn snatch_and_destroy_bind_groups( + device: &Device, + bind_groups: &[Weak>], +) { + for bind_group in bind_groups { + if let Some(bind_group) = bind_group.upgrade() { + if let Some(raw_bind_group) = bind_group.raw.snatch(device.snatchable_lock.write()) { + resource_log!("Destroy raw BindGroup (destroyed) {:?}", bind_group.label()); + + #[cfg(feature = "trace")] + if let Some(t) = device.trace.lock().as_mut() { + t.add(trace::Action::DestroyBindGroup(bind_group.info.id())); + } + + unsafe { + use hal::Device; + device.raw().destroy_bind_group(raw_bind_group); + } + } + } + } +} + /// A buffer that has been marked as destroyed and is staged for actual deletion soon. #[derive(Debug)] pub struct DestroyedBuffer { @@ -604,6 +635,7 @@ pub struct DestroyedBuffer { label: String, pub(crate) id: BufferId, pub(crate) submission_index: u64, + bind_groups: Vec>>, } impl DestroyedBuffer { @@ -618,6 +650,8 @@ impl DestroyedBuffer { impl Drop for DestroyedBuffer { fn drop(&mut self) { + snatch_and_destroy_bind_groups(&self.device, &self.bind_groups); + if let Some(raw) = self.raw.take() { resource_log!("Destroy raw Buffer (destroyed) {:?}", self.label()); @@ -742,6 +776,7 @@ pub struct Texture { pub(crate) info: ResourceInfo, pub(crate) clear_mode: RwLock>, pub(crate) views: Mutex>>>, + pub(crate) bind_groups: Mutex>>>, } impl Drop for Texture { @@ -858,9 +893,15 @@ impl Texture { std::mem::take(&mut *guard) }; + let bind_groups = { + let mut guard = self.bind_groups.lock(); + std::mem::take(&mut *guard) + }; + queue::TempResource::DestroyedTexture(Arc::new(DestroyedTexture { raw: Some(raw), views, + bind_groups, device: Arc::clone(&self.device), submission_index: self.info.submission_index(), id: self.info.id.unwrap(), @@ -978,6 +1019,7 @@ impl Global { pub struct DestroyedTexture { raw: Option, views: Vec>>, + bind_groups: Vec>>, device: Arc>, label: String, pub(crate) id: TextureId, @@ -997,6 +1039,8 @@ impl DestroyedTexture { impl Drop for DestroyedTexture { fn drop(&mut self) { let device = &self.device; + snatch_and_destroy_bind_groups(device, &self.bind_groups); + for view in self.views.drain(..) { if let Some(view) = view.upgrade() { if let Some(raw_view) = view.raw.snatch(device.snatchable_lock.write()) {