remove all internal BindGroupState Mutexes

This commit is contained in:
teoxoy 2024-08-02 18:42:10 +02:00 committed by Teodor Tanasoaia
parent 14170fd963
commit 3a5ad193db
5 changed files with 31 additions and 54 deletions

View File

@ -1998,7 +1998,7 @@ impl<A: HalApi> Device<A> {
fn create_sampler_binding<'a>(
self: &Arc<Self>,
used: &BindGroupStates<A>,
used: &mut BindGroupStates<A>,
binding: u32,
decl: &wgt::BindGroupLayoutEntry,
sampler: &'a Arc<Sampler<A>>,
@ -2177,7 +2177,7 @@ impl<A: HalApi> Device<A> {
(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<A: HalApi> Device<A> {
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);
}

View File

@ -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 { }

View File

@ -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<A: HalApi> {
buffers: Mutex<Vec<(Arc<Buffer<A>>, BufferUses)>>,
buffers: Vec<(Arc<Buffer<A>>, BufferUses)>,
}
impl<A: HalApi> BufferBindGroupState<A> {
pub fn new() -> Self {
Self {
buffers: Mutex::new(rank::BUFFER_BIND_GROUP_STATE_BUFFERS, Vec::new()),
buffers: Vec::new(),
}
}
@ -54,27 +53,23 @@ impl<A: HalApi> BufferBindGroupState<A> {
///
/// 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<Item = TrackerIndex> + '_ {
let buffers = self.buffers.lock();
buffers
self.buffers
.iter()
.map(|(ref b, _)| b.tracker_index())
.map(|(b, _)| b.tracker_index())
.collect::<Vec<_>>()
.into_iter()
}
/// Adds the given resource with the given state.
pub fn add_single(&self, buffer: &Arc<Buffer<A>>, state: BufferUses) {
let mut buffers = self.buffers.lock();
buffers.push((buffer.clone(), state));
pub fn add_single(&mut self, buffer: &Arc<Buffer<A>>, state: BufferUses) {
self.buffers.push((buffer.clone(), state));
}
}
@ -136,8 +131,7 @@ impl<A: HalApi> BufferUsageScope<A> {
&mut self,
bind_group: &BufferBindGroupState<A>,
) -> 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 {

View File

@ -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<T: Trackable> {
resources: Mutex<Vec<Arc<T>>>,
resources: Vec<Arc<T>>,
}
impl<T: Trackable> StatelessBindGroupState<T> {
pub fn new() -> Self {
Self {
resources: Mutex::new(rank::STATELESS_BIND_GROUP_STATE_RESOURCES, Vec::new()),
resources: Vec::new(),
}
}
@ -28,15 +24,14 @@ impl<T: Trackable> StatelessBindGroupState<T> {
///
/// 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<T>) {
let mut resources = self.resources.lock();
resources.push(resource.clone());
pub fn add_single(&mut self, resource: &Arc<T>) {
self.resources.push(resource.clone());
}
}

View File

@ -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<A: HalApi> {
/// Stores all the textures that a bind group stores.
#[derive(Debug)]
pub(crate) struct TextureBindGroupState<A: HalApi> {
textures: Mutex<Vec<TextureBindGroupStateData<A>>>,
textures: Vec<TextureBindGroupStateData<A>>,
}
impl<A: HalApi> TextureBindGroupState<A> {
pub fn new() -> Self {
Self {
textures: Mutex::new(rank::TEXTURE_BIND_GROUP_STATE_TEXTURES, Vec::new()),
textures: Vec::new(),
}
}
@ -174,20 +173,19 @@ impl<A: HalApi> TextureBindGroupState<A> {
///
/// 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<Texture<A>>,
selector: Option<TextureSelector>,
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<A: HalApi> TextureUsageScope<A> {
&mut self,
bind_group: &TextureBindGroupState<A>,
) -> 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<A: HalApi> TextureTracker<A> {
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);