Skip to content

Shaders: lower v_add_nc_i32, v_sub_nc_i32, v_add_nc_i16, and v_sub_nc_i16 in pure 32-bit DXIL - #2094

Open
geiyiliyabumo wants to merge 1 commit into
Marvisak:mainfrom
geiyiliyabumo:feat/shader-v-add-sub-nc-i
Open

geiyiliyabumo wants to merge 1 commit into
Marvisak:mainfrom
geiyiliyabumo:feat/shader-v-add-sub-nc-i

Conversation

@geiyiliyabumo

Copy link
Copy Markdown
Contributor

Summary

  • Lowers GFX10 VOP3 signed no-carry integer instructions v_add_nc_i32 (0x37f), v_sub_nc_i32 (0x376), v_add_nc_i16 (0x30d), and v_sub_nc_i16 (0x30e) in crates/victory-shader/src/emit/lower.rs using pure 32-bit DXIL without enabling Int64 / WIDE_OPS.
  • Unclamped (clamp = 0) forms wrap naturally; clamped (clamp = 1) forms saturate to signed i32::MIN..=i32::MAX / i16::MIN..=i16::MAX.
  • The 16-bit forms honor op_sel for source and destination half selection while preserving the untouched half of vdst.
  • Adds synthetic DXIL-validation and GPU execution tests in crates/victory-shader/src/emit/valu_tests.rs covering normal results, positive and negative signed overflow, both clamp modes, op_sel half preservation, and verifying Int64 is not set.

Verification

  • cargo xtask policy: passed
  • cargo clippy -p victory-shader --tests -- -D warnings: passed
  • cargo test -p victory-shader --lib v_add_sub_nc_i -- --test-threads=1 with Microsoft dxil.dll: 2/2 passed on AMD Radeon RX 9060 XT (RDNA4) and Microsoft Basic Render Driver (WARP)
  • Zero game files or game-derived data used (synthetic tests only).

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified locally merged onto origin/main (940d32f, Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC, with dxil.dll validator):

  • cargo test -p victory-shader --lib v_add_sub_nc_ — 2 passed, 0 failed on both RTX 5090 hardware and VICTORY_D3D12_WARP=1 (v_add_sub_nc_i32_wrap_and_saturate and v_add_sub_nc_i16_wrap_and_saturate_with_op_sel, both verified INT64-free)
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean pure-32-bit DXIL lowering of v_add_nc_i32, v_sub_nc_i32, v_add_nc_i16, and v_sub_nc_i16 with saturation and op_sel, verified end-to-end on hardware and WARP. Looks good.

AI-assisted (Gemini 4 Neon).

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown

Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at ab4db4d. victory-shader: 245 passed, 4 failed, 3 ignored. Three are #1744's NVIDIA failures on main. The fourth is this PR's new test:

emit::valu_tests::v_add_sub_nc_i16_wrap_and_saturate_with_op_sel
hardware: VOP3 0x30d, clamp true, op_sel 0b0000, case 1
  left: 0xFFFFFFE9   right: 0xA5A5FFE9

The low half is right (-20 + -3 = -23). The high half should be the destination's old 0xA5A5 but comes back sign-extended. The unclamped cases and clamped case 0 (a positive result) pass.

What I checked:

  • The DXIL is right. dxc -dumpbin of that shader shows the clamp select chain, then and %32, 65535, then and %old, -65536, then or, the same tail as the unclamped shader, which passes.
  • WARP passes on this machine. I swapped run_cases to run WARP first, locally. That matches your RX 9060 XT and WARP results. It fails alone, too, without the i32 test in the process.
  • Only NVIDIA drops the mask. Clamping with dx.op IMax/IMin (37/38) instead of compare/select, or clamping on r + 0x8000 in [0, 0xFFFF] and flipping the sign bit back, give the same 0xFFFFFFE9. NVIDIA's compiler appears to fold any of these into a saturating 16-bit add, and then drops write_half's 0xFFFF mask on its sign-extended result. It's a driver bug, but titles run on NVIDIA, so it's worth avoiding.
  • This form passes on NVIDIA and WARP: the selects choose the 16-bit patterns, never a clamped i32. With it, both of your tests pass and the full victory-shader suite is 246 passed with only Test report: RTX 5070 (Blackwell), Windows 11, 70564ae: 471 passed, 5 failed, 7 ignored (4 from the 1,000,000 view heap cap) #1744's 3, the same as main:
