Shaders: d16 buffer format loads of UNORM / SNORM data round once to nearest #1940
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!1940
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1940"
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?
@upipibi on GitHub · 2026-11-25 01:24 UTC · original
upipibi:fix/d16-norm-single-round→main· +162 −4 · 3 files · 4 commits · openStacked on #1927: the second, third and fourth commits (
Shaders: d16 format loads of UNORM / SNORM data round once to nearest, and95dc5d2, which moves this PR'snorm_f16unchanged into the new child moduleemit/lower/d16.rsto keeplower.rsunder the 512 KiB policy limit, and steven-seagull27'sf41d919, which drops the 64-bit divide) belong to this PR. Both of the first two changed16_channels. Scope: D16 buffer format loads (tbuffer_load_format_d16*/buffer_load_format_d16*) only.A d16 buffer format load of a UNORM / SNORM channel went through
legacyF32ToF16of the decoded f32. That's two roundings, and the second rounds toward zero. RDNA2 roundscode / (2^w − 1)once, to nearest even (gfx1031 for buffer format loads, xcl-dev/xcl#1488 XCL-BT-020; gfx1036 for D16 image reads, boykopovar/AnyPS5#22750). So about half of all codes came back one unit in the last place low: 16_UNORM0x0801gave0x2800, where RDNA2 gives0x2801.What is in it
emit/lower/d16.rs(a child module ofemit/lower.rs):norm_f16, used byd16_channels(inlower.rs) for UNORM / SNORM channels of 2 to 16 bits. The width comes frombuffer_format, so it's a constant.round(|f| · (2^w − 1)).code / (2^w − 1)has a binary expansion that repeats thew-bit code, so its first 32 bits decide the rounding, and a tie can't happen. They are built in 32 bits:code · ⌊(2^32 − 1) / (2^w − 1)⌋places every whole copy, and whenwdoesn't divide 32 the top32 mod wbits of the code fill the low bits. No 64-bit arithmetic, so the shader no longer needs Int64ShaderOps. This is steven-seagull27's patch (f41d919, #1940), which replaced the 64-bitudivthe first version used; it also addsd16_normalized_format_loads_do_not_require_int64, which checks that SFI0 bit 0x8000 is clear for all 20 UNORM / SNORM buffer formats.|v| / (2^(w−1) − 1)with the sign put back.legacyF32ToF16, whose toward-zero rounding matches RDNA2's packing of 32-bit float texels.emit/valu_tests.rs:tbuffer_d16_loads_of_16_bit_norms_round_oncechecks 64 16_UNORM and 64 16_SNORM codes against exact single rounding. Only the low half is compared.Overlap: #2843 (opened 22:02Z) changes the same
d16_channelspath andvalu_tests.rs. For USCALED / SSCALED / UNORM / SNORM it converts the decoded f32 withnarrow16_bits(f, false)(round to nearest even) instead oflegacyF32ToF16; this PR instead roundscode / (2^w − 1)once, from the integer code, for UNORM / SNORM only. So #2843 still rounds twice (to f32, then to f16), and also covers the scaled formats, which this PR leaves alone. Measured over all 65536 codes per format ata0a2e52(RTX 3070, driver 616.56, and WARP, same bits on both): it differs from single rounding on exactly 2 16_UNORM codes (0xFFAFgives0x3BFE, want0x3BFD;0xFFEFgives0x3C00, want0x3BFF) and 2 16_SNORM codes (0x7FF7gives0x3C00, want0x3BFF;0x8009gives0xBC00, want0xBBFF), each 1 ulp larger in magnitude: there the decoded f32 lands exactly on an f16 midpoint. This PR'stbuffer_d16_loads_of_16_bit_norms_round_once, copied onto #2843 unchanged, fails on those codes on both backends. For comparison,main(842b0cb) truncates and differs on 31740 16_UNORM and 30716 16_SNORM codes. #2843's own test checks only thatlegacyF32ToF16is gone. dooprazol confirmed the two 16_UNORM codes on a Radeon RX 9060 XT and WARP, with this PR exact on both: #1940 The two conflict inlower.rs(git merge-treeof both heads).Related: #2030 rounds D16 image reads of UNORM / SNORM textures the same way (
code / (2^w − 1)rounded once to nearest even). This PR covers the buffer format-load path (d16_channels) and doesn't touch the image path; the two change different functions, so their own changes don't conflict.Tested
f41d919merged withmain63bcca4(clean):cargo xtask policypasses,cargo clippy -p victory-shader --all-targets --locked -- -D warningsis clean,cargo test -p victory-shader --lib d16 -- --nocapture: 7 passed, 0 failed, on both the RTX 3070 (616.56) and WARP (no skip lines). Atf41d919alone: policy and clippy pass, d16 7 passed, 0 failed.lower.rsis unchanged by it (521,518 bytes after the merge). The 32-bit sum equals the old(code << 32) / (2^w − 1)for every code below2^w − 1atw= 1…16 (131,054 checks, CPU). steven-seagull27 reports 962,048 / 962,048 exact field values on an RTX 5080 and on WARP.95dc5d2merged withmain63bcca4(clean):lower.rs521,518 bytes (limit 524,288; it was 524,906 before the move),cargo xtask policypasses,cargo clippy -p victory-shader --all-targets --locked -- -D warningsclean,cargo test -p victory-shader --lib d16: 6 passed, 0 failed on the RTX 3070 (616.56) and on WARP. Headroom after the merge is 2,770 bytes: another PR that growslower.rs(e.g. #2942) can use it up.norm_f16against exact rational rounding: every code atw= 2…16, UNORM and SNORM, with the decoded value off by up to ±2 ULP (524,272 checks). No mismatches.4919f2e(base940d32f):mainfails on both backends with 57 of the 128 codes one ULP low. This branch passes.cargo test -p victory-shader --locked: 245 passed, 3 failed, 3 ignored. The 3 are #1744's NVIDIA failures onmain.cargo xtask policyandcargo clippy -p victory-shader --all-targets --locked -- -D warnings: pass.main67e8ae1(headd09a715). The conflict withda82e44was two functions added at the same place inlower.rs(itsnorm_quotient/exact_normfor f32 loads, and this PR'snorm_f16); both are kept. RTX 3070, debug:cargo test -p victory-shader --lib d16: 6 passed, 0 failed.main67e8ae1plus #1927's commit, with only this test added (no fix): fails 3 of 3 on the RTX 3070 and on WARP, first at 16_UNORM0x0801(0x2800, want0x2801). Soda82e44doesn't cover the d16 path; this PR is still needed.cargo test -p victory-shader --lockedwithheap_direct_texture_reads_mark_their_heap_indexskipped: 268 passed, 3 failed (the same #1744 failures). That test crashes the test process onmain67e8ae1on this card too (#2571; a fix is proposed in #2596, open, which passes that test 3 of 3 on this card).cargo xtask policyandcargo clippy -p victory-shader --all-targets --locked -- -D warnings: pass.Not tested: a title. D16 image reads are not in scope here; #2030 covers them with the same single-rounding rule.
AI-assisted (Claude Code, Claude Opus 5.5).
🤖 Generated with Claude Code
@Headache3318 on GitHub · 2026-11-25 02:04 UTC · original
Tested on Linux (Arch, kernel 7.3.5, 3× RTX 4090 driver 620.73.04), Rust 1.98.1 (pinned). PR head
4919f2eapplied cleanly onto currentmainccfb029.cargo test -p victory-shader --locked: 144 passed, 0 failed, 2 ignored. All tests pass. The newtbuffer_d16_loads_of_16_bit_norms_round_oncetest is GPU-specific (requires dxil.dll/WARP) and is skipped on Linux.cargo clippy -p victory-shader --all-targets -- -D warnings: 4 pre-existing errors in test code (fail identically on mainccfb029). The PR introduces no new clippy warnings.Not tested: Windows MSVC build, the title itself. AI-assisted.
@StExpeditus on GitHub · 2026-11-25 04:21 UTC · original
Test report: RTX 5060 Ti (Blackwell), Windows 11 Pro 26200
Environment: Windows 11 Pro 10.0.26200, NVIDIA GeForce RTX 5060 Ti (driver 621.24,
32.0.16.2124) beside an Intel UHD 770 iGPU, Intel Core i5-14600K, 32 GB, Rust 1.98.1 MSVC. Branchfix/d16-norm-single-roundat4919f2e1. No game files; the title was not run.cargo test --workspace --locked --no-fail-fast-> 694 passed, 5 failed, 10 ignored.The PR's D16 buffer tests pass on this hardware:
tbuffer_d16_loads_of_16_bit_norms_round_oncetbuffer_d16_loads_saturate_32_bit_integersThe 5 failures are the same pre-existing NVIDIA 1,000,000-view heap cap (
HEAP_VIEWSis 2^21) I also reported on #2030:victory-gpudescriptor::host::tests::dynamic_slots_are_held_until_releasedandmemory::tests::lists_share_a_table_and_copies_keep_their_views(heap.NumDescriptors >= HEAP_VIEWS), andvictory-shaderemit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride,emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros(dispatchE_INVALIDARG) andemit::synthetic_tests::gathers_at_a_level_match_the_hardware. They reproduce identically on main, and match #1835, #1808, #1760 and test report #1744. Nothing introduced by this PR fails on this machine.Triage note: this branch is based on
ccfb0299, older than current main (940d32f6); GitHub reports it mergeable, but the report above is for the branch as-is, not merged onto current main.@Marvisak on GitHub · 2026-11-25 08:17 UTC · original · edited 2026-11-26 03:23 UTC
Automated triage for the maintainers.
Verdict: merge candidate (unchanged). The new commit
f41d919(steven-seagull27's, taken in with authorship) replaces the 64-bitudivinLower::norm_f16(emit/lower/d16.rs) with 32-bit field replication. I checked it.u32::MAX / dis2^r · Σ 2^(i·w)withr = 32 mod w, soy · copiesplaces every whole copy of the code, andy >> (w − r)fills the lowrbits. The product cannot overflow becausey < d.y = 0andy ≥ dare still selected explicitly, which also covers the 1-bit SNORM case (d = 1). The shader no longer setsINT64, and the newd16_normalized_format_loads_do_not_require_int64test checks SFI0 bit0x8000for all 20 UNORM/SNORM formats.It merges cleanly with
main, andlower.rscomes to 521,518 bytes after the merge (limit 524,288). It is still stacked on #1927 (open), which should land first or together with it. It still conflicts with #2843 on the same path, and only one of the two should land.CI: passing (
check).Generated by Claude Code
@sinan-cmyk on GitHub · 2026-11-25 11:19 UTC · original
Offline measurement on a retail dump
Result: no count change against
main.I merged head 4919f2e1 onto
main940d32f(clean merge) and ranvictory translate --validate --listonshaders.rpf(1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result withmainprogram by program (25,889 programs). Validated: 23,918 onmain, 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).
@dooprazol on GitHub · 2026-11-25 23:10 UTC · original
I tested the double-rounding question on Windows 11 with a Radeon RX 9060 XT 16 GB. I ran the same synthetic
16_UNORMD16 buffer-load probe on #2843 (a0a2e525) and #1940 (d09a7159). The physical Radeon and WARP produced identical results on each revision.0x3bfe0x3bfd0x3bfd0x3c000x3bff0x3bffBoth revisions also returned the expected values for the endpoint controls, 0 and 65535. This confirms two inputs where the intermediate f32 rounding in #2843 changes the final FP16 result by one ULP. The single-rounding path in #1940 matched the exact reference for both. This probe covered
16_UNORMbuffer loads only.@fuzeofficlal on GitHub · 2026-11-26 00:46 UTC · original
Code review and RTX 5090 + WARP + DXIL verification at head
d09a71591c42merged ontomain(63bcca43c6a0):xtask):crates/victory-shader/src/emit/lower.rsreaches524,906bytes (+618B over the524,288-byte limit), causingcargo xtask policyto fail (rc=1). Movingnorm_f16/d16_channels(or another ~1–2 KiB helper) into a submodule underemit/will clear the policy gate with headroom.norm_f16(lower.rs:5202-5263) recovers the integer codeyfromformat_load'sf32viaround_to_nearest_even+FpToUi, then computes(y << 32) / dini64(flags::INT64) whered = (1 << bits) - 1is always odd — so exacty / dis never a binary half-way tie for0 < y < d, eliminating double rounding throughf32 -> legacyF32ToF16.y == 0 -> 0x0000,y >= d -> ±1.0, normal exponent carry, subnormal-to-min-normal0x0400carry, and RDNA2UINT/SINTsaturation ind16_channels) all check out cleanly (note: coverstbuffer_load_format_d16_*, notimage_loadD16).dxil.dll10.0.26100.0, Rust 1.98.1):cargo xtask policy: FAIL (lower.rs: 524906 bytes exceeds the 524288-byte limit)cargo clippy --locked -p victory-shader --tests -- -D warnings: 0 warningsemit::lower::tests buffer_formats_by_number tbuffer_d16: 36/36 passed across both RTX 5090 (hardware) and WARP (warp+ explicitVICTORY_WARP=1), including all 3 newtbuffer_d16_loads_*tests.Math and hardware tests look solid — just needs extracting out of
lower.rsto satisfy the 512 KiB policy check.@upipibi on GitHub · 2026-11-26 00:55 UTC · original
@fuzeofficlal thanks.
95dc5d2moves this PR'snorm_f16unchanged into a new child module,emit/lower/d16.rs(set up likelower/bvh.rs);d16_channelsstays inlower.rs. Merged with currentmain(63bcca4),lower.rsis now 521,518 bytes (was 524,906; the limit is 524,288), and the merge is clean.On that merge, RTX 3070 (driver 616.56) and WARP:
cargo xtask policypasses,cargo clippy -p victory-shader --all-targets --locked -- -D warningsis clean, and the 6d16tests pass on both backends.On scope: you're right that it covers buffer format loads only (
tbuffer/buffer_load_formatD16), so I've narrowed the title. D16 image reads are #2030, which uses the same single-rounding rule.AI-assisted (Claude Code, Claude Opus 5.5).
@steven-seagull27 on GitHub · 2026-11-26 01:51 UTC · original
The single-rounding result can be kept without requiring
Int64ShaderOps. I put a one-commit patch on top of your current head. It computes the first 32 fractional bits by repeating the normalized code in a 32-bit word. The field width is known at translation time. For a width that doesn't divide 32, a shift and OR fill the final partial copy.The new DXIL feature regression failed on
95dc5d2at format 1 with SFI0 bit0x8000set. With the patch, all 20 UNORM/SNORM format shaders clear the bit. I adapted the exhaustive readback from #1112 to compare the packed D16 halves against the directly rounded ratio. The RTX 5080 (621.40) and built-in Microsoft WARP each returned 962,048/962,048 exact field values across all 20 formats, including both signed and unsigned 16-bit midpoints. Each adapter ran separately and was identified by its DXGI adapter name. Strict all-targets Clippy passed.@upipibi on GitHub · 2026-11-26 02:49 UTC · original
@steven-seagull27 thanks, pushed as
f41d919on the branch, with your authorship. Here atf41d919merged withmain63bcca4:cargo test -p victory-shader --lib d16 -- --nocapture7 passed, 0 failed on an RTX 3070 (616.56) and on WARP, andcargo xtask policyand clippy-D warningspass. Your 32-bit sum also equals the old(code << 32) / (2^w − 1)for every code below2^w − 1atw= 1…16 (131,054 checks on the CPU), so the f16 results are unchanged and Int64ShaderOps is gone. The description now credits the patch.AI-assisted (Claude Code, Claude Opus 5.5).
div:2of a result in the lowest binade: +0, or a zero of its sign? #3171View 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.