Shaders: lower v_cvt_pknorm_{i16,u16}_{f32,f16} with exact rounding #596
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!596
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-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?
@mercuryVM on GitHub · 2026-11-24 05:18 UTC · original
mercuryVM:impl/cvt-pknorm→main· +252 −1 · 2 files · 4 commits · openWhat
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
@Lyrnc on GitHub · 2026-11-24 05:49 UTC · original · edited 2026-11-24 05:52 UTC
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.
@mercuryVM on GitHub · 2026-11-24 06:30 UTC · original
@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).
@mercuryVM on GitHub · 2026-11-24 08:48 UTC · original
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).
@slint6614-dev on GitHub · 2026-11-24 10:16 UTC · original
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.@mercuryVM on GitHub · 2026-11-24 11:06 UTC · original
@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).
@Parakeet-Wheat on GitHub · 2026-11-24 11:31 UTC · original
@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.
@mercuryVM on GitHub · 2026-11-24 11:53 UTC · original
@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).
@Marvisak on GitHub · 2026-11-25 00:29 UTC · original
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
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.