Skip to content

Recompiler: round normalized D16 buffer loads exactly - #2360

Closed
mokkamokacaffe wants to merge 2 commits into
xcl-dev:mainfrom
mokkamokacaffe:recomp/d16-normalized-buffer-rounding
Closed

mokkamokacaffe wants to merge 2 commits into
xcl-dev:mainfrom
mokkamokacaffe:recomp/d16-normalized-buffer-rounding

Conversation

@mokkamokacaffe

@mokkamokacaffe mokkamokacaffe commented Nov 25, 2026 •

Copy link
Copy Markdown

Part of #2026.

D16 typed-buffer loads currently decode UNORM/SNORM source codes to binary32 and then use f32tof16. The intermediate binary32 rounding makes roughly half of the normalized codes one binary16 ULP too low. Convert the original integer code directly to binary16 so it is rounded once, to nearest-even.

This is the first split proposed in #2026. Image reads remain a separate follow-up.

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs: emit an integer-only normalized-code-to-binary16 conversion for HLSL buffer_load_format_d16_* and tbuffer_load_format_d16_* operations.
  • Avoid the HLSL-reserved snorm identifier in the generated helper; independent DXC runs on the original head exposed this as the cause of the broad shader-compilation failures.
  • crates/xcl/src/gpu/dxil/lower_mem.rs: emit the same conversion directly in DXIL while preserving destination selection, integer formats and hi_x behavior.
  • Add exhaustive CPU coverage for UNORM and SNORM widths 2, 7, 8, 10, 11, 15 and 16, including zero, signed saturation and binary16 normal/subnormal boundaries.

Tested

  • Linux x86-64, Rust 1.100.0, compile-only cross-check against x86_64-pc-windows-msvc:
    • cargo check -p xcl --lib --target x86_64-pc-windows-msvc: passed.
    • cargo check -p xcl --tests --target x86_64-pc-windows-msvc: passed.
    • A local lib.exe-compatible archive wrapper was used only to let the Windows-only libudis86-sys build script complete; no Windows binary was linked or run.
  • Independent exhaustive arithmetic harness: 203,528 UNORM/SNORM code-and-width cases matched a binary64 nearest-even reference, 0 mismatches.
  • git diff --check origin/main...HEAD: passed.
  • Rebased onto main at e1d84b7; the conflict in emit.rs retained both this PR's exhaustive D16 test and main's newer RLA shader tests.
  • Clean post-merge diff against main: only crates/xcl/src/gpu/recomp/emit.rs and crates/xcl/src/gpu/dxil/lower_mem.rs, 241 additions and 13 deletions; git diff --check origin/main...HEAD passed at 62ff8aed9394.

Regression run

main:

Pending: requires Windows, a D3D12 GPU and a real-title installation.

this PR:

Pending on clean head 62ff8aed9394: requires Windows, a D3D12 GPU and the same title and duration as the main run.

Not tested

  • The repository tests on Windows; the added Rust test was compiled but not executed here.
  • Post-fix HLSL compilation and Direct-DXIL validation with the Windows SDK. Independent tests of the previous head found that the generated HLSL used reserved identifier snorm; this is renamed to signed_norm at the current head and needs a clean rerun.
  • GPU output parity on AMD or NVIDIA hardware.
  • Paired scripts/smoke.ps1 retail-title runs.
  • D16 normalized image reads; those remain the second split of Recompiler: D16 reads of UNORM/SNORM data are one ULP low for about half the codes (f32 then truncating f32tof16) #2026.

AI use

Convert UNORM and SNORM source codes directly to binary16 so the intermediate binary32 decode cannot double-round the result. Cover every supported normalized code width exhaustively.

Part of xcl-dev#2026

Assisted-by: OpenAI Codex (GPT-5)
@FIJI1299

Copy link
Copy Markdown

Tested commit <647932f> on Windows with an NVIDIA GPU (wave32):

Environment

  • OS: Windows 11
  • GPU: NVIDIA GeForce RTX 5070 (12 GB VRAM, driver 621.35, wave32 only — no native wave64)
  • DXC: 1.8.2502.11 (Windows SDK 10.0.26100.0)

Results

  • Full library test suite (cargo test -p xcl --lib): 1060 passed, 147 failed, 26 ignored (including gpu::recomp::tests::buffer_format_d16_packs_and_unpacks_halves).
  • GPU tests skipped on this adapter: 4 (no wave64), 1 (cannot disable the driver cache (Windows Developer Mode is required): 0x887A002D).

Note

