gpu: opt-in lazy GPU write-back (VEH), O(dirty) CopiedPages, AVX2 shadow & AdapterKey PSO (vitutu1#20 handover) #51

Open
github-bot wants to merge 3 commits from github-bot/gh-51 into beta AGit
Owner

@⁠Vladimir-Human on GitHub · 2026-11-25 21:40 UTC · original
Vladimir-Human:perf/lazy-copyback-rebar-pso → beta · +6734 −511 · 4 files · 3 commits · open

Summary

AI-assisted handover and major reliability/performance upgrade of vitutu1/victory-perf PR #20 (since that repository is now unavailable), bringing four opt-in / zero-regression subsystems onto PastiiJay/klbf-lab beta:

  1. Opt-in Lazy GPU-Resident Write-Back (VICTORY_LAZY_COPYBACK=1, default OFF):
    • Avoids copying 42–100+ MB/frame of shader-written buffers back over PCIe every frame when title CPU code never reads those pages.
    • Tracks clean eligible whole-page-aligned regions with PAGE_NOACCESS and an acyclic VEH fault handler (lazy_on_fault, never acquiring Level 0 watch/Shadow/BUFFERS/CARD_WORK/host.submitted locks); flushes only actually accessed 64 KiB pages on demand, using atomic dual-view MapViewOfFile writes before restoring PAGE_READWRITE for section-backed MEM_MAPPED console heaps (zero torn-read window).
    • Preserves coherence across DirectStorage title_memory_written (lazy_release_written_range with partial-page complement copy and pending-write FlushedBaseline preservation), VICTORY_RELAY / late_exact (Shadow::writing pending carve-out, stale_under_writes/merge_under_writes settle on LAZY_RESYNC, Step::GpuWrite settle in stale_under_flights), host::{copy_mapped, read_into, write_mapped} lazy_flush_and_hold range barrier, multi-flight FlushedBaseline subrange consumption, overlapping Shadow::exact / nested views (lazy_flush_other_shadows), shadow::unregister, and watch::Remapping (LazyRemapGuard).
    • Completely inert (0 overhead, unchanged paths) unless VICTORY_LAZY_COPYBACK=1 is set.
  2. O(dirty) CopiedPages & AVX2 Shadow/Merge Acceleration:
    • Replaces linear full-shadow scans with a 64-page uncopied_bits bitmask and lock-released plan_stale / plan_stale_filtered, plus AVX2 first_difference / last_difference, changed_runs, merge_piece, merge_sync_card, merge_gpu_into_title_runs, and merge_back_piece (preserving title-wins conflict handling).
  3. Opt-in ReBAR Upload Path (VICTORY_REBAR=1|2, default OFF):
    • Explicit D3D12_HEAP_TYPE_GPU_UPLOAD path with write-combining fences (_mm_sfence() after WC stores; _mm_mfence() + VMOVNTDQA + RAII MfenceOnExit for reads).
  4. Adapter-Keyed PSO Library Isolation (AdapterKey):
    • Keys pipeline/library.rs cache filenames by (vendor, device, revision, software) ({:04x}-{:04x}-r{:02x}-{sw|hw}) with strict Release/Acquire publish ordering (VIEW[1] / LOADED_AT / TRIMMED before VIEW[0]). Subsystem ID and driver version are deliberately omitted from the filename key so driver updates reopen the same file and fall back cleanly to an empty library if D3D12 rejects old blobs.

Verification (6b174ce6f, monotonic hardening on top of 7d31a81e)

  • Scope: only 4 files touched (host.rs, pipeline/library.rs, shadow.rs, watch.rs; shadow.rs = 410,902 B < 512 KiB), xtask policy --staged exit 0, clean merge-tree vs beta (78ad571).
  • cargo clippy -p victory-gpu --all-targets --locked (+ --all-features): 0 warnings in all modified/added code.
  • VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --locked --all-features -- --test-threads=1: 185 passed; 0 failed; 5 ignored (0 D3D12 debug errors; includes all F1–F5 + 5 relay/VEH concurrency regression tests).
  • VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --locked -- --test-threads=1: 177 passed; 0 failed; 5 ignored (0 D3D12 debug errors).
  • --release RTX 4070 SUPER / i5-13400F benchmarks (7d31a81e):
    • bench_lazy_copyback (64 MiB carded VRAM, 30 frames): 0% CPU reads 37.299 -> 0.585 ms/frame (63.78x, 0.00 MB/f readback/upload, 0.0 VEH faults/f); 2% sparse CPU reads 37.454 -> 9.001 ms/frame (4.16x, 1.25 MB/f, 20.0 faults/f).
    • bench_shadow_and_rebar: plan_stale 11.4 ns/call, 64 MiB AVX2 first+last 0.38 ms (175.5 GB/s), ReBAR GPU_UPLOAD 32 MiB write+SFENCE 3.10 ms (10.8 GB/s).

(Completes 10/10 open vitutu1/victory-perf PR handovers onto PastiiJay/klbf-lab beta: #15, #16, #29, #41, #42, #43, #44, #46, #50, and this PR.)

Co-Authored-By: AI Assistant

**@⁠Vladimir-Human** on GitHub · 2026-11-25 21:40 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/51) `Vladimir-Human:perf/lazy-copyback-rebar-pso` → `beta` · +6734 −511 · 4 files · 3 commits · open ## Summary AI-assisted handover and major reliability/performance upgrade of `vitutu1/victory-perf` PR #20 (since that repository is now unavailable), bringing four opt-in / zero-regression subsystems onto `PastiiJay/klbf-lab` `beta`: 1. **Opt-in Lazy GPU-Resident Write-Back (`VICTORY_LAZY_COPYBACK=1`, default OFF)**: - Avoids copying 42–100+ MB/frame of shader-written buffers back over PCIe every frame when title CPU code never reads those pages. - Tracks clean eligible whole-page-aligned regions with `PAGE_NOACCESS` and an acyclic VEH fault handler (`lazy_on_fault`, never acquiring Level 0 `watch`/`Shadow`/`BUFFERS`/`CARD_WORK`/`host.submitted` locks); flushes only actually accessed 64 KiB pages on demand, using atomic dual-view `MapViewOfFile` writes before restoring `PAGE_READWRITE` for section-backed `MEM_MAPPED` console heaps (zero torn-read window). - Preserves coherence across DirectStorage `title_memory_written` (`lazy_release_written_range` with partial-page complement copy and pending-write `FlushedBaseline` preservation), `VICTORY_RELAY` / `late_exact` (`Shadow::writing` pending carve-out, `stale_under_writes`/`merge_under_writes` settle on `LAZY_RESYNC`, `Step::GpuWrite` settle in `stale_under_flights`), `host::{copy_mapped, read_into, write_mapped}` `lazy_flush_and_hold` range barrier, multi-flight `FlushedBaseline` subrange consumption, overlapping `Shadow::exact` / nested views (`lazy_flush_other_shadows`), `shadow::unregister`, and `watch::Remapping` (`LazyRemapGuard`). - Completely inert (`0` overhead, unchanged paths) unless `VICTORY_LAZY_COPYBACK=1` is set. 2. **O(dirty) `CopiedPages` & AVX2 Shadow/Merge Acceleration**: - Replaces linear full-shadow scans with a 64-page `uncopied_bits` bitmask and lock-released `plan_stale` / `plan_stale_filtered`, plus AVX2 `first_difference` / `last_difference`, `changed_runs`, `merge_piece`, `merge_sync_card`, `merge_gpu_into_title_runs`, and `merge_back_piece` (preserving title-wins conflict handling). 3. **Opt-in ReBAR Upload Path (`VICTORY_REBAR=1|2`, default OFF)**: - Explicit `D3D12_HEAP_TYPE_GPU_UPLOAD` path with write-combining fences (`_mm_sfence()` after WC stores; `_mm_mfence()` + `VMOVNTDQA` + RAII `MfenceOnExit` for reads). 4. **Adapter-Keyed PSO Library Isolation (`AdapterKey`)**: - Keys `pipeline/library.rs` cache filenames by `(vendor, device, revision, software)` (`{:04x}-{:04x}-r{:02x}-{sw|hw}`) with strict `Release`/`Acquire` publish ordering (`VIEW[1]` / `LOADED_AT` / `TRIMMED` before `VIEW[0]`). Subsystem ID and driver version are deliberately omitted from the filename key so driver updates reopen the same file and fall back cleanly to an empty library if D3D12 rejects old blobs. ### Verification (`6b174ce6f`, monotonic hardening on top of `7d31a81e`) - Scope: only 4 files touched (`host.rs`, `pipeline/library.rs`, `shadow.rs`, `watch.rs`; `shadow.rs` = 410,902 B < 512 KiB), `xtask policy --staged` exit 0, clean `merge-tree` vs `beta` (`78ad571`). - `cargo clippy -p victory-gpu --all-targets --locked` (+ `--all-features`): **0 warnings** in all modified/added code. - `VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --locked --all-features -- --test-threads=1`: **185 passed; 0 failed; 5 ignored** (0 D3D12 debug errors; includes all F1–F5 + 5 relay/VEH concurrency regression tests). - `VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --locked -- --test-threads=1`: **177 passed; 0 failed; 5 ignored** (0 D3D12 debug errors). - `--release` RTX 4070 SUPER / i5-13400F benchmarks (`7d31a81e`): - `bench_lazy_copyback` (64 MiB carded VRAM, 30 frames): `0%` CPU reads `37.299 -> 0.585 ms/frame` (**63.78x**, `0.00 MB/f` readback/upload, `0.0` VEH faults/f); `2%` sparse CPU reads `37.454 -> 9.001 ms/frame` (**4.16x**, `1.25 MB/f`, `20.0` faults/f). - `bench_shadow_and_rebar`: `plan_stale` `11.4 ns/call`, 64 MiB AVX2 `first+last` `0.38 ms` (`175.5 GB/s`), ReBAR `GPU_UPLOAD` 32 MiB write+SFENCE `3.10 ms` (`10.8 GB/s`). *(Completes 10/10 open `vitutu1/victory-perf` PR handovers onto `PastiiJay/klbf-lab` `beta`: #15, #16, #29, #41, #42, #43, #44, #46, #50, and this PR.)* Co-Authored-By: AI Assistant
Add opt-in Lazy GPU-Resident Write-Back (`VICTORY_LAZY_COPYBACK=1`, default OFF)
plus O(dirty) CopiedPages, AVX2 VMOVNTDQA ReBAR/shadow helpers, and AdapterKey
PSO library isolation on top of beta (9c4f1e8):

- Lazy GPU-Resident Write-Back (`shadow.rs`, `watch.rs`, `host.rs`):
  - Track dirty GPU-resident 64 KiB Shadow pieces (`LAZY_DEFERRED`) across frames
    instead of eagerly reading back and re-uploading 42-100 MB over PCIe every frame.
  - Protect only eligible non-fence, non-pinned, whole-page-aligned, whole-page-
    initialized committed pages with `PAGE_NOACCESS` (never fence/sync polling
    ranges or partial boundaries); pin fence/defer_write ranges via `lazy_pin_range`.
  - Integrate a deadlock-free Windows VEH demand-fault handler in `watch.rs` with
    strictly acyclic 4-level lock ordering (Level 0 host/shadow/watch -> Level 1
    LAZY_TABLE -> Level 2 LAZY_GPU -> Level 3 FlushWait; VEH never acquires Level 0
    locks). On CPU read/write fault, flush only that single 64 KiB piece from GPU,
    using atomic dual-view MapViewOfFile writes for section-backed MEM_MAPPED pages
    and FlushWait serialization for concurrent faults, restore PAGE_READWRITE, and
    resync staging/VRAM hashes on next GPU use.
  - Flush or release protections cleanly on VirtualFree, VirtualProtect,
    title_memory_remapped/remapping, views_mapped, shadow::unregister, Shadow::drop,
    and kernel ReadProcessMemory/NtWriteVirtualMemory hooks.
- Shadow / ReBAR AVX2 & CopiedPages (`shadow.rs`):
  - O(dirty) `CopiedPages` with 64-page uncopied bitmask and lock-released `plan_stale`.
  - AVX2 `VMOVNTDQA` WC read/compare/copy-back bracketed by entry `_mm_mfence()` +
    RAII `MfenceOnExit` drop guard + `_mm_sfence()` after WC stores; self-contained
    opt-in ReBAR (`VICTORY_REBAR=1|2`, default OFF) helpers in `shadow.rs`.
  - AVX2 `merge_piece`, `merge_sync_card`, `merge_gpu_into_title_runs`,
    `merge_back_piece` (preserving upstream #2009 title-wins conflict resolution),
    `first_difference`/`last_difference`, `changed_runs`, lock-free `WideDone`,
    lock-free `has_pending`/`has_dirty`/`has_sent`, and `Shadow::differing` `cut`
    `i == 0` / `saturating_sub` underflow fix.
- AdapterKey PSO Library Isolation (`pipeline/library.rs`):
  - Isolate hardware vs software PSO caches by `{vendor}-{device}-r{rev}-{hw|sw}`
    via `GetAdapterLuid -> EnumAdapterByLuid`, preserving legacy hardware fallback
    without discarding hardware caches on WARP.
  - Order `LOADED_AT.store` strictly before `VIEW[0].store(..., Ordering::Release)`
    with matching `Acquire` loads in `trim()`, `removal_note()`, and `save()`.
- F1 (`watch.rs`, `shadow.rs`): replace post-write `lazy_flush_range` in
  `watch::title_memory_written` with unconditional `lazy_release_written_range`
  so DirectStorage physical-page writes are never clobbered by stale GPU bytes
  (restoring GPU bytes only into any unwritten partial-page complement).
- F2 (`shadow.rs`): settle in-flight flights (`return true`) in
  `Shadow::merge_under_writes` and `Shadow::stale_under_writes` whenever a page
  under pending GPU writes is `LAZY_RESYNC`, preventing prior-flight GPU writes
  from being misclassified as fresh CPU writes.
- F3 (`shadow.rs`): track unapplied byte subranges inside `FlushedBaseline` so
  `lazy_apply_flushed_baseline` is idempotent across consecutive `card_merge_relay`
  / `relay_merge` landings, and clean up `flushed_baselines` in `Shadow::drop`
  when `deferred_count > 0 || resync_count > 0`.
- F4 (`shadow.rs`): add `lazy_flush_other_shadows` at the start of
  `Shadow::update_free` and `Shadow::writing` so overlapping `Shadow::exact` /
  nested sub-buffers always observe deferred GPU writes (even under `ask_once`).
- F5 (`watch.rs`, `shadow.rs`): hold a scoped `LazyRemapGuard` interval barrier
  inside `watch::Remapping` from entry of `title_memory_remapping` until drop to
  block concurrent re-arming during unmap/remap, and pass `save_baseline = true`
  on `shadow::unregister` (keeping `false` only on `Drop::drop`).
- F6 (`shadow.rs`): remove `PAGE_READONLY` downgrade during `MEM_WRITE_WATCH`
  arming and refuse unwatched `MEM_PRIVATE` arming (declining Frame 1 virgin
  demand-zero pages to eager copy-back so Frame 2+ arms with `GetWriteWatch(0) == 0`);
  document zero-tear dual-view `MEM_MAPPED` vs `MEM_PRIVATE` ordering clearly.
- Tests (`shadow.rs`): add live RTX 4070 SUPER regression tests covering F1-F6.
Backport six lazy copy-back and ReBAR hardening fixes plus five
regression tests to victory-gpu:

- shadow::try_defer_piece: set restore_rw immediately after
  watch::transition_clean_guard_to_noaccess succeeds so a subsequent
  was_written_since rejection always rolls MEM_MAPPED pages back from
  PAGE_NOACCESS to PAGE_READWRITE.
- shadow::{writing, update, update_free} & host::stale_under_flights:
  carve out page-expanded self.pending subranges before resyncing
  overlapping shadows or LAZY_RESYNC pages so in-flight relay merges
  keep self.data and FlushedBaseline intact; add
  Shadow::has_lazy_resync_under_writes and wire Step::GpuWrite into
  host::stale_under_flights.
- shadow::merge_under_writes: re-check per-page lazy_state before and
  after host::read_into so mid-loop LAZY_RESYNC transitions settle
  in-flight flights instead of diffing flushed GPU bytes against stale
  staging or snapshot data.
- shadow::lazy_release_written_range: when full_cover and
  shadow.writes_pending() hold (including post-unprotect re-check),
  capture GPU bytes into FlushedBaseline without writing them to title
  memory so in-flight merges preserve fresh full-page writes.
- shadow::lazy_flush_and_hold & host::{copy_mapped, read_into,
  write_mapped}: hold a scoped non-arming LazyRemapGuard range barrier
  across host reads and writes to prevent concurrent relay re-arming
  (inert with zero locking when VICTORY_LAZY_COPYBACK is off).
- shadow::rebar::{reserve_bytes, reserve_with_card}: replace deprecated
  AtomicU64::fetch_update calls with compare_exchange_weak loops.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:53:42 +00:00
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-25 21:43 UTC · original

Thanks a lot @⁠Vladimir-Human, and again for carrying vitutu1/victory-perf #20 over with credit. What we keep: lazy GPU write-back (VICTORY_LAZY_COPYBACK=1) and the ReBAR upload path (VICTORY_REBAR) are both off by default; the O(dirty) CopiedPages with AVX2 merges and the adapter-keyed pipeline library are always on; and the victory-gpu suite passes on your side with the D3D12 debug layer, plus the rest of the workspace.

Since we test one change at a time on the RX 9070 XT, we'll run it in steps: first the PR with both switches off (that measures the always-on parts), then VICTORY_LAZY_COPYBACK=1 alone, then VICTORY_REBAR alone, each against our reference build for fps, picture and stability, and post each result here. If it's easy for you, splitting the always-on parts and the two opt-in switches into separate PRs would make it simpler to merge what works on its own; no obligation, we can also test it as it is.

**@⁠PastiiJay** on GitHub · 2026-11-25 21:43 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/51#issuecomment-7175457249) Thanks a lot @⁠Vladimir-Human, and again for carrying vitutu1/victory-perf #20 over with credit. What we keep: lazy GPU write-back (`VICTORY_LAZY_COPYBACK=1`) and the ReBAR upload path (`VICTORY_REBAR`) are both off by default; the O(dirty) `CopiedPages` with AVX2 merges and the adapter-keyed pipeline library are always on; and the `victory-gpu` suite passes on your side with the D3D12 debug layer, plus the rest of the workspace. Since we test one change at a time on the RX 9070 XT, we'll run it in steps: first the PR with both switches off (that measures the always-on parts), then `VICTORY_LAZY_COPYBACK=1` alone, then `VICTORY_REBAR` alone, each against our reference build for fps, picture and stability, and post each result here. If it's easy for you, splitting the always-on parts and the two opt-in switches into separate PRs would make it simpler to merge what works on its own; no obligation, we can also test it as it is.
Author
Owner

@⁠HandshakeHackin on GitHub · 2026-11-25 21:53 UTC · original

I verified the pipeline-cache part of 7d31a81 on hosted Windows. The adapter-filename and legacy-fallback tests passed. I then ran the WARP child directly and required it to save and reload its compute pipeline. It wrote host-pipelines-1414-008c-r00-sw.bin (1,874 bytes) in each of two temporary directories. Both the synthetic hardware-named file and the unsuffixed legacy fixture retained their original bytes. Windows test log

One detail for the PR summary and driver-update retests: the filename key here uses vendor ID, device ID, revision and the software/hardware flag. It omits the subsystem ID and driver version. A driver update can therefore reopen the same filename; D3D12 can reject the old contents and the code then starts an empty library. Adapter key · Rejected-cache handling

**@⁠HandshakeHackin** on GitHub · 2026-11-25 21:53 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/51#issuecomment-7175666740) I verified the pipeline-cache part of `7d31a81` on hosted Windows. The adapter-filename and legacy-fallback tests passed. I then ran the WARP child directly and required it to save and reload its compute pipeline. It wrote `host-pipelines-1414-008c-r00-sw.bin` (1,874 bytes) in each of two temporary directories. Both the synthetic hardware-named file and the unsuffixed legacy fixture retained their original bytes. [Windows test log](https://github.com/HandshakeHackin/klbf-lab/actions/runs/45707827754/job/139534222203) One detail for the PR summary and driver-update retests: the filename key here uses vendor ID, device ID, revision and the software/hardware flag. It omits the subsystem ID and driver version. A driver update can therefore reopen the same filename; D3D12 can reject the old contents and the code then starts an empty library. [Adapter key](https://github.com/PastiiJay/klbf-lab/blob/7d31a81ed4f35930b9a205ae620ea2942563ccec/crates/victory-gpu/src/pipeline/library.rs#L423-L467) · [Rejected-cache handling](https://github.com/PastiiJay/klbf-lab/blob/7d31a81ed4f35930b9a205ae620ea2942563ccec/crates/victory-gpu/src/pipeline/library.rs#L644-L672)
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-25 21:54 UTC · original

Thanks @⁠HandshakeHackin, confirmed on our side: the key in 7d31a81 is vendor, device and revision (plus the software flag), and the code comment says leaving out the driver version is deliberate, so old caches aren't orphaned on a driver update. So the code looks intentional and it's the PR summary that lists subsystem and driver too.

That matters for our testers, since the pipeline pre-build is long: after a driver update the same file is reopened, and if D3D12 rejects it the library starts empty. When we test #51 we'll include one run right after a driver update to see that case. @⁠Vladimir-Human, could you align the summary with the code when you have a moment?

**@⁠PastiiJay** on GitHub · 2026-11-25 21:54 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/51#issuecomment-7175695760) Thanks @⁠HandshakeHackin, confirmed on our side: the key in `7d31a81` is vendor, device and revision (plus the software flag), and the code comment says leaving out the driver version is deliberate, so old caches aren't orphaned on a driver update. So the code looks intentional and it's the PR summary that lists subsystem and driver too. That matters for our testers, since the pipeline pre-build is long: after a driver update the same file is reopened, and if D3D12 rejects it the library starts empty. When we test #51 we'll include one run right after a driver update to see that case. @⁠Vladimir-Human, could you align the summary with the code when you have a moment?
Author
Owner

@⁠Vladimir-Human on GitHub · 2026-11-25 23:13 UTC · original

@⁠PastiiJay @⁠HandshakeHackin Thank you both for the thorough check and feedback!

  1. PR summary aligned with code (AdapterKey): Updated item 4 in the PR description to state the exact filename key used in pipeline/library.rs: (vendor, device, revision, software) ({:04x}-{:04x}-r{:02x}-{sw|hw}), deliberately omitting subsystem ID and driver version so driver updates reopen the same cache file and fall back cleanly to an empty library if D3D12 rejects old blobs.
  2. Hardening update pushed (6b174ce6f9 on top of 7d31a81ed4): While auditing concurrency edge cases across ports, we caught and fixed 6 subtle VICTORY_LAZY_COPYBACK=1 / VICTORY_RELAY edge cases and added 5 targeted regression tests in crates/victory-gpu/src/{shadow.rs,host.rs}:
    • Set restore_rw = true immediately on transition_clean_guard_to_noaccess before checking was_written_since (prevents stranding PAGE_NOACCESS on concurrent MEM_MAPPED write races).
    • Skip page-expanded self.pending ranges during Shadow::writing / lazy_resync_range / lazy_prepare_update and include Step::GpuWrite in stale_under_flights when writes_pending() && has_lazy_resync_under_writes().
    • Re-check is_lazy_held / LAZY_RESYNC per-page before and after read_into inside Shadow::merge_under_writes.
    • Preserve FlushedBaseline (without overwriting title memory) on full-page title_memory_written when shadow.writes_pending() is true.
    • Hold scoped LazyRemapGuard barriers across host::{copy_mapped, read_into, write_mapped} to close concurrent relay re-arming TOCTOU.
    • Replace deprecated AtomicU64::fetch_update calls in rebar with compare_exchange_weak loops (0 warnings on newer rustc).
    • Re-verified on RTX 4070 SUPER (D3D12_DEBUG=1): 185/185 (--all-features), 177/177 (default features), 0 D3D12 debug errors, 0 clippy warnings in modified files, shadow.rs = 410,902 B (< 512 KiB), xtask policy --staged exit 0.
  3. Splitting: Happy to split CopiedPages+AVX2 / AdapterKey PSO / VICTORY_LAZY_COPYBACK=1 / VICTORY_REBAR into separate PRs if that helps after your queued RX 9070 XT checks — just let me know! Meanwhile both new paths (VICTORY_LAZY_COPYBACK, VICTORY_REBAR) stay off by default so baseline behavior is easy to A/B test.
**@⁠Vladimir-Human** on GitHub · 2026-11-25 23:13 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/51#issuecomment-7177283749) @⁠PastiiJay @⁠HandshakeHackin Thank you both for the thorough check and feedback! 1. **PR summary aligned with code (`AdapterKey`)**: Updated item 4 in the PR description to state the exact filename key used in `pipeline/library.rs`: `(vendor, device, revision, software)` (`{:04x}-{:04x}-r{:02x}-{sw|hw}`), deliberately omitting subsystem ID and driver version so driver updates reopen the same cache file and fall back cleanly to an empty library if D3D12 rejects old blobs. 2. **Hardening update pushed (`6b174ce6f9` on top of `7d31a81ed4`)**: While auditing concurrency edge cases across ports, we caught and fixed 6 subtle `VICTORY_LAZY_COPYBACK=1` / `VICTORY_RELAY` edge cases and added 5 targeted regression tests in `crates/victory-gpu/src/{shadow.rs,host.rs}`: - Set `restore_rw = true` immediately on `transition_clean_guard_to_noaccess` before checking `was_written_since` (prevents stranding `PAGE_NOACCESS` on concurrent `MEM_MAPPED` write races). - Skip page-expanded `self.pending` ranges during `Shadow::writing` / `lazy_resync_range` / `lazy_prepare_update` and include `Step::GpuWrite` in `stale_under_flights` when `writes_pending() && has_lazy_resync_under_writes()`. - Re-check `is_lazy_held` / `LAZY_RESYNC` per-page before and after `read_into` inside `Shadow::merge_under_writes`. - Preserve `FlushedBaseline` (without overwriting title memory) on full-page `title_memory_written` when `shadow.writes_pending()` is true. - Hold scoped `LazyRemapGuard` barriers across `host::{copy_mapped, read_into, write_mapped}` to close concurrent relay re-arming TOCTOU. - Replace deprecated `AtomicU64::fetch_update` calls in `rebar` with `compare_exchange_weak` loops (`0` warnings on newer `rustc`). - Re-verified on RTX 4070 SUPER (`D3D12_DEBUG=1`): **185/185** (`--all-features`), **177/177** (default features), `0` D3D12 debug errors, `0` clippy warnings in modified files, `shadow.rs` = 410,902 B (< 512 KiB), `xtask policy --staged` exit `0`. 3. **Splitting**: Happy to split `CopiedPages`+AVX2 / `AdapterKey` PSO / `VICTORY_LAZY_COPYBACK=1` / `VICTORY_REBAR` into separate PRs if that helps after your queued RX 9070 XT checks — just let me know! Meanwhile both new paths (`VICTORY_LAZY_COPYBACK`, `VICTORY_REBAR`) stay off by default so baseline behavior is easy to A/B test.
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-25 23:14 UTC · original

Thanks a lot @⁠Vladimir-Human, quick and thorough. What we keep: the summary now matches the cache key in the code, 6b174ce6f9 hardens six lazy-copyback / relay edge cases with five new regression tests, and both opt-in paths stay off by default.

On splitting: yes please, when it's convenient for you. Separate PRs for CopiedPages+AVX2, the adapter-keyed pipeline library, lazy copy-back and ReBAR would let us run each one on the RX 9070 XT on its own and merge what works without waiting for the rest. We'll post results in each PR.

**@⁠PastiiJay** on GitHub · 2026-11-25 23:14 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/51#issuecomment-7177312157) Thanks a lot @⁠Vladimir-Human, quick and thorough. What we keep: the summary now matches the cache key in the code, `6b174ce6f9` hardens six lazy-copyback / relay edge cases with five new regression tests, and both opt-in paths stay off by default. On splitting: yes please, when it's convenient for you. Separate PRs for `CopiedPages`+AVX2, the adapter-keyed pipeline library, lazy copy-back and ReBAR would let us run each one on the RX 9070 XT on its own and merge what works without waiting for the rest. We'll post results in each PR.
Author
Owner

@⁠Vladimir-Human on GitHub · 2026-11-25 23:43 UTC · original

Uncontested --release benchmark on idle i5-13400F / RTX 4070 SUPER after hardening commit 6b174ce6:

  • bench_lazy_copyback (64 MiB carded VRAM): 0% CPU reads 37.868 -> 0.566 ms/frame (66.88x, 0.00 MB/f readback/upload, 0.0 VEH faults/f); 2% sparse CPU reads 37.524 -> 8.622 ms/frame (4.35x, 1.25 MB/f, 20.0 faults/f).
  • bench_shadow_and_rebar: plan_stale (160 MiB, 4 dirty) 11.1 ns/call; 64 MiB AVX2 first+last 0.40 ms (169.6 GB/s), merge_piece 4.58 ms, changed_runs 5.26 ms; ReBAR GPU_UPLOAD 32 MiB write+SFENCE 3.09 ms (10.9 GB/s).
**@⁠Vladimir-Human** on GitHub · 2026-11-25 23:43 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/51#issuecomment-7177962865) Uncontested `--release` benchmark on idle `i5-13400F` / `RTX 4070 SUPER` after hardening commit `6b174ce6`: - `bench_lazy_copyback` (64 MiB carded VRAM): `0%` CPU reads `37.868 -> 0.566 ms/frame` (**66.88x**, `0.00 MB/f` readback/upload, `0.0` VEH faults/f); `2%` sparse CPU reads `37.524 -> 8.622 ms/frame` (**4.35x**, `1.25 MB/f`, `20.0` faults/f). - `bench_shadow_and_rebar`: `plan_stale` (160 MiB, 4 dirty) `11.1 ns/call`; 64 MiB AVX2 `first+last` `0.40 ms` (`169.6 GB/s`), `merge_piece` `4.58 ms`, `changed_runs` `5.26 ms`; ReBAR `GPU_UPLOAD` 32 MiB `write+SFENCE` `3.09 ms` (`10.9 GB/s`).
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-25 23:44 UTC · original

Thanks @⁠Vladimir-Human, good to have the micro-benchmarks: lazy copy-back going from ~38 to ~0.6 ms/frame when the CPU reads nothing is a big ceiling. The real test is in-game though, so we'll see how much of that survives on the RX 9070 XT once the split PRs land.

**@⁠PastiiJay** on GitHub · 2026-11-25 23:44 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/51#issuecomment-7177975320) Thanks @⁠Vladimir-Human, good to have the micro-benchmarks: lazy copy-back going from ~38 to ~0.6 ms/frame when the CPU reads nothing is a big ceiling. The real test is in-game though, so we'll see how much of that survives on the RX 9070 XT once the split PRs land.
Author
Owner

@⁠Vladimir-Human on GitHub · 2026-11-26 00:48 UTC · original

Split #51 into 4 clean, independent PRs on beta (each passes VICTORY_D3D12_DEBUG=1 tests + xtask policy --staged, with zero cross-dependencies so they can be merged or reverted individually):

  1. Part 1/4 — Adapter-keyed PSO pipeline cache: #56
  2. Part 2/4 — CopiedPages O(dirty) bitmask, lock-free has_pending/has_dirty/has_sent mirrors, AVX2 merge/diff & WideDone: #57
  3. Part 3/4 — Opt-in ReBAR (D3D12_HEAP_TYPE_GPU_UPLOAD) + VRAM headroom guard & WC streaming helpers: #58
  4. Part 4/4 — Opt-in VEH lazy GPU-to-CPU copy-back: #59

Leaving #51 open until you'd like to close it once the split queue lands.

**@⁠Vladimir-Human** on GitHub · 2026-11-26 00:48 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/51#issuecomment-7179128059) Split #51 into 4 clean, independent PRs on `beta` (each passes `VICTORY_D3D12_DEBUG=1` tests + `xtask policy --staged`, with zero cross-dependencies so they can be merged or reverted individually): 1. **Part 1/4 — Adapter-keyed PSO pipeline cache**: https://git.gtahub.st/github/PastiiJay--klbf-lab/pulls/56 2. **Part 2/4 — `CopiedPages` O(dirty) bitmask, lock-free `has_pending`/`has_dirty`/`has_sent` mirrors, AVX2 merge/diff & `WideDone`**: https://git.gtahub.st/github/PastiiJay--klbf-lab/pulls/57 3. **Part 3/4 — Opt-in ReBAR (`D3D12_HEAP_TYPE_GPU_UPLOAD`) + VRAM headroom guard & WC streaming helpers**: https://git.gtahub.st/github/PastiiJay--klbf-lab/pulls/58 4. **Part 4/4 — Opt-in VEH lazy GPU-to-CPU copy-back**: https://git.gtahub.st/github/PastiiJay--klbf-lab/pulls/59 Leaving #51 open until you'd like to close it once the split queue lands.
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-26 00:49 UTC · original

All four parts received (#56-#59), that's much easier to test. We'll go through them one at a time on the RX 9070 XT, starting with #56 and #57, and report in each PR. Closing #51 once they land is fine.

**@⁠PastiiJay** on GitHub · 2026-11-26 00:49 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/51#issuecomment-7179143403) All four parts received (#56-#59), that's much easier to test. We'll go through them one at a time on the RX 9070 XT, starting with #56 and #57, and report in each PR. Closing #51 once they land is fine.
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/51/head:github-bot/gh-51'
git switch 'github-bot/gh-51'

Merge

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