GPU: account for delivered bytes in bulk copyback #1816

Open
github-bot wants to merge 5 commits from github-bot/gh-1816 into main AGit
Owner

@⁠aycow on GitHub · 2026-11-24 22:58 UTC · original
aycow:fix/bulk-copyback-protection-70564ae → main · +154 −37 · 2 files · 5 commits · open

What changed

Count bytes delivered during bulk GPU copyback. The normal case keeps upstream's single kernel copy with no upfront protection query. On partial delivery, retry only the undelivered tail in upstream's 64 KiB and 4 KiB pieces. Computed-address copyback, declared-write snapshots, and GPU-marked pages now acknowledge a full tracked span only after complete delivery.

This revision scopes the PR to delivery accounting and keeps the existing write-first protection policy. Guard-specific protection and exact changed-byte reconciliation stay separate.

Verification

The title-copy tests verify delivered bytes over committed blocks, an unmapped hole, a read-only destination, and an unaligned span. The shadow tests verify partial delivery and dirty tracking for computed, declared and GPU-marked writes. Two title-copy tests and all 11 shadow tests passed locally, with the shadow suite run on the default hardware adapter and WARP. Strict GPU diagnostics Clippy passed.

cargo test --locked -p victory-gpu --features diagnostics --lib title_copies -- --test-threads=1
cargo test --locked -p victory-gpu --features diagnostics --lib shadow::tests:: -- --test-threads=1

The fixture checks correctness. Title frame time remains unmeasured. AI-assisted with OpenAI Codex.

**@⁠aycow** on GitHub · 2026-11-24 22:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/1816) `aycow:fix/bulk-copyback-protection-70564ae` → `main` · +154 −37 · 2 files · 5 commits · open ## What changed Count bytes delivered during bulk GPU copyback. The normal case keeps upstream's single kernel copy with no upfront protection query. On partial delivery, retry only the undelivered tail in upstream's 64 KiB and 4 KiB pieces. Computed-address copyback, declared-write snapshots, and GPU-marked pages now acknowledge a full tracked span only after complete delivery. This revision scopes the PR to delivery accounting and keeps the existing write-first protection policy. Guard-specific protection and exact changed-byte reconciliation stay separate. ## Verification The title-copy tests verify delivered bytes over committed blocks, an unmapped hole, a read-only destination, and an unaligned span. The shadow tests verify partial delivery and dirty tracking for computed, declared and GPU-marked writes. Two title-copy tests and all 11 shadow tests passed locally, with the shadow suite run on the default hardware adapter and WARP. Strict GPU diagnostics Clippy passed. ```powershell cargo test --locked -p victory-gpu --features diagnostics --lib title_copies -- --test-threads=1 cargo test --locked -p victory-gpu --features diagnostics --lib shadow::tests:: -- --test-threads=1 ``` The fixture checks correctness. Title frame time remains unmeasured. AI-assisted with OpenAI Codex.
Port the checked region writer from e4e015f to current main. Keep the write-watch lease during copyback and acknowledge its page only after full delivery, for declared writes and computed-address writes. The region walker in #943 motivated the conservative preflight.
# Conflicts:
#	crates/victory-gpu/src/shadow.rs
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:45:33 +00:00
Author
Owner

@⁠DesertGamer on GitHub · 2026-11-24 23:07 UTC · original

VIctory V2: community PRs, merged and tested

Open PRs sitting in the queue? V2 pulls them into one build, screens each one (binaries, build scripts, hooks, new deps) and runs tests after every batch.

Where it stands today: launches, runs the menu at 30 fps, plays the intro video, reaches the start screen and loads the world (~50 s). Entering the world is the current blocker: rendering fails with a GPU "device removed" error (tracked in issue #1).

⚠️ Entering the world on a real GPU can hang it and make Windows reset the driver. Try it at your own risk.

Not playable yet, but every batch of merged fixes moves it closer. If you have a PR that's stuck, or a GPU to test on, I'd love your help:
https://DesertGamer/VIctory-V2

Unofficial community fork of Marvisak/VIctory, GPL-3.0.

**@⁠DesertGamer** on GitHub · 2026-11-24 23:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/1816#issuecomment-7147033630) ### VIctory V2: community PRs, merged and tested Open PRs sitting in the queue? V2 pulls them into one build, screens each one (binaries, build scripts, hooks, new deps) and runs tests after every batch. **Where it stands today:** launches, runs the menu at 30 fps, plays the intro video, reaches the start screen and loads the world (~50 s). Entering the world is the current blocker: rendering fails with a GPU "device removed" error (tracked in issue #1). :warning: Entering the world on a real GPU can hang it and make Windows reset the driver. Try it at your own risk. Not playable yet, but every batch of merged fixes moves it closer. If you have a PR that's stuck, or a GPU to test on, I'd love your help: https://DesertGamer/VIctory-V2 *Unofficial community fork of Marvisak/VIctory, GPL-3.0.*
Author
Owner

@⁠aycow on GitHub · 2026-11-24 23:11 UTC · original

@⁠ruipedro-pinheiro The skip in your review is intentional. A failed kernel write can consume the one-shot PAGE_GUARD bit. A second write could then succeed against the same bytes and bypass the protection that blocked the first attempt. The writer makes one attempt for that span, excludes the failed span from its confirmed byte count, and leaves the watched page dirty. That dirty mark lets the next refresh see current title memory. It does not queue an old GPU write for retry after protection or mapping changes.

I reran the four Windows protection tests at 4398fcb. All passed, including a_guard_added_after_query_is_never_retried, which installs the guard after the region query and verifies the byte is preserved. Review

AI-assisted using Codex.

**@⁠aycow** on GitHub · 2026-11-24 23:11 UTC · [original](https://github.com/Marvisak/VIctory/pull/1816#issuecomment-7147124941) @⁠ruipedro-pinheiro The skip in your review is intentional. A failed kernel write can consume the one-shot `PAGE_GUARD` bit. A second write could then succeed against the same bytes and bypass the protection that blocked the first attempt. The writer makes one attempt for that span, excludes the failed span from its confirmed byte count, and leaves the watched page dirty. That dirty mark lets the next refresh see current title memory. It does not queue an old GPU write for retry after protection or mapping changes. I reran the four Windows protection tests at `4398fcb`. All passed, including `a_guard_added_after_query_is_never_retried`, which installs the guard after the region query and verifies the byte is preserved. [Review](https://git.gtahub.st/github/Marvisak--VIctory/pulls/1816) AI-assisted using Codex.
Author
Owner

@⁠aycow on GitHub · 2026-11-25 01:02 UTC · original · edited 2026-11-25 01:10 UTC

Merged upstream 42363ab into the PR at a688849. The copyback path retains the protected-region walk and confirmed-byte accounting and uses main’s ReadProcessMemory kernel copy from #322. Strict diagnostics Clippy, all four active protection tests (including the protection-change case), and all four shadow tests on the default hardware adapter and WARP passed. GitHub reports a clean merge.

The GitHub workspace run hit the same gather and wave-32 permute test failures as upstream at 42363ab. Upstream CI

**@⁠aycow** on GitHub · 2026-11-25 01:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/1816#issuecomment-7149147590) · edited 2026-11-25 01:10 UTC Merged upstream `42363ab` into the PR at `a688849`. The copyback path retains the protected-region walk and confirmed-byte accounting and uses main’s `ReadProcessMemory` kernel copy from #322. Strict diagnostics Clippy, all four active protection tests (including the protection-change case), and all four shadow tests on the default hardware adapter and WARP passed. GitHub reports a clean merge. The GitHub workspace run hit the same gather and wave-32 permute test failures as upstream at `42363ab`. [Upstream CI](https://github.com/Marvisak/VIctory/actions/runs/45520251168/job/138889994349)
Author
Owner

@⁠Melow-code on GitHub · 2026-11-25 01:22 UTC · original

Reviewed the PR's own commit (4398fcb, above the merge of main).

The design is right: count what WriteProcessMemory actually delivered, walk stable protection spans once instead of the fixed 64K/4K split, and acknowledge a watched page only when the delivery is complete, so a partial copy keeps the page dirty and the next copyback retries it. The no-retry rule for failed spans is the correct one — a failed write can already clear PAGE_GUARD, so re-querying after a failure would trust a stale region.

Two non-blocking points:

  1. for_mapped_write_regions breaks silently when VirtualQuery fails; the uncounted tail keeps the pages dirty, which is safe, but a debug log line would make a kernel failure visible instead of looking like a quiet short copyback.
  2. The done == n check counts a region only when the whole region landed. A region that delivered half its bytes is not retried (per the no-retry rule) and not counted, so the page stays dirty and the next copyback walks it again — correct, just worth a comment for the next reader, since the old code would have retried it.

The two fixtures cover both watched and unwatched shadows with a READONLY page in the middle; the protection is applied after the write-watch lease is dropped, which is the right ordering.

Nothing blocking. (Review written with DeepSeek V4 Pro, OpenCode.)

**@⁠Melow-code** on GitHub · 2026-11-25 01:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/1816#issuecomment-7149474394) Reviewed the PR's own commit (4398fcb, above the merge of main). The design is right: count what WriteProcessMemory actually delivered, walk stable protection spans once instead of the fixed 64K/4K split, and acknowledge a watched page only when the delivery is complete, so a partial copy keeps the page dirty and the next copyback retries it. The no-retry rule for failed spans is the correct one — a failed write can already clear PAGE_GUARD, so re-querying after a failure would trust a stale region. Two non-blocking points: 1. `for_mapped_write_regions` breaks silently when VirtualQuery fails; the uncounted tail keeps the pages dirty, which is safe, but a debug log line would make a kernel failure visible instead of looking like a quiet short copyback. 2. The `done == n` check counts a region only when the whole region landed. A region that delivered half its bytes is not retried (per the no-retry rule) and not counted, so the page stays dirty and the next copyback walks it again — correct, just worth a comment for the next reader, since the old code would have retried it. The two fixtures cover both watched and unwatched shadows with a READONLY page in the middle; the protection is applied after the write-watch lease is dropped, which is the right ordering. Nothing blocking. (Review written with DeepSeek V4 Pro, OpenCode.)
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 06:30 UTC · original · edited 2026-11-26 01:19 UTC

Automated triage for the maintainers.

Merge candidate (re-checked on 7f65c84). The new head only merges current main into 0e56f5f, and the PR's own diff is unchanged. write_mapped in crates/victory-gpu/src/host.rs still tries the whole span with one ReadProcessMemory and no VirtualQuery. It returns the bytes delivered, and on a partial copy it retries only the undelivered tail in 64 KiB, then 4 KiB pieces.

In shadow.rs, copy_back_page, copy_back's declared-write loop and copy_back_marked mark a page as taken or synced only when it was fully delivered. The remaining caller in list.rs ignores the count, as before. The tests cover an unmapped hole, a read-only block, an unaligned span, and partial delivery for watched, unwatched and GPU-marked copies.

CI: passing (check).


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 06:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/1816#issuecomment-7154857589) · edited 2026-11-26 01:19 UTC <!-- victory-triage v1 head=7f65c841e2b60f6a3f8d4f599ac600d30bbce1ef verdict=merge-candidate at=2026-11-26T01:19:42Z --> Automated triage for the maintainers. **Merge candidate (re-checked on `7f65c84`).** The new head only merges current `main` into `0e56f5f`, and the PR's own diff is unchanged. `write_mapped` in `crates/victory-gpu/src/host.rs` still tries the whole span with one `ReadProcessMemory` and no `VirtualQuery`. It returns the bytes delivered, and on a partial copy it retries only the undelivered tail in 64 KiB, then 4 KiB pieces. In `shadow.rs`, `copy_back_page`, `copy_back`'s declared-write loop and `copy_back_marked` mark a page as taken or synced only when it was fully delivered. The remaining caller in `list.rs` ignores the count, as before. The tests cover an unmapped hole, a read-only block, an unaligned span, and partial delivery for watched, unwatched and GPU-marked copies. CI: passing (`check`). --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠aycow on GitHub · 2026-11-25 16:57 UTC · original

Addressed the write-first feedback at 0e56f5f. A complete bulk write takes one kernel call with no destination preflight. The returned byte count lets fallback resume at the first undelivered byte. On current main, GPU-marked copyback now also keeps a partially delivered page dirty. The title-copy tests and 11 shadow tests passed, including a partially writable marked page. I narrowed the PR description to delivery accounting and separated the guard-specific work.

**@⁠aycow** on GitHub · 2026-11-25 16:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/1816#issuecomment-7168796742) Addressed the write-first feedback at `0e56f5f`. A complete bulk write takes one kernel call with no destination preflight. The returned byte count lets fallback resume at the first undelivered byte. On current main, GPU-marked copyback now also keeps a partially delivered page dirty. The title-copy tests and 11 shadow tests passed, including a partially writable marked page. I narrowed the PR description to delivery accounting and separated the guard-specific work.
Author
Owner

@⁠catminou4 on GitHub · 2026-11-24 23:03 UTC · original · reviewed

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

Delivered bytes accounted in protected bulk copyback: 20K gpu diff on copyback accounting. Windows-only.

**@⁠catminou4** on GitHub · 2026-11-24 23:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/1816#pullrequestreview-6048801746) · **reviewed** Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff. Delivered bytes accounted in protected bulk copyback: 20K gpu diff on copyback accounting. Windows-only.
Author
Owner

@⁠Melow-code on GitHub · 2026-11-24 23:07 UTC · original · reviewed

The VirtualQuery walk is the right approach for protected pages. Concern: a guard page set between VirtualQuery and the write makes that span fail permanently — is retry on guard faults planned, or is the skip intentional?

**@⁠Melow-code** on GitHub · 2026-11-24 23:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/1816#pullrequestreview-6048844256) · **reviewed** The VirtualQuery walk is the right approach for protected pages. Concern: a guard page set between VirtualQuery and the write makes that span fail permanently — is retry on guard faults planned, or is the skip intentional?
Author
Owner

@⁠Melow-code on GitHub · 2026-11-25 23:55 UTC · original · approved these changes

Re-checked at 7f65c84. The write-first path is back, so my earlier VirtualQuery concern no longer applies. Delivery accounting is correct: write_mapped resumes at the first undelivered byte, and the three copy-back sites keep a page dirty until the whole span lands. LGTM — nit only: the write_mapped doc sentence ("as a protection query for each copy cost 39 ms") needs a reword.

**@⁠Melow-code** on GitHub · 2026-11-25 23:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/1816#pullrequestreview-6066664818) · **approved these changes** Re-checked at 7f65c84. The write-first path is back, so my earlier VirtualQuery concern no longer applies. Delivery accounting is correct: `write_mapped` resumes at the first undelivered byte, and the three copy-back sites keep a page dirty until the whole span lands. LGTM — nit only: the `write_mapped` doc sentence ("as a protection query for each copy cost 39 ms") needs a reword.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 16:40 UTC · original · approved these changes

Verified 7f65c841e2b60f6a3f8d4f599ac600d30bbce1ef merged cleanly onto 63bcca43 on Windows 11 with both an RTX 5090 (VICTORY_D3D12_DEBUG=1) and WARP (--features diagnostics, VICTORY_D3D12_DEBUG=1).

Code review

  • copy_in_kernel (crates/victory-gpu/src/host.rs:2529-2539) and write_mapped (host.rs:2544-2562) preserve the single-call write-first fast path without upfront VirtualQuery, guard len == 0 || base.checked_add(len).is_none(), cap kernel-reported progress with done.min(len), and fall back in 64 KiB then 4 KiB pieces only over the undelivered tail [base + first, base + len).
  • All three shadow copy-back sites — copy_back_page (crates/victory-gpu/src/shadow.rs:203-238), copy_back_or_not (shadow.rs:447-468), and copy_back_marked (shadow.rs:530-540) — now return actual delivered byte counts and call t.taken(...) only when the full span landed (or when merge_writeback found no title-side change needed and reconciled host in-place), keeping partially delivered pages dirty so the next refresh sees current title memory.

Verification

  • cargo xtask policy: 191 files ok.
  • cargo clippy --locked -p victory-gpu --all-targets -- -D warnings (default and --all-features): clean.
  • RTX 5090 + VICTORY_D3D12_DEBUG=1: shadow:: host::title_copies:: 15 passed, 0 failed.
  • WARP + --features diagnostics + VICTORY_D3D12_DEBUG=1: shadow:: host::title_copies:: 15 passed, 0 failed.
  • Additional probe on both RTX 5090 and WARP verifying len == 0 and usize::MAX wraparound guards, an unaligned [2048..10240) write crossing [RW | RO | RW] 4 KiB OS pages (delivering exactly 4,096 bytes and leaving the RO page intact), and a watched declared copy_back partial delivery (60 KiB -> stays dirty) followed by restoring PAGE_READWRITE, refreshing, and full 64 KiB delivery clearing dirty: passed on both adapters.

(Minor non-blocking pre-existing note: for_pieces at host.rs:2570 computes (at / step + 1) * step, which could overflow if a synthetic pointer lay in the top 64 KiB of usize::MAX, though unreachable for user-mode Windows addresses.)

**@⁠fuzeofficlal** on GitHub · 2026-11-26 16:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/1816#pullrequestreview-6077691119) · **approved these changes** Verified `7f65c841e2b60f6a3f8d4f599ac600d30bbce1ef` merged cleanly onto `63bcca43` on Windows 11 with both an RTX 5090 (`VICTORY_D3D12_DEBUG=1`) and WARP (`--features diagnostics`, `VICTORY_D3D12_DEBUG=1`). ### Code review - `copy_in_kernel` (`crates/victory-gpu/src/host.rs:2529-2539`) and `write_mapped` (`host.rs:2544-2562`) preserve the single-call write-first fast path without upfront `VirtualQuery`, guard `len == 0 || base.checked_add(len).is_none()`, cap kernel-reported progress with `done.min(len)`, and fall back in 64 KiB then 4 KiB pieces only over the undelivered tail `[base + first, base + len)`. - All three shadow copy-back sites — `copy_back_page` (`crates/victory-gpu/src/shadow.rs:203-238`), `copy_back_or_not` (`shadow.rs:447-468`), and `copy_back_marked` (`shadow.rs:530-540`) — now return actual delivered byte counts and call `t.taken(...)` only when the full span landed (or when `merge_writeback` found no title-side change needed and reconciled `host` in-place), keeping partially delivered pages dirty so the next refresh sees current title memory. ### Verification - `cargo xtask policy`: `191 files ok`. - `cargo clippy --locked -p victory-gpu --all-targets -- -D warnings` (default and `--all-features`): clean. - RTX 5090 + `VICTORY_D3D12_DEBUG=1`: `shadow:: host::title_copies::` **15 passed, 0 failed**. - WARP + `--features diagnostics` + `VICTORY_D3D12_DEBUG=1`: `shadow:: host::title_copies::` **15 passed, 0 failed**. - Additional probe on both RTX 5090 and WARP verifying `len == 0` and `usize::MAX` wraparound guards, an unaligned `[2048..10240)` write crossing `[RW | RO | RW]` 4 KiB OS pages (delivering exactly 4,096 bytes and leaving the RO page intact), and a watched declared `copy_back` partial delivery (60 KiB -> stays dirty) followed by restoring `PAGE_READWRITE`, refreshing, and full 64 KiB delivery clearing dirty: passed on both adapters. *(Minor non-blocking pre-existing note: `for_pieces` at `host.rs:2570` computes `(at / step + 1) * step`, which could overflow if a synthetic pointer lay in the top 64 KiB of `usize::MAX`, though unreachable for user-mode Windows addresses.)*
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1816'
git switch 'github-bot/gh-1816'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1816'
git switch 'github-bot/gh-1816'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1816'
git switch 'main'
git merge --squash 'github-bot/gh-1816'
git switch 'main'
git merge --ff-only 'github-bot/gh-1816'
git switch 'main'
git merge 'github-bot/gh-1816'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1816
No description provided.