Shaders: a pixel shader writing SV_StencilRef declares StencilRef #382
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/382"
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 Marvisak/VIctory. Originally posted by Dufftwt on 2026-11-24T02:48:23Z.
What's wrong
exp mrtzchannel y, the stencil value a pixel shader exports, is lowered to anSV_StencilRefoutput (lower.rs). But the module never declares it:StencilRef, bit 11;SFI0part never hasD3D_SHADER_FEATURE_STENCIL_REF,0x200.DXC sets both for any pixel shader whose output signature contains
SV_StencilRef(DxilShaderFlags.cpp), and the DXIL validator checks the module's flags against what it actually uses (ValidateShaderFlags, "Flags must match usage"). Our translation of a stencil-exporting pixel shader therefore contradicts itself. The validator rejects it, and the runtime isn't told that the shader needs the stencil-reference output feature.What the fix does
flags::STENCIL_REF = 1 << 11is added toemit/shader.rs's flag list.Shadersets it when a pixel shader has an output of kind 20 (SV_StencilRef).SFI0features map it to0x200, like the other flags already mapped there (0x800for typed UAV loads, and so on).How it behaves after the fix
Pixel shaders that export a stencil reference declare
StencilRefin their flags andSFI0, as DXC would. Every other shader is unchanged.Tests
The new test
a_stencil_export_declares_the_stencil_ref_feature(inemit/mod.rs) translatesexp mrtz v0, v1(depth and stencil). The output signature hasSV_DepthandSV_StencilRef, and the test checks thatSFI0has0x200.Where
dxil.dllis available, as on GitHub's Windows runner, the test also passes the shader through the validator, asstub_pipeline_is_createdandconstant_color_validatesalready do.I ran
cargo test --workspaceon GitHub'swindows-latestwith Rust 1.98.1 fromrust-toolchain.toml, and the shader crate's tests on Linux:dxil.dllaccepts the shader.victory-shaderhas 70 passed, the other crates have 0 failed.main:SFI0is0x0, anddxil.dllreports: (2048 is bit 11,StencilRef.)--test-threads=1. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (#181, #244).Not tested: a title's stencil-exporting shaders on a GPU.
Related: #294 adds two other feature bits right next to this one in
SFI0. Whichever merges second needs a trivial rebase.Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T04:20:24Z.
Thanks @Dufftwt for fixing the stencil export metadata. I tested head
c2f60d6bddc228c5be5feb6e08daccbf6c4e7f94with:Result: 1 passed, 0 failed (69 filtered out). The synthetic shader emission test writes
SV_StencilRefand checks that the emitted container declares theStencilReffeature. It validates metadata generation, not stencil output in a rendered frame.