Shaders: a pixel shader writing SV_StencilRef declares StencilRef #382

Merged
Dufftwt merged 1 commit from upstream-pr/382 into main 2026-11-24 08:09:30 +00:00
Dufftwt commented 2026-11-24 02:48:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dufftwt on 2026-11-24T02:48:23Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6573484261 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/382). Originally posted by [Dufftwt](https://github.com/Dufftwt) on 2026-11-24T02:48:23Z. --- ## 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.
jeremygold02 commented 2026-11-24 06:55:41 +00:00 (Migrated from github.com)

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 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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7121927381 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/382#issuecomment-7121927381). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T04:20:24Z. --- 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.
upstream-sync closed this pull request 2026-11-24 09:05:27 +00:00
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!382
No description provided.