Skip to content

victory-gpu: 64-thread CARD_COMPARE with per-plan VRAM staging and COPY queue readback - #688

Open
Claudinhomerq wants to merge 1 commit into
fxceless:mainfrom
Claudinhomerq:perf/card-compare-vram-stage-copy-queue
Open

Claudinhomerq wants to merge 1 commit into
fxceless:mainfrom
Claudinhomerq:perf/card-compare-vram-stage-copy-queue

Conversation

@Claudinhomerq

Copy link
Copy Markdown

Summary

Reduces GPU-to-CPU card-memory readback latency and eliminates DIRECT queue fetch stalls under VICTORY_RELAY=1:

  1. 64-thread workgroup CARD_COMPARE compute shader (crates/victory-gpu/src/host.rs)

    • Changes dispatch from 1 thread per 64 KiB piece to 1 workgroup ([numthreads(64, 1, 1)]) per piece using coalesced 16-byte loads and deterministic groupshared reduction.
  2. Per-plan exclusive VRAM staging + dedicated COPY queue readback (crates/victory-gpu/src/host.rs)

    • When VICTORY_RELAY=1 and inline staging is enabled (default; disable with VICTORY_CARD_INLINE_STAGE=0), differing pieces are written inline to a per-plan exclusive DEFAULT-heap StageBuf (65792 B stride, 256 slots/plan, explicit UAV -> COMMON barrier) and pulled over a dedicated D3D12_COMMAND_LIST_TYPE_COPY queue into a canonical D3D12_HEAP_TYPE_READBACK buffer (Host::readback_buffer).
    • Ring overflow sets CARD_DIFF_OVERFLOW_BIT and cleanly falls back to the existing DIRECT queue CopyBufferRegion fetch path.
  3. Byte-disjoint parallel relay merge (crates/victory-gpu/src/host.rs, crates/victory-gpu/src/shadow.rs)

    • Partitions relay merges into byte-disjoint chunks (partition_disjoint_merge_chunks) for parallel wide_run merging while keeping overlapping ranges ordered by submission sequence and preserving shadow.walls_merged(...).
    • Changes Shadow::before from Mutex to RwLock so parallel readers do not serialize.
  4. Tests (crates/victory-gpu/src/indirect.rs, crates/victory-gpu/src/host.rs)

    • Updates existing WARP CARD_COMPARE test for the 8-constant / 4-UAV root signature.
    • Adds warp_card_compare_inline_stages_and_handles_ring_overflow (verifying skewed 65,552 B pieces, ring overflow, tail reclamation, and COPY queue -> READBACK transfer on WARP) and relay_tests::disjoint_merge_chunks_keep_overlaps_together.

Measured Impact (RTX 5070 Ti, VICTORY_RELAY=1, 395s sustained run, 0 crashes/TDRs)

  • Frame time (world scene): 286-324 ms (3.1-3.5 FPS) -> 221-237 ms (4.2-4.5 FPS)
  • fetches: 148 ms -> 41 ms (-67%)
  • 128 lists in the air stalls: 76 ms -> 0.0 ms (-100%)
  • copying back: -52%; on the GPU: -65%

Verification

  • cargo xtask policy — asset policy: 169 files ok
  • cargo test -p victory-gpu --lib -- warp_card_compare relay_tests card_tests — 13 passed; 0 failed

@iGoldenNinja

iGoldenNinja commented Nov 25, 2026 •

Copy link
Copy Markdown

Independent RTX 5080 comparison. Windows 11 Pro 26200, driver 617.14, Ryzen 9 5900X, 32 GB RAM, Series X profile, SATA SSD and DRED. I compared 310c6eb with PR head 7e7bb18, VICTORY_RELAY=1 and VICTORY_CARD_INLINE_STAGE=1. Both runs covered the same prison room with the same timed camera and movement sequence from the first camera input. Both logged zero shader-cache hits during startup.

Five-minute capture Base PR head
Presented FPS 4.209 4.579
P99 417.475 ms 393.971 ms
Worst interval 523.282 ms 876.101 ms
Median reported card-copy fetch wait 143.30 ms 34.10 ms
Median reported copy-back 175.25 ms 65.35 ms

The PR run had visible missing wall and ceiling surfaces at a similar interior angle after retreating. The base capture showed those surfaces. This comparison is the full PR versus its base. It does not isolate staging from the other changes. The title responded to input and ended deliberately after the capture. DRED did not report device removal.

All 13 selected relay/card/compare tests passed, including a rerun that required WARP initialization in both card-compare tests. Method, GPU busy and sample counts. Testing and reporting assisted by OpenAI Codex.

Same-binary follow-up on 7e7bb18, with VICTORY_RELAY=1 in both runs and the inline staging flag changed from 1 to 0. Same profile, room and replay timing from the first camera input. The staging-on run logged zero shader-cache hits during startup. The staging-off run hit that cache. One five-minute run per setting.

Measurement Staging on Staging off
Presented FPS 4.579 4.522
P99 display interval 393.971 ms 441.020 ms
Worst display interval 876.101 ms 1,034.859 ms
Median card-copy fetch wait 34.10 ms 75.25 ms
Median copy-back 65.35 ms 110.90 ms

The same wall and ceiling gaps appeared in both modes at the post-retreat interior angle. Both runs logged the same 0x80070057 on the reported mesh/pixel PSO pair. The staging-off run responded to input and ended deliberately without a DRED device-removal report. Paired results. AI-assisted with OpenAI Codex.

@larelgit

Copy link
Copy Markdown

Another RTX 5080 series, on a different CPU from @iGoldenNinja's, with this PR's two settings and the base alternated in one sitting.

Setup. RTX 5080 16 GB (driver 610.88), Ryzen 7 7800X3D, 32 GB, Windows 11 Pro 22631, diag build, rustc 1.98.1. PR head 7e7bb18 as it is (it sits on 310c6eb; it also merges onto today's 8dd30ab without conflicts, not run there yet). The base binary is 310c6eb with #154's two commits and VICTORY_USER_COPIES=0, which copies through the kernel as main does. main's defaults otherwise, so relay stage 2 is on in every run. Each run loads the autosaved checkpoint at the cell window and nobody touches the pad. Frame time is the spacing of the recording per frame lines over 260-355 s of the run (median of 16 intervals); the other columns are means of the frame and relay lines over the same window. One run at a time, alternating; the first run of the PR binary (translating its shaders) is listed apart.

Build Runs ms/frame On the GPU Copying back Relay: waiting for fetches Relay: waiting for the card
base 2 163, 162 84-87 ms 125-127 ms 99-101 ms 15-27 ms
this PR 3 156, 152, 153 (first run 156) 72-76 ms 59-63 ms 29-33 ms 71-78 ms
this PR, VICTORY_CARD_INLINE_STAGE=0 2 156, 156 76-77 ms 108-109 ms 77-81 ms 24-34 ms

So here staging takes the fetch wait from about 100 ms to about 30 and halves copying back, as in the PR's and @iGoldenNinja's numbers, but the frame gains only 7-10 ms of 162-163 (about 5%). The relay thread now spends most of its time waiting for the card with nothing to land (71-78 ms), and the card is 73-75% busy against 78% on the base, so on this machine the frame is held by the card and the submitting thread rather than by the fetches. With staging off, the other parts of the PR (wide compare, parallel merge) are worth about 6-7 ms on their own.

Picture. Screenshots at 300 and 350 s, checking the chain-link mesh in the window, the print on the back of the shirt and the cell wall left of the window:

  • This PR with staging: mesh and print there in all 3 runs after the first.
  • Staging off: both there in both runs. In one run, at 350 s, a dark stretched triangle crossed the character's back.
  • Base: one run had a dark block in the window at 300 s (a dark wedge at 350 s), and the other lost the mesh at 300 and 350 s.
  • First runs: the first run of any new binary here draws differently while its shaders are translated (the hair shaded bright, the print faint in this PR's first run), so I leave first runs out of the picture checks.

On the wall and ceiling gaps: the cell wall left of the window is missing in most runs of every build I have run in this scene since main moved to 310c6eb, the base included. Over 26 runs it was drawn in 7: in 2 of the 9 runs of the base build with VICTORY_USER_COPIES=0, in 1 of the 4 runs of this PR with staging (the last one) and in neither run with staging off. That is too few to separate the builds, but it means one base run that shows the walls does not show that the base always does. The angle after the retreat in @iGoldenNinja's replay is not covered here, since nobody moves in my runs.

On the hazard raised on #71 (a fetch beside later flights): as I read relay_card_back here, the copy queue reads only each completed plan's own staging, which its compare filled on the direct queue in flight order and which nothing on the direct queue touches again until card_done. Pieces that overflow the ring take the direct-queue fetch as on main. So a later flight cannot change what the copy queue fetches, and this PR does not carry that hazard.

(Runs, measurements and this note by Claude Code with Claude Opus 5.5 on the hardware above, at the account owner's request.)

triippywhale pushed a commit to triippywhale/victory-fork that referenced this pull request Nov 25, 2026
The 9e6137f run spent 54 ms a frame copying back on the thread that
lands flights, merging the pushed runs one after another. The runs of a
landing are now noted first and merged once every plan's are, before
the slots are given back: with VICTORY_WIDE_MERGE=1 each copy's runs go
to one thread in their order (a copy's merges take its own lock anyway),
the copies spread over up to eight jobs by their bytes. Without the
switch the runs merge in the same order as before.

The push line now says how many copies the runs were of and how long the
merges took, so a run shows whether the copy-back is spread over enough
copies to gain from the threads.

This takes the parallel-merge part of fxceless fxceless#665 and fxceless#688 for the
push path. Their copy-queue fetch and VRAM staging do not apply with
VICTORY_CARD_PUSH on, where nothing is fetched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017V7WBB9Mgcxkr8YRW4LAzi
@Pin0d1

Pin0d1 commented Nov 25, 2026 •

Copy link
Copy Markdown

@Claudinhomerq, I checked the per-plan staging boundary on 7e7bb18. Two tracked flights write 17 and then 29 to the same 64 KiB pieces. Both finish on DIRECT before the relay lands the first one. With 256 changed pieces, the first landing returns 256 producer words. With 257, the flags show 256 staged and one overflow. That first landing returns 256 words of 17 and one of 29. The second landing returns 29 for all 257.

This repeated twice each on WARP and an RTX 2060 SUPER with staging enabled. Only the overflow piece took the live DIRECT fallback. Is that piece intended to retain the producing flight's value at its first landing? Exact test and transcripts.

Prepared with the help of Codex 6.2 Astra Max.

…M via COPY queue + READBACK, and parallelize relay merge

- Parallelize CARD_COMPARE from 1 thread per 64 KiB piece (4,096 serial loads) to one 64-thread group per piece (64 coalesced loads + deterministic groupshared tree reduction).
- Under VICTORY_RELAY (when VICTORY_INFLIGHT_FETCH is off), inline-stage differing pieces inside CARD_COMPARE into an exclusive per-plan DEFAULT-heap VRAM StageBuf (256-byte-aligned 65,792 B stride) with an explicit UAV -> COMMON barrier at the end of check, avoiding any simultaneous DIRECT-write / COPY-read hazard on the same ID3D12Resource (escape hatch: VICTORY_CARD_INLINE_STAGE=0).
- Pull staged pieces on a dedicated D3D12_COMMAND_LIST_TYPE_COPY queue into a canonical D3D12_HEAP_TYPE_READBACK buffer so relay_card_back no longer stalls behind newer graphics flights queued on the DIRECT queue.
- Enable VICTORY_WIDE_MERGE parallel merging in relay_card_back across byte-disjoint chunks (keeping overlapping sub-ranges in chronological order) using an RwLock on Shadow::before.
- Extend WARP D3D12 unit tests to cover skewed 65,552 B pieces, ring overflow fallback, and COPY-queue readback into D3D12_HEAP_TYPE_READBACK.

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
@openbotgit
openbotgit Bot force-pushed the perf/card-compare-vram-stage-copy-queue branch from 7e7bb18 to 7b91f86 Compare November 25, 2026 18:20
@Claudinhomerq

Copy link
Copy Markdown
Author

Thanks for the thorough overflow test on both WARP and the 2060 SUPER, @Pin0d1!

Yes — that behavior on the 257th (overflow) piece is intentional and matches main's existing VICTORY_RELAY live-fetch contract:

  • Without inline staging, every flagged piece is fetched from the live VRAM buffer at landing time, so if Flight 2 is already queued/completed and overwrote a piece (17 -> 29), Flight 1's landing merges 29 (the latest GPU bytes) and Flight 2's landing becomes a no-op for that piece. Whenever the title needs to observe Flight 1's 17 on the CPU before Flight 2 runs, it synchronizes on Flight 1's fence/bottom-of-pipe signal before submitting Flight 2.
  • With per-plan StageBuf, pieces 1–256 happen to capture Flight 1's exact snapshot (17) inline during CARD_COMPARE, while piece 257 (ring overflow) falls back cleanly to that exact main live-fetch path using after from QUEUE_ORDER as its fetched_after watermark, while relay_fetched is kept conservative via fallback_fetched_mark so no earlier in-flight send notes are dropped prematurely.
  • If a larger inline capacity is useful in a follow-up, we can easily bump or expose CARD_STAGE_SLOTS, though in real GTA VI frames the differing-piece count per submission stays well under 256.

I also just rebased this branch cleanly onto main (d510ee4) so it coexists with VICTORY_INFLIGHT_FETCH, VICTORY_QUEUE2 (BESIDE/refetch), and cardmeter (inline staging automatically yields when VICTORY_INFLIGHT_FETCH is active). Verified on default + --features diagnostics builds, all 19 warp_card_compare / warp_card_fetch / relay_tests / card_tests, and cargo xtask policy.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants