GPU: the indirect budget lives in the card's memory
The clamp charges the list's budget with atomics: at least an atomic read and a compare-and-swap in a row for every clamp, so for every indirect execution clamped on its own. The budget was in memory the CPU maps (zeroed on the CPU at submission), so on a card of its own each of those crosses the bus. Now the words live in a device-local buffer of the list. The list zeroes it before its first clamp (WriteBufferImmediate, as a copy), the clamps charge it as before, and when the list closes the clamp's notes on mesh records are copied to the mapped words, where the next submission reads them for the frame line as before. VICTORY_INDIRECT_CARD_BUDGET=0 keeps the old place, for an A/B; lists without ID3D12GraphicsCommandList2 keep it too. Test: a recording whose clamps spend the whole budget, then one with a small record after the reset, which runs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
32c6229990
commit
d5c15cb9dc
2 files changed
+206
-9
No files matched your search
@@ -60,11 +60,11 @@ static const uint BUDGET = 1u << 27;
|
||||
static const uint GROUPS = 1u << 20;
|
||||
// Each group charges the budget once: the costs of its records go here,
|
||||
// one thread charges their sum, saturating as each record would in lane
|
||||
// order, and every thread then reads whether its own record fitted. The
|
||||
// budget word is CPU-visible memory (zeroed on the CPU at submission), so
|
||||
// on a card of its own every atomic on it crosses the bus; a compare and
|
||||
// swap per record, 64 threads racing for one word, cost a round for each
|
||||
// record of a window.
|
||||
// order, and every thread then reads whether its own record fitted. A
|
||||
// compare and swap per record, 64 threads racing for one word, cost a
|
||||
// round for each record of a window, and in memory the CPU maps (where the
|
||||
// budget was, and is with VICTORY_INDIRECT_CARD_BUDGET=0) every atomic
|
||||
// crosses the bus on a card of its own.
|
||||
groupshared uint costs[64];
|
||||
groupshared uint fits[64];
|
||||
groupshared uint largest_asked;
|
||||
@@ -1370,6 +1370,108 @@ void main() {
|
||||
assert_eq!(spent, BUDGET_CAP, "the record that did not fit took the rest");
|
||||
}
|
||||
|
||||
/// The list's budget starts empty at each submission, wherever it
|
||||
/// lives (the card's copy is zeroed in the list): a recording whose
|
||||
/// clamps spend all of it is followed, after the reset, by one whose
|
||||
/// small record still runs.
|
||||
#[test]
|
||||
fn host_list_budget_starts_empty_at_each_submission() {
|
||||
let Some(host) = super::host() else {
|
||||
return;
|
||||
};
|
||||
let Ok(list) = host.new_list() else {
|
||||
return;
|
||||
};
|
||||
let _ = host.log_messages();
|
||||
let worker = compile_cs(&host.device, WORKER_CS).expect("worker shader must compile");
|
||||
const KIND_DISPATCH: u32 = 2;
|
||||
const STRIDE: u32 = 12;
|
||||
let signature = {
|
||||
let arg = D3D12_INDIRECT_ARGUMENT_DESC {
|
||||
Type: D3D12_INDIRECT_ARGUMENT_TYPE(KIND_DISPATCH as i32),
|
||||
..Default::default()
|
||||
};
|
||||
let desc = D3D12_COMMAND_SIGNATURE_DESC {
|
||||
ByteStride: STRIDE,
|
||||
NumArgumentDescs: 1,
|
||||
pArgumentDescs: &arg,
|
||||
NodeMask: 0,
|
||||
};
|
||||
let mut sig: Option<ID3D12CommandSignature> = None;
|
||||
// SAFETY: a valid single-argument command signature description.
|
||||
unsafe {
|
||||
host.device
|
||||
.CreateCommandSignature(&desc, None::<&ID3D12RootSignature>, &mut sig)
|
||||
.unwrap();
|
||||
}
|
||||
sig.unwrap()
|
||||
};
|
||||
// Three records of 65,535 x 16 groups: about half the budget each.
|
||||
let Some((big, big_ptr)) = host.shared_buffer(3 * u64::from(STRIDE)) else {
|
||||
return;
|
||||
};
|
||||
let Some((small, small_ptr)) = host.shared_buffer(u64::from(STRIDE)) else {
|
||||
return;
|
||||
};
|
||||
let Some((hits_buf, hits_ptr)) = host.shared_buffer(256) else {
|
||||
return;
|
||||
};
|
||||
// SAFETY: mapped buffers of these sizes; nothing runs yet.
|
||||
unsafe {
|
||||
for i in 0..3 {
|
||||
let words = [65535u32, 16, 1];
|
||||
std::ptr::copy_nonoverlapping(
|
||||
words.as_ptr().cast::<u8>(),
|
||||
big_ptr.add(i * STRIDE as usize),
|
||||
12,
|
||||
);
|
||||
}
|
||||
std::ptr::copy_nonoverlapping([1u32, 1, 1].as_ptr().cast::<u8>(), small_ptr, 12);
|
||||
std::ptr::write_bytes(hits_ptr, 0, 256);
|
||||
}
|
||||
// SAFETY: a live buffer.
|
||||
let hits_gpu = unsafe { hits_buf.GetGPUVirtualAddress() };
|
||||
for round in 0..2 {
|
||||
// Spends the budget (clamped, not executed), then one group.
|
||||
bound(&list, (&big, 0), None, (KIND_DISPATCH, STRIDE, 3)).unwrap();
|
||||
let scratch = bound(&list, (&small, 0), None, (KIND_DISPATCH, STRIDE, 1)).unwrap();
|
||||
// SAFETY: an open list; the worker's root signature and
|
||||
// pipeline; buffers the test holds until the list has run.
|
||||
unsafe {
|
||||
list.list.SetComputeRootSignature(&worker.root);
|
||||
list.list.SetPipelineState(&worker.pso);
|
||||
list.list.SetComputeRootUnorderedAccessView(0, hits_gpu);
|
||||
list.list.ExecuteIndirect(&signature, 1, &scratch, 0, None, 0);
|
||||
}
|
||||
list.close();
|
||||
assert_eq!(host.log_messages(), None, "the debug layer found an error");
|
||||
host.execute(&[&list]);
|
||||
list.reset();
|
||||
// SAFETY: the list has run; a mapped buffer.
|
||||
let hits = unsafe { (hits_ptr as *const u32).read_volatile() };
|
||||
// The small record comes after the big ones spent the budget:
|
||||
// it runs empty, in every round alike (none carries over).
|
||||
assert_eq!(hits, 0, "round {round}: the spent budget empties the record after it");
|
||||
}
|
||||
// A recording with only the small record: the budget is empty
|
||||
// again, so it runs.
|
||||
let scratch = bound(&list, (&small, 0), None, (KIND_DISPATCH, STRIDE, 1)).unwrap();
|
||||
// SAFETY: as above.
|
||||
unsafe {
|
||||
list.list.SetComputeRootSignature(&worker.root);
|
||||
list.list.SetPipelineState(&worker.pso);
|
||||
list.list.SetComputeRootUnorderedAccessView(0, hits_gpu);
|
||||
list.list.ExecuteIndirect(&signature, 1, &scratch, 0, None, 0);
|
||||
}
|
||||
list.close();
|
||||
assert_eq!(host.log_messages(), None, "the debug layer found an error");
|
||||
host.execute(&[&list]);
|
||||
list.reset();
|
||||
// SAFETY: the list has run; a mapped buffer.
|
||||
let hits = unsafe { (hits_ptr as *const u32).read_volatile() };
|
||||
assert_eq!(hits, 1, "the budget starts empty at the next submission");
|
||||
}
|
||||
|
||||
/// Records the dispatch records `groups` (three words each, `step`
|
||||
/// bytes apart, the rest of each record ones) into a fresh mapped
|
||||
/// buffer of `warp`'s device.
|
||||
|
||||
Reference in new issue
Block a user