GPU: the indirect scratch buffer stays readable between executions

Every indirect execution was followed by a move of the list's clamped
arguments back to the common state, and the next clamp moved them from
common to unordered access. Drivers may treat a barrier into or out of
the common state as a full wait, so even the window path
(VICTORY_INDIRECT_WINDOW=1), whose draws skip the clamp, still recorded
one barrier between every two draws.

The list now remembers that its scratch buffer was left readable as
indirect arguments: executions read it with no barrier of their own, the
next clamp moves it straight back to unordered access, and it returns to
common once, when the list closes or a bigger buffer replaces it. The
waits for earlier shader writes around the clamp are unchanged.

Tests: two windows of executions with no barrier between them, on WARP
and through a host list; the tracked state across clamps, growth and
reset; the existing clamp tests follow the new states. With
VICTORY_D3D12_DEBUG=1 in a diagnostics build, the host-list tests also
check that the debug layer reports nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Vyacheslav VenglovskyandClaude Opus 5.5 committed 2026-11-24 23:34:30 +01:00
1 parent 43ebd22d7d
commit 2136b81420
3 files changed
+336 -76

No files matched your search

+55
View File
@@ -1600,6 +1600,11 @@ struct Deferred {
backs: Vec<(ID3D12Resource, usize, [usize; 3])>,
/// Whether a step zeroes the indirect-work budget.
budget_zeroed: bool,
/// The indirect scratch buffer (`HostList::indirect_args`, by its
/// interface pointer) that a clamp in this recording left readable as
/// indirect arguments; 0 if none. It stays readable until the next
/// clamp into it ([`HostList::indirect_state`]).
indirect_readable: usize,
}
impl Deferred {
@@ -4406,6 +4411,10 @@ impl HostList {
pub(crate) fn close(&self) {
self.twins_to_depth();
let scratch = self.indirect_args.lock().unwrap_or_else(|e| e.into_inner()).clone();
if let Some((scratch, _)) = scratch {
self.indirect_common(&scratch);
}
#[cfg(feature = "diagnostics")]
if let Some((heap, back)) = self.stamps() {
let count = (self.notes.lock().unwrap_or_else(|e| e.into_inner()).len() as u32).min(STAMPS - 1);
@@ -5033,11 +5042,57 @@ impl HostList {
let old = slot.replace((buffer.clone(), alloc));
drop(slot);
if let Some((old_buf, _)) = old {
// It ends the list common, as every scratch buffer does.
self.indirect_common(&old_buf);
self.keep(&old_buf);
}
Some(buffer)
}
/// Moves `scratch` back to the common state if a clamp left it readable
/// as indirect arguments ([`HostList::indirect_readable`]): where the
/// list's first clamp expects it. Done before the list closes and before
/// a bigger scratch buffer replaces it: one barrier per list (where
/// there was one per indirect execution), and the list ends as it
/// begins however it is submitted.
fn indirect_common(&self, scratch: &ID3D12Resource) {
{
let mut deferred = self.deferred();
if deferred.indirect_readable != scratch.as_raw() as usize {
return;
}
deferred.indirect_readable = 0;
}
let barrier = transition(
scratch,
D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT,
D3D12_RESOURCE_STATE_COMMON,
);
// SAFETY: an open list and a live buffer this recording left in
// `INDIRECT_ARGUMENT`.
unsafe { self.list.ResourceBarrier(&[barrier]) };
}
/// The state the list's indirect scratch buffer `scratch` is in at this
/// point of the recording: indirect arguments if a clamp in this
/// recording left it so ([`HostList::indirect_readable`]), otherwise
/// common: the state a buffer is made in, and the one a recording
/// leaves it in when it closes or replaces it ([`HostList::indirect_common`]).
pub(crate) fn indirect_state(&self, scratch: &ID3D12Resource) -> D3D12_RESOURCE_STATES {
if self.deferred().indirect_readable == scratch.as_raw() as usize {
D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT
} else {
D3D12_RESOURCE_STATE_COMMON
}
}
/// A clamp recorded in this list left `scratch` readable as indirect
/// arguments. It stays so for every execution that reads it, until the
/// next clamp moves it back to unordered access.
pub(crate) fn indirect_readable(&self, scratch: &ID3D12Resource) {
self.deferred().indirect_readable = scratch.as_raw() as usize;
}
/// Keeps `object` alive until the list is reset.
pub(crate) fn keep(&self, object: &impl Interface) {
if let Ok(object) = object.cast::<windows::core::IUnknown>() {