Skip to content

Shaders: guard memory_dirty atomics with test-before-or and coalesce page-table loads (FIND-12) - #2167

Open
RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/find12-dirty-atomics
Open

RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/find12-dirty-atomics

Conversation

@RVZO6

@RVZO6 RVZO6 commented Nov 25, 2026

Copy link
Copy Markdown

Addresses FIND-12: shaders lowered through the title-memory page-table path currently issue an unconditional dx.op.atomicBinOp.i32 (Or) into the global memory_dirty bitmap on every store — twice per multi-byte store (for start and end, even when both land on the same 64 KiB page) — causing severe L2 atomic contention when many compute lanes write the same page (~2.4 s/dispatch, 4.8–6.9 s/frame reported on RTX 5080). In addition, every title-memory access re-loads memory_table[4096] (memory_null) and reads (view, start) as two separate 4-byte rawBufferLoads, and mark_view_write reads (base, phase) as two separate 4-byte rawBufferLoads and emits a second dirty mark even for single-byte stores.

The change

  • crates/victory-shader/src/emit/shader.rs: add memory_null: Option<V> and Shader::memory_null() so the null-view index at memory_table[4096 * 4] is loaded once in entry block 0 (emit_at_entry, safe even when first used inside a branch) and reused across all title-memory accesses.
  • crates/victory-shader/src/emit/lower.rs:
    • Add raw_dword2() (dx.op.rawBufferLoad.f32, mask = 3, align = 8) to coalesce the contiguous 8-byte (view, start) load in memory_entry() and (base, phase) load in mark_view_write().
    • Add mark_dirty_bits(bitmap, bits, guard):
      • Omits the 2nd dirty mark statically when bytes <= 1 (single-byte stores cannot cross a 64 KiB page boundary).
      • For multi-byte stores, checks n != bits[0] before computing the word/bit offset and loading the bitmap.
      • Guards every dirty-mark dx.op.atomicBinOp.i32 (Or) with a read-only rawBufferLoad + (old & bit) == 0 branch so already-dirty pages never serialize on L2 atomics. Uses pure 32-bit ops without WAVE_OPS (SFI0 & 0x4000 == 0).
  • crates/victory-shader/src/emit/memory_tests.rs: add memory_entry_caches_null_coalesces_loads_and_guards_dirty_atomics and dirty_mark_width_and_branch_hoisting verifying single entry-block memory_null, coalesced raw_dword2 loads, guarded dirty-mark atomics, n != bits[0] gating before bitmap load, 1-byte 2nd-mark elision, dxil.dll validation, and zero WAVE_OPS.

Not a duplicate of

FIND-12 is open in CLAIMS.md; no other open PR guards memory_dirty atomics or coalesces page-table / view-mark raw_dword2 loads in crates/victory-shader/src/emit/lower.rs.

Tested

  • Official windows-latest MSVC + Windows WARP SM 6.6 CI on fork integration stack (Run 45562558052):
    • cargo clippy --workspace --all-targets --locked -- -D warnings: clean.
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean.
    • cargo test --workspace --locked: 712 passed, 0 failed, 10 ignored, including victory-shader 249 passed, 0 failed, 3 ignored (all 5 emit::memory_tests::* including title_memory_is_read_and_written_at_computed_addresses, root_views_read_their_copy_and_zeros_past_it, writes_through_views_mark_their_pages, and both new bitcode/validator tests on Windows WARP SM 6.6) and xtask (6 passed, 0 failed, repository_passes_policy).
  • Standalone branch 6ab9c98: cargo clippy (default & --all-features) -D warnings clean; Wine-10.0 + dxil.dll 1.9.2609.5 passed both new tests and emit::lower:: / emit::bitcode:: (29 passed, 0 failed).

Not run: the title; any hardware GPU (CI ran on Windows WARP SM 6.6 only). The title was not used in these tests.

AI-assisted: T3 Code (Antigravity, gemini-4-neon)

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified locally merged onto origin/main (940d32f, Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC, with dxil.dll validator):

  • cargo test -p victory-shader --lib emit::memory_tests:: — 5 passed, 0 failed on both RTX 5090 hardware and VICTORY_D3D12_WARP=1
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean test-before-or guard on memory_dirty atomics and coalesced page-table loads (FIND-12), verified end-to-end on hardware and WARP. Looks great.

AI-assisted (Gemini 4 Neon).

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, cargo test -p victory-shader --locked.

144 passed / 0 failed / 2 ignored. memory_dirty atomics guarded with test-before-or — atomic guard correctness fix.

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head 6ab9c98 onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

@nathanbzrr

Copy link
Copy Markdown
Contributor

Verified head 6ab9c9880f5b8dbad7582f8a046473b4e3e659c6 merged cleanly onto main (940d32f69b) on Windows 11 x64 / AMD Radeon RX 7600 (driver 32.0.32029.2010, rustc 1.98.1, dxil.dll 1.8.2502.11):

  • cargo test -p victory-shader emit::memory_tests -- --nocapture: 5 passed, 0 failed (confirms dx.op.rawBufferLoad.f32(..., mask=3, align=8) validates against dxil.dll 1.8.2502.11 and executes on RDNA3 hardware).
  • $env:VICTORY_D3D12_DEBUG='1'; cargo test -p victory-gpu --lib memory:: -- --test-threads=1: 3 passed, 0 failed.
  • Source audit against lower.rs / shader.rs / draw.rs / memory.rs:
    • Shader::memory_null() hoisting into block 0 strictly dominates all uses in bitcode.rs SSA layout, resets cleanly on Stop::Retry, and every dispatched stage with stage.memory.is_some() binds a full page table (draw.rs:739-771).
    • mark_dirty_bits structured CFG keeps begin_guarded/end_guarded nested with zero wave ops (SFI0 & 0x4000 == 0 -> RDNA3 Wave32/64 divergence safe), non-globallycoherent L1 reads of monotonically 0 -> 1 bits are conservative, and all four write == true callers in lower.rs re-read self.cur.

Looks good to merge from this check.

AI-assisted; synthetic tests only, no game files, dumps, or captures used.

@Slay326

Slay326 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Hardware data point for the contention this PR removes. I measured main (940d32f) against main + this PR (6ab9c98) with a synthetic compute test: 64 threads per group, each doing 8 global_store_dwordx4 through the title-memory page table (the shape of a node writer), GPU timestamps, 3 runs of 5 dispatches each (median per run, mean of the 3 runs). RTX 3070 8 GB, driver 32.0.16.1088, Windows 11 Pro 26200.

Written-pages bitmap in system memory (as memory::bind makes it), copy of title memory in video memory:

groups per dispatch main #2167 factor
71 585 us/group (41.6 ms) 0.18 us/group (0.013 ms) ~3,200x
1000 575 us/group (575 ms) 0.103 us/group (0.10 ms) ~5,600x
9000 559 us/group (5.03 s) 0.0965 us/group (0.87 ms) ~5,800x

Bitmap in video memory (as in #2175), same test: 0.62 us/group on main, 0.096 us/group with #2167 (9000 groups). With #2167 the bitmap's location no longer matters for the marks (0.0965 vs 0.096). On WARP (same test, bitmap in system memory, 1000 groups) it is 10.3 us/group on main and 0.86 us/group with #2167.

The marked pages are identical on both branches in every variant and every run (9 of 9, 125 of 125, 1125 of 1125 pages). cargo test -p victory-shader and -p victory-gpu give the same failures on main and on main + #2167 (3 hardware-comparison tests in stride_tests/synthetic_tests, and 2 heap-size tests in victory-gpu; they fail identically without this PR, and all other tests pass).

With #1890 and other local changes (so not main; title 1.0.2.1): one list of the XboxBvhBuilder passes (a node writer with 12 stores per thread over ~140,000 groups) ran for more than 535 s and never finished. With the same guard in place (our equivalent of this PR) the same passes take a median of 25 ms and 19 ms of GPU time per frame and no list stalls (300 s run, 42 world intervals).

On correctness: the bitmap only goes from 0 to 1 during a submission, and the host clears it (defer_after, mem::take) only after execute has waited for the lists. A stale load can therefore only see a bit as clear (an extra, harmless OR), never a cleared bit as set. Two lanes that both see a clear bit both OR it, which is idempotent. A list named twice in one submission shares the bitmap, and the union is read after both runs, so skipped atomics lose nothing. So globallycoherent is not needed.

AI-assisted.

desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 25, 2026
…rehydration

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 25, 2026
…his stack has them

The synthetic dirty-mark program (Marvisak#2167, written against main) lacked the
target, console_cf and per_lane_only fields and Lower::new's
numeric_selects argument that the wave and select branches add, so the
Windows test build did not compile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 25, 2026
desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 25, 2026
…ak#2310

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 25, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@desplmfao

Copy link
Copy Markdown
Contributor

@RVZO6 a data point from the title on an RTX 5080 (Linux, Proton, vkd3d-proton 3.2.0), on our stack with #439.

At world load, the title issues one list of about 7,000 direct dispatches of Microsoft's XboxBvhBuilder.dll rehydration passes (Box32Rehydration 28918b7150a7aad5, Box16Rehydration caa78b8ca62bc3d6, DecompressHeader 4ddd6f9702fab2a7). Most are 1 to 11 groups, and Box32 goes up to about 1,800. Without this PR the GPU worked through about 200 of them per second (measured with the breadcrumbs from #2310), so the list ran past the driver's timeout: NVRM: Xid 109 CTX SWITCH TIMEOUT, then the title's watchdog. We had to skip those three passes.

With this PR merged (cleanly; one fix-up commit so the Windows test helper builds on #439's Parts/Lower), the whole list finishes and the run gets past it. Breadcrumbs stop pointing at these passes. The next stall is a different program, in the title's hair system, which we're looking at separately. The translated Box32 drops from 274 to 233 raw loads, and every dirty-mark atomic now sits behind the bit test.

Thanks. This turned the world load on NVIDIA from "times out" to "goes through" for us.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate (re-checked after the rebase onto 63bcca4, head 3cb308d). It merges cleanly again. In crates/victory-shader/src/emit/lower.rs, Lower::memory now marks both its first page and the straddling page from main's 256 MiB / 4 GiB fix through the new mark_dirty_bit. That function loads the bitmap word first and issues the atomicOr only when the bit is clear, the same pattern mark_view_write already used on main. Other changes:

  • mark_view_write skips the second mark for 1-byte writes.
  • The (view, start) and (base, phase) pairs are read with one 8-byte-aligned raw_dword2.
  • Shader::memory_null loads the null index once in the entry block, as memory_handle does.

Which pages end up marked is the same as on main, by my reading.

Performance results are mixed: one tester reports a large speedup, another sees no change in W's time on an RTX 3070 Ti. Either way, the change only removes atomics for bits that are already set. #2755 adds wave-level coalescing on top of the same pre-check, so the maintainer may want to land this first and rebase that one. Optional: lower/mark_tests.rs and the bitcode reader in memory_tests.rs are not rustfmt-formatted.

CI: passing (check).


Generated by Claude Code

@RVZO6

RVZO6 commented Nov 25, 2026

Copy link
Copy Markdown
Author

Rebased onto current main (13f91ca, head 87e9805; resolved the end-of-file test-append conflict in memory_tests.rs).

Verified green on windows-latest MSVC + WARP SM 6.6 CI (Run 45662428628) and Wine + dxil.dll 1.9.2609.5.

Thanks to @Slay326 for the RTX 3070 timestamp benchmark (~3,200x–5,800x; 535 s -> 19–25 ms in title) and @desplmfao for confirming it resolves the RTX 5080 world-load timeout.

@davesousa

Copy link
Copy Markdown

In-game check against W on Windows NVIDIA (W is FIND-12's 16x16x16 dispatch, [compute 00167d58cc5a78d7] in #1968): RTX 3070 Ti 8 GB, driver 621.40, Windows 11 26200. main 13f91ca + #2319, with and without this PR (87e9805, merges cleanly; memory_tests 5/5 pass here, skipping the #2571 crash), shader cache rebuilt for each translator.

Build W, per host submission that contains it (slow host submission ms=)
13f91ca + #2319 + #1906 10.1-11.6 s (21 submissions, average 11.3 s)
13f91ca + #2319 + this PR 11.3, 11.6 s (2 submissions)
13f91ca + #2319 + #1906 + this PR 11.4-11.6 s (3 submissions)

On this card W's time does not change with this PR. With VICTORY_GPU_TIMING=1 (base 13f91ca + #2319), W alone is 11.4-11.6 s as one list command (4 times), so the time is in W itself, not the lists around it. W was issued in every run that got to world loading (6 of 6), in the first submission with heavy compute and then about once per world frame (FIND-15).

One observation I could not attribute: with both #1906 and this PR, 2 of 3 runs ended at boot with the title's own 0xc0000005 write to 0 at image+0x258b000 (before the first menu). The third reached the world. With only one of the two PRs, 3 of 3 booted. That is too few runs to blame either PR; I note it in case someone sees the same offset.

AI-assisted (Claude Code); the runs are on my machine.

…page-table loads

Coalesce 8-byte (view, start) page-table and (base, phase) view-mark
loads via raw_dword2 (mask 3, alignment 8), cache memory_null once in
the entry block across title-memory accesses, omit the second dirty
mark statically for single-byte writes, hoist the n != bits[0] guard
before bitmap word/bit loads, and guard every dirty-mark atomicBinOp
OR with a test-before-atomic rawBufferLoad check without WAVE_OPS.

Assisted-by: T3 Code (Antigravity, gemini-4-neon)
@RVZO6

RVZO6 commented Nov 26, 2026

Copy link
Copy Markdown
Author

Rebased onto current main (63bcca4, head 3cb308d). Preserved 63bcca4's 256 MiB / 4 GiB straddling-write fix in Lower::memory (applying test-before-OR mark_dirty_bit to both primary and straddling-page marks) and moved synthetic_mark_dxil into crates/victory-shader/src/emit/lower/mark_tests.rs so lower.rs stays below the 512 KiB asset-policy limit. Verified green on windows-latest MSVC + WARP SM 6.6 CI (clippy, xtask policy, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45727774760.

@RVZO6
RVZO6 force-pushed the rvzo/find12-dirty-atomics branch from 87e9805 to 3cb308d Compare November 26, 2026 00:21
desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 26, 2026
…, coalesce page-table loads)

memory_tests.rs: main's heap-direct read-mark test and Marvisak#2167's bitcode
tests both kept. Marvisak#2167's synthetic_mark_dxil gets Marvisak#439's Parts fields and
Lower::new's numeric_selects argument (false). memory_tests pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 26, 2026
Upstream main merged Marvisak#2143, Marvisak#2162, Marvisak#158, Marvisak#1906 and Marvisak#1152 and took up what
Marvisak#1769 (LDS bounds, Marvisak#1949) and Marvisak#1763 (typed UAV formats, Marvisak#2462) did; main's
code is kept for those.
- emit/lower.rs: ds_bpermute within each half (main's Marvisak#573, same as
  Marvisak#439's); LDS bounds as main's Marvisak#1949 (Marvisak#1769's test-only counter kept, its
  and_guard dropped); title-memory marks: main's straddling-write lookup
  (Marvisak#2484) with Marvisak#2167's current head (3cb308d, rebased on 63bcca4): guarded
  dirty atomics, raw_dword2, memory_null, synthetic_mark_dxil in
  lower/mark_tests.rs; the old Marvisak#2167 head taken out first.
- emit/host.rs: main's shared WARP device and lock (Marvisak#244) with Marvisak#439's
  adapter list and Host::on (create_device takes an optional adapter).
- descriptor/host.rs: main's Marvisak#2462 format checks; Marvisak#1763's tests kept.
- gpu host.rs: main's multi-plane present with Marvisak#2310's crumbed copy.
- pipeline.rs: Marvisak#2118/Marvisak#2311's builder and BVH checks with main's skip
  hashes (Marvisak#1320); draw.rs, shadow.rs, main.rs: both sides.
- Tests main added since get Marvisak#439's Parts fields and emit's Target.
- README: base 63bcca4; rows of PRs merged upstream, superseded or closed
  taken out, with a note.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 26, 2026
The previous renderer (old upstream base 940d32f) is the first parent and
is also kept as the branch renderer-2026-11-25-oldbase; the second parent
is the rebuild: fork main (upstream main 63bcca4 + our pull requests) with
the renderer stack merged on it: Marvisak#439 (on main: update/439-main's
resolution), Marvisak#2309, Marvisak#2167, Marvisak#2118, Marvisak#2311, Marvisak#1722, Marvisak#1751, Marvisak#688, Marvisak#1872, Marvisak#2292,
Marvisak#2310, Marvisak#2112 (opt-in), Marvisak#2230 (latch on by default), Marvisak#2389, Marvisak#2088 (small
windows opt-in), Marvisak#2099's new head, and tonight's Marvisak#491 journal fixes.
Merged upstream since: Marvisak#2143, Marvisak#2162, Marvisak#158, Marvisak#1906, Marvisak#1152; superseded by
main: Marvisak#1769, Marvisak#1763, Marvisak#2366.

The picture on this base still shows the overlay that plain upstream main
shows too; that is being looked into.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
github-actions Bot pushed a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
A benchmark (ignored by default) and a check, in HLSL that does what
translated programs do when they store to title memory: look the address
up in the page table (an upload heap), set the bits of the pages written
in the list's bitmap with atomics, store into the host copy. Variants:
main's two unconditional atomics per store; a load first and an atomic
only for a clear bit (Marvisak#2167's approach); one atomic per wave for lanes
sharing a bitmap word (Marvisak#2755's); both; none. Each with the bitmap in
system memory, as now, and in video memory; four ways of writing.

RTX 5070 Ti, driver 621.24, ms per dispatch of 131,072 threads storing
16 dwords each (bitmap clear before the dispatch / already set):

                            one page      linear  page/lane  scattered
  main, system bitmap      79.8/76.5   75.8/75.0  1595/1583  336/337
  test-first, system         1.6/0.33   11.5/0.35    21/20    13.0/6.7
  wave, system               1.7/0.33   11.4/0.36    22/22    13.5/6.8
  main, video bitmap        0.31/0.31   0.32/0.32    20/20     6.4/6.5
  test-first, video         0.30/0.30   0.32/0.32    20/21     6.8/6.7
  no marks                  0.30/0.29   0.32/0.32    20/19     6.5/6.5

Atomics on a bitmap in system memory are what cost: up to 250 times the
dispatch without marks. Loading first removes them once a page is marked;
one atomic per wave adds nothing on this GPU. A bitmap in video memory
costs nothing even with main's unconditional atomics, first marks
included. What is left in "page per lane" and "scattered" is the stores
into the host copy in system memory.

`every_variant_marks_the_pages_main_marks` checks every variant, in both
places, sets exactly main's bits. `dxc.exe` comes from the Windows SDK;
without it or a GPU both tests do nothing.

    cargo test -p victory-gpu --release --lib marks_bench -- --ignored --nocapture
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…with test-before-or and coalesce page-table loads (FIND-12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants