Skip to content

Shaders: d16 format loads saturate 32-bit integer channels - #1927

Open
upipibi wants to merge 1 commit into
Marvisak:mainfrom
upipibi:fix/d16-int-saturate
Open

upipibi wants to merge 1 commit into
Marvisak:mainfrom
upipibi:fix/d16-int-saturate

Conversation

@upipibi

@upipibi upipibi commented Nov 25, 2026

Copy link
Copy Markdown

tbuffer_load_format_d16_* / buffer_load_format_d16_* of a 32-bit integer channel kept its low 16 bits. RDNA2 saturates it instead: UINT to 0xFFFF, SINT to -32768..32767. So 70000 in a 32_UINT buffer loaded as 0x1170 here, and as 0xFFFF on the console's GPU.

Sources for the rule: measured on gfx1031 for buffer format loads (xcl-dev/xcl#1488, item XCL-BT-020) and on gfx1036 for D16 image reads (boykopovar/AnyPS5#22750).

What is in it

  • emit/lower.rs, d16_channels: UINT channels go through min(x, 0xFFFF) and SINT ones through a clamp to -32768..32767 before the 16-bit mask. 8- and 16-bit integer formats never leave that range, so they're unchanged. Float formats are unchanged too: the legacy f32→f16 conversion already rounds toward zero, which matches RDNA2.
  • emit/valu_tests.rs: tbuffer_d16_loads_saturate_32_bit_integers covers 32_UINT and 32_SINT, past both ends and in range. It compares only the low half, so it doesn't depend on what an odd last channel leaves in the high half (Shaders: preserve the unwritten high half of packed D16 buffer loads #1441 changes that part, and the two merge cleanly).

Tested on Windows 11, RTX 3070 (Ampere, driver 616.56), and WARP, debug, on main 42363ab:

Not tested: a title.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

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>
@upipibi
upipibi force-pushed the fix/d16-int-saturate branch from 5b42f8f to 53affcc 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 53affcc 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_saturate_32_bit_integers 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 format loads saturate 32-bit integer channels — pairs with #1940 (same d16 load path, UNORM/SNORM rounding); both pass independently, maintainer should check they compose.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate.

emit/lower.rs, d16_channels: d16 format loads of 32-bit integer channels now saturate (UINT via unsigned min(x, 0xFFFF), SINT via a clamp to -32768..32767 before the 16-bit mask) instead of keeping the low 16 bits. 8/16-bit integer formats can't go past those bounds, so they behave as before, and float formats are untouched. The cited measurements exist: xcl-dev/xcl#1488 (XCL-BT-020, gfx1031, with a gfx1036 confirmation in a comment) and boykopovar/AnyPS5#22750 (gfx1036 image d16). These are third-party hardware results, not official documentation. The new test tbuffer_d16_loads_saturate_32_bit_integers covers both ends and the in-range cases, and its expected values match the stated rule.

CI: check passing.


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 53affcc 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).

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.

5 participants