Frame time: write-watched device memory, a parallel card compare, a shader cache that survives rebuilds, in place safe with late wait #651

Closed
github-bot wants to merge 2 commits from gh-pr/651/head into gh-pr/651/base
Owner

@⁠mengy007 on GitHub · 2026-11-25 04:01 UTC · original
perf/frame-time → main · +492 −73 · 11 files · 2 commits · closed without merging

Requested 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

XMemAllocDefault blocks of 64 KiB and more, and the memory resource.rs gives 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 for XMemVirtualAlloc memory 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 tells XMemFreeDefault to VirtualFree the reservation. Alignments above 64 KiB reserve the extra room and move the user address up.
  • resource.rs: Memory keeps the reservation's base and releases it on drop.
  • VICTORY_XMEM_WATCH=0 and VICTORY_HEAP_WATCH=0 restore 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_COMPARE hashes 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 on rehash_runs records 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_groups gives the dispatch size at all three call sites (two in host.rs, the WARP test in indirect.rs). The WARP test warp_card_compare_stores_the_hash_it_made_when_told passes.

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:

  • a hash of the translator's sources, made by a new crates/victory-gpu/build.rs over victory-shader/src, victory-shader/Cargo.toml, shader_cache.rs and Cargo.lock;
  • every VICTORY_* switch the translator reads (build.rs finds them in the sources);
  • the host's wave width (victory_shader::emit::host_max_wave, now public) and the diagnostics feature;
  • everything the old key already added (loop limit, view heap limit, group sync, wave64 switches, clean-up passes).

VICTORY_SHADER_CACHE_KEY=exe restores the old key. Checked by running the bench, rebuilding after a change to victory-hle, and running again: no new cache entries.

4. In place is safe with late wait (second commit)

inplace.rs documented VICTORY_INPLACE=2 as unsafe with VICTORY_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_writes now tells the list the ranges shaders may write in place (HostList::defer_placed_write, a new Deferred::placed).
  • They travel with the flight (Sent::placed, Flight::placed) and join Line::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.
  • A separate Line::placed also holds up refreshes of host copies that read those bytes (refresh_blocked). A copy's own declared writes don't need this, because Shadow::update leaves them out of its refresh.

Test: the bench with each submission also copying (CopyBufferRegion) the output the submission before it wrote, with VICTORY_INPLACE=2 and default late wait.

  • Before: 1,983,520 of 1,987,200 copied dwords were stale.
  • After: every dword is right, at 4.8 ms a frame (17.7 ms with default settings, where these buffers are not placed).

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:

  • writes 8 to 16 MB of per-frame data from the CPU;
  • submits 24 or 96 command lists of one dispatch each, which read that data and a 64 to 256 MB static buffer and write an output the size of a 1080p image (1920x1080 dwords) in title memory;
  • presents a 1080p image and waits for the GPU.

Medians, i9-13900K + RTX 4090, GPU idle:

Shape and settings Before After
24 submissions, 64 MB static, 8 MB CPU-written, defaults 390 ms 9.8 ms
same, VICTORY_CARD_BUFFERS=0 9.0 ms
same, VICTORY_INPLACE=2 6.5 ms
same, VICTORY_INPLACE=2 VICTORY_CARD_BUFFERS=0 2.5 ms
same, synchronous (VICTORY_LATE_WAIT=0 VICTORY_RELAY=0) 10.0 ms
96 submissions, 256 MB static, 16 MB CPU-written, defaults 33.0 ms
same, VICTORY_INPLACE=2 VICTORY_CARD_BUFFERS=0 4.8 ms

With 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-hle on this branch (rebased onto main 310c6eb), 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 on main (draw.rs, resource.rs placed_filler_twins).
  • cargo xtask policy: ok.
  • Our probe title (boot, device, compute, draw, queries, storage) passes against the build with these changes.
  • Not tested with the game: its files were not on the test machine.

🤖 Generated with Claude Code

