From 3a5ad193db8eb344cfa8e1e5cc58c0cf446c3903 Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Fri, 2 Aug 2024 18:42:10 +0200 Subject: [PATCH] remove all internal `BindGroupState` `Mutex`es --- wgpu-core/src/device/resource.rs | 7 ++++--- wgpu-core/src/lock/rank.rs | 9 --------- wgpu-core/src/track/buffer.rs | 26 ++++++++++---------------- wgpu-core/src/track/stateless.rs | 21 ++++++++------------- wgpu-core/src/track/texture.rs | 22 +++++++++------------- 5 files changed, 31 insertions(+), 54 deletions(-) diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 6bafd6844..581c52009 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -1998,7 +1998,7 @@ impl Device { fn create_sampler_binding<'a>( self: &Arc, - used: &BindGroupStates, + used: &mut BindGroupStates, binding: u32, decl: &wgt::BindGroupLayoutEntry, sampler: &'a Arc>, @@ -2177,7 +2177,7 @@ impl Device { (res_index, num_bindings) } Br::Sampler(ref sampler) => { - let sampler = self.create_sampler_binding(&used, binding, decl, sampler)?; + let sampler = self.create_sampler_binding(&mut used, binding, decl, sampler)?; let res_index = hal_samplers.len(); hal_samplers.push(sampler); @@ -2189,7 +2189,8 @@ impl Device { let res_index = hal_samplers.len(); for sampler in samplers.iter() { - let sampler = self.create_sampler_binding(&used, binding, decl, sampler)?; + let sampler = + self.create_sampler_binding(&mut used, binding, decl, sampler)?; hal_samplers.push(sampler); } diff --git a/wgpu-core/src/lock/rank.rs b/wgpu-core/src/lock/rank.rs index f109fe1a8..b4b5e2748 100644 --- a/wgpu-core/src/lock/rank.rs +++ b/wgpu-core/src/lock/rank.rs @@ -91,18 +91,12 @@ define_lock_ranks! { DEVICE_SNATCHABLE_LOCK, DEVICE_USAGE_SCOPES, SHARED_TRACKER_INDEX_ALLOCATOR_INNER, - BUFFER_BIND_GROUP_STATE_BUFFERS, - TEXTURE_BIND_GROUP_STATE_TEXTURES, BUFFER_MAP_STATE, - STATELESS_BIND_GROUP_STATE_RESOURCES, } rank DEVICE_SNATCHABLE_LOCK "Device::snatchable_lock" followed by { SHARED_TRACKER_INDEX_ALLOCATOR_INNER, DEVICE_TRACE, BUFFER_MAP_STATE, - BUFFER_BIND_GROUP_STATE_BUFFERS, - TEXTURE_BIND_GROUP_STATE_TEXTURES, - STATELESS_BIND_GROUP_STATE_RESOURCES, // Uncomment this to see an interesting cycle. // COMMAND_BUFFER_DATA, } @@ -125,7 +119,6 @@ define_lock_ranks! { } rank BUFFER_BIND_GROUPS "Buffer::bind_groups" followed by { } - rank BUFFER_BIND_GROUP_STATE_BUFFERS "BufferBindGroupState::buffers" followed by { } rank BUFFER_INITIALIZATION_STATUS "Buffer::initialization_status" followed by { } rank BUFFER_SYNC_MAPPED_WRITES "Buffer::sync_mapped_writes" followed by { } rank DEVICE_DEFERRED_DESTROY "Device::deferred_destroy" followed by { } @@ -142,10 +135,8 @@ define_lock_ranks! { rank RENDER_BUNDLE_SCOPE_RENDER_PIPELINES "RenderBundleScope::render_pipelines" followed by { } rank RESOURCE_POOL_INNER "ResourcePool::inner" followed by { } rank SHARED_TRACKER_INDEX_ALLOCATOR_INNER "SharedTrackerIndexAllocator::inner" followed by { } - rank STATELESS_BIND_GROUP_STATE_RESOURCES "StatelessBindGroupState::resources" followed by { } rank SURFACE_PRESENTATION "Surface::presentation" followed by { } rank TEXTURE_BIND_GROUPS "Texture::bind_groups" followed by { } - rank TEXTURE_BIND_GROUP_STATE_TEXTURES "TextureBindGroupState::textures" followed by { } rank TEXTURE_INITIALIZATION_STATUS "Texture::initialization_status" followed by { } rank TEXTURE_CLEAR_MODE "Texture::clear_mode" followed by { } rank TEXTURE_VIEWS "Texture::views" followed by { } diff --git a/wgpu-core/src/track/buffer.rs b/wgpu-core/src/track/buffer.rs index 4920cc4cc..b0bcdca03 100644 --- a/wgpu-core/src/track/buffer.rs +++ b/wgpu-core/src/track/buffer.rs @@ -9,7 +9,6 @@ use std::sync::{Arc, Weak}; use super::{PendingTransition, TrackerIndex}; use crate::{ hal_api::HalApi, - lock::{rank, Mutex}, resource::{Buffer, Trackable}, snatch::SnatchGuard, track::{ @@ -41,12 +40,12 @@ impl ResourceUses for BufferUses { /// Stores all the buffers that a bind group stores. #[derive(Debug)] pub(crate) struct BufferBindGroupState { - buffers: Mutex>, BufferUses)>>, + buffers: Vec<(Arc>, BufferUses)>, } impl BufferBindGroupState { pub fn new() -> Self { Self { - buffers: Mutex::new(rank::BUFFER_BIND_GROUP_STATE_BUFFERS, Vec::new()), + buffers: Vec::new(), } } @@ -54,27 +53,23 @@ impl BufferBindGroupState { /// /// When this list of states is merged into a tracker, the memory /// accesses will be in a constant ascending order. - #[allow(clippy::pattern_type_mismatch)] - pub(crate) fn optimize(&self) { - let mut buffers = self.buffers.lock(); - buffers.sort_unstable_by_key(|(b, _)| b.tracker_index()); + pub(crate) fn optimize(&mut self) { + self.buffers + .sort_unstable_by_key(|(b, _)| b.tracker_index()); } /// Returns a list of all buffers tracked. May contain duplicates. - #[allow(clippy::pattern_type_mismatch)] pub fn used_tracker_indices(&self) -> impl Iterator + '_ { - let buffers = self.buffers.lock(); - buffers + self.buffers .iter() - .map(|(ref b, _)| b.tracker_index()) + .map(|(b, _)| b.tracker_index()) .collect::>() .into_iter() } /// Adds the given resource with the given state. - pub fn add_single(&self, buffer: &Arc>, state: BufferUses) { - let mut buffers = self.buffers.lock(); - buffers.push((buffer.clone(), state)); + pub fn add_single(&mut self, buffer: &Arc>, state: BufferUses) { + self.buffers.push((buffer.clone(), state)); } } @@ -136,8 +131,7 @@ impl BufferUsageScope { &mut self, bind_group: &BufferBindGroupState, ) -> Result<(), ResourceUsageCompatibilityError> { - let buffers = bind_group.buffers.lock(); - for &(ref resource, state) in &*buffers { + for &(ref resource, state) in bind_group.buffers.iter() { let index = resource.tracker_index().as_usize(); unsafe { diff --git a/wgpu-core/src/track/stateless.rs b/wgpu-core/src/track/stateless.rs index b2de45363..3899e45ac 100644 --- a/wgpu-core/src/track/stateless.rs +++ b/wgpu-core/src/track/stateless.rs @@ -5,22 +5,18 @@ use std::sync::Arc; -use crate::{ - lock::{rank, Mutex}, - resource::Trackable, - track::ResourceMetadata, -}; +use crate::{resource::Trackable, track::ResourceMetadata}; /// Stores all the resources that a bind group stores. #[derive(Debug)] pub(crate) struct StatelessBindGroupState { - resources: Mutex>>, + resources: Vec>, } impl StatelessBindGroupState { pub fn new() -> Self { Self { - resources: Mutex::new(rank::STATELESS_BIND_GROUP_STATE_RESOURCES, Vec::new()), + resources: Vec::new(), } } @@ -28,15 +24,14 @@ impl StatelessBindGroupState { /// /// When this list of states is merged into a tracker, the memory /// accesses will be in a constant ascending order. - pub(crate) fn optimize(&self) { - let mut resources = self.resources.lock(); - resources.sort_unstable_by_key(|resource| resource.tracker_index()); + pub(crate) fn optimize(&mut self) { + self.resources + .sort_unstable_by_key(|resource| resource.tracker_index()); } /// Adds the given resource. - pub fn add_single(&self, resource: &Arc) { - let mut resources = self.resources.lock(); - resources.push(resource.clone()); + pub fn add_single(&mut self, resource: &Arc) { + self.resources.push(resource.clone()); } } diff --git a/wgpu-core/src/track/texture.rs b/wgpu-core/src/track/texture.rs index 592c9a6c8..77361a10f 100644 --- a/wgpu-core/src/track/texture.rs +++ b/wgpu-core/src/track/texture.rs @@ -21,7 +21,6 @@ use super::{range::RangedStates, PendingTransition, PendingTransitionList, TrackerIndex}; use crate::{ hal_api::HalApi, - lock::{rank, Mutex}, resource::{Texture, TextureInner, Trackable}, snatch::SnatchGuard, track::{ @@ -161,12 +160,12 @@ struct TextureBindGroupStateData { /// Stores all the textures that a bind group stores. #[derive(Debug)] pub(crate) struct TextureBindGroupState { - textures: Mutex>>, + textures: Vec>, } impl TextureBindGroupState { pub fn new() -> Self { Self { - textures: Mutex::new(rank::TEXTURE_BIND_GROUP_STATE_TEXTURES, Vec::new()), + textures: Vec::new(), } } @@ -174,20 +173,19 @@ impl TextureBindGroupState { /// /// When this list of states is merged into a tracker, the memory /// accesses will be in a constant ascending order. - pub(crate) fn optimize(&self) { - let mut textures = self.textures.lock(); - textures.sort_unstable_by_key(|v| v.texture.tracker_index()); + pub(crate) fn optimize(&mut self) { + self.textures + .sort_unstable_by_key(|v| v.texture.tracker_index()); } /// Adds the given resource with the given state. pub fn add_single( - &self, + &mut self, texture: &Arc>, selector: Option, state: TextureUses, ) { - let mut textures = self.textures.lock(); - textures.push(TextureBindGroupStateData { + self.textures.push(TextureBindGroupStateData { selector, texture: texture.clone(), usage: state, @@ -327,8 +325,7 @@ impl TextureUsageScope { &mut self, bind_group: &TextureBindGroupState, ) -> Result<(), ResourceUsageCompatibilityError> { - let textures = bind_group.textures.lock(); - for t in &*textures { + for t in bind_group.textures.iter() { unsafe { self.merge_single(&t.texture, t.selector.clone(), t.usage)? }; } @@ -616,8 +613,7 @@ impl TextureTracker { self.set_size(incoming_size); } - let textures = bind_group_state.textures.lock(); - for t in textures.iter() { + for t in bind_group_state.textures.iter() { let index = t.texture.tracker_index().as_usize(); scope.tracker_assert_in_bounds(index);