Skip to content

Shaders: d16 buffer format loads of UNORM / SNORM data round once to nearest - #1940

Open
upipibi wants to merge 4 commits into
Marvisak:mainfrom
upipibi:fix/d16-norm-single-round
Open

upipibi wants to merge 4 commits into
Marvisak:mainfrom
upipibi:fix/d16-norm-single-round

Conversation

@upipibi

@upipibi upipibi commented Nov 25, 2026 •

Copy link
Copy Markdown

Stacked on #1927: the second, third and fourth commits (Shaders: d16 format loads of UNORM / SNORM data round once to nearest, and 95dc5d2, which moves this PR's norm_f16 unchanged into the new child module emit/lower/d16.rs to keep lower.rs under the 512 KiB policy limit, and steven-seagull27's f41d919, which drops the 64-bit divide) belong to this PR. Both of the first two change d16_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 legacyF32ToF16 of the decoded f32. That's two roundings, and the second rounds toward zero. RDNA2 rounds code / (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_UNORM 0x0801 gave 0x2800, where RDNA2 gives 0x2801.

What is in it

  • emit/lower/d16.rs (a child module of emit/lower.rs): norm_f16, used by d16_channels (in lower.rs) for UNORM / SNORM channels of 2 to 16 bits. The width comes from buffer_format, so it's a constant.
    • The code is recovered from the decoded value as round(|f| · (2^w − 1)).
    • code / (2^w − 1) has a binary expansion that repeats the w-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 when w doesn't divide 32 the top 32 mod w bits 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, Shaders: d16 buffer format loads of UNORM / SNORM data round once to nearest #1940 (comment)), which replaced the 64-bit udiv the first version used; it also adds d16_normalized_format_loads_do_not_require_int64, which checks that SFI0 bit 0x8000 is clear for all 20 UNORM / SNORM buffer formats.
    • SNORM uses |v| / (2^(w−1) − 1) with the sign put back.
    • Other formats are unchanged. Floats keep 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_once checks 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_channels path and valu_tests.rs. For USCALED / SSCALED / UNORM / SNORM it converts the decoded f32 with narrow16_bits(f, false) (round to nearest even) instead of legacyF32ToF16; this PR instead rounds code / (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 at a0a2e52 (RTX 3070, driver 616.56, and WARP, same bits on both): it differs from single rounding on exactly 2 16_UNORM codes (0xFFAF gives 0x3BFE, want 0x3BFD; 0xFFEF gives 0x3C00, want 0x3BFF) and 2 16_SNORM codes (0x7FF7 gives 0x3C00, want 0x3BFF; 0x8009 gives 0xBC00, want 0xBBFF), each 1 ulp larger in magnitude: there the decoded f32 lands exactly on an f16 midpoint. This PR's tbuffer_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 that legacyF32ToF16 is gone. dooprazol confirmed the two 16_UNORM codes on a Radeon RX 9060 XT and WARP, with this PR exact on both: #1940 (comment) The two conflict in lower.rs (git merge-tree of 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

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

@upipibi
upipibi force-pushed the fix/d16-norm-single-round branch from ee5726d to 4919f2e Compare November 25, 2026 01:35
@Headache3318

Copy link
Copy Markdown

Tested on Linux (Arch, kernel 7.3.5, 3× RTX 4090 driver 620.73.04), Rust 1.98.1 (pinned). PR head 4919f2e applied cleanly onto current main ccfb029.

  • cargo test -p victory-shader --locked: 144 passed, 0 failed, 2 ignored. All tests pass. The new tbuffer_d16_loads_of_16_bit_norms_round_once test 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 main ccfb029). The PR introduces no new clippy warnings.

Not tested: Windows MSVC build, the title itself. AI-assisted.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, cargo test -p victory-shader --locked.

144 passed / 0 failed / 2 ignored. d16 UNORM/SNORM loads round once to nearest — single-rounding conversion fix; pairs with #1927.

@StExpeditus

Copy link
Copy Markdown

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. Branch fix/d16-norm-single-round at 4919f2e1. 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_once
  • tbuffer_d16_loads_saturate_32_bit_integers

The 5 failures are the same pre-existing NVIDIA 1,000,000-view heap cap (HEAP_VIEWS is 2^21) I also reported on #2030: victory-gpu descriptor::host::tests::dynamic_slots_are_held_until_released and memory::tests::lists_share_a_table_and_copies_keep_their_views (heap.NumDescriptors >= HEAP_VIEWS), and victory-shader emit::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 (dispatch E_INVALIDARG) and emit::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 commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Verdict: merge candidate (unchanged). The new commit f41d919 (steven-seagull27's, taken in with authorship) replaces the 64-bit udiv in Lower::norm_f16 (emit/lower/d16.rs) with 32-bit field replication. I checked it. u32::MAX / d is 2^r · Σ 2^(i·w) with r = 32 mod w, so y · copies places every whole copy of the code, and y >> (w − r) fills the low r bits. The product cannot overflow because y < d. y = 0 and y ≥ d are still selected explicitly, which also covers the 1-bit SNORM case (d = 1). The shader no longer sets INT64, and the new d16_normalized_format_loads_do_not_require_int64 test checks SFI0 bit 0x8000 for all 20 UNORM/SNORM formats.

