Skip to content

Shaders: lower v_add_nc_i32, v_sub_nc_i32, v_mul_lo_i32 and the signed 16-bit add and sub - #92

Open
tedin7 wants to merge 1 commit into
Marvisak:mainfrom
tedin7:vic-vop-nc-i32
Open

tedin7 wants to merge 1 commit into
Marvisak:mainfrom
tedin7:vic-vop-nc-i32

Conversation

@tedin7

@tedin7 tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

What

Lowers v_add_nc_i32, v_sub_nc_i32, v_mul_lo_i32, v_add_nc_i16 and v_sub_nc_i16. They were decoded but refused in emit/lower.rs ("unsupported"), so any shader using one fell back instead of translating.

Why

The low 32 bits of a sum, difference or product do not depend on the sign, so the 32-bit forms lower exactly as v_add_nc_u32, v_sub_nc_u32 and v_mul_lo_u32. The 16-bit forms go through the existing 16-bit integer path, which already sign-extends _i16 operands and keeps the low half (including op_sel and the high-half destination). Clamp (which saturates), abs, neg and omod stay refused rather than miscompiled.

The diff is local: names added to four existing match arms and the 16-bit name list, plus tests.

Tested

Linux x86_64 only:

  • cargo test -j4 -p victory-shader: 54 passed, 0 failed (3 of them new).
  • Before the change the new tests fail (Unsupported { what: "v_add_nc_i32" }, "v_add_nc_i16"); after it they pass.
  • cargo clippy -j4 -p victory-shader --tests: no new warnings (the existing constant_color dead-code warning on non-Windows is untouched).
  • rustfmt --check --edition 2024 on lower.rs: clean.

The new tests build a synthetic pixel program in memory (no game data), lower it, and check that the signed 32-bit forms produce the same DXIL as their unsigned twins, that the 16-bit forms lower with op_sel variants, and that clamp is still refused.

Not run: the Windows runtime, a GPU, DXC validation of the emitted DXIL.

…16, v_sub_nc_i16

These were decoded but refused. The low bits of a sum, difference or product do not depend on the sign, so they lower as their unsigned twins; the 16-bit forms use the existing 16-bit path, which already sign-extends _i16 operands. Clamp, abs and neg are still refused.
@DaviTostes

Copy link
Copy Markdown

Tested 4f2d5db stacked on #39 on Arch Linux x86_64, rustc 1.98.1: pass.

  • cargo test --workspace: 158 passed, 0 failed, 3 ignored; merge with Build: make workspace tests run on Linux #39 clean.
  • The six #[test]s this PR adds for the integer add/sub/mul lowerings are included in that run.
  • No game data used.

AI-assisted (DeepSeek V4.1 Flash coding agent).

@lordon1a

Copy link
Copy Markdown
Contributor

Verified 4f2d5dbd8 on Windows 11 (x86_64-pc-windows-gnullvm, rustc 1.100.0, applied cleanly to main@35b054ec0): pass.

  • cargo xtask policy: 150 files ok
  • cargo test -p victory-shader: 60 passed, 0 failed (+3 new lowering tests for v_add_nc_i32/v_sub_nc_i32/v_mul_lo_i32/16-bit add & sub).

AI-assisted.

@Lyrnc

Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

RDNA2 hardware check: the add/sub forms match, with one note on v_mul_lo_i32.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. Hand-written instructions in a HIP kernel (assembled by the AMD driver's amd_comgr). RDNA2 desktop silicon, not the console's. No game data. 400 operand pairs (edge values 0, 1, 0x7fff, 0x8000, 0xffff, 0x7fffffff, 0x80000000, 0xffffffff, … in all pairs, plus 300 random). The 16-bit destinations were preset to 0xdead0000.

match
v_add_nc_i32, v_sub_nc_i32: wrapping low 32 bits 400/400 each
v_add_nc_i16, v_sub_nc_i16: wrapping low 16 bits, high half kept (0xdead) 400/400 each
for reference, with clamp (which this PR refuses): signed saturation, 32- and 16-bit 400/400 each

v_mul_lo_i32: AMD's own assembler (the driver's amd_comgr) rejects v_mul_lo_i32 for gfx1036 ("instruction not supported on this GPU"). The raw VOP3 opcode 0x16b that our table (from Mesa's GFX10 column) names v_mul_lo_i32 writes 0 on this hardware for all 400 pairs: 0xd56b000c 0x00020b04 with v12 = 0xdeadbeef beforehand leaves v12 = 0. So on RDNA2 desktop parts the opcode is not a multiply. The console may differ, but lowering it as v_mul_lo_u32 means translating an opcode that the RDNA2 tools don't emit. Refusing it until a title is seen using it may be safer.

AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-shader/src/emit/lower.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

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.

5 participants