Skip to content

Shaders: LDS accesses outside the program's allocation read 0 and drop their writes - #1769

Open
Impre-visible wants to merge 1 commit into
Marvisak:mainfrom
Impre-visible:fix/lds-out-of-bounds
Open

Impre-visible wants to merge 1 commit into
Marvisak:mainfrom
Impre-visible:fix/lds-out-of-bounds

Conversation

@Impre-visible

Copy link
Copy Markdown

What

Lower::ds in crates/victory-shader/src/emit/lower.rs took every LDS address as in bounds (its own comment called it a shortcut: "out-of-bounds console accesses read 0 and drop writes"). This gives LDS accesses that console behaviour:

  • each access compares its dword index with the program's allocation (lds_dwords, unsigned);
  • outside it, a read gives 0, a store or an atomic is skipped, and the value an atomic returns is 0 for an active lane (an inactive lane keeps its register, as before);
  • the element address is clamped to element 0 when outside, so nothing is ever addressed past the array.

A small and_guard helper narrows a lane guard. One test, lds_accesses_are_bounded_by_the_allocation, lowers a write, a read and an atomic with a returned value and checks that each got its bound (a test-only counter beside nonuniform_handles).

Why it matters

A title compute program can be written for one thread group and still be dispatched with several: its LDS addresses then come from the global thread id, and in every group but the first they lie far outside the allocation. On the console that is harmless. On the host the writes land in whatever group-shared variable sits next to the LDS array.

