Skip to content

Shaders: look up the second page of straddling title-memory writes in the page table - #2484

Merged
Marvisak merged 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/shader-memory-straddle-dirty-mark
Nov 25, 2026
Merged

Marvisak merged 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/shader-memory-straddle-dirty-mark

Conversation

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Summary

Fixes two out-of-bounds / mis-indexed dirty-bitmap updates in Lower::memory (crates/victory-shader/src/emit/lower.rs) when a multi-byte title-memory store (bytes > 1, such as global_store_dwordx2 / x3 / x4) straddles a 256 MiB or 4 GiB boundary:

  1. 4 GiB boundary wraparound (lo + (bytes - 1) in 32 bits):
    Lower::memory previously computed the last byte's page delta from the low address dword alone:

    let end = self.s.m.bin(c, Bin::Add, lo, last);
    let pages = self.s.m.bin(c, Bin::Sub, end >> 16, lo >> 16);
    let next = self.s.m.bin(c, Bin::Add, e.entry, pages);

    A store straddling a 4 GiB boundary (for example 0x1_FFFF_FFF8 ..= 0x2_0000_0007, which memory::table and lo.wrapping_sub(start) explicitly allow within a single host copy) wrapped end to 7, yielding pages = 0xFFFF_0001 (-65535 mod 2^32) and issuing an atomicBinOp.i32 on the root dirty-bitmap UAV at byte offset ~+512 MiB past the 256.5 KiB mapped bitmap buffer.

  2. 256 MiB level-1 region boundary overflow (e.entry + 1):
    e.entry packs (block << 12) | page. When a store crossed from page 0xFFF of one 256 MiB region into page 0x000 of the next (0x1_0FFF_FFF8 ..= 0x1_1000_0007), e.entry + 1 overflowed the 12-bit page index into ((e.block + 1) << 12) | 0 instead of looking up l1 + 1 in the level-1 table (memory::table assigns blocks in descending copy-size order, so adjacent 256 MiB regions do not occupy e.block + 1):

    • At e.block == CAPACITY (512), (513 << 12) | 0 (word = 262_656 bytes) indexed past the (CAPACITY + 1) * 4096-bit bitmap.
    • For e.block < CAPACITY, marking (e.block + 1) << 12 either dirtied an unrelated region's page 0 or hit an unallocated/unowned block entry that caused memory::bind's defer_missed / defer_after check to treat the entire command list as having read unmapped memory (withholding all title-memory and marked-view copy-backs for that submission), while leaving the actual second page unmarked.

Changes

  • Split Lower::memory_page and Lower::memory_mark_index out of Lower::memory_entry and Lower::memory, plus Lower::mark_memory_bit for bitmap updates.
  • Emit the first dirty/miss bitmap update unconditionally under marks, and only when write && bytes > 1, !e.miss, and (lo >> 16) != (end_lo >> 16) (computed with 64-bit add64(lo, hi, bytes - 1, false) and 48-bit end_hi masking) look up (end_lo, end_hi) via memory_page + memory_mark_index inside a guarded block. This also avoids emitting a redundant second atomicBinOp.i32 on single-byte writes, missed writes, and non-straddling multi-byte writes.
  • Note on overlap with Shaders: guard memory_dirty atomics with test-before-or and coalesce page-table loads (FIND-12) #2167: Shaders: guard memory_dirty atomics with test-before-or and coalesce page-table loads (FIND-12) #2167 optimizes mark_dirty_bits with test-before-OR and coalesced loads while keeping the 32-bit end = lo + last; next = e.entry + pages calculation; whichever lands second is a straightforward merge in Lower::memory.

Verification

  • Added emit::memory_tests::straddling_writes_across_256mib_and_4gib_boundaries_mark_exact_pages (crates/victory-shader/src/emit/memory_tests.rs), lowering and DXIL-validating a global_store_dwordx4 compute shader and executing it on both SM 6.6 hardware (RTX 5090) and D3D12 WARP across:
    1. a 4 GiB boundary (0x1_FFFF_FFF8 across l1 = 0x1F -> block 2 into l1 = 0x20 -> block 1),
    2. a 256 MiB boundary at e.block == 512 (CAPACITY) into an unmapped next region (0x1_0FFF_FFF8 -> marks 512 * 4096 + 0xFFF and missed region bit 0x11 without overflowing past block 512), and
    3. an aligned single-page store (0x1_FFFF_FF00).
  • cargo test -p victory-shader --lib emit::memory_tests (4/4 passed on hardware + WARP)
  • cargo clippy -p victory-shader --all-targets -- -D warnings
  • cargo xtask policy

@Bj0rnis

Bj0rnis commented Nov 25, 2026

Copy link
Copy Markdown

