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

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/2668 into main
fuzeofficlal commented 2026-11-25 17:47:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T17:47:17Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6610333851 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2668). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T17:47:17Z. --- ## 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.
kotekompotek commented 2026-11-25 18:02:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T17:55:55Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7170201030 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2668#issuecomment-7170201030). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T17:55:55Z. --- 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.
cloph-dsp commented 2026-11-25 18:02:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-25T17:58:37Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7170263984 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2668#issuecomment-7170263984). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-25T17:58:37Z. --- 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).
Mericas77 commented 2026-11-25 19:06:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Mericas77 on 2026-11-25T18:28:16Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7170951723 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2668#issuecomment-7170951723). Originally posted by [Mericas77](https://github.com/Mericas77) on 2026-11-25T18:28:16Z. --- 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.
Marvisak commented 2026-11-25 23:06:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:23:39Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7176306685 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2668#issuecomment-7176306685). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T22:23:39Z. --- <!-- 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)_
KainanP commented 2026-11-25 23:06:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-25T22:53:54Z.


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://github.com/Marvisak/VIctory/issues/2571#issuecomment-7176908308 (AI-assisted)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7176914150 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2668#issuecomment-7176914150). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-25T22:53:54Z. --- 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://github.com/Marvisak/VIctory/issues/2571#issuecomment-7176908308 (AI-assisted)
rezioliver-collab commented 2026-11-26 01:03:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by rezioliver-collab on 2026-11-26T00:17:21Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7178600177 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2668#issuecomment-7178600177). Originally posted by [rezioliver-collab](https://github.com/rezioliver-collab) on 2026-11-26T00:17:21Z. --- **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.
Dukestar69 commented 2026-11-26 05:02:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T04:27:35Z.

Review: APPROVED


Tested merged onto origin/main (63bcca4) on Windows 11 x64 (dxil.dll validator active):

  • VICTORY_WARP=1 cargo test -p victory-shader --lib emit::memory_tests: 7 passed, 0 failed (including new heap_direct_texture_reads_skip_marks_when_exec_is_zero and heap_direct_texture_reads_mark_with_partial_exec; WARP used due to pre-existing #2571 on NVIDIA hardware).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6068551280 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2668#pullrequestreview-6068551280). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T04:27:35Z. **Review:** APPROVED --- Tested merged onto `origin/main` (`63bcca4`) on Windows 11 x64 (`dxil.dll` validator active): - `VICTORY_WARP=1 cargo test -p victory-shader --lib emit::memory_tests`: **7 passed, 0 failed** (including new `heap_direct_texture_reads_skip_marks_when_exec_is_zero` and `heap_direct_texture_reads_mark_with_partial_exec`; WARP used due to pre-existing `#2571` on NVIDIA hardware). - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2668:upstream-pr/2668'
git switch 'upstream-pr/2668'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2668'
git switch 'upstream-pr/2668'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2668'
git switch 'upstream-pr/2668'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2668'
git switch 'main'
git merge --squash 'upstream-pr/2668'
git switch 'main'
git merge --ff-only 'upstream-pr/2668'
git switch 'main'
git merge 'upstream-pr/2668'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!2666
No description provided.