Merge PR 220

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 18:57:48 -06:00
commit eca1d231e3
4 files changed
+1198 -102

No files matched your search

+154 -4
View File
@@ -789,8 +789,13 @@ pub(crate) struct HostList {
/// The pipelines the list binds, for reports of slow submissions.
pipelines: Mutex<std::collections::BTreeSet<String>>,
/// The work its indirect executions may still do (`indirect`): a
/// shader-writable word, zeroed when the list is submitted.
/// shader-writable word, zeroed when the list is submitted, and after
/// it what the clamp notes about mesh records. In memory the CPU maps.
budget: OnceLock<Option<(ID3D12Resource, usize)>>,
/// The same words in the card's own memory ([`card_budget`]), which the
/// clamp uses instead when there is one: zeroed in the list before its
/// first clamp, its notes copied to `budget` when the list closes.
card_budget: OnceLock<Option<ID3D12Resource>>,
/// Device-local scratch buffer for clamped indirect arguments (`indirect`),
/// grown to the largest indirect execution recorded on the list.
indirect_args: Mutex<Option<(ID3D12Resource, u64)>>,
@@ -1270,6 +1275,16 @@ pub(crate) fn dred_on() -> bool {
/// budget buffers, a run late).
static MESH_GROUPS: [std::sync::atomic::AtomicU64; 2] = [const { std::sync::atomic::AtomicU64::new(0) }; 2];
/// Whether a list's indirect-work budget lives in the card's own memory
/// (`VICTORY_INDIRECT_CARD_BUDGET=0` keeps it in memory the CPU maps, as
/// before). The clamp charges it with atomics, at least two in a row for
/// every clamp; in mapped memory on a card of its own each of them
/// crosses the bus.
fn card_budget() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_INDIRECT_CARD_BUDGET").is_none_or(|v| v != "0"))
}
/// Whether each list of a submission runs and is timed on its own, slow
/// ones logged with their commands (`VICTORY_GPU_TIMING`, diagnostics
/// builds; the commands are noted with `VICTORY_D3D12_DRED` on).
@@ -1655,8 +1670,14 @@ struct Deferred {
/// memory they go to, the bytes of a row, the rows, the title's row
/// pitch.
backs: Vec<(ID3D12Resource, usize, [usize; 3])>,
/// Whether a step zeroes the indirect-work budget.
/// Whether the indirect-work budget is zeroed for this recording (a
/// step on the CPU, or in the list for the card's copy).
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 {
@@ -2994,6 +3015,7 @@ impl Host {
prelude,
pipelines: Mutex::default(),
budget: OnceLock::new(),
card_budget: OnceLock::new(),
indirect_args: Mutex::default(),
_alive: Counted::new(),
#[cfg(feature = "diagnostics")]
@@ -4607,6 +4629,11 @@ 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);
}
self.card_budget_out();
#[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);
@@ -5190,6 +5217,7 @@ impl HostList {
Some((b, d as usize))
})
.as_ref()?;
let card = self.card_budget.get_or_init(|| self.make_card_budget()).as_ref();
let mut deferred = self.deferred();
if !deferred.budget_zeroed {
deferred.budget_zeroed = true;
@@ -5197,7 +5225,8 @@ impl HostList {
// SAFETY: the list's own mapped words, written before it runs.
// The words after the budget hold what the clamp noted about the
// list's last run (mesh dispatches: the largest group count
// asked for, the groups asked in all): taken, then zeroed.
// asked for, the groups asked in all; with the card's copy, as
// the list's end copied them here): taken, then zeroed.
deferred.copies.push(Step::Host(Box::new(move || unsafe {
let words = at as *mut u32;
let (largest, all) = (words.add(1).read_volatile(), words.add(2).read_volatile());
@@ -5207,9 +5236,84 @@ impl HostList {
words.add(k).write_volatile(0);
}
})));
drop(deferred);
if let Some(card) = card {
self.zero_card_budget(card);
}
}
// SAFETY: a live buffer.
Some(unsafe { buffer.GetGPUVirtualAddress() })
Some(unsafe { card.unwrap_or(buffer).GetGPUVirtualAddress() })
}
/// The budget's words in the card's own memory ([`card_budget`]), if
/// the list can zero them itself (`WriteBufferImmediate`).
fn make_card_budget(&self) -> Option<ID3D12Resource> {
if !card_budget() {
return None;
}
self.list.cast::<ID3D12GraphicsCommandList2>().ok()?;
let buffer = host()?.uav_buffer(256)?;
name(&buffer, || "indirect budget".to_owned());
Some(buffer)
}
/// Records, before the list's first clamp, the zeroing of the card's
/// copy of the budget: common, written as a copy, then left for
/// unordered access until the list closes ([`HostList::card_budget_out`]).
fn zero_card_budget(&self, card: &ID3D12Resource) {
let Ok(list2) = self.list.cast::<ID3D12GraphicsCommandList2>() else {
return;
};
// SAFETY: a live buffer.
let at = unsafe { card.GetGPUVirtualAddress() };
let zeros = [0u64, 4, 8].map(|offset| D3D12_WRITEBUFFERIMMEDIATE_PARAMETER {
Dest: at + offset,
Value: 0,
});
// SAFETY: an open list; the buffer is the list's own and in the
// common state at the start of every recording.
unsafe {
self.list.ResourceBarrier(&[transition(
card,
D3D12_RESOURCE_STATE_COMMON,
D3D12_RESOURCE_STATE_COPY_DEST,
)]);
list2.WriteBufferImmediate(zeros.len() as u32, zeros.as_ptr(), None);
self.list.ResourceBarrier(&[transition(
card,
D3D12_RESOURCE_STATE_COPY_DEST,
D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
)]);
}
}
/// Before the list closes, if this recording charged the card's copy
/// of the budget: its notes on mesh records go to the mapped words
/// (read at the next submission), and it goes back to common.
fn card_budget_out(&self) {
if !self.deferred().budget_zeroed {
return;
}
let (Some(Some(card)), Some(Some((mapped, _)))) =
(self.card_budget.get(), self.budget.get())
else {
return;
};
// SAFETY: an open list; both buffers are the list's own. The
// mapped one is promoted from common to the copy destination.
unsafe {
self.list.ResourceBarrier(&[transition(
card,
D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
D3D12_RESOURCE_STATE_COPY_SOURCE,
)]);
self.list.CopyBufferRegion(mapped, 4, card, 4, 8);
self.list.ResourceBarrier(&[transition(
card,
D3D12_RESOURCE_STATE_COPY_SOURCE,
D3D12_RESOURCE_STATE_COMMON,
)]);
}
}
/// A device-local unordered-access buffer of at least `size` bytes for
@@ -5234,11 +5338,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>() {