Skip to content

Recompiler: a 2-bit SNORM store component rounds a half away from zero - #2051

Open
upipibi wants to merge 2 commits into
xcl-dev:mainfrom
upipibi:fix/snorm2-store-half-away
Open

upipibi wants to merge 2 commits into
xcl-dev:mainfrom
upipibi:fix/snorm2-store-half-away

Conversation

@upipibi

@upipibi upipibi commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

fmt_encode rounded every SNORM component to nearest even. So 0.5 and −0.5 stored 0 in the 2-bit W of 2_10_10_10_SNORM, where RDNA2 stores 1 and −1. Storing x = w = 0.5, y = 0.25, z = −0.75 gave 0x28120100 instead of 0x68120100.

Source for the rule: boykopovar/AnyPS5#25877 ran a store sweep of tbuffer_store_format_xyzw in 75 formats on gfx1036. Only this 2-bit component differed from nearest-even rounding. 8-, 10- and 16-bit SNORM components and every UNORM width round to nearest even on the hardware too, so they stay as they are. These formats are common for packed normals and tangents, where the 2-bit component carries the handedness sign.

What is in it

  • recomp/emit.rs: the SNORM arm of fmt_encode uses sign(s) · floor(|s| + 0.5) when the component is 2 bits wide, and round(s) otherwise.
  • dxil/lower_mem.rs: the same in Lw::fmt_encode.
  • recomp/tests.rs: gpu_buffer_store_two_bit_snorm_rounds_half_away. It stores eight W values (±0.5, ±1, 0.49, −0.75, 0.4999999, 0.5000001) with x = 0.5, y = 0.25, z = −0.75, and compares the packed words.
  • 98e83c0: the 2-bit magnitude (at most 1) is compared with 0.5 directly. floor(|x| + 0.5) rounded 0.49999997 + 0.5 to exactly 1.0 in f32, so 0x3effffff / 0xbeffffff stored ±1 (found by nnonno91). Exactly ±0.5 still stores ±1; other SNORM widths unchanged.

Tested

Regression run
I have no retail title here. Could someone with one post the xcl-smoke blocks for main and this head?

Not tested

  • AMD, titles.

AI use

  • AI-assisted (Claude Code, Claude Opus 5.5): wrote the change, the tests and this description, and ran the tests above. Synthetic tests only; no game files.

🤖 Generated with Claude Code

@nnonno91

Copy link
Copy Markdown

Independent AMD check on 1b617e31db8414bd1ae0d23d4376f4108a6429bd. The existing regression test passed through both backends. The closest representable inputs with magnitude below 0.5 exposed a new boundary failure on both.

Tested

  • AMD Radeon(TM) Graphics (driver 32.0.21042.62, native wave64), Ryzen 7 9800X3D, Windows 11 25H2 build 26200.9974, Rust 1.100.0 MSVC, SDK DXC/DXIL 1.8.2502.11, unoptimized test profile.
  • Ran cargo test --locked -p xcl --lib gpu::recomp::tests::gpu_buffer_store_two_bit_snorm_rounds_half_away -- --exact --nocapture --test-threads=1 with XCL_ADAPTER=AMD Radeon, XCL_REQUIRE_GPU=wave64 and XCL_DIRECT_DXIL=0 and 1 in separate runs. The original test passed on the head (1 passed, 0 failed per backend).
  • In a temporary copy of the fixture, I used f32::from_bits(0x3eff_ffff) and f32::from_bits(0xbeff_ffff) as the last two W inputs and printed all readbacks before asserting. Both should store W = 0 (packed 0x28120100). The head stored W = +1 and −1 (packed 0x68120100 and 0xe8120100) on both backends. On the exact parent c00f3fcfce59bb4dfd3a1f96bbc84c60e765b538, both of these inputs stored W = 0, and the exact ±0.5 cases also stored W = 0. The adapted test failed once per backend on each revision at the respective boundary. All runs had 0 ignored and no skips.
  • In a separate CPU arithmetic check, rounding 0.4999999701976776 + 0.5 to binary32 gave exactly 1.0. That matches the below-half values crossing the threshold with floor(abs(s) + 0.5).

Not tested

Retail smoke, the full workspace suite, and other GPUs.

AI use

OpenAI Codex (GPT-6.2 Sol) adapted and ran the test, verified the logs, and wrote this report.

fmt_encode rounded every SNORM component to nearest even, so 0.5 and
-0.5 stored 0 in the 2-bit W of 2_10_10_10_SNORM (0x28120100 for
0x68120100). RDNA2 stores +-1 from a magnitude of 0.5 up; its 8-, 10- and
16-bit SNORM components do round to nearest even (AnyPS5#25877, a store
sweep of 75 formats on gfx1036). Both paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@upipibi
upipibi force-pushed the fix/snorm2-store-half-away branch from 1b617e3 to 1e29ff4 Compare November 25, 2026 02:09
@JoaoPauluu

Copy link
Copy Markdown

A run with vkd3d-proton as the host D3D12 (DXIL → SPIR-V through dxil-spirv, then the NVIDIA Vulkan driver), so the DXIL goes through a different compiler than the native-Windows runs here: this PR's test passes.

Tested

  • RTX 3060 12 GB (NVIDIA 620.70.06, 32-lane waves), i5-10400F, 16 GB, CachyOS (kernel 7.3.3), Proton Experimental 11.0-20261112 with the patched vkd3d-proton 3.1 from Linux: docs/linux.md, scripts/linux and Wine start-up checks for vkd3d-proton and DXVK (#154, #241) #257, DXC 1.9.2609.5. Release, cross-built for x86_64-pc-windows-gnu. 1e29ff4 as it is (merge base with main 5ce558f), no uncommitted changes. The lib test binary runs under Wine, one test per process.
    • gpu_buffer_store_two_bit_snorm_rounds_half_away: ok, not skipped

Not tested

  • the whole suite on this head, a retail title, native Windows, AMD.

AI use

  • Claude Code (Claude Opus 5.5) built the commit, ran the tests and wrote this report. The account owner asked for test reports to be posted and has not reviewed this one.

@JJAutoCompare

Copy link
Copy Markdown

Arc A770 verification of PR #2051 at 1e29ff44c7fa0afea70f24ff4c46c0e3311817c5.

Tested

  • Intel Arc A770 (driver 32.0.101.9175), Ryzen 7 3800X, Windows 11 Enterprise Evaluation 26200.9974, Rust 1.100.0 x86_64-pc-windows-msvc, DXC and DXIL 1.9.2609.5.
  • From the clean checkout at that SHA, prebuilt outside the Arc mutex with cargo test --locked -p xcl --lib --no-run --message-format=json. Used the unoptimized test profile, two Cargo jobs, CARGO_INCREMENTAL=0, CARGO_PROFILE_DEV_DEBUG=0, CARGO_PROFILE_TEST_DEBUG=0, and CARGO_TARGET_DIR=L:/XclTooling/pr1512-tiling-edges-fddfa267/target. Both TEMP and TMP pointed to L:/XclTooling/pr2051.
  • Ran the Cargo-emitted binary once per backend with XCL_ADAPTER=A770 and XCL_DXC_DIR=%LOCALAPPDATA%/XclTooling/dxc-1.9.2609/bin/x64. The working directory was crates/xcl. Each backend ran in a separate child PowerShell with -NoProfile -NonInteractive -ExecutionPolicy RemoteSigned to load the local mutex wrapper.
$env:PR2051_TEST_EXE = 'L:/XclTooling/pr1512-tiling-edges-fddfa267/target/debug/build/xcl/97fdf2b9ff8e1482/out/xcl-97fdf2b9ff8e1482.exe'
& 'L:/XclTooling/Invoke-ArcTest.ps1' -Test {
    & $env:PR2051_TEST_EXE 'gpu::recomp::tests::gpu_buffer_store_two_bit_snorm_rounds_half_away' --exact --nocapture --test-threads=1
}
Backend XCL_DIRECT_DXIL Passed Failed Ignored Skipped Filtered out
HLSL + DXC 0 1 0 0 0 1119
Direct DXIL 1 1 0 0 0 1119

Both runs acquired and released the Arc lock. Readback assertions matched these 32-bit words on both backends, with X = 0.5, Y = 0.25 and Z = −0.75.

W input Packed word
+0.5 0x68120100
−0.5 0xE8120100
+0.49 0x28120100
−0.75 0xE8120100
+1.0 0x68120100
−1.0 0xE8120100
+0.4999999 0x28120100
+0.5000001 0x68120100

Not tested

The immediate binary32 neighbors of the half ties, including 0x3effffff and 0xbeffffff from the earlier AMD report, are outside this fixture. This run also leaves other GPUs, the full workspace suite, and retail smoke untested. Earlier AMD report

AI use

T3 Code / Codex (gpt-6.2-sol) ran the checked-in test, verified the logs, and wrote this report.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

floor(|x| + 0.5) stored +-1 for the f32 neighbours just below +-0.5
(0x3effffff, 0xbeffffff): 0.49999997 + 0.5 rounds to 1.0 in f32, so W
read 0x68120100 / 0xe8120100 instead of 0x28120100 on both paths
(reported by nnonno91 on AMD). The 2-bit magnitude is at most 1, so
compare it with 0.5 directly; +-0.5 still stores +-1. The test gains the
two below-half inputs and their +-0.5 neighbours.

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

upipibi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks nnonno91, that's a real bug. In f32, 0.49999997 + 0.5 rounds to exactly 1.0, so floor(|x| + 0.5) stored ±1 for 0x3effffff / 0xbeffffff. The 2-bit magnitude is at most 1, so 98e83c0 compares it with 0.5 directly on both paths; exactly ±0.5 still stores ±1. The test now also covers 0x3effffff and 0xbeffffff (W = 0) and 0x3f000000 and 0xbf000000 (W = ±1). On an RTX 3070 (616.56), the extended test fails at 1e29ff4 and passes after the fix with XCL_DIRECT_DXIL=0 and 1. The gpu_ suite passes on both paths, apart from gpu_image_get_resinfo_1d_array with direct DXIL, which also fails on main.

AI-assisted (Claude Code).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants