Shaders: a pixel shader writing SV_StencilRef declares StencilRef #382

Manually merged
github merged 1 commit from gh-pr/382/head into gh-pr/382/base 2026-11-26 04:01:55 +00:00
Owner

@⁠Dufftwt on GitHub · 2026-11-24 02:48 UTC · original
Dufftwt:fix/stencil-ref-shader-flag → main · +70 −0 · 2 files · 1 commits · merged 2026-11-24 08:09 UTC

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:

  • This PR: everything passes, and dxil.dll accepts the shader. victory-shader has 70 passed, the other crates have 0 failed.
  • The new test alone on main: SFI0 is 0x0, and dxil.dll reports:
    error: Flags must match usage.
    note: Flags declared=0, actual=2048
    
    (2048 is bit 11, StencilRef.)
  • I ran with --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.

**@⁠Dufftwt** on GitHub · 2026-11-24 02:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/382) `Dufftwt:fix/stencil-ref-shader-flag` → `main` · +70 −0 · 2 files · 1 commits · merged 2026-11-24 08:09 UTC ## 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: - **This PR:** everything passes, and `dxil.dll` accepts the shader. `victory-shader` has 70 passed, the other crates have 0 failed. - **The new test alone on `main`:** `SFI0` is `0x0`, and `dxil.dll` reports: ``` error: Flags must match usage. note: Flags declared=0, actual=2048 ``` (2048 is bit 11, `StencilRef`.) - I ran with `--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.
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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:01:53 +00:00
github manually merged commit c2f60d6bdd into gh-pr/382/base 2026-11-26 04:01:55 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 04:20 UTC · original

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.

**@⁠jeremygold02** on GitHub · 2026-11-24 04:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/382#issuecomment-7121927381) 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.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!382
No description provided.