**@⁠mengy007** on GitHub · 2026-11-25 04:01 UTC · [original](https://github.com/fxceless/victory-fork/pull/651) `perf/frame-time` → `main` · +492 −73 · 11 files · 2 commits · closed without merging <!-- claude-projects-attribution: {"github_login":"mengy007"} --> _Requested by **Meng** · [project thread](https://claude.ai/code/project/chan_01NBNZihVpCwUWEaMqpzc2Bg?thread=cmsg_01NBNZihVpCwUWEaMqpzc2Bg9KzNVbvj3VvauzGiU7kEFT)_ 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 `XMemAllocDefault` blocks of 64 KiB and more, and the memory `resource.rs` gives 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 for `XMemVirtualAlloc` memory 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 tells `XMemFreeDefault` to `VirtualFree` the reservation. Alignments above 64 KiB reserve the extra room and move the user address up. - `resource.rs`: `Memory` keeps the reservation's base and releases it on drop. - `VICTORY_XMEM_WATCH=0` and `VICTORY_HEAP_WATCH=0` restore 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_COMPARE` hashes 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 on `rehash_runs` records 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_groups` gives the dispatch size at all three call sites (two in `host.rs`, the WARP test in `indirect.rs`). The WARP test `warp_card_compare_stores_the_hash_it_made_when_told` passes. 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: - a hash of the translator's sources, made by a new `crates/victory-gpu/build.rs` over `victory-shader/src`, `victory-shader/Cargo.toml`, `shader_cache.rs` and `Cargo.lock`; - every `VICTORY_*` switch the translator reads (build.rs finds them in the sources); - the host's wave width (`victory_shader::emit::host_max_wave`, now public) and the diagnostics feature; - everything the old key already added (loop limit, view heap limit, group sync, wave64 switches, clean-up passes). `VICTORY_SHADER_CACHE_KEY=exe` restores the old key. Checked by running the bench, rebuilding after a change to `victory-hle`, and running again: no new cache entries. ## 4. In place is safe with late wait (second commit) `inplace.rs` documented `VICTORY_INPLACE=2` as unsafe with `VICTORY_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_writes` now tells the list the ranges shaders may write in place (`HostList::defer_placed_write`, a new `Deferred::placed`). - They travel with the flight (`Sent::placed`, `Flight::placed`) and join `Line::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. - A separate `Line::placed` also holds up refreshes of host copies that read those bytes (`refresh_blocked`). A copy's own declared writes don't need this, because `Shadow::update` leaves them out of its refresh. Test: the bench with each submission also copying (`CopyBufferRegion`) the output the submission before it wrote, with `VICTORY_INPLACE=2` and default late wait. - Before: 1,983,520 of 1,987,200 copied dwords were stale. - After: every dword is right, at 4.8 ms a frame (17.7 ms with default settings, where these buffers are not placed). 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: - writes 8 to 16 MB of per-frame data from the CPU; - submits 24 or 96 command lists of one dispatch each, which read that data and a 64 to 256 MB static buffer and write an output the size of a 1080p image (1920x1080 dwords) in title memory; - presents a 1080p image and waits for the GPU. Medians, i9-13900K + RTX 4090, GPU idle: | Shape and settings | Before | After | |---|---|---| | 24 submissions, 64 MB static, 8 MB CPU-written, defaults | 390 ms | 9.8 ms | | same, `VICTORY_CARD_BUFFERS=0` | | 9.0 ms | | same, `VICTORY_INPLACE=2` | | 6.5 ms | | same, `VICTORY_INPLACE=2 VICTORY_CARD_BUFFERS=0` | | 2.5 ms | | same, synchronous (`VICTORY_LATE_WAIT=0 VICTORY_RELAY=0`) | | 10.0 ms | | 96 submissions, 256 MB static, 16 MB CPU-written, defaults | | 33.0 ms | | same, `VICTORY_INPLACE=2 VICTORY_CARD_BUFFERS=0` | | 4.8 ms | With 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-hle` on this branch (rebased onto `main` 310c6eb), 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 on `main` (`draw.rs`, `resource.rs` `placed_filler_twins`). - `cargo xtask policy`: ok. - Our probe title (boot, device, compute, draw, queries, storage) passes against the build with these changes. - Not tested with the game: its files were not on the test machine. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
- Device heaps, committed resources and XMemAllocDefault blocks of 64 KiB
  and more are write-watched reservations instead of host heap memory, so
  their host copies are refreshed by the pages the title wrote instead of
  copied whole at every submission (VICTORY_HEAP_WATCH=0 and
  VICTORY_XMEM_WATCH=0 restore the old behaviour).
- The card-side compare (CARD_COMPARE) hashes each 64 KiB piece with a
  group of 256 threads instead of one thread walking 4,096 dependent loads.
- The shader cache is keyed by a hash of the translator's sources and the
  switches it reads (crates/victory-gpu/build.rs), not by the whole
  executable (VICTORY_SHADER_CACHE_KEY=exe restores the old key).

Measured with a bench title of our own (docs/fork-performance.md): 390 ms
to 9.8 ms a frame with default settings on an idle RTX 4090.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shadow::gpu_writes tells the list the title memory its shaders may write
through a buffer placed in it (HostList::defer_placed_write). The ranges
travel with the flight and join the line's written ranges, so buffer
copies, fills, textures and memory waits that touch them wait for the
flights that write them; refresh_blocked also holds up refreshes of host
copies that read them. With VICTORY_INPLACE=2 and late wait, a buffer copy
of what the submission before wrote read stale bytes in 1,983,520 of
1,987,200 dwords before this, none after.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:48:14 +00:00
github-bot closed this pull request 2026-11-26 03:48:14 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.