perf(gpu): reservation-aware write-watch batching, lock-free view/non-watched bitsets & Wine/Proton compatibility #207

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

@⁠jocost4 on GitHub · 2026-11-27 19:43 UTC · original
jocost4:perf/watch-batching-and-wine-compat → main · +289 −46 · 5 files · 1 commits · open

Summary

A set of low-risk, zero-regression performance and Wine/Proton compatibility improvements for victory-gpu (default Windows behavior is preserved everywhere; all tuning knobs are opt-in via environment variables):

1. Write-watch & guard optimizations (crates/victory-gpu/src/watch.rs)

  • Fused reservation-aware ask_once + RESERVATIONS cache: Caches VirtualAlloc reservation spans (AllocationBase..end) so ask_once only merges GetWriteWatch ranges within the same reservation — eliminating cross-reservation GetWriteWatch failures and binary splits.
  • Fine-grained Asking::hold_many: Replaces Asking::hold_all() (0..usize::MAX) in ask_once with atomic range holding for only the queried spans, allowing unrelated memory (e.g. shader-visible descriptor heaps in sync_heaps and texture refreshes) to proceed concurrently.
  • Lock-free KNOWN_VIEW and SINGLE_NOT_WATCHED bitsets:
    • KNOWN_VIEW marks blocks confirmed as MEM_MAPPED view memory inside protect_here (cleared on g.foreign and all title_memory_* invalidations).
    • SINGLE_NOT_WATCHED only marks isolated single 64 KiB blocks (last - first == 1) where GetWriteWatch failed (never poisoning adjacent write-watched blocks).
    • Adds a lock-free fast path in writes(..., false) before acquiring Asking::hold, avoiding redundant lock/condvar churn on known view memory (e.g. texture.rs).
  • Wine/Proton placeholder() fallback: Falls back to VirtualQuery (MEM_RESERVE && PAGE_NOACCESS) when NtQueryVirtualMemory(class = 7) returns STATUS_INVALID_INFO_CLASS under Wine/Proton (zero impact on native Windows, where NtQueryVirtualMemory succeeds). Reduces hole queries from ~15,000/frame to ~8–48/frame under Proton.
  • Loop termination & lock-order hardening: Adds to <= end progress guards in allocation_end / reservation_of and calls forget_reservations outside the GUARDS lock in title_memory_unmapping / title_memory_freed.
  • Configurable VICTORY_AHEAD_GAP_KB (default 8192 = 8 MiB, unchanged on Windows; setting e.g. 256 on Wine/Proton avoids scanning megabytes of unused pages under Wine's virtual.c lock).

2. Additional low-overhead improvements

  • crates/victory-gpu/src/shadow.rs: Short-circuits wide_each for n == 0 and n == 1 to avoid worker pool wakeups for trivial work.
  • crates/victory-gpu/src/host.rs: Sets hbrBackground to BLACK_BRUSH in make_window to avoid a white/blue uninitialized window flash before the first Present().
  • crates/victory-gpu/src/draw.rs: Makes buffer-heap rescan cadence configurable via VICTORY_HEAP_RESCAN_FRAMES (default 16, unchanged).
  • crates/victory-gpu/src/texture.rs: Makes guarded texture blind-write hash cadence configurable via VICTORY_TEXTURE_HASH_EVERY (default 8, unchanged).

Verification

  • Verified clean cross-compilation for x86_64-pc-windows-msvc (cargo xwin check --release -p victory --target x86_64-pc-windows-msvc, 0 new warnings).
  • Tested on Linux (Ubuntu 26.10, Proton-GE / Wine 11.0 + vkd3d-proton 3.2, Ryzen 5 5600 + RTX 3060 Ti 8 GB): cut frame preparing time from ~200+ ms down to ~50–55 ms/frame (copies ~11–12 ms, textures ~5–6 ms, ask_once ~14–19 ms, heaps_wait ~4–5 ms) with GPU utilization reaching 88–92%.
**@⁠jocost4** on GitHub · 2026-11-27 19:43 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/207) `jocost4:perf/watch-batching-and-wine-compat` → `main` · +289 −46 · 5 files · 1 commits · open ## Summary A set of low-risk, zero-regression performance and Wine/Proton compatibility improvements for `victory-gpu` (default Windows behavior is preserved everywhere; all tuning knobs are opt-in via environment variables): ### 1. Write-watch & guard optimizations (`crates/victory-gpu/src/watch.rs`) - **Fused reservation-aware `ask_once` + `RESERVATIONS` cache:** Caches `VirtualAlloc` reservation spans (`AllocationBase..end`) so `ask_once` only merges `GetWriteWatch` ranges within the same reservation — eliminating cross-reservation `GetWriteWatch` failures and binary splits. - **Fine-grained `Asking::hold_many`:** Replaces `Asking::hold_all()` (`0..usize::MAX`) in `ask_once` with atomic range holding for only the queried spans, allowing unrelated memory (e.g. shader-visible descriptor heaps in `sync_heaps` and texture refreshes) to proceed concurrently. - **Lock-free `KNOWN_VIEW` and `SINGLE_NOT_WATCHED` bitsets:** - `KNOWN_VIEW` marks blocks confirmed as `MEM_MAPPED` view memory inside `protect_here` (cleared on `g.foreign` and all `title_memory_*` invalidations). - `SINGLE_NOT_WATCHED` only marks isolated single 64 KiB blocks (`last - first == 1`) where `GetWriteWatch` failed (never poisoning adjacent write-watched blocks). - Adds a lock-free fast path in `writes(..., false)` before acquiring `Asking::hold`, avoiding redundant lock/condvar churn on known view memory (e.g. `texture.rs`). - **Wine/Proton `placeholder()` fallback:** Falls back to `VirtualQuery` (`MEM_RESERVE && PAGE_NOACCESS`) when `NtQueryVirtualMemory(class = 7)` returns `STATUS_INVALID_INFO_CLASS` under Wine/Proton (zero impact on native Windows, where `NtQueryVirtualMemory` succeeds). Reduces hole queries from ~15,000/frame to ~8–48/frame under Proton. - **Loop termination & lock-order hardening:** Adds `to <= end` progress guards in `allocation_end` / `reservation_of` and calls `forget_reservations` outside the `GUARDS` lock in `title_memory_unmapping` / `title_memory_freed`. - **Configurable `VICTORY_AHEAD_GAP_KB`** (default `8192` = 8 MiB, unchanged on Windows; setting e.g. `256` on Wine/Proton avoids scanning megabytes of unused pages under Wine's `virtual.c` lock). ### 2. Additional low-overhead improvements - **`crates/victory-gpu/src/shadow.rs`:** Short-circuits `wide_each` for `n == 0` and `n == 1` to avoid worker pool wakeups for trivial work. - **`crates/victory-gpu/src/host.rs`:** Sets `hbrBackground` to `BLACK_BRUSH` in `make_window` to avoid a white/blue uninitialized window flash before the first `Present()`. - **`crates/victory-gpu/src/draw.rs`:** Makes buffer-heap rescan cadence configurable via `VICTORY_HEAP_RESCAN_FRAMES` (default `16`, unchanged). - **`crates/victory-gpu/src/texture.rs`:** Makes guarded texture blind-write hash cadence configurable via `VICTORY_TEXTURE_HASH_EVERY` (default `8`, unchanged). ## Verification - Verified clean cross-compilation for `x86_64-pc-windows-msvc` (`cargo xwin check --release -p victory --target x86_64-pc-windows-msvc`, 0 new warnings). - Tested on Linux (Ubuntu 26.10, Proton-GE / Wine 11.0 + vkd3d-proton 3.2, Ryzen 5 5600 + RTX 3060 Ti 8 GB): cut frame `preparing` time from ~200+ ms down to ~50–55 ms/frame (`copies` ~11–12 ms, `textures` ~5–6 ms, `ask_once` ~14–19 ms, `heaps_wait` ~4–5 ms) with GPU utilization reaching 88–92%.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 19:47:34 +00:00
Author
Owner