With a wave64-on-32-lane implementation that keeps its own synchronisation state in group-shared memory (#1433's console flow: phase, arrived, finished next to lds), those stray writes can corrupt that state, and its wait loops are not covered by the loop limit. On my machine this is what ended every run at world entry.

Measured

Linux, Wine (GE-Proton11-6) + vkd3d-proton, GTX 1660 6 GB (32-lane waves), NVIDIA 620.73.04. Test tree: #1433 with #1492 merged by hand plus #1077 #1175 #523 #530 #1040 #1406 #542 #1056 #1392 #1516 #1388 #1189 #1523 (details in my comment on #1581).

Without this change, in that tree:

  • 9 of 9 runs that reached the world: kernel NVRM: Xid 109 ... CTX SWITCH TIMEOUT 16 to 28 s before the title stops, GPU at 100 % for about 8 s first, VIctory then waiting forever on the submission's fence.
  • A bounded first wait named the stuck submission: one list with 12 compute dispatches. Skipping pipelines (diagnostic only): all 12 skipped, no Xid in 7 minutes; with the first 6, 9 or 11 skipped the Xid 109 stays, so it is the last one, a 32x1x1 dispatch of a small program that uses LDS. VICTORY_SHADER_LOOP_LIMIT=64 does not help, which fits a spin outside the program's own loops.

With this change, same tree, nothing skipped, 3 runs:

run Xid 109 within 30 s of world entry what happened
1 no 6 minutes in the world, no Xid, reached a frame where the final image shows a lit 3D scene (first time on this machine); ended by my time limit
2 no 3 min 17 s in the world, then one Xid 109 (a later, different stall; not identified yet)
3 no no Xid at all; the title stopped on its own after 5 min 50 s of game time (the #1323 stop)

So the immediate hang is gone; the world is not stable yet for other reasons.

Validity of what is emitted: on this branch (plus #226's two lines applied locally, without which main's SPIR-V fails on NVIDIA anyway), vkd3d-proton converted 1,484 modules in a run into the world and spirv-val accepts all of them. (My first attempt used the float predicate ULT instead of IULT for the bound, which the unit tests did not catch and spirv-val did; that is fixed here.)

Not tested

  • Windows and AMD: not run. cargo test -p victory-shader --lib: 97 passed, 1 ignored. Builds for x86_64-pc-windows-msvc with and without --features diagnostics.
  • The execution result itself (that an out-of-bounds read returns 0 on a host) is not covered by a GPU test; the unit test checks the lowering only.
  • I did not verify how NVIDIA lays out group-shared variables; the corruption path is inferred from the emitted module and from the A/B above, not observed directly.
  • Byte and halfword LDS accesses are still unsupported, as before.
  • cargo clippy: not run here.

🤖 Generated with Claude Code

@DesertGamer

Copy link
Copy Markdown

VIctory V2: community PRs, merged and tested

Open PRs sitting in the queue? V2 pulls them into one build, screens each one (binaries, build scripts, hooks, new deps) and runs tests after every batch.

Where it stands today: launches, runs the menu at 30 fps, plays the intro video, reaches the start screen and loads the world (~50 s). Entering the world is the current blocker: rendering fails with a GPU "device removed" error (tracked in issue #1).

⚠️ Entering the world on a real GPU can hang it and make Windows reset the driver. Try it at your own risk.

Not playable yet, but every batch of merged fixes moves it closer. If you have a PR that's stuck, or a GPU to test on, I'd love your help:
https://DesertGamer/VIctory-V2

Unofficial community fork of Marvisak/VIctory, GPL-3.0.

@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.

Merge conflict with current main (70564ae) — the LDS-allocation bounding change (and_guard + lds_accesses_are_bounded_by_the_allocation test, reads-out-of-allocation → 0 and drops) needs a rebase. The approach looks right statically: bounds are checked against the program's LDS allocation rather than a fixed size. Happy to re-test after rebase.

@rminzao

rminzao commented Nov 24, 2026

Copy link
Copy Markdown

Gap with the sub-dword LDS paths

Besides the rebase conflict already mentioned, the bound doesn't reach every LDS address:

  • Current main has byte/halfword LDS lowering (0b35b7f). Those read and write paths call dword(self, offset) and need the new inside result applied too. The PR body still calls those accesses "still unsupported", which is out of date.
  • Shaders: LDS halfwords straddling two dwords, and hardware tests of byte and halfword LDS accesses #1795 adds lds_straddle, which builds the following dword (index + 1) directly instead of going through dword. With both PRs merged, a halfword at byte 3 of the last dword still reads or writes past the allocation, the corruption this PR is meant to stop. Whichever merges second needs an inside check on that second element.

The new test only counts calls to the closure, so it can't catch a wrong predicate (as with the ULT/IULT mix-up that slipped through). A test with an address just past the allocation, checking that it reads 0 and the write is dropped, would catch that.

Static review (AI-assisted), read against main at 70564ae; nothing was run on a GPU.

@Impre-visible

Copy link
Copy Markdown
Author

Merged today's main into this branch. main added byte and halfword LDS reads and writes through the same dword helper, so they now get the same bound: a read outside the allocation gives 0, the atomics of a write outside are skipped. cargo test -p victory-shader --lib: 120 passed, 1 ignored; builds for x86_64-pc-windows-msvc with and without --features diagnostics. The unit test still covers the dword forms only, and the merged result has not been run in the title yet.

🤖 Generated with Claude Code

@jules47800

Copy link
Copy Markdown

Hardware check of this PR with the device test from #1870. AMD RX 6600 XT (RDNA2, driver 32.0.21045.29003) and WARP, Windows 11, Rust 1.98.1 MSVC, DXC 1.9.2609 validator. Synthetic programs only, no game files.

This PR's test checks the lowering (a counter). #1870 has a test that runs the translated program on the device: lds_out_of_bounds_reads_zero_and_drops_writes. It covers b32 write/read at +512 and +65532, ds_read_i8 at +600, ds_write_b8 at +513, ds_read2_b32 with offset1 800 bytes, ds_add_rtn_u32 at +512, a 32-bit wrap back inside (own - 16, offset 16), and a returning atomic under a partial EXEC.

Tree Result
main 42363ab + that test fails: hardware 306/340 inputs (the out-of-bounds write is kept and read back), WARP 0
this PR's head cd90d74 + that test passes on hardware and WARP
this PR merged into main 42363ab + that test cargo test -p victory-shader --lib: 248 passed, 0 failed, 3 ignored; both clippy configurations clean with -D warnings

#1870 measured the same 306/340 on an RX 6750 XT, so this isn't specific to one card. #1870 is the same fix written separately. If this PR lands, #1870 can close. Its test is worth bringing along: it is about 40 lines in semantics_tests.rs, and the existing ds helper only needs its offset widened to u16 to encode offset1.

Not checked: the title (no game files here), and the lds_straddle path from #1795 raised above.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Proposed close: already in main. Thanks for rebuilding the branch and saying so plainly. As you note, #1949 put the LDS bound into main. The dword helper in Lower::ds (crates/victory-shader/src/emit/lower.rs) compares the dword index with lds_dwords (IULT), reads 0 and drops writes outside the allocation. main also has the device test lds_out_of_bounds_reads_zero_and_drops_writes (emit/semantics_tests.rs), which checks the actual behaviour on hardware and WARP.

What's left here is a test-only counter (lds_bounds) and a test that counts how many times the bound is emitted. It doesn't check what the bound does, which main's device test already covers. So there isn't enough left to merge.

This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label triage: proposed close.

CI: passing.


Generated by Claude Code

@Impre-visible

Copy link
Copy Markdown
Author

Merged the current main (940d32f) into this branch; it merges without conflict now. Builds for x86_64-pc-windows-msvc with and without --features diagnostics; cargo test -p victory-shader --lib: 145 passed, 2 ignored.

🤖 Generated with Claude Code

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown

The Windows run the Not-tested list mentions. Ran on Windows 11 with an RTX 3070 (Ampere, 32-lane waves, driver 616.56), debug build, at 38e1d55, cargo test -p victory-shader --locked: 245 passed, 3 failed, 3 ignored. The new lds_accesses_are_bounded_by_the_allocation passes on NVIDIA. The 3 failures are #1744's NVIDIA failures on main. Same as main on this hardware.

AI-assisted (Claude Code).

@Mericas77

Copy link
Copy Markdown

NVIDIA counterpart of the RX 6600 XT check above: on an RTX 4070, main keeps the out-of-bounds LDS write too, and this PR drops it.

Setup: RTX 4070 (Ada, 32-lane waves), driver 621.01, Windows 11, Rust 1.100.0. main at 940d32f and this PR at 38e1d55, each with #1870's lds_out_of_bounds_reads_zero_and_drops_writes added. Synthetic programs only, no game files.

cargo test -p victory-shader --locked --lib lds_ -- --test-threads=1, 3 runs each, same result every run:

Tree Result
main 940d32f + the test fails. Probe 1 (b32 at +512): 306/340 inputs differ on hardware, 0 on WARP. ds_read_b32 [own + 512] reads back b, so the write landed. Probes 2 and 3 (b8 at +513, read2 at +800, ds_add_rtn_u32 at +512, 32-bit wrap, partial EXEC) show 0 differences.
this PR + the test passes on hardware and WARP. lds_accesses_are_bounded_by_the_allocation and lds_bytes_and_halves_match_the_hardware also pass.

That is the same 306/340 as on the RX 6750 XT and RX 6600 XT, so this is not AMD-specific. On NVIDIA, the stray write lands in the group-shared memory next to the array.

Full shader suite (cargo test -p victory-shader --locked --lib -- --test-threads=1): main 244 passed, 4 failed, 3 ignored; this PR 246 passed, 3 failed, 3 ignored. The 3 left are the known NVIDIA failures from #1744 (a_heap_buffer_read_by_element_takes_the_views_stride, a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, gathers_at_a_level_match_the_hardware). None of them is about LDS.

AI-assisted.

@Impre-visible

Copy link
Copy Markdown
Author

@Mericas77 thank you, that closes the gap I could not close myself: this PR had only a WARP-level test plus title runs on one NVIDIA card, and you now show on hardware that main keeps the out-of-bounds write on NVIDIA as well (306/340, same as the two AMD cards) and that this branch drops it.

Summary of the independent checks so far, for the maintainers:

Card main + #1870's test this PR + #1870's test
RX 6750 XT (from #1870) 306/340 differ not run there
RX 6600 XT fails passes
RTX 4070 fails, 306/340 passes, hardware and WARP

The hardware test that proves it is the one from #1870 (lds_out_of_bounds_reads_zero_and_drops_writes), written by @wwwwwwww3333333, not by me. I have not copied it into this branch because it is their work. If they agree, I will add it here with their authorship kept (a Co-authored-by commit), or the maintainers can take the lowering from either place and the test from #1870; both changes bound the same accesses the same way.

🤖 Generated with Claude Code

@qtLkz

qtLkz commented Nov 25, 2026

Copy link
Copy Markdown

Native Windows run, which the description lists as not done: RTX 4070 Ti (Ada), driver 621.01, Windows 11 Pro build 26300, Rust 1.98.1 pinned, this PR merged on main 940d32f (clean merge).

cargo test -p victory-shader --lib --locked -- --nocapture --test-threads=1

Limit: the new test checks the lowering (one bound per LDS access), it doesn't dispatch on the GPU, and I didn't run a title. This doesn't answer the NVIDIA group-shared layout question.

AI-assisted (Claude).

desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 26, 2026
…allocation read 0, drop writes)

lower.rs: main's test-only heap_handles count and Marvisak#1769's lds_bounds
count both kept, in Lowered and where it is built.

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>
@wwwwwwww3333333

Copy link
Copy Markdown
Contributor

@Impre-visible yes, please take lds_out_of_bounds_reads_zero_and_drops_writes from #1870 into this PR. A Co-authored-by line is fine, and thanks for asking. Keeping the lowering and the test in one place makes it easier to review.

main bounds every LDS access by the program's allocation (Marvisak#1949), and its
test lds_out_of_bounds_reads_zero_and_drops_writes runs the translated
program on a device, so it runs on Windows only. The lowering now counts
the bounds it emits (in tests), and lds_accesses_are_bounded_by_the_allocation
checks that a write, a read and a returning atomic each get one, on any
host.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TsTmwWj9LVy4o3qDH52Jas
Signed-off-by: Impre-visible <69050895+Impre-visible@users.noreply.github.com>
@Impre-visible

Copy link
Copy Markdown
Author

Rebuilt this branch on main 63bcca4. The conflict was in the LDS lowering. #1949 (the patch from #1870, thanks @wwwwwwww3333333) already bounds every LDS access the same way, so I kept main's lowering and dropped ours. Main also already has lds_out_of_bounds_reads_zero_and_drops_writes, so nothing was left to bring over from #1870.

What remains is one test-only commit: a counter of the bounds the lowering emits, and lds_accesses_are_bounded_by_the_allocation, which runs on any host (the device test is Windows-only). If that isn't wanted, this PR can close as superseded by #1949.

Linux: cargo test -p victory-shader --lib, 173 passed, 2 ignored (with main's Windows-only validator calls fenced locally, not committed). Windows target: both CI clippy runs pass.

🤖 Generated with Claude Code

@wwwwwwww3333333

Copy link
Copy Markdown
Contributor

@Impre-visible thanks for rebuilding this on 63bcca4 and for checking against #1949. I ran c2ee29a on an RX 6750 XT (RDNA2, driver 32.0.21045.29003, Windows 11, Rust 1.98.1): lds_accesses_are_bounded_by_the_allocation and lds_out_of_bounds_reads_zero_and_drops_writes both pass, and the whole victory-shader suite is 303 passed, 0 failed, 3 ignored (main on this machine: 302 passed, 0 failed).

For what it's worth, the counter test is useful on its own: the device test for #1949 only runs on Windows, and this one also checks the lowering off Windows. Whether it stays open is the maintainer's decision, and closing it as superseded is fine by me too.

Unit tests only, no game files.

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.

10 participants