From b6d61da6ce098741ff1991f0c42a08c29d87eac3 Mon Sep 17 00:00:00 2001 From: gents83 Date: Mon, 13 Nov 2023 19:32:34 +0100 Subject: [PATCH] Fix insert_impl and take_and_mark_destroyed --- tests/tests/zero_init_texture_after_discard.rs | 16 ++++++---------- wgpu-core/src/identity.rs | 4 ++-- wgpu-core/src/registry.rs | 10 ++++++---- wgpu-core/src/storage.rs | 13 +++++++++---- wgpu/src/backend/direct.rs | 4 ---- 5 files changed, 23 insertions(+), 24 deletions(-) diff --git a/tests/tests/zero_init_texture_after_discard.rs b/tests/tests/zero_init_texture_after_discard.rs index b041d470c0..3c0561343c 100644 --- a/tests/tests/zero_init_texture_after_discard.rs +++ b/tests/tests/zero_init_texture_after_discard.rs @@ -13,11 +13,8 @@ static DISCARDING_COLOR_TARGET_RESETS_TEXTURE_INIT_STATE_CHECK_VISIBLE_ON_COPY_A let mut case = TestCase::new(&mut ctx, TextureFormat::Rgba8UnormSrgb); case.create_command_encoder(); case.discard(); - case.submit_command_encoder(); - - case.create_command_encoder(); case.copy_texture_to_buffer(); - case.submit_command_encoder(); + case.submit_command_encoder_and_wait(); case.assert_buffers_are_zero(); }); @@ -31,7 +28,7 @@ static DISCARDING_COLOR_TARGET_RESETS_TEXTURE_INIT_STATE_CHECK_VISIBLE_ON_COPY_I case.create_command_encoder(); case.discard(); case.copy_texture_to_buffer(); - case.submit_command_encoder(); + case.submit_command_encoder_and_wait(); case.assert_buffers_are_zero(); }); @@ -58,7 +55,7 @@ static DISCARDING_DEPTH_TARGET_RESETS_TEXTURE_INIT_STATE_CHECK_VISIBLE_ON_COPY_I case.create_command_encoder(); case.discard(); case.copy_texture_to_buffer(); - case.submit_command_encoder(); + case.submit_command_encoder_and_wait(); case.assert_buffers_are_zero(); } @@ -85,12 +82,10 @@ static DISCARDING_EITHER_DEPTH_OR_STENCIL_ASPECT_TEST: GpuTestConfiguration = ] { let mut case = TestCase::new(&mut ctx, format); case.create_command_encoder(); - case.discard_depth(); case.discard_stencil(); case.copy_texture_to_buffer(); - - case.submit_command_encoder(); + case.submit_command_encoder_and_wait(); case.assert_buffers_are_zero(); } @@ -208,10 +203,11 @@ impl<'ctx> TestCase<'ctx> { ) } - pub fn submit_command_encoder(&mut self) { + pub fn submit_command_encoder_and_wait(&mut self) { self.ctx .queue .submit([self.encoder.take().unwrap().finish()]); + self.ctx.device.poll(MaintainBase::Wait); } pub fn discard(&mut self) { diff --git a/wgpu-core/src/identity.rs b/wgpu-core/src/identity.rs index ae83a905c3..3f421dd697 100644 --- a/wgpu-core/src/identity.rs +++ b/wgpu-core/src/identity.rs @@ -53,7 +53,7 @@ impl IdentityValues { Some((index, epoch)) => I::zip(index, epoch + 1, backend), None => { let epoch = 1; - let used = self.used.entry(epoch).or_default(); + let used = self.used.entry(epoch).or_insert_with(Default::default); let index = if let Some(i) = used.iter().max_by_key(|v| *v) { i + 1 } else { @@ -68,7 +68,7 @@ impl IdentityValues { pub fn mark_as_used(&mut self, id: I) -> I { self.count += 1; let (index, epoch, _backend) = id.unzip(); - let used = self.used.entry(epoch).or_default(); + let used = self.used.entry(epoch).or_insert_with(Default::default); used.push(index); id } diff --git a/wgpu-core/src/registry.rs b/wgpu-core/src/registry.rs index 17d538c494..6c86b275b0 100644 --- a/wgpu-core/src/registry.rs +++ b/wgpu-core/src/registry.rs @@ -81,14 +81,16 @@ impl> FutureId<'_, I, T> { } pub fn assign(self, value: T) -> (I, Arc) { - self.data.write().insert(self.id, self.init(value)); - (self.id, self.data.read().get(self.id).unwrap().clone()) + let mut data = self.data.write(); + data.insert(self.id, self.init(value)); + (self.id, data.get(self.id).unwrap().clone()) } pub fn assign_existing(self, value: &Arc) -> I { + let mut data = self.data.write(); #[cfg(debug_assertions)] - debug_assert!(!self.data.read().contains(self.id)); - self.data.write().insert(self.id, value.clone()); + debug_assert!(!data.contains(self.id)); + data.insert(self.id, value.clone()); self.id } diff --git a/wgpu-core/src/storage.rs b/wgpu-core/src/storage.rs index a292403dcb..c3fc899113 100644 --- a/wgpu-core/src/storage.rs +++ b/wgpu-core/src/storage.rs @@ -130,26 +130,31 @@ where } } - fn insert_impl(&mut self, index: usize, element: Element) { + fn insert_impl(&mut self, index: usize, epoch: Epoch, element: Element) { if index >= self.map.len() { self.map.resize_with(index + 1, || Element::Vacant); } match std::mem::replace(&mut self.map[index], element) { Element::Vacant => {} - _ => panic!("Index {index:?} is already occupied"), + Element::Occupied(_, storage_epoch) => { + assert_ne!(epoch, storage_epoch, "Index {index:?} of {} is already occupied", T::TYPE); + } + Element::Error(storage_epoch, _) => { + assert_ne!(epoch, storage_epoch, "Index {index:?} of {} is already occupied with Error", T::TYPE); + } } } pub(crate) fn insert(&mut self, id: I, value: Arc) { log::info!("User is inserting {}{:?}", T::TYPE, id); let (index, epoch, _backend) = id.unzip(); - self.insert_impl(index as usize, Element::Occupied(value, epoch)) + self.insert_impl(index as usize, epoch, Element::Occupied(value, epoch)) } pub(crate) fn insert_error(&mut self, id: I, label: &str) { log::info!("User is insering as error {}{:?}", T::TYPE, id); let (index, epoch, _) = id.unzip(); - self.insert_impl(index as usize, Element::Error(epoch, label.to_string())) + self.insert_impl(index as usize, epoch, Element::Error(epoch, label.to_string())) } pub(crate) fn take_and_mark_destroyed(&mut self, id: I) -> Result, InvalidId> { diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index f0c550fea3..147322b66b 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -2352,10 +2352,6 @@ impl crate::Context for Context { Err(err) => self.handle_error_fatal(err, "Queue::submit"), }; - for cmdbuf in &temp_command_buffers { - wgc::gfx_select!(*queue => global.command_buffer_drop(*cmdbuf)); - } - (Unused, index) }