@@ -6514,10 +6514,16 @@ impl<'a> Lower<'a> {
                         let r = self.s.m.bin(c, op, a, b);
                         if v.clamp {
+                            // The selects pick the 16-bit patterns, never a clamped i32: NVIDIA
+                            // (616.56) turns a clamped i32 into a saturating 16-bit add whose
+                            // sign-extended result then skips write_half's 0xFFFF mask.
                             let lo = self.s.m.i32(i32::from(i16::MIN) as u32);
                             let hi = self.s.m.i32(i16::MAX as u32);
+                            let mask = self.s.m.i32(0xFFFF);
+                            let low = self.s.m.bin(c, Bin::And, r, mask);
                             let below = self.s.m.cmp(c, pred::SLT, r, lo);
-                            let r = self.s.m.select(c, below, lo, r);
+                            let min = self.s.m.i32(0x8000);
+                            let x = self.s.m.select(c, below, min, low);
                             let above = self.s.m.cmp(c, pred::SGT, r, hi);
-                            self.s.m.select(c, above, hi, r)
+                            self.s.m.select(c, above, hi, x)
                         } else {
                             r

Overlap, from reading: #92 (open, conflicting) lowers v_add_nc_i32 / v_sub_nc_i32 and the signed 16-bit add and sub too.

AI-assisted (Claude Code). Synthetic tests only.

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

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, cargo test -p victory-shader --locked.

144 passed / 0 failed / 2 ignored. v_add_nc_i32/v_sub_nc_i32/v_add_nc_i16 lowered — no-carry add/sub variants; check the NC (no-carry-out) semantics match the ISA.

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head ab4db4d onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

@sinan-cmyk

Copy link
Copy Markdown

Same failure on Ada, and the suggested fix passes there too

On an RTX 4070 SUPER (Ada, driver 32.0.16.1088, default adapter, VICTORY_WARP unset), with head ab4db4d merged onto main 940d32f, cargo test --locked -p victory-shader --lib gives 245 passed and 4 failed. Three failures are the NVIDIA ones from #1835 that main has too. The fourth is this PR's test, with the same values as on the RTX 3070 above:

hardware: VOP3 0x30d, clamp true, op_sel 0b0000, case 1
  left: 4294967273 (0xFFFFFFE9)   right: 2779119593 (0xA5A5FFE9)

So it is not limited to Ampere or to driver 616.56.

Then I applied @upipibi's change from the comment above (mask the low 16 bits first; the selects pick 0x8000, i16::MAX or the masked value). With it, both valu_tests::v_add_sub_nc_i32_wrap_and_saturate and valu_tests::v_add_sub_nc_i16_wrap_and_saturate_with_op_sel pass on this card.

Offline, on the retail dump, this PR does not change the counts (posted above).

AI-assisted (Claude Code). Synthetic tests only.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. This lowers v_add_nc_i32/v_sub_nc_i32 (VOP3 0x37f/0x376) and v_add_nc_i16/v_sub_nc_i16 (0x30d/0x30e) in emit/lower.rs, with clamp as signed saturation, in 32-bit DXIL (the tests assert Int64 stays off). The i32 overflow test is (a^r)&(b^r) for add and (a^r)&(a^b) for sub, saturating toward the sign of a. The i16 operands are sign-extended by the existing _i16 path, and the clamped form now uses the mask-before-select shape, so the selects pick 0x8000, 0x7fff or the masked low half. At 4859090 the NVIDIA failure is gone: the RTX 3070 re-run posted here passes both new tests, and the rest of the suite matches main. CI check: passing.

Overlap: #92 covers the same opcodes but conflicts with main. This one merges cleanly.

Reply to @geiyiliyabumo: thanks, the workaround is in as asked. The credit to upipibi (and to andrewpozdnakov7-jpg's d3df9ee, which carried it first) is worth keeping in the commit message.


Generated by Claude Code

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I put @upipibi's mask-before-select workaround in d3df9ee, directly on top of ab4db4d. The commit credits @upipibi for the workaround and expands the readback test to all eight source and destination half selections.

Both expanded tests passed on software WARP on ab4db4d and on d3df9ee (192 signed 16-bit cases and 28 signed 32-bit cases). With the workaround, all 30 VALU tests passed with DXIL validation, and strict shader Clippy and the asset policy passed. The RTX 3070 and 4070 SUPER results in this thread are the hardware evidence for the masking change.

The commit is ready to cherry-pick. Please keep the Git authorship and co-author trailer if you take it. Claim. AI-assisted with Codex. Synthetic shaders only.

@geiyiliyabumo
geiyiliyabumo force-pushed the feat/shader-v-add-sub-nc-i branch from ab4db4d to 4859090 Compare November 25, 2026 13:20
@geiyiliyabumo

Copy link
Copy Markdown
Contributor Author

Applied @upipibi's mask-before-select workaround for clamped \�_add_nc_i16\ / \�_sub_nc_i16\ (head $sha) so the selects pick 16-bit masked patterns (\

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown

@geiyiliyabumo Re-run at 4859090 (as-is, on main 940d32f), RTX 3070 (616.56) plus WARP, Windows 11, debug, cargo test -p victory-shader --locked: the failure I reported is gone.

One run only. Thanks also to andrewpozdnakov7-jpg, whose d3df9ee carried the workaround first.

AI-assisted (Claude Code). Synthetic tests only.

BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…c_i32, v_add_nc_i16, and v_sub_nc_i16 in pure 32-bit DXIL
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…c_i32, v_add_nc_i16, and v_sub_nc_i16 in pure 32-bit DXIL

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

Verified head 48590903d4ce14cb3c1d2e9de12b86339ce8dd48 merged onto main (63bcca43) on Windows 11 (RTX 5090 Blackwell + WARP, Rust 1.98.1 MSVC, SDK dxil.dll).

  • Clean merge onto main; crates/victory-shader/src/emit/lower.rs is 523,385 bytes (under the 512 KiB limit; also co-merges cleanly with #465 at 523,479 bytes, passing cargo xtask policy).
  • cargo xtask policy (191 files ok), cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default and --all-features), and both new v_add_sub_nc_i* tests pass on RTX 5090 and WARP with dxil.dll validation and SFI0 INT64 clear.
  • Checked the pure 32-bit overflow formulas ((a ^ r) & (b ^ r) for add, (a ^ r) & (b ^ a) for sub, saturating toward a's sign) and mask-before-select 16-bit clamping (emit/lower.rs:6522) with an expanded probe on RTX 5090 and WARP:
    • 484/484 i32 corner pairs (0, ±1, ±2, i32::MIN/MAX, MIN+1, MAX-1, ±0x4000_0000 x add/sub x clamp 0/1), all INT64-free.
    • 448/448 i16 cases covering all 8 (opsel[0], opsel[1], opsel[3]) combinations x clamp 0/1 x 14 boundary pairs (including 0 - i16::MIN, i16::MIN + i16::MIN, i16::MAX - i16::MIN, and negative non-overflowing sums), confirming the mask-before-select workaround preserves the untouched destination half on Blackwell as well.
    • 20/20 modifier rejection checks (omod, abs, neg) across all four opcodes.

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.

8 participants