@⁠domx4q on GitHub · 2026-11-27 21:42 UTC · original

Independent confirmation of the Wine/Proton placeholder fallback in this PR, from a separate setup.

Setup: i9-14900KF, RTX 4090 (driver 620.73), Proton with vkd3d-proton, Linux 7.3, headless gamescope, prologue (prison cell), 1080p -> 1440p DLSS Quality. Same save, same scene, 60 s average per run.

I hit the same problem independently. On Wine, NtQueryVirtualMemory(..., MemoryRegionInformationEx) returns STATUS_INVALID_INFO_CLASS, so watch::placeholder() never learns that a hole is a placeholder. In my profile that meant ~16,000 VirtualQuery calls per frame under the guards' lock (ntdll.so:get_basic_memory_info was the hottest symbol in the victory-wide threads).

My local fix was slightly different: a small BTreeMap of the runtime's own MEM_RESERVE_PLACEHOLDER reservations, filled from xmem.rs and cleared in title_memory_freed, used only after the host has once answered STATUS_INVALID_INFO_CLASS. Result on top of VICTORY_PIPELINE_WAIT_MS=0: 18.6 -> 20.5 fps, "preparing" ~45 -> ~31 ms/frame. Your version covers the same case, so I'm not opening a separate PR. I'm happy to post my diff if a second variant is useful for comparison.

Not tested on Windows by me (the new path only runs when the host rejects the info class, which Windows doesn't).

**@⁠domx4q** on GitHub · 2026-11-27 21:42 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/207#issuecomment-7228682798) Independent confirmation of the Wine/Proton placeholder fallback in this PR, from a separate setup. Setup: i9-14900KF, RTX 4090 (driver 620.73), Proton with vkd3d-proton, Linux 7.3, headless gamescope, prologue (prison cell), 1080p -> 1440p DLSS Quality. Same save, same scene, 60 s average per run. I hit the same problem independently. On Wine, `NtQueryVirtualMemory(..., MemoryRegionInformationEx)` returns `STATUS_INVALID_INFO_CLASS`, so `watch::placeholder()` never learns that a hole is a placeholder. In my profile that meant ~16,000 VirtualQuery calls per frame under the guards' lock (`ntdll.so:get_basic_memory_info` was the hottest symbol in the `victory-wide` threads). My local fix was slightly different: a small `BTreeMap` of the runtime's own `MEM_RESERVE_PLACEHOLDER` reservations, filled from `xmem.rs` and cleared in `title_memory_freed`, used only after the host has once answered `STATUS_INVALID_INFO_CLASS`. Result on top of `VICTORY_PIPELINE_WAIT_MS=0`: 18.6 -> 20.5 fps, "preparing" ~45 -> ~31 ms/frame. Your version covers the same case, so I'm not opening a separate PR. I'm happy to post my diff if a second variant is useful for comparison. Not tested on Windows by me (the new path only runs when the host rejects the info class, which Windows doesn't).
Author
Owner

@⁠jocost4 on GitHub · 2026-11-28 00:34 UTC · original

Thanks for the independent numbers, @⁠domx4q — good to see the same ~16k queries/frame bottleneck confirmed on a 4090 setup too.

Your BTreeMap approach from xmem.rs is actually cleaner for distinguishing real MEM_RESERVE_PLACEHOLDER spans from plain PAGE_NOACCESS reservations (and avoids the VirtualQuery call altogether once STATUS_INVALID_INFO_CLASS is seen). Feel free to drop your diff here if you'd like — happy to compare or fold it in if maintainers prefer that variant.

**@⁠jocost4** on GitHub · 2026-11-28 00:34 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/207#issuecomment-7231024872) Thanks for the independent numbers, @⁠domx4q — good to see the same ~16k queries/frame bottleneck confirmed on a 4090 setup too. Your `BTreeMap` approach from `xmem.rs` is actually cleaner for distinguishing real `MEM_RESERVE_PLACEHOLDER` spans from plain `PAGE_NOACCESS` reservations (and avoids the `VirtualQuery` call altogether once `STATUS_INVALID_INFO_CLASS` is seen). Feel free to drop your diff here if you'd like — happy to compare or fold it in if maintainers prefer that variant.
Author
Owner

@⁠POMXARK on GitHub · 2026-11-28 02:11 UTC · original

Testing feedback on Windows 11, AMD Ryzen 7 5700X (8C/16T), NVIDIA RTX 5080 16 GB, 32 GB RAM, Series X 1440p + DLSS Quality + DLSS MFG 4× (4 MiB windows).

Full disclosure: tested on a fork branch based on v2026.11.27.7 (with local GPU patches + x86-64-v3/ThinLTO), porting only the watch.rs changes (reservation_of, Asking::hold_many, KNOWN_VIEW, SINGLE_NOT_WATCHED, fused ask_once with sorted ahead_normalize preserved for PR #469's binary search) plus wide_each(0/1) in shadow.rs (omitting the Wine/Proton knobs).

Result

  • Unit tests: all 48/48 cargo test -p victory-gpu --lib watch tests passed cleanly (0.78s) after updating the ask_rest test call to ask_once.
  • In-game 3D run (reproducible failure at t=169s, Prison -> Office):
    • Baseline (same branch without PR #207): 14.0 FPS (~71 ms/frame) in Prison/Office, 12-minute run completes with 0 errors / 0 DeviceRemoved.
    • With PR #207 watch.rs: 3D immediately degraded to 0.2–6.3 FPS (158.9 -> 275.8 -> 1297.7 -> 4072.7 ms/frame, copies up to 67.2 ms vs 28.1 ms baseline, list::thin: 19464 in 12280.4 ms), followed by DXGI_ERROR_DEVICE_REMOVED (0x887A0005) at t=169s:
      • WARN victory_gpu::streamline: DLSS Frame Generation: a present failed hresult=0x887a0005
      • ERROR victory_gpu::host: a submission sent ahead could not be followed by a signal ... (0x887A0005); device removed
    • Reverting only watch.rs and shadow.rs immediately restored clean 12-minute stability.

Possible root causes to double-check on native Windows:

  1. SINGLE_NOT_WATCHED / KNOWN_VIEW bitsets skipping GetWriteWatch if any 64 KiB block transitions state (e.g., uncommitted -> committed) without hitting one of the title_memory_* clear hooks, or
  2. Asking::hold_many(spans) allowing concurrent sync_heaps / texture refreshes during ask_once, leading to stale buffer/descriptor reads on the GPU and a TDR (0x887A0005).
**@⁠POMXARK** on GitHub · 2026-11-28 02:11 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/207#issuecomment-7232188941) Testing feedback on **Windows 11, AMD Ryzen 7 5700X (8C/16T), NVIDIA RTX 5080 16 GB, 32 GB RAM**, Series X 1440p + DLSS Quality + DLSS MFG 4× (`4 MiB` windows). Full disclosure: tested on a fork branch based on `v2026.11.27.7` (with local GPU patches + `x86-64-v3`/ThinLTO), porting only the `watch.rs` changes (`reservation_of`, `Asking::hold_many`, `KNOWN_VIEW`, `SINGLE_NOT_WATCHED`, fused `ask_once` with sorted `ahead_normalize` preserved for PR #469's binary search) plus `wide_each(0/1)` in `shadow.rs` (omitting the Wine/Proton knobs). ### Result - **Unit tests:** all **48/48** `cargo test -p victory-gpu --lib watch` tests passed cleanly (`0.78s`) after updating the `ask_rest` test call to `ask_once`. - **In-game 3D run (reproducible failure at `t=169s`, Prison -> Office):** - Baseline (same branch without PR #207): `14.0 FPS` (`~71 ms/frame`) in Prison/Office, 12-minute run completes with `0` errors / `0` `DeviceRemoved`. - With PR #207 `watch.rs`: 3D immediately degraded to **`0.2–6.3 FPS`** (`158.9 -> 275.8 -> 1297.7 -> 4072.7 ms/frame`, `copies` up to `67.2 ms` vs `28.1 ms` baseline, `list::thin: 19464 in 12280.4 ms`), followed by **`DXGI_ERROR_DEVICE_REMOVED` (`0x887A0005`)** at `t=169s`: - `WARN victory_gpu::streamline: DLSS Frame Generation: a present failed hresult=0x887a0005` - `ERROR victory_gpu::host: a submission sent ahead could not be followed by a signal ... (0x887A0005); device removed` - Reverting only `watch.rs` and `shadow.rs` immediately restored clean 12-minute stability. Possible root causes to double-check on native Windows: 1. `SINGLE_NOT_WATCHED` / `KNOWN_VIEW` bitsets skipping `GetWriteWatch` if any 64 KiB block transitions state (e.g., uncommitted -> committed) without hitting one of the `title_memory_*` clear hooks, or 2. `Asking::hold_many(spans)` allowing concurrent `sync_heaps` / texture refreshes during `ask_once`, leading to stale buffer/descriptor reads on the GPU and a TDR (`0x887A0005`).
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/207/head:github-bot/gh-207'
git switch 'github-bot/gh-207'

Merge

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