Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly Barrier Compute Indirect Buffers #2810

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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