add ActiveSubmission.temp_resources that contains all temporary resources.

It's worth noting that `suspected_resources` never contained those resources.
This commit is contained in:
teoxoy 2024-07-04 14:08:37 +02:00 committed by Teodor Tanasoaia
parent 7223bfa88d
commit c6761bdd7a
4 changed files with 13 additions and 116 deletions

View File

@ -9,10 +9,7 @@ use crate::{
id, id,
lock::Mutex, lock::Mutex,
pipeline::{ComputePipeline, RenderPipeline}, pipeline::{ComputePipeline, RenderPipeline},
resource::{ resource::{self, Buffer, Labeled, QuerySet, Sampler, Texture, TextureView, Trackable},
self, Buffer, DestroyedBuffer, DestroyedTexture, Labeled, QuerySet, Sampler, StagingBuffer,
Texture, TextureView, Trackable,
},
snatch::SnatchGuard, snatch::SnatchGuard,
track::{ResourceTracker, Tracker, TrackerIndex}, track::{ResourceTracker, Tracker, TrackerIndex},
FastHashMap, SubmissionIndex, FastHashMap, SubmissionIndex,
@ -25,7 +22,6 @@ use thiserror::Error;
/// A struct that keeps lists of resources that are no longer needed by the user. /// A struct that keeps lists of resources that are no longer needed by the user.
pub(crate) struct ResourceMaps<A: HalApi> { pub(crate) struct ResourceMaps<A: HalApi> {
pub buffers: FastHashMap<TrackerIndex, Arc<Buffer<A>>>, pub buffers: FastHashMap<TrackerIndex, Arc<Buffer<A>>>,
pub staging_buffers: FastHashMap<TrackerIndex, Arc<StagingBuffer<A>>>,
pub textures: FastHashMap<TrackerIndex, Arc<Texture<A>>>, pub textures: FastHashMap<TrackerIndex, Arc<Texture<A>>>,
pub texture_views: FastHashMap<TrackerIndex, Arc<TextureView<A>>>, pub texture_views: FastHashMap<TrackerIndex, Arc<TextureView<A>>>,
pub samplers: FastHashMap<TrackerIndex, Arc<Sampler<A>>>, pub samplers: FastHashMap<TrackerIndex, Arc<Sampler<A>>>,
@ -36,15 +32,12 @@ pub(crate) struct ResourceMaps<A: HalApi> {
pub pipeline_layouts: FastHashMap<TrackerIndex, Arc<PipelineLayout<A>>>, pub pipeline_layouts: FastHashMap<TrackerIndex, Arc<PipelineLayout<A>>>,
pub render_bundles: FastHashMap<TrackerIndex, Arc<RenderBundle<A>>>, pub render_bundles: FastHashMap<TrackerIndex, Arc<RenderBundle<A>>>,
pub query_sets: FastHashMap<TrackerIndex, Arc<QuerySet<A>>>, pub query_sets: FastHashMap<TrackerIndex, Arc<QuerySet<A>>>,
pub destroyed_buffers: FastHashMap<TrackerIndex, Arc<DestroyedBuffer<A>>>,
pub destroyed_textures: FastHashMap<TrackerIndex, Arc<DestroyedTexture<A>>>,
} }
impl<A: HalApi> ResourceMaps<A> { impl<A: HalApi> ResourceMaps<A> {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
ResourceMaps { ResourceMaps {
buffers: FastHashMap::default(), buffers: FastHashMap::default(),
staging_buffers: FastHashMap::default(),
textures: FastHashMap::default(), textures: FastHashMap::default(),
texture_views: FastHashMap::default(), texture_views: FastHashMap::default(),
samplers: FastHashMap::default(), samplers: FastHashMap::default(),
@ -55,15 +48,12 @@ impl<A: HalApi> ResourceMaps<A> {
pipeline_layouts: FastHashMap::default(), pipeline_layouts: FastHashMap::default(),
render_bundles: FastHashMap::default(), render_bundles: FastHashMap::default(),
query_sets: FastHashMap::default(), query_sets: FastHashMap::default(),
destroyed_buffers: FastHashMap::default(),
destroyed_textures: FastHashMap::default(),
} }
} }
pub(crate) fn clear(&mut self) { pub(crate) fn clear(&mut self) {
let ResourceMaps { let ResourceMaps {
buffers, buffers,
staging_buffers,
textures, textures,
texture_views, texture_views,
samplers, samplers,
@ -74,11 +64,8 @@ impl<A: HalApi> ResourceMaps<A> {
pipeline_layouts, pipeline_layouts,
render_bundles, render_bundles,
query_sets, query_sets,
destroyed_buffers,
destroyed_textures,
} = self; } = self;
buffers.clear(); buffers.clear();
staging_buffers.clear();
textures.clear(); textures.clear();
texture_views.clear(); texture_views.clear();
samplers.clear(); samplers.clear();
@ -89,14 +76,11 @@ impl<A: HalApi> ResourceMaps<A> {
pipeline_layouts.clear(); pipeline_layouts.clear();
render_bundles.clear(); render_bundles.clear();
query_sets.clear(); query_sets.clear();
destroyed_buffers.clear();
destroyed_textures.clear();
} }
pub(crate) fn extend(&mut self, other: &mut Self) { pub(crate) fn extend(&mut self, other: &mut Self) {
let ResourceMaps { let ResourceMaps {
buffers, buffers,
staging_buffers,
textures, textures,
texture_views, texture_views,
samplers, samplers,
@ -107,11 +91,8 @@ impl<A: HalApi> ResourceMaps<A> {
pipeline_layouts, pipeline_layouts,
render_bundles, render_bundles,
query_sets, query_sets,
destroyed_buffers,
destroyed_textures,
} = self; } = self;
buffers.extend(other.buffers.drain()); buffers.extend(other.buffers.drain());
staging_buffers.extend(other.staging_buffers.drain());
textures.extend(other.textures.drain()); textures.extend(other.textures.drain());
texture_views.extend(other.texture_views.drain()); texture_views.extend(other.texture_views.drain());
samplers.extend(other.samplers.drain()); samplers.extend(other.samplers.drain());
@ -122,8 +103,6 @@ impl<A: HalApi> ResourceMaps<A> {
pipeline_layouts.extend(other.pipeline_layouts.drain()); pipeline_layouts.extend(other.pipeline_layouts.drain());
render_bundles.extend(other.render_bundles.drain()); render_bundles.extend(other.render_bundles.drain());
query_sets.extend(other.query_sets.drain()); query_sets.extend(other.query_sets.drain());
destroyed_buffers.extend(other.destroyed_buffers.drain());
destroyed_textures.extend(other.destroyed_textures.drain());
} }
} }
@ -171,11 +150,13 @@ struct ActiveSubmission<A: HalApi> {
/// `triage_submissions` removes resources that don't need to be held alive any longer /// `triage_submissions` removes resources that don't need to be held alive any longer
/// from there. /// from there.
/// ///
/// This includes things like temporary resources and resources that are /// This includes resources that are used by submitted commands but have been
/// used by submitted commands but have been dropped by the user (meaning that /// dropped by the user (meaning that this submission is their last reference.)
/// this submission is their last reference.)
last_resources: ResourceMaps<A>, last_resources: ResourceMaps<A>,
/// Temporary resources to be freed once this queue submission has completed.
temp_resources: Vec<TempResource<A>>,
/// Buffers to be mapped once this submission has completed. /// Buffers to be mapped once this submission has completed.
mapped: Vec<Arc<Buffer<A>>>, mapped: Vec<Arc<Buffer<A>>>,
@ -310,30 +291,10 @@ impl<A: HalApi> LifetimeTracker<A> {
temp_resources: impl Iterator<Item = TempResource<A>>, temp_resources: impl Iterator<Item = TempResource<A>>,
encoders: Vec<EncoderInFlight<A>>, encoders: Vec<EncoderInFlight<A>>,
) { ) {
let mut last_resources = ResourceMaps::new();
for res in temp_resources {
match res {
TempResource::StagingBuffer(raw) => {
last_resources
.staging_buffers
.insert(raw.tracker_index(), Arc::new(raw));
}
TempResource::DestroyedBuffer(destroyed) => {
last_resources
.destroyed_buffers
.insert(destroyed.tracker_index, Arc::new(destroyed));
}
TempResource::DestroyedTexture(destroyed) => {
last_resources
.destroyed_textures
.insert(destroyed.tracker_index, Arc::new(destroyed));
}
}
}
self.active.push(ActiveSubmission { self.active.push(ActiveSubmission {
index, index,
last_resources, last_resources: ResourceMaps::new(),
temp_resources: temp_resources.collect(),
mapped: Vec::new(), mapped: Vec::new(),
encoders, encoders,
work_done_closures: SmallVec::new(), work_done_closures: SmallVec::new(),
@ -399,6 +360,7 @@ impl<A: HalApi> LifetimeTracker<A> {
let raw = unsafe { encoder.land() }; let raw = unsafe { encoder.land() };
command_allocator.release_encoder(raw); command_allocator.release_encoder(raw);
} }
drop(a.temp_resources);
work_done_closures.extend(a.work_done_closures); work_done_closures.extend(a.work_done_closures);
} }
work_done_closures work_done_closures
@ -413,25 +375,9 @@ impl<A: HalApi> LifetimeTracker<A> {
.active .active
.iter_mut() .iter_mut()
.find(|a| a.index == last_submit_index) .find(|a| a.index == last_submit_index)
.map(|a| &mut a.last_resources); .map(|a| &mut a.temp_resources);
if let Some(resources) = resources { if let Some(resources) = resources {
match temp_resource { resources.push(temp_resource);
TempResource::StagingBuffer(raw) => {
resources
.staging_buffers
.insert(raw.tracker_index(), Arc::new(raw));
}
TempResource::DestroyedBuffer(destroyed) => {
resources
.destroyed_buffers
.insert(destroyed.tracker_index, Arc::new(destroyed));
}
TempResource::DestroyedTexture(destroyed) => {
resources
.destroyed_textures
.insert(destroyed.tracker_index, Arc::new(destroyed));
}
}
} }
} }
@ -627,30 +573,6 @@ impl<A: HalApi> LifetimeTracker<A> {
self self
} }
fn triage_suspected_destroyed_buffers(&mut self) {
for (id, buffer) in self.suspected_resources.destroyed_buffers.drain() {
let submit_index = buffer.submission_index;
if let Some(resources) = self.active.iter_mut().find(|a| a.index == submit_index) {
resources
.last_resources
.destroyed_buffers
.insert(id, buffer);
}
}
}
fn triage_suspected_destroyed_textures(&mut self) {
for (id, texture) in self.suspected_resources.destroyed_textures.drain() {
let submit_index = texture.submission_index;
if let Some(resources) = self.active.iter_mut().find(|a| a.index == submit_index) {
resources
.last_resources
.destroyed_textures
.insert(id, texture);
}
}
}
fn triage_suspected_compute_pipelines(&mut self, trackers: &Mutex<Tracker<A>>) -> &mut Self { fn triage_suspected_compute_pipelines(&mut self, trackers: &Mutex<Tracker<A>>) -> &mut Self {
let mut trackers = trackers.lock(); let mut trackers = trackers.lock();
let suspected_compute_pipelines = &mut self.suspected_resources.compute_pipelines; let suspected_compute_pipelines = &mut self.suspected_resources.compute_pipelines;
@ -726,12 +648,6 @@ impl<A: HalApi> LifetimeTracker<A> {
self self
} }
fn triage_suspected_staging_buffers(&mut self) -> &mut Self {
self.suspected_resources.staging_buffers.clear();
self
}
/// Identify resources to free, according to `trackers` and `self.suspected_resources`. /// Identify resources to free, according to `trackers` and `self.suspected_resources`.
/// ///
/// Remove from `trackers`, the [`Tracker`] belonging to same [`Device`] as /// Remove from `trackers`, the [`Tracker`] belonging to same [`Device`] as
@ -776,12 +692,9 @@ impl<A: HalApi> LifetimeTracker<A> {
self.triage_suspected_bind_group_layouts(); self.triage_suspected_bind_group_layouts();
self.triage_suspected_query_sets(trackers); self.triage_suspected_query_sets(trackers);
self.triage_suspected_samplers(trackers); self.triage_suspected_samplers(trackers);
self.triage_suspected_staging_buffers();
self.triage_suspected_texture_views(trackers); self.triage_suspected_texture_views(trackers);
self.triage_suspected_textures(trackers); self.triage_suspected_textures(trackers);
self.triage_suspected_buffers(trackers); self.triage_suspected_buffers(trackers);
self.triage_suspected_destroyed_buffers();
self.triage_suspected_destroyed_textures();
} }
/// Determine which buffers are ready to map, and which must wait for the /// Determine which buffers are ready to map, and which must wait for the

View File

@ -18,7 +18,7 @@ use crate::{
resource::{ resource::{
Buffer, BufferAccessError, BufferMapState, DestroyedBuffer, DestroyedResourceError, Buffer, BufferAccessError, BufferMapState, DestroyedBuffer, DestroyedResourceError,
DestroyedTexture, Labeled, ParentDevice, ResourceErrorIdent, StagingBuffer, Texture, DestroyedTexture, Labeled, ParentDevice, ResourceErrorIdent, StagingBuffer, Texture,
TextureInner, Trackable, TrackingData, TextureInner, Trackable,
}, },
resource_log, resource_log,
track::{self, TrackerIndex}, track::{self, TrackerIndex},
@ -138,11 +138,8 @@ pub struct WrappedSubmissionIndex {
/// - `PendingWrites::temp_resources`: resources used by queue writes and /// - `PendingWrites::temp_resources`: resources used by queue writes and
/// unmaps, waiting to be folded in with the next queue submission /// unmaps, waiting to be folded in with the next queue submission
/// ///
/// - `ActiveSubmission::last_resources`: temporary resources used by a queue /// - `ActiveSubmission::temp_resources`: temporary resources used by a queue
/// submission, to be freed when it completes /// submission, to be freed when it completes
///
/// - `LifetimeTracker::free_resources`: resources to be freed in the next
/// `maintain` call, no longer used anywhere
#[derive(Debug)] #[derive(Debug)]
pub enum TempResource<A: HalApi> { pub enum TempResource<A: HalApi> {
StagingBuffer(StagingBuffer<A>), StagingBuffer(StagingBuffer<A>),
@ -336,7 +333,6 @@ pub(crate) fn prepare_staging_buffer<A: HalApi>(
raw: Mutex::new(rank::STAGING_BUFFER_RAW, Some(buffer)), raw: Mutex::new(rank::STAGING_BUFFER_RAW, Some(buffer)),
device: device.clone(), device: device.clone(),
size, size,
tracking_data: TrackingData::new(device.tracker_indices.staging_buffers.clone()),
is_coherent: mapping.is_coherent, is_coherent: mapping.is_coherent,
}; };

View File

@ -770,8 +770,6 @@ impl<A: HalApi> Buffer<A> {
queue::TempResource::DestroyedBuffer(DestroyedBuffer { queue::TempResource::DestroyedBuffer(DestroyedBuffer {
raw: Some(raw), raw: Some(raw),
device: Arc::clone(&self.device), device: Arc::clone(&self.device),
submission_index: self.submission_index(),
tracker_index: self.tracker_index(),
label: self.label().to_owned(), label: self.label().to_owned(),
bind_groups, bind_groups,
}) })
@ -823,8 +821,6 @@ pub struct DestroyedBuffer<A: HalApi> {
raw: Option<A::Buffer>, raw: Option<A::Buffer>,
device: Arc<Device<A>>, device: Arc<Device<A>>,
label: String, label: String,
pub(crate) tracker_index: TrackerIndex,
pub(crate) submission_index: u64,
bind_groups: Vec<Weak<BindGroup<A>>>, bind_groups: Vec<Weak<BindGroup<A>>>,
} }
@ -878,7 +874,6 @@ pub struct StagingBuffer<A: HalApi> {
pub(crate) device: Arc<Device<A>>, pub(crate) device: Arc<Device<A>>,
pub(crate) size: wgt::BufferAddress, pub(crate) size: wgt::BufferAddress,
pub(crate) is_coherent: bool, pub(crate) is_coherent: bool,
pub(crate) tracking_data: TrackingData,
} }
impl<A: HalApi> Drop for StagingBuffer<A> { impl<A: HalApi> Drop for StagingBuffer<A> {
@ -902,7 +897,6 @@ impl<A: HalApi> Labeled for StagingBuffer<A> {
} }
crate::impl_parent_device!(StagingBuffer); crate::impl_parent_device!(StagingBuffer);
crate::impl_storage_item!(StagingBuffer); crate::impl_storage_item!(StagingBuffer);
crate::impl_trackable!(StagingBuffer);
pub type TextureDescriptor<'a> = wgt::TextureDescriptor<Label<'a>, Vec<wgt::TextureFormat>>; pub type TextureDescriptor<'a> = wgt::TextureDescriptor<Label<'a>, Vec<wgt::TextureFormat>>;
@ -1141,8 +1135,6 @@ impl<A: HalApi> Texture<A> {
views, views,
bind_groups, bind_groups,
device: Arc::clone(&self.device), device: Arc::clone(&self.device),
tracker_index: self.tracker_index(),
submission_index: self.submission_index(),
label: self.label().to_owned(), label: self.label().to_owned(),
}) })
}; };
@ -1333,8 +1325,6 @@ pub struct DestroyedTexture<A: HalApi> {
bind_groups: Vec<Weak<BindGroup<A>>>, bind_groups: Vec<Weak<BindGroup<A>>>,
device: Arc<Device<A>>, device: Arc<Device<A>>,
label: String, label: String,
pub(crate) tracker_index: TrackerIndex,
pub(crate) submission_index: u64,
} }
impl<A: HalApi> DestroyedTexture<A> { impl<A: HalApi> DestroyedTexture<A> {

View File

@ -213,7 +213,6 @@ impl SharedTrackerIndexAllocator {
pub(crate) struct TrackerIndexAllocators { pub(crate) struct TrackerIndexAllocators {
pub buffers: Arc<SharedTrackerIndexAllocator>, pub buffers: Arc<SharedTrackerIndexAllocator>,
pub staging_buffers: Arc<SharedTrackerIndexAllocator>,
pub textures: Arc<SharedTrackerIndexAllocator>, pub textures: Arc<SharedTrackerIndexAllocator>,
pub texture_views: Arc<SharedTrackerIndexAllocator>, pub texture_views: Arc<SharedTrackerIndexAllocator>,
pub samplers: Arc<SharedTrackerIndexAllocator>, pub samplers: Arc<SharedTrackerIndexAllocator>,
@ -231,7 +230,6 @@ impl TrackerIndexAllocators {
pub fn new() -> Self { pub fn new() -> Self {
TrackerIndexAllocators { TrackerIndexAllocators {
buffers: Arc::new(SharedTrackerIndexAllocator::new()), buffers: Arc::new(SharedTrackerIndexAllocator::new()),
staging_buffers: Arc::new(SharedTrackerIndexAllocator::new()),
textures: Arc::new(SharedTrackerIndexAllocator::new()), textures: Arc::new(SharedTrackerIndexAllocator::new()),
texture_views: Arc::new(SharedTrackerIndexAllocator::new()), texture_views: Arc::new(SharedTrackerIndexAllocator::new()),
samplers: Arc::new(SharedTrackerIndexAllocator::new()), samplers: Arc::new(SharedTrackerIndexAllocator::new()),