It merges cleanly with main, and lower.rs comes 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

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head 4919f2e onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 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).

upipibi and others added 2 commits November 25, 2026 10:25
RDNA2 saturates a 32-bit UINT channel of a d16 format load to 0xFFFF and
a SINT one to -32768..32767; d16_channels kept the low 16 bits (70000
loaded as 0x1170). Measured on gfx1031 (xcl-dev/xcl#1488, XCL-BT-020) and
gfx1036 (boykopovar/AnyPS5#22750). 8- and 16-bit formats never leave that
range, so they are unaffected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A d16 format load of a UNORM / SNORM channel went through legacyF32ToF16
of the decoded f32: two roundings, the second toward zero. RDNA2 rounds
code / (2^w - 1) once to nearest even, so about half of the codes came
back a unit low (16_UNORM 0x0801: 0x2800 for 0x2801; 57 of 128 sampled
16-bit codes on hardware and WARP). The value's binary expansion repeats
the w-bit code, so its first 32 bits decide the rounding and a tie
cannot happen (checked against exact rounding for every code at w = 2..16).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dooprazol

Copy link
Copy Markdown

I tested the double-rounding question on Windows 11 with a Radeon RX 9060 XT 16 GB. I ran the same synthetic 16_UNORM D16 buffer-load probe on #2843 (a0a2e525) and #1940 (d09a7159). The physical Radeon and WARP produced identical results on each revision.

Input code #2843 #1940 Exact nearest-even FP16
65455 0x3bfe 0x3bfd 0x3bfd
65519 0x3c00 0x3bff 0x3bff

Both 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_UNORM buffer loads only.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Code review and RTX 5090 + WARP + DXIL verification at head d09a71591c42 merged onto main (63bcca43c6a0):

  • 512 KiB file-size policy blocker (xtask):
    • Code merges cleanly, but crates/victory-shader/src/emit/lower.rs reaches 524,906 bytes (+618 B over the 524,288-byte limit), causing cargo xtask policy to fail (rc=1). Moving norm_f16/d16_channels (or another ~1–2 KiB helper) into a submodule under emit/ will clear the policy gate with headroom.
  • Numerical & code audit:
    • norm_f16 (lower.rs:5202-5263) recovers the integer code y from format_load's f32 via round_to_nearest_even + FpToUi, then computes (y << 32) / d in i64 (flags::INT64) where d = (1 << bits) - 1 is always odd — so exact y / d is never a binary half-way tie for 0 < y < d, eliminating double rounding through f32 -> legacyF32ToF16.
    • Edge cases (y == 0 -> 0x0000, y >= d -> ±1.0, normal exponent carry, subnormal-to-min-normal 0x0400 carry, and RDNA2 UINT/SINT saturation in d16_channels) all check out cleanly (note: covers tbuffer_load_format_d16_*, not image_load D16).
  • Verified (Windows 11, NVIDIA RTX 5090 + WARP, dxil.dll 10.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 warnings
    • emit::lower::tests buffer_formats_by_number tbuffer_d16: 36/36 passed across both RTX 5090 (hardware) and WARP (warp + explicit VICTORY_WARP=1), including all 3 new tbuffer_d16_loads_* tests.

Math and hardware tests look solid — just needs extracting out of lower.rs to satisfy the 512 KiB policy check.

…he size limit

Merged onto main, lower.rs was 524,906 bytes, over the 524,288 bytes
cargo xtask policy allows. norm_f16, which this branch adds, moves
unchanged with its doc comment into lower/d16.rs, a child module like
lower/bvh.rs; d16_channels stays in lower.rs and calls it as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@upipibi upipibi changed the title Shaders: d16 format loads of UNORM / SNORM data round once to nearest Shaders: d16 buffer format loads of UNORM / SNORM data round once to nearest Nov 26, 2026
@upipibi

upipibi commented Nov 26, 2026

Copy link
Copy Markdown
Author

@fuzeofficlal thanks. 95dc5d2 moves this PR's norm_f16 unchanged into a new child module, emit/lower/d16.rs (set up like lower/bvh.rs); d16_channels stays in lower.rs. Merged with current main (63bcca4), lower.rs is 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 policy passes, cargo clippy -p victory-shader --all-targets --locked -- -D warnings is clean, and the 6 d16 tests pass on both backends.

On scope: you're right that it covers buffer format loads only (tbuffer / buffer_load_format D16), 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

Copy link
Copy Markdown

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 95dc5d2 at format 1 with SFI0 bit 0x8000 set. 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.

git fetch https://github.com/steven-seagull27/VIctory.git refs/heads/codex/pr1940-d16-i32
git cherry-pick f41d919dd9ace93f669464b911eaa34caac69a02
cargo test --locked -p victory-shader --lib d16

@upipibi

upipibi commented Nov 26, 2026

Copy link
Copy Markdown
Author

@steven-seagull27 thanks, pushed as f41d919 on the branch, with your authorship. Here at f41d919 merged with main 63bcca4: cargo test -p victory-shader --lib d16 -- --nocapture 7 passed, 0 failed on an RTX 3070 (616.56) and on WARP, and cargo xtask policy and clippy -D warnings pass. Your 32-bit sum also equals the old (code << 32) / (2^w − 1) for every code below 2^w − 1 at w = 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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants