Shaders: narrow mesh output bounds from GS_ALLOC_REQ M0 analysis (#201) #1385

Open
github-bot wants to merge 2 commits from github-bot/gh-1385 into main AGit
Owner

@⁠Ctrl-Alt-Haze on GitHub · 2026-11-24 17:40 UTC · original
Ctrl-Alt-Haze:fix/mesh-gs-alloc-req-bounds → main · +1602 −39 · 3 files · 2 commits · open

Summary

Fixes #201 (mesh shaders exceeding the 32,768 B DXIL output limit because declared PSV0 max_vertices / max_primitives bounds remain at worst-case 256/256 instead of the proven s_sendmsg(MSG_GS_ALLOC_REQ) M0 allocation bounds).

(Note: #586 has merge conflicts and touches 22 files; this PR is a focused 3-file implementation cleanly on ffe310eb.)

Changes

  • crates/victory-shader/src/emit/lower.rs:
    • Added ScalarBound fixpoint analysis + slice_range_val ((m0 >> shift) & 0x3FF) across s_mov_b32, s_movk_i32, s_or_b32, s_lshl_b32, s_lshr_b32, s_and_b32, s_bfe_u32, s_min_u32, s_max_u32, s_add_*, s_sub_*, s_cselect_b32, s_cmov_b32/s_cmovk_i32, and Phi joins (with widening on loop phis).
    • Narrowed shader::Mesh.max_vertices / max_primitives to the conservative max across all s_sendmsg(GS_ALLOC_REQ) sites only when every site is proven <= 256 (clamped to ms.max_vertices / ms.max_primitives and 256; left unchanged when 0 sites or any site is unproven).
    • Added runtime M0 lowering for setMeshOutputCounts(min(v, max_v), min(p, max_p)) (inline at block-0 GS_ALLOC_REQ, or pure-SOP materialization hoisted to block-0 end for lane-0 guarded GS_ALLOC_REQ), plus mesh_export_limits (MeshExportLimit::Const / Dynamic) so mesh_export_guard checks threads > emitted_limit even when mesh.max_* stays unnarrowed.
    • Added s_cmov_b32/s_cmovk_i32 lowering in sop(). Zero unsafe blocks added.
  • crates/victory-shader/src/emit/mod.rs & crates/victory-shader/src/emit/mesh_tests.rs:
    • Added 7 synthetic GFX10 mesh shader tests covering >32 KB dxil.dll rejection without narrowing vs DXIL validation + RTX 5070 Ti & WARP Mesh Shader PSO + DispatchMesh execution for literal M0, s_min_u32-bounded dynamic M0, lane-0 guarded dynamic M0, s_and_b32/s_bfe_u32/s_cselect_b32/Phi narrowing, mixed unbounded-vertex + small-constant-primitive export guarding, and negative soundness controls (unbounded M0, & 0x3FF, unbounded Phi, clamping, multi-site max, fail-open when any site is unproven).

Verification

  • Hardware / Toolchain: Windows x64, NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.2135), WARP (VICTORY_WARP=1 / VICTORY_D3D12_WARP=1), Windows SDK 10.0.26100.0 dxil.dll, Rust 1.98.1, base ffe310eb.
  • cargo xtask policy: 155 files ok
  • cargo clippy --workspace --all-targets --locked -- -D warnings (default & --all-features): passed
  • cargo test --workspace --locked: 349 passed, 0 failed, 6 ignored (victory-shader 127/1 ign)
  • cargo test -p victory-shader --features host --locked (RTX 5070 Ti): 127 passed, 0 failed, 1 ignored
  • VICTORY_WARP=1 cargo test -p victory-shader --features host --locked (WARP): 127 passed, 0 failed, 1 ignored
  • cargo test -p victory-gpu --features diagnostics --locked & VICTORY_D3D12_WARP=1: 53 passed, 0 failed, 1 ignored each
  • rustfmt --edition 2024 --check on modified files: zero diffs on mesh_tests.rs and all added/modified lines in lower.rs and mod.rs.
  • Not tested: retail title corpus (no retail game assets used; synthetic tests only).

AI use: Written and verified locally with an AI coding swarm (gemini-4-hydrogen-high), then independently audited and tested on RTX 5070 Ti + WARP.

**@⁠Ctrl-Alt-Haze** on GitHub · 2026-11-24 17:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/1385) `Ctrl-Alt-Haze:fix/mesh-gs-alloc-req-bounds` → `main` · +1602 −39 · 3 files · 2 commits · open ## Summary Fixes #201 (mesh shaders exceeding the 32,768 B DXIL output limit because declared `PSV0` `max_vertices` / `max_primitives` bounds remain at worst-case 256/256 instead of the proven `s_sendmsg(MSG_GS_ALLOC_REQ)` `M0` allocation bounds). *(Note: #586 has merge conflicts and touches 22 files; this PR is a focused 3-file implementation cleanly on `ffe310eb`.)* ## Changes - `crates/victory-shader/src/emit/lower.rs`: - Added `ScalarBound` fixpoint analysis + `slice_range_val` (`(m0 >> shift) & 0x3FF`) across `s_mov_b32`, `s_movk_i32`, `s_or_b32`, `s_lshl_b32`, `s_lshr_b32`, `s_and_b32`, `s_bfe_u32`, `s_min_u32`, `s_max_u32`, `s_add_*`, `s_sub_*`, `s_cselect_b32`, `s_cmov_b32`/`s_cmovk_i32`, and `Phi` joins (with widening on loop phis). - Narrowed `shader::Mesh.max_vertices` / `max_primitives` to the conservative max across all `s_sendmsg(GS_ALLOC_REQ)` sites only when every site is proven `<= 256` (clamped to `ms.max_vertices` / `ms.max_primitives` and `256`; left unchanged when 0 sites or any site is unproven). - Added runtime `M0` lowering for `setMeshOutputCounts(min(v, max_v), min(p, max_p))` (inline at block-0 `GS_ALLOC_REQ`, or pure-SOP materialization hoisted to block-0 end for lane-0 guarded `GS_ALLOC_REQ`), plus `mesh_export_limits` (`MeshExportLimit::Const` / `Dynamic`) so `mesh_export_guard` checks `threads > emitted_limit` even when `mesh.max_*` stays unnarrowed. - Added `s_cmov_b32`/`s_cmovk_i32` lowering in `sop()`. Zero `unsafe` blocks added. - `crates/victory-shader/src/emit/mod.rs` & `crates/victory-shader/src/emit/mesh_tests.rs`: - Added 7 synthetic GFX10 mesh shader tests covering >32 KB `dxil.dll` rejection without narrowing vs DXIL validation + RTX 5070 Ti & WARP Mesh Shader PSO + `DispatchMesh` execution for literal `M0`, `s_min_u32`-bounded dynamic `M0`, lane-0 guarded dynamic `M0`, `s_and_b32`/`s_bfe_u32`/`s_cselect_b32`/`Phi` narrowing, mixed unbounded-vertex + small-constant-primitive export guarding, and negative soundness controls (unbounded `M0`, `& 0x3FF`, unbounded `Phi`, clamping, multi-site max, fail-open when any site is unproven). ## Verification - **Hardware / Toolchain**: Windows x64, NVIDIA GeForce RTX 5070 Ti (driver `32.0.16.2135`), WARP (`VICTORY_WARP=1` / `VICTORY_D3D12_WARP=1`), Windows SDK `10.0.26100.0` `dxil.dll`, Rust `1.98.1`, base `ffe310eb`. - `cargo xtask policy`: `155 files ok` - `cargo clippy --workspace --all-targets --locked -- -D warnings` (default & `--all-features`): passed - `cargo test --workspace --locked`: `349 passed, 0 failed, 6 ignored` (`victory-shader` `127/1 ign`) - `cargo test -p victory-shader --features host --locked` (RTX 5070 Ti): `127 passed, 0 failed, 1 ignored` - `VICTORY_WARP=1 cargo test -p victory-shader --features host --locked` (WARP): `127 passed, 0 failed, 1 ignored` - `cargo test -p victory-gpu --features diagnostics --locked` & `VICTORY_D3D12_WARP=1`: `53 passed, 0 failed, 1 ignored` each - `rustfmt --edition 2024 --check` on modified files: zero diffs on `mesh_tests.rs` and all added/modified lines in `lower.rs` and `mod.rs`. - **Not tested**: retail title corpus (no retail game assets used; synthetic tests only). AI use: Written and verified locally with an AI coding swarm (`gemini-4-hydrogen-high`), then independently audited and tested on RTX 5070 Ti + WARP.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:29:50 +00:00
Author
Owner

@⁠Ctrl-Alt-Haze on GitHub · 2026-11-24 18:28 UTC · original

@⁠catminou4 Thanks for reviewing and testing on macOS arm64!

**@⁠Ctrl-Alt-Haze** on GitHub · 2026-11-24 18:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/1385#issuecomment-7140699991) @⁠catminou4 Thanks for reviewing and testing on macOS arm64!
Author
Owner

@⁠marquezinii on GitHub · 2026-11-24 18:48 UTC · original

Ran on an RTX 5070 (desktop, driver 621.40, the only adapter), Windows 11 26300, Rust 1.98.1 at 9e77b5c. cargo test -p victory-shader --lib -- --test-threads=1 --nocapture gives 127 passed, 0 failed, 1 ignored. All seven mesh_tests pass, including the two that create a mesh PSO and draw (s_min_u32_dynamic_m0_narrows_below_32kb_and_creates_pso, guarded_lane0_dynamic_m0_materializes_and_creates_pso), on the hardware adapter and WARP.

Not covered here: whether this clears the 53 programs from #201 that #459 left at 53. That needs the title's shaders.rpf, and I don't have it. A translate --validate count from someone with the pack would show how many of the 53 the M0 analysis proves.

**@⁠marquezinii** on GitHub · 2026-11-24 18:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/1385#issuecomment-7141174180) Ran on an RTX 5070 (desktop, driver 621.40, the only adapter), Windows 11 26300, Rust 1.98.1 at `9e77b5c`. `cargo test -p victory-shader --lib -- --test-threads=1 --nocapture` gives 127 passed, 0 failed, 1 ignored. All seven `mesh_tests` pass, including the two that create a mesh PSO and draw (`s_min_u32_dynamic_m0_narrows_below_32kb_and_creates_pso`, `guarded_lane0_dynamic_m0_materializes_and_creates_pso`), on the hardware adapter and WARP. Not covered here: whether this clears the 53 programs from #201 that #459 left at 53. That needs the title's `shaders.rpf`, and I don't have it. A `translate --validate` count from someone with the pack would show how many of the 53 the M0 analysis proves.
Author
Owner

@⁠Ctrl-Alt-Haze on GitHub · 2026-11-24 18:57 UTC · original

@⁠marquezinii Thanks for verifying 9e77b5c on your desktop RTX 5070 + WARP (including both live mesh PSO draw tests)!

**@⁠Ctrl-Alt-Haze** on GitHub · 2026-11-24 18:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/1385#issuecomment-7141396045) @⁠marquezinii Thanks for verifying `9e77b5c` on your desktop RTX 5070 + WARP (including both live mesh PSO draw tests)!
Author
Owner

@⁠Ctrl-Alt-Haze on GitHub · 2026-11-24 21:54 UTC · original

Rebased onto upstream main (70564ae) at 3e630d3 cleanly (resolved crates/victory-shader/src/emit/mod.rs).

Verified locally on Windows 11 x64 / NVIDIA GeForce RTX 5070 Ti + WARP:

  • cargo xtask policy: passed (175 files ok)
  • cargo clippy --workspace --all-targets --locked -- -D warnings (default & --all-features): passed
  • victory-shader targeted mesh checks (mesh_tests, translated_mesh_shaders_draw_as_pc_ones): passed
  • cargo test -p victory-shader --features host --locked (hardware): 213 passed; 3 failed; 2 ignored (the 3 failures are pre-existing on clean upstream/main 70564ae on RTX 5070 Ti: stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, synthetic_tests::gathers_at_a_level_match_the_hardware)
  • VICTORY_WARP=1 cargo test -p victory-shader --features host --locked: 214 passed; 2 failed; 2 ignored (pre-existing gathers_at_a_level_match_the_hardware, plus permutes_read_the_lane_named_and_zero_from_lanes_left_out which skips under VICTORY_WARP=1 and passed on RTX hardware)
  • cargo test -p victory-gpu --locked -- --test-threads=1: 76 passed; 2 failed; 1 ignored (both failures are pre-existing on clean 70564ae: dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views)
