Recompiler: export SV_StencilRef from exp mrtz channel 1 with adapter gating #3296
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3296"
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 ivall/xcl. Originally posted by Kalmarv on 2026-11-29T06:20:47Z.
What is in it
Exports pixel-shader
SV_StencilRef(system_value == 69) fromexp mrtz(tgt == 8) channel 1 in both the default Direct DXIL backend (crates/xcl/src/gpu/dxil/kernel.rs,crates/xcl/src/gpu/dxil/lower_gfx.rs) and the HLSL backend (crates/xcl/src/gpu/recomp/emit.rs), matching Mesaac_nir_lower_ps_late.c::emit_ps_mrtz_export, with adapter capability gating incrates/xcl/src/gpu/recomp/pso.rs:SPI_SHADER_32_GR/32_ABGR,COMPR=0,EN & 2): readsVSRC1[7:0](& 0xFF).SPI_SHADER_UINT16_ABGR,COMPR=1,EN & 3): readsVSRC0[23:16]((VSRC0 >> 16) & 0xFF), and guards depth writes with!comprso compressed MRTZ never overwritesSV_Depth.SV_StencilRefwhen bothOSG1declaressystem_value == 69and anexp mrtzenables channel 1 (avoiding false positives on GFX10SPI_SHADER_32_ARMRT0-alpha exports or depth-only exports), and omits the dummySV_Target7for stencil-only pixel shaders.PSSpecifiedStencilRefSupported): probesD3D12_FEATURE_DATA_D3D12_OPTIONS::PSSpecifiedStencilRefSupportedinsidepso::graphics_wave32_for_device. On adapters that reportFALSE(such as NVIDIA D3D12 drivers, which reject PSOs declaringSV_StencilRefwithE_INVALIDARG), stripssystem_value == 69before translation and isolates disk/RAM cache keys (_nostencil) for those shaders only so existing caches stay untouched.ps_mrtz_stencil_ref_writes_sv_stencil_ref(recomp/tests.rs): HLSL codegen + DXC 1.9 compilation across uncompressed, depth+stencil, compressed, compressed+coverage, unused, stencil-only, and undeclared cases.ps_mrtz_stencil_ref_direct_and_gpu(dxil/core_tests.rs): Direct DXILdxil.dllvalidation +SFI00x200flag checks across all cases, plus end-to-end 3-pass D3D12D24_UNORM_S8_UINTstencil-buffer draw verification (StencilPassOp = REPLACEwith decoyOMSetStencilRef(0x11), followed byEQUALpass on expected ref andEQUALnegative check on decoy) for all 4 combinations (Direct DXIL + HLSL,COMPR=0+COMPR=1), falling back to WARP when the hardware adapter lacksPSSpecifiedStencilRefSupported.ps_stencil_ref_gating_strips_and_splits_cache_filename(recomp/pso.rs): verifies capability stripping and_nostencilcache filename isolation.Tested
Hardware:
NVIDIA GeForce RTX 5070 Ti(driver32.0.16.1074),AMD Ryzen 9 9950X,Windows 11 Pro (Build 26200),rustc 1.99.0 (b940084d7),XCL_DXC_DIRwith DXC1.9.2609.5.cargo check --workspace --all-targets: 0 errors.ps_mrtz_stencil_ref_writes_sv_stencil_ref,ps_mrtz_stencil_ref_direct_and_gpuon WARP because RTX 5070 Ti reportsPSSpecifiedStencilRefSupported = FALSE,ps_stencil_ref_gating_strips_and_splits_cache_filename): 3 passed, 0 failed (0 skips).cargo test --workspace -- --test-threads=4): 2611 passed, 0 failed, 49 ignored (26SKIPPED: 21no wave64on NVIDIA wave32, 3 pre-existing#1426f16 refusals, 2no heap windows).Regression run
main-ancestor baseline (f0e8fdd5c3942ddd399d5ee11384c8595c5aacff):This branch (
537d0a06fb7a87dbe235821e7e9e554fccf4c773, 0 minidumps, 0 System display events):Not tested
PSSpecifiedStencilRefSupported = TRUE(end-to-end D3D12 stencil-buffer draw verification executed on Microsoft WARP D3D12 device because NVIDIA D3D12 drivers reportPSSpecifiedStencilRefSupported = FALSE).STENCIL_OP_VAL(VSRC1[15:8]) andSTENCIL_WRITE_MASK(VSRC1[23:16]), which D3D12/DXIL has no pixel-shader output semantic for.AI use
Written, verified, and tested with an AI coding assistant (
Antigravity), with all unit tests, DXIL validation, WARP D3D12 stencil-buffer draw tests, full workspace GPU tests, and 150 sxcl-smokeruns executed on local hardware.Imported from ivall/xcl. Originally posted by Foniass on 2026-11-29T14:59:06Z.
Review (head
537d0a06, diff read, not a merge-check):git merge-treegives conflicts inlower_gfx.rsandcore_tests.rs), and the two use different designs. Whichever merges second needs to addstencil_ref.is_none()to #3252's dummy-target condition.ps_stencil_ref_allowed()is true until a device probe says otherwise, andxcl shaders --precompilenever probes, so on NVIDIA (which reports the capability FALSE) the precompiled pixel shaders keepSV_StencilRefand miss the_nostencilcache key at run time, as far as I can see.PSSpecifiedStencilRefSupported = FALSE, so the draw test ran only on WARP (the description says so).Suggested verdict: not yet (rebase against #3252 or the other way round).
(Review written with Claude Code, Claude Opus 5.5.)
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T21:17:31Z.
Review: COMMENTED
Review of
537d0a06: 2 finding(s). 2 are inline.These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T21:17:32Z.
Code review location:
crates/xcl/src/gpu/recomp/pso.rs; line 1573The PSSpecifiedStencilRefSupported check only runs as a side effect of
graphics_wave32_for_device, and when it hasn't run the default is "allowed".xcl shaders --precompilewith no startup device (shaders.rs:3421 usesrunner::graphics_wave32, which never checks), or with an explicit--split(theunwrap_or_elseskips the closure), leaves PS_STENCIL_REF_SUPPORTED at 0. Every PS declaring system_value 69 is then stored with SV_StencilRef under<hash>_Pixel_..._0.bin. At runtime on NVIDIA the value becomes 1,translated::fileadds_nostenciland misses, so all those shaders are translated again at draw time. Tools on the runner device that build pixel PSOs get SV_StencilRef PSOs that NVIDIA rejects with E_INVALIDARG.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T21:17:32Z.
Code review location:
crates/xcl/src/gpu/recomp/pso.rs; line 1518The strip decision is one process-wide atomic that the last checked device overwrites, and it's read separately for the RAM key, each disk filename and the translation. If a translation starts at 0 (no
_nostencilin its ShaderKey or file path) and another thread's first PSO sets it to 1 while DXC is compiling,strip_ps_stencil_ref_if_unsupportedstrips the shader, or a store re-runsfile()and picks the other name. Either way the output is cached under the name meant for the other adapter. With two devices in one process (WARP next to hardware, or several adapters), whichever was checked last decides every later translation. Passing the capability explicitly, likesplit_wave64, would remove the race and also fix the precompile case above.Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T19:38:57Z.
Closing: conflicts with merged #3252 (
7ac333736),SV_StencilRefcannot run on NVIDIA, and precompile cache keys would miss.Pull request closed