Add opt-in GPU-direct zero-CPU-copy 3-way sync engine (VICTORY_GPU_SYNC=1) #279
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/TaynanGT--Vice-Tay!279
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-279"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Vladimir-Human on GitHub · 2026-11-27 13:30 UTC · original
Vladimir-Human:perf/gpu-direct-3way-sync→main· +4288 −22 · 5 files · 1 commits · openSummary
Adds an opt-in (
VICTORY_GPU_SYNC=1, off by default) GPU-direct zero-CPU-copy bidirectional 3-way sync compute + DMA engine overOpenExistingHeapFromAddress1(crates/victory-gpu/src/gpu_sync.rs), targeting the CPU buffer-copy and readback-merge bottleneck measured in #212 (16.7 mscopy +10.0 mscopy-back =26.7 ms/frame).When
VICTORY_GPU_SYNCis unset or0, default behavior is completely unchanged.Architecture
shadow.rs::lookup) reserve local card VRAM (card_for) before attaching aSyncedShadow(OpenExistingHeapFromAddress1L0 write-back view of the title's 64 KiB pages + device-local L1before_bufferbaseline twin), avoidingtitle_shared_bufferCPU staging allocations and CPU copy-back scans (host.rs).probe_device): Probed once per device (ID3D12Device13,OpenExistingHeapFromAddress1,D3D12_FEATURE_ARCHITECTURE1::CacheCoherentUMA, L0 computeSRV, L0 computeUAV, L0InterlockedCompareExchangeatomics,L0<->L1CopyBufferRegion, andID3D12InfoQueuedebug validation), selecting cleanly between:SyncMode::BidirectionalUav: Full zero-CPU-copy bidirectional 3-way sync in compute (SYNC_BIDIR_HLSL, branchless SWAR byte-inequality masks, 4x coalesced 16-byte unrolling,InterlockedCompareExchangeL0 CAS for all partial and full-dword GPU writes, UAV barrier between disjoint ranges sharing a 32-bit dword boundary) + DMAL0->L1+L1->L1Init(record_init_dma).SyncMode::SrvDirectUpload: Adaptive fallback when a driver allows L0 compute SRV reads while rejecting L0 compute UAV writes — zero-CPU-stagingL0->L1upload and L1 3-way diff emitting 16-byte(w_off, gpu_word, gpu_mask, base_t)delta records with bounded overflow draining, applied safely viahost::read_into, per-bytecur == basetitle-wins check,watch::landing, andsafecopy/write_mapped, plus pre-refreshneeds_settle()at the top ofHost::submit.SyncMode::Unsupported: Automatic fallback to the existing CPU path, plus per-buffer eligibility fallback (check_eligibility) when unaligned, empty/>1 GiB, overlapping views (shown_runsover the full 64 KiB-rounded locked range), or uncommitted/non-RW (private_whole).T != B -> S, B;T == B && S != B -> T, B).SyncedShadow::initialized_pagesensuresShadow::writingwithout priorrefreshandmerge_under_writeson fresh shadows auto-Initin the pre-list before title command lists run.placed_copybaseline staging:stage_placed_snapshotsuploads exactbatch.placedbyte snapshots into a transientUPLOADbuffer and copiesupload -> before_bufferwith explicit D3D12 state barriers without touchingshadow.bufferor re-reading live title RAM.inplace::title_memory_freeing->shadow::gpu_sync_let_godetaches any synced shadow whose 64 KiB-roundedOpenExistingHeapFromAddress1page lock overlaps the freed range.Live Hardware & WARP Capability Probe (
VICTORY_D3D12_DEBUG=1,0debug errors)device13opensrvuavcopy L0->L1copy L1->L0atomicsheap_flagstruetruetruetruetruetruetrue0x421BidirectionalUav0truetruetruetruetruetruetrue0x421BidirectionalUav0ID3D12QueryHeapGPU Timestamp Benchmarks (RTX 4070 SUPER,--release,VICTORY_D3D12_DEBUG=0, post-full-CAS)L0->L1 + L1->L1)228.4 us(18.37 GB/s)329.7 us(12.72 GB/s)670.7 us(6.25 GB/s)96.3 us3,983.3 us(1.05 GB/s)901.1 us(18.62 GB/s)1,231.9 us(13.62 GB/s)1,246.2 us(13.46 GB/s)110.9 us16,021.3 us(1.05 GB/s)3,793.9 us(17.69 GB/s)5,140.5 us(13.05 GB/s)5,673.0 us(11.83 GB/s)149.6 us73,099.8 us(0.92 GB/s)(CPU submit overhead remains
O(1)at ~30-150 usregardless of synced buffer size, with zero CPU staging memory copies.)Verification (
rustc 1.98.1,VICTORY_D3D12_DEBUG=1,--test-threads=1,0D3D12/GBV errors)cargo clippy -p victory-gpu --all-targets --locked -- -D warnings(default &--features diagnostics): 0 warningscargo test -p victory-gpu --locked gpu_sync::tests -- --test-threads=1(HW & WARP, default &--features diagnostics): 10/10 passed (coalescing, fallbacks, live HW probe + timestamps, HW + WARP 3-way merge oracle parity across unaligned/overlapping/concurrent-write cases & forced-overflowSrvDirectUpload, HW + WARP same-dword & full-dword atomic CAS, end-to-end zero-staging + TOCTOUplaced_copy+VirtualFree(MEM_DECOMMIT)detach, auto-Initon freshShadow::writing,SrvDirectUploadguarded RO/concurrent delta apply, and 64 KiB-roundedgpu_sync_let_go)VICTORY_D3D12_DEBUG=1,VICTORY_D3D12_GBV=1,ID3D12Debug1::SetEnableGPUBasedValidation(true)):gpu_sync::tests10/10 passed (0GBV errors)cargo test -p victory-gpu --locked inplace::tests -- --test-threads=1: 8/8 passedcargo test -p victory-gpu --locked shadow:: -- --test-threads=1: 59/59 passed (4 ignored)cargo xtask policy(314 files ok),cargo test -p xtask --locked(11/11 passed),git diff --checkclean.Introduces an opt-in compute/DMA synchronization engine (`VICTORY_GPU_SYNC=1`, off by default) over `OpenExistingHeapFromAddress1` that eliminates CPU staging buffers and CPU readback merges for eligible large local-VRAM title buffers: - Live device capability probe (`probe_device`, incl. `CacheCoherentUMA`) selecting between: - `BidirectionalUav`: L0 `OpenExistingHeapFromAddress1` supports compute SRV reads and UAV writes (plus L0 `InterlockedCompareExchange` atomics routed for all partial and full-dword GPU writes and DMA `L0->L1` + `L1->L1` `Init`), performing full byte-exact 3-way sync directly on the GPU. - `SrvDirectUpload`: adaptive fallback when a driver allows L0 compute SRV reads but rejects L0 compute UAV writes (zero-CPU-staging `L0->L1` upload, L1 3-way diff emitting 16-byte `(off, gpu_word, mask, base_t)` deltas applied safely via `host::read_into`, `watch::landing`, and `safecopy` preserving concurrent CPU writes, plus pre-refresh `needs_settle()`). - `Unsupported`: automatic fallback to the existing CPU path, plus per-buffer eligibility checks over the 64 KiB-rounded locked range (`shown_runs`, `private_whole`) and card-budget admission before opening an L0 heap. - Byte-exact 3-way conflict resolution (CPU/title write wins when both CPU and GPU modify the same byte), per-64 KiB-page initialization tracking so `Shadow::writing` / `merge_under_writes` on fresh shadows auto-`Init` before title lists execute, transient-upload `placed_copy` baseline staging (free of TOCTOU reads of live title RAM), and 64 KiB-rounded page-lock detach on title memory free (`inplace::title_memory_freeing` -> `shadow::gpu_sync_let_go`). - Verified with `VICTORY_D3D12_DEBUG=1` (0 debug errors) and real `VICTORY_D3D12_GBV=1` (`ID3D12Debug1::SetEnableGPUBasedValidation(true)`), HW (RTX 4070 SUPER) + WARP tests against a CPU 3-way merge oracle across unaligned, overlapping, and concurrent-write cases, plus `ID3D12QueryHeap` timestamp throughput benchmarks.@TaynanGT on GitHub · 2026-11-27 13:37 UTC · original
@Vladimir-Human The hardware probe and fallback are important for a GPU-direct path. I’ll check that simultaneous CPU and GPU changes preserve the expected bytes and that fallback leaves the original snapshot valid. The in-game test can then compare copy, copy-back and frame time with the option as the only change.
@Vladimir-Human on GitHub · 2026-11-27 13:52 UTC · original
Thanks @TaynanGT - here are the exact tests and fallback paths in
crates/victory-gpu/src/gpu_sync.rs(all 4 CI checks green on70a150e):hw_capability_probe_and_timestamp_benchmark(gpu_sync.rs:3041) exercisesprobe_devicelive (ID3D12Device13, L0SRV/UAV/CopyBufferRegion/atomics,CacheCoherentUMA,0debug errors) withID3D12QueryHeaptimestamps.titlewins):hw_three_way_merge_matches_cpu_oracle(gpu_sync.rs:3231) andwarp_three_way_merge_matches_cpu_oracle(gpu_sync.rs:3239) verify byte-exact parity againstcpu_three_way_merge_oracleacross unaligned, overlapping, and concurrent writes in bothBidirectionalUavand forced-overflowSrvDirectUpload, alongsidehw_and_warp_same_dword_concurrent_byte_writes_with_atomics(gpu_sync.rs:3532),srv_delta_apply_preserves_concurrent_cpu_writes_and_guards_readonly(gpu_sync.rs:3883), andend_to_end_shadow_zero_staging_and_detach_lifecycle(gpu_sync.rs:3543, including TOCTOUplaced_copysnapshot preservation).fallback_when_unaligned_or_unopenable(gpu_sync.rs:3248),uninitialized_shadow_writing_auto_inits_before_bidir_sync(gpu_sync.rs:3779), andgpu_sync_let_go_detaches_on_64k_rounded_boundary_frees(gpu_sync.rs:3948) verify fallback rejection, fresh-shadow baseline auto-init, and 64 KiB page-lock detach; inshadow.rs::lookup, anyVICTORY_GPU_SYNC=0/unset, ineligible, or failedtry_attachrolls backCARD_BYTES[0]and falls straight through to the untouchedtitle_shared_buffer+ original snapshot path soVICTORY_GPU_SYNC=1stays a clean single-switch A/B comparison.@TaynanGT on GitHub · 2026-11-27 14:11 UTC · original
@Vladimir-Human The same-dword tests and the attach-failure rollback cover the two correctness questions. For the in-game A/B, keep
VICTORY_GPU_SYNCas the only change and include the selected mode and eligible bytes alongside copy, copy-back and frame time. That tells us which path actually ran.@CowboyBingus on GitHub · 2026-11-27 14:28 UTC · original
In-game A/B on an RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 64 GB, Windows 11, with
VICTORY_GPU_SYNCas the only change.Setup: one exe for both runs (md5
D4823FA78BB10CF499686A3CF6615DB0), a diag build (--profile diag -p victory --features diagnostics) of this PR's head70a150emerged with #206 (292adb5) and then withmainat877a40e.scripts\play.ps1 -Console series-s -Machine 4070 -Windowed -Dlss quality(DRED at the launcher default,faults; the logs showbreadcrumbs=false). Our deviations, the same in both runs:-Windowed,-Machine 4070, the reflection hold (VICTORY_AMBIENT_REFLECT=1,VICTORY_AMBIENT_REFLECT_IN=CS_TemporalResolve2_Opaque,CS_TemporalResolve1_Alpha), Enter presses (held 0.3 s) written to a keys file (VICTORY_KEYS_FILE) by a background job that begins no new round after 62 s on its clock, and a window capture after each 3 s wait while the game window was up. Same session: GS1 (VICTORY_GPU_SYNC=1, 450 s), then GO1 (switch unset), which we stopped after about 210 s, so it has fewer lines. No second round was run.Selected mode (GS1 log, the only
gpu_syncline in it):The log has no line with eligible bytes.
Frame-meter lines with > 1000 draws and 0 pipelines being made; medians (mean of the two middle lines for an even count, rounded half up); p10/p90 by nearest rank; with fewer than 100 lines per run, the 1% low is the lowest line.
VICTORY_GPU_SYNC=1)The GPU ms column is a CPU-side wall-clock timer (
std::time::Instantvalues summed inSUBMISSION_US[1],host.rs), not a measurement on the card. The on-screen overlay in a GS1 capture at 367 s shows "GPU EST 320.9 MS".@TaynanGT on GitHub · 2026-11-27 14:38 UTC · original
@CowboyBingus That’s a clear regression on this 5070 Ti run:
BidirectionalUavtook 192.9 ms per frame versus 40.9 ms in the control. I’ll keep it out of the recommended profile. Eligible bytes, time spent waiting for its fences, and GPU dispatch time are the useful next counters. The existing CPU timer can’t distinguish them.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.