Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions crates/leonida-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1639,11 +1639,16 @@ unsafe impl Send for CardWork {}
static CARD_WORK: Mutex<Option<CardWork>> = Mutex::new(None);

/// The compare of a declared range of a copy in the card's memory with
/// its bytes from before, one thread per 64 KiB piece (cut at multiples of
/// its bytes from before, one group per 64 KiB piece (cut at multiples of
/// 64 KiB of title memory, as `Shadow::card_piece` cuts them): `differs`
/// gets a dword per piece, 1 where a byte differs. The offsets are widened
/// to 16 bytes; buffers are multiples of 64 KiB, and a piece flagged for a
/// byte just outside the range costs a fetch that finds nothing.
///
/// A piece's 64 threads walk it side by side, each every 64th 16 bytes, and
/// one of them hashes their 64 hashes: 64 steps after one another, where one
/// thread walking the piece took 4,096 (after avatarmanz/VIctory-7900XTX
/// f669746: 24.8 of 82 ms of card time a frame went to these passes there).
pub(crate) const CARD_COMPARE: &str = r#"
#define RS "RootConstants(num32BitConstants=6, b0), SRV(t0), UAV(u0), UAV(u1)"
cbuffer Span : register(b0) {
Expand All @@ -1659,22 +1664,31 @@ ByteAddressBuffer now : register(t0);
// last sent up (or hashed whole, the first time).
RWByteAddressBuffer hashes : register(u0);
RWByteAddressBuffer differs : register(u1);
groupshared uint4 parts[64];
[RootSignature(RS)]
[numthreads(64, 1, 1)]
void main(uint3 id : SV_DispatchThreadID) {
if (id.x >= pieces) return;
void main(uint3 id : SV_GroupID, uint thread : SV_GroupIndex) {
uint piece = first + id.x;
uint lo = piece * 65536u;
lo = lo > skew ? lo - skew : 0u;
uint hi = min((piece + 1u) * 65536u - skew, size);
lo = lo & ~15u;
hi = (hi + 15u) & ~15u;
uint4 h = uint4(0x9E3779B9u, 0x85EBCA6Bu, 0xC2B2AE35u, 0x27D4EB2Fu);
uint4 h = uint4(0x9E3779B9u, 0x85EBCA6Bu, 0xC2B2AE35u, 0x27D4EB2Fu) + thread;
[loop]
for (uint at = lo; at < hi; at += 16u) {
for (uint at = lo + thread * 16u; at < hi; at += 1024u) {
h = (h ^ now.Load4(at)) * uint4(0x01000193u, 0x9E3779B1u, 0x85EBCA77u, 0xC2B2AE3Du);
h = h.yzwx ^ (h >> 15);
}
parts[thread] = h;
GroupMemoryBarrierWithGroupSync();
if (thread != 0u) return;
h = parts[0];
[loop]
for (uint i = 1u; i < 64u; ++i) {
h = (h ^ parts[i]) * uint4(0x01000193u, 0x9E3779B1u, 0x85EBCA77u, 0xC2B2AE3Du);
h = h.yzwx ^ (h >> 15);
}
if (compare != 0u) {
differs.Store(out_at + id.x * 4u, any(h != hashes.Load4(piece * 16u)) ? 1u : 0u);
// With the copy-back on its own thread: the next compare of the
Expand All @@ -1686,6 +1700,28 @@ void main(uint3 id : SV_DispatchThreadID) {
}
"#;

/// One pass of [`CARD_COMPARE`] over a span's pieces, into `differs` from
/// dword `at` on (`mode` as the shader's `compare`): a group per piece, in
/// parts a dispatch can hold.
///
/// # Safety
/// The list is open with the compare's root signature, pipeline and buffers
/// set, and the pieces lie in the buffer.
unsafe fn card_pass(list: &ID3D12GraphicsCommandList, span: &crate::shadow::CardSpan, size: u32, at: u32, mode: u32) {
const MOST: u32 = D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION;
let mut done = 0;
while done < span.pieces {
let n = (span.pieces - done).min(MOST);
let constants = [span.first + done, span.skew, size, n, (at + done) * 4, mode];
// SAFETY: the caller's.
unsafe {
list.SetComputeRoot32BitConstants(0, 6, constants.as_ptr().cast(), 0);
list.Dispatch(n, 1, 1);
}
done += n;
}
}

/// Whether the pieces a flight's compare flags come down to the PC inside
/// the flight itself (`LEONIDA_CARD_FETCH_INFLIGHT=1`; off by default, and
/// only with [`relay`]).
Expand Down Expand Up @@ -4289,9 +4325,7 @@ impl Host {
CARD_PASSES[0].fetch_add(runs.len() as u64, Ordering::Relaxed);
CARD_PASSES[1].fetch_add(runs.iter().map(|r| u64::from(r.pieces)).sum::<u64>(), Ordering::Relaxed);
for run in runs {
let constants = [run.first, run.skew, size as u32, run.pieces, 0, 0];
list.SetComputeRoot32BitConstants(0, 6, constants.as_ptr().cast(), 0);
list.Dispatch(run.pieces.div_ceil(64), 1, 1);
card_pass(list, &run, size as u32, 0, 0);
}
}
crate::cardmeter::end(list, timed);
Expand Down Expand Up @@ -4382,11 +4416,9 @@ impl Host {
let Some((_, Some(before), size)) = r.shadow.card_sides() else {
continue;
};
let constants = [r.span.first, r.span.skew, size as u32, r.span.pieces, r.at * 4, if flown { 2 } else { 1 }];
list.SetComputeRoot32BitConstants(0, 6, constants.as_ptr().cast(), 0);
list.SetComputeRootShaderResourceView(1, r.shadow.buffer.GetGPUVirtualAddress());
list.SetComputeRootUnorderedAccessView(2, before.GetGPUVirtualAddress());
list.Dispatch(r.span.pieces.div_ceil(64), 1, 1);
card_pass(list, &r.span, size as u32, r.at, if flown { 2 } else { 1 });
CARD_PASSES[2].fetch_add(1, Ordering::Relaxed);
}
// The fetch inside the flight (`inflight_fetch`): behind
Expand Down
4 changes: 2 additions & 2 deletions crates/leonida-gpu/src/indirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ void main() {
warp.list.SetComputeRootShaderResourceView(1, now_gpu);
warp.list.SetComputeRootUnorderedAccessView(2, hashes_gpu);
warp.list.SetComputeRootUnorderedAccessView(3, differs_gpu);
warp.list.Dispatch(PIECES.div_ceil(64), 1, 1);
warp.list.Dispatch(PIECES, 1, 1);
}
warp.submit_and_wait();
// SAFETY: as above.
Expand Down Expand Up @@ -1261,7 +1261,7 @@ void main() {
warp.list.SetComputeRootShaderResourceView(1, now_gpu);
warp.list.SetComputeRootUnorderedAccessView(2, hashes_gpu);
warp.list.SetComputeRootUnorderedAccessView(3, differs_gpu);
warp.list.Dispatch(1, 1, 1);
warp.list.Dispatch(pieces, 1, 1);
if mode == 2 {
warp.list.ResourceBarrier(&[uav_barrier()]);
warp.list.SetComputeRootSignature(&fetch.root);
Expand Down
14 changes: 14 additions & 0 deletions docs/fork-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
The detailed log of what this fork measured and changed, moved here from the README.
The status summary is in [README.md](../README.md).

## Newest (2026-11-26)

**The passes over copies in the card's memory hash a piece with 64
threads.** The compare after a submission and the rehash after ranges go
up walked each 64 KiB piece with one thread, 4,096 steps one after the
other. A group of 64 threads now walks it side by side, and one of them
hashes their 64 results (after avatarmanz/VIctory-7900XTX f669746). RTX
3080 12 GB (driver 621.24), Ryzen 7 7800X3D, first rooms, with
`LEONIDA_CARD_WINDOWS_MB=3072`, `LEONIDA_WINDOW_UPLOAD=1` and
`LEONIDA_TEXTURE_ROOM_PERCENT=70` in both runs: our send-and-hash and
compare lists 21 -> 3.6 ms of card time a frame, the usual frame 149-158
-> 129-135 ms. Not behind a switch: the hashes are made and compared by
the same shader, so only their values change, not which pieces differ.

## Newest (2026-11-25, later the same day)

**Update, 2026-11-25 afternoon: what a frame is made of, measured, and two
Expand Down
Loading