Make submission index lockable.

This commit is contained in:
Vecvec 2025-04-10 07:26:13 +12:00 committed by Connor Fitzgerald
parent 09a641bdf4
commit 382a1e3c9b
3 changed files with 27 additions and 16 deletions

View File

@ -1068,11 +1068,10 @@ impl Queue {
// Fence lock must be acquired after the snatch lock everywhere to avoid deadlocks.
let mut fence = self.device.fence.write();
submit_index = self
.device
.active_submission_index
.fetch_add(1, Ordering::SeqCst)
+ 1;
let mut command_index_guard = self.device.command_indices.write();
command_index_guard.active_submission_index += 1;
submit_index = command_index_guard.active_submission_index;
let mut active_executions = Vec::new();
let mut used_surface_textures = track::TextureUsageScope::default();
@ -1292,6 +1291,8 @@ impl Queue {
break 'error Err(e.into());
}
drop(command_index_guard);
// Advance the successful submission index.
self.device
.last_successful_submission_index

View File

@ -62,6 +62,17 @@ use core::sync::atomic::AtomicU64;
#[cfg(not(supports_64bit_atomics))]
use portable_atomic::AtomicU64;
pub(crate) struct CommandIndices {
/// The index of the last command submission that was attempted.
///
/// Note that `fence` may never be signalled with this value, if the command
/// submission failed. If you need to wait for everything running on a
/// `Queue` to complete, wait for [`last_successful_submission_index`].
///
/// [`last_successful_submission_index`]: Device::last_successful_submission_index
pub(crate) active_submission_index: hal::FenceValue,
}
/// Structure describing a logical device. Some members are internally mutable,
/// stored behind mutexes.
pub struct Device {
@ -74,14 +85,7 @@ pub struct Device {
pub(crate) command_allocator: command::CommandAllocator,
/// The index of the last command submission that was attempted.
///
/// Note that `fence` may never be signalled with this value, if the command
/// submission failed. If you need to wait for everything running on a
/// `Queue` to complete, wait for [`last_successful_submission_index`].
///
/// [`last_successful_submission_index`]: Device::last_successful_submission_index
pub(crate) active_submission_index: hal::AtomicFenceValue,
pub(crate) command_indices: RwLock<CommandIndices>,
/// The index of the last successful submission to this device's
/// [`hal::Queue`].
@ -91,7 +95,7 @@ pub struct Device {
/// so waiting for this value won't hang waiting for work that was never
/// submitted.
///
/// [`active_submission_index`]: Device::active_submission_index
/// [`active_submission_index`]: CommandIndices::active_submission_index
pub(crate) last_successful_submission_index: hal::AtomicFenceValue,
// NOTE: if both are needed, the `snatchable_lock` must be consistently acquired before the
@ -276,7 +280,12 @@ impl Device {
zero_buffer: ManuallyDrop::new(zero_buffer),
label: desc.label.to_string(),
command_allocator,
active_submission_index: AtomicU64::new(0),
command_indices: RwLock::new(
rank::DEVICE_COMMAND_INDICES,
CommandIndices {
active_submission_index: 0,
},
),
last_successful_submission_index: AtomicU64::new(0),
fence: RwLock::new(rank::DEVICE_FENCE, ManuallyDrop::new(fence)),
snatchable_lock: unsafe { SnatchLock::new(rank::DEVICE_SNATCHABLE_LOCK) },

View File

@ -130,7 +130,8 @@ define_lock_ranks! {
rank BUFFER_BIND_GROUPS "Buffer::bind_groups" followed by { }
rank BUFFER_INITIALIZATION_STATUS "Buffer::initialization_status" followed by { }
rank DEVICE_DEFERRED_DESTROY "Device::deferred_destroy" followed by { }
rank DEVICE_COMMAND_INDICES "Device::command_indices" followed by {}
rank DEVICE_DEFERRED_DESTROY "Device::deferred_destroy" followed by {}
rank DEVICE_FENCE "Device::fence" followed by { }
#[allow(dead_code)]
rank DEVICE_TRACE "Device::trace" followed by { }