GPU: account for delivered bytes in bulk copyback #1814

Open
aycow wants to merge 5 commits from upstream-pr/1816 into main
aycow commented 2026-11-24 22:58:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by aycow on 2026-11-24T22:58:50Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6593109721 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1816). Originally posted by [aycow](https://github.com/aycow) on 2026-11-24T22:58:50Z. --- ## 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.
catminou4 commented 2026-11-24 23:03:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T23:03:26Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6048801746 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1816#pullrequestreview-6048801746). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T23:03:26Z. **Review:** COMMENTED --- 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.
Melow-code commented 2026-11-24 23:07:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Melow-code on 2026-11-24T23:07:37Z.

Review: COMMENTED


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?

<!-- greyfinch-sync:Marvisak/VIctory:review:6048844256 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1816#pullrequestreview-6048844256). Originally posted by [Melow-code](https://github.com/Melow-code) on 2026-11-24T23:07:37Z. **Review:** COMMENTED --- 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?
DesertGamer commented 2026-11-25 00:06:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DesertGamer on 2026-11-24T23:07:06Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7147033630 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1816#issuecomment-7147033630). Originally posted by [DesertGamer](https://github.com/DesertGamer) on 2026-11-24T23:07:06Z. --- ### 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.*
aycow commented 2026-11-25 00:06:44 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by aycow on 2026-11-24T23:11:50Z.


@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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7147124941 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1816#issuecomment-7147124941). Originally posted by [aycow](https://github.com/aycow) on 2026-11-24T23:11:50Z. --- @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://github.com/Marvisak/VIctory/pull/1816#pullrequestreview-6048844256) AI-assisted using Codex.
aycow commented 2026-11-25 01:08:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by aycow on 2026-11-25T01:02:39Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149147590 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1816#issuecomment-7149147590). Originally posted by [aycow](https://github.com/aycow) on 2026-11-25T01:02:39Z. --- 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)
Melow-code commented 2026-11-25 02:05:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Melow-code on 2026-11-25T01:22:08Z.


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.)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149474394 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1816#issuecomment-7149474394). Originally posted by [Melow-code](https://github.com/Melow-code) on 2026-11-25T01:22:08Z. --- 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.)
Marvisak commented 2026-11-25 07:03:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T06:30:15Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7154857589 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1816#issuecomment-7154857589). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T06:30:15Z. --- <!-- 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)_
upstream-sync changed title from [PR #1816] GPU: account for delivered bytes in protected bulk copyback to [PR #1816] GPU: account for delivered bytes in bulk copyback 2026-11-25 17:02:56 +00:00
aycow commented 2026-11-25 17:05:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by aycow on 2026-11-25T16:57:54Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7168796742 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1816#issuecomment-7168796742). Originally posted by [aycow](https://github.com/aycow) on 2026-11-25T16:57:54Z. --- 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.
Melow-code commented 2026-11-25 23:55:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Melow-code on 2026-11-25T23:55:37Z.

Review: APPROVED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066664818 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1816#pullrequestreview-6066664818). Originally posted by [Melow-code](https://github.com/Melow-code) on 2026-11-25T23:55:37Z. **Review:** APPROVED --- 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.
fuzeofficlal commented 2026-11-26 16:40:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T16:40:03Z.

Review: APPROVED


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.)

<!-- greyfinch-sync:Marvisak/VIctory:review:6077691119 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1816#pullrequestreview-6077691119). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T16:40:03Z. **Review:** APPROVED --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1816:upstream-pr/1816'
git switch 'upstream-pr/1816'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1816'
git switch 'upstream-pr/1816'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1816'
git switch 'upstream-pr/1816'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1816'
git switch 'main'
git merge --squash 'upstream-pr/1816'
git switch 'main'
git merge --ff-only 'upstream-pr/1816'
git switch 'main'
git merge 'upstream-pr/1816'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!1814
No description provided.