|
|
@@ -1527,11 +1527,17 @@ 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. One thread
|
|
|
+/// walked the piece before, 4,096 steps; the passes took 24.8 ms of a frame's
|
|
|
+/// 82 on the card (capture replay1, 405 of them), most of it waiting for
|
|
|
+/// those steps.
|
|
|
pub(crate) const CARD_COMPARE: &str = r#"
|
|
|
#define RS "RootConstants(num32BitConstants=6, b0), SRV(t0), UAV(u0), UAV(u1)"
|
|
|
cbuffer Span : register(b0) {
|
|
|
@@ -1547,22 +1553,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
|
|
|
@@ -1574,16 +1589,36 @@ 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 hashed again after ranges were sent up to a copy in
|
|
|
/// the card's memory are hashed in runs of pieces that touch, each piece
|
|
|
/// once (`VICTORY_CARD_REHASH_RUNS=1`; off by default), and not in a pass
|
|
|
/// per range sent up.
|
|
|
///
|
|
|
/// These passes are ours, they run on the card before every submission's
|
|
|
-/// lists, and no timing so far names them: each walks its 64 KiB pieces
|
|
|
-/// 16 bytes at a time, 4,096 steps one after the other, so a pass takes
|
|
|
-/// the time of one piece however few its pieces are. Run 102 sent up 514
|
|
|
-/// ranges a frame: 514 such passes. The same pieces are hashed either way;
|
|
|
+/// lists: a pass takes the time of one piece however few its pieces are.
|
|
|
+/// Run 102 sent up 514 ranges a frame: 514 such passes. The same pieces are hashed either way;
|
|
|
/// a piece hashed when it need not be, or not hashed when it should, costs
|
|
|
/// a fetch that finds nothing and loses no shader write.
|
|
|
fn rehash_runs() -> bool {
|
|
|
@@ -3263,8 +3298,12 @@ impl Host {
|
|
|
(|| -> windows::core::Result<()> {
|
|
|
allocator.Reset()?;
|
|
|
list.Reset(allocator, None)?;
|
|
|
+ // Every copy first, then one barrier call, then every
|
|
|
+ // pass: a wait between each copy and its pass had the
|
|
|
+ // card do them one after the other.
|
|
|
+ let mut sent = Vec::new();
|
|
|
for (shadow, ranges) in &dirty {
|
|
|
- let Some((staging, before, size)) = shadow.card_sides() else {
|
|
|
+ let Some((staging, before, _)) = shadow.card_sides() else {
|
|
|
continue;
|
|
|
};
|
|
|
for &(a, b) in ranges {
|
|
|
@@ -3272,7 +3311,22 @@ impl Host {
|
|
|
crate::shadow::CARD_STATS[0].fetch_add((b - a) as u64, Ordering::Relaxed);
|
|
|
}
|
|
|
crate::shadow::CARD_STATS[1].fetch_add(ranges.len() as u64, Ordering::Relaxed);
|
|
|
- let Some(before) = before else {
|
|
|
+ if before.is_some() && !ranges.is_empty() {
|
|
|
+ sent.push(transition(
|
|
|
+ &shadow.buffer,
|
|
|
+ D3D12_RESOURCE_STATE_COPY_DEST,
|
|
|
+ D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,
|
|
|
+ ));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if !sent.is_empty() {
|
|
|
+ list.ResourceBarrier(&sent);
|
|
|
+ }
|
|
|
+ list.SetComputeRootSignature(&work.root);
|
|
|
+ list.SetPipelineState(&work.pso);
|
|
|
+ list.SetComputeRootUnorderedAccessView(3, work.differs.0.GetGPUVirtualAddress());
|
|
|
+ for (shadow, ranges) in &dirty {
|
|
|
+ let Some((_, Some(before), size)) = shadow.card_sides() else {
|
|
|
continue;
|
|
|
};
|
|
|
// No twin (it cost the card the copy's size again,
|
|
|
@@ -3281,18 +3335,8 @@ impl Host {
|
|
|
// hashed again, all of them the first time; the
|
|
|
// compare after the lists hashes the declared
|
|
|
// pieces and flags those whose hash is another.
|
|
|
- if !ranges.is_empty() {
|
|
|
- list.ResourceBarrier(&[transition(
|
|
|
- &shadow.buffer,
|
|
|
- D3D12_RESOURCE_STATE_COPY_DEST,
|
|
|
- D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,
|
|
|
- )]);
|
|
|
- }
|
|
|
- list.SetComputeRootSignature(&work.root);
|
|
|
- list.SetPipelineState(&work.pso);
|
|
|
list.SetComputeRootShaderResourceView(1, shadow.buffer.GetGPUVirtualAddress());
|
|
|
list.SetComputeRootUnorderedAccessView(2, before.GetGPUVirtualAddress());
|
|
|
- list.SetComputeRootUnorderedAccessView(3, work.differs.0.GetGPUVirtualAddress());
|
|
|
let runs: Vec<crate::shadow::CardSpan> = if whole.iter().any(|s| Arc::ptr_eq(s, shadow)) {
|
|
|
vec![shadow.card_run(0, usize::MAX)]
|
|
|
} else {
|
|
|
@@ -3303,9 +3347,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);
|
|
|
}
|
|
|
}
|
|
|
list.Close()?;
|
|
|
@@ -3387,11 +3429,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);
|
|
|
}
|
|
|
list.Close()?;
|