**@⁠Ctrl-Alt-Haze** on GitHub · 2026-11-24 21:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/1385#issuecomment-7145552286) Rebased onto upstream `main` (`70564ae`) at `3e630d3` cleanly (resolved `crates/victory-shader/src/emit/mod.rs`). Verified locally on Windows 11 x64 / NVIDIA GeForce RTX 5070 Ti + WARP: - `cargo xtask policy`: passed (175 files ok) - `cargo clippy --workspace --all-targets --locked -- -D warnings` (default & `--all-features`): passed - `victory-shader` targeted mesh checks (`mesh_tests`, `translated_mesh_shaders_draw_as_pc_ones`): passed - `cargo test -p victory-shader --features host --locked` (hardware): `213 passed; 3 failed; 2 ignored` (the 3 failures are pre-existing on clean `upstream/main` `70564ae` on RTX 5070 Ti: `stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride`, `stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros`, `synthetic_tests::gathers_at_a_level_match_the_hardware`) - `VICTORY_WARP=1 cargo test -p victory-shader --features host --locked`: `214 passed; 2 failed; 2 ignored` (pre-existing `gathers_at_a_level_match_the_hardware`, plus `permutes_read_the_lane_named_and_zero_from_lanes_left_out` which skips under `VICTORY_WARP=1` and passed on RTX hardware) - `cargo test -p victory-gpu --locked -- --test-threads=1`: `76 passed; 2 failed; 1 ignored` (both failures are pre-existing on clean `70564ae`: `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views`)
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 02:38 UTC · original · edited 2026-11-25 03:22 UTC

Automated triage for the maintainers.

Proposed close: no measured value. This adds about 1,100 lines to crates/victory-shader/src/emit/lower.rs (ScalarBound, analyze_mesh_alloc, block-0 materialization) to narrow setMeshOutputCounts from M0, aimed at the over-32 KiB mesh programs in #201. The author's own translate --validate census on the full shader pack, against main 70564ae, shows the same counts before and after: 8,694 mesh programs validated, 71 over-limit failures, and no program gained or lost. The runtime approach to the allocation request is in #586. CI has not run on this head.

On your reply: thank you for running the census and citing the M0 layout sources. With none of the 71 failures cleared, the analysis does not pay for its size, and closing it as you suggest is the right call. The M0 layout references would help in #586.

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


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 02:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/1385#issuecomment-7150983655) · edited 2026-11-25 03:22 UTC <!-- victory-triage v1 head=3e630d357c13e5fc4eeeb1bc0d3c9a9f8cc2310c verdict=proposed-close at=2026-11-25T03:22:30Z --> Automated triage for the maintainers. **Proposed close: no measured value.** This adds about 1,100 lines to `crates/victory-shader/src/emit/lower.rs` (`ScalarBound`, `analyze_mesh_alloc`, block-0 materialization) to narrow `setMeshOutputCounts` from M0, aimed at the over-32 KiB mesh programs in #201. The author's own `translate --validate` census on the full shader pack, against `main` 70564ae, shows the same counts before and after: 8,694 mesh programs validated, 71 over-limit failures, and no program gained or lost. The runtime approach to the allocation request is in #586. CI has not run on this head. On your reply: thank you for running the census and citing the M0 layout sources. With none of the 71 failures cleared, the analysis does not pay for its size, and closing it as you suggest is the right call. The M0 layout references would help in #586. This pull request will be closed in 48 hours unless the author pushes a change or a maintainer removes the `triage: proposed close` label. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠Ctrl-Alt-Haze on GitHub · 2026-11-25 03:07 UTC · original

Thanks for the detailed triage review. I ran the clean-room victory translate --validate before/after census on a 1.0.2.1 shaders.rpf (26,503 containers / 26,084 programs) comparing 70564ae against #1385 (3e630d3):

Build Mesh emitted Mesh validated > 32768 mesh validator failures Total validated (all stages)
Base (70564ae) 8,765 8,694 71 24,785
PR #1385 (3e630d3) 8,765 8,694 71 24,785

0 gained, 0 lost across all 26,084 programs (counts only; no shader names or data included per docs/asset-policy.md).

