Skip to content

Card copies: pages with many changed runs go up as the page and a byte mask, one compute pass instead of a copy a run (VICTORY_MERGE_MASK, off) - #1295

Open
KainanP wants to merge 1 commit into
fxceless:mainfrom
KainanP:perf/merge-page-mask
Open

KainanP wants to merge 1 commit into
fxceless:mainfrom
KainanP:perf/merge-page-mask

Conversation

@KainanP

@KainanP KainanP commented Nov 26, 2026 •

Copy link
Copy Markdown

What it changes

Off by default. Without VICTORY_MERGE_MASK, the copies are recorded exactly as before.

Under flights' shader writes, Shadow::merge_under_writes puts the title's bytes into a copy in the card's memory run by run. In Host::card_before, each run became its own CopyBufferRegion. With VICTORY_CARD_REHASH_RUNS unset (the default), each run also got its own hash pass over its 64 KiB piece. A page that the title rewrites with a few bytes changed here and there has thousands of runs: up to about 16,000 in @MKmajster's profile in #1083. That page goes over the VICTORY_MERGE_RUNS budget (512 by default), and the flights are settled instead. #1083 measured 1.3–1.5 such settles a frame, 27–35 ms of the submitting thread on an RTX 3070. Raising the cap made that frame slower (199 ms against 162–173 ms): more copies, the same settles.

With VICTORY_MERGE_MASK=1 (or =<n> for another threshold, or alt to switch on and off every 20 s):

  • Budget. A 64 KiB page with more than 16 changed ranges costs the merge budget one run instead of one per range.
  • Upload. In card_before, the ranges of such a page become one row of a small compute pass (card_mask::CARD_MASK), with one thread per dword. Each thread reads the copy's CPU side, where the copies would have copied from, and writes only the bytes its mask names. It keeps the other bytes of the card side, which may be a flight's writes. A partly masked dword is read and written back on the card. The pass runs on the queue behind every flight and ahead of the submission's lists, as the copies do, so no other writer races it.
  • Hashing. The page's piece is hashed again once, not once per range.
  • What stays the same. The ranges are still noted one by one in dirty and sent, so the relay's merge skips exactly the same bytes as before. A page with 16 ranges or fewer is still copied range by range.
  • When it does not apply. It is not used with VICTORY_QUEUE2 uploads, whose bytes land packed. It is also not used for a copy whose start or size is not 4-aligned, or when the 1 MiB+ upload buffer for the masks cannot be made. In each of these cases the old path runs.
  • Merge side. The merge's card branch moves into Shadow::card_put_title. A masked page is copied into the CPU side whole, which is exact because outside its runs the bytes are already equal, and its runs are noted under one lock.
  • Log. A frame-log line gives the masked pages per frame.

Synthetic benchmark (card_mask::gpu::mask_bench, #[ignore])

Method.

  • Fixture. A real carded copy: a 1 MiB title buffer through shadow::register, with shader writes declared once so the card keeps its piece hashes, as for a copy under flights.
  • One round.
    1. Change 4 pages, each with N runs of 2 bytes spread evenly.
    2. Run card_put_title per page. This is the merge's part, minus reading title memory, which is the same for both paths.
    3. Run Host::card_before with no lists.
  • What is timed.
    • Submitting thread: steps 2 and 3, measured with Instant.
    • Card: two timestamps around the sync list. The queue waits on a fence until all lists are queued, so the stamps measure the card's work and not this thread's.
  • Statistics. Median of 15 rounds after 2 warm-up rounds (9 rounds on WARP), per page. "A copy a run" and "mask" are measured back to back in the same process.
  • Machine. i7-14650HX. Nine unrelated single-threaded CPU jobs were running on the machine (9 of 24 threads); nothing else used the GPUs.
  • Base. Re-measured on main e4245f3 (after the rebase); the earlier numbers were on ceb9e1f. Single cells moved by up to about 25 %; no conclusion changed.

Hardware. RTX 4060 Laptop (driver 32.0.16.1047), Intel UHD Graphics 8086:a78b (31.0.101.5445, selected with a local adapter hook that is not part of this PR), WARP (--features diagnostics, VICTORY_D3D12_WARP=1). Release build.

Default settings (VICTORY_CARD_REHASH_RUNS unset). The cells give the submitting thread / the card, in µs per page:

runs a page RTX 4060: copy a run RTX 4060: mask Intel UHD: copy a run Intel UHD: mask WARP: copy a run WARP: mask
16,384 7,581 / 15,065 403 / 52 16,312 / 364,590 523 / 241 99,735 / 178,096 562 / 188
4,096 1,829 / 3,760 94 / 52 4,241 / 91,194 87 / 241 20,235 / 47,711 120 / 192
1,024 477 / 979 43 / 52 951 / 22,855 37 / 241 2,496 / 12,105 47 / 167
256 132 / 286 22 / 48 334 / 5,796 40 / 270 608 / 3,106 27 / 142
64 46 / 93 19 / 48 139 / 1,606 50 / 395 184 / 977 21 / 142
16 21 / 48 21 / 48 80 / 1,177 48 / 606 50 / 310 24 / 148
8 22 / 46 19 / 47 68 / 666 42 / 606 36 / 219 19 / 150
1 15 / 46 14 / 46 57 / 607 50 / 598 16 / 118 16 / 125

With VICTORY_CARD_REHASH_RUNS=1 (hash passes joined), the copies' card time is much lower. The submitting thread still pays for each copy:

runs a page RTX 4060: copy a run RTX 4060: mask Intel UHD: copy a run Intel UHD: mask WARP: copy a run WARP: mask
16,384 5,921 / 274 552 / 60 10,709 / 39,138 540 / 255 7,836 / 10,089 480 / 93
4,096 1,409 / 106 102 / 59 3,770 / 10,014 128 / 356 1,948 / 2,629 67 / 112
1,024 393 / 67 43 / 58 1,030 / 2,726 57 / 340 534 / 697 31 / 117
256 103 / 62 20 / 60 277 / 1,181 42 / 468 117 / 201 16 / 89
64 34 / 60 16 / 60 93 / 819 50 / 366 34 / 76 13 / 65
16 19 / 58 17 / 59 58 / 330 42 / 295 15 / 47 13 / 87
8 18 / 59 17 / 58 40 / 310 34 / 298 13 / 42 11 / 70
1 14 / 58 14 / 58 40 / 351 58 / 347 9 / 38 9 / 38

With VICTORY_CARD_HASH_WIDE=1 (e4245f3's group-a-piece hash, which its alt measured at -10 ms of card a frame), VICTORY_CARD_REHASH_RUNS unset. Every hash pass is cheaper, so the copies' card time drops too. The submitting thread is unchanged:

runs a page RTX 4060: copy a run RTX 4060: mask Intel UHD: copy a run Intel UHD: mask WARP: copy a run WARP: mask
16,384 7,380 / 962 403 / 18 12,429 / 51,969 537 / 25 89,896 / 85,004 548 / 120
4,096 1,810 / 244 109 / 15 3,653 / 12,665 74 / 22 20,844 / 21,650 113 / 115
1,024 451 / 67 36 / 13 914 / 3,209 64 / 30 2,792 / 5,150 45 / 99
256 114 / 23 18 / 10 330 / 1,089 57 / 36 590 / 1,374 29 / 104
64 42 / 11 15 / 8 153 / 392 69 / 43 170 / 419 21 / 93
16 22 / 7 15 / 8 87 / 119 63 / 42 45 / 147 22 / 92
8 15 / 7 14 / 8 74 / 73 44 / 41 31 / 110 22 / 91
1 14 / 7 14 / 7 54 / 41 55 / 40 13 / 50 13 / 54

Crossover:

  • Submitting thread. The mask is no slower from 16 runs a page up on all three adapters, and clearly faster from 64 up: 19–49× on the RTX 4060 and the UHD at 4,096 runs or more, and up to about 180× on WARP. Below 16 runs, the two paths are within noise. The hash form does not change this.
  • Card. Cases where the mask is not better:
    • On WARP with joined rehash (VICTORY_CARD_REHASH_RUNS=1), the copies stay cheaper at 16 runs and below (47 vs 87 µs at 16; with the wide hash too, 35 vs 63 µs).
    • On the RTX 4060, the two are even below 64, with either hash form.
  • Threshold. 16, which on these numbers avoids the region where the mask loses on the submitting thread. VICTORY_MERGE_MASK=<n> changes it.

Full tables, including 2 and 4 runs, are in the test output: cargo test --release -p victory-gpu --lib card_mask::gpu::mask_bench -- --ignored --nocapture, with MASK_BENCH_RUNS, MASK_BENCH_PAGES and MASK_BENCH_ROUNDS to vary it.

Correctness

  • card_mask::gpu::masked_pages_leave_the_bytes_the_copies_would (#[ignore], GPU). Runs 48 random cases on one copy. Each case:

    1. Random bytes are written into the card side, standing in for flights' writes the CPU side does not know.
    2. 1 to 16 pages of the title get random run patterns, from 1 run a page to every other byte, with runs of 1–64 bytes.
    3. Merge, then card_before.
    4. The whole card side is read back and compared byte for byte with the copy-a-run reference: the title's bytes in the runs, the card's own bytes everywhere else.
    5. The touched pieces' hashes are checked against a CPU hash of the read-back bytes.

    Thresholds 1, 16, 300 and off alternate, so both paths run on the same copy.

  • Results. Passes on the RTX 4060, the Intel UHD and WARP, with VICTORY_CARD_SYNC_BATCH 0 and 1 (re-run on e4245f3: 48 cases, 193 masked pages each).

  • Mutation checks. A shader that writes whole dwords fails at case 0. Leaving the masked page out of the rehash also fails.

  • CPU tests. card_mask::tests compares the split and the mask apply with plain copies for 400 random cases, including title buffers that start off a block edge and short copies.

  • Existing test. host::tests::two_dirty_card_buffers_reach_readback_in_the_same_submission passes with the mask off and on, in both sync modes.

  • Suite.

    • cargo test -p victory-gpu (on e4245f3): 224 passed on RTX and on Intel.
    • WARP, --features diagnostics --test-threads=1, release: 240 passed; 3 profile::tests fail (deep_stacks_are_cut_not_lost, the_sampler_finds_threads_and_writes_its_files, time_goes_to_the_function_that_spent_it). The same three fail on unmodified main e4245f3 in the same build, and this PR does not touch profile.
    • In parallel on WARP, the suite ends in an access violation on unmodified main too (5 of 8 runs here), with the same tests in flight.
    • cargo clippy -p victory-gpu --all-targets [--all-features] -- -D warnings: clean.
    • card_mask.rs passes rustfmt --check, and the lines changed in host.rs and shadow.rs add no new rustfmt differences.

What is not measured

  • In-game. No in-game frame time: I have no game here. The 27–35 ms of settles in RTX 3070 8 GB + 16 GB RAM, Series S profile: what each switch and recent PR did on main (3973179 → ca8829f) #1083 should go away only if run-cap settles are the cause there. The masked page still pays for its ranges in dirty, and with the relay in sent. Neither the frame nor the relay thread's merge (card_merge_relay scans those lists) is measured.
  • Mixed loads. No measurement with other submissions' copies mixed in, or with VICTORY_QUEUE2 (excluded).
  • Laptop iGPU. On the UHD the card side lives in shared memory, so its card times are not representative of a discrete card.

Request for an A/B

@MKmajster, @CuteBooty: you have the scenes from #1083. Could you run your opening scene with VICTORY_MERGE_MASK=alt? It switches on and off every 20 s and logs each change. Please compare the frame log's "merge gave up … for more changed runs than VICTORY_MERGE_RUNS leaves" count, the settles, and the frame time between the on and off halves. The new line "card copies' pages that went up with a mask per frame" shows how many pages took the path. A row with VICTORY_MERGE_MASK=64 would also help place the threshold.

AI use: written with Claude Code (Claude Opus 5.5). It read the code paths, wrote the change and tests, and ran the benchmarks and test suites on the hardware above. No game data was used.

🤖 Generated with Claude Code

@HLVM04

HLVM04 commented Nov 26, 2026 •

Copy link
Copy Markdown

In-game numbers for VICTORY_MERGE_MASK=1. They come from separate runs, not an alt run. Setup: RTX 5070 Ti, Ryzen 7 9800X3D, Windows 11, Series S profile. The tree is fork main 0b0074e plus several open PRs and local changes, with this PR merged on top. Each figure is the in-world steady frame time after 135 s.

build frame (ms)
base (with our local MERGE_DEFER, which holds over-budget merges until the next submission) 44.6–46.0 (3 runs)
+ VICTORY_MERGE_MASK=1 60.3 / 61.2
MERGE_DEFER off, no mask 54.8
MERGE_DEFER off + VICTORY_MERGE_MASK=1 65.9 / 60.9

With the mask, preparing rises from about 34 to about 50 ms a frame. Earlier on this tree, the run cap was hit by about 60,000 runs of 3 bytes a frame. Deferring those merges removed the "changed under shader writes" settles here (101 → 91 ms median at the time), and raising VICTORY_MERGE_RUNS to 65536 was slower (119 ms).

Limits: our card is not the bottleneck (CPU-bound), there are only 1–3 runs per setting, and we didn't split the extra preparing time between the mask's recording and its per-page hash.

Edit (2026-11-26), correction on our base: the local MERGE_DEFER in the base row made draws read stale bytes (over-budget title changes under shader-written ranges landed a submission late; with ray tracing on, the main character's skin draw lost its geometry), so that row is not a correct baseline and the deferral shouldn't be copied. Our replacement merges every run at once and joins nearby runs (gap of 64 bytes); it is as fast on our stack (in-world ~45–46 ms) and correct with ray tracing on. Exact merging without the gap was ~300 ms a frame. The mask figures above were not re-measured against it.

@CuteBooty

Copy link
Copy Markdown

RTX 3070 8 GB: raising the run cap cut the settles by three quarters and did not clearly change the frame

Not a test of this PR: my tree does not have it. A data point on whether the run cap is where the time goes, next to HLVM04's numbers. Same exe, Series S, Ryzen 5 3600, town; the only change is my cap on merged runs raised to 4096:

default cap cap 4096
settles a frame caused by the run cap 1.84 0.46
time in those settles a frame 7.5 ms 3.7 ms
frame, 1,000-3,000 draws 113 ms 127 ms
frame, 7,000-9,000 draws 259 ms 251 ms
frame, over 11,000 draws 272 ms 256 ms

On this machine every settle "for title memory changed under shader writes" was a run-cap settle, so the cap is the right thing to count, but removing most of them moved the frame by less than run-to-run noise in either direction. Limits: one short run each (3 to 12 five-second samples per band), and the machine was not quiet during the second.

My rule stays: I would port this only if a longer run shows the run cap costing more than a few ms here.

…e mask, one compute pass instead of a copy a run (VICTORY_MERGE_MASK, off)

Under flights' shader writes the title's bytes go into a copy in the card's
memory run by run (Shadow::merge_under_writes), and each run was a
CopyBufferRegion of its own in the sync list plus a hash pass of its own. A page
with thousands of runs (up to about 16,000 in fork issue fxceless#1083) exceeds the
merge budget (VICTORY_MERGE_RUNS) and the flights are settled instead.

- VICTORY_MERGE_MASK=1 (threshold 16), =<n> (threshold n) or alt: a 64 KiB
  page with more changed ranges than the threshold costs the merge budget one
  run; in Host::card_before its ranges become one row of a compute pass
  (card_mask::CARD_MASK) that writes the masked bytes from the CPU side and
  keeps every other byte of the card side, and the page's piece is hashed once.
  Byte-exact: the card side ends as the copies leave it. Ranges are still noted
  one by one (the relay's merge skips them). Not with VICTORY_QUEUE2 uploads.
- The merge's card branch is Shadow::card_put_title; a masked page is copied
  into the CPU side whole and its runs noted under one lock.
- Frame log line with the masked pages per frame.
- Tests: CPU reference of split + mask apply over random patterns; GPU test
  (ignored) comparing the card side and its piece hashes against a copy a run
  for random patterns, both paths on one copy; mask_bench (ignored) timing the
  submitting thread and the card for 16,384 / 4,096 / 1,024 / 256 / 64 / 16 /
  8 / 4 / 2 / 1 runs a page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KainanP
KainanP force-pushed the perf/merge-page-mask branch from 55c9076 to d404dda Compare November 26, 2026 17:13
KainanP added a commit to KainanP/victory-fork that referenced this pull request Nov 26, 2026
…ORY_CARD_REHASH_RUNS=0 as before, =1 touching pieces joined)

With VICTORY_CARD_REHASH_RUNS unset, every range sent up to a copy in the
card's memory had a hash pass of its own, so a 64 KiB piece holding 16,384
changed runs (fork issue fxceless#1083) was hashed 16,384 times: 14-15 ms of an RTX
4060 Laptop's time, 362 ms of an Intel UHD's (synthetic, card alone).

- Unset (the new default): runs of pieces that share a piece are one pass
  (Rehash::Sharing), so each piece is hashed once: 0.43 / 36.5 ms for that page.
- =1 keeps the touching join (Rehash::Touching). It is not the default because
  CARD_COMPARE gives a pass's pieces one thread each in a single group: 16
  touching pieces with one range each took 179 us of the RTX's time as 16
  passes and 266 us as one (Intel 1.15 / 1.23 ms).
- =0 is the old pass a range; alt still alternates =1 and =0.
- joined_piece_runs takes `touching`; the frame-rate line names the way.

Tests: rehash_tests::joined_rehash_leaves_the_hashes_a_pass_a_range_does
(GPU, ignored) runs 56 random cases on three copies, one per way, and checks
the card bytes and every piece hash are identical across ways, equal to a CPU
model of CARD_COMPARE for touched pieces and unchanged for the others (RTX,
Intel UHD, WARP; also with VICTORY_CARD_HASH_WIDE=1). rehash_bench (ignored)
prints the table. Host::card_sync_timed and Shadow::card_put_runs are test
only (card_sync_timed is the same helper as in PR fxceless#1295).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KainanP

KainanP commented Nov 26, 2026

Copy link
Copy Markdown
Author

Rebased on main e4245f3 (now d404dda).

  • Conflict. host.rs only: e4245f3 added Host::make_card_compare_wide at the same place as this PR's Host::make_card_mask. Both functions are kept as they were; nothing else changed.

  • e4245f3's changes and the mask. The mask does not pick the hash form: the masked page's piece is rehashed with whatever compare the submission uses (compare_pso, including VICTORY_CARD_HASH_WIDE=alt). VICTORY_DIRTY_4K narrows only the CPU refresh of write-watched pages (copy_stale). The changed runs card_before sends up still come from the byte compare, so the runs per 64 KiB page, the page size and the threshold of 16 stay as they were.

  • Tests on e4245f3. The GPU test (48 random cases) passes on RTX 4060, Intel UHD and WARP, with VICTORY_CARD_SYNC_BATCH 0 and 1. cargo test -p victory-gpu: 224 passed on RTX and on Intel. On WARP (diagnostics, serial), 3 profile::tests fail, and the same 3 fail on unmodified e4245f3. clippy -D warnings is clean with and without --all-features.

  • mask_bench re-run (median of 15 rounds, 9 on WARP). The tables in the description are updated, and there is a new one with VICTORY_CARD_HASH_WIDE=1. At 16,384 runs a page, copy a run → mask, submitting thread / card, µs:

    • RTX: 7,581 / 15,065 → 403 / 52.
    • With the wide hash, RTX: 7,380 / 962 → 403 / 18.
    • With the wide hash, Intel: 12,429 / 51,969 → 537 / 25.

    The wide hash cuts the copies' card time, but each run still costs a copy on the submitting thread, so the gain on that thread is the same as before.

Note for whoever merges: #1298 adds the same test-only helper Host::card_sync_timed. Whichever of the two lands second should drop its copy.

AI use: Claude Code (Claude Opus 5.5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants