Skip to content

Shadows & watch: fused AVX2 word_runs_and_update, linear stale_pages_in, and sorted Journal::ahead - #1637

Open
Bazovsky wants to merge 1 commit into
trustno1-coder:mainfrom
Bazovsky:pr/shadow-watch-fused
Open

Bazovsky wants to merge 1 commit into
trustno1-coder:mainfrom
Bazovsky:pr/shadow-watch-fused

Conversation

@Bazovsky

Copy link
Copy Markdown

Fixes #

What it changes

Cuts CPU work in shadow CPU-side diff/copy (under) and write-watch / stale-page lookups (ref):

  • Fused single-pass AVX2 word_runs_and_update (crates/victory-gpu/src/shadow.rs): when updating card-backed shadow copies in Shadow::take, compares title against held and writes updated 32-byte vectors to held in one AVX2 pass instead of running word_runs and then re-scanning with copy_from_slice. Falls back to dword_runs + copy_from_slice on unaligned or non-AVX2 paths.
  • Linear O(N) stale_pages_in (crates/victory-gpu/src/shadow.rs): advances a single linear cursor over the sorted written slice after one initial partition_point instead of binary-searching written for every page in lo..hi.
  • Sorted Journal::ahead + ahead_contains binary search (crates/victory-gpu/src/watch.rs): keeps Journal::ahead sorted and non-touching across ask_ahead and ask_rest, uses ahead_contains (linear for <= 8 entries, partition_point above), and in ask_rest tries merged kernel_written first with a per-piece fallback so a touching unwatched or separate reservation never prevents caching its watched neighbour.
  • Tests: expanded word_runs_and_update_matches_word_runs_and_copies_exactly (aligned random, unaligned/odd fallback, all-differ, none-differ), expanded pages_never_taken_are_stale_written_or_not (cursor skips below lo, gaps, and past hi), and added ask_rest_falls_back_per_piece_when_touching_unwatched_neighbour and ahead_contains_matches_linear_over_binary_threshold.

How it was tested

  • GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): NVIDIA RTX 5090 32 GB (driver 610.88), AMD Ryzen 9 9950X3D (16C/32T, 96 MB 3D V-Cache), 62 GB RAM, Windows 11; Series X profile, 1440p SDR, DLSS Quality, Frame Generation OFF, RT default. Identical release builds (RUSTFLAGS="-C target-cpu=znver5") on clean origin/main (d4b5d51), 90 s same-session A/B runs (aligned steady window DLSS frames 100..1800, 0 D3D12 errors).
  • Before (d4b5d51): median 26.80 ms (37.3 fps), p95 36.30 ms, Prep 26.05 ms (ref=7.50 ms, under=2.05 ms), GPU busy 63%, 1,839 DLSS frames in 90 s.
  • After (pr/shadow-watch-fused, b366bb9): median 25.40 ms (39.4 fps, +2.1 fps / +5.6%), p95 32.55 ms (-3.75 ms), Prep 24.40 ms (-1.65 ms) (ref=6.95 ms (-7.3%), under=1.45 ms (-29.3%)), GPU busy 67%, 1,908 DLSS frames (+69) in 90 s.
  • For shader changes, the wave sizes tested (32 and/or 64 lanes): N/A (no shader changes).
  • cargo test passed for the crates it touches: yes (victory-gpu: 553 passed, 0 failed — 514 non-watch + 39 watch::tests serial; clippy + --features diagnostics check clean with 0 new warnings).

Checked for overlap

@github-actions github-actions Bot added NVIDIA pr-performance Pull request: makes it faster or use less memory rendering Missing, black, flickering or corrupted graphics labels Nov 28, 2026
@github-actions github-actions Bot changed the title [pr-performance][rendering][NVIDIA] Shadows & watch: fused AVX2 word_runs_and_update, linear stale_pages_in, and sorted Journal::ahead Shadows & watch: fused AVX2 word_runs_and_update, linear stale_pages_in, and sorted Journal::ahead Nov 28, 2026
@POMXARK

POMXARK commented Nov 28, 2026

Copy link
Copy Markdown

@Bazovsky — ran a 20-minute gameplay A/B of the shadow.rs portion (fused AVX2 word_runs_and_update + linear O(N) stale_pages_in, since our stack already carries ahead_normalize/ahead_holds from #1094) on Windows 11 (RTX 5080 16 GB, Ryzen 7 5700X 8C/16T, 32 GB RAM, Series X 1440p, DLSS Quality + MFG 4x, release build, matched switches):

Verification

  • Unit tests: word_runs_and_update_matches_word_runs_and_copies_exactly + dword_runs + word_runs 3/3 PASS (0.00s).
  • Stability & Visuals: 0 errors over 20 minutes, refused=2, clean visuals across all 16 captured screenshots.

20-minute A/B (with draw-bucket & copy-throughput normalization)

Bucket Baseline (R27) PR #1637 (shadow.rs) Throughput delta (MB/ms)
4k–7k draws (n=50 vs n=51) 13.00 FPS (77.2 ms, cop 30.1 ms / 58.2 MB = 1.93 MB/ms) 13.70 FPS (73.0 ms, cop 26.6 ms / 53.6 MB = 2.02 MB/ms) +4.2% MB/ms (+5.4% FPS)
7k–10k draws (n=12 vs n=31) 10.95 FPS (cop 33.5 ms / 68.8 MB = 2.05 MB/ms) 9.70 FPS (cop 40.6 ms / 85.9 MB = 2.12 MB/ms) +3.0% MB/ms
under (putting title's bytes under flights) 3.4 ms 3.3 ms -0.1 ms
Peak Shared / Commit 10,525 / 32,351 MB 9,622 / 31,456 MB -903 / -895 MB

(Note: overall unweighted ALL 3D p50 differed (15.30 vs 13.55 FPS) because the 20m route spent more samples in the heavier 7k–10k bucket (n=31 vs n=12); within the matched 4k–7k bucket and in normalized copy throughput (MB/ms), fused AVX2 word_runs_and_update + linear stale_pages_in improved copy efficiency by +3.0% to +4.2% with 0 errors and lower commit!)

@unueberlegt

Copy link
Copy Markdown

Tested this part (sorted, disjoint Journal::ahead + partition_point containment) standalone on our side while working #1521: implemented the same shape (sort + join touching ranges at both writers, binary search above 8 entries, linear below) and measured it in isolation on a machine where ahead holds only dozens of ranges: RTX 5070 12 GB, Ryzen 9 9950X3D (16C/32T), Series X 2560x1440, prologue, steady windows, medians. The refresh/journal numbers moved within scene noise there (7.0 vs 7.2 ms refreshes/frame against baseline) — expected: the linear scan over dozens of in-cache ranges was already cheap at this core count, so the win case remains the issue reporter's 8-core CPU-bound setup, where the per-span scan was cache-cold. The design matches ours, for what a second implementation is worth: the join has to include touching (not only overlapping) ranges, or a probe across two former neighbours answers falsely negative — ours joined them and the containment stayed exact.

GPU and profile: NVIDIA RTX 5070 12 GB, AMD Ryzen 9 9950X3D, 31 GB RAM, Windows 11 26300; Series X profile, 2560x1440, release builds, steady-window medians.

@Bazovsky
Bazovsky force-pushed the pr/shadow-watch-fused branch from b366bb9 to 03d5f57 Compare November 29, 2026 01:43
@Bazovsky

Copy link
Copy Markdown
Author

Thanks @POMXARK for the 20-minute shadow.rs A/B on Ryzen 7 5700X + RTX 5080 (+3.0% to +4.2% copy throughput MB/ms, +5.4% FPS in matched 4k–7k draws, 0 errors), and thanks @unueberlegt for independently confirming the sorted/disjoint Journal::ahead + touching-range join invariant while working #1521 — great to have two independent implementations converge on the exact same semantics.

Rebased onto origin/main (d585f88 -> 03d5f57), preserving #1062's owned checks in ask_rest/asked/writes/nothing_new and 0174844's Copied lock changes. Verified with clippy (default + --features diagnostics, 0 new warnings) and native Windows victory-gpu tests (592/592 pass).

@POMXARK

POMXARK commented Nov 29, 2026

Copy link
Copy Markdown

@Bazovsky — quick follow-up data point on watch.rs (GetWriteWatch) + xmem.rs from our synthetic repro + 20-minute 3D A/B on Windows 11 (Ryzen 7 5700X 8C/16T + RTX 5080 16 GB, 1440p DLSS Quality + 4x FG, 0 errors):

  1. Why AHEAD_GAP = 8 MiB hurts CPU recording threads on Windows 11:
    In a real Win32 VirtualAlloc(MEM_WRITE_WATCH) microbenchmark (174 MiB heap, 160 clustered 256 KiB spans, 4,200 dirty 4 KiB pages/f), GetWriteWatch(WRITE_WATCH_FLAG_RESET) with AHEAD_GAP = 8 MiB resets untouched gap pages (4,200 vs 987), causing next-frame CPU writes to re-trap into the Windows kernel (~560 ns/page). Shrinking AHEAD_GAP to 512 KiB cut combined GetWriteWatch + next-frame CPU write time by -32% (2.85 -> 1.94 ms/f).
  2. Skipping MEM_WRITE_WATCH on strictly CPU-only XMEM_CPU allocations (flags & 2 == 0 and no PAGE_GRAPHICS_* bits):
    On a 128 MiB CPU-only buffer (8,000 scattered 4 KiB page writes/f), omitting MEM_WRITE_WATCH sped up CPU writes by 76x (4.478 -> 0.059 ms/f).
  3. 20-minute 3D A/B (AHEAD_GAP=512 KiB + unwatched XMEM_CPU, matched heavy open-world trajectory, 0 errors):
    • GetWriteWatch (ww): 6.7 ms (p90 16.9 ms) → 5.6 ms (p90 10.1 ms) (-16.4% med / -40.2% p90)
    • Submitting prep (SUB): 67.1 → 60.5 ms (-6.6 ms)
    • ALL 3D median: 13.60 → 14.15 FPS (+4.0%), 7k–10k: 10.20 → 10.60 FPS (+3.9%), >=10k: 8.80 → 8.90 FPS
    • Also verified in isolation that Windows 11 serializes GetWriteWatch across threads (1T=0.64 ms, 4T=0.61 ms, 8T=0.66 ms), so shrinking the reset footprint beats parallelizing queries.

Clean-room writeup & standalone repro suite: https://github.com/POMXARK/bcpack-gpu-hlsl/blob/main/docs/PERF_REPRO_AND_TUNING.md

edwardteach247 pushed a commit to edwardteach247/VIctory-NVIDIA that referenced this pull request Nov 29, 2026
The pass over the textures picked by index walked a placed view's pages
(VirtualQuery, whole textures at a time) before it asked whether the
card's fullness holds the slot: held ones threw the answer away. The
held decision is made first now, and held ones are counted where they
skip the walk (HEAP_TEXTURES_HELD_SKIP, in the 5 s line).

What changes besides the order: a held slot that streamed out is told
Held without the walk (it was told Away), and its host copy is not let
go until a pass with room walks it again — about four seconds, or as
soon as the card has room.

Point 4 of trustno1-coder#1521; the issue's other three are covered by 0174844 (the
shadow copy state lock, merged) and the open trustno1-coder#614, trustno1-coder#1133, trustno1-coder#1556, trustno1-coder#1637.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@Bazovsky

Copy link
Copy Markdown
Author

Thanks @POMXARK for the GetWriteWatch / AHEAD_GAP (8 MiB at watch.rs:473) and unwatched XMEM_CPU measurements (ww 6.7 -> 5.6 ms med / -40.2% p90, ALL 3D +4.0%, and the Win11 GetWriteWatch serialization finding) plus the clean-room repro!
I'll keep #1637 focused on shadow.rs (fused AVX2 word_runs_and_update + linear stale_pages_in) and sorted Journal::ahead so the diff stays easy to review; tuning AHEAD_GAP and skipping MEM_WRITE_WATCH on CPU-only XMEM_CPU allocations look like a strong, well-scoped separate follow-up PR.

@amazedturnip

Copy link
Copy Markdown

Tested on 9800X3D (AVX2) + RTX 5070 Ti 16 GB in a combined perf stack across 20+ min sessions. Zero shadow/watch regressions; combined stack cut write-watch time from ~4.5 ms to ~1.9-2.3 ms/frame.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

NVIDIA pr-performance Pull request: makes it faster or use less memory rendering Missing, black, flickering or corrupted graphics

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants