victory-shader: med3 with NaN, fract near 1.0 and signed bfe past bit 31 differ from RDNA2 hardware #2
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#2
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
@Daazd on GitHub · 2026-11-23 22:35 UTC · original
I compared
victory-shader's VALU lowering (crates/victory-shader/src/emit/lower.rsat015d45b) against RDNA2 behaviour that the Leonida project measured on hardware. Leonida translates the same instruction set to SPIR-V, and records each measurement in itsdocs/design.mdcontract 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 throughhost.rsyet. I'm filing it as an issue first in case you'd rather fix it your own way.1.
v_med3_f32/v_med3_f16with a NaN operandlower.rs:3627(and:4224for f16) builds med3 asmax(min(a,b), min(max(a,b), c)). On RDNA2, any NaN operand makes med3 returnmin3(s0, s1, s2)(RX 6800 gfx1030 and gfx1035/1036, all NaN positions, wave32 and wave64).med3(1, 2, NaN)med3(NaN, 1, 0)med3(NaN, 0, 1)(the usual clamp order)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, selectmin(min(a,b),c); otherwise use the current median.2.
v_fract_f32/v_fract_f16near 1.0lower.rs:3652maps to DXILFrc(x − floor x). The hardware returns the largest value below 1.0 (0x3f7fffff; f160x3bff) where that would round to 1.0, e.g.fract(-1e-10): hardware0x3f7fffff,Frcgives 1.0.fract(±inf)is NaN on both. A fix:select(r == 1.0, 0x3f7fffff, r).3.
v_bfe_i32/s_bfe_i32when the field runs past bit 31bfe()(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_i32past bit 31"):v_bfe_i320xd2f901590x0000000d0xfffffffdv_bfe_i320x800000000x000000080xffffffffA fix: use
Bin::AShrinstead ofBin::LShrforshiftedwhensigned. The mask and the sign-extension step then give the hardware result in every case, and the unsigned path is unchanged.s_bfe_i32follows the same rule (the same row fors_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_bfewidth masking (m31), the shift-amount masking,v_ffbh/s_ff1/s_flbit, andv_cvt_pkrtz_f16_f32overflow (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.rstests, it would come with the expected words for a Windows test.call [r9+0x30]at the account-picker → world transition; a second crash overruns a 24-byte-stride table; crash-log appends interleave #2025victory_gpu::timelinefail-open GPU memory wait timeout immediately precedes title0xc0000005crash (afterretired host resource queueepoch pinning) #2174@MiguelMatMar on GitHub · 2026-11-23 22:44 UTC · original
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.
@9hvn9yt2vk-netizen on GitHub · 2026-11-24 01:10 UTC · original
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?