diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 2d354a225..e66e45206 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -2054,8 +2054,6 @@ impl Device { used_texture_ranges: &mut Vec>, snatch_guard: &'a SnatchGuard<'a>, ) -> Result, binding_model::CreateBindGroupError> { - used.views.insert_single(view.clone()); - view.same_device(self)?; let (pub_usage, internal_use) = self.texture_use_parameters( @@ -2064,12 +2062,10 @@ impl Device { view, "SampledTexture, ReadonlyStorageTexture or WriteonlyStorageTexture", )?; - let texture = &view.parent; - // Careful here: the texture may no longer have its own ref count, - // if it was deleted by the user. - used.textures - .insert_single(texture.clone(), Some(view.selector.clone()), internal_use); + used.views.insert_single(view.clone(), internal_use); + + let texture = &view.parent; texture.check_usage(pub_usage)?; used_texture_ranges.push(TextureInitTrackerAction { diff --git a/wgpu-core/src/track/buffer.rs b/wgpu-core/src/track/buffer.rs index a24148230..8fdcf3167 100644 --- a/wgpu-core/src/track/buffer.rs +++ b/wgpu-core/src/track/buffer.rs @@ -37,7 +37,7 @@ impl ResourceUses for BufferUses { } } -/// Stores all the buffers that a bind group stores. +/// Stores a bind group's buffers + their usages (within the bind group). #[derive(Debug)] pub(crate) struct BufferBindGroupState { buffers: Vec<(Arc>, BufferUses)>, diff --git a/wgpu-core/src/track/mod.rs b/wgpu-core/src/track/mod.rs index c8b634ed7..82c1406db 100644 --- a/wgpu-core/src/track/mod.rs +++ b/wgpu-core/src/track/mod.rs @@ -119,8 +119,8 @@ pub(crate) use buffer::{ use metadata::{ResourceMetadata, ResourceMetadataProvider}; pub(crate) use stateless::StatelessTracker; pub(crate) use texture::{ - DeviceTextureTracker, TextureBindGroupState, TextureSelector, TextureTracker, - TextureTrackerSetSingle, TextureUsageScope, + DeviceTextureTracker, TextureSelector, TextureTracker, TextureTrackerSetSingle, + TextureUsageScope, TextureViewBindGroupState, }; use wgt::strict_assert_ne; @@ -422,8 +422,7 @@ impl fmt::Display for InvalidUse { #[derive(Debug)] pub(crate) struct BindGroupStates { pub buffers: BufferBindGroupState, - pub textures: TextureBindGroupState, - pub views: StatelessTracker>, + pub views: TextureViewBindGroupState, pub samplers: StatelessTracker>, } @@ -431,8 +430,7 @@ impl BindGroupStates { pub fn new() -> Self { Self { buffers: BufferBindGroupState::new(), - textures: TextureBindGroupState::new(), - views: StatelessTracker::new(), + views: TextureViewBindGroupState::new(), samplers: StatelessTracker::new(), } } @@ -443,7 +441,7 @@ impl BindGroupStates { /// accesses will be in a constant ascending order. pub fn optimize(&mut self) { self.buffers.optimize(); - self.textures.optimize(); + self.views.optimize(); } } @@ -484,7 +482,7 @@ impl RenderBundleScope { bind_group: &BindGroupStates, ) -> Result<(), ResourceUsageCompatibilityError> { unsafe { self.buffers.merge_bind_group(&bind_group.buffers)? }; - unsafe { self.textures.merge_bind_group(&bind_group.textures)? }; + unsafe { self.textures.merge_bind_group(&bind_group.views)? }; Ok(()) } @@ -551,7 +549,7 @@ impl<'a, A: HalApi> UsageScope<'a, A> { ) -> Result<(), ResourceUsageCompatibilityError> { unsafe { self.buffers.merge_bind_group(&bind_group.buffers)?; - self.textures.merge_bind_group(&bind_group.textures)?; + self.textures.merge_bind_group(&bind_group.views)?; } Ok(()) @@ -653,7 +651,7 @@ impl Tracker { }; unsafe { self.textures - .set_and_remove_from_usage_scope_sparse(&mut scope.textures, &bind_group.textures) + .set_and_remove_from_usage_scope_sparse(&mut scope.textures, &bind_group.views) }; } } diff --git a/wgpu-core/src/track/texture.rs b/wgpu-core/src/track/texture.rs index 6c177829f..243bd2520 100644 --- a/wgpu-core/src/track/texture.rs +++ b/wgpu-core/src/track/texture.rs @@ -21,7 +21,7 @@ use super::{range::RangedStates, PendingTransition, PendingTransitionList, TrackerIndex}; use crate::{ hal_api::HalApi, - resource::{Texture, TextureInner, Trackable}, + resource::{Texture, TextureInner, TextureView, Trackable}, snatch::SnatchGuard, track::{ invalid_resource_state, skip_barrier, ResourceMetadata, ResourceMetadataProvider, @@ -150,23 +150,14 @@ impl ComplexTextureState { } } +/// Stores a bind group's texture views + their usages (within the bind group). #[derive(Debug)] -struct TextureBindGroupStateData { - selector: Option, - texture: Arc>, - usage: TextureUses, +pub(crate) struct TextureViewBindGroupState { + views: Vec<(Arc>, TextureUses)>, } - -/// Stores all the textures that a bind group stores. -#[derive(Debug)] -pub(crate) struct TextureBindGroupState { - textures: Vec>, -} -impl TextureBindGroupState { +impl TextureViewBindGroupState { pub fn new() -> Self { - Self { - textures: Vec::new(), - } + Self { views: Vec::new() } } /// Optimize the texture bind group state by sorting it by ID. @@ -174,22 +165,13 @@ 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(&mut self) { - self.textures - .sort_unstable_by_key(|v| v.texture.tracker_index()); + self.views + .sort_unstable_by_key(|(view, _)| view.parent.tracker_index()); } /// Adds the given resource with the given state. - pub fn insert_single( - &mut self, - texture: Arc>, - selector: Option, - usage: TextureUses, - ) { - self.textures.push(TextureBindGroupStateData { - selector, - texture, - usage, - }); + pub fn insert_single(&mut self, view: Arc>, usage: TextureUses) { + self.views.push((view, usage)); } } @@ -323,10 +305,10 @@ impl TextureUsageScope { /// method is called. pub unsafe fn merge_bind_group( &mut self, - bind_group: &TextureBindGroupState, + bind_group: &TextureViewBindGroupState, ) -> Result<(), ResourceUsageCompatibilityError> { - for t in bind_group.textures.iter() { - unsafe { self.merge_single(&t.texture, t.selector.clone(), t.usage)? }; + for (view, usage) in bind_group.views.iter() { + unsafe { self.merge_single(&view.parent, Some(view.selector.clone()), *usage)? }; } Ok(()) @@ -606,21 +588,21 @@ impl TextureTracker { pub unsafe fn set_and_remove_from_usage_scope_sparse( &mut self, scope: &mut TextureUsageScope, - bind_group_state: &TextureBindGroupState, + bind_group_state: &TextureViewBindGroupState, ) { let incoming_size = scope.set.simple.len(); if incoming_size > self.start_set.simple.len() { self.set_size(incoming_size); } - for t in bind_group_state.textures.iter() { - let index = t.texture.tracker_index().as_usize(); + for (view, _) in bind_group_state.views.iter() { + let index = view.parent.tracker_index().as_usize(); scope.tracker_assert_in_bounds(index); if unsafe { !scope.metadata.contains_unchecked(index) } { continue; } - let texture_selector = &t.texture.full_range; + let texture_selector = &view.parent.full_range; unsafe { insert_or_barrier_update( texture_selector,