Shadow: build first-touch copies outside write locks and cache buffer misses #4817

Closed
Bazovsky wants to merge 1 commit from upstream-pr/4817 into main
Bazovsky commented 2026-12-01 10:09:18 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Bazovsky on 2026-12-01T10:09:18Z.


What it changes

On main, first-touch allocation of a registered title buffer copy (lookup_buffer) or loose window (loose) acquires BUFFERS.write() or LOOSE.write() across D3D12 resource / CPU mirror / in-place heap creation, stalling all concurrent VICTORY_THIN recording threads. In addition, lookup_buffer takes BUFFERS.write() via buffers(), bumping BUFFERS_CHANGED on every None -> Some first-touch population (~4,350 times during world entry and streaming) and invalidating every thread's FOUND cache and per-list root_views (VICTORY_ROOT_SAME), while lookup_buffer never caches BufferHit::Miss — forcing every loose-window and unaligned scratch root-CBV lookup to take BUFFERS.read() and scan BTreeMap nodes across max_size().

This change:

  • Builds first-touch buffer and loose-window shadows outside BUFFERS.write() / LOOSE.write() behind 64-way per-key build stripes (BUFFER_BUILD / LOOSE_BUILD), with in-stripe copy fallback when an in-place candidate is invalidated, and installs None -> Some into BUFFERS without bumping BUFFERS_CHANGED (since [start, end) and max_size() do not change).
  • Keeps Entry at 16 bytes (const _: () = assert!(size_of::<Entry>() == 16)) and tracks same-key overwrites, removals, and placed evictions in a 1,024-slot Buffers::ops ring while returning (key, &Entry) in a single BTreeMap walk (find_buffer_entry).
  • Adds a 16-slot thread-local MISS_GAPS interval cache (VICTORY_QUICK_FIND) in lookup_buffer that records proven buffer-free spans [gap_lo, gap_hi) bounded by max_end.max(page_lo) and the next registered buffer start, invalidated via BUFFERS_CHANGED SeqCst seqlock checks on register / unregister.
  • Synchronizes unlocked in-place builds (PLACE_GATE inflight + freeing, PlacedRegistry 256-event 64 KiB-aligned ring, PlacedActive / REGIONS_COUNT handoff in holds_any(), and 64 KiB-aligned CapGuard / SectionView byte-cap reservations) so title_memory_freeing drains overlapping in-flight heaps and declines new overlapping placements before VirtualFree / VirtualProtect runs.
  • Fixes Shadow::drop subtracting CARD_BYTES[0] when dropping a spilled loose window (window && fallback.0 != 0), balances buffer_sides rollback on CardCpu::allocate failure, uses atomic fetch_max frame-use tracking under LOOSE.read(), and converts diagnostic/census LOOSE.write() readers (stale_pages, spans, memory, window_trim, windows, window_use, window_cover) to LOOSE.read().

How it was tested

  • GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): NVIDIA GeForce RTX 5090 (32 GB), AMD Ryzen 9 9950X3D, Windows 11; Series X profile, 1440p, DLSS Super Resolution Quality, DLSS Frame Generation 2x, ray tracing off, stock play settings (4 x 115 s interleaved ABBA runs on 5fabb46e vs c36730f3, 0 errors in all runs).
  • Before:
    • Steady-state VICTORY_THIN translation CPU time (all threads/frame): median 86.68 ms (86.85 / 86.50 ms), p95 124.90 ms (121.20 / 128.60 ms)
    • World-entry streaming burst ([0..10 s] post-load): 31.56 FPS (30.33 / 32.78), worst frame 231.9 ms (297.1 / 166.6 ms)
    • Full post-load window (2 x 72 s): median frame 29.95 ms (33.59 native FPS / 67.18 FG 2x FPS), p95 per-second max frame 98.15 ms (102.9 / 93.4 ms), worst frame 231.85 ms, smooth seconds (<= 50 ms) 70.8%
  • After:
    • Steady-state VICTORY_THIN translation CPU time (all threads/frame): median 84.13 ms (83.95 / 84.30 ms, -2.9%), p95 108.30 ms (108.20 / 108.40 ms, -13.3%)
    • World-entry streaming burst ([0..10 s] post-load): 33.67 FPS (33.22 / 34.11, +6.7%), worst frame 152.9 ms (161.7 / 144.1 ms, -34.1%)
    • Full post-load window (2 x 72 s): median frame 29.90 ms (33.34 native FPS / 66.68 FG 2x FPS), p95 per-second max frame 83.25 ms (78.7 / 87.8 ms, -15.2%), worst frame 181.45 ms (-21.7%), smooth seconds (<= 50 ms) 72.2%
  • For shader changes, the wave sizes tested (32 and/or 64 lanes): not applicable (no shader translator changes)
  • cargo test passed for the crates it touches: yes (victory-gpu --lib on x86_64-pc-windows-msvc: shadow:: 89 passed x 5 runs under --test-threads=8, inplace:: 15 passed, port:: 4 passed, watch::tests:: 43 passed; cargo xtask policy passed; cargo xwin clippy has 0 warnings in touched files, with 5 pre-existing collapsible_if warnings in crates/victory-gpu/src/host/{font,menu,panel}.rs on main)

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches. Related: #3401, #3503, #4041, #4715, #4750
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5583495178 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4817). Originally posted by [Bazovsky](https://github.com/Bazovsky) on 2026-12-01T10:09:18Z. --- ## What it changes On `main`, first-touch allocation of a registered title buffer copy (`lookup_buffer`) or loose window (`loose`) acquires `BUFFERS.write()` or `LOOSE.write()` across D3D12 resource / CPU mirror / in-place heap creation, stalling all concurrent `VICTORY_THIN` recording threads. In addition, `lookup_buffer` takes `BUFFERS.write()` via `buffers()`, bumping `BUFFERS_CHANGED` on every `None -> Some` first-touch population (~4,350 times during world entry and streaming) and invalidating every thread's `FOUND` cache and per-list `root_views` (`VICTORY_ROOT_SAME`), while `lookup_buffer` never caches `BufferHit::Miss` — forcing every loose-window and unaligned scratch root-CBV lookup to take `BUFFERS.read()` and scan `BTreeMap` nodes across `max_size()`. This change: - Builds first-touch buffer and loose-window shadows outside `BUFFERS.write()` / `LOOSE.write()` behind 64-way per-key build stripes (`BUFFER_BUILD` / `LOOSE_BUILD`), with in-stripe copy fallback when an in-place candidate is invalidated, and installs `None -> Some` into `BUFFERS` without bumping `BUFFERS_CHANGED` (since `[start, end)` and `max_size()` do not change). - Keeps `Entry` at 16 bytes (`const _: () = assert!(size_of::<Entry>() == 16)`) and tracks same-key overwrites, removals, and placed evictions in a 1,024-slot `Buffers::ops` ring while returning `(key, &Entry)` in a single `BTreeMap` walk (`find_buffer_entry`). - Adds a 16-slot thread-local `MISS_GAPS` interval cache (`VICTORY_QUICK_FIND`) in `lookup_buffer` that records proven buffer-free spans `[gap_lo, gap_hi)` bounded by `max_end.max(page_lo)` and the next registered buffer start, invalidated via `BUFFERS_CHANGED` SeqCst seqlock checks on `register` / `unregister`. - Synchronizes unlocked in-place builds (`PLACE_GATE` `inflight` + `freeing`, `PlacedRegistry` 256-event 64 KiB-aligned ring, `PlacedActive` / `REGIONS_COUNT` handoff in `holds_any()`, and 64 KiB-aligned `CapGuard` / `SectionView` byte-cap reservations) so `title_memory_freeing` drains overlapping in-flight heaps and declines new overlapping placements before `VirtualFree` / `VirtualProtect` runs. - Fixes `Shadow::drop` subtracting `CARD_BYTES[0]` when dropping a spilled loose window (`window && fallback.0 != 0`), balances `buffer_sides` rollback on `CardCpu::allocate` failure, uses atomic `fetch_max` frame-use tracking under `LOOSE.read()`, and converts diagnostic/census `LOOSE.write()` readers (`stale_pages`, `spans`, `memory`, `window_trim`, `windows`, `window_use`, `window_cover`) to `LOOSE.read()`. ## How it was tested - GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): NVIDIA GeForce RTX 5090 (32 GB), AMD Ryzen 9 9950X3D, Windows 11; Series X profile, 1440p, DLSS Super Resolution Quality, DLSS Frame Generation 2x, ray tracing off, stock `play` settings (`4 x 115 s` interleaved ABBA runs on `5fabb46e` vs `c36730f3`, `0` errors in all runs). - Before: - Steady-state `VICTORY_THIN` translation CPU time (all threads/frame): median `86.68 ms` (`86.85 / 86.50 ms`), p95 `124.90 ms` (`121.20 / 128.60 ms`) - World-entry streaming burst (`[0..10 s]` post-load): `31.56 FPS` (`30.33 / 32.78`), worst frame `231.9 ms` (`297.1 / 166.6 ms`) - Full post-load window (`2 x 72 s`): median frame `29.95 ms` (`33.59` native FPS / `67.18` FG 2x FPS), p95 per-second max frame `98.15 ms` (`102.9 / 93.4 ms`), worst frame `231.85 ms`, smooth seconds (`<= 50 ms`) `70.8%` - After: - Steady-state `VICTORY_THIN` translation CPU time (all threads/frame): median `84.13 ms` (`83.95 / 84.30 ms`, **-2.9%**), p95 `108.30 ms` (`108.20 / 108.40 ms`, **-13.3%**) - World-entry streaming burst (`[0..10 s]` post-load): `33.67 FPS` (`33.22 / 34.11`, **+6.7%**), worst frame `152.9 ms` (`161.7 / 144.1 ms`, **-34.1%**) - Full post-load window (`2 x 72 s`): median frame `29.90 ms` (`33.34` native FPS / `66.68` FG 2x FPS), p95 per-second max frame `83.25 ms` (`78.7 / 87.8 ms`, **-15.2%**), worst frame `181.45 ms` (**-21.7%**), smooth seconds (`<= 50 ms`) `72.2%` - For shader changes, the wave sizes tested (32 and/or 64 lanes): not applicable (no shader translator changes) - `cargo test` passed for the crates it touches: yes (`victory-gpu --lib` on `x86_64-pc-windows-msvc`: `shadow::` 89 passed x 5 runs under `--test-threads=8`, `inplace::` 15 passed, `port::` 4 passed, `watch::tests::` 43 passed; `cargo xtask policy` passed; `cargo xwin clippy` has 0 warnings in touched files, with 5 pre-existing `collapsible_if` warnings in `crates/victory-gpu/src/host/{font,menu,panel}.rs` on `main`) ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. Related: #3401, #3503, #4041, #4715, #4750
trustno1-coder commented 2026-12-01 18:31:21 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by trustno1-coder on 2026-12-01T18:31:21Z.


Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7321080018 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4817#issuecomment-7321080018). Originally posted by [trustno1-coder](https://github.com/trustno1-coder) on 2026-12-01T18:31:21Z. --- Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

Pull request closed

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
trustno1-coder/VIctory-NVIDIA!4817
No description provided.