Skip to content

Commit

Permalink
Fix insert_impl and take_and_mark_destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
gents83 committed Nov 13, 2023
1 parent 715bd00 commit b6d61da
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
16 changes: 6 additions & 10 deletions tests/tests/zero_init_texture_after_discard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -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();
});
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions wgpu-core/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -68,7 +68,7 @@ impl IdentityValues {
pub fn mark_as_used<I: id::TypedId>(&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
}
Expand Down
10 changes: 6 additions & 4 deletions wgpu-core/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,16 @@ impl<I: id::TypedId + Copy, T: Resource<I>> FutureId<'_, I, T> {
}

pub fn assign(self, value: T) -> (I, Arc<T>) {
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<T>) -> 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
}

Expand Down
13 changes: 9 additions & 4 deletions wgpu-core/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,26 +130,31 @@ where
}
}

fn insert_impl(&mut self, index: usize, element: Element<T>) {
fn insert_impl(&mut self, index: usize, epoch: Epoch, element: Element<T>) {
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<T>) {
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<Arc<T>, InvalidId> {
Expand Down
4 changes: 0 additions & 4 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit b6d61da

Please sign in to comment.