Skip to content

Shaders: a pixel shader writing SV_StencilRef declares StencilRef - #382

Merged
Marvisak merged 1 commit into
Marvisak:mainfrom
Dufftwt:fix/stencil-ref-shader-flag
Nov 24, 2026
Merged

Marvisak merged 1 commit into
Marvisak:mainfrom
Dufftwt:fix/stencil-ref-shader-flag

Conversation

@Dufftwt

@Dufftwt Dufftwt commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

What's wrong

exp mrtz channel y, the stencil value a pixel shader exports, is lowered to an SV_StencilRef output (lower.rs). But the module never declares it:

  • the entry's shader flags (metadata tag 0) never have StencilRef, bit 11;
  • the SFI0 part never has D3D_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 << 11 is added to emit/shader.rs's flag list.
  • Shader sets it when a pixel shader has an output of kind 20 (SV_StencilRef).
  • The SFI0 features map it to 0x200, like the other flags already mapped there (0x800 for typed UAV loads, and so on).

How it behaves after the fix

Pixel shaders that export a stencil reference declare StencilRef in their flags and SFI0, as DXC would. Every other shader is unchanged.

Tests

The new test a_stencil_export_declares_the_stencil_ref_feature (in emit/mod.rs) translates exp mrtz v0, v1 (depth and stencil). The output signature has SV_Depth and SV_StencilRef, and the test checks that SFI0 has 0x200.

Where dxil.dll is available, as on GitHub's Windows runner, the test also passes the shader through the validator, as stub_pipeline_is_created and constant_color_validates already do.

I ran cargo test --workspace on GitHub's windows-latest with Rust 1.98.1 from rust-toolchain.toml, and the shader crate's tests on Linux:

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.

exp mrtz's channel y is lowered to an SV_StencilRef output, but the
module's shader flags never had StencilRef (bit 11) and SFI0 never had
D3D_SHADER_FEATURE_STENCIL_REF (0x200). DXC sets both for a pixel
shader whose output signature has the stencil reference, and the
validator checks the flags against the module.

Both are now set when a pixel shader outputs SV_StencilRef.
muhammed-aksoy pushed a commit to muhammed-aksoy/VIctory that referenced this pull request Nov 24, 2026
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
… StencilRef

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jeremygold02

Copy link
Copy Markdown
Contributor

Thanks @Dufftwt for fixing the stencil export metadata. I tested head c2f60d6bddc228c5be5feb6e08daccbf6c4e7f94 with:

cargo test --locked -p victory-shader --lib a_stencil_export_declares_the_stencil_ref_feature -- --test-threads=1

Result: 1 passed, 0 failed (69 filtered out). The synthetic shader emission test writes SV_StencilRef and checks that the emitted container declares the StencilRef feature. It validates metadata generation, not stencil output in a rendered frame.

@ghost ghost mentioned this pull request Nov 24, 2026
@Marvisak
Marvisak merged commit faba64c into Marvisak:main Nov 24, 2026
suirentakamura referenced this pull request in suirentakamura/victory-suiren Nov 24, 2026
Shaders: a pixel shader writing SV_StencilRef declares StencilRef
itsonlysimbo pushed a commit to itsonlysimbo/VIctory that referenced this pull request Nov 24, 2026
Shaders: a pixel shader writing SV_StencilRef declares StencilRef
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.

3 participants