Addressing your three points:

  1. M0 layout & hoisting soundness:
    • Source: AMDGPU::SendMsg::ID_GS_ALLOC_REQ = 9 in LLVM (SIDefines.h) and Mesa's NGG lowering (ac_nir_lower_ngg.c), which pack (num_verts) | (num_prims << 12) into M0 (GS_ALLOC_COUNT_MASK 0x3FF for the 10-bit 0..1023 RDNA2 count range vs 0xFFF 12-bit slot width; GS_ALLOC_PRIM_SHIFT = 12).
    • Block-0 hoisting (emit_mesh_counts_block0_end): collect_block0_materialization refuses Phis, EXEC reads (s_and_saveexec, etc.), st.masks/st.highs, memory/descriptor loads, and any non-pure SOP ops, accepting only acyclic is_pure_32bit_sop ALU ops rooted at entry inputs or block-0 definitions — so even when s_sendmsg(GS_ALLOC_REQ) itself sits inside a lane-0-guarded block, the hoisted scalar dependency slice is uniform and lane-independent.
  2. Real corpus impact on #201: The measurement above confirms ScalarBound static SOP M0 narrowing clears 0 / 71 of the > 32768 mesh failures on 70564ae. That means carrying ~1,100 lines of static interval analysis and M0 materialization in lower.rs is not justified by the corpus data.
  3. Overlap with #586: Runtime s_sendmsg(GS_ALLOC_REQ) lowering belongs with #586 (uniform join + LDS staging), rather than static ScalarBound metadata narrowing.
  4. Minor doc note: Noted on the GPU model name in crates/victory-shader/src/emit/mesh_tests.rs.

Since #1385 clears 0 real > 32 KiB mesh programs on shaders.rpf, I don't think it is worth merging as-is and am happy to close it rather than ask maintainers for more review cycles unless you see another use for it.

**@⁠Ctrl-Alt-Haze** on GitHub · 2026-11-25 03:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/1385#issuecomment-7151620732) Thanks for the detailed triage review. I ran the clean-room `victory translate --validate` before/after census on a 1.0.2.1 `shaders.rpf` (`26,503` containers / `26,084` programs) comparing `70564ae` against `#1385` (`3e630d3`): | Build | Mesh emitted | Mesh validated | `> 32768` mesh validator failures | Total validated (all stages) | |---|---:|---:|---:|---:| | Base (`70564ae`) | 8,765 | 8,694 | 71 | 24,785 | | PR `#1385` (`3e630d3`) | 8,765 | 8,694 | 71 | 24,785 | **`0` gained, `0` lost** across all 26,084 programs (counts only; no shader names or data included per `docs/asset-policy.md`). Addressing your three points: 1. **M0 layout & hoisting soundness:** - **Source:** `AMDGPU::SendMsg::ID_GS_ALLOC_REQ = 9` in LLVM (`SIDefines.h`) and Mesa's NGG lowering (`ac_nir_lower_ngg.c`), which pack `(num_verts) | (num_prims << 12)` into `M0` (`GS_ALLOC_COUNT_MASK` `0x3FF` for the 10-bit 0..1023 RDNA2 count range vs `0xFFF` 12-bit slot width; `GS_ALLOC_PRIM_SHIFT = 12`). - **Block-0 hoisting (`emit_mesh_counts_block0_end`):** `collect_block0_materialization` refuses `Phi`s, `EXEC` reads (`s_and_saveexec`, etc.), `st.masks`/`st.highs`, memory/descriptor loads, and any non-pure SOP ops, accepting only acyclic `is_pure_32bit_sop` ALU ops rooted at entry inputs or block-0 definitions — so even when `s_sendmsg(GS_ALLOC_REQ)` itself sits inside a lane-0-guarded block, the hoisted scalar dependency slice is uniform and lane-independent. 2. **Real corpus impact on `#201`:** The measurement above confirms `ScalarBound` static SOP `M0` narrowing clears **`0 / 71`** of the `> 32768` mesh failures on `70564ae`. That means carrying ~1,100 lines of static interval analysis and `M0` materialization in `lower.rs` is not justified by the corpus data. 3. **Overlap with `#586`:** Runtime `s_sendmsg(GS_ALLOC_REQ)` lowering belongs with `#586` (uniform join + LDS staging), rather than static `ScalarBound` metadata narrowing. 4. **Minor doc note:** Noted on the GPU model name in `crates/victory-shader/src/emit/mesh_tests.rs`. Since `#1385` clears 0 real `> 32 KiB` mesh programs on `shaders.rpf`, I don't think it is worth merging as-is and am happy to close it rather than ask maintainers for more review cycles unless you see another use for it.
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/1385/head:github-bot/gh-1385'
git switch 'github-bot/gh-1385'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1385'
git switch 'github-bot/gh-1385'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1385'
git switch 'github-bot/gh-1385'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1385'
git switch 'main'
git merge --squash 'github-bot/gh-1385'
git switch 'main'
git merge --ff-only 'github-bot/gh-1385'
git switch 'main'
git merge 'github-bot/gh-1385'
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!1385
No description provided.