Skip to content

Recompiler: D16 image loads of 32-bit integer texels saturate to 16 bits - #2042

Open
upipibi wants to merge 5 commits into
xcl-dev:mainfrom
upipibi:fix/image-d16-int-saturate
Open

upipibi wants to merge 5 commits into
xcl-dev:mainfrom
upipibi:fix/image-d16-int-saturate

Conversation

@upipibi

@upipibi upipibi commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #2030 (itself on #2027): only the last commit (Recompiler: D16 image loads of 32-bit integer texels saturate to 16 bits) belongs to this PR. It edits the same image D16 packing as #2030. Part of #2026; XCL-BT-028 in #1489.

image_load with D16=1 kept the low 16 bits of a 32-bit UINT / SINT texel, so 70000 loaded as 0x1170. RDNA2 saturates instead: UINT to 0xFFFF, SINT to -32768..32767 (boykopovar/AnyPS5#22750, gfx1036). This is the image counterpart of #1977's buffer change.

What is in it

  • recomp/emit.rs: the image-load D16 half closure saturates integer channels by class (min(e, 0xFFFF) for UINT, a clamp for SINT) instead of & 0xFFFF.
  • dxil/lower_image.rs: Lw::d16_int, used at the load and MSAA-load D16 sites.
  • recomp/tests.rs: gpu_image_d16_loads_saturate_32_bit_integers covers R32_UINT and R32_SINT textures with values in range and past both ends.

8- and 16-bit integer formats never leave that range, so they're 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

Tested

Independent Windows/D3D12 check on exact head 86e9cef31fd6cc7294f70f7a90d649e61a0dd62b. Windows 11 25H2 (26200.9974), Ryzen 7 9800X3D, Radeon(TM) Graphics (32.0.21042.62, native wave64) and 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.
  • cargo test --locked -p xcl --lib gpu::recomp::tests::gpu_image_d16_loads_saturate_32_bit_integers -- --exact --test-threads=1 --nocapture passed on both GPUs with XCL_DIRECT_DXIL=0 and 1 (1 passed, 0 failed, 0 ignored, 0 skipped per run).
  • cargo test --locked -p xcl --lib gpu::dxil::msaa_tests -- --test-threads=1 --nocapture passed on both GPUs (5 passed, 0 failed, 0 ignored, 0 skipped per run).
  • cargo test --locked -p xcl --test d16_integer_msaa_gpu_probe -- --exact d16_msaa_integer_reads_saturate_each_sample --test-threads=1 --nocapture used a temporary Direct DXIL probe with 4× MSAA R32_UINT and R32_SINT textures. It checked D16 IMAGE_LOAD and IMAGE_MSAA_LOAD against CPU saturation, including both signed limits and zero for slots past the sample count. Each GPU checked 64 single-sample and 256 packed-sample values per format. The probe also ran on parent 2d941f0c62772661a3c844f5f7767122ee31f39e, where it explicitly expected the old low-16-bit behavior. All four probe runs passed their assertions (1 passed, 0 failed, 0 ignored, 0 skipped per run).
Mismatches against saturation, per GPU Parent single / packed PR head single / packed
R32_UINT 20/64, 55/256 0/64, 0/256
R32_SINT 32/64, 82/256 0/64, 0/256

Not tested

Real-title smoke runs, the full workspace suite, and other multisample counts. The extra multisample probe covered Direct DXIL.

AI use

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

@raylitebull

Copy link
Copy Markdown
Member

Diff audit on 86e9cef looks clean (Lw::d16_int in lower_image.rs + matching emit.rs UINT/SINT clamp + gpu_image_d16_loads_saturate_32_bit_integers, already verified across RTX 3070, RTX 5090, and AMD iGPU).

Remaining before merge:

  1. Rebase once #2027 and #2030 rebase onto main (dropping the manual TRANSLATOR_VERSION bump in kernel.rs, since #1947 now auto-hashes gpu/dxil sources via XCL_DIRECT_DXIL_SOURCE_ID).
  2. Add paired xcl-smoke v1 blocks (main and PR) for PR description.

upipibi and others added 3 commits November 24, 2026 20:06
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>
…rest

image_load / image_sample / image_gather4 with D16 packed each channel
with f32tof16 of the f32 the view returned: rounded twice, the second time
toward zero on D3D12. RDNA2 rounds code / (2^w - 1) once to nearest even:
123 of the 256 8_8_8_8_UNORM codes came back one unit in the last place
low (code 9: 0x2884 for 0x2885). The channels go through d16_fmt with the
T#'s format, as the buffer format loads do. xcl-dev#2026.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
image_load with D16 kept the low half of a 32-bit UINT / SINT texel (70000
loaded as 0x1170). RDNA2 saturates: UINT to 0xFFFF, SINT to -32768..32767
(AnyPS5#22750, gfx1036; XCL-BT-028 in xcl-dev#1489). 8- and 16-bit integer
formats never leave that range. Both paths. xcl-dev#2026.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@upipibi
upipibi force-pushed the fix/image-d16-int-saturate branch from a4296ca to 7add2e7 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

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

Tested

Independent Windows/D3D12 check on clean PR #2042 head 7add2e7250844bec3c130835417ce3a33a6d498a, stacked on #2030 at 7009727951604107093a31e9bdc8775a0979a7d8. The PR head matched before and after execution.

  • Intel Arc A770 (driver 32.0.101.9175), AMD Ryzen 7 3800X, Windows 11 Enterprise Evaluation 26200.9974, Rust 1.100.0 x86_64-pc-windows-msvc, DXC/DXIL 1.9.2609.5, unoptimized test profile.
  • cargo test --locked -p xcl --lib -j 2 --no-run succeeded outside the Arc lock. 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 DXC 1.9.2609 x64 directory under %LOCALAPPDATA%, I ran this command under L:/XclTooling/Invoke-ArcTest.ps1 -WaitMinutes 60 -Test { … } once per backend.
$testExe = 'L:\XclTooling\pr1947-1831ec4-t3-97236edf\target\debug\build\xcl\97fdf2b9ff8e1482\out\xcl-97fdf2b9ff8e1482.exe'
& $testExe gpu::recomp::tests::gpu_image_d16_loads_saturate_32_bit_integers --exact --nocapture --test-threads=1
Backend Passed Failed Ignored GPU skips Filtered out
HLSL/DXC (XCL_DIRECT_DXIL=0) 1 0 0 0 1121
Direct DXIL (XCL_DIRECT_DXIL=1, no fallback in this test) 1 0 0 0 1121

Each run checked 64 lane results per format. R32_UINT saturated to 0xFFFF and R32_SINT saturated at both -32768 and 32767. In-range texels produced their expected values. This covers the signed and unsigned integer IMAGE_LOAD cases. The prior #2030 result covers normalized image conversion, and #1977 covers D16 buffer FormatLoad.

Not tested

MSAA and mip image loads, odd-channel high-half preservation, the full workspace suite, retail-title smoke runs, and other GPU adapters. The #2030 and #1977 tests were outside this run.

AI use

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

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>
(cherry picked from commit b0aae2e)
@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

IvyNotFound added a commit to IvyNotFound/xcl that referenced this pull request Nov 25, 2026
# Conflicts:
#	crates/xcl/src/gpu/recomp/tests.rs
d16_fmt reads a UNORM / SNORM value back as a code of the format and
rounds code / (2^w - 1) once. A load, a gather or a point-sampled texel is
such a code; a filtered sample is a blend of texels (or mips) and is not, so
rounding it to a code quantizes it again: half way between codes 0 and 255
gave 0x3804 (code 128), not 0x3800 (reported on xcl-dev#2030, RTX 3090 and AMD).

Both paths now pack a sample whose S# can filter (XY_MAG / XY_MIN_FILTER
not point, or MIP_FILTER linear, as desc::sampler_d3d12 maps them) with
f16_rtz of the host's value, as d16_fmt packs float channels. Gathers and
point samplers keep d16_fmt. A static sampler decides it when translating;
otherwise S# word 2 decides it when the shader runs. image_get_lod's D16
result, an LOD and not a texel, goes back to f32tof16 in the HLSL path, as
the direct DXIL path packs it.

gpu_image_d16_filtered_samples_keep_the_blend samples a 2x1 RGBA8_UNORM
texture between codes 0 and 255 with heap and static linear samplers and
checks f16_rtz of the f32 sample in every lane (0x3800 half way), and codes
9 and 128 through heap and static point samplers (0x2885, 0x3804).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 2d4b3a5)
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