shader: lower image_sample_c_lz to SampleCmpLevelZero in pixel_aot #712
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/718"
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 roansystemlabs on 2026-11-22T22:24:36Z.
Change
pixel_aotloweredimage_sampleandimage_sample_lz, but no depth-comparison form. Everyimage_sample_c*stopped translation, so a pixel shader that reads a shadow map could not reach DXIL through the internal path. This PR adds the LOD-zero 2D form. Refs #710.It follows the scope agreed in #710 ("Please go ahead with a focused 2D
_c_lzPR…"):the ordinary-sampler path is unchanged, and the comparison-only path carries explicit desktop sampler state, which the test host binds from the decoded words;
mixed comparison and ordinary sampling is rejected;
LLVM 15 assembles and disassembles both address forms, showing the reference before the coordinates (Verification); the descriptor fields and the compare order cite Mesa, Linux and RADV (below);
the draws separate
LESSfromGREATER, use a reference equal to some texels so the strict and inclusive functions disagree, and use depth neighbourhoods where compare-then-filter disagrees with comparing a filtered depth (a negative control for the latter);one fixture holds the texture view, the host sampler and the generated HLSL, checked against an independent CPU reference.
image_sample_c_lz vD, v[a:a+2], s[T:T+7], s[S:S+3] dmask:0x1 dim:SQ_RSRC_IMG_2Dand the NSA spelling[vR, vU, vV]. In the RDNA 2 address order the reference comes before the coordinates. The result lowers totex.SampleCmpLevelZero(samp, float2(u, v), reference)withTexture2D<float> tex : register(t0)andSamplerComparisonState samp : register(s0). The reference may be a constant, an ALU result or an interpolated attribute. Operands use the same constant check as the other paths since #727 (params, soparams1[,params2[and later buffers are recognised).The four sampler words must still match the caller binding (
--sampler-words), as for ordinary sampling. In addition,comparison_sampler_state()decodes the fields that change a comparison result, and the generated HLSL names the desktop sampler it needs in a comment:D3D12_COMPARISON_FUNCorder minus one, asreference OP texel;D3D12_FILTER_COMPARISON_MIN_MAG_MIP_POINTor..._MIN_MAG_LINEAR_MIP_POINT;CLAMP_LAST_TEXEL, anisotropy, unnormalized coordinates,MC_COORD_TRUNC,FORCE_DEGAMMA, a min/max filter mode,MIN_LOD, LOD bias, different min and mag filters, mip filter 3, word 2 bits 28–31 (MIP_POINT_PRECLAMP,ANISO_OVERRIDE,BLEND_ZERO_PRT) and word 3 bits 12–29, which gfx10 leaves unnamed (bit 29 isUPGRADED_DEPTHon GFX8);TRUNC_COORD(word 0 bit 27) is part of the contract. On gfx10 it applies to X and Y for every filter (Mesaac_gpu_info.h). With 0, point sampling rounds to nearest even at six fraction bits before dropping the fraction (Mesa d573d1d8), so it can take the next texel when the coordinate is up to 1/128 texel below an edge; D3D takes the floor (D3D11.3 §7.18.7). Only the pairs RADV programs on gfx10 by default are accepted (radv_sampler.c), point with 1 and bilinear with 0; the other two are refused. RADV clears the bit for DXVK and vkd3d-proton (00-radv-defaults.conf), so point with 0 is a setting drivers use. Bilinear results are in the contract where the texel-space position is a multiple of 1/64;sample_c), and the image format is not decoded, so values outside [0, 1] and NaN are outside the contract. The generated HLSL states both premises in its comment;v255is refused;docs/experiments.md.Field positions follow
SQ_IMG_SAMP_WORD0…3(registers 0x8F30…0x8F3C) in Mesasrc/amd/registers/gfx10-rsrc.json:DEPTH_COMPARE_FUNCis word 0 bits 14:12,XY_MAG_FILTER/XY_MIN_FILTERword 2 bits 21:20 and 23:22. The enum values follow Linuxnavi10_enum.h(SQ_TEX_DEPTH_COMPARE_NEVER= 0 …ALWAYS= 7,SQ_TEX_CLAMP_LAST_TEXEL= 2,SQ_TEX_XY_FILTER_POINT/BILINEAR= 0/1).A program that mixes comparison and ordinary sampling, or uses the second texture, is refused, because both would need
s0. The ordinary sampling paths emit the same HLSL as before.tests/pixel_aot_vs.hlslhas one ordinary static sampler, and a static sampler cannot carry a per-case compare function. So I did not change it. The newoffscreen_check --check-pixel-compare(tests/pixel_compare_render.cpp) builds its own root signature with t0, b0 and a descriptor-heap sampler that comes from the case file. If you would rather bind this another way, I will change it: a second static sampler ats1, or a root signature emitted by the translator.New test
tests/pixel_compare_check.py: CPU controls with no arguments,binaryfor LLVM 15 anddrawfor D3D12. It is registered aspixel_aot_compare_{hardware,warp}and added to the Ubuntu LLVM 15 step. Docs: a new paragraph after theimage_sample_lzone indocs/experiments.md.Overlap with open PRs (checked with
git merge-treeagainst every open PR that touches these files): #694 renames thevmcntcounter inlower()(samples→vector_loads), so whichever of the two lands second needs a small rebase of the new branch; #680 adds a CI line at the same place inwindows.yml. No other open PR conflicts beyond its existing conflicts withmain. The branch merges cleanly withmainat67edabe.Verification
Commit
617374a, rebased onmainatfd2486f(after #727; #685 moved theoffscreen_checkoperations into a table, and--check-pixel-compareis a row there). Windows 11 build 26200.9974, D3D12 debug layer enabled, NVIDIA GeForce RTX 3070 (driver 621.01) and WARP.pixel_aot_compare_hardwareandpixel_aot_compare_warpeach reportdraws=104 negative_controls_detected=18. Every draw printedmismatches=0anddebug_errors=0 debug_warnings=0.The fixture draws an 8×8
R32_FLOATtexture into a 16×16R32G32B32A32_FLOATtarget and compares every result bitwise. Each row covers all 8 compare functions, 104 draws per adapter:EQUALand theLESS/LESS_EQUALsplit are visiblev[7:9]attr0.x; it varies per pixel and is never equal to a texelv_fma_f32of constant words[v20, v8, v9]LESS)The offsets are exact in binary32, and the CPU controls check every sum against the rational value.
The expected images come from an independent CPU model, using
Fractionarithmetic: compare each texel, clamp to the edge, then weight bilinearly. It does not call the translator. The test writes the sampler words from the field layout itself, not through the decoder. The host sampler state comes from the decoder, so a wrong decoded bit changes the draw.Negative controls, on each adapter, all failing with
pixel_mismatch(18 per adapter):GREATERandGREATER_EQUALagainst aLESSdescriptor; for the constant reference alsoLESS_EQUAL;0.5f);TRUNC_COORD0 (x=8 y=0 expected=1 actual=0on both adapters); on an edge, the texel below (x=8 y=0 expected=0 actual=1);LESS_EQUALagainstLESSandGREATER_EQUALagainstGREATER.The draws show the desktop side: the accepted point rule is the floor, and D3D does not round at six fraction bits. They cannot show the console rule; that comes from the Mesa sources above.
CPU controls, run in every mode:
TRUNC_COORD0 with point and 1 with bilinear;dmask:0x3, two or four address VGPRs,dim:SQ_RSRC_IMG_2D_ARRAYand implicit-LODimage_sample_care rejected;v256as the destination,v[254:256]and[v20, v8, v256]are rejected withVGPR out of range, andv255is accepted;b1(--extra-constant 20 0x60, word 1) lowers toSampleCmpLevelZerowith a reference equal toparams1[0].yundercbuffer Constants1 : register(b1); without that contract entry the same program is rejected;NEVERandALWAYS, the truncating and rounding models differ below an edge, and the floor and the texel below differ on an edge; at 0 and 1 the strict and inclusive functions differ;s_waitcnt vmcnt(0), a reference taken from an outstanding comparison, a mixed program, a second texture and no sampler binding are rejected;image_sample_lzstill emitsSamplerState.Mutation checks (local, not added, each reverted):
u, v, reference = values): caught by the CPU controls before any draw;D3D12_TEXTURE_ADDRESS_MODE_WRAPinstead ofCLAMPin the test host: caught on WARP atconstant bilinear LESS(x=5 y=0 expected=0 actual=0.0625);D3D12_FILTER_COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINTin place of the bilinear filter: caught on WARP (x=7 y=0 expected=0.25 actual=0). The opposite split (MIN_LINEAR_MAG_POINT) passed all 48 draws of the first commit: at LOD zero WARP uses the min filter. The decoder accepts only equal min and mag filters, so this split cannot reach the host;x=8 y=0 expected=0 actual=1). Moved down by 1/1024 texel: caught by the on-edge draw (x=8 y=0 expected=1 actual=0);TRUNC_COORDread from bit 26, the check removed, or point with 0 accepted: caught by the CPU controls;v255limit raised tov256, or applied to the destination or the address only: caught by the CPU controls;b1control:aluremoved from the operand prefixes, the extra contract ignored, orb1declared asregister(b0): each caught. Putting backparams[is not caught, and neither is droppingparamsfrom the prefixes: the operands are always VGPRs, and no instruction writes a constant-buffer value into a VGPR as is (a move makes it analuvalue). So the prefix change keeps the check identical to the other paths, like the VGPR-only checks #727 updated, rather than changing a result.Pure-Python checks from
AGENTS.md, pluspixel_aot_check.pywith no arguments: all exit 0.pixel_compare_check.py binaryneeds LLVM 15 with AMDGPU, which I don't have locally. It ran in this PR's CI at617374a, and passed: https://github.com/R0Y00/Xbox2Windows/actions/runs/45088811425. It assembles the four distinct programs (including the whole-quad edge program) with two descriptors usingllvm-mc-15and decodes them throughlibLLVM-15.so.1. LLVM printsimage_sample_c_lz v12, v[7:9], s[28:35], s[40:43] dmask:0x1 dim:SQ_RSRC_IMG_2Dandimage_sample_c_lz v12, [v20, v8, v9], .... Each decoded program translates to the same HLSL as its line fixture. The other jobs in that run also passed: the Windows build and tests, the Windows /O2 build and the Linux Python checks.GPU load (
nvidia-smi, RTX 3070): 4 % before and 2 % after the ctest run, with other processes using the GPU. About 5.6 GB of the 8 GB of VRAM was already in use by them, and the reading changed by 6 MiB. The draws are 16×16.pixel_aot_compare_hardwaretook 41 s and_warp10 s. In an earlier run of the same selection atdafb61e,pixel_aot_blend_scalar_hardwarehit its 60 s timeout while the GPU was shared; it passed alone and in later runs.Evidence and follow-up
tools/scan_unsupported_shaders.pyalready classifiesimage_sample_cas MIMG, so a rescan could count how many PS records stop at a comparison sample, and how many of those use_c_lzon 2D.image_sample_c(needs whole-quad coordinates),_c_l,_c_b,_c_o,_c_d,image_gather4_c*;TRUNC_COORDpairs, which would need a six-bit rounding emulation in HLSL. Point with 0 is the pair RADV programs for DXVK and vkd3d-proton.reference OP texel, the same as D3DSampleCmp. Mesa RADV maps Vulkan'sDref OP texelone to one ontoSQ_TEX_DEPTH_COMPARE. The draws confirm the D3D side only, not the console.tools/scan_unsupported_shaders.pydoes not listimage_sample_lzamong its supported regexes either. I left it unchanged to keep this PR to one behavior.Checklist
docs/experiments.md; the README row's-R '^pixel_aot_'already selects the new tests.)AI-assisted: yes
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T22:58:13Z.
The 48 readbacks and mismatch controls cover the intended compare/filter path. One profile boundary remains: the docs exclude exact filter boundaries with TRUNC_COORD=0, but the decoder currently accepts either bit value and the lowerer does not apply it. Please define the accepted coordinate/precision premise at the caller binding and add a boundary case that would distinguish the modes, or refuse the unsupported setting. Check that depth references outside the tested interval are either covered or explicitly outside the contract. Also add a rejection for
v256as the sample destination so the line entry point does not accept an invalid VGPR.Imported from R0Y00/Xbox2Windows. Originally posted by rwZephyr on 2026-11-23T00:02:18Z.
Independent Windows 10 run on
dafb61ebf2123f48987a5e32858e714f88c39d68. Both tests passed withctest --preset dev --no-tests=error -V -R '^pixel_aot_compare_(hardware|warp)$'. Each backend matched all 256 pixels in each of 104 draws and detected all 18 negative controls. No skips.In the focused
LESSreruns, both backends matched the expected images just below the texel edge, on the edge, and at bilinear texel centres. The wrong references produced 96, 96, and 192 pixel mismatches, respectively, on each backend. At pixel(8, 0), the red comparison result was0just below the edge and1on the edge. This verifies the D3D12 behavior for the accepted sampler settings.Windows 10 22H2
19045.6466, RTX 3060 Ti driver621.24(32.0.16.2124via DXGI), in-box WARP10.0.19041.5794, DXC1.8.2502.11, Debug/MTd. D3D12 debug layer unavailable on both backends.AI-assisted. All reported tests ran locally.
Imported from R0Y00/Xbox2Windows. Originally posted by roansystemlabs on 2026-11-23T00:06:50Z.
Thanks. All three points are in
dafb61e. I also rebased the branch on currentmain(434fabe): #685 moved theoffscreen_checkoperations into a table, so--check-pixel-compareis now a row there. Nothing else in the first commit changed.1.
TRUNC_COORD. I chose to refuse the unsupported setting. Emulating it would need six-bit coordinate rounding written out in HLSL. Mesa gives the semantics:ac_gpu_info.h);The decoder now accepts only the pairs RADV programs on gfx10 by default (
radv_sampler.c): point withTRUNC_COORD1, bilinear with 0. The other two pairs are refused, each with its own message. RADV clears the bit for DXVK and vkd3d-proton (00-radv-defaults.conf), so point with 0 is a setting drivers use; it is listed as a follow-up. Bilinear results are in the contract where the texel-space position is a multiple of 1/64. The docstring, the HLSL comment anddocs/experiments.mdstate this premise.New draws, each with all 8 compare functions:
LESS;The coordinate offsets are exact in binary32, and the CPU controls check every sum. The negative control for the
TRUNC_COORD0 image fails on both adapters (x=8 y=0 expected=1 actual=0), and so does the one for the texel below an exact edge (expected=0 actual=1). As a local check on WARP, HLSL that moves u and v up by 1/256 texel fails the below-edge draw, and HLSL that moves them down by 1/1024 texel fails the on-edge draw. This shows only the desktop side. The console rule comes from the Mesa sources above.2. Depth references. The contract is now references and texels in [0, 1], ends included. Two new programs use constant references 0 and 1 on a texture with texels equal to 0 and 1, with both filters and all 8 functions. Controls check
LESS_EQUALagainstLESSat 0 andGREATER_EQUALagainstGREATERat 1. Values outside [0, 1] and NaN are explicitly outside the contract. D3D quantizes and clamps the reference to the texture format (sample_c), and this lowering does not decode the image format.3.
v256. A destination, range or NSA operand pastv255is refused withVGPR out of range. The CPU controls coverv256as the destination,v[254:256]and[v20, v8, v256], and check thatv255is still accepted.Results at
dafb61e, Windows 11, D3D12 debug layer on, RTX 3070 (621.01) and WARP:ctest -R "^(pixel_aot_|offscreen_warp|sampled_coverage_|pixel_cb_read_|pixel_position_)": 31/31;pixel_aot_compare_hardwareand_warp:draws=104 negative_controls_detected=18. All 208 draws reportmismatches=0 debug_errors=0 debug_warnings=0;AGENTS.md: all exit 0;dafb61e, all four jobs green, including the LLVM 15 assembly and decode of the new edge program: https://github.com/roansystemlabs/Xbox2Windows/actions/runs/45086039933.The PR description is updated to match.
AI-assisted: yes
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T00:13:09Z.
Your
dafb61eupdate already resolves the dispatcher conflict, so I left my parallel change unpushed. One combined-path detail from #727 remains: the new sample operand check still acceptsparams[, while additional bound buffers generateparams1[,params2[, etc. Please apply the same constant operand check as the other paths and add a CPU control for one comparison operand from b1. The local integration test with that predicate adjustment passed 104 WARP draws and all 18 mismatch controls.Imported from R0Y00/Xbox2Windows. Originally posted by roansystemlabs on 2026-11-23T00:44:51Z.
Thanks, done in
617374a. I rebased onmainatfd2486f(after #727) first, so the combined path is what the branch now tests. It also merges cleanly withmainat67edabe.("sample", "alu", "params"), soparams1[,params2[and later buffers are accepted.b1(extra_constants=[(20, 0x60)],s_buffer_load_dwordof word 1, thenv_mov_b32 v7). It must lower toSampleCmpLevelZerowith a reference equal toparams1[0].yundercbuffer Constants1 : register(b1). Without that contract entry the same program is rejected.Local mutations of the new control: removing
alufrom the operand prefixes, ignoring the extra contract, or declaringb1asregister(b0)each fail it. Putting backparams[does not, and neither does droppingparamsfrom the prefixes. The three operands are always VGPRs, and no instruction writes a constant-buffer value into a VGPR as is: a move turns it into analuvalue, which is howb1reaches the reference in the new control. So the change makes this check identical to the other paths, like the VGPR-only checks #727 updated (thev_cvt_f16_f32destination andUNUSED_PRESERVE), but it changes no result today.Results at
617374a, Windows 11, D3D12 debug layer on, RTX 3070 (621.01) and WARP:ctest -R "^(pixel_aot_|offscreen_warp|sampled_coverage_|pixel_cb_read_|pixel_position_)": 35/35;pixel_aot_compare_hardwareand_warp:draws=104 negative_controls_detected=18. All 208 draws reportmismatches=0 debug_errors=0 debug_warnings=0;AGENTS.md, pluspixel_aot_check.pyandpixel_compare_check.py: all exit 0;617374a: the four jobs passed, https://github.com/R0Y00/Xbox2Windows/actions/runs/45088811425.The PR description is updated to match.
AI-assisted: yes