Skip to content

Commit

Permalink
[core] Allocate queue submission indices at the last minute.
Browse files Browse the repository at this point in the history
As of gfx-rs#5976, we no longer mark resources with the index of the latest
queue submission at which they were used, so in
`wgpu_core::Global::queue_submit`, rather than allocating a new
submission index from `Device::active_submission_index` before
processing all the command buffers, allocate it afterwards.
This shrinks the criticial section between submission index allocation
and actual submission, making gfx-rs#5978 easier to address.
  • Loading branch information
jimblandy committed Jul 22, 2024
1 parent d2e308d commit e52db9e
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions wgpu-core/src/device/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,12 +1063,11 @@ impl Global {
// Fence lock must be acquired after the snatch lock everywhere to avoid deadlocks.
let mut fence_guard = device.fence.write();
let fence = fence_guard.as_mut().unwrap();
let submit_index = device
.active_submission_index
.fetch_add(1, Ordering::SeqCst)
+ 1;
let mut active_executions = Vec::new();

#[cfg(feature = "trace")]
let mut tracing_commands = Vec::new();

let mut used_surface_textures = track::TextureUsageScope::default();

// Use a hashmap here to deduplicate the surface textures that are used in the command buffers.
Expand Down Expand Up @@ -1100,9 +1099,8 @@ impl Global {
};

#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
trace.add(Action::Submit(
submit_index,
if device.trace.lock().is_some() {
tracing_commands.push(
cmdbuf
.data
.lock()
Expand All @@ -1111,7 +1109,7 @@ impl Global {
.commands
.take()
.unwrap(),
));
);
}

cmdbuf.same_device_as(queue.as_ref())?;
Expand Down Expand Up @@ -1241,8 +1239,6 @@ impl Global {
pending_textures: FastHashMap::default(),
});
}

log::trace!("Device after submission {}", submit_index);
}
}

Expand Down Expand Up @@ -1286,9 +1282,25 @@ impl Global {
}
}

if let Some(pending_execution) =
pending_writes.pre_submit(&device.command_allocator, device.raw(), queue.raw())?
{
let submit_index = device
.active_submission_index
.fetch_add(1, Ordering::SeqCst)
+ 1;

log::trace!("Device after submission {}", submit_index);

#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
for commands in tracing_commands {
trace.add(Action::Submit(submit_index, commands));
}
}

if let Some(pending_execution) = pending_writes.pre_submit(
&device.command_allocator,
device.raw(),
queue.raw(),
)? {
active_executions.insert(0, pending_execution);
}

Expand Down

0 comments on commit e52db9e

Please sign in to comment.