From 27cab8efd71af8da764e08fd2c6b0530a7434008 Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Wed, 21 Feb 2024 11:37:52 +0100 Subject: [PATCH] Internalize the tracker index allocator locks --- wgpu-core/src/command/compute.rs | 14 ++---- wgpu-core/src/command/render.rs | 18 +++---- wgpu-core/src/resource.rs | 10 ++-- wgpu-core/src/track/mod.rs | 84 ++++++++++++++++++++------------ 4 files changed, 70 insertions(+), 56 deletions(-) diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index e3a1d6c1f7..c2fd3ab397 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -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 diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 244989199e..9141ddb021 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -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; diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 06bb327d31..aca077caab 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -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, }; @@ -58,7 +58,7 @@ use std::{ pub struct ResourceInfo { id: Option>, tracker_index: TrackerIndex, - tracker_indices: Option>>, + tracker_indices: Option>, /// The index of the last queue submission in which the resource /// was used. /// @@ -75,7 +75,7 @@ pub struct ResourceInfo { impl Drop for ResourceInfo { fn drop(&mut self) { if let Some(indices) = &self.tracker_indices { - indices.lock().free(self.tracker_index); + indices.free(self.tracker_index); } } } @@ -84,11 +84,11 @@ impl ResourceInfo { #[allow(unused_variables)] pub(crate) fn new( label: &str, - tracker_indices: Option>>, + tracker_indices: Option>, ) -> Self { let tracker_index = tracker_indices .as_ref() - .map(|indices| indices.lock().alloc()) + .map(|indices| indices.alloc()) .unwrap_or(TrackerIndex::INVALID); Self { id: None, diff --git a/wgpu-core/src/track/mod.rs b/wgpu-core/src/track/mod.rs index 3cb2348e54..c0895c44a6 100644 --- a/wgpu-core/src/track/mod.rs +++ b/wgpu-core/src/track/mod.rs @@ -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, next_index: TrackerIndex, } @@ -182,36 +182,62 @@ impl std::fmt::Debug for TrackerIndexAllocator { } } +/// See TrackerIndexAllocator. +#[derive(Debug)] +pub(crate) struct SharedTrackerIndexAllocator { + inner: Mutex, +} + +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>, - pub staging_buffers: Arc>, - pub textures: Arc>, - pub texture_views: Arc>, - pub samplers: Arc>, - pub bind_groups: Arc>, - pub bind_group_layouts: Arc>, - pub compute_pipelines: Arc>, - pub render_pipelines: Arc>, - pub pipeline_layouts: Arc>, - pub bundles: Arc>, - pub query_sets: Arc>, + pub buffers: Arc, + pub staging_buffers: Arc, + pub textures: Arc, + pub texture_views: Arc, + pub samplers: Arc, + pub bind_groups: Arc, + pub bind_group_layouts: Arc, + pub compute_pipelines: Arc, + pub render_pipelines: Arc, + pub pipeline_layouts: Arc, + pub bundles: Arc, + pub query_sets: Arc, } 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()), } } } @@ -520,12 +546,8 @@ impl UsageScope { 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 }