startup: unpack 2D A16 coordinates for image_sample and image_sample_lz #1990
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1997"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by consolelogkyo on 2026-11-24T22:51:47Z.
Summary
Fixes silent miscompilation of 2D
image_sampleandimage_sample_lzinstructions carrying thea16packed 16-bit coordinate modifier in the startup shader translator (Refs #1723).Background & What Changed
On RDNA 2 (
gfx1013), 2Dimage_sampleandimage_sample_lzwitha16 = 1pack both IEEE binary16 texture coordinates into a single VGPR (uin bits15:0,vin bits31:16) without an NSA extension — the same single-register layout already handled for plain 2Dimage_load(#1854). Previously:tools/disasm_rdna2.py(decode_instruction): Only checkedop_name == "image_load"when collapsing 2Da16addr_countto1, so 2Da16image_sampleandimage_sample_lzdisassembled with a two-register rangev[N:N+1]instead ofvNand did not flag a spurious NSA extension (invalid_nsa).tools/startup/gfx2hlsl.py(Stage.image): Thesample/lzpath checked neither modifiers nordmasknor address-register counts on 2D, 3D, or 2D-array images. As a result, a 2Da16sample translated without error into the shader cache while reading the raw packed word as binary32uand the adjacent VGPRv(N+1)as binary32v— sampling wrong texels at draw time. Unmodeled modifiers (unorm,r128,tfe,lwe,d16,reserved,invalid_nsa, anda16on 3D / 2D-array dimensions) and emptydmask:0x0were likewise silently dropped.tools/startup/gfx2hlsl.py&src/startup_stubs/d3d12_draw.inl): Both the offlinepipeline_keyseed and the desktop runtimerevision[]advance in lockstep fromx2w-startup-shader-5tox2w-startup-shader-6. User-visible effect: any existingx2w-startup-shader-5shader cache is regenerated on the next run rather than reused, because previously cacheda16sample pipelines hold the miscompiled two-VGPR translation.With this change:
tools/disasm_rdna2.pydecodes one address VGPR (vN) for 2Da16image_load,image_sample, andimage_sample_lz, surfacinginvalid_nsaifnsize > 0(leavingimage_sample_land 3D / 2D-arraya16forms unchanged).Stage.imageunpacks 2Da16image_sampleandimage_sample_lzcoordinates asfloat2(f16tof32(p), f16tof32(p >> 16))(f16tof32is the built-in Shader Model 5.0 HLSL intrinsic, so generated shaders compile under FXCps_5_1and DXCps_6_0directly with no helper definition incommon()), preserves cache-policy flags (dlc,glc,slc) and valid 2D NSA pairs, and refuses unmodeled modifiers, emptydmask, wrong address-register counts, and 3D / 2D-arraya16withUnsupported.tests/offscreen_check.cpp/tests/startup_image_3d.cppadd--startup-image-2d(single-slice2x2R32G32B32A32_FLOATTexture2D, accepting exact0x...32-bit packed coordinate words inud1) so translated 2DTexture2Dshaders can be drawn unmodified under both DXC (ps_6_0) and FXC (ps_5_1).Verification
tests/startup_image_sample_a16_check.py):disasm_rdna2.decode_instructionsingle-VGPRa16decoding andinvalid_nsaflagging, lockstep offline/runtimex2w-startup-shader-6cache keys,SamplevsSampleLevel(..., 0)lowering, non-v0packed coordinate registers (v2), partialdmask, 2D NSA[v0, v3], cache-policy flags (glc,slc,dlc), 1D guard preservation, and refusal ofunorm,tfe,lwe,d16,r128,invalid_nsa, all 6 reserved MIMG bits,dmask:0x0, 3D / 2D-arraya16, and wrong address-register counts..github/workflows/windows.ymlfor bothbinary llvm-mc-15assembly parity anddecode libLLVM-15.so.1disassembly parity.startup_image_sample_a16_warp&startup_image_sample_a16_hardware):82c07000, Windows 11 26H2 (build26300.9974), D3D12 debug layer enabled (debug_errors=0 debug_warnings=0), with both DXC (ps_6_0) and FXC (ps_5_1) on:1414:008c, driver10.0.26100.9818)1002:7550, driver32.0.32029.2010)sample_a16,lz_a16, andlz_v2_cache) match all four2x2quadrants plus negative low-half (u = -0.25) and negative high-half (v = -0.25, bit 31 set) wrap cases, while all 6 mutated HLSL negative controls (pre-fixfloat2(F(v0), F(v1)), swapped halves, dropped>> 16,>> 15,>> 17, and hardcodedv0instead ofv2) fail readback with the expected mismatched texel.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T22:54:57Z.
Ran it, broke it five ways — all caught, decoder and translator independently
Windows 11 build 26300.9974, device-free:
tools/disasm_rdna2.py:op_name in ("image_load", "image_sample", "image_sample_lz")→op_name == "image_load"(the pre-change line)tools/disasm_rdna2.py:addr_count = 1droppedtools/startup/gfx2hlsl.py:f16tof32(p >> 16)→f16tof32(p)forv(both halves read the low half)tools/startup/gfx2hlsl.py: the empty-dmaskrefusal disabledtools/startup/gfx2hlsl.py: the address-register count check disabledThe first is the original defect, so the fixture genuinely fails without the fix. The third is the miscompilation this PR is about — reading the
raw packed word as binary32
uand the neighbour asv— and it is caught at the text level, which is the right level for it.Two things I checked rather than assumed
f16tof32needs no local definition. It appears at seven sites butcommon()defines onlymul_legacy,ftz32,mad32,med3,class32and friends. It is the SM 5.0 HLSL intrinsic, so the generated shader compiles under FXC
ps_5_1without a preamble addition — worth saying in thedescription, because a reader who greps for a definition will not find one.
ufrom bits15:0,vfrom31:16, matching theimage_loadA16 layout handled since #1854. Thefixture pins both halves in order (
float2(f16tof32(v0), f16tof32(v0 >> 16))), so a swap cannot pass.On the refusals this PR adds
The
sample/lzpath previously checked neither modifiers nordmasknor the address count on 2D, 3D and 2D-array images, and droppedunorm,r128,tfe,lwe,d16,reserved,invalid_nsaanda16-on-3D/array silently. Turning nine silent drops into refusals is the larger half ofthis change by risk, and three of my mutations land there — all caught, so the whitelist is exercised.
One thing worth stating in the description
The cache revision moves to
x2w-startup-shader-6. That is a user-visible behaviour: an existing cache from-5is regenerated rather than reused.Saying so explicitly, with the reason (previously cached A16 samples hold the wrong translation), prevents someone from "restoring" the old revision
to avoid the rebuild.
Scope
No draw was run here; the changes to
d3d12_draw.inl,offscreen_check.cppandstartup_image_3d.cppare part of the WARP/hardware route that thedescription does not claim to have verified for this PR. Everything above is the decoder and the emitted text.
Verdict
Reviewed. Five independent breakages, five failures, decoder and translator each pinned separately.
Notes
Imported from R0Y00/Xbox2Windows. Originally posted by consolelogkyo on 2026-11-24T23:01:47Z.
Thank you @ChazY11 for the thorough review and the five-way mutation check!
I updated the PR description to call out both points explicitly:
f16tof32is the built-in SM 5.0 HLSL intrinsic (sofxcps_5_1compiles it directly without a helper incommon()).x2w-startup-shader-6intentionally regenerates existing-5shader caches on the next run so pre-fixa16sample translations are not reused.AI-assisted.