Skip to content

Commit

Permalink
Internalize the tracker index allocator locks
Browse files Browse the repository at this point in the history
  • Loading branch information
nical committed Feb 21, 2024
1 parent 4a08afc commit 27cab8e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 56 deletions.
14 changes: 5 additions & 9 deletions wgpu-core/src/command/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,17 +453,13 @@ impl Global {
let snatch_guard = device.snatchable_lock.read();

let indices = &device.tracker_indices;
tracker.buffers.set_size(indices.buffers.lock().size());
tracker.textures.set_size(indices.textures.lock().size());
tracker
.bind_groups
.set_size(indices.bind_groups.lock().size());
tracker.buffers.set_size(indices.buffers.size());
tracker.textures.set_size(indices.textures.size());
tracker.bind_groups.set_size(indices.bind_groups.size());
tracker
.compute_pipelines
.set_size(indices.compute_pipelines.lock().size());
tracker
.query_sets
.set_size(indices.query_sets.lock().size());
.set_size(indices.compute_pipelines.size());
tracker.query_sets.set_size(indices.query_sets.size());

let discard_hal_labels = self
.instance
Expand Down
18 changes: 7 additions & 11 deletions wgpu-core/src/command/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1411,19 +1411,15 @@ impl Global {
.map_pass_err(pass_scope)?;

let indices = &device.tracker_indices;
tracker.buffers.set_size(indices.buffers.lock().size());
tracker.textures.set_size(indices.textures.lock().size());
tracker.views.set_size(indices.texture_views.lock().size());
tracker
.bind_groups
.set_size(indices.bind_groups.lock().size());
tracker.buffers.set_size(indices.buffers.size());
tracker.textures.set_size(indices.textures.size());
tracker.views.set_size(indices.texture_views.size());
tracker.bind_groups.set_size(indices.bind_groups.size());
tracker
.render_pipelines
.set_size(indices.render_pipelines.lock().size());
tracker.bundles.set_size(indices.bundles.lock().size());
tracker
.query_sets
.set_size(indices.query_sets.lock().size());
.set_size(indices.render_pipelines.size());
tracker.bundles.set_size(indices.bundles.size());
tracker.query_sets.set_size(indices.query_sets.size());

let raw = &mut encoder.raw;

Expand Down
10 changes: 5 additions & 5 deletions wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
init_tracker::{BufferInitTracker, TextureInitTracker},
resource, resource_log,
snatch::{ExclusiveSnatchGuard, SnatchGuard, Snatchable},
track::{TextureSelector, TrackerIndex, TrackerIndexAllocator},
track::{SharedTrackerIndexAllocator, TextureSelector, TrackerIndex},
validation::MissingBufferUsageError,
Label, SubmissionIndex,
};
Expand Down Expand Up @@ -58,7 +58,7 @@ use std::{
pub struct ResourceInfo<T: Resource> {
id: Option<Id<T::Marker>>,
tracker_index: TrackerIndex,
tracker_indices: Option<Arc<Mutex<TrackerIndexAllocator>>>,
tracker_indices: Option<Arc<SharedTrackerIndexAllocator>>,
/// The index of the last queue submission in which the resource
/// was used.
///
Expand All @@ -75,7 +75,7 @@ pub struct ResourceInfo<T: Resource> {
impl<T: Resource> Drop for ResourceInfo<T> {
fn drop(&mut self) {
if let Some(indices) = &self.tracker_indices {
indices.lock().free(self.tracker_index);
indices.free(self.tracker_index);
}
}
}
Expand All @@ -84,11 +84,11 @@ impl<T: Resource> ResourceInfo<T> {
#[allow(unused_variables)]
pub(crate) fn new(
label: &str,
tracker_indices: Option<Arc<Mutex<TrackerIndexAllocator>>>,
tracker_indices: Option<Arc<SharedTrackerIndexAllocator>>,
) -> Self {
let tracker_index = tracker_indices
.as_ref()
.map(|indices| indices.lock().alloc())
.map(|indices| indices.alloc())
.unwrap_or(TrackerIndex::INVALID);
Self {
id: None,
Expand Down
84 changes: 53 additions & 31 deletions wgpu-core/src/track/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl TrackerIndex {
/// per resource type. Indices have the same lifetime as the internal resource they
/// are associated to (alloc happens when creating the resource and free is called when
/// the resource is dropped).
pub(crate) struct TrackerIndexAllocator {
struct TrackerIndexAllocator {
unused: Vec<TrackerIndex>,
next_index: TrackerIndex,
}
Expand Down Expand Up @@ -182,36 +182,62 @@ impl std::fmt::Debug for TrackerIndexAllocator {
}
}

/// See TrackerIndexAllocator.
#[derive(Debug)]
pub(crate) struct SharedTrackerIndexAllocator {
inner: Mutex<TrackerIndexAllocator>,
}

impl SharedTrackerIndexAllocator {
pub fn new() -> Self {
SharedTrackerIndexAllocator {
inner: Mutex::new(TrackerIndexAllocator::new()),
}
}

pub fn alloc(&self) -> TrackerIndex {
self.inner.lock().alloc()
}

pub fn free(&self, index: TrackerIndex) {
self.inner.lock().free(index);
}

pub fn size(&self) -> usize {
self.inner.lock().size()
}
}

pub(crate) struct TrackerIndexAllocators {
pub buffers: Arc<Mutex<TrackerIndexAllocator>>,
pub staging_buffers: Arc<Mutex<TrackerIndexAllocator>>,
pub textures: Arc<Mutex<TrackerIndexAllocator>>,
pub texture_views: Arc<Mutex<TrackerIndexAllocator>>,
pub samplers: Arc<Mutex<TrackerIndexAllocator>>,
pub bind_groups: Arc<Mutex<TrackerIndexAllocator>>,
pub bind_group_layouts: Arc<Mutex<TrackerIndexAllocator>>,
pub compute_pipelines: Arc<Mutex<TrackerIndexAllocator>>,
pub render_pipelines: Arc<Mutex<TrackerIndexAllocator>>,
pub pipeline_layouts: Arc<Mutex<TrackerIndexAllocator>>,
pub bundles: Arc<Mutex<TrackerIndexAllocator>>,
pub query_sets: Arc<Mutex<TrackerIndexAllocator>>,
pub buffers: Arc<SharedTrackerIndexAllocator>,
pub staging_buffers: Arc<SharedTrackerIndexAllocator>,
pub textures: Arc<SharedTrackerIndexAllocator>,
pub texture_views: Arc<SharedTrackerIndexAllocator>,
pub samplers: Arc<SharedTrackerIndexAllocator>,
pub bind_groups: Arc<SharedTrackerIndexAllocator>,
pub bind_group_layouts: Arc<SharedTrackerIndexAllocator>,
pub compute_pipelines: Arc<SharedTrackerIndexAllocator>,
pub render_pipelines: Arc<SharedTrackerIndexAllocator>,
pub pipeline_layouts: Arc<SharedTrackerIndexAllocator>,
pub bundles: Arc<SharedTrackerIndexAllocator>,
pub query_sets: Arc<SharedTrackerIndexAllocator>,
}

impl TrackerIndexAllocators {
pub fn new() -> Self {
TrackerIndexAllocators {
buffers: Arc::new(Mutex::new(TrackerIndexAllocator::new())),
staging_buffers: Arc::new(Mutex::new(TrackerIndexAllocator::new())),
textures: Arc::new(Mutex::new(TrackerIndexAllocator::new())),
texture_views: Arc::new(Mutex::new(TrackerIndexAllocator::new())),
samplers: Arc::new(Mutex::new(TrackerIndexAllocator::new())),
bind_groups: Arc::new(Mutex::new(TrackerIndexAllocator::new())),
bind_group_layouts: Arc::new(Mutex::new(TrackerIndexAllocator::new())),
compute_pipelines: Arc::new(Mutex::new(TrackerIndexAllocator::new())),
render_pipelines: Arc::new(Mutex::new(TrackerIndexAllocator::new())),
pipeline_layouts: Arc::new(Mutex::new(TrackerIndexAllocator::new())),
bundles: Arc::new(Mutex::new(TrackerIndexAllocator::new())),
query_sets: Arc::new(Mutex::new(TrackerIndexAllocator::new())),
buffers: Arc::new(SharedTrackerIndexAllocator::new()),
staging_buffers: Arc::new(SharedTrackerIndexAllocator::new()),
textures: Arc::new(SharedTrackerIndexAllocator::new()),
texture_views: Arc::new(SharedTrackerIndexAllocator::new()),
samplers: Arc::new(SharedTrackerIndexAllocator::new()),
bind_groups: Arc::new(SharedTrackerIndexAllocator::new()),
bind_group_layouts: Arc::new(SharedTrackerIndexAllocator::new()),
compute_pipelines: Arc::new(SharedTrackerIndexAllocator::new()),
render_pipelines: Arc::new(SharedTrackerIndexAllocator::new()),
pipeline_layouts: Arc::new(SharedTrackerIndexAllocator::new()),
bundles: Arc::new(SharedTrackerIndexAllocator::new()),
query_sets: Arc::new(SharedTrackerIndexAllocator::new()),
}
}
}
Expand Down Expand Up @@ -520,12 +546,8 @@ impl<A: HalApi> UsageScope<A> {
textures: TextureUsageScope::new(),
};

value
.buffers
.set_size(tracker_indices.buffers.lock().size());
value
.textures
.set_size(tracker_indices.textures.lock().size());
value.buffers.set_size(tracker_indices.buffers.size());
value.textures.set_size(tracker_indices.textures.size());

value
}
Expand Down

0 comments on commit 27cab8e

Please sign in to comment.