Skip to content

Commit

Permalink
WIP: feat(core): impl. destroy for QuerySets
Browse files Browse the repository at this point in the history
TODOs:

- Actually implement the destruction properly.
- Need to add to `wgpu::QuerySet` dtor.?
  • Loading branch information
ErichDonGubler committed Jan 11, 2025
1 parent 4efc992 commit f98127c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
19 changes: 19 additions & 0 deletions wgpu-core/src/device/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,25 @@ impl Global {
(id, Some(error))
}

pub fn query_set_destroy(
&self,
query_set_id: id::QuerySetId,
) -> Result<(), resource::DestroyError> {
profiling::scope!("QuerySet::destroy");
api_log!("QuerySet::destroy {query_set_id:?}");

let hub = &self.hub;

let query_set = hub.query_sets.get(query_set_id).get()?;

#[cfg(feature = "trace")]
if let Some(trace) = query_set.device.trace.lock().as_mut() {
trace.add(trace::Action::FreeQuerySet(query_set_id));
}

query_set.destroy()
}

pub fn query_set_drop(&self, query_set_id: id::QuerySetId) {
profiling::scope!("QuerySet::drop");
api_log!("QuerySet::drop {query_set_id:?}");
Expand Down
1 change: 1 addition & 0 deletions wgpu-core/src/device/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub enum Action<'a> {
id: id::QuerySetId,
desc: crate::resource::QuerySetDescriptor<'a>,
},
FreeQuerySet(id::QuerySetId),
DestroyQuerySet(id::QuerySetId),
WriteBuffer {
id: id::BufferId,
Expand Down
49 changes: 49 additions & 0 deletions wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,55 @@ impl QuerySet {
pub(crate) fn raw(&self) -> &dyn hal::DynQuerySet {
self.raw.as_ref()
}

pub(crate) fn destroy(self: &Arc<Self>) -> Result<(), DestroyError> {
let device = &self.device;

let temp = {
let mut snatch_guard = device.snatchable_lock.write();

let raw = match self.raw.snatch(&mut snatch_guard) {
Some(raw) => raw,
None => {
return Err(DestroyError::AlreadyDestroyed);
}
};

#[cfg(feature = "indirect-validation")]
let raw_indirect_validation_bind_group = self
.raw_indirect_validation_bind_group
.snatch(&mut snatch_guard);

drop(snatch_guard);

let bind_groups = {
let mut guard = self.bind_groups.lock();
mem::take(&mut *guard)
};

queue::TempResource::DestroyedBuffer(DestroyedBuffer {
raw: ManuallyDrop::new(raw),
device: Arc::clone(&self.device),
label: self.label().to_owned(),
bind_groups,
#[cfg(feature = "indirect-validation")]
raw_indirect_validation_bind_group,
})
};

let mut pending_writes = device.pending_writes.lock();
if pending_writes.contains_query_set(self) {
pending_writes.consume_temp(temp);
} else {
let mut life_lock = device.lock_life();
let last_submit_index = life_lock.get_query_set_last_index(self);
if let Some(last_submit_index) = last_submit_index {
life_lock.schedule_resource_destruction(temp, last_submit_index);
}
}

Ok(())
}
}

#[derive(Clone, Debug, Error)]
Expand Down

0 comments on commit f98127c

Please sign in to comment.