This branch appears to predate the wave32 / split-wave64 updates on main (current main skips 13 no wave64 tests on this adapter instead of 4), causing 124 tests in gpu::recomp::tests to fail on wave32-only NVIDIA hardware. Rebasing onto current main should allow re-testing on this GPU.

@throny

throny commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Two contributors checked independently and found that #2027 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.

@SlopTart

Copy link
Copy Markdown

Tested: current PR head 647932fc merged onto current origin/main (clean), Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT (NAVI31), cargo target x86_64-pc-windows-gnu run under wine-11.0 (Proton), rustc 1.94.0, vkd3d-proton 3.1 with the XCL patch, official MS DXC (binaries 2026-09-29, 1.9.2609.5 (01b62ad47)).

One red across the whole lane, and the cause is visible in every failure. The gpu_ suite at this head gives 40 passed; 108 failed with a cold shader cache (deterministic at --test-threads=1 and --test-threads=2). 106 of the failures print the same DXC compile error in the generated prelude:

dxc: hlsl.hlsl:437:47: error: modifiers must appear before type
uint norm_code_to_f16(uint code, uint w, bool snorm) {
                                              ^
dxc: hlsl.hlsl:439:29: error: expected expression
    uint period = w - (uint)snorm;

snorm is a reserved HLSL modifier keyword, so this pinned DXC refuses the parameter name. The helper sits in the shared translator prelude, so every program that embeds the prelude fails to compile, which is why the failure count is a large fraction of the suite rather than one test.

Standalone repro against the same pinned dxc.exe (extracted from the 2026-09-29 zip), no XCL involved:

uint norm_code_to_f16(uint code, uint w, bool snorm) {
    uint period = w - (uint)snorm;
    return period;
}

gives error: modifiers must appear before type at column 47 plus error: expected expression on (uint)snorm.

Observations around the new tests themselves:

  • targeted run of the new D16 tests at this head with a cold cache: 1 passed (that path does not compile the HLSL prelude), and the new D16 tests are not among the 106 failures.
  • an earlier run I left with a warm cache showed only 41 failures, so the pass count depends on how much compiled DXIL is still cached; the cold-cache number (108 failed) is the clean signal.

Renaming the parameter (anything that is not a modifier keyword) would restore the prelude compiles. The exact rounding behaviour of the change could not be judged on this lane because the HLSL backend does not compile at this head.

Not tested: retail titles (no title data on this lane, counts only). The post-rename rounding behaviour itself.

AI use: Hermes agent (local Qwen-class) ran both machines and drafted this; I checked it before posting.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

  • cargo test -p xcl --lib: 143 real failures, deterministic (also fail --test-threads=1, 1064/143):

Every DXC compile of the generated HLSL prelude fails:

dxc: hlsl.hlsl:437:47: error: modifiers must appear before type
uint norm_code_to_f16(uint code, uint w, bool snorm) {
                                              ^
hlsl.hlsl:439:29: error: expected expression
    uint period = w - (uint)snorm;
hlsl.hlsl:440:25: error: expected expression
    bool negative = and(snorm, (code & (1u << (w - 1u))) != 0u);

The parameter name snorm collides with the HLSL snorm texture modifier keyword — rename it (e.g. signed_norm) and the whole suite should recover. Serious breakage, needs a fix before merge.

— Devin

@bmjask bmjask 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.

No concrete numeric defect found; the endpoint/subnormal/selectors logic appears consistent across backends. This draft is not merge-ready: it lacks Windows CI, DXC/Direct-DXIL validation, GPU output parity, and paired smoke. The exhaustive test validates only the Rust helper, not emitted HLSL or the separate Direct-DXIL lowering. Add a GPU D16 typed-buffer test for both backends covering UNORM/SNORM tie-adjacent normal/subnormal values, selectors, and hi_x.

@mokkamokacaffe

Copy link
Copy Markdown
Author

The updated head 62ff8aed resolves the merge conflict and the generated-HLSL failure by renaming the reserved snorm parameter to signed_norm.

After comparing this with #2027 and the RDNA2 measurements linked there, #2027 is the more complete implementation. Both PRs produce the intended single-rounding result for UNORM/SNORM codes, while #2027 also applies the measured truncation behavior to the other non-integer D16 channels and has GPU coverage for both the HLSL and Direct DXIL paths, exact-head NVIDIA validation, and paired smoke runs.

Closing this PR in favor of #2027 so testing and review remain focused there.

@mokkamokacaffe
mokkamokacaffe deleted the recomp/d16-normalized-buffer-rounding branch November 25, 2026 23:27
@DavidM1911

Copy link
Copy Markdown
Contributor

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

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.

6 participants