Skip to content

Shaders: lower v_cvt_pknorm_{i16,u16}_{f32,f16} in pure 32-bit DXIL - #2100

Open
geiyiliyabumo wants to merge 1 commit into
Marvisak:mainfrom
geiyiliyabumo:feat/shader-v-cvt-pknorm
Open

geiyiliyabumo wants to merge 1 commit into
Marvisak:mainfrom
geiyiliyabumo:feat/shader-v-cvt-pknorm

Conversation

@geiyiliyabumo

Copy link
Copy Markdown
Contributor

Summary

  • Lowers GFX10 VOP3 packed normalized-16 conversion instructions v_cvt_pknorm_i16_f32 (0x368), v_cvt_pknorm_u16_f32 (0x369), v_cvt_pknorm_i16_f16 (0x312), and v_cvt_pknorm_u16_f16 (0x313) in crates/victory-shader/src/emit/lower.rs using pure 32-bit DXIL without enabling Int64 / WIDE_OPS.
  • Applies f32 clamping ([-1.0, 1.0] -> 32767.0 for i16, [0.0, 1.0] -> 65535.0 for u16), ties-to-even rounding, NaN -> 0, VOP3 abs/neg source modifiers, and op_sel half selection for _f16, while rejecting unsupported clamp and omod.
  • Adds 3 synthetic DXIL-validation and GPU execution tests in crates/victory-shader/src/emit/valu_tests.rs covering boundary values (0.0, -0.0, ±1.0, ±0.5, ±2.0, ±inf, NaN), abs/neg modifiers, op_sel half selection, output modifier rejection, 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_cvt_pknorm -- --test-threads=1 with Microsoft dxil.dll: 3/3 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).

@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 de026e8. victory-shader: 247 passed, 3 failed, 3 ignored. The new v_cvt_pknorm_rounds_clamps_and_packs, v_cvt_pknorm_honors_source_modifiers and v_cvt_pknorm_rejects_output_modifiers pass. The 3 failures are #1744's NVIDIA failures on main. Same as main on this hardware.

Overlap, from reading: #596 (open, conflicting with main) lowers the same four v_cvt_pknorm_{i16,u16}_{f32,f16} instructions with exact rounding.

AI-assisted (Claude Code).

@Dathinel

Dathinel commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

I replayed two of Lyrnc's native gfx1036 measurements on de026e8 with synthetic VOP3 programs. Set both source registers to the input bits and read back the packed destination. RTX 3050 Laptop and WARP returned the same words:

Opcode Input bits This PR, both backends Native RDNA2 desktop, from #596
v_cvt_pknorm_i16_f32 3f7334e6 799a799a 79997999
v_cvt_pknorm_u16_f32 3e2abaab 2aae2aae 2aaf2aaf

These two inputs would make regression cases for a 32-bit implementation that preserves the native result. Rounding the product to f32 first changes each converted half by one.

For the f16 forms, the 422-input native sample in #596 did not distinguish the rounding rules. I compared both models over all 65,536 half encodings on CPU; only four encodings per signedness distinguish them. If someone still has the native probe, one input covers both forms: put half 0x3801 in both sources. For signed/unsigned the exact-product predictions are 400f400f / 801f801f. This PR returned 40104010 / 80208020 on both backends. A native readback of those two words would settle this fixture's expectation.

AI-assisted. Synthetic inputs only.

Tested f32-only alternative that keeps Int64 off. After NaN handling and clamping, take the magnitude m and set P to 32768 (signed) or 65536 (unsigned). In f32, compute a = m * P and q = roundEven(a). The integer magnitude is int(q) - int((a - (q - 0.5)) < m); then restore the sign for SNORM. Keep the subtractions in that order. Scaling by the power of two is exact. The only exact halfway input magnitude is 0.5, for which q is even.

I checked every nonnegative binary32 encoding from 0 through 1 against an exact-product reference for each scale. An experimental lowering passed 94 packed readbacks each on RTX 3050 Laptop and WARP, including the two native f32 witnesses above. SFI0 Int64 stayed clear. The same probe failed on the unmodified PR at the first native witness. This experiment leaves the f16 decision open.

@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_cvt_pknorm_{i16,u16}_{f32,f16} lowered in pure 32-bit DXIL — packed-normalization converts.

@steven-seagull27

Copy link
Copy Markdown

Confirmed the two packed f32 rounding differences on an RTX 5080 (driver 621.40) at de026e8418ef97cf3531888e4a77204a86f5e5a6. Built-in WARP 10.0.26100.10068 returned the same words on Windows 11 build 26300.10075.

Both adapters passed all 56 round/clamp/pack readbacks and all 8 source-modifier readbacks in the PR. I then ran the two additional inputs below, putting the same f32 bit pattern in both source registers and reading back the packed 32-bit destination.

Instruction Input to each source RTX 5080 Built-in WARP Native Radeon RDNA2 from #596
v_cvt_pknorm_i16_f32 (0x368) 0x3f7334e6 0x799a799a 0x799a799a 0x79997999
v_cvt_pknorm_u16_f32 (0x369) 0x3e2abaab 0x2aae2aae 0x2aae2aae 0x2aaf2aaf

Each adapter completed both extra dispatches. Relative to the native measurements, each signed half is one high and each unsigned half is one low. This reproduces the earlier RTX 3050 Laptop result on Blackwell. Native Radeon measurements · Earlier RTX 3050 Laptop replay

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head de026e8 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).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

The new v_cvt_pknorm_* arm in emit/lower.rs (Lower, VOP3 lowering) rounds the f32 product x * 32767 / x * 65535 with round_ne. Native RDNA2 readbacks posted on #596 show a different result. Two testers reproduced this on this head (RTX 3050, RTX 5080, WARP):

  • v_cvt_pknorm_i16_f32 of 0x3f7334e6 gives 0x799a799a; hardware gives 0x79997999.
  • v_cvt_pknorm_u16_f32 of 0x3e2abaab gives 0x2aae2aae; hardware gives 0x2aaf2aaf.

The PR body cites no hardware or compiler evidence for its rounding rule, and the f16 forms are still unsettled. To change the verdict, round from the exact product (a 32-bit way is described in the comments) and add those witnesses as test cases. #596 lowers the same four instructions with exact rounding (conflicting with main), so please coordinate. CI: passing.


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.

7 participants