Independent Windows 11 + NVIDIA GeForce RTX 5070 Ti verification of #2484 at d0c55787 (clean git merge-tree with origin/main 940d32f6, Rust 1.98.1 MSVC):

  • victory-shader tests (RTX 5070 Ti): cargo test -p victory-shader --lib --locked -- --test-threads=1 -> 245 passed, 3 failed, 3 ignored (89.04s). The new test passes; all 3 failures reproduce identically on clean main 940d32f6 on RTX 5070 Ti (emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros [NVIDIA D3D12 (70564ae): stride_tests & synthetic_tests fail due to HEAP_VIEWS (2M > 1M NVIDIA limit) and Stride::Structured with rawBufferLoad #1835], and emit::synthetic_tests::gathers_at_a_level_match_the_hardware).
  • Clippy: cargo clippy -p victory-shader --all-targets --locked -- -D warnings -> 0 warnings.
  • Asset policy: cargo xtask policy -> 182 files ok.

Not tested: live game workload / shader translation cache run.

AI-assisted test run and summary.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Independent Windows verification (software WARP only, no hardware GPU in the final runs)

  • PR head: d0c55787be9521d42e986f74c19c7f5db42ab15d; origin/main: 940d32f69b22265690a0b5a15d6ded572ba33baf (merge base = origin/main; git merge-tree merges clean)
  • Windows 11 Pro 10.0.26200, rustc/cargo 1.98.1, WARP D3D10Warp.dll 10.0.26100.9818, dxil.dll 1.9.2602.17
  • Adapter: VICTORY_D3D12_WARP=1 and VICTORY_WARP=1 (the latter is what victory-shader's test Host::new actually reads; the code path is EnumWarpAdapter, i.e. Microsoft Basic Render Driver 0x1414). The tests do not print the adapter id, so 0x1414 is from the code path, not from log output.

Results:

  • cargo test -p victory-shader --lib --locked -- --test-threads=1: 246 passed, 2 failed, 3 ignored. All 4 memory_tests pass, including straddling_writes_across_256mib_and_4gib_boundaries_mark_exact_pages (new) and writes_through_views_mark_their_pages.
  • The 2 failures are synthetic_tests::gathers_at_a_level_match_the_hardware and synthetic_tests::permutes_read_the_lane_named_and_zero_from_lanes_left_out (WARP gather result / "no pipeline for a wave of 32"). Both fail identically on origin/main under WARP, so they are not caused by this PR.
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean.
  • cargo xtask policy: asset policy: 182 files ok.

Diff review: the second-page lookup is sound. It computes the last byte with a 64-bit add (carry into hi, masked to 16 bits), looks the page up through memory_page instead of offsetting entry, and only does so when the write crosses a 64 KiB boundary, is not a miss, and is active. A wrap past 4 GiB also reads as "crossed". I found no logic issues and no overlap with other open PRs.

Caveats: environment notes for others running these tests on WARP: the Windows SDK 10.0.19041 dxil.dll makes the SM 6.6 tests crash with STATUS_ACCESS_VIOLATION on WARP (also on main); a current dxil.dll avoids it. Also, victory-shader tests ignore VICTORY_D3D12_WARP and only honor VICTORY_WARP=1, otherwise their "hardware" pass uses the real GPU. An earlier attempt of mine set only VICTORY_D3D12_WARP, so its "hardware" pass briefly ran on the physical adapter before crashing; all results above are from the later VICTORY_WARP=1 runs.

Verified by Claude Code (Claude Sonnet 5.5), an AI assistant, on behalf of a maintainer.

@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: merged into main 940d32f, cargo test -p victory-shader --locked on macOS arm64 — 144 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — second page of straddling title-memory writes looked up (+241, the page-straddle fix).

Lower::memory previously computed the dirty bitmap bit for the last byte of a write by adding 32-bit wrapped page deltas to the first page's level-2 entry. Writes straddling a 4 GiB boundary wrapped the 32-bit addition to ~512 MiB out-of-bounds in the dirty bitmap UAV, and writes crossing a 256 MiB boundary overflowed the 12-bit page index into the wrong or out-of-bounds level-2 block. Guard the second bitmap update on non-missed 64 KiB page crossings and resolve the end address with 64-bit addition through the page table.
@fuzeofficlal
fuzeofficlal force-pushed the fix/shader-memory-straddle-dirty-mark branch from df829fb to 3c5fa89 Compare November 25, 2026 16:10

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate.

In Lower::memory (crates/victory-shader/src/emit/lower.rs), the second dirty bit of a multi-byte write is no longer e.entry + (pages crossed) computed from the low dword. Main has two real bugs here:

  • A store that crosses 4 GiB wraps lo + bytes - 1, giving a huge entry and an out-of-range atomic on the root UAV bitmap.
  • A store that crosses a 256 MiB region turns entry + 1 into (block + 1) << 12, which is the wrong region's block.

The PR computes the last byte with add64 and looks it up again through the new memory_page and memory_mark_index helpers. It only does so inside a guarded block when the write actually crosses a 64 KiB page, is not a miss, and is active. The marking convention is unchanged (a page entry, or the region's level-1 bit when the region has no block). Values from the second lookup are used only inside the guard.

The new test straddling_writes_across_256mib_and_4gib_boundaries_mark_exact_pages checks the exact bits, including the capacity-block edge. It overlaps textually with #2167.

CI: check passing.


Generated by Claude Code

@Marvisak
Marvisak merged commit e815cef into Marvisak:main Nov 25, 2026
1 check passed
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>
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.

5 participants