Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove resources ONLY when needed inside wgpu and not in user land #4782

Merged
merged 5 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tests/tests/zero_init_texture_after_discard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ 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_and_wait();

case.create_command_encoder();
case.copy_texture_to_buffer();
case.submit_command_encoder_and_wait();

Expand Down Expand Up @@ -89,7 +92,13 @@ 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.submit_command_encoder_and_wait();

case.create_command_encoder();
case.discard_stencil();
case.submit_command_encoder_and_wait();

case.create_command_encoder();
case.copy_texture_to_buffer();
case.submit_command_encoder_and_wait();

Expand Down
7 changes: 3 additions & 4 deletions wgpu-core/src/device/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2004,7 +2004,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
// Wait for all work to finish before configuring the surface.
let fence = device.fence.read();
let fence = fence.as_ref().unwrap();
match device.maintain(hub, fence, wgt::Maintain::Wait) {
match device.maintain(fence, wgt::Maintain::Wait) {
Ok((closures, _)) => {
user_callbacks = closures;
}
Expand Down Expand Up @@ -2074,7 +2074,6 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
return Err(InvalidDevice);
}
device.lock_life().triage_suspected(
hub,
&device.trackers,
#[cfg(feature = "trace")]
None,
Expand Down Expand Up @@ -2109,7 +2108,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.map_err(|_| DeviceError::Invalid)?;
let fence = device.fence.read();
let fence = fence.as_ref().unwrap();
device.maintain(hub, fence, maintain)?
device.maintain(fence, maintain)?
};

closures.fire();
Expand Down Expand Up @@ -2143,7 +2142,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
};
let fence = device.fence.read();
let fence = fence.as_ref().unwrap();
let (cbs, queue_empty) = device.maintain(hub, fence, maintain)?;
let (cbs, queue_empty) = device.maintain(fence, maintain)?;
all_queue_empty = all_queue_empty && queue_empty;

closures.extend(cbs);
Expand Down
91 changes: 5 additions & 86 deletions wgpu-core/src/device/life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ use crate::{
DeviceError, DeviceLostClosure,
},
hal_api::HalApi,
hub::Hub,
id::{
self, BindGroupId, BindGroupLayoutId, BufferId, ComputePipelineId, PipelineLayoutId,
QuerySetId, RenderBundleId, RenderPipelineId, SamplerId, StagingBufferId, TextureId,
TextureViewId,
},
pipeline::{ComputePipeline, RenderPipeline},
registry::Registry,
resource::{
self, Buffer, QuerySet, Resource, ResourceType, Sampler, StagingBuffer, Texture,
TextureView,
Expand Down Expand Up @@ -77,16 +75,6 @@ impl ResourceMaps {
self.maps.insert(R::TYPE, Box::new(map));
self
}
fn map<Id, R>(&self) -> &FastHashMap<Id, Arc<R>>
where
Id: id::TypedId,
R: Resource<Id>,
{
let map = self.maps.get(R::TYPE).unwrap();
let any_map = map.as_ref().as_any();
let map = any_map.downcast_ref::<FastHashMap<Id, Arc<R>>>().unwrap();
map
}
fn map_mut<Id, R>(&mut self) -> &mut FastHashMap<Id, Arc<R>>
where
Id: id::TypedId,
Expand Down Expand Up @@ -132,13 +120,6 @@ impl ResourceMaps {
self.map_mut().insert(id, r);
self
}
pub(crate) fn contains<Id, R>(&mut self, id: &Id) -> bool
where
Id: id::TypedId,
R: Resource<Id>,
{
self.map::<Id, R>().contains_key(id)
}
}

/// Resources used by a queue submission, and work to be done once it completes.
Expand Down Expand Up @@ -424,35 +405,27 @@ impl<A: HalApi> LifetimeTracker<A> {
}

