Repository navigation
Description
I compared victory-shader's VALU lowering (crates/victory-shader/src/emit/lower.rs at 015d45b) against RDNA2 behaviour that the Leonida project measured on hardware. Leonida translates the same instruction set to SPIR-V, and records each measurement in its docs/design.md contract table. Three lowerings give different results from the hardware. All inputs below are synthetic. I have no Windows machine here, so none of this is run through host.rs yet. I'm filing it as an issue first in case you'd rather fix it your own way.
1. v_med3_f32 / v_med3_f16 with a NaN operand
lower.rs:3627 (and :4224 for f16) builds med3 as max(min(a,b), min(max(a,b), c)). On RDNA2, any NaN operand makes med3 return min3(s0, s1, s2) (RX 6800 gfx1030 and gfx1035/1036, all NaN positions, wave32 and wave64).
| inputs | lowering | hardware |
|---|---|---|
med3(1, 2, NaN) |
2 | 1 |
med3(NaN, 1, 0) |
1 | 0 |
med3(NaN, 0, 1) (the usual clamp order) |
0 | 0 |
So the common clamp(x, 0, 1) form happens to agree, but any other operand order with a NaN does not. A fix: when any operand is NaN, select min(min(a,b),c); otherwise use the current median.
2. v_fract_f32 / v_fract_f16 near 1.0
lower.rs:3652 maps to DXIL Frc (x − floor x). The hardware returns the largest value below 1.0 (0x3f7fffff; f16 0x3bff) where that would round to 1.0, e.g. fract(-1e-10): hardware 0x3f7fffff, Frc gives 1.0. fract(±inf) is NaN on both. A fix: select(r == 1.0, 0x3f7fffff, r).
3. v_bfe_i32 / s_bfe_i32 when the field runs past bit 31
bfe() (lower.rs:1360) shifts right logically before masking, so a signed field cut off at bit 31 is not sign-extended from bit 31. Measured on gfx1036 (Leonida, row "v_bfe_u32 / v_bfe_i32 past bit 31"):
| op | a | offset | width | lowering | hardware |
|---|---|---|---|---|---|
v_bfe_i32 |
0xd2f90159 |
28 | 16 | 0x0000000d |
0xfffffffd |
v_bfe_i32 |
0x80000000 |
31 | 4 | 0x00000008 |
0xffffffff |
A fix: use Bin::AShr instead of Bin::LShr for shifted when signed. The mask and the sign-extension step then give the hardware result in every case, and the unsigned path is unchanged. s_bfe_i32 follows the same rule (the same row for s_bfe).
Lower priority, for completeness
v_sin/v_cos: scaling by 2π beforeSin/Cosloses precision for large angles (about 1.2e-4 rad near 255 turns). Reducing in turns first (x - round_even(x), then × 2π) fixes it. The hardware also gives exactly +0 at half turns (sin) and quarter turns (cos), where the result above gives about ±8.7e-8.legacy_mul: the hardware reads a denormal operand as zero, so the +0 rule also applies to it. The current== 0.0test leaves that to the driver's denorm mode.
The v_bfe width masking (m31), the shift-amount masking, v_ffbh/s_ff1/s_flbit, and v_cvt_pkrtz_f16_f32 overflow (stepping back to 65504) already match.
If any of these are welcome as a PR, I'm happy to send one (starting with #3, the smallest). Since I can't run host.rs tests, it would come with the expected words for a Windows test.
Activity
MiguelMatMar commentedon Nov 23, 2026
Windows results for this, on an NVIDIA RTX 4080 SUPER (driver 621.24, Windows 11), at
015d45b. AI-assisted (Claude Code), synthetic inputs only.cargo test --workspace: everything passes (about 220 tests; 3 ignored, the ones that need files outside the repo). Thediff_testsskip without the consoledxc, so they don't cover this.I can't drive
lower.rswithout a console container, so I checked the host side instead. A throwaway local test (not committed) compiled the same expressions with the Windows SDKdxc(cs_6_5) and ran them throughHost::compute_pipeline_with+Host::dispatch. Inputs came from root constants, so nothing was constant-folded.frac(-1e-10)(DXILFrc)0x3f800000(1.0)0x3f7fffff0x3f7ffffffrac(-1e-8)0x3f8000000x3f7fffffmax(min(1,2), min(max(1,2), NaN))(med3 1,2,NaN)(NaN, 1, 0)(NaN, 0, 1)sin(255.5 * 2π)(scaled beforeSin)9.57e-5-8.74e-8Frcreturning 1.0 depends on the driver. NVIDIA gives 1.0 and WARP already clamps. The proposedselect(r == 1.0, 0x3f7fffff, r)makes them agree.min3. The issue is confirmed on both.v_bfe_i32: I tracedbfe()(lower.rs:1360) by hand.LShrbefore the mask drops the sign. One correction to the table: fora = 0x80000000, off 31, width 4the current lowering gives0x00000001, not0x00000008: the shift leaves 1, the mask keeps it, and a 4-bit field0001sign-extends to 1. Hardware gives0xffffffff. WithAShrthe shift gives0xffffffff, the mask gives0xf, and sign-extension gives0xffffffff, which matches.Happy to run a PR's test on this GPU if one goes up.
Shaders: med3 with NaN, fract below 1.0, signed bit fields past bit 31
Shaders: sine and cosine in turns, exact zeros at half and quarter turns
shader: keep fract(NaN) as NaN on NVIDIA and add synthetic GPU VALU t…
shader: keep fract(NaN) as NaN on NVIDIA and add synthetic GPU VALU t…
9hvn9yt2vk-netizen commentedon Nov 24, 2026
I think this is fixed on main: commit 36eb0b4 ("Shaders: med3 with NaN, fract below 1.0, signed bit fields past bit 31") and PR #37 (fract(NaN) on NVIDIA, plus synthetic GPU VALU tests) are both merged. Can this be closed?
GPU: any table to make afresh that cannot be made drops its draw or d…
Shaders: v_mul_legacy_f32 treats denormal operands as zero
Shaders: v_mul_legacy_f32 treats denormal operands as zero
GPU: any table to make afresh that cannot be made drops its draw or d…
decomp: note oracle pending (Marvisak#2)
Merge pull request Marvisak#2 from JMAN730/upstream-open-prs
Shaders: med3 with NaN, fract below 1.0, signed bit fields past bit 31
Shaders: sine and cosine in turns, exact zeros at half and quarter turns
shader: keep fract(NaN) as NaN on NVIDIA and add synthetic GPU VALU t…
Merge PR Marvisak#526 (Linux: a native runtime, no Wine) into main