GPU: coalesce VirtualProtect runs in watch.rs, use NtReadVirtualMemory in host::read, and reuse Deferred capacities #1057

Open
UwUCammie wants to merge 7 commits from upstream-pr/1059 into main
UwUCammie commented 2026-11-25 17:50:59 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by UwUCammie on 2026-11-25T17:50:59Z.


Three small CPU-side overhead fixes that cut syscall and allocation churn per frame:

  1. Coalesce contiguous VirtualProtect runs (watch.rs): open_here and the alias-guarding pass both walk sorted block lists and called VirtualProtect once per 64 KiB block. Now they group contiguous runs into one VirtualProtect call and fall back per-block if a run fails.
  2. NtReadVirtualMemory in host::read (host.rs): when VICTORY_DIRECT_WRITES is on (default), host::read now uses NtReadVirtualMemory instead of ReadProcessMemory (matching write_mapped's NtWriteVirtualMemory path to avoid the extra VirtualQuery overhead on large pools).
  3. Reuse Deferred capacities on HostList::reset (host.rs): clear the existing Vec/HashSet buffers in Deferred instead of replacing the struct with fresh empty collections on every list reset.

Tested on Linux + Wine 11.22 + RX 7900 XT (RADV, VICTORY_RELAY=2, 480 s run into the cell scene):

  • copies dropped from ~46–50 ms to ~42.5–44.7 ms/frame, 0 GPUVM faults, 0 refused waits, picture unchanged
  • All 11 watch::tests pass under Wine.
<!-- greyfinch-sync:fxceless/victory-fork:issue:6610399695 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1059). Originally posted by [UwUCammie](https://github.com/UwUCammie) on 2026-11-25T17:50:59Z. --- Three small CPU-side overhead fixes that cut syscall and allocation churn per frame: 1. **Coalesce contiguous `VirtualProtect` runs (`watch.rs`)**: `open_here` and the alias-guarding pass both walk sorted block lists and called `VirtualProtect` once per 64 KiB block. Now they group contiguous runs into one `VirtualProtect` call and fall back per-block if a run fails. 2. **`NtReadVirtualMemory` in `host::read` (`host.rs`)**: when `VICTORY_DIRECT_WRITES` is on (default), `host::read` now uses `NtReadVirtualMemory` instead of `ReadProcessMemory` (matching `write_mapped`'s `NtWriteVirtualMemory` path to avoid the extra `VirtualQuery` overhead on large pools). 3. **Reuse `Deferred` capacities on `HostList::reset` (`host.rs`)**: clear the existing `Vec`/`HashSet` buffers in `Deferred` instead of replacing the struct with fresh empty collections on every list reset. Tested on Linux + Wine 11.22 + RX 7900 XT (RADV, `VICTORY_RELAY=2`, 480 s run into the cell scene): - `copies` dropped from ~46–50 ms to ~42.5–44.7 ms/frame, 0 GPUVM faults, 0 refused waits, picture unchanged - All 11 `watch::tests` pass under Wine.
Proudnoob4 commented 2026-11-25 19:09:45 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Proudnoob4 on 2026-11-25T18:28:34Z.


Tested on Windows 10 (i5-13600K + RTX 4080 16 GB, Series X) as part of a 7-PR stack (#1014 + #1015 + #1029 + #1043 + #1044 + #1058 + #1059) on Xeltax/VIcky 3fbc9cb8 (live BVH RT, 420s vs 300s 3fbc9cb8 baseline):

  • World FPS (>1000 draws): median 3.3 -> 5.05 fps (mean 2.46 -> 4.12, max 5.7, last10 = 4.8–5.7)
  • framelog usual frame / submit: 278 -> 176–180 ms (shortest 155 ms), submit user time 187.5 -> 97.2–114.2 ms
  • Stability: 0 GPUVM/device-removal issues across 420s live RT, exit 0, and watch::tests passed locally on Windows 10 (where coalesced VirtualProtect runs across placeholder/view boundaries hit the fallback path safely).

Note this run measured #1059 bundled with the 6 other PRs rather than standalone.

AI-assisted benchmark and analysis (Gemini 4 Hydrogen).

<!-- greyfinch-sync:fxceless/victory-fork:comment:7170958314 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1059#issuecomment-7170958314). Originally posted by [Proudnoob4](https://github.com/Proudnoob4) on 2026-11-25T18:28:34Z. --- Tested on Windows 10 (i5-13600K + RTX 4080 16 GB, Series X) as part of a 7-PR stack (#1014 + #1015 + #1029 + #1043 + #1044 + #1058 + #1059) on `Xeltax/VIcky` `3fbc9cb8` (live BVH RT, 420s vs 300s `3fbc9cb8` baseline): - **World FPS (>1000 draws):** median `3.3 -> 5.05 fps` (mean `2.46 -> 4.12`, max `5.7`, `last10 = 4.8–5.7`) - **`framelog` usual frame / submit:** `278 -> 176–180 ms` (`shortest 155 ms`), submit user time `187.5 -> 97.2–114.2 ms` - **Stability:** 0 GPUVM/device-removal issues across 420s live RT, exit 0, and `watch::tests` passed locally on Windows 10 (where coalesced `VirtualProtect` runs across placeholder/view boundaries hit the fallback path safely). Note this run measured #1059 bundled with the 6 other PRs rather than standalone. *AI-assisted benchmark and analysis (Gemini 4 Hydrogen).*
Ym1r1710 commented 2026-11-25 19:09:46 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Ym1r1710 on 2026-11-25T18:40:34Z.


Tried this together with #1068 on Windows, RTX 5080 + 7800X3D, tree is ca8829f + #1043 + #868 (SKIP_WC_BACK=1), RELAY=2. Two runs to world+800: 100.7 and 96.6 ms median against 102.8 ms before on the same base. Small, and close to my run-to-run noise, but both runs were on the right side. Picture at world+250/+400 unchanged, no device loss.

One note if anyone else has #812 (view sets) in their tree: the open_here hunk conflicts, and the alias-guarding run uses view_known()/viewed which aren't there anymore. I marked the run's blocks unclean/clean the same way the single-block paths do and it built and ran fine.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7171238185 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1059#issuecomment-7171238185). Originally posted by [Ym1r1710](https://github.com/Ym1r1710) on 2026-11-25T18:40:34Z. --- Tried this together with #1068 on Windows, RTX 5080 + 7800X3D, tree is ca8829f + #1043 + #868 (SKIP_WC_BACK=1), RELAY=2. Two runs to world+800: 100.7 and 96.6 ms median against 102.8 ms before on the same base. Small, and close to my run-to-run noise, but both runs were on the right side. Picture at world+250/+400 unchanged, no device loss. One note if anyone else has #812 (view sets) in their tree: the open_here hunk conflicts, and the alias-guarding run uses view_known()/viewed which aren't there anymore. I marked the run's blocks unclean/clean the same way the single-block paths do and it built and ran fine.
rafaconvaitis commented 2026-11-25 20:09:24 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by rafaconvaitis on 2026-11-25T19:11:23Z.


Windows/RTX 4070 in-game data point for the Flamingo downstream that includes this change. This is the Flamingo main commit 63d976b, with the #1059-derived changes in 4825fda, and the same local input-only hook in both runs. Hardware: RTX 4070 (616.64), Ryzen 9 5900X, 32 GB, SATA HDD. Series S, D3D12, Relay 1, async submit off and plain copies. The build resumed the locally created prison checkpoint with a warm pipeline library and accepted camera and movement input.

A fixed view toward the window for 120 seconds produced 915 measured presents: 7.84 effective FPS, P50/P95 123/169.3 ms. Median host metrics were 67.0 ms preparation, 38.1 ms copy-back for 38.4 MB, 33.3 ms inspecting 13,612 buffer slots, and 4,012 draws/frame. Raw descriptor synchronization was 19.7 ms median over 110 reports.

For context, an earlier 7ca2674 run in the same room, camera fixed and using only the off half of its own-view test, ran at 7.74 FPS, P50/P95 127/169.8 ms, 70.9 ms preparation and 45.1 ms copy-back for 41.35 MB (283 presents). The warm state, draw count (4,213/frame in the earlier sample), and exact view differ, and the newer Flamingo build includes other changes. This is an independent in-game compatibility/performance measurement on NVIDIA, not an isolated throughput comparison of this PR.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7171941270 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1059#issuecomment-7171941270). Originally posted by [rafaconvaitis](https://github.com/rafaconvaitis) on 2026-11-25T19:11:23Z. --- Windows/RTX 4070 in-game data point for the Flamingo downstream that includes this change. This is the Flamingo main commit `63d976b`, with the #1059-derived changes in `4825fda`, and the same local input-only hook in both runs. Hardware: RTX 4070 (616.64), Ryzen 9 5900X, 32 GB, SATA HDD. Series S, D3D12, Relay 1, async submit off and plain copies. The build resumed the locally created prison checkpoint with a warm pipeline library and accepted camera and movement input. A fixed view toward the window for 120 seconds produced 915 measured presents: **7.84 effective FPS**, P50/P95 **123/169.3 ms**. Median host metrics were **67.0 ms preparation, 38.1 ms copy-back for 38.4 MB, 33.3 ms inspecting 13,612 buffer slots**, and 4,012 draws/frame. Raw descriptor synchronization was **19.7 ms** median over 110 reports. For context, an earlier `7ca2674` run in the same room, camera fixed and using only the off half of its own-view test, ran at 7.74 FPS, P50/P95 127/169.8 ms, 70.9 ms preparation and 45.1 ms copy-back for 41.35 MB (283 presents). The warm state, draw count (4,213/frame in the earlier sample), and exact view differ, and the newer Flamingo build includes other changes. This is an independent in-game compatibility/performance measurement on NVIDIA, not an isolated throughput comparison of this PR.
DoubleD17 commented 2026-11-25 21:05:45 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by DoubleD17 on 2026-11-25T20:13:37Z.

Review: COMMENTED


Independent synthetic API check for the read path in 62de6c06b4da48f5ecc2b712653d40a44bf0a884 on Windows x64 build 26300 (AMD Ryzen 7 5800X). I used two adjacent 4 KiB pages in the current process, with a known byte pattern in the first and PAGE_NOACCESS on the second. I modeled the PR's success predicate and 64 KiB/4 KiB fallback in a standalone fixture. Read path at this SHA

Both APIs read all 4,096 bytes from the readable page. The 8 KiB read crossing the boundary returned 0x8000000D (STATUS_PARTIAL_COPY) and zero bytes with NtReadVirtualMemory, and Win32 error 299 and zero bytes with ReadProcessMemory. Splitting the failed read and zero-filling unreadable pieces reproduced the first page byte for byte and zeroed all 4,096 bytes of the second page with both APIs.

This exercises the native API and fallback assumptions on one Windows build. The fixture ran independently of the PR binary and did not measure performance or cover protection batching or deferred resets.

AI-assisted.

<!-- greyfinch-sync:fxceless/victory-fork:review:6064299197 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1059#pullrequestreview-6064299197). Originally posted by [DoubleD17](https://github.com/DoubleD17) on 2026-11-25T20:13:37Z. **Review:** COMMENTED --- Independent synthetic API check for the read path in `62de6c06b4da48f5ecc2b712653d40a44bf0a884` on Windows x64 build 26300 (AMD Ryzen 7 5800X). I used two adjacent 4 KiB pages in the current process, with a known byte pattern in the first and `PAGE_NOACCESS` on the second. I modeled the PR's success predicate and 64 KiB/4 KiB fallback in a standalone fixture. [Read path at this SHA](https://github.com/fxceless/victory-fork/blob/62de6c06b4da48f5ecc2b712653d40a44bf0a884/crates/victory-gpu/src/host.rs#L7360-L7406) Both APIs read all 4,096 bytes from the readable page. The 8 KiB read crossing the boundary returned `0x8000000D` (`STATUS_PARTIAL_COPY`) and zero bytes with `NtReadVirtualMemory`, and Win32 error 299 and zero bytes with `ReadProcessMemory`. Splitting the failed read and zero-filling unreadable pieces reproduced the first page byte for byte and zeroed all 4,096 bytes of the second page with both APIs. This exercises the native API and fallback assumptions on one Windows build. The fixture ran independently of the PR binary and did not measure performance or cover protection batching or deferred resets. AI-assisted.
Ym1r1710 commented 2026-11-25 22:07:23 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Ym1r1710 on 2026-11-25T21:31:02Z.


One thing to watch now that #999 (VICTORY_TEXTURE_BACK) is on main: it adds two fields to Deferred, back_wanted and texture_backs. With the Deferred::clear from this PR they don't get cleared on reset, so a reused list would land an old texture read-back again. I added self.back_wanted.clear() and self.texture_backs.clear() next to self.backs.clear() when merging both into my tree. Probably worth adding here before it goes in.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7175181296 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1059#issuecomment-7175181296). Originally posted by [Ym1r1710](https://github.com/Ym1r1710) on 2026-11-25T21:31:02Z. --- One thing to watch now that #999 (VICTORY_TEXTURE_BACK) is on main: it adds two fields to Deferred, back_wanted and texture_backs. With the Deferred::clear from this PR they don't get cleared on reset, so a reused list would land an old texture read-back again. I added self.back_wanted.clear() and self.texture_backs.clear() next to self.backs.clear() when merging both into my tree. Probably worth adding here before it goes in.
UwUCammie commented 2026-11-26 01:06:16 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by UwUCammie on 2026-11-26T00:31:09Z.


Updated this branch to 006c353 and fixed a stale readback on command-list reuse. The retained-capacity reset missed the two texture-readback queues introduced after this PR. A second, empty recording could copy the previous recording’s pixels over newer CPU data.

A synthetic 4×4 texture on the RX 7900 XT reproduced it: after the first GPU readback, I wrote 0xa5 to the first pixel. An empty recording on the reset list changed it back to 0x00. It now stays 0xa5. The reset names every Deferred field, so adding another queue or cache requires a reset decision at compile time. The merge also clears each block’s published clean bit before the batched write-watch call makes the run writable.

The new pixel test, the existing upload/readback test and the guarded kernel-read test passed under Wine 11.22 and RADV 26.3.1. The broader test filter had the same two failures on unmodified upstream: the Windows driver-event subscription and placeholder-region query. Fix and test.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7178840079 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1059#issuecomment-7178840079). Originally posted by [UwUCammie](https://github.com/UwUCammie) on 2026-11-26T00:31:09Z. --- Updated this branch to `006c353` and fixed a stale readback on command-list reuse. The retained-capacity reset missed the two texture-readback queues introduced after this PR. A second, empty recording could copy the previous recording’s pixels over newer CPU data. A synthetic 4×4 texture on the RX 7900 XT reproduced it: after the first GPU readback, I wrote `0xa5` to the first pixel. An empty recording on the reset list changed it back to `0x00`. It now stays `0xa5`. The reset names every `Deferred` field, so adding another queue or cache requires a reset decision at compile time. The merge also clears each block’s published clean bit before the batched write-watch call makes the run writable. The new pixel test, the existing upload/readback test and the guarded kernel-read test passed under Wine 11.22 and RADV 26.3.1. The broader test filter had the same two failures on unmodified upstream: the Windows driver-event subscription and placeholder-region query. [Fix and test](https://github.com/UwUCammie/victory-amd/commit/af4b566).
UwUCammie commented 2026-11-26 03:06:50 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by UwUCammie on 2026-11-26T02:43:49Z.


Updated the reset path for the three recording caches added in main: bound pipelines, filled root uploads and texture alias sources. The first use in a new recording must copy the GPU alias again, even when the same list and resources were used in its previous recording. I reproduced the stale result when combining the two changes on the RADV integration and added a regression there. The exhaustive field match in this PR also caught the two new list-reuse fields at build time.

At ea00c4e, the reset regression, GPU atlas readback and earlier texture-readback reset test passed on an RX 7900 XT (RADV 26.3.1, Wine). The broader watch filter has the same two Wine failures on ad54207 and on the PR: the driver-event subscription and the remaining-placeholder check. One test is ignored on each.

Reused-list regression on the integration branch

<!-- greyfinch-sync:fxceless/victory-fork:comment:7181066866 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1059#issuecomment-7181066866). Originally posted by [UwUCammie](https://github.com/UwUCammie) on 2026-11-26T02:43:49Z. --- Updated the reset path for the three recording caches added in main: bound pipelines, filled root uploads and texture alias sources. The first use in a new recording must copy the GPU alias again, even when the same list and resources were used in its previous recording. I reproduced the stale result when combining the two changes on the RADV integration and added a regression there. The exhaustive field match in this PR also caught the two new list-reuse fields at build time. At `ea00c4e`, the reset regression, GPU atlas readback and earlier texture-readback reset test passed on an RX 7900 XT (RADV 26.3.1, Wine). The broader watch filter has the same two Wine failures on `ad54207` and on the PR: the driver-event subscription and the remaining-placeholder check. One test is ignored on each. [Reused-list regression on the integration branch](https://github.com/UwUCammie/victory-amd/commit/bfeb0bb)
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/1059:upstream-pr/1059'
git switch 'upstream-pr/1059'

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/1059'
git switch 'upstream-pr/1059'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1059'
git switch 'upstream-pr/1059'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1059'
git switch 'main'
git merge --squash 'upstream-pr/1059'
git switch 'main'
git merge --ff-only 'upstream-pr/1059'
git switch 'main'
git merge 'upstream-pr/1059'
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
fxceless/victory-fork!1057
No description provided.