diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index c5d321ad2..572c6e817 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -293,21 +293,14 @@ impl<'scope, 'snatch_guard, 'cmd_enc> State<'scope, 'snatch_guard, 'cmd_enc> { } for bind_group in self.pass.binder.list_active() { - unsafe { - self.intermediate_trackers - .set_and_remove_from_usage_scope_sparse(&mut self.pass.scope, &bind_group.used) - } + self.intermediate_trackers + .set_from_bind_group(&mut self.pass.scope, &bind_group.used); } // Add the state of the indirect buffer if it hasn't been hit before. - unsafe { - self.intermediate_trackers - .buffers - .set_and_remove_from_usage_scope_sparse( - &mut self.pass.scope.buffers, - indirect_buffer, - ); - } + self.intermediate_trackers + .buffers + .set_multiple(&mut self.pass.scope.buffers, indirect_buffer); CommandEncoder::drain_barriers( self.pass.base.raw_encoder, diff --git a/wgpu-core/src/track/buffer.rs b/wgpu-core/src/track/buffer.rs index 17d1970b1..7bf6cebdf 100644 --- a/wgpu-core/src/track/buffer.rs +++ b/wgpu-core/src/track/buffer.rs @@ -442,12 +442,7 @@ impl BufferTracker { /// over all elements in the usage scope. We use each the /// a given iterator of ids as a source of which IDs to look at. /// All the IDs must have first been added to the usage scope. - /// - /// # Safety - /// - /// [`Self::set_size`] must be called with the maximum possible Buffer ID before this - /// method is called. - pub unsafe fn set_and_remove_from_usage_scope_sparse( + pub fn set_multiple( &mut self, scope: &mut BufferUsageScope, index_source: impl IntoIterator, @@ -465,6 +460,9 @@ impl BufferTracker { if unsafe { !scope.metadata.contains_unchecked(index) } { continue; } + + // SAFETY: we checked that the index is in bounds for the scope, and + // called `set_size` to ensure it is valid for `self`. unsafe { self.insert_or_barrier_update( index, @@ -477,8 +475,6 @@ impl BufferTracker { }, ) }; - - unsafe { scope.metadata.remove(index) }; } } diff --git a/wgpu-core/src/track/metadata.rs b/wgpu-core/src/track/metadata.rs index 2d63b1a7b..4c67f7fda 100644 --- a/wgpu-core/src/track/metadata.rs +++ b/wgpu-core/src/track/metadata.rs @@ -130,14 +130,6 @@ impl ResourceMetadata { }; iterate_bitvec_indices(&self.owned) } - - /// Remove the resource with the given index from the set. - pub(super) unsafe fn remove(&mut self, index: usize) { - unsafe { - *self.resources.get_unchecked_mut(index) = None; - } - self.owned.set(index, false); - } } /// A source of resource metadata. diff --git a/wgpu-core/src/track/mod.rs b/wgpu-core/src/track/mod.rs index 3da7b95c6..aa59b7ee8 100644 --- a/wgpu-core/src/track/mod.rs +++ b/wgpu-core/src/track/mod.rs @@ -641,8 +641,7 @@ impl Tracker { } /// Iterates through all resources in the given bind group and adopts - /// the state given for those resources in the UsageScope. It also - /// removes all touched resources from the usage scope. + /// the state given for those resources in the UsageScope. /// /// If a transition is needed to get the resources into the needed /// state, those transitions are stored within the tracker. A @@ -650,32 +649,17 @@ impl Tracker { /// [`TextureTracker::drain_transitions`] is needed to get those transitions. /// /// This is a really funky method used by Compute Passes to generate - /// barriers after a call to dispatch without needing to iterate - /// over all elements in the usage scope. We use each the - /// bind group as a source of which IDs to look at. The bind groups - /// must have first been added to the usage scope. + /// barriers for each dispatch. We use the bind group as a source of which + /// IDs to look at. /// /// Only stateful things are merged in here, all other resources are owned /// indirectly by the bind group. - /// - /// # Safety - /// - /// The maximum ID given by each bind group resource must be less than the - /// value given to `set_size` - pub unsafe fn set_and_remove_from_usage_scope_sparse( - &mut self, - scope: &mut UsageScope, - bind_group: &BindGroupStates, - ) { - unsafe { - self.buffers.set_and_remove_from_usage_scope_sparse( - &mut scope.buffers, - bind_group.buffers.used_tracker_indices(), - ) - }; - unsafe { - self.textures - .set_and_remove_from_usage_scope_sparse(&mut scope.textures, &bind_group.views) - }; + pub fn set_from_bind_group(&mut self, scope: &mut UsageScope, bind_group: &BindGroupStates) { + self.buffers.set_multiple( + &mut scope.buffers, + bind_group.buffers.used_tracker_indices(), + ); + self.textures + .set_multiple(&mut scope.textures, &bind_group.views); } } diff --git a/wgpu-core/src/track/texture.rs b/wgpu-core/src/track/texture.rs index bfa1e3f3e..b672b2e76 100644 --- a/wgpu-core/src/track/texture.rs +++ b/wgpu-core/src/track/texture.rs @@ -611,12 +611,7 @@ impl TextureTracker { /// over all elements in the usage scope. We use each the /// bind group as a source of which IDs to look at. The bind groups /// must have first been added to the usage scope. - /// - /// # Safety - /// - /// [`Self::set_size`] must be called with the maximum possible Buffer ID before this - /// method is called. - pub unsafe fn set_and_remove_from_usage_scope_sparse( + pub fn set_multiple( &mut self, scope: &mut TextureUsageScope, bind_group_state: &TextureViewBindGroupState, @@ -634,6 +629,8 @@ impl TextureTracker { continue; } let texture_selector = &view.parent.full_range; + // SAFETY: we checked that the index is in bounds for the scope, and + // called `set_size` to ensure it is valid for `self`. unsafe { insert_or_barrier_update( texture_selector, @@ -649,8 +646,6 @@ impl TextureTracker { &mut self.temp, ) }; - - unsafe { scope.metadata.remove(index) }; } } }