merge the texture and texture view trackers of BindGroupStates

This commit is contained in:
teoxoy 2024-08-02 19:49:57 +02:00 committed by Teodor Tanasoaia
parent 62af9d78b5
commit 4e777bd0e7
4 changed files with 29 additions and 53 deletions

View File

@ -2054,8 +2054,6 @@ impl<A: HalApi> Device<A> {
used_texture_ranges: &mut Vec<TextureInitTrackerAction<A>>,
snatch_guard: &'a SnatchGuard<'a>,
) -> Result<hal::TextureBinding<'a, A>, 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<A: HalApi> Device<A> {
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 {

View File

@ -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<A: HalApi> {
buffers: Vec<(Arc<Buffer<A>>, BufferUses)>,

View File

@ -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<T: ResourceUses> fmt::Display for InvalidUse<T> {
#[derive(Debug)]
pub(crate) struct BindGroupStates<A: HalApi> {
pub buffers: BufferBindGroupState<A>,
pub textures: TextureBindGroupState<A>,
pub views: StatelessTracker<resource::TextureView<A>>,
pub views: TextureViewBindGroupState<A>,
pub samplers: StatelessTracker<resource::Sampler<A>>,
}
@ -431,8 +430,7 @@ impl<A: HalApi> BindGroupStates<A> {
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<A: HalApi> BindGroupStates<A> {
/// 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<A: HalApi> RenderBundleScope<A> {
bind_group: &BindGroupStates<A>,
) -> 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<A: HalApi> Tracker<A> {
};
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)
};
}
}

View File

@ -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<A: HalApi> {
selector: Option<TextureSelector>,
texture: Arc<Texture<A>>,
usage: TextureUses,
pub(crate) struct TextureViewBindGroupState<A: HalApi> {
views: Vec<(Arc<TextureView<A>>, TextureUses)>,
}
/// Stores all the textures that a bind group stores.
#[derive(Debug)]
pub(crate) struct TextureBindGroupState<A: HalApi> {
textures: Vec<TextureBindGroupStateData<A>>,
}
impl<A: HalApi> TextureBindGroupState<A> {
impl<A: HalApi> TextureViewBindGroupState<A> {
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<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(&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<Texture<A>>,
selector: Option<TextureSelector>,
usage: TextureUses,
) {
self.textures.push(TextureBindGroupStateData {
selector,
texture,
usage,
});
pub fn insert_single(&mut self, view: Arc<TextureView<A>>, usage: TextureUses) {
self.views.push((view, usage));
}
}
@ -323,10 +305,10 @@ impl<A: HalApi> TextureUsageScope<A> {
/// method is called.
pub unsafe fn merge_bind_group(
&mut self,
bind_group: &TextureBindGroupState<A>,
bind_group: &TextureViewBindGroupState<A>,
) -> 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<A: HalApi> TextureTracker<A> {
pub unsafe fn set_and_remove_from_usage_scope_sparse(
&mut self,
scope: &mut TextureUsageScope<A>,
bind_group_state: &TextureBindGroupState<A>,
bind_group_state: &TextureViewBindGroupState<A>,
) {
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,