Skip to content

Commit 7ca463a

Browse files
committed
Make submission index lockable.
1 parent a843c88 commit 7ca463a

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

wgpu-core/src/device/queue.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,11 +1068,10 @@ impl Queue {
10681068

10691069
// Fence lock must be acquired after the snatch lock everywhere to avoid deadlocks.
10701070
let mut fence = self.device.fence.write();
1071-
submit_index = self
1072-
.device
1073-
.active_submission_index
1074-
.fetch_add(1, Ordering::SeqCst)
1075-
+ 1;
1071+
1072+
let mut command_index_guard = self.device.command_indices.write();
1073+
command_index_guard.active_submission_index += 1;
1074+
submit_index = command_index_guard.active_submission_index;
10761075
let mut active_executions = Vec::new();
10771076

10781077
let mut used_surface_textures = track::TextureUsageScope::default();
@@ -1292,6 +1291,8 @@ impl Queue {
12921291
break 'error Err(e.into());
12931292
}
12941293

1294+
drop(command_index_guard);
1295+
12951296
// Advance the successful submission index.
12961297
self.device
12971298
.last_successful_submission_index

wgpu-core/src/device/resource.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ use core::sync::atomic::AtomicU64;
6262
#[cfg(not(supports_64bit_atomics))]
6363
use portable_atomic::AtomicU64;
6464

65+
pub(crate) struct CommandIndices {
66+
/// The index of the last command submission that was attempted.
67+
///
68+
/// Note that `fence` may never be signalled with this value, if the command
69+
/// submission failed. If you need to wait for everything running on a
70+
/// `Queue` to complete, wait for [`last_successful_submission_index`].
71+
///
72+
/// [`last_successful_submission_index`]: Device::last_successful_submission_index
73+
pub(crate) active_submission_index: hal::FenceValue,
74+
}
75+
6576
/// Structure describing a logical device. Some members are internally mutable,
6677
/// stored behind mutexes.
6778
pub struct Device {
@@ -74,14 +85,7 @@ pub struct Device {
7485

7586
pub(crate) command_allocator: command::CommandAllocator,
7687

77-
/// The index of the last command submission that was attempted.
78-
///
79-
/// Note that `fence` may never be signalled with this value, if the command
80-
/// submission failed. If you need to wait for everything running on a
81-
/// `Queue` to complete, wait for [`last_successful_submission_index`].
82-
///
83-
/// [`last_successful_submission_index`]: Device::last_successful_submission_index
84-
pub(crate) active_submission_index: hal::AtomicFenceValue,
88+
pub(crate) command_indices: RwLock<CommandIndices>,
8589

8690
/// The index of the last successful submission to this device's
8791
/// [`hal::Queue`].
@@ -91,7 +95,7 @@ pub struct Device {
9195
/// so waiting for this value won't hang waiting for work that was never
9296
/// submitted.
9397
///
94-
/// [`active_submission_index`]: Device::active_submission_index
98+
/// [`active_submission_index`]: CommandIndices::active_submission_index
9599
pub(crate) last_successful_submission_index: hal::AtomicFenceValue,
96100

97101
// NOTE: if both are needed, the `snatchable_lock` must be consistently acquired before the
@@ -276,7 +280,13 @@ impl Device {
276280
zero_buffer: ManuallyDrop::new(zero_buffer),
277281
label: desc.label.to_string(),
278282
command_allocator,
279-
active_submission_index: AtomicU64::new(0),
283+
command_indices: RwLock::new(
284+
rank::DEVICE_COMMAND_INDICES,
285+
CommandIndices {
286+
active_submission_index: 0,
287+
// By starting at one, we can put the result in a NonZeroU64.
288+
},
289+
),
280290
last_successful_submission_index: AtomicU64::new(0),
281291
fence: RwLock::new(rank::DEVICE_FENCE, ManuallyDrop::new(fence)),
282292
snatchable_lock: unsafe { SnatchLock::new(rank::DEVICE_SNATCHABLE_LOCK) },

wgpu-core/src/lock/rank.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ define_lock_ranks! {
130130

131131
rank BUFFER_BIND_GROUPS "Buffer::bind_groups" followed by { }
132132
rank BUFFER_INITIALIZATION_STATUS "Buffer::initialization_status" followed by { }
133-
rank DEVICE_DEFERRED_DESTROY "Device::deferred_destroy" followed by { }
133+
rank DEVICE_COMMAND_INDICES "Device::command_indices" followed by {}
134+
rank DEVICE_DEFERRED_DESTROY "Device::deferred_destroy" followed by {}
134135
rank DEVICE_FENCE "Device::fence" followed by { }
135136
#[allow(dead_code)]
136137
rank DEVICE_TRACE "Device::trace" followed by { }

0 commit comments

Comments
 (0)