Add opt-in enhanced-barrier optimization for placed copies and mid-list barriers #544
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/lobomfz--VIctory-xmem!544
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-544"
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?
@Ayphex on GitHub · 2026-12-05 02:35 UTC · original
Ayphex:pr/c6a-barriers→main· +2540 −88 · 11 files · 2 commits · openSummary
Adds an opt-in, pure-Rust enhanced-barrier optimization pass (zero Mesa/
vkd3dchanges) that eliminates unnecessary full-GPU drains, L2 flushes, wildcard UAV barriers, redundant mid-list barriers, and full-pipe barriers around inline placed buffer copies. Default-off (VICTORY_BARRIER_OPT=0) for exact upstream parity; opt-in viaVICTORY_BARRIER_OPT=1.Key Changes
war— execution-only WAR (crates/victory-gpu/src/waits.rs): Lowers pure WAR (READ -> WRITE/COMMON) transitions to execution-onlyD3D12_BARRIER_ACCESS_NO_ACCESS(0x8000_0000) barriers, eliminating the unnecessary L2 cache flush/writeback; pure RAR andREAD -> NO_ACCESSare skipped.narrow— exact sync/access narrowing (waits.rs): NarrowsSyncAfter/AccessAfterto the stages and accesses actually involved and eliminates wildcardResourceBarrier(UAV(NULL)).dirty— per-list producer tracking (waits.rs): A per-HostListDirtyTracker(dirty_write_stages/dirty_read_stages, conservatively initialized to ALL on every fresh/reset list) narrowsSyncBeforemid-list and elides provably redundant barriers without weakening cross-list safety.join— safeResourceBarriercoalescing (crates/victory-gpu/src/host/rec.rs): RestoresRec::barriers()batch coalescing with conflict flushes on same-resource transitions, adjacent identical UAV/global barrier deduplication, and Acquire/Release synchronization.place— placed-copy barrier narrowing (crates/victory-gpu/src/host/place.rs): Replaces the full-GPUALL -> ALL, COMMON -> COMMONbarriers inHostList::place_copyandHostList::place_sourcewithDirtyTracker-narrowed/elided pre-copy barriers (-> COPY), pureCOPY -> COPYmid-copy alias and post-source barriers, and buffer-scopedCOPY (COPY_DEST) -> buffer consumerspost-copy barriers (preservingDirtyTrackerRT/DS consumer slots and stopping redundantDIRTY_COPYre-dirtying).parts_in_onepreludes are left untouched.docs/SETTINGS.md): DocumentedVICTORY_BARRIER_OPT,VICTORY_BARRIER_OPT_MASK, andVICTORY_BARRIER_STATS.Settings / Env Vars
VICTORY_BARRIER_OPT=0|1(default0= exact upstream parity).VICTORY_BARRIER_OPT_MASK=war,narrow,dirty,join,place(default when enabled = all five).VICTORY_BARRIER_STATS=0|1(default0; per-frame[barrier-opt]telemetry).Benchmarks (RX 6800 XT 16 GB Full ReBAR, i9-12900K,
VICTORY_BENCH=nort, 3840x2160 FSR4 Quality, 60 s timed window)System & method: AMD Radeon RX 6800 XT (RDNA 2,
gfx1030, 16 GB GDDR6, ReBAR), Intel i9-12900K, 32 GB DDR4-3600, CachyOS Linux; 3840x2160 FSR 4.1.2 Quality (internal 2560x1440),VICTORY_BENCH=nort,VICTORY_BENCH_MINUTES=2(final 60 s in-world window),VICTORY_LOG=off,VICTORY_BARRIER_STATS=0.Transparent note: measurements were taken stacked on top of the VRAM-first residency branch (#522,
VICTORY_VRAM_FIRST=1 VICTORY_VRAM_CLASSES=all) comparingVICTORY_BARRIER_OPT=0vs1on the same binary; this PR is cleanly rebased ontoorigin/mainwith zero dependency on #522.C6a (
war,narrow,dirty,joinvsVICTORY_BARRIER_OPT=0,VICTORY_LOG=off)VICTORY_BARRIER_OPT=0(parity)war,narrow,join(dirty off)war,narrow,dirty,joinC6a.5 (
+placeincremental A/B on the same binary,VICTORY_LOG=off)war,narrow,dirty,joinwar,narrow,dirty,join,placeTelemetry (
VICTORY_BARRIER_STATS=1, per-frame averages)war,narrow,dirty,join):total=925.3 emitted=770.4 noop=110.2 war_exec=52.8 narrow_before=585.3 narrow_after=770.4 elided=44.7 uav_null_elim=22.8 rec=589.2->404.3(dedup=0.5) place_joined=105.9+place):total=992.3 emitted=820.0 noop=119.7 war_exec=56.4 narrow_before=635.9 narrow_after=820.0 elided=52.6 uav_null_elim=7.5 rec=664.8->474.0(dedup=0.7) place_joined=51.5 place_opt=pre:19.4(elide=12.3) mid:0.0 post_copy:28.8 post_src:3.0place_copy/place_source) full-GPU drains eliminated (63.5/f -> 0, cuttingplace_joinedfrom105.9to51.5/fresidualparts_in_onepreludes); 39% pre-copy elision (12.3/31.7); guest barrier elision increased44.7 -> 52.6/f;Reccoalescing reduced calls664.8 -> 474.0/f(-28.7%).Verification
./build.sh(release): 0 errors on the standaloneorigin/mainbase (the singleroom_latelynever-used warning is pre-existing inresource.rsonorigin/main, outside this PR's file set).waits::tests11/11 pass,host::rec::tests2/2 pass (under Wine).VICTORY_BARRIER_OPT=0.vkd3dpatches.@Kal0s-design on GitHub · 2026-12-05 03:18 UTC · original
Checked
040c8aeon native Windows (one NVIDIA desktop card plus WARP, didn't run the game).Default off looks fine. With VICTORY_BARRIER_OPT unset the only unconditional changes are ResourceBarrier becoming Rec::barriers in settle, clear_rtv and texture::alias (with join off that is flush plus ResourceBarrier, same as main), the Asked word growing to u64 with the low bits unchanged, the mark_dirty calls that return early, and stats counters behind VICTORY_BARRIER_STATS. place_copy and place_source with the flag off record the same wait_for_all calls in the same order. Both the card and WARP report EnhancedBarriersSupported, so I couldn't exercise the fallback on a device without enhanced barriers. Reading it, groups_asked goes back to the UAV(NULL) path and place.rs falls back to wait_for_all when list.precise() is None.
The problem is with the flag on, on native D3D12. Several barrier shapes the PR builds are rejected by the D3D12 runtime itself and not only by the debug layer. A list with one of them in it fails Close with E_INVALIDARG with the debug layer off too (checked on the card and on WARP), and HostList::close (host.rs:5078) turns that into fail("a host command list did not close"). So on Windows I'd expect VICTORY_BARRIER_OPT=1 to end the run on the first list that hits one of these. I'd guess vkd3d-proton doesn't check these rules, which would fit the clean Linux runs, but I haven't looked.
The first one is the place pre-copy barrier. plan_place_pre_copy asks for MB_UAV | MB_COPY_DEST | MB_RT_DS (waits.rs:1195), and encode_d3d12_access_before adds RENDER_TARGET | DEPTH_STENCIL_WRITE whenever draw stages are dirty (waits.rs:905). A fresh list starts all dirty, so the first place_copy or place_source of every list (place.rs:285 and 397) gets AccessBefore RT|UAV|DSW|COPY_DEST with SyncAfter COPY. The runtime says a global barrier with RT or DS access would need a layout transition (message 1331) and Close fails. The same happens later in a list whenever draws have read something since the last barrier, and also with dirty off in the mask. The copies only touch buffers, so MB_RT_DS shouldn't be there. Dropping it is enough for place_source, whose barrier is SyncBefore DRAW|COMPUTE|COPY with UAV|COPY_DEST and closes fine that way. place_copy also has the WAR half, which adds EXECUTE_INDIRECT to SyncBefore, and the runtime won't pair EXECUTE_INDIRECT with UAV or COPY_DEST access either. That stage needs its own execution-only barrier or has to stay out of the one with write access. The pre-copy barrier you get with compute and copy writes and no pending reads (SyncBefore COMPUTE|COPY, AccessBefore UAV|COPY_DEST) closed, ran and copied the right bytes.
The second one is narrow on the title's barriers (groups_asked, list.rs:2343). I swept plan_barrier with single-barrier calls, every buffer barrier from 18 sync values by 17 access values on each side that the runtime accepts as a buffer barrier, plus a few texture barriers into and out of UAV, against 7 tracker states. With the default mask that gives 1,845 distinct global barriers and the runtime rejects 734 of them. 710 have AccessBefore COMMON with a narrowed AccessAfter. A title barrier with AccessBefore COMMON is encoded as ACCESS_COMMON (waits.rs:893) while AccessAfter is narrowed to SRV, CBV, NO_ACCESS and so on (waits.rs:1096), and a global barrier has to keep AccessAfter COMMON when AccessBefore is COMMON. 22 come from SyncAfter ALL_SHADING or NON_PIXEL_SHADING with VERTEX_BUFFER access, because decode_sync splits the umbrella into compute, vertex and pixel (waits.rs:400) and the re-encoded COMPUTE_SHADING bit can't go with VERTEX_BUFFER. The last 2 are texture RT or DS-write going to UAV, which gives RT|DSW access in a global barrier as in the place case.
Merged calls look like the more likely trigger in practice, since Asked::of ORs every group of a title call into one word and the runtime checks each sync bit against the whole access mask. I only tried a few by hand. A pure WAR from EXECUTE_INDIRECT reads to compute UAV, in the same call as compute UAV to pixel SRV, comes out as SyncBefore COMPUTE|EXECUTE_INDIRECT with AccessBefore UAV, and Close fails. Compute UAV to indirect arguments together with pixel SRV to compute UAV comes out as SyncAfter COMPUTE|EXECUTE_INDIRECT with AccessAfter INDIRECT_ARGUMENT, and Close fails. Both come from folding the WAR stages into the memory barrier (waits.rs:1084 and 1086) while the access masks only describe the memory part. Two other merged pairs closed fine. I checked one representative of each rejected kind without the debug layer, on its own list, on the card and on WARP, and every one failed Close. Main turns all of these into after() or UAV(NULL), which the runtime accepts. Masks without narrow had zero rejections, so the war,narrow,dirty,join mask from the C6a table is affected too, not only place.
The war execution-only barriers (NO_ACCESS on both sides), the mid-copy, post-source and post-copy barriers, and the join path all passed. A place_copy-shaped sequence ran clean under the debug layer and with GPU-based validation and copied the right bytes, also on WARP. It sent the legacy transitions through Rec::barriers, which held them and flushed on a same-resource transition and on the next copy, and used the PR's barriers where they're valid.
On the other points I looked at. The PR adds only global barriers, no buffer or texture barriers and no layouts, so there's no layout versus legacy state mixing on one resource beyond what main already does with after() next to ResourceBarrier. Nothing touches placed-resource initial layouts or COMMON/UNDEFINED discards. Split barriers (and other sync bits decode_sync doesn't know) take the unknown-bit path to SYNC_ALL (waits.rs:379), which looks safe, but I didn't record one on a device. Rec::barriers takes the held lock and calls the raw list inside it, flush takes the same lock but is never reached from there, poisoning is tolerated, and the expects are only in tests.
The suite on main and on the PR, cargo test -p victory-gpu --lib skipping views_die_with_their_resource, shows the same failures (indirect host_list_* x2, the two pipeline::host library loads, the flaky watch test), and the 13 new tests pass. Same with VICTORY_BARRIER_OPT=1, with --features diagnostics and VICTORY_D3D12_DEBUG=1, and on WARP, where descriptor::host views_are_made_beside_the_heap_and_copied_in also fails, as it does on main WARP. None of the existing tests record groups_asked or place_copy on a device, so they can't catch any of this. Everything above came from a throwaway test that records each plan shape on a real list and closes it, and something like that in the PR would have caught it. cargo check -p victory is clean. Clippy adds 10 warnings in waits.rs, nine bool_assert_comparison in the new tests and one operator-precedence warning on DirtyTracker::INIT (waits.rs:583).
Two smaller things. The body says VICTORY_BARRIER_STATS is documented, but SETTINGS.md only gets the OPT and OPT_MASK rows. And narrow trims SyncAfter to the title's own consumers where main used ALL_SHADING, while the host records its own work between a title barrier and the title's next draw (indirect clamps, the ambient and lightprobe passes). I didn't find host work that reads title-written data with no barrier of its own (the in-place clamp has its own after(COMPUTE)), but I didn't go through all of it.
Not tested are the game itself, performance, Linux and vkd3d-proton, a device without enhanced barriers, title barriers with more than one group beyond the few above, and whether the dirty elision is complete. For that I only checked that mark_dirty covers the title's draws, dispatches, copies and resolves, not every host pass.
AI-assisted: yes
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.