Shaders: lower v_cvt_pknorm_{i16,u16}_{f32,f16} with exact rounding #596
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/596"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T05:18:05Z.
What
v_cvt_pknorm_i16_f32,v_cvt_pknorm_u16_f32,v_cvt_pknorm_i16_f16andv_cvt_pknorm_u16_f16(VOP3 0x368, 0x369, 0x312, 0x313) were refused, so any program that used one failed to translate. They now lower:x * 32767can need up to 39 significant bits, so it can round onto a tie before the rounding to an integer. That is why #163 left these refused ("needs the console rounding and NaN rules").One new helper,
Lower::norm16, incrates/victory-shader/src/emit/lower.rs. It sets theINT64flag, assrc64already does.Effect on the title: none
Measured by @Parakeet-Wheat on the retail 1.0.2.1 shaders, in a comment below: the title contains 0
v_cvt_pknorm_*(and no unpackedv_cvt_norm_*); its only packing conversion isv_cvt_pk_u8_f32.victory translate --list --validategives the same outcome onmainand on this PR: 21,736 validated, 4,003 failed, 162 rejected. So this PR only completes the instruction set. It doesn't unblock anything in the game, and it can wait behind work that does.Overlap
I searched open and closed PRs and issues: #376 lowers the unpacked
v_cvt_norm_{i16,u16}_f16, and #163 lists all six as refused. No PR lowers thepknormforms. This PR does not touch #376's opcodes, and either can land first without a conflict in the match.Tested
Windows 11, NVIDIA RTX 5070, AMD CPU. No game data.
valu_tests.rs,v_cvt_pknorm_f32_rounds_the_exact_productandv_cvt_pknorm_f16_rounds_the_exact_product. They run hand-assembled compute programs on the hardware device and on WARP, validate them withdxil.dll(10.0.26100), and compare against an f64 reference (the product is exact in f64). Inputs: ±0, ±1, ±2, ±inf, ±NaN, 0.5 (a tie in both scales), denormals, the smallest normal, values just below 1, and inputs found by search where an f32 product lands exactly on a tie but the exact product does not. The test asserts that those inputs really tell an f32 implementation apart from the exact one. The f16 cases put junk in the unread high halves and include large denormal halves;0x83d6and0x823fare checked against the hardware's results (0xfffe,0xffff).v_cvt_pknorm_i16_f32(NaN, 0.5) = 0x7fff0000, want 0x40000000).cargo test --workspace: all passed (victory-shader 71 passed).rustfmt --edition 2024 --checkon both changed files: clean.cargo clippy -p victory-shader --all-targets: no warnings.cargo xtask policy: 151 files ok.Not tested
D = {(snorm/unorm)S1, (snorm/unorm)S0}) and the D3D float-to-norm rules (NaN → 0, round to nearest even). If the console multiplies in f32 before it rounds, results differ by one step on the rare inputs the trap search finds. In [0, 1] the only exact tie is 0.5, so nearest-even and half-up agree on every exact tie.AI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-24T05:49:06Z.
RDNA2 hardware measurement of the rounding rule (the description lists "no console hardware"): the hardware rounds the exact product once, as this PR does. One note on f16 denormals below.
Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. Hand-written
v_cvt_pknorm_{i16,u16}_{f32,f16}in a HIP kernel (assembled by the AMD driver's amd_comgr). 422 inputs, the same value in src0 and src1: ±0, ±1, ±2, ±inf, NaNs, ±0.5, denormals, the smallest normal, one ulp below ±1, 300 random values in ±[2^-31, 1), and 100 inputs found by search where the f32 product lands exactly on .5 but the exact product does not. The f16 forms got the same values as halves, with junk in the unread high halves. RDNA2 desktop silicon, not the console's. No game data.v_cvt_pknorm_i16_f32v_cvt_pknorm_u16_f32v_cvt_pknorm_i16_f16(FP16 input denormals kept)v_cvt_pknorm_u16_f16(FP16 input denormals kept)For example,
0x3f7334e6gives0x79997999(i16) on the hardware, where an f32 product would round to0x799a799a.0x3e2abaabgives u160x2aaf2aaf, not0x2aae2aae. The f32 forms give the same results with f32 denormals flushed or kept, as the PR says. Other values: 1.0 and +inf give0x7fff7fff/0xffffffff; -1.0 and -inf give0x80018001(-32767) /0; NaN gives0; 0.5 gives0x40004000/0x80008000.f16 denormals. "Denormals give 0" holds for the f32 forms. A denormal half can be as large as ±6.1e-5, though, and times 32767 that is up to about 2. With the FP16 denormal mode allowing input denormals (
denorm16= 1 or 3), the hardware returns the scaled value. For example, half0x83d6gives i160xfffeand0x823fgives0xffff, exactly what this PR computes, since it widens the half to f32 exactly. With input denormals flushed (denorm16= 0 or 2), the same inputs give 0 (26 of the 422 rows). The new f16 test has only0x0001/0x8001, where the two agree. Since the translator does not model MODE, this only says which FP16 mode the lowering matches (input denormals kept). A half like0x03d6in the test would pin that choice.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.
Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T06:30:41Z.
@Lyrnc thank you for measuring this on RDNA2, especially the f32-product traps. I added your suggestion in
dab7a58: the f16 test now has large denormal halves (0x03d6,0x83d6,0x823f,0x03ff), and it checks that0x83d6gives i160xfffeand0x823fgives0xffff, as your run with FP16 input denormals kept did. That pins the lowering to that mode; the console's MODE is not modelled, which the description now says.AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T08:48:43Z.
Merged current
main(58fed37) ina421407. Only the end ofvalu_tests.rsconflicted. #376 has since landed withcvt_norm16for the unpacked f16 forms. This PR'snorm16covers f32 and f16 sources in 64-bit integers, so the two don't overlap, and either could later serve both.victory-shader: 91 passed on the RTX 5070.Note for testing:
cargo test -p victory-shaderdoes not compile on currentmain(#719, a call tolowerswithout its newrsrc2argument; @LAKSHYA-cmd-tech has a fix branch). I ran the suite with that one-line fix applied locally, not committed here.AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T09:37:30Z.
Review: APPROVED
Merges cleanly with main; macOS arm64, rustc 1.98.1,
cargo test -p victory-shader --lockedwith the PR merged into main8e79c80→ 70 passed / 0 failed / 1 ignored.Forming the product in integers from the float bits (mantissa × 32767/65535, then one round-to-nearest-even at the right shift) avoids the double rounding an f32
x * scalewould introduce — that's the correct way to get exactv_cvt_pknorm_*. I checked the edge handling: saturation at |x| ≥ 1, denormals → 0, NaN → 0, sign applied only for the signed forms, and the s-cap at 63 keeps shifts defined while preserving the result. The f64 reference in the tests is the right oracle.Note: the execution tests are Windows-gated (host device), so macOS coverage here is the lowering/compile path; Windows CI runs the real checks.
Imported from Marvisak/VIctory. Originally posted by slint6614-dev on 2026-11-24T10:16:39Z.
AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the hardware device of
run_cases(local adapter-selection patch, not part of this PR);cargo test -p victory-shader --release valu_testsat the PR head. Negative control: the PR's non-test files reverted to its merge base, its new tests kept.v_cvt_pknorm_f32_rounds_the_exact_productandv_cvt_pknorm_f16_rounds_the_exact_productpass on both AMD GPUs.Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T11:06:57Z.
@slint6614-dev thank you for the RDNA4 and RDNA2 runs, and for the negative control.
@Parakeet-Wheat if you have a moment: your count on #707 showed that PR doesn't touch the title. Could you run the same check here? That's the number of
v_cvt_pknorm_{i16,u16}_{f32,f16}invictory shader-info --disasmon the retail shaders, and whethertranslate --list --validatechanges any outcome. Counts only. If it's zero, I'll say so in the description, as on #707.AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by Parakeet-Wheat on 2026-11-24T11:31:25Z.
@mercuryVM here are the counts for
be92188, merged locally ontomainffe310e(clean):610.88), Ryzen 5 5600X, Rust 1.98.1 (x86_64-pc-windows-msvc),dxil.dll1.8.2502, title 1.0.2.1.victory shader-info --disasmovershaders.rpfand the executable: 0v_cvt_pknorm_*, and no unpackedv_cvt_norm_*either. The only packing conversion isv_cvt_pk_u8_f32(2,606).victory translate <exe> shaders.rpf --list --validate,mainvs this PR: no program changes outcome (21,736 validated, 4,003 failed, 162 rejected on both).cargo test -p victory-shader --locked: 122 passed, 0 failed, 1 ignored; bothv_cvt_pknorm_*tests pass on hardware and WARP.Offline translation and validation only; no shader contents or extracted data included. AI-assisted (Claude Code); run on my machine.
Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T11:53:44Z.
@Parakeet-Wheat thank you. Zero in the title: I've put that at the top of the description. This PR is instruction-set completeness only.
AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T13:51:32Z.
Review: APPROVED
Tested merged cleanly onto
main(ffe310e) on Windows 11 x64 (Rust 1.98.1 MSVC) with NVIDIA GeForce RTX 5090 (Blackwell) and WARP:cargo test -p victory-shader valu_tests: 15 passed, 0 failed (includingv_cvt_pknorm_f16_rounds_the_exact_productandv_cvt_pknorm_f32_rounds_the_exact_producton both RTX 5090 and WARP)cargo clippy -p victory-shader --all-targets: 0 warningscargo xtask policy: okVery thorough integer-bit exact-product rounding (
norm16) avoiding f32 intermediate rounding errors, with clean NaN/denormal/saturation handling and solid GPU verification. Approving.AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:29:43Z.
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,crates/victory-shader/src/emit/valu_tests.rs.The content has not been reviewed yet; it will be once the branch is rebased onto
main.Generated by Claude Code
upstream-sync referenced this pull request2026-11-25 11:05:28 +00:00
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.