impl<A: HalApi> LifetimeTracker<A> {
fn triage_resources<Id, R, F, T>(
fn triage_resources<Id, R, T>(
resources_map: &mut FastHashMap<Id, Arc<R>>,
active: &mut [ActiveSubmission<A>],
free_resources: &mut ResourceMaps,
trackers: &mut impl ResourceTracker<Id, R>,
registry: &Registry<Id, R>,
count_fn: F,
mut on_remove: T,
) -> Vec<Arc<R>>
where
Id: id::TypedId,
R: Resource<Id>,
F: Fn(u64, &[ActiveSubmission<A>], &Id) -> usize,
T: FnMut(&Id, &Arc<R>),
{
let mut removed_resources = Vec::new();
resources_map.retain(|&id, resource| {
let submit_index = resource.as_info().submission_index();
let mut count = 1;
count += count_fn(submit_index, active, &id);
count += registry.contains(id) as usize;

let non_referenced_resources = active
.iter_mut()
.find(|a| a.index == submit_index)
.map_or(&mut *free_resources, |a| &mut a.last_resources);
count += non_referenced_resources.contains::<Id, R>(&id) as usize;

let is_removed = trackers.remove_abandoned(id, count);
let is_removed = trackers.remove_abandoned(id);
if is_removed {
on_remove(&id, resource);
removed_resources.push(resource.clone());
Expand All @@ -465,7 +438,6 @@ impl<A: HalApi> LifetimeTracker<A> {

fn triage_suspected_render_bundles(
&mut self,
hub: &Hub<A>,
trackers: &Mutex<Tracker<A>>,
#[cfg(feature = "trace")] trace: &mut Option<&mut trace::Trace>,
) -> &mut Self {
Expand All @@ -476,8 +448,6 @@ impl<A: HalApi> LifetimeTracker<A> {
self.active.as_mut_slice(),
&mut self.free_resources,
&mut trackers.bundles,
&hub.render_bundles,
|_submit_index, _active, _id| 0,
|_bundle_id, _bundle| {
#[cfg(feature = "trace")]
if let Some(ref mut t) = *trace {
Expand Down Expand Up @@ -507,7 +477,6 @@ impl<A: HalApi> LifetimeTracker<A> {

fn triage_suspected_bind_groups(
&mut self,
hub: &Hub<A>,
trackers: &Mutex<Tracker<A>>,
#[cfg(feature = "trace")] trace: &mut Option<&mut trace::Trace>,
) -> &mut Self {
Expand All @@ -518,8 +487,6 @@ impl<A: HalApi> LifetimeTracker<A> {
self.active.as_mut_slice(),
&mut self.free_resources,
&mut trackers.bind_groups,
&hub.bind_groups,
|_submit_index, _active, _id| 0,
|_bind_group_id, _bind_group| {
#[cfg(feature = "trace")]
if let Some(ref mut t) = *trace {
Expand Down Expand Up @@ -553,7 +520,6 @@ impl<A: HalApi> LifetimeTracker<A> {

fn triage_suspected_texture_views(
&mut self,
hub: &Hub<A>,
trackers: &Mutex<Tracker<A>>,
#[cfg(feature = "trace")] trace: &mut Option<&mut trace::Trace>,
) -> &mut Self {
Expand All @@ -564,8 +530,6 @@ impl<A: HalApi> LifetimeTracker<A> {
self.active.as_mut_slice(),
&mut self.free_resources,
&mut trackers.views,
&hub.texture_views,
|_submit_index, _active, _id| 0,
|_texture_view_id, _texture_view| {
#[cfg(feature = "trace")]
if let Some(ref mut t) = *trace {
Expand All @@ -585,7 +549,6 @@ impl<A: HalApi> LifetimeTracker<A> {

fn triage_suspected_textures(
&mut self,
hub: &Hub<A>,
trackers: &Mutex<Tracker<A>>,
#[cfg(feature = "trace")] trace: &mut Option<&mut trace::Trace>,
) -> &mut Self {
Expand All @@ -596,8 +559,6 @@ impl<A: HalApi> LifetimeTracker<A> {
self.active.as_mut_slice(),
&mut self.free_resources,
&mut trackers.textures,
&hub.textures,
|_submit_index, _active, _id| 0,
|_texture_id, _texture| {
#[cfg(feature = "trace")]
if let Some(ref mut t) = *trace {
Expand All @@ -610,7 +571,6 @@ impl<A: HalApi> LifetimeTracker<A> {

fn triage_suspected_samplers(
&mut self,
hub: &Hub<A>,
trackers: &Mutex<Tracker<A>>,
#[cfg(feature = "trace")] trace: &mut Option<&mut trace::Trace>,
) -> &mut Self {
Expand All @@ -621,8 +581,6 @@ impl<A: HalApi> LifetimeTracker<A> {
self.active.as_mut_slice(),
&mut self.free_resources,
&mut trackers.samplers,
&hub.samplers,
|_submit_index, _active, _id| 0,
|_sampler_id, _sampler| {
#[cfg(feature = "trace")]
if let Some(ref mut t) = *trace {
Expand All @@ -635,7 +593,6 @@ impl<A: HalApi> LifetimeTracker<A> {

fn triage_suspected_buffers(
&mut self,
hub: &Hub<A>,
trackers: &Mutex<Tracker<A>>,
#[cfg(feature = "trace")] trace: &mut Option<&mut trace::Trace>,
) -> &mut Self {
Expand All @@ -646,20 +603,6 @@ impl<A: HalApi> LifetimeTracker<A> {
self.active.as_mut_slice(),
&mut self.free_resources,
&mut trackers.buffers,
&hub.buffers,
|submit_index, active, buffer_id| {
let mut count = 0;
let mapped = active
.iter()
.find(|a| a.index == submit_index)
.map_or(&self.mapped, |a| &a.mapped);
mapped.iter().for_each(|b| {
if b.as_info().id() == *buffer_id {
count += 1;
}
});
count
},
|_buffer_id, _buffer| {
#[cfg(feature = "trace")]
if let Some(ref mut t) = *trace {
Expand All @@ -681,7 +624,6 @@ impl<A: HalApi> LifetimeTracker<A> {

fn triage_suspected_compute_pipelines(
&mut self,
hub: &Hub<A>,
trackers: &Mutex<Tracker<A>>,
#[cfg(feature = "trace")] trace: &mut Option<&mut trace::Trace>,
) -> &mut Self {
Expand All @@ -692,8 +634,6 @@ impl<A: HalApi> LifetimeTracker<A> {
self.active.as_mut_slice(),
&mut self.free_resources,
&mut trackers.compute_pipelines,
&hub.compute_pipelines,
|_submit_index, _active, _id| 0,
|_compute_pipeline_id, _compute_pipeline| {
#[cfg(feature = "trace")]
if let Some(ref mut t) = *trace {
Expand All @@ -712,7 +652,6 @@ impl<A: HalApi> LifetimeTracker<A> {

fn triage_suspected_render_pipelines(
&mut self,
hub: &Hub<A>,
trackers: &Mutex<Tracker<A>>,
#[cfg(feature = "trace")] trace: &mut Option<&mut trace::Trace>,
) -> &mut Self {
Expand All @@ -723,8 +662,6 @@ impl<A: HalApi> LifetimeTracker<A> {
self.active.as_mut_slice(),
&mut self.free_resources,
&mut trackers.render_pipelines,
&hub.render_pipelines,
|_submit_index, _active, _id| 0,
|_render_pipeline_id, _render_pipeline| {
#[cfg(feature = "trace")]
if let Some(ref mut t) = *trace {
Expand Down Expand Up @@ -787,20 +724,14 @@ impl<A: HalApi> LifetimeTracker<A> {
self
}

fn triage_suspected_query_sets(
&mut self,
hub: &Hub<A>,
trackers: &Mutex<Tracker<A>>,
) -> &mut Self {
fn triage_suspected_query_sets(&mut self, trackers: &Mutex<Tracker<A>>) -> &mut Self {
let mut trackers = trackers.lock();
let resource_map = self.suspected_resources.map_mut();
Self::triage_resources(
resource_map,
self.active.as_mut_slice(),
&mut self.free_resources,
&mut trackers.query_sets,
&hub.query_sets,
|_submit_index, _active, _id| 0,
|_query_set_id, _query_set| {},
);
self
Expand Down Expand Up @@ -858,33 +789,28 @@ impl<A: HalApi> LifetimeTracker<A> {
/// [`self.free_resources`]: LifetimeTracker::free_resources
pub(crate) fn triage_suspected(
&mut self,
hub: &Hub<A>,
trackers: &Mutex<Tracker<A>>,
#[cfg(feature = "trace")] mut trace: Option<&mut trace::Trace>,
) {
profiling::scope!("triage_suspected");

//NOTE: the order is important to release resources that depends between each other!
self.triage_suspected_render_bundles(
hub,
trackers,
#[cfg(feature = "trace")]
&mut trace,
);
self.triage_suspected_compute_pipelines(
hub,
trackers,
#[cfg(feature = "trace")]
&mut trace,
);
self.triage_suspected_render_pipelines(
hub,
trackers,
#[cfg(feature = "trace")]
&mut trace,
);
self.triage_suspected_bind_groups(
hub,
trackers,
#[cfg(feature = "trace")]
&mut trace,
Expand All @@ -897,28 +823,24 @@ impl<A: HalApi> LifetimeTracker<A> {
#[cfg(feature = "trace")]
&mut trace,
);
self.triage_suspected_query_sets(hub, trackers);
self.triage_suspected_query_sets(trackers);
self.triage_suspected_samplers(
hub,
trackers,
#[cfg(feature = "trace")]
&mut trace,
);
self.triage_suspected_staging_buffers();
self.triage_suspected_texture_views(
hub,
trackers,
#[cfg(feature = "trace")]
&mut trace,
);
self.triage_suspected_textures(
hub,
trackers,
#[cfg(feature = "trace")]
&mut trace,
);
self.triage_suspected_buffers(
hub,
trackers,
#[cfg(feature = "trace")]
&mut trace,
Expand Down Expand Up @@ -959,7 +881,6 @@ impl<A: HalApi> LifetimeTracker<A> {
#[must_use]
pub(crate) fn handle_mapping(
&mut self,
hub: &Hub<A>,
raw: &A::Device,
trackers: &Mutex<Tracker<A>>,
) -> Vec<super::BufferMapPendingClosure> {
Expand All @@ -973,9 +894,7 @@ impl<A: HalApi> LifetimeTracker<A> {
let buffer_id = buffer.info.id();
let is_removed = {
let mut trackers = trackers.lock();
let mut count = 1;
count += hub.buffers.contains(buffer_id) as usize;
trackers.buffers.remove_abandoned(buffer_id, count)
trackers.buffers.remove_abandoned(buffer_id)
};
if is_removed {
*buffer.map_state.lock() = resource::BufferMapState::Idle;
Expand Down
Loading
Loading