Skip to content

Recompiler: D16 format loads of UNORM / SNORM data round once to nearest - #2027

Open
upipibi wants to merge 2 commits into
xcl-dev:mainfrom
upipibi:fix/d16-norm16-single-round
Open

upipibi wants to merge 2 commits into
xcl-dev:mainfrom
upipibi:fix/d16-norm16-single-round

Conversation

@upipibi

@upipibi upipibi commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Known: this PR's D16 normalization uses a 64-bit divide (d16_fmt in dxil/lower_mem.rs, norm16_rep in recomp/emit.rs), so every shader with a D16 load declares Int64 (SFI0 0x8000) in both backends. A 32-bit fix with tests is on upipibi/xcl branch fix/d16-norm16-no-int64 (af15e87, one commit on #2027's head; RTX 3070: gpu_buffer_d16 2/2 with XCL_DIRECT_DXIL=0 and =1, lib suite 1170 passed, 0 failed; a no-GPU test checks that the Int64 flag is clear for all 20 UNORM / SNORM D16 buffer and tbuffer loads). It isn't pushed here because a push needs a new title smoke pair, which I can't make.

Part of #2026 (XCL-BT-020's third item, #1488). The image half follows in a second PR.

A D16 buffer format load packed a UNORM / SNORM channel with f32tof16 of the decoded f32. That's two roundings, and on D3D12 the second one rounds toward zero. RDNA2 rounds code / (2^w − 1) once, to nearest even. So about half of all codes came back one unit in the last place low: 16_UNORM 0x0801 gave 0x2800, where RDNA2 gives 0x2801.

Overlap with #2360 (#1607 group D102): both round UNORM / SNORM codes to f16 once, to nearest. This PR also truncates the other non-integer channels to f16 (f16_rtz), where #2360 rounds them to nearest. NWProgramming00 measured buffer_load_format_d16_x on an RDNA2 gfx1036 (#1607 comment): the hardware truncates 32_FLOAT (6,011 of 6,011 values), and rounds normalized codes once to nearest.

What is in it

  • recomp/emit.rs: norm16_rep and d16_fmt in the prelude. The FormatLoad D16 channel goes through d16_fmt instead of f32tof16.

    • 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, floor((code << 32) / (2^w − 1)), decide the rounding, and a tie can't happen.
    • SNORM uses |v| / (2^(w−1) − 1) and adds the sign.
    • Channels that aren't UNORM/SNORM, and DST_SEL 0/1, stay on f32tof16.
  • dxil/lower_mem.rs: the same as Lw::d16_fmt, at the three D16 format-load sites.

  • recomp/tests.rs: gpu_buffer_d16_loads_of_16_bit_norms_round_once, with 64 lanes each loading their own 16_UNORM and 16_SNORM code, against exact single rounding.

  • Second commit (b0aae2e): float channels of a D16 format load truncate with f16_rtz on both paths, instead of passing through f32tof16 / legacyF32ToF16. That conversion's rounding is the host's: it truncates on the RTX 3070 and WARP, but under vkd3d-proton it rounds to nearest (Shaders: D16 image reads of UNORM / SNORM texels are one ULP low for about half the codes Marvisak/VIctory#2006), while RDNA2 truncates (fix(shader): pack D16 image reads toward zero and saturate 32-bit integers as RDNA2 does boykopovar/AnyPS5#22750). New test gpu_buffer_d16_loads_of_32_bit_floats_truncate: 32_FLOAT values the two roundings tell apart (1 + 1.5 / 1.75 ulp, 65535, ±70000, a denormal half-way), plus -1.5 and +inf.

Tested

Regression run
Pair for the current head 0cab3ef against main 1a0bbe3, run by thehollowedone (comment, thanks): RTX 3080 Ti (621.35), stock scripts/smoke.ps1, clean release builds, 60 s offline. Pasted unedited.

xcl-smoke v1
commit: 1a0bbe3a2ba91ea8f5b6fc29130a053868d3c9e6
gpu: NVIDIA GeForce RTX 3080 Ti
title: retail-private-fixture
seconds: 60
exit: 0x0
frames shown: 1641 (PresentX 1641)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14266 MB
xcl-smoke v1
commit: 0cab3ef3b46a9b522fd8838051f2c404baa5fa62
gpu: NVIDIA GeForce RTX 3080 Ti
title: retail-private-fixture
seconds: 60
exit: 0x0
frames shown: 1639 (PresentX 1639)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14365 MB

Earlier: Toons29's pair, comment, was for b0aae2e, before the rebase onto main 5478c4a7..

Not tested

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

A D16 buffer format load packed a UNORM / SNORM channel with f32tof16 of
the decoded f32: two roundings, the second toward zero on D3D12. RDNA2
rounds code / (2^w - 1) once to nearest even, so about half of all codes
came back one unit in the last place low (16_UNORM 0x0801: 0x2800 for
0x2801). That value's binary expansion repeats the w-bit code, so its
first 32 bits, floor((code << 32) / (2^w - 1)), decide the rounding and a
tie cannot happen. Both paths; checked against exact rounding for every
code at w = 2..16. xcl-dev#2026 (XCL-BT-020's third item).

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

Copy link
Copy Markdown

Tested

Independent Windows/D3D12 check on exact head d7bd99057d01219b23acb0ec52a68d2e11e88c77. Windows 11 25H2 (26200.9974), Ryzen 7 9800X3D, AMD Radeon(TM) Graphics (32.0.21042.62, native wave64), and GeForce RTX 5090 (32.0.16.1742, split wave64). Rust 1.100.0 x64 MSVC, DXC/DXIL 1.8.2502.11, unoptimized dev/test profiles with debug info off.

  • cargo build --workspace --locked passed at the PR head.
  • cargo test --locked -p xcl --lib gpu::recomp::tests::gpu_buffer_d16_loads_of_16_bit_norms_round_once -- --exact --test-threads=1 --nocapture passed on each GPU with XCL_DIRECT_DXIL=0 and 1 (1 passed, 0 failed, 0 ignored, 0 skipped per run). With only the test added to parent de36462e0b1043b915a418d4591ed1e397622ede, all four baseline runs failed as expected (57 of 128 half values differed in each).
  • cargo test --locked -p xcl --test d16_norm_gpu_probe -- --exact all_16_bit_normalized_buffer_codes_match_exact_rounding --test-threads=1 --nocapture compared all 65,536 codes per format for 16-bit UNORM and SNORM. The temporary compute shader used BUFFER_LOAD_FORMAT_D16_X for both formats and wrote one packed pair to a separate output region per input. The CPU reference found the nearest binary16 value with exact integer cross-products. Both HLSL + DXC and Direct DXIL ran in each test. Each probe process passed (1 passed, 0 failed, 0 ignored, 0 skipped), with the parent run explicitly checking for the old rounding error.
Mismatched values per GPU and backend Parent PR head
16_UNORM 31,740 / 65,536 0 / 65,536
16_SNORM 30,716 / 65,536 0 / 65,536

The counts were identical on both adapters and both backends. For example, 16_UNORM 0x0801 changed from 0x2800 to the expected 0x2801.

Not tested

Real-title smoke runs, full workspace test suite, other channel widths, and image format loads. The exhaustive probe covers single-channel 16-bit buffer inputs.

AI use

OpenAI Codex (GPT-6 family, exact variant unavailable) prepared and ran the temporary probe, checked the outputs, and drafted this report.

@JJAutoCompare

JJAutoCompare commented Nov 25, 2026 •

Copy link
Copy Markdown

Tested

Independent Intel Arc A770 verification for #2026 on clean PR head d7bd99057d01219b23acb0ec52a68d2e11e88c77. Windows 11 Enterprise Evaluation 25H2 (26200.9974), Ryzen 7 3800X, Arc A770 driver 32.0.101.9175, Rust/Cargo 1.100.0 (x86_64-pc-windows-msvc), and DXC/DXIL 1.9.2609.5.

  • Prebuilt with cargo test --locked -p xcl --lib --no-run (exit 0). The test profile was unoptimized, with CARGO_BUILD_JOBS=2, CARGO_INCREMENTAL=0, CARGO_PROFILE_DEV_DEBUG=0, and CARGO_PROFILE_TEST_DEBUG=0.
  • With XCL_ADAPTER=A770, XCL_REQUIRE_GPU=wave64, and XCL_DXC_DIR set to the 1.9.2609 package, ran the prebuilt lib-test executable under the shared Arc lock. For each test below, the command was & $exe gpu::recomp::tests::<name> --exact --nocapture --test-threads=1, in a separate process for each listed XCL_DIRECT_DXIL setting.
Test 0 (HLSL + DXC) 1 (direct DXIL for kernel execution)
gpu_buffer_d16_loads_of_16_bit_norms_round_once Pass Pass
gpu_typed_buffer_loads_and_stores Pass Pass
buffer_format_d16_packs_and_unpacks_halves Pass Pass
buffer_format_table_matches_llvm Pass

7 passed, 0 failed, 0 ignored, 0 skipped across seven processes. The rounding regression checked 64 UNORM and 64 SNORM output halves per backend.

  • Ran powershell -NoProfile -ExecutionPolicy Bypass -File L:/XclTooling/pr2027/reference.ps1 -Source <worktree>/crates/xcl/src/gpu/recomp/tests.rs. The reference used exact integer distances between adjacent binary16 values to check all 128 committed fixture values. It also compared a CPU model of the PR conversion against that reference for all 65,536 codes per format at five binary32 magnitude offsets (−2 through +2 ULP, clamped at zero). Each format had 327,680 comparisons and zero mismatches.

Not tested

Exhaustive GPU output, other normalized channel widths, D16 image loads, the parent commit, the full workspace suite, and retail-title smoke runs.

AI use

Codex (gpt-6.2-sol) wrote and ran the temporary reference check, ran the focused tests, and wrote this report.

Follow-up on 83d3d05

Tested

Independent STOR-WINLAB Arc A770 rerun for #2027 at 83d3d05107998927f48efe501584dacfd934c14c. GitHub showed the PR open at that exact head before and after the run. The worktree was clean and detached at the same SHA throughout testing. The existing JJAutoCompare Arc result in comment 7149616157 covers d7bd99057d01219b23acb0ec52a68d2e11e88c77. The PR discussion and reviews had no equivalent exact-head Arc result.

Windows 11 Enterprise Evaluation 25H2 x64 (26200.9974), AMD Ryzen 7 3800X, Intel Arc A770 driver 32.0.101.9175, Rust and Cargo 1.100.0 (stable-x86_64-pc-windows-msvc, rustc 0ef1bc386), DXC and DXIL 1.9.2609.5. Cargo used the unoptimized test profile with two jobs and debug info and incremental compilation disabled.

cargo test --locked -p xcl --lib --no-run completed outside the Arc lock (exit 0). Under & 'L:/XclTooling/Invoke-ArcTest.ps1' -Test { ... }, ran the exact prebuilt command below in separate processes for XCL_DIRECT_DXIL=0 and 1, with XCL_ADAPTER=A770, XCL_REQUIRE_GPU=wave64, and the specified DXC directory.

& $exe gpu::recomp::tests::gpu_buffer_d16_loads_of_16_bit_norms_round_once --exact --nocapture --test-threads=1
Backend Passed Failed Ignored Skipped Filtered Child exit Test time
HLSL + DXC (0) 1 0 0 0 1,119 0 1.06 s
Direct DXIL (1) 1 0 0 0 1,119 0 0.47 s

Each run matched all 128 expected binary16 values across 64 UNORM and 64 SNORM inputs. The wrapper logged both settings, and the stock fixture's runner::run_code uses the direct translator without fallback when enabled. Both stderr logs show the DXC version because the fixture checks DXC availability before dispatch. The wrapper exited 0 and logged the mutex release.

Not tested

Other buffer widths or codes outside the fixture, image loads, filtered sampling, other shaders or adapters, the parent commit, the full workspace suite, and retail-title smoke runs.

AI use

T3 Code / Codex (gpt-6.2-sol) inspected the existing fixture and backend routing, ran the two tests, and wrote this report.

Raw evidence in L:/XclTooling/pr2027/run-20261125T041438211Z: environment.log, prebuild.{stdout.log,stderr.log,exit.txt}, direct-0.{stdout.log,stderr.log,exit.txt}, direct-1.{stdout.log,stderr.log,exit.txt}, and gpu-wrapper.{stdout.log,stderr.log,exit.txt}.

@raylitebull

Copy link
Copy Markdown
Member

Diff audit on d7bd990 looks strong (norm16_rep/d16_fmt in recomp/emit.rs + dxil/lower_mem.rs matches exact single rounding across all 65,536 16-bit UNORM/SNORM codes on AMD iGPU, RTX 5090, RTX 3070, and Intel Arc A770).

Remaining before merge:

  1. Rebase onto main to clear CONFLICTING/DIRTY (also drop the manual TRANSLATOR_VERSION = 9 bump in kernel.rs, as #1947 now auto-hashes gpu/dxil sources via XCL_DIRECT_DXIL_SOURCE_ID).
  2. Add paired xcl-smoke v1 blocks (main and PR) so PR description passes (or ask another contributor with a retail title to post them).

@luckyteagirl

Copy link
Copy Markdown
Contributor

Tested

  • Exact PR head 83d3d05107998927f48efe501584dacfd934c14c and main 5ce558f82a48c51d527f6035234dacbd29db3f05. Windows 11 build 26200, Ryzen 9 5980HX, AMD Radeon RX 6800M driver 30.0.13002.13003, Rust 1.99.0 MSVC, DXC/DXIL 1.9.2609.5.
  • With XCL_ADAPTER=6800M and XCL_REQUIRE_GPU=1, cargo test --locked -p xcl --lib -j 2 gpu::recomp::tests::gpu_buffer_d16_loads_of_16_bit_norms_round_once -- --exact --test-threads=1 --nocapture passed under both XCL_DIRECT_DXIL=0 (HLSL/DXC) and =1 (direct DXIL): 1 passed, 0 failed, 0 ignored, no explicit skip in each run.
  • cargo build --locked --release -p xcl -j 2 passed for both revisions. A locally owned title completed a paired 7-second scripts/smoke.ps1 run on the RX 6800M. Each run showed one frame, with no crash or device removal. Peak process commit was 6,963 MB on main and 7,058 MB on the PR.

The two blocks below can be copied into Regression run in the PR description. A comment by itself does not satisfy XCL's description check.

main:

xcl-smoke v1
commit: 5ce558f82a48c51d527f6035234dacbd29db3f05
gpu: AMD Radeon RX 6800M
title: 2ef5fd64
seconds: 7
exit: 0x0
frames shown: 1 (PresentX 1)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 6963 MB

PR head:

xcl-smoke v1
commit: 83d3d05107998927f48efe501584dacfd934c14c
gpu: AMD Radeon RX 6800M
title: 2ef5fd64
seconds: 7
exit: 0x0
frames shown: 1 (PresentX 1)
presents per 2 s (last 3): 0 0 1
device removals: 0
crashes: 0
peak commit: 7058 MB

Not tested

  • The short title runs logged zero draws and used default shader routing, so they do not demonstrate the D16 format-load path. The focused GPU test covers that path. I did not run the full workspace suite, longer title sessions, other adapters, or other normalized widths.

AI use

OpenAI Codex (GPT-6, exact variant not exposed in this session) ran the tests and smoke comparisons and drafted this report from their logs.

@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. 83d3d05 as it is (merge base with main 5ce558f), no uncommitted changes. The lib test binary runs under Wine, one test per process.
    • gpu_buffer_d16_loads_of_16_bit_norms_round_once: 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.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

d16_fmt passed float channels to f32tof16 / legacyF32ToF16, whose rounding
is the host's: it truncates on the RTX 3070 and WARP but rounds to nearest
under vkd3d-proton (Marvisak/VIctory#2006), where RDNA2 truncates
(boykopovar/AnyPS5#22750). Use f16_rtz in both paths, as the format
stores already do.

gpu_buffer_d16_loads_of_32_bit_floats_truncate loads 32_FLOAT values
that the two roundings tell apart (1 + 1.5 / 1.75 ulp, 65535, +/-70000,
a denormal half-way, -1.5, +inf).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
IvyNotFound added a commit to IvyNotFound/xcl that referenced this pull request Nov 25, 2026
# Conflicts:
#	crates/xcl/src/gpu/recomp/tests.rs
@Toons29

Toons29 commented Nov 25, 2026

Copy link
Copy Markdown

Regression run of b0aae2ec on the retail title, for this PR's Regression run section. Both blocks are from scripts/smoke.ps1 as printed, not edited: main on 568e7db7, this PR on its head commit with no uncommitted changes, same title and time.

xcl-smoke v1
commit: 568e7db7729c8fa6c8f0f4b32d798faaf11462bb
gpu: AMD Radeon RX 9070
title: 34df7c50
seconds: 60
exit: 0x0
frames shown: 1566 (PresentX 1566)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14592 MB
xcl-smoke v1
commit: b0aae2ecbb5c92bda70990ab3cb75475e869fbeb
gpu: AMD Radeon RX 9070
title: 34df7c50
seconds: 60
exit: 0x0
frames shown: 1627 (PresentX 1627)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14597 MB

Against the limits in CONTRIBUTING.md: 104 % of main's frames, 100 % of its peak commit, no crash, no device removal.

AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, cargo build --release -p xcl, 60 s, XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.

AI use: Claude Code (Claude Opus 5.5) built both trees and ran scripts/smoke.ps1; a script posted this right after the run, with both blocks exactly as printed.

@thehollowedone

Copy link
Copy Markdown

Here is the requested scripts/smoke.ps1 pair from clean release builds of main 1a0bbe3 and PR head 0cab3ef.

RTX 3080 Ti (621.35), Ryzen 7 9800X3D, Windows 11 build 26300, Rust 1.99.1 MSVC. The 60 s offline runs used separate copies of the same starting profile and DXIL seed, no input, PSO preload off, a 16 GiB job commit cap, and a 12 GiB system commit floor.

xcl-smoke v1
commit: 1a0bbe3a2ba91ea8f5b6fc29130a053868d3c9e6
gpu: NVIDIA GeForce RTX 3080 Ti
title: retail-private-fixture
seconds: 60
exit: 0x0
frames shown: 1641 (PresentX 1641)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14266 MB
xcl-smoke v1
commit: 0cab3ef3b46a9b522fd8838051f2c404baa5fa62
gpu: NVIDIA GeForce RTX 3080 Ti
title: retail-private-fixture
seconds: 60
exit: 0x0
frames shown: 1639 (PresentX 1639)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14365 MB

The two D16 buffer tests also passed in separate processes with XCL_DIRECT_DXIL=0 and =1. cargo test --offline --locked -p xcl --lib gpu_buffer_d16_loads_of -- --nocapture --test-threads=1 gave 2 passed, 0 failed, 0 ignored, and 0 skips in each mode.

The smoke pair covers startup. AMD hardware, Wine, and world load were outside this test.

AI-assisted. Codex (GPT-6, exact variant unavailable) ran the tests and drafted this report.

@throny

throny commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Two contributors checked independently and found that #2360 solves the same problem (for #2026) in a different way: #1607, group D102. You may want to compare them with the other author and agree on one, or say in the PR why both are needed.

This is a hint from the community triage in #1607, not a maintainer decision.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

@raylitebull

Copy link
Copy Markdown
Member

Diff review (norm16_rep/d16_fmt in recomp/emit.rs + dxil/lower_mem.rs and f16_rtz float D16 truncation) and green CI look solid, with exact-head 0cab3ef NVIDIA and smoke runs verified.

Remaining before merge:

  1. An exact-head 0cab3ef AMD test run (previous RX 6800M and RX 9070 runs were on earlier commits 83d3d05 and b0aae2e).
  2. Confirm resolution/preference vs #2360 per the #1607 (D102) triage note.

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@raylitebull On the two points:

  1. AMD run at 0cab3ef: I only have an RTX 3070, so this needs someone with a Radeon card. The earlier RX 6800M and RX 9070 runs were on 83d3d05 and b0aae2e.
  2. Against Recompiler: round normalized D16 buffer loads exactly #2360 (D102): both round a UNORM / SNORM code to f16 once, to nearest, and NWProgramming00 found both exact for every code of widths 2 to 16 (#1607 comment). They differ in the other non-integer channels. This PR truncates them to f16 (f16_rtz, in both backends). Recompiler: round normalized D16 buffer loads exactly #2360 keeps f32tof16, which rounds to nearest. NWProgramming00 measured buffer_load_format_d16_x on an RDNA2 gfx1036 with 32_FLOAT: the hardware truncates (6,011 of 6,011 values match truncation, 3,028 match round-to-nearest). Separately, SlopTart's run of Recompiler: round normalized D16 buffer loads exactly #2360 at 647932f (comment) shows DXC refusing its prelude helper, because snorm is a reserved HLSL keyword used as a parameter name there.

So on those measurements this PR matches the hardware in both parts. If the maintainers prefer one PR, #2360's UNORM / SNORM routine and this one give the same results for those codes.

AI-assisted (Claude Code, Claude Opus 5.5).

@DavidM1911

Copy link
Copy Markdown
Contributor

Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

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.

10 participants