watch: lock-free DirectStorage stream notices and soft-view guard fast paths #2655

Manually merged
github merged 1 commit from github-bot/gh-2655 into main 2026-11-30 14:15:46 +00:00 AGit
Owner

@⁠0x4e454f on GitHub · 2026-11-29 15:37 UTC · original
0x4e454f:watch-stream-fastpath → main · +177 −39 · 4 files · 1 commits · merged 2026-11-30 14:12 UTC

Summary

Cuts watch::writes() CPU overhead on streaming view windows (SoftViews / placeholder-backed 6 MB windows) by 61% (5,566 ms -> 2,192 ms per 10 s across refresh threads, 2.54x faster) without changing guard semantics or visual output.

Changes

  1. Lock-free DirectStorage write notices (stream_memory_written) (watch.rs, lib.rs, xmem.rs, dstorage.rs):
    • xmem::pages_written (and post-write dstorage::into_destination) previously called kernel_will_write -> watch::writing -> open() under the global GUARDS mutex, calling VirtualProtect(..., PAGE_READWRITE) on every 64 KiB block (including SoftViews blocks where VirtualProtect always fails) only for the next refresh to re-guard them.
    • Replaced with stream_memory_written(at, len, all_views) which marks blocks via atomic BESIDE.set + PUBLISHED.clear (and descriptor::host::remapped) with zero GUARDS lock and zero VirtualProtect syscalls.
  2. O(words) BESIDE fast-path in watch::guard():
    • When clean_or_held(&g, first, last) && !unsettled(first, last) is true, guard() now handles both empty and BESIDE-dirty ranges in a word scan without calling VirtualProtect, VirtualQuery, g.absent.range, or soft_runs (xmem::PHYSICAL lock).
    • Also stopped unconditional drop/re-lock of GUARDS when unsure is empty, and lazily fetches soft_runs only when protect() is actually needed.
  3. Skip failing VirtualProtect on SoftViews in unguard() (Guards::soft):
    • Tracks blocks guarded in the books alone (in_books / soft_block) in Guards::soft so unguard() skips VirtualProtect on them.
  4. Placeholder & view-memory fast-paths:
    • writes() calls publish_range(first, last) on empty guard() returns as well so cover=Holes windows hit published() on subsequent frames.
    • placeholder(block) checks in-memory PLACEHOLDERS before NtQueryVirtualMemory.

ot_write_watched(first, last) (checking PROTECTED, inplace::cover, and PLACEHOLDERS) skips failing GetWriteWatch syscalls in writes(), sk_ahead(), sk_rest(), and sk_more().

Measured Impact (GTA 6 Series S open world, Windows Server 2025, 4c/8t Zen 2 + A10G)

  • Shadow watch::writes() total (10 s, all threads): 5,566.1 ms -> 2,192.0 ms (-61%)
  • Top refreshed 6 MB window watch time (10 s): 148.2 ms -> 54.9 ms (-63%)

efreshes per frame (wall-clock): 16.9–23.0 ms -> 12.1–16.5 ms

Verification

  • cargo test -p victory-gpu --locked --lib watch::tests -- --test-threads=1 — 40 passed, 0 failed
  • cargo test -p victory-hle --locked --lib modules::xmem::tests::page_windows_cover_one_run_in_place — 1 passed, 0 failed
  • Release build play-tested in open world with zero visual regressions.
**@⁠0x4e454f** on GitHub · 2026-11-29 15:37 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2655) `0x4e454f:watch-stream-fastpath` → `main` · +177 −39 · 4 files · 1 commits · merged 2026-11-30 14:12 UTC ## Summary Cuts watch::writes() CPU overhead on streaming view windows (SoftViews / placeholder-backed 6 MB windows) by **61%** (5,566 ms -> 2,192 ms per 10 s across refresh threads, 2.54x faster) without changing guard semantics or visual output. ### Changes 1. **Lock-free DirectStorage write notices (stream_memory_written)** (watch.rs, lib.rs, xmem.rs, dstorage.rs): - xmem::pages_written (and post-write dstorage::into_destination) previously called kernel_will_write -> watch::writing -> open() under the global GUARDS mutex, calling VirtualProtect(..., PAGE_READWRITE) on every 64 KiB block (including SoftViews blocks where VirtualProtect always fails) only for the next refresh to re-guard them. - Replaced with stream_memory_written(at, len, all_views) which marks blocks via atomic BESIDE.set + PUBLISHED.clear (and descriptor::host::remapped) with zero GUARDS lock and zero VirtualProtect syscalls. 2. **O(words) BESIDE fast-path in watch::guard()**: - When clean_or_held(&g, first, last) && !unsettled(first, last) is true, guard() now handles both empty and BESIDE-dirty ranges in a word scan without calling VirtualProtect, VirtualQuery, g.absent.range, or soft_runs (xmem::PHYSICAL lock). - Also stopped unconditional drop/re-lock of GUARDS when unsure is empty, and lazily fetches soft_runs only when protect() is actually needed. 3. **Skip failing VirtualProtect on SoftViews in unguard() (Guards::soft)**: - Tracks blocks guarded in the books alone (in_books / soft_block) in Guards::soft so unguard() skips VirtualProtect on them. 4. **Placeholder & view-memory fast-paths**: - writes() calls publish_range(first, last) on empty guard() returns as well so cover=Holes windows hit published() on subsequent frames. - placeholder(block) checks in-memory PLACEHOLDERS before NtQueryVirtualMemory. - ot_write_watched(first, last) (checking PROTECTED, inplace::cover, and PLACEHOLDERS) skips failing GetWriteWatch syscalls in writes(), sk_ahead(), sk_rest(), and sk_more(). ## Measured Impact (GTA 6 Series S open world, Windows Server 2025, 4c/8t Zen 2 + A10G) - Shadow watch::writes() total (10 s, all threads): **5,566.1 ms -> 2,192.0 ms** (**-61%**) - Top refreshed 6 MB window watch time (10 s): **148.2 ms -> 54.9 ms** (**-63%**) - efreshes per frame (wall-clock): **16.9–23.0 ms -> 12.1–16.5 ms** ## Verification - cargo test -p victory-gpu --locked --lib watch::tests -- --test-threads=1 — **40 passed, 0 failed** - cargo test -p victory-hle --locked --lib modules::xmem::tests::page_windows_cover_one_run_in_place — **1 passed, 0 failed** - Release build play-tested in open world with zero visual regressions.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-29 15:41:38 +00:00
Author
Owner

@⁠CopiiDeco on GitHub · 2026-11-29 19:42 UTC · original

Tested this on Windows; two things worth a look.

Rig: Ryzen 5 5600X (6C/12T), RTX 3080 10 GB (driver 616.92), 32 GB DDR4-3600, B450 (PCIe 3.0), Windows 11 26200.
Setup: diagnostics build, scripts/bench.ps1 -Dlss performance -FrameGen off -RayTracing off, Series S profile, VICTORY_WC_BAR=1 VICTORY_WINDOWS_BAR_MB=2048, same save and opening cutscene each run, 30 s warm-up + 120 s measured, pipelines precompiled.

1. On its own (main 829188e9 + this PR): frame rate unchanged, but many more writes missed by the tracking.

median fps preparing p99.9 "a texture picked by index changed after its upload with nothing to tell of it"
main 18.2 42.8 ms 199 ms 30
this PR 18.0 43.0 ms 170 ms 200

That warning is the safety net catching a write the watch/guards did not report, so ~6.7x more of them suggests some writes now go unnoticed. Textures get re-uploaded by that net; buffers might not have one, which is why I'm flagging it rather than calling it harmless.

2. Combined with other open PRs: an intermittent device-level failure. main + #2776 (with VICTORY_INPLACE=1 VICTORY_INPLACE_VIEWS=1 VICTORY_SOFT_VIEWS=1) + #2656 + #2647 + #2745 + this PR (I resolved the textual conflicts in watch.rs by keeping #2647's structure and adding this PR's not_write_watched filter at the three places) failed about a minute into the cutscene:

ERROR victory_gpu::host: a host command list did not close (The parameter is incorrect. (0x80070057)): a command recorded into it was invalid

The same stack without this PR ran the full window with no failure, so it points here or at its interaction with those PRs. A rerun with VICTORY_D3D12_DEBUG=1 did not reproduce it in 5 minutes (the debug layer reported no errors, only resource-state warnings), so it looks timing-dependent, which would fit a race in the new lock-free paths (stream_memory_written, the BESIDE fast path in guard()).

One run per configuration, ±0.4 fps run-to-run noise on this rig. I can rerun anything you want (timeline, other switches) if it helps.

**@⁠CopiiDeco** on GitHub · 2026-11-29 19:42 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2655#issuecomment-7261487104) Tested this on Windows; two things worth a look. **Rig:** Ryzen 5 5600X (6C/12T), RTX 3080 10 GB (driver 616.92), 32 GB DDR4-3600, B450 (PCIe 3.0), Windows 11 26200. **Setup:** diagnostics build, `scripts/bench.ps1 -Dlss performance -FrameGen off -RayTracing off`, Series S profile, `VICTORY_WC_BAR=1 VICTORY_WINDOWS_BAR_MB=2048`, same save and opening cutscene each run, 30 s warm-up + 120 s measured, pipelines precompiled. **1. On its own (main 829188e9 + this PR): frame rate unchanged, but many more writes missed by the tracking.** | | median fps | preparing | p99.9 | "a texture picked by index changed after its upload with nothing to tell of it" | |---|---|---|---|---| | main | 18.2 | 42.8 ms | 199 ms | 30 | | this PR | 18.0 | 43.0 ms | 170 ms | **200** | That warning is the safety net catching a write the watch/guards did not report, so ~6.7x more of them suggests some writes now go unnoticed. Textures get re-uploaded by that net; buffers might not have one, which is why I'm flagging it rather than calling it harmless. **2. Combined with other open PRs: an intermittent device-level failure.** main + #2776 (with `VICTORY_INPLACE=1 VICTORY_INPLACE_VIEWS=1 VICTORY_SOFT_VIEWS=1`) + #2656 + #2647 + #2745 + this PR (I resolved the textual conflicts in `watch.rs` by keeping #2647's structure and adding this PR's `not_write_watched` filter at the three places) failed about a minute into the cutscene: ``` ERROR victory_gpu::host: a host command list did not close (The parameter is incorrect. (0x80070057)): a command recorded into it was invalid ``` The same stack **without this PR** ran the full window with no failure, so it points here or at its interaction with those PRs. A rerun with `VICTORY_D3D12_DEBUG=1` did **not** reproduce it in 5 minutes (the debug layer reported no errors, only resource-state warnings), so it looks timing-dependent, which would fit a race in the new lock-free paths (`stream_memory_written`, the `BESIDE` fast path in `guard()`). One run per configuration, ±0.4 fps run-to-run noise on this rig. I can rerun anything you want (timeline, other switches) if it helps.
Author
Owner

@⁠CopiiDeco on GitHub · 2026-11-29 19:56 UTC · original

Correction to my comment above: please disregard the "a texture picked by index changed after its upload with nothing to tell of it" counts. I counted that warning across all of today's runs of unmodified main on this rig and it is bimodal from run to run (8, 27, 30, 52, 124, 160, 200, 201, 204 …), so the ~200 I saw with this PR is within main's own range and says nothing about missed writes. Sorry for the noise.

The rest stands as measured (one run per configuration, so treat the fps deltas as indicative).

**@⁠CopiiDeco** on GitHub · 2026-11-29 19:56 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2655#issuecomment-7261684382) **Correction to my comment above:** please disregard the "a texture picked by index changed after its upload with nothing to tell of it" counts. I counted that warning across all of today's runs of **unmodified main** on this rig and it is bimodal from run to run (8, 27, 30, 52, 124, 160, 200, 201, 204 …), so the ~200 I saw with this PR is within main's own range and says nothing about missed writes. Sorry for the noise. The rest stands as measured (one run per configuration, so treat the fps deltas as indicative).
Author
Owner

@⁠CopiiDeco on GitHub · 2026-11-30 00:00 UTC · original

Correction on point 2 above: the device-level failure (a host command list did not close (0x80070057)) is probably not this PR's. #2656 on its own was since reported to fail with the same error on another rig (2 runs out of 2, in the plane cutscene), and #2656 was in my five-PR stack too; the run without this PR that passed was one run of an intermittent failure. Sorry for pointing it here.

**@⁠CopiiDeco** on GitHub · 2026-11-30 00:00 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2655#issuecomment-7264886823) **Correction on point 2 above:** the device-level failure (`a host command list did not close (0x80070057)`) is probably not this PR's. #2656 on its own was since reported to fail with the same error on another rig (2 runs out of 2, in the plane cutscene), and #2656 was in my five-PR stack too; the run without this PR that passed was one run of an intermittent failure. Sorry for pointing it here.
github manually merged commit b17a3bd2b5 into main 2026-11-30 14:15:45 +00:00
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/trustno1-coder--VIctory-NVIDIA!2655
No description provided.