Skip to content

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

Open
Ctrl-Alt-Haze wants to merge 2 commits into
Marvisak:mainfrom
Ctrl-Alt-Haze:fix/mesh-gs-alloc-req-bounds
Open

Ctrl-Alt-Haze wants to merge 2 commits into
Marvisak:mainfrom
Ctrl-Alt-Haze:fix/mesh-gs-alloc-req-bounds

Conversation

@Ctrl-Alt-Haze

Copy link
Copy Markdown

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.

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

Mesh output bounds from GS_ALLOC_REQ M0 analysis. The ScalarBound interval analysis over SOP defs is conservative by construction: phi joins widen and bail to TOP after 4 updates, unknown producers stay TOP, and bounds are only trusted when EVERY s_sendmsg/s_sendmsgalloc site proves vertex/primitive counts <= 256 (MAX_HOST_MESH_COUNT, the D3D12 mesh limit); any unproven site keeps the declared maxima. slice_range_val extracts the M0[9:0]/M0[21:12] fields through the def-use chain with cycle guards. The mesh_tests cover the negative cases (unbounded dynamic M0 keeps full bounds, multiple alloc sites take the max and require all proven). +3 macOS tests green. Local verification: macOS arm64, rustc 1.98.1, PR head merged into main ffe310e, cargo test -p victory-shader --locked. Shader execution tests (valu_tests/host/diff_tests) are #[cfg(all(test, windows))], so macOS covers lowering/decode/unit tests only. 99 passed, 0 failed.

@Ctrl-Alt-Haze

Copy link
Copy Markdown
Author

@catminou4 Thanks for reviewing and testing on macOS arm64!

@marquezinii

Copy link
Copy Markdown
Contributor

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.

@Ctrl-Alt-Haze

Copy link
Copy Markdown
Author

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

@Ctrl-Alt-Haze

Copy link
Copy Markdown
Author

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)

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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

@Ctrl-Alt-Haze

Copy link
Copy Markdown
Author

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.

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.

Corpus census: 185 emitted DXIL modules fail dxil.dll validation

4 participants