Skip to content

Commit b30c0c2

Browse files
authored
Remove the free_resources tracker (#5037)
1 parent c90f43b commit b30c0c2

File tree

2 files changed

+35
-96
lines changed

2 files changed

+35
-96
lines changed

wgpu-core/src/device/life.rs

Lines changed: 35 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,8 @@ struct ActiveSubmission<A: HalApi> {
142142
/// Resources to be freed once this queue submission has completed.
143143
///
144144
/// When the device is polled, for completed submissions,
145-
/// `triage_submissions` merges these into
146-
/// `LifetimeTracker::free_resources`. From there,
147-
/// `LifetimeTracker::cleanup` passes them to the hal to be freed.
145+
/// `triage_submissions` removes resources that don't need to be held alive any longer
146+
/// from there.
148147
///
149148
/// This includes things like temporary resources and resources that are
150149
/// used by submitted commands but have been dropped by the user (meaning that
@@ -205,8 +204,6 @@ pub enum WaitIdleError {
205204
/// buffers that were dropped by the user get moved to
206205
/// `self.free_resources`.
207206
///
208-
/// 4) `cleanup` frees everything in `free_resources`.
209-
///
210207
/// Only calling `Global::buffer_map_async` clones a new `Arc` for the
211208
/// buffer. This new `Arc` is only dropped by `handle_mapping`.
212209
pub(crate) struct LifetimeTracker<A: HalApi> {
@@ -233,13 +230,6 @@ pub(crate) struct LifetimeTracker<A: HalApi> {
233230
/// to particular entries.
234231
active: Vec<ActiveSubmission<A>>,
235232

236-
/// Raw backend resources that are neither referenced nor used.
237-
///
238-
/// These are freed by `LifeTracker::cleanup`, which is called from periodic
239-
/// maintenance functions like `Global::device_poll`, and when a device is
240-
/// destroyed.
241-
free_resources: ResourceMaps<A>,
242-
243233
/// Buffers the user has asked us to map, and which are not used by any
244234
/// queue submission still in flight.
245235
ready_to_map: Vec<Arc<Buffer<A>>>,
@@ -264,7 +254,6 @@ impl<A: HalApi> LifetimeTracker<A> {
264254
future_suspected_textures: Vec::new(),
265255
suspected_resources: ResourceMaps::new(),
266256
active: Vec::new(),
267-
free_resources: ResourceMaps::new(),
268257
ready_to_map: Vec::new(),
269258
work_done_closures: SmallVec::new(),
270259
device_lost_closure: None,
@@ -374,7 +363,6 @@ impl<A: HalApi> LifetimeTracker<A> {
374363
let mut work_done_closures: SmallVec<_> = self.work_done_closures.drain(..).collect();
375364
for a in self.active.drain(..done_count) {
376365
log::debug!("Active submission {} is done", a.index);
377-
self.free_resources.extend(a.last_resources);
378366
self.ready_to_map.extend(a.mapped);
379367
for encoder in a.encoders {
380368
let raw = unsafe { encoder.land() };
@@ -385,11 +373,6 @@ impl<A: HalApi> LifetimeTracker<A> {
385373
work_done_closures
386374
}
387375

388-
pub fn cleanup(&mut self) {
389-
profiling::scope!("LifetimeTracker::cleanup");
390-
self.free_resources.clear();
391-
}
392-
393376
pub fn schedule_resource_destruction(
394377
&mut self,
395378
temp_resource: TempResource<A>,
@@ -399,22 +382,24 @@ impl<A: HalApi> LifetimeTracker<A> {
399382
.active
400383
.iter_mut()
401384
.find(|a| a.index == last_submit_index)
402-
.map_or(&mut self.free_resources, |a| &mut a.last_resources);
403-
match temp_resource {
404-
TempResource::Buffer(raw) => {
405-
resources.buffers.insert(raw.as_info().id(), raw);
406-
}
407-
TempResource::StagingBuffer(raw) => {
408-
resources.staging_buffers.insert(raw.as_info().id(), raw);
409-
}
410-
TempResource::DestroyedBuffer(destroyed) => {
411-
resources.destroyed_buffers.insert(destroyed.id, destroyed);
412-
}
413-
TempResource::Texture(raw) => {
414-
resources.textures.insert(raw.as_info().id(), raw);
415-
}
416-
TempResource::DestroyedTexture(destroyed) => {
417-
resources.destroyed_textures.insert(destroyed.id, destroyed);
385+
.map(|a| &mut a.last_resources);
386+
if let Some(resources) = resources {
387+
match temp_resource {
388+
TempResource::Buffer(raw) => {
389+
resources.buffers.insert(raw.as_info().id(), raw);
390+
}
391+
TempResource::StagingBuffer(raw) => {
392+
resources.staging_buffers.insert(raw.as_info().id(), raw);
393+
}
394+
TempResource::DestroyedBuffer(destroyed) => {
395+
resources.destroyed_buffers.insert(destroyed.id, destroyed);
396+
}
397+
TempResource::Texture(raw) => {
398+
resources.textures.insert(raw.as_info().id(), raw);
399+
}
400+
TempResource::DestroyedTexture(destroyed) => {
401+
resources.destroyed_textures.insert(destroyed.id, destroyed);
402+
}
418403
}
419404
}
420405
}
@@ -437,7 +422,6 @@ impl<A: HalApi> LifetimeTracker<A> {
437422
fn triage_resources<Id, R>(
438423
resources_map: &mut FastHashMap<Id, Arc<R>>,
439424
active: &mut [ActiveSubmission<A>],
440-
free_resources: &mut ResourceMaps<A>,
441425
trackers: &mut impl ResourceTracker<Id, R>,
442426
get_resource_map: impl Fn(&mut ResourceMaps<A>) -> &mut FastHashMap<Id, Arc<R>>,
443427
) -> Vec<Arc<R>>
@@ -451,12 +435,14 @@ impl<A: HalApi> LifetimeTracker<A> {
451435
let non_referenced_resources = active
452436
.iter_mut()
453437
.find(|a| a.index == submit_index)
454-
.map_or(&mut *free_resources, |a| &mut a.last_resources);
438+
.map(|a| &mut a.last_resources);
455439

456440
let is_removed = trackers.remove_abandoned(id);
457441
if is_removed {
458442
removed_resources.push(resource.clone());
459-
get_resource_map(non_referenced_resources).insert(id, resource.clone());
443+
if let Some(ressources) = non_referenced_resources {
444+
get_resource_map(ressources).insert(id, resource.clone());
445+
}
460446
}
461447
!is_removed
462448
});
@@ -469,7 +455,6 @@ impl<A: HalApi> LifetimeTracker<A> {
469455
let mut removed_resources = Self::triage_resources(
470456
resource_map,
471457
self.active.as_mut_slice(),
472-
&mut self.free_resources,
473458
&mut trackers.bundles,
474459
|maps| &mut maps.render_bundles,
475460
);
@@ -507,7 +492,6 @@ impl<A: HalApi> LifetimeTracker<A> {
507492
let mut removed_resource = Self::triage_resources(
508493
resource_map,
509494
self.active.as_mut_slice(),
510-
&mut self.free_resources,
511495
&mut trackers.bind_groups,
512496
|maps| &mut maps.bind_groups,
513497
);
@@ -544,7 +528,6 @@ impl<A: HalApi> LifetimeTracker<A> {
544528
let mut removed_resources = Self::triage_resources(
545529
resource_map,
546530
self.active.as_mut_slice(),
547-
&mut self.free_resources,
548531
&mut trackers.views,
549532
|maps| &mut maps.texture_views,
550533
);
@@ -565,7 +548,6 @@ impl<A: HalApi> LifetimeTracker<A> {
565548
Self::triage_resources(
566549
resource_map,
567550
self.active.as_mut_slice(),
568-
&mut self.free_resources,
569551
&mut trackers.textures,
570552
|maps| &mut maps.textures,
571553
);
@@ -578,7 +560,6 @@ impl<A: HalApi> LifetimeTracker<A> {
578560
Self::triage_resources(
579561
resource_map,
580562
self.active.as_mut_slice(),
581-
&mut self.free_resources,
582563
&mut trackers.samplers,
583564
|maps| &mut maps.samplers,
584565
);
@@ -588,23 +569,13 @@ impl<A: HalApi> LifetimeTracker<A> {
588569
fn triage_suspected_buffers(&mut self, trackers: &Mutex<Tracker<A>>) -> &mut Self {
589570
let mut trackers = trackers.lock();
590571
let resource_map = &mut self.suspected_resources.buffers;
591-
let mut removed_resources = Self::triage_resources(
572+
Self::triage_resources(
592573
resource_map,
593574
self.active.as_mut_slice(),
594-
&mut self.free_resources,
595575
&mut trackers.buffers,
596576
|maps| &mut maps.buffers,
597577
);
598-
removed_resources.drain(..).for_each(|buffer| {
599-
if let resource::BufferMapState::Init {
600-
ref stage_buffer, ..
601-
} = *buffer.map_state.lock()
602-
{
603-
self.free_resources
604-
.buffers
605-
.insert(stage_buffer.as_info().id(), stage_buffer.clone());
606-
}
607-
});
578+
608579
self
609580
}
610581

@@ -616,8 +587,6 @@ impl<A: HalApi> LifetimeTracker<A> {
616587
.last_resources
617588
.destroyed_buffers
618589
.insert(id, buffer);
619-
} else {
620-
self.free_resources.destroyed_buffers.insert(id, buffer);
621590
}
622591
}
623592
}
@@ -630,8 +599,6 @@ impl<A: HalApi> LifetimeTracker<A> {
630599
.last_resources
631600
.destroyed_textures
632601
.insert(id, texture);
633-
} else {
634-
self.free_resources.destroyed_textures.insert(id, texture);
635602
}
636603
}
637604
}
@@ -642,7 +609,6 @@ impl<A: HalApi> LifetimeTracker<A> {
642609
let mut removed_resources = Self::triage_resources(
643610
resource_map,
644611
self.active.as_mut_slice(),
645-
&mut self.free_resources,
646612
&mut trackers.compute_pipelines,
647613
|maps| &mut maps.compute_pipelines,
648614
);
@@ -661,7 +627,6 @@ impl<A: HalApi> LifetimeTracker<A> {
661627
let mut removed_resources = Self::triage_resources(
662628
resource_map,
663629
self.active.as_mut_slice(),
664-
&mut self.free_resources,
665630
&mut trackers.render_pipelines,
666631
|maps| &mut maps.render_pipelines,
667632
);
@@ -693,18 +658,11 @@ impl<A: HalApi> LifetimeTracker<A> {
693658
}
694659

695660
fn triage_suspected_bind_group_layouts(&mut self) -> &mut Self {
696-
self.suspected_resources.bind_group_layouts.retain(
697-
|bind_group_layout_id, bind_group_layout| {
698-
//Note: this has to happen after all the suspected pipelines are destroyed
699-
//Note: nothing else can bump the refcount since the guard is locked exclusively
700-
//Note: same BGL can appear multiple times in the list, but only the last
701-
// encounter could drop the refcount to 0.
702-
self.free_resources
703-
.bind_group_layouts
704-
.insert(*bind_group_layout_id, bind_group_layout.clone());
705-
false
706-
},
707-
);
661+
//Note: this has to happen after all the suspected pipelines are destroyed
662+
//Note: nothing else can bump the refcount since the guard is locked exclusively
663+
//Note: same BGL can appear multiple times in the list, but only the last
664+
self.suspected_resources.bind_group_layouts.clear();
665+
708666
self
709667
}
710668

@@ -714,22 +672,15 @@ impl<A: HalApi> LifetimeTracker<A> {
714672
Self::triage_resources(
715673
resource_map,
716674
self.active.as_mut_slice(),
717-
&mut self.free_resources,
718675
&mut trackers.query_sets,
719676
|maps| &mut maps.query_sets,
720677
);
721678
self
722679
}
723680

724681
fn triage_suspected_staging_buffers(&mut self) -> &mut Self {
725-
self.suspected_resources
726-
.staging_buffers
727-
.retain(|staging_buffer_id, staging_buffer| {
728-
self.free_resources
729-
.staging_buffers
730-
.insert(*staging_buffer_id, staging_buffer.clone());
731-
false
732-
});
682+
self.suspected_resources.staging_buffers.clear();
683+
733684
self
734685
}
735686

@@ -746,12 +697,8 @@ impl<A: HalApi> LifetimeTracker<A> {
746697
/// - Add resources used by queue submissions still in flight to the
747698
/// [`last_resources`] table of the last such submission's entry in
748699
/// [`self.active`]. When that submission has finished execution. the
749-
/// [`triage_submissions`] method will move them to
750-
/// [`self.free_resources`].
751-
///
752-
/// - Add resources that can be freed right now to [`self.free_resources`]
753-
/// directly. [`LifetimeTracker::cleanup`] will take care of them as
754-
/// part of this poll.
700+
/// [`triage_submissions`] method will remove from the tracker and the
701+
/// resource reference count will be responsible carrying out deallocation.
755702
///
756703
/// ## Entrained resources
757704
///
@@ -771,7 +718,6 @@ impl<A: HalApi> LifetimeTracker<A> {
771718
/// [`last_resources`]: ActiveSubmission::last_resources
772719
/// [`self.active`]: LifetimeTracker::active
773720
/// [`triage_submissions`]: LifetimeTracker::triage_submissions
774-
/// [`self.free_resources`]: LifetimeTracker::free_resources
775721
pub(crate) fn triage_suspected(&mut self, trackers: &Mutex<Tracker<A>>) {
776722
profiling::scope!("triage_suspected");
777723

@@ -844,9 +790,6 @@ impl<A: HalApi> LifetimeTracker<A> {
844790
if is_removed {
845791
*buffer.map_state.lock() = resource::BufferMapState::Idle;
846792
log::trace!("Buffer ready to map {:?} is not tracked anymore", buffer_id);
847-
self.free_resources
848-
.buffers
849-
.insert(buffer_id, buffer.clone());
850793
} else {
851794
let mapping = match std::mem::replace(
852795
&mut *buffer.map_state.lock(),

wgpu-core/src/device/resource.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,6 @@ impl<A: HalApi> Device<A> {
372372
);
373373
let mapping_closures = life_tracker.handle_mapping(self.raw(), &self.trackers);
374374

375-
//Cleaning up resources and released all unused suspected ones
376-
life_tracker.cleanup();
377-
378375
// Detect if we have been destroyed and now need to lose the device.
379376
// If we are invalid (set at start of destroy) and our queue is empty,
380377
// and we have a DeviceLostClosure, return the closure to be called by
@@ -3383,7 +3380,6 @@ impl<A: HalApi> Device<A> {
33833380
current_index,
33843381
self.command_allocator.lock().as_mut().unwrap(),
33853382
);
3386-
life_tracker.cleanup();
33873383
#[cfg(feature = "trace")]
33883384
{
33893385
*self.trace.lock() = None;

0 commit comments

Comments
 (0)