Skip to content

Commit

Permalink
Properly Barrier Compute Indirect Buffers (#2810)
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald authored Jun 26, 2022
1 parent 770935a commit 533fc13
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
11 changes: 11 additions & 0 deletions wgpu-core/src/command/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,15 @@ impl<A: HalApi> State<A> {
Ok(())
}

// `extra_buffer` is there to represent the indirect buffer that is also part of the usage scope.
fn flush_states(
&mut self,
raw_encoder: &mut A::CommandEncoder,
base_trackers: &mut Tracker<A>,
bind_group_guard: &Storage<BindGroup<A>, id::BindGroupId>,
buffer_guard: &Storage<Buffer<A>, id::BufferId>,
texture_guard: &Storage<Texture<A>, id::TextureId>,
indirect_buffer: Option<id::Valid<id::BufferId>>,
) -> Result<(), UsageConflict> {
for id in self.binder.list_active() {
unsafe {
Expand All @@ -295,6 +297,13 @@ impl<A: HalApi> State<A> {
}
}

// Add the state of the indirect buffer if it hasn't been hit before.
unsafe {
base_trackers
.buffers
.set_and_remove_from_usage_scope_sparse(&mut self.scope.buffers, indirect_buffer);
}

log::trace!("Encoding dispatch barriers");

CommandBuffer::drain_barriers(raw_encoder, base_trackers, buffer_guard, texture_guard);
Expand Down Expand Up @@ -584,6 +593,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
&*bind_group_guard,
&*buffer_guard,
&*texture_guard,
None,
)
.map_pass_err(scope)?;

Expand Down Expand Up @@ -659,6 +669,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
&*bind_group_guard,
&*buffer_guard,
&*texture_guard,
Some(id::Valid(buffer_id)),
)
.map_pass_err(scope)?;
unsafe {
Expand Down
15 changes: 7 additions & 8 deletions wgpu-core/src/track/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,8 @@ impl<A: hub::HalApi> BufferTracker<A> {
/// 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.
/// 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
///
Expand All @@ -477,15 +477,15 @@ impl<A: hub::HalApi> BufferTracker<A> {
pub unsafe fn set_and_remove_from_usage_scope_sparse(
&mut self,
scope: &mut BufferUsageScope<A>,
bind_group_state: &BufferBindGroupState<A>,
id_source: impl IntoIterator<Item = Valid<BufferId>>,
) {
let incoming_size = scope.state.len();
if incoming_size > self.start.len() {
self.set_size(incoming_size);
}

for &(id, ref ref_count, _) in bind_group_state.buffers.iter() {
let (index32, epoch, _) = id.0.unzip();
for id in id_source {
let (index32, _, _) = id.0.unzip();
let index = index32 as usize;

scope.debug_assert_in_bounds(index);
Expand All @@ -504,9 +504,8 @@ impl<A: hub::HalApi> BufferTracker<A> {
state: &scope.state,
},
None,
ResourceMetadataProvider::Direct {
epoch,
ref_count: Cow::Borrowed(ref_count),
ResourceMetadataProvider::Indirect {
metadata: &scope.metadata,
},
&mut self.temp,
);
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/track/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ impl<A: hub::HalApi> Tracker<A> {
bind_group: &BindGroupStates<A>,
) {
self.buffers
.set_and_remove_from_usage_scope_sparse(&mut scope.buffers, &bind_group.buffers);
.set_and_remove_from_usage_scope_sparse(&mut scope.buffers, bind_group.buffers.used());
self.textures.set_and_remove_from_usage_scope_sparse(
textures,
&mut scope.textures,
Expand Down

0 comments on commit 533fc13

Please sign in to comment.