|
|
@@ -58,31 +58,49 @@ RWByteAddressBuffer budget : register(u1);
|
|
|
static const uint BUDGET = 1u << 27;
|
|
|
// Groups of one dispatch; more run empty.
|
|
|
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.
|
|
|
+groupshared uint costs[64];
|
|
|
+groupshared uint fits[64];
|
|
|
+groupshared uint largest_asked;
|
|
|
+groupshared uint all_asked;
|
|
|
[RootSignature(RS)]
|
|
|
[numthreads(64, 1, 1)]
|
|
|
-void main(uint3 id : SV_DispatchThreadID) {
|
|
|
+void main(uint3 id : SV_DispatchThreadID, uint gi : SV_GroupIndex) {
|
|
|
uint eff_count = records;
|
|
|
bool ahead = (has_count & 2u) != 0;
|
|
|
if ((has_count & 1u) != 0) {
|
|
|
eff_count = min(eff_count, count_buf.Load(count_offset));
|
|
|
}
|
|
|
- if (id.x >= eff_count) return;
|
|
|
+ // Threads past the records take part in the group's charge (cost 0)
|
|
|
+ // and store nothing.
|
|
|
+ bool active = id.x < eff_count;
|
|
|
uint src_at = args_offset + id.x * stride;
|
|
|
uint dst_at = id.x * stride;
|
|
|
- uint cost;
|
|
|
+ uint cost = 0;
|
|
|
+ uint asked = 0;
|
|
|
uint2 draw_counts = uint2(0, 0);
|
|
|
uint3 groups = uint3(0, 0, 0);
|
|
|
- if (kind <= 1) {
|
|
|
+ if (gi == 0) {
|
|
|
+ largest_asked = 0;
|
|
|
+ all_asked = 0;
|
|
|
+ }
|
|
|
+ if (active && kind <= 1) {
|
|
|
// Draws: vertex or index count, then instances.
|
|
|
- uint2 asked = src_args.Load2(src_at);
|
|
|
- if (ahead && (asked.x > (1u << 24) || asked.y > (1u << 24))) {
|
|
|
- asked = uint2(0, 0);
|
|
|
+ uint2 want = src_args.Load2(src_at);
|
|
|
+ if (ahead && (want.x > (1u << 24) || want.y > (1u << 24))) {
|
|
|
+ want = uint2(0, 0);
|
|
|
}
|
|
|
- uint count = min(asked.x, 1u << 24);
|
|
|
- uint instances = min(asked.y, (1u << 26) / max(count, 1u));
|
|
|
+ uint count = min(want.x, 1u << 24);
|
|
|
+ uint instances = min(want.y, (1u << 26) / max(count, 1u));
|
|
|
cost = count * instances;
|
|
|
draw_counts = uint2(count, instances);
|
|
|
- } else if (kind == 2) {
|
|
|
+ } else if (active && kind == 2) {
|
|
|
uint3 g = min(src_args.Load3(src_at), uint3(65535u, 65535u, 65535u));
|
|
|
// x * y fits 32 bits; the whole count may not.
|
|
|
if (g.z != 0 && g.x * g.y > GROUPS / g.z) {
|
|
|
@@ -90,7 +108,7 @@ void main(uint3 id : SV_DispatchThreadID) {
|
|
|
}
|
|
|
cost = g.x * g.y * g.z * 64u;
|
|
|
groups = g;
|
|
|
- } else {
|
|
|
+ } else if (active) {
|
|
|
// Mesh dispatches: the console's are one-dimensional.
|
|
|
uint3 g = src_args.Load3(src_at);
|
|
|
// Ahead of its draw, a record over the most a dispatch runs is a
|
|
|
@@ -99,29 +117,62 @@ void main(uint3 id : SV_DispatchThreadID) {
|
|
|
if (ahead && g.x > 65535u) {
|
|
|
g = uint3(0, 0, 0);
|
|
|
}
|
|
|
- // Diagnostics, in the budget buffer's next words: the largest
|
|
|
- // group count a mesh record asked for, and the groups asked in all.
|
|
|
- uint was;
|
|
|
- budget.InterlockedMax(4, g.x, was);
|
|
|
- budget.InterlockedAdd(8, min(g.x, 1u << 24), was);
|
|
|
+ asked = g.x;
|
|
|
g = uint3(min(g.x, 65535u), min(g.y, 1u), min(g.z, 1u));
|
|
|
cost = g.x * g.y * g.z * 128u;
|
|
|
groups = g;
|
|
|
}
|
|
|
- bool over = false;
|
|
|
- if (cost > 0) {
|
|
|
+ costs[gi] = cost;
|
|
|
+ GroupMemoryBarrierWithGroupSync();
|
|
|
+ if (active && kind > 2) {
|
|
|
+ // Diagnostics, in the budget buffer's next words (below): the
|
|
|
+ // largest group count a mesh record asked for, and the groups
|
|
|
+ // asked in all.
|
|
|
+ uint was;
|
|
|
+ InterlockedMax(largest_asked, asked, was);
|
|
|
+ InterlockedAdd(all_asked, min(asked, 1u << 24), was);
|
|
|
+ }
|
|
|
+ if (gi == 0) {
|
|
|
+ // What the records charge, from `before` on, in lane order: a
|
|
|
+ // record that does not fit whole runs empty and takes the rest.
|
|
|
uint before;
|
|
|
budget.InterlockedOr(0, 0, before);
|
|
|
[allow_uav_condition]
|
|
|
while (before < BUDGET) {
|
|
|
- uint next = (BUDGET - before <= cost) ? BUDGET : (before + cost);
|
|
|
+ uint next = before;
|
|
|
+ for (uint j = 0; j < 64; j++) {
|
|
|
+ uint c = costs[j];
|
|
|
+ if (c > 0 && next < BUDGET) {
|
|
|
+ next = (BUDGET - next <= c) ? BUDGET : (next + c);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (next == before) break;
|
|
|
uint prev;
|
|
|
budget.InterlockedCompareExchange(0, before, next, prev);
|
|
|
if (prev == before) break;
|
|
|
before = prev;
|
|
|
}
|
|
|
- over = before >= BUDGET || BUDGET - before < cost;
|
|
|
+ uint at = before;
|
|
|
+ for (uint k = 0; k < 64; k++) {
|
|
|
+ uint c = costs[k];
|
|
|
+ uint fit = 1;
|
|
|
+ if (c > 0) {
|
|
|
+ fit = (at < BUDGET && BUDGET - at >= c) ? 1u : 0u;
|
|
|
+ at = (at >= BUDGET || BUDGET - at <= c) ? BUDGET : (at + c);
|
|
|
+ }
|
|
|
+ fits[k] = fit;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ GroupMemoryBarrierWithGroupSync();
|
|
|
+ if (gi == 0 && kind > 2) {
|
|
|
+ uint was;
|
|
|
+ budget.InterlockedMax(4, largest_asked, was);
|
|
|
+ budget.InterlockedAdd(8, all_asked, was);
|
|
|
+ }
|
|
|
+ if (!active) {
|
|
|
+ return;
|
|
|
}
|
|
|
+ bool over = fits[gi] == 0;
|
|
|
if (kind <= 1) {
|
|
|
if (over) {
|
|
|
draw_counts = uint2(0, 0);
|
|
|
@@ -1242,6 +1293,71 @@ void main() {
|
|
|
assert_eq!(host.log_messages(), None, "the debug layer found an error");
|
|
|
}
|
|
|
|
|
|
+ /// A group charges its records in lane order: with 300 left of the
|
|
|
+ /// budget, the first draw (100) fits, the second (250) does not and
|
|
|
+ /// takes the rest, and the ones after it run empty. (Racing for the
|
|
|
+ /// word record by record, which of them fitted was up to the GPU.)
|
|
|
+ #[test]
|
|
|
+ fn warp_a_group_charges_its_records_in_lane_order() {
|
|
|
+ let Some(mut warp) = WarpHarness::new() else {
|
|
|
+ return;
|
|
|
+ };
|
|
|
+ const KIND_DRAW: u32 = 0;
|
|
|
+ const STRIDE: u32 = 16;
|
|
|
+ const BUDGET_CAP: u32 = 1 << 27;
|
|
|
+ let counts = [100u32, 250, 50, 100];
|
|
|
+ let total = counts.len() * STRIDE as usize;
|
|
|
+ let (src_buf, src_ptr, _) = warp.shared_buffer(total as u64);
|
|
|
+ let (_budget_buf, budget_ptr, budget_gpu) = warp.shared_buffer(256);
|
|
|
+ let (readback_buf, readback_ptr, _) = warp.shared_buffer(total as u64);
|
|
|
+ let dst_buf = warp.default_uav(total as u64);
|
|
|
+ // SAFETY: mapped buffers of these sizes.
|
|
|
+ unsafe {
|
|
|
+ for (i, &count) in counts.iter().enumerate() {
|
|
|
+ let words = [count, 1, 7, 9];
|
|
|
+ std::ptr::copy_nonoverlapping(
|
|
|
+ words.as_ptr().cast::<u8>(),
|
|
|
+ src_ptr.add(i * STRIDE as usize),
|
|
|
+ 16,
|
|
|
+ );
|
|
|
+ }
|
|
|
+ std::ptr::write_bytes(budget_ptr, 0, 256);
|
|
|
+ (budget_ptr as *mut u32).write_volatile(BUDGET_CAP - 300);
|
|
|
+ }
|
|
|
+ assert!(record_clamp_as(
|
|
|
+ &warp.clamp,
|
|
|
+ &warp.list,
|
|
|
+ (&dst_buf, D3D12_RESOURCE_STATE_COMMON),
|
|
|
+ budget_gpu,
|
|
|
+ (&src_buf, 0),
|
|
|
+ None,
|
|
|
+ (KIND_DRAW, STRIDE, counts.len() as u32),
|
|
|
+ false,
|
|
|
+ ));
|
|
|
+ // SAFETY: an open list; the clamped records are copied for the check.
|
|
|
+ unsafe {
|
|
|
+ warp.list.ResourceBarrier(&[transition(
|
|
|
+ &dst_buf,
|
|
|
+ D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT,
|
|
|
+ D3D12_RESOURCE_STATE_COPY_SOURCE,
|
|
|
+ )]);
|
|
|
+ warp.list.CopyBufferRegion(&readback_buf, 0, &dst_buf, 0, total as u64);
|
|
|
+ }
|
|
|
+ warp.submit_and_wait();
|
|
|
+ // SAFETY: the work is done; mapped buffers.
|
|
|
+ let (spent, out) = unsafe {
|
|
|
+ (
|
|
|
+ (budget_ptr as *const u32).read_volatile(),
|
|
|
+ std::slice::from_raw_parts(readback_ptr.cast::<u32>().cast_const(), total / 4).to_vec(),
|
|
|
+ )
|
|
|
+ };
|
|
|
+ assert_eq!(out[0..4], [100, 1, 7, 9], "the first record fits");
|
|
|
+ for i in 1..counts.len() {
|
|
|
+ assert_eq!(out[i * 4..i * 4 + 4], [0, 0, 7, 9], "record {i} runs empty");
|
|
|
+ }
|
|
|
+ assert_eq!(spent, BUDGET_CAP, "the record that did not fit took the rest");
|
|
|
+ }
|
|
|
+
|
|
|
/// 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.
|