GPU: runs of a copy-back that lie close together go back in one write (240,000 writes a frame under Wine) #857

Open
github-bot wants to merge 1 commit from github-bot/gh-857 into main AGit
Owner

@⁠UwUCammie on GitHub · 2026-11-25 10:49 UTC · original
UwUCammie:fix/copy-back-joined-runs → main · +70 −2 · 1 files · 1 commits · open

Problem

Since only the GPU's bytes are written back (upstream #2009, in 8dd30ab), Shadow::copy_back calls write_mapped once for every run of changed bytes in a piece. A shader that writes records leaves a lot of short runs, because every byte of a record that comes out the same as before ends one.

In the world frames of the opening scene I counted 240,000 to 280,000 runs a frame, in about 250 pieces. Each is one NtWriteVirtualMemory call. Under Wine a call measured about 4 µs, so "putting them back" in the frame account was 0.8 to 1.2 s of every frame.

Change

Runs with fewer than 64 bytes between them are written as one (join_runs). VICTORY_BACK_RUN_GAP sets the distance; 0 writes every run on its own, as before.

What it gives up

The bytes between two joined runs are written as they were read a moment earlier. A title write that lands on one of them between the read and the write is lost. Before #2009 that was true of the whole stretch from a piece's first changed byte to its last; with this it is true only of bytes less than 64 bytes away from one the shader wrote. The README's VICTORY_MERGE_RACE count (none in 138,000 write-backs of whole stretches) is for the other merges, not this one, so I can't put a number on it here.

Measurements

RX 7900 XT, RADV (Mesa 26.3.1), Wine 11.22, vkd3d-proton from GE-Proton11-14, diagnostics build, opening scene up to the first room, one run each of about ten minutes.

every run its own write joined under 64 bytes
writes a frame 240,000-280,000 about 4,000
"putting them back" 826-1,222 ms 13-22 ms (median 16)
world frame 1,260-1,790 ms 367-414 ms (median 383)

How the number of writes falls with the distance, counted in one run: under 8 bytes about 15,000 a frame, under 64 about 4,000, under 512 about 1,100.

The picture is the same in both runs (first room with its walls, the character, the "Move around" prompt).

Not measured: Windows. I'd expect the calls to be cheaper there but still a quarter of a million of them a frame.

The tree I ran is this main (9376fd6) merged into myokex/victory-amd, with two local loader switches and myokex/victory-amd#6 on top; main alone was not run. Another program used the card during the first minutes of the second run, before the world frames counted above.

  • #256 and #154 make each write cheaper under Wine. This makes them fewer; they go together.
  • #786 writes runs in three more merges, where the same joining would apply.

Tests

  • New: runs_closer_than_the_gap_are_joined.
  • cargo test -p victory-gpu --locked under Wine: 145 passed.
  • cargo clippy -p victory-gpu -- -D warnings reports two things that are on main already (placed_filler_twins unused without diagnostics, sort_unstable_by in draw.rs), nothing in shadow.rs.

No game files, captures or logs are included.

**@⁠UwUCammie** on GitHub · 2026-11-25 10:49 UTC · [original](https://github.com/fxceless/victory-fork/pull/857) `UwUCammie:fix/copy-back-joined-runs` → `main` · +70 −2 · 1 files · 1 commits · open ## Problem Since only the GPU's bytes are written back (upstream #2009, in `8dd30ab`), `Shadow::copy_back` calls `write_mapped` once for every run of changed bytes in a piece. A shader that writes records leaves a lot of short runs, because every byte of a record that comes out the same as before ends one. In the world frames of the opening scene I counted 240,000 to 280,000 runs a frame, in about 250 pieces. Each is one `NtWriteVirtualMemory` call. Under Wine a call measured about 4 µs, so "putting them back" in the frame account was 0.8 to 1.2 s of every frame. ## Change Runs with fewer than 64 bytes between them are written as one (`join_runs`). `VICTORY_BACK_RUN_GAP` sets the distance; `0` writes every run on its own, as before. ## What it gives up The bytes between two joined runs are written as they were read a moment earlier. A title write that lands on one of them between the read and the write is lost. Before #2009 that was true of the whole stretch from a piece's first changed byte to its last; with this it is true only of bytes less than 64 bytes away from one the shader wrote. The README's `VICTORY_MERGE_RACE` count (none in 138,000 write-backs of whole stretches) is for the other merges, not this one, so I can't put a number on it here. ## Measurements RX 7900 XT, RADV (Mesa 26.3.1), Wine 11.22, vkd3d-proton from GE-Proton11-14, diagnostics build, opening scene up to the first room, one run each of about ten minutes. | | every run its own write | joined under 64 bytes | |---|---|---| | writes a frame | 240,000-280,000 | about 4,000 | | "putting them back" | 826-1,222 ms | 13-22 ms (median 16) | | world frame | 1,260-1,790 ms | 367-414 ms (median 383) | How the number of writes falls with the distance, counted in one run: under 8 bytes about 15,000 a frame, under 64 about 4,000, under 512 about 1,100. The picture is the same in both runs (first room with its walls, the character, the "Move around" prompt). Not measured: Windows. I'd expect the calls to be cheaper there but still a quarter of a million of them a frame. The tree I ran is this `main` (`9376fd6`) merged into myokex/victory-amd, with two local loader switches and myokex/victory-amd#6 on top; `main` alone was not run. Another program used the card during the first minutes of the second run, before the world frames counted above. ## Related - #256 and #154 make each write cheaper under Wine. This makes them fewer; they go together. - #786 writes runs in three more merges, where the same joining would apply. ## Tests - New: `runs_closer_than_the_gap_are_joined`. - `cargo test -p victory-gpu --locked` under Wine: 145 passed. - `cargo clippy -p victory-gpu -- -D warnings` reports two things that are on `main` already (`placed_filler_twins` unused without `diagnostics`, `sort_unstable_by` in `draw.rs`), nothing in `shadow.rs`. No game files, captures or logs are included.
Since only the GPU's bytes are written back (upstream #2009), every run of
changed bytes in a piece is a write of its own (`write_mapped`, one
NtWriteVirtualMemory call each). A shader that writes records leaves many
short runs, because every byte of a record that comes out as it was ends
one: in the world frames of the opening scene about 250 pieces a frame held
240,000 to 280,000 runs. At about 4 microseconds a call under Wine that was
0.8 to 1.2 s of every frame ("putting them back" in the frame account).

Runs with fewer than 64 bytes between them are now written as one
(`VICTORY_BACK_RUN_GAP`; `0` writes every run on its own, as before). That
leaves about 4,000 writes a frame.

What it gives up: the bytes between two joined runs are written as they
were read a moment before, so a title write that lands on one of them
between the read and the write is lost. Before #2009 that held for the
whole stretch from a piece's first changed byte to its last; now it holds
for bytes less than 64 bytes from one the shader wrote.

Test: `runs_closer_than_the_gap_are_joined`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:52:39 +00:00
Author
Owner

@⁠alexodee on GitHub · 2026-11-25 11:09 UTC · original

Windows numbers for the idea, since the description says they are missing. I have not run this PR and have not tried a gap of 64. These come from an equivalent local join in back_piece (runs at most N bytes apart become one write, N = 0 is one write per run).

Setup: RTX 3070 Ti Laptop 8 GB (driver 596.36), i7-12700H, 15.7 GB RAM, Windows 11 build 26300, diagnostics build, Series X profile. VICTORY_RELAY=0 VICTORY_LATE_WAIT=3 VICTORY_WIDE_BACK=1 VICTORY_CARD_BUFFERS_MIN_MB=1 VICTORY_CARD_BUFFERS_MB=512, native NtWriteVirtualMemory. Medians over the 5 s report lines with at least 3,000 draws a frame and 400 differing pieces (no minimum of pieces for the card-buffers run below). Rows (a) start at the beginning of the game and include the opening cutscene, (b) start from a save near the first gameplay scene.

tree gap lines pieces "putting them back", median (p10-p90) frame
a26859f + PRs (b) 0, one write per run 76 688 632 ms (550-725) 1,109 ms
same, plus VICTORY_HEAP_SYNC=0 (b) 4096 71 685 37.3 ms (34.9-41.6) 462 ms
same, plus VICTORY_TEXTURE_BUDGET_MB=2800 (b) 4096 83 680 37.3 ms (35.1-42.2) 467 ms
same (a) 4096 209 633 32.4 ms (30.6-37.0) 442 ms
same, plus VICTORY_TEXTURE_BUDGET_MB=3200 (a) 4096 201 680 38.9 ms (37.0-41.6) 498 ms
9376fd6 + PRs (b) 4096 45 954 54.3 ms (52.5-58.2) 636 ms
9376fd6 + PRs, same executable and switches (b) 256 35 925 50.8 ms (48.7-55.2) 605 ms
  • So on Windows too, one write per run was most of the frame here (1,434 ms of kernel CPU a frame on all threads, against 442-493 ms in the joined runs on that tree). I did not count the writes.
  • The last two rows differ only by the gap: 256 was not slower than 4096 (one run each, so equal within noise). That speaks for a small gap.
  • Card buffers: with VICTORY_CARD_BUFFERS_MB=1024 and VICTORY_TEXTURE_BUDGET_MB=2200 (against 512 and 2600 in the last two rows, gap 4096), "putting them back" was 4.9 ms (4.8-5.6; 48 lines, 183 pieces) and the frame 469 ms. The log has 97 host copies in the card's memory (941 MB) instead of 46 (511 MB), so most of the buffers took the card's path, whose pieces this counter does not include (1,527 fetched a frame instead of 772). Both switches changed at once, so I cannot say how much of the frame time is the buffers and how much the smaller texture budget.
  • No crash, error line or device removal in these runs (7 to 25 minutes each). That says nothing about the race in the joined gaps.

The a26859f rows carry #276 #52 #51 #610, the 9376fd6 rows also #640 #823 #799 and VICTORY_HEAP_SYNC_TABLES=1. None of them changes shadow.rs. All carry a local switch in the crash handler.

Limits: one machine, one launch per row. Apart from the last two rows this is not a strict A/B (the gap 0 row was not repeated on 9376fd6). The relay was not run, so nothing here is about the default path. #786 would add the same per-run writes there.

AI-assisted (Claude Code). No game data.

**@⁠alexodee** on GitHub · 2026-11-25 11:09 UTC · [original](https://github.com/fxceless/victory-fork/pull/857#issuecomment-7160660574) Windows numbers for the idea, since the description says they are missing. I have not run this PR and have not tried a gap of 64. These come from an equivalent local join in `back_piece` (`runs` at most N bytes apart become one write, N = 0 is one write per run). Setup: RTX 3070 Ti Laptop 8 GB (driver 596.36), i7-12700H, 15.7 GB RAM, Windows 11 build 26300, diagnostics build, Series X profile. `VICTORY_RELAY=0 VICTORY_LATE_WAIT=3 VICTORY_WIDE_BACK=1 VICTORY_CARD_BUFFERS_MIN_MB=1 VICTORY_CARD_BUFFERS_MB=512`, native `NtWriteVirtualMemory`. Medians over the 5 s report lines with at least 3,000 draws a frame and 400 differing pieces (no minimum of pieces for the card-buffers run below). Rows (a) start at the beginning of the game and include the opening cutscene, (b) start from a save near the first gameplay scene. | tree | gap | lines | pieces | "putting them back", median (p10-p90) | frame | |---|---|---:|---:|---:|---:| | `a26859f` + PRs (b) | 0, one write per run | 76 | 688 | **632 ms** (550-725) | 1,109 ms | | same, plus `VICTORY_HEAP_SYNC=0` (b) | 4096 | 71 | 685 | **37.3 ms** (34.9-41.6) | 462 ms | | same, plus `VICTORY_TEXTURE_BUDGET_MB=2800` (b) | 4096 | 83 | 680 | **37.3 ms** (35.1-42.2) | 467 ms | | same (a) | 4096 | 209 | 633 | **32.4 ms** (30.6-37.0) | 442 ms | | same, plus `VICTORY_TEXTURE_BUDGET_MB=3200` (a) | 4096 | 201 | 680 | **38.9 ms** (37.0-41.6) | 498 ms | | `9376fd6` + PRs (b) | 4096 | 45 | 954 | **54.3 ms** (52.5-58.2) | 636 ms | | `9376fd6` + PRs, same executable and switches (b) | 256 | 35 | 925 | **50.8 ms** (48.7-55.2) | 605 ms | - So on Windows too, one write per run was most of the frame here (1,434 ms of kernel CPU a frame on all threads, against 442-493 ms in the joined runs on that tree). I did not count the writes. - The last two rows differ only by the gap: 256 was not slower than 4096 (one run each, so equal within noise). That speaks for a small gap. - Card buffers: with `VICTORY_CARD_BUFFERS_MB=1024` and `VICTORY_TEXTURE_BUDGET_MB=2200` (against 512 and 2600 in the last two rows, gap 4096), "putting them back" was **4.9 ms** (4.8-5.6; 48 lines, 183 pieces) and the frame 469 ms. The log has 97 host copies in the card's memory (941 MB) instead of 46 (511 MB), so most of the buffers took the card's path, whose pieces this counter does not include (1,527 fetched a frame instead of 772). Both switches changed at once, so I cannot say how much of the frame time is the buffers and how much the smaller texture budget. - No crash, error line or device removal in these runs (7 to 25 minutes each). That says nothing about the race in the joined gaps. The `a26859f` rows carry #276 #52 #51 #610, the `9376fd6` rows also #640 #823 #799 and `VICTORY_HEAP_SYNC_TABLES=1`. None of them changes `shadow.rs`. All carry a local switch in the crash handler. Limits: one machine, one launch per row. Apart from the last two rows this is not a strict A/B (the gap 0 row was not repeated on `9376fd6`). The relay was not run, so nothing here is about the default path. #786 would add the same per-run writes there. AI-assisted (Claude Code). No game data.
Author
Owner

@⁠UwUCammie on GitHub · 2026-11-25 11:59 UTC · original

Thanks, that's the half I couldn't measure. So it isn't a Wine thing: one write per run costs most of the frame on native Windows as well (632 ms against 37 ms here).

Your 256 against 4096 rows fit what I saw with the counts: past a few dozen bytes the number of writes hardly falls any more (about 4,000 at 64 and about 1,100 at 512 here), so a bigger gap only widens the window in which a title write can be lost. I'll leave the default at 64.

The PR still merges cleanly onto f85101b. That commit's VICTORY_MERGE_GAP is the other direction (title bytes going up to the card), it doesn't touch the copy-back.

**@⁠UwUCammie** on GitHub · 2026-11-25 11:59 UTC · [original](https://github.com/fxceless/victory-fork/pull/857#issuecomment-7161780823) Thanks, that's the half I couldn't measure. So it isn't a Wine thing: one write per run costs most of the frame on native Windows as well (632 ms against 37 ms here). Your 256 against 4096 rows fit what I saw with the counts: past a few dozen bytes the number of writes hardly falls any more (about 4,000 at 64 and about 1,100 at 512 here), so a bigger gap only widens the window in which a title write can be lost. I'll leave the default at 64. The PR still merges cleanly onto `f85101b`. That commit's `VICTORY_MERGE_GAP` is the other direction (title bytes going up to the card), it doesn't touch the copy-back.
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-25 12:23 UTC · original

Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present. I merged this PR's head 3a0b70b into main f85101b in a throwaway tree (no conflict). cargo clippy --workspace --all-targets --locked -- -D warnings fails with exactly the errors main itself has on this machine (placed_filler_twins, depth_written, a sort; PR #841 fixes them), also with --all-features; this PR adds none. cargo test --workspace --locked: run 1: stopped at indirect::tests::warp_budget_saturates_without_wraparound_and_preserves_draw_fields (the one that fails on main itself most runs here (PR #849 and #847 fix its assertion)) and the victory-gpu test binary died with an access violation (it does that on main about 2 runs in 100, issue #850); run 2: stopped at indirect::tests::warp_budget_saturates_without_wraparound_and_preserves_draw_fields (the one that fails on main itself most runs here (PR #849 and #847 fix its assertion)) and the victory-gpu test binary died with an access violation (it does that on main about 2 runs in 100, issue #850) (main alone: 555 passed, 0 failed, 11 ignored). The title was not run, so this says nothing about in-game effect. No game data.

AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-25 12:23 UTC · [original](https://github.com/fxceless/victory-fork/pull/857#issuecomment-7162308376) Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present. I merged this PR's head `3a0b70b` into `main` `f85101b` in a throwaway tree (no conflict). `cargo clippy --workspace --all-targets --locked -- -D warnings` fails with exactly the errors `main` itself has on this machine (`placed_filler_twins`, `depth_written`, a sort; PR #841 fixes them), also with `--all-features`; this PR adds none. `cargo test --workspace --locked`: run 1: stopped at `indirect::tests::warp_budget_saturates_without_wraparound_and_preserves_draw_fields` (the one that fails on `main` itself most runs here (PR #849 and #847 fix its assertion)) and the `victory-gpu` test binary died with an access violation (it does that on `main` about 2 runs in 100, issue #850); run 2: stopped at `indirect::tests::warp_budget_saturates_without_wraparound_and_preserves_draw_fields` (the one that fails on `main` itself most runs here (PR #849 and #847 fix its assertion)) and the `victory-gpu` test binary died with an access violation (it does that on `main` about 2 runs in 100, issue #850) (`main` alone: 555 passed, 0 failed, 11 ignored). The title was not run, so this says nothing about in-game effect. No game data. AI-assisted (Claude Code); no game data.
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/shadow.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/857/head:github-bot/gh-857'
git switch 'github-bot/gh-857'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-857'
git switch 'github-bot/gh-857'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-857'
git switch 'github-bot/gh-857'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-857'
git switch 'main'
git merge --squash 'github-bot/gh-857'
git switch 'main'
git merge --ff-only 'github-bot/gh-857'
git switch 'main'
git merge 'github-bot/gh-857'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.