Shaders: guard heap-direct texture read marks with EXEC #2668

Open
github-bot wants to merge 1 commit from github-bot/gh-2668 into main AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 17:47 UTC · original
fuzeofficlal:fix/shader-guard-view-read-marks → main · +98 −66 · 2 files · 1 commits · open

Summary

In crates/victory-shader/src/emit/lower.rs, Lower::image called Lower::mark_view_read(index) after image_op without passing the instruction's per-lane EXEC guard (self.guard(b, i)?), unlike Lower::image_op (which guards VGPR writes and UAV stores/atomics) and Lower::mark_view_write (which gates its bitmap load and atomic OR with begin_guarded).

As a result, predicated-off lanes (EXEC == 0, e.g. after v_cmpx_* without a branch) or inactive lanes carrying stale/fallback descriptor indices (Source::LanePhi / Source::Fetched) still read VIEW_READS and set read-mark bits for texture slots that no active lane actually read, causing unnecessary demand materialization of unread placed textures.

Changes

  • crates/victory-shader/src/emit/lower.rs:
    • Return the instruction's Option<V> per-lane guard from Lower::image_op and pass it from Lower::image into Lower::mark_view_read(index, guard).
    • Wrap the bitmap index computation, raw_dword load, clear check, and inner atomicBinOp.i32 in begin_guarded(guard) / end_guarded(outer, &[]) (leaving unguarded guard == None paths completely unchanged).
  • crates/victory-shader/src/emit/memory_tests.rs:
    • Add heap_direct_texture_reads_skip_marks_when_exec_is_zero (v_cmpx_ne_u32 exec_lo, v0, v0, asserting marked == []) and heap_direct_texture_reads_mark_with_partial_exec (v_cmpx_eq_u32 exec_lo, 0, v0 across 2 threads, asserting only [VIEW_READS + 1]).

Verification

  • Negative control (before fix): heap_direct_texture_reads_skip_marks_when_exec_is_zero fails on both RTX 5090 and WARP, recording [VIEW_READS + 1] instead of [].
  • After fix: emit::memory_tests (6/6 on WARP, all 3 heap-direct texture read tests on both RTX 5090 and WARP with dxil.dll validation) pass.
  • cargo clippy -p victory-shader --all-targets -- -D warnings — pass (0 warnings).
  • cargo xtask policy — pass (184 files ok).

Note: memory_tests.rs uses vec![0u32; shader.user_data_dwords().max(1) as usize] for the test helper's user-data slice so the test runs cleanly on NVIDIA independently of the host-side fix in #2587 / #2596 (#2571); it merges cleanly with either.

**@⁠fuzeofficlal** on GitHub · 2026-11-25 17:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/2668) `fuzeofficlal:fix/shader-guard-view-read-marks` → `main` · +98 −66 · 2 files · 1 commits · open ## Summary In `crates/victory-shader/src/emit/lower.rs`, `Lower::image` called `Lower::mark_view_read(index)` after `image_op` without passing the instruction's per-lane `EXEC` guard (`self.guard(b, i)?`), unlike `Lower::image_op` (which guards VGPR writes and UAV stores/atomics) and `Lower::mark_view_write` (which gates its bitmap load and atomic OR with `begin_guarded`). As a result, predicated-off lanes (`EXEC == 0`, e.g. after `v_cmpx_*` without a branch) or inactive lanes carrying stale/fallback descriptor indices (`Source::LanePhi` / `Source::Fetched`) still read `VIEW_READS` and set read-mark bits for texture slots that no active lane actually read, causing unnecessary demand materialization of unread placed textures. ## Changes - `crates/victory-shader/src/emit/lower.rs`: - Return the instruction's `Option<V>` per-lane `guard` from `Lower::image_op` and pass it from `Lower::image` into `Lower::mark_view_read(index, guard)`. - Wrap the bitmap index computation, `raw_dword` load, clear check, and inner `atomicBinOp.i32` in `begin_guarded(guard)` / `end_guarded(outer, &[])` (leaving unguarded `guard == None` paths completely unchanged). - `crates/victory-shader/src/emit/memory_tests.rs`: - Add `heap_direct_texture_reads_skip_marks_when_exec_is_zero` (`v_cmpx_ne_u32 exec_lo, v0, v0`, asserting `marked == []`) and `heap_direct_texture_reads_mark_with_partial_exec` (`v_cmpx_eq_u32 exec_lo, 0, v0` across 2 threads, asserting only `[VIEW_READS + 1]`). ## Verification - Negative control (before fix): `heap_direct_texture_reads_skip_marks_when_exec_is_zero` fails on both RTX 5090 and WARP, recording `[VIEW_READS + 1]` instead of `[]`. - After fix: `emit::memory_tests` (6/6 on WARP, all 3 heap-direct texture read tests on both RTX 5090 and WARP with `dxil.dll` validation) pass. - `cargo clippy -p victory-shader --all-targets -- -D warnings` — pass (0 warnings). - `cargo xtask policy` — pass (`184 files ok`). Note: `memory_tests.rs` uses `vec![0u32; shader.user_data_dwords().max(1) as usize]` for the test helper's user-data slice so the test runs cleanly on NVIDIA independently of the host-side fix in #2587 / #2596 (#2571); it merges cleanly with either.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:11:37 +00:00
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 17:55 UTC · original

