Skip to content

Fix #721, #722: copy-back writes only changed bytes; mark pages current conditionally - #2009

Closed
tharun-37 wants to merge 2 commits into
Marvisak:mainfrom
tharun-37:fix/shadow-copyback
Closed

tharun-37 wants to merge 2 commits into
Marvisak:mainfrom
tharun-37:fix/shadow-copyback

Conversation

@tharun-37

Copy link
Copy Markdown

Split from #2000 per review feedback (gpu-only, Windows path). #721: copy_back now writes only changed bytes. #722: page marked current only if title still matches. Builds clean; shadow tests pass.

@jeremygold02

jeremygold02 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Triage verdict — Needs changes (57fdaea)

Evidence

Checks and limits

  • I did not reproduce that interleaving; the updated test assertions count bytes copied and do not schedule a write in this window.

Next step

  • I’d preserve those tests and add a controlled write between the compare and the clean-state transition. Also worth carrying forward its sparse-write cost check: this head makes one write_mapped call per changed run, so a passing dense or small test does not bound that cost. Prior sparse-write discussion

This verification was AI-assisted.

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown

Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at 57fdaea. victory-gpu: 141 passed, 2 failed, 2 ignored. This PR adds no test of its own for #721 / #722, so this only shows the existing tests still pass. The 2 are #1744's NVIDIA failures on main (dynamic_slots_are_held_until_released and lists_share_a_table_and_copies_keep_their_views, the heap-cap ones). Same as main on this hardware. Not run: a title.

AI-assisted (Claude Code).

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.

Copy-back writes only changed bytes + pages marked current (#721/#722) — part of a three-way split of #2000 (which fixed #721/#722/#726 in one PR): #2009 covers copy-back, #2010 covers #726's v_med3 NaN fix (shader, macOS-tested separately). Maintainer should compare #2000 vs #2009+#2010 and pick.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
… only changed bytes; mark pages current conditionally

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This PR no longer merges cleanly into main (conflicts in crates/victory-gpu/src/shadow.rs and crates/victory-gpu/src/watch.rs). It will be reviewed again once it is rebased.

From the earlier needs-work note, still open: regression tests in the PR for the #722 race and for #721, a bound or measurement for the per-run write_mapped cost, and the overlap with #1024.


Generated by Claude Code

Xeltax added a commit to Xeltax/VIcky that referenced this pull request Nov 25, 2026
…ck as one (opt-in); copy-back timings

Putting shader writes back into title memory wrote each run of the GPU's
bytes with its own NtWriteVirtualMemory. With the structure builder on, a
frame put back about 350 pieces holding 780,000-910,000 runs (about 2,400
per 64 KiB piece: the builder changes a few bytes here and there in each
node), 248 ms a frame on an RTX 4080.

VICTORY_BACK_JOIN=<n> writes runs no more than n bytes apart as one call,
gaps included (title bytes read just before). With n = 256 the same frames
take 1,300 calls and 1.8 ms of writing; copying back fell from 327-354 ms to
58-65 ms a frame and the frame from 849-916 ms to 502-583 ms (one run each).
Unset or 0: each run alone, as before (upstream Marvisak#2009). The shortcut: a
title write landing in a gap between the read and the write is lost; the
relay's merge already writes whole stretches the same way.

Also: a frame-rate line of the copy-back's pieces, runs, kernel writes and
the time reading and merging, opening guards and writing, and of the
relay's merges.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A4bpLmErkufJL3LGgfR561
@ajsem1337

Copy link
Copy Markdown

Deterministic reproduction of the guarded copy-back race

Following up on the race identified in review, a test simulates a GPU write by changing the host copy. During guarded snapshot copy-back, a test hook makes a second thread write to the tracked title page just before Tracked::taken.

Tested build Marker in title memory per seed Marker in host copy after two more submissions
main (940d32f) 32/32 0/32
main with #2009 (57fdaea) 32/32 0/32

Three seeds on main and two on the PR gave the same result. The other eight cases met their visibility deadlines on both builds.

On #2009 the write lands after the final comparison and before the page is re-protected. That write is marked as already copied. Marking the page current requires validating its bytes after re-protection and preserving later writes, including writes through aliases. Prior review · Related earlier report

Reproducer (test, test-only hook, and the one-line assertion change for this PR): https://gist.github.com/ajsem1337/551639fa596a63f63282956d42917b0c

Found with AI assistance (Codex wrote the original test, Claude Code ported it to main and ran it).

@ajsem1337

Copy link
Copy Markdown

Re-ran the reproducer from the gist on b9ecb1e: the BeforeTaken case now passes for all three seeds (3913, 77, 1234567). The write made just before Tracked::taken reaches the host copy one submission later (lost: 0, deferred: 32, was lost: 32 on main and on 57fdaea), and the whole test passes.

Limits: the gist's 0001 no longer applies to the changed taken, so I put the same BEFORE_TAKEN hook in by hand at the same place (top of taken, before guards()), plus 0002. The test covers that window only, not a write landing inside the guarded section, and it ran under Proton on an RX 7900 GRE, not on Windows.

@cloph-dsp

Copy link
Copy Markdown

Independent GPU validation for PR #2009 (Windows, Rust 1.100.0/MSVC, head b9ecb1e, no game data): cargo test --locked -p victory-gpu --lib shadow::tests with VICTORY_D3D12_WARP=0 on RTX 5060 Laptop (driver 621.24) -> 4 tests passed (title_writes_under_a_busy_range_are_copied, snapshots_cover_the_declared_ranges_and_writes_go_back_once, marked_pages_go_back_by_the_bytes_the_gpu_changed, mark_runs_are_reused_and_joined), 141 filtered, exit 0; with VICTORY_D3D12_WARP=1 --features diagnostics (WARP) -> same 4 tests passed, 144 filtered, exit 0. Explicit adapter selection used because the hybrid laptop may default to the 780M iGPU. No real title or game data used. AI-assisted (Hy4 flash). GPU/driver: RTX 5060 Laptop, driver 621.24, Vulkan 1.4.357 (WARP second data point).

Fayberr pushed a commit to Fayberr/Victory-United that referenced this pull request Nov 25, 2026
… (240,000 writes a frame under Wine)

Since only the GPU's bytes are written back (upstream Marvisak#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 Marvisak#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`.
@tharun-37

Copy link
Copy Markdown
Author

Closing to re-scope: taking on better-scoped issues instead.

@tharun-37 tharun-37 closed this Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants