Frame time: write-watched device memory, a parallel card compare, a shader cache that survives rebuilds, in place safe with late wait #651
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/fxceless--victory-fork!651
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/651/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@mengy007 on GitHub · 2026-11-25 04:01 UTC · original
perf/frame-time→main· +492 −73 · 11 files · 2 commits · closed without mergingRequested by Meng · project thread
Four changes to frame time: memory the device hands out is write-watched, the card-side compare runs in parallel, the shader cache survives rebuilds, and in-place buffers (
VICTORY_INPLACE=2) are safe with late wait. On a bench title of our own (described below) the frame went from 390 ms to 9.8 ms with default settings on an idle RTX 4090. With the GPU shared with another busy process it went from 1.3 to 31 s down to 29 to 93 ms.1. Device heaps and large title allocations are write-watched
XMemAllocDefaultblocks of 64 KiB and more, and the memoryresource.rsgives heaps the title makes through the device and committed resources (Memory::new), were host heap memory. That memory cannot be write-watched, so every submission copied every range of it that its lists read, in full. On the bench that was 72 refreshes and 1,627 MB a frame, 174 to 380 ms of copying.Both now come from reservations of their own made with
MEM_WRITE_WATCH, so the host copies are refreshed by the 64 KiB pages the title wrote, as forXMemVirtualAllocmemory already. The same frames now copy 9.9 MB in 1.4 ms.xmem.rs:alloc_watched; a flag bit in the block header's second word tellsXMemFreeDefaulttoVirtualFreethe reservation. Alignments above 64 KiB reserve the extra room and move the user address up.resource.rs:Memorykeeps the reservation's base and releases it on drop.VICTORY_XMEM_WATCH=0andVICTORY_HEAP_WATCH=0restore the old behaviour.Memory the title maps from physical pages (the game's big GPU heap) is guarded already and is not touched.
2. The card-side compare runs in parallel
For copies in the card's memory (
VICTORY_CARD_BUFFERS, on by default),CARD_COMPAREhashes 64 KiB pieces after ranges are sent up and compares them after the lists, to find shader writes. It ran one thread per piece, walking 4,096 dependent 16-byte loads, so every pass took the same long time however few pieces it had. The comment onrehash_runsrecords 514 such passes a frame in run 102. On the bench the card was busy with these passes for about 330 ms a frame.It is now one group of 256 threads per piece. Each thread hashes 16 coalesced steps, and the 256 hashes are folded in a fixed order in group-shared memory. The hash only has to tell changed bytes from unchanged ones, and the stored hashes live for one run, so changing the function is safe.
card_compare_groupsgives the dispatch size at all three call sites (two inhost.rs, the WARP test inindirect.rs). The WARP testwarp_card_compare_stores_the_hash_it_made_when_toldpasses.Bench with default settings and 24 submissions a frame: 324 ms → 32 ms (busy GPU), 9.8 ms (idle GPU).
3. The shader cache survives rebuilds
Translations were keyed by the BLAKE3 of the whole
victory.exe. Any rebuild, whatever it changed, made every cached translation stale, so the next run translated every shader again and had the driver compile every pipeline again during play.The key is now:
crates/victory-gpu/build.rsovervictory-shader/src,victory-shader/Cargo.toml,shader_cache.rsandCargo.lock;VICTORY_*switch the translator reads (build.rs finds them in the sources);victory_shader::emit::host_max_wave, now public) and the diagnostics feature;VICTORY_SHADER_CACHE_KEY=exerestores the old key. Checked by running the bench, rebuilding after a change tovictory-hle, and running again: no new cache entries.4. In place is safe with late wait (second commit)
inplace.rsdocumentedVICTORY_INPLACE=2as unsafe withVICTORY_LATE_WAIT, which is now on by default. A submission sent ahead writes title memory directly through a placed buffer, but the flights' rules only knew the ranges that copies declare. A later submission could read those bytes on the CPU before the GPU had written them, through a buffer copy, a fill, a texture or a refresh of another host copy.Shadow::gpu_writesnow tells the list the ranges shaders may write in place (HostList::defer_placed_write, a newDeferred::placed).Sent::placed,Flight::placed) and joinLine::written. Buffer copies, fills, textures and memory-value waits that touch them now wait for the flights that write them, as they already did for copies' declared writes.Line::placedalso holds up refreshes of host copies that read those bytes (refresh_blocked). A copy's own declared writes don't need this, becauseShadow::updateleaves them out of its refresh.Test: the bench with each submission also copying (
CopyBufferRegion) the output the submission before it wrote, withVICTORY_INPLACE=2and default late wait.In place is still off by default. The bench's shader does not read its inputs, so it does not yet show what reading big buffers over the bus costs.
The bench
No game files were on the test machine, so the workload is a bench title of our own (not in this PR). It is an executable started by the loader like a title. Each frame it:
Medians, i9-13900K + RTX 4090, GPU idle:
VICTORY_CARD_BUFFERS=0VICTORY_INPLACE=2VICTORY_INPLACE=2 VICTORY_CARD_BUFFERS=0VICTORY_LATE_WAIT=0 VICTORY_RELAY=0)VICTORY_INPLACE=2 VICTORY_CARD_BUFFERS=0With another process keeping the GPU about 94 % busy: 24 submissions went from 1,312 ms (mean 6,088) to 28 to 38 ms, and 96 submissions from 31,298 ms to 93 ms.
The in-place rows are not a recommendation yet: the bench's shader does not read its inputs, so it hides the cost of reading big buffers over the bus. The numbers are recorded in
docs/fork-performance.md.Checks
cargo test --release -p victory-gpu -p victory-hleon this branch (rebased ontomain310c6eb), after both commits: 118 + 96 + 1 passed.cargo build --release -p victory: no new warnings. Clippy on the three crates shows only the two warnings already onmain(draw.rs,resource.rsplaced_filler_twins).cargo xtask policy: ok.🤖 Generated with Claude Code
Pull request closed