Reviewed: marking view reads under the EXEC guard is the correct fix — a texture read on an inactive lane was previously marking its view as read, which feeds copy decisions. The lowered? propagation preserves the old error behavior (marks only on success) and the outer guard wraps the atomic marking block. CI green.

**@⁠kotekompotek** on GitHub · 2026-11-25 17:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/2668#issuecomment-7170201030) Reviewed: marking view reads under the EXEC guard is the correct fix — a texture read on an inactive lane was previously marking its view as read, which feeds copy decisions. The `lowered?` propagation preserves the old error behavior (marks only on success) and the outer guard wraps the atomic marking block. CI green.
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-25 17:58 UTC · original

Independent Windows verification of PR #2668 (head 12fdbc9), Rust/Cargo 1.100.0 MSVC, synthetic tests only, no game data, no source edits.

  • cargo test --locked -p victory-shader --lib heap_direct_texture_reads -- --test-threads=1 --skip heap_direct_texture_reads_mark_their_heap_index on the PR head: 2 passed, 0 failed, 273 filtered. Both new tests pass: emit::memory_tests::heap_direct_texture_reads_skip_marks_when_exec_is_zero and emit::memory_tests::heap_direct_texture_reads_mark_with_partial_exec.
  • Negative control on merge base 13f91ca with the same filter: 0 tests (the new tests are absent).
  • Full crate with the same skip: cargo test --locked -p victory-shader --lib -- --test-threads=1 --skip heap_direct_texture_reads_mark_their_heap_index -> 268 passed, 3 failed, 3 ignored, 1 filtered. The three failures are the known hardware-specific ones (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); they reproduce with identical assertions on unmodified base 13f91ca, so they are not regressions from this PR.
  • The skipped sibling heap_direct_texture_reads_mark_their_heap_index terminates with STATUS_ACCESS_VIOLATION on both this head and unmodified base, so it was excluded.

No regression attributable to the PR found. AI-assisted (Hy4 flash).

**@⁠cloph-dsp** on GitHub · 2026-11-25 17:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/2668#issuecomment-7170263984) Independent Windows verification of PR #2668 (head `12fdbc9`), Rust/Cargo 1.100.0 MSVC, synthetic tests only, no game data, no source edits. - `cargo test --locked -p victory-shader --lib heap_direct_texture_reads -- --test-threads=1 --skip heap_direct_texture_reads_mark_their_heap_index` on the PR head: **2 passed**, 0 failed, 273 filtered. Both new tests pass: `emit::memory_tests::heap_direct_texture_reads_skip_marks_when_exec_is_zero` and `emit::memory_tests::heap_direct_texture_reads_mark_with_partial_exec`. - Negative control on merge base `13f91ca` with the same filter: **0 tests** (the new tests are absent). - Full crate with the same skip: `cargo test --locked -p victory-shader --lib -- --test-threads=1 --skip heap_direct_texture_reads_mark_their_heap_index` -> **268 passed**, 3 failed, 3 ignored, 1 filtered. The three failures are the known hardware-specific ones (`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`); they reproduce with identical assertions on unmodified base `13f91ca`, so they are not regressions from this PR. - The skipped sibling `heap_direct_texture_reads_mark_their_heap_index` terminates with `STATUS_ACCESS_VIOLATION` on both this head and unmodified base, so it was excluded. No regression attributable to the PR found. AI-assisted (Hy4 flash).
Author
Owner

@⁠Mericas77 on GitHub · 2026-11-25 18:28 UTC · original

Tested on NVIDIA: RTX 4070 (the only adapter), driver 621.01, Windows 11, rustc 1.98.1. Head 12fdbc9 against main 13f91ca.

  • cargo test -p victory-shader --lib memory_tests -- --test-threads=1, without any skip: 6/6 pass, hardware and WARP, including the two new tests and heap_direct_texture_reads_mark_their_heap_index. On main that last test alone still ends the process with 0xc0000005 here (#2571).
  • Same with #2596 (577839a) cherry-picked on top: 6/6.
  • cargo test -p victory-shader --lib --locked (with --skip heap_direct_texture_reads_mark_their_heap_index, which crashes the process on main here, #2571): main 266 passed / 3 failed / 3 ignored, this PR 268 / 3 / 3. The 3 failures are the known NVIDIA ones on main (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) and are the same.

One thing worth saying in the description: the test no longer crashes on NVIDIA because of the ud = vec![0u32; shader.user_data_dwords().max(1) as usize] change in memory_tests.rs, which works around #2571 in this one test. The host-side fixes (#2587, #2596) do the same for every caller; with either of them merged, the .max(1) is no longer needed. The two don't conflict (I tested the combination).

No game data; the title was not run.

AI-assisted.

**@⁠Mericas77** on GitHub · 2026-11-25 18:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/2668#issuecomment-7170951723) Tested on NVIDIA: RTX 4070 (the only adapter), driver 621.01, Windows 11, rustc 1.98.1. Head `12fdbc9` against `main` `13f91ca`. - `cargo test -p victory-shader --lib memory_tests -- --test-threads=1`, **without** any skip: **6/6 pass**, hardware and WARP, including the two new tests and `heap_direct_texture_reads_mark_their_heap_index`. On `main` that last test alone still ends the process with `0xc0000005` here (#2571). - Same with #2596 (`577839a`) cherry-picked on top: 6/6. - `cargo test -p victory-shader --lib --locked` (with `--skip heap_direct_texture_reads_mark_their_heap_index`, which crashes the process on `main` here, #2571): `main` 266 passed / 3 failed / 3 ignored, this PR 268 / 3 / 3. The 3 failures are the known NVIDIA ones on `main` (`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`) and are the same. One thing worth saying in the description: the test no longer crashes on NVIDIA because of the `ud = vec![0u32; shader.user_data_dwords().max(1) as usize]` change in `memory_tests.rs`, which works around #2571 in this one test. The host-side fixes (#2587, #2596) do the same for every caller; with either of them merged, the `.max(1)` is no longer needed. The two don't conflict (I tested the combination). No game data; the title was not run. AI-assisted.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 22:23 UTC · original

Automated triage for the maintainers.

Merge candidate. Lower::image (crates/victory-shader/src/emit/lower.rs) now passes the instruction's EXEC guard (returned by image_op) to mark_view_read, which wraps its bitmap load and atomic OR in begin_guarded/end_guarded, as mark_view_write already does. Lanes that are off no longer mark texture slots as read, so placed textures nobody read are not made on demand. Active lanes still mark, so nothing that is read can go unmarked, and marks are still only emitted when lowering succeeds. The guarded blocks nest and close in reverse order, and the BVH path returns no guard because it uses no views. The two new tests check zero EXEC and partial EXEC; the existing test moves into a shared helper. The helper's .max(1) on the user-data size sidesteps #2571 for this test only. It merges cleanly; CI check: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 22:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/2668#issuecomment-7176306685) <!-- victory-triage v1 head=12fdbc9eca24cd4697491aefc4bb012321aa97ca verdict=merge-candidate at=2026-11-25T22:29:30Z --> Automated triage for the maintainers. **Merge candidate.** `Lower::image` (`crates/victory-shader/src/emit/lower.rs`) now passes the instruction's EXEC guard (returned by `image_op`) to `mark_view_read`, which wraps its bitmap load and atomic OR in `begin_guarded`/`end_guarded`, as `mark_view_write` already does. Lanes that are off no longer mark texture slots as read, so placed textures nobody read are not made on demand. Active lanes still mark, so nothing that is read can go unmarked, and marks are still only emitted when lowering succeeds. The guarded blocks nest and close in reverse order, and the BVH path returns no guard because it uses no views. The two new tests check zero EXEC and partial EXEC; the existing test moves into a shared helper. The helper's `.max(1)` on the user-data size sidesteps #2571 for this test only. It merges cleanly; CI `check`: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠KainanP on GitHub · 2026-11-25 22:53 UTC · original

Measured in the #2571 comparison: the crash is avoided only in the rewritten test (#2587's host-level test still crashes on an RTX 4060), and the EXEC-guard change is independent and adds no failures: #2571 (AI-assisted)

**@⁠KainanP** on GitHub · 2026-11-25 22:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/2668#issuecomment-7176914150) Measured in the #2571 comparison: the crash is avoided only in the rewritten test (#2587's host-level test still crashes on an RTX 4060), and the EXEC-guard change is independent and adds no failures: https://git.gtahub.st/github/Marvisak--VIctory/issues/2571 (AI-assisted)
Author
Owner

@⁠rezioliver-collab on GitHub · 2026-11-26 00:17 UTC · original

With #2668 and #2724 combined, disabling EXEC left VIEW_READS empty on both arms of a mixed descriptor phi. With EXEC enabled, only the slot actually read was marked, including when the fixed arm was taken.

For a table starting at heap slot 30 and a runtime index of 5, the fixed arm selects slot 31 and the computed arm selects slot 37. +n means that only bit VIEW_READS + n was set.

Selected phi arm EXEC #2724 alone (569aa6ad) #2668 alone (12fdbc9e) Both PRs
Fixed (slot 31) Off +31 Empty Empty
Computed (slot 37) Off +37 Empty Empty
Fixed (slot 31) On +31 Empty +31
Computed (slot 37) On +37 Empty +37

I tested both PR heads separately and merged them locally. The compiler changes merged cleanly. Only the shared helper in emit/memory_tests.rs needed a conflict resolution. On an Apple M1 (macOS 14.7, Rust 1.98.1), I reused the synthetic Wave32 shader from #2722 and inserted v_cmpx_ne_u32 immediately before image_load. A runtime input toggled EXEC independently of the branch and index, so both mask states ran the same emitted code.

I replayed that code on the CPU with a distinct texel per slot and checked the entire bitmap. Across two table bases and three runtime indices, all 24 masked invocations of the combined mixed-phi shader made zero bitmap loads and zero atomic ORs. With EXEC enabled, the expected texel reached shared memory and a second read of the same slot issued no further atomic OR. The CPU replay still invoked textureLoad with EXEC off, but its result never reached shared memory. These measurements cover shader-side marking. D3D12 device traffic was outside the test.

**@⁠rezioliver-collab** on GitHub · 2026-11-26 00:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/2668#issuecomment-7178600177) **With #2668 and #2724 combined, disabling `EXEC` left `VIEW_READS` empty on both arms of a mixed descriptor phi. With `EXEC` enabled, only the slot actually read was marked, including when the fixed arm was taken.** For a table starting at heap slot 30 and a runtime index of 5, the fixed arm selects slot 31 and the computed arm selects slot 37. `+n` means that only bit `VIEW_READS + n` was set. | Selected phi arm | `EXEC` | #2724 alone (`569aa6ad`) | #2668 alone (`12fdbc9e`) | Both PRs | |---|---|---|---|---| | Fixed (slot 31) | Off | `+31` | Empty | **Empty** | | Computed (slot 37) | Off | `+37` | Empty | **Empty** | | Fixed (slot 31) | On | `+31` | Empty | **`+31`** | | Computed (slot 37) | On | `+37` | Empty | **`+37`** | I tested both PR heads separately and merged them locally. The compiler changes merged cleanly. Only the shared helper in `emit/memory_tests.rs` needed a conflict resolution. On an Apple M1 (macOS 14.7, Rust 1.98.1), I reused the synthetic Wave32 shader from #2722 and inserted `v_cmpx_ne_u32` immediately before `image_load`. A runtime input toggled `EXEC` independently of the branch and index, so both mask states ran the same emitted code. I replayed that code on the CPU with a distinct texel per slot and checked the entire bitmap. Across two table bases and three runtime indices, **all 24 masked invocations of the combined mixed-phi shader made zero bitmap loads and zero atomic ORs**. With `EXEC` enabled, the expected texel reached shared memory and a second read of the same slot issued no further atomic OR. The CPU replay still invoked `textureLoad` with `EXEC` off, but its result never reached shared memory. These measurements cover shader-side marking. D3D12 device traffic was outside the test.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2668/head:github-bot/gh-2668'
git switch 'github-bot/gh-2668'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2668'
git switch 'github-bot/gh-2668'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2668'
git switch 'github-bot/gh-2668'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2668'
git switch 'main'
git merge --squash 'github-bot/gh-2668'
git switch 'main'
git merge --ff-only 'github-bot/gh-2668'
git switch 'main'
git merge 'github-bot/gh-2668'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2668
No description provided.