Game: fix stencil write-mask reachability for NEVER/ALWAYS funcs #3129

Open
fuzeofficlal wants to merge 2 commits from upstream-pr/3131 into main
fuzeofficlal commented 2026-11-26 09:32:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T09:32:00Z.


Summary

pipelines::stencil_writes (crates/victory-game/src/pipelines.rs) previously misclassified two unreachable stencil operations when deciding whether D3D12_DEPTH_STENCIL_DESC1.StencilWriteMask can be cleared to zero:

  1. When StencilFunc is NEVER (1) with StencilFailOp == KEEP (1) and a non-KEEP StencilDepthFailOp, it checked depth_fail != KEEP and returned true even though the stencil test never passes.
  2. When StencilFunc can pass, StencilPassOp != KEEP, and DepthEnable != 0 with DepthFunc == NEVER (1), it fell through to else { true } even though the depth test never passes.

Fix

  • Replace the branch condition with direct per-operation reachability checks matching D3D12 rules:
    • fail: StencilFailOp != KEEP && StencilFunc != ALWAYS
    • depth_fail: StencilDepthFailOp != KEEP && StencilFunc != NEVER && depth_enabled && DepthFunc != ALWAYS
    • pass: StencilPassOp != KEEP && StencilFunc != NEVER && (!depth_enabled || DepthFunc != NEVER)
  • Align depth_enabled (c[0] != 0) with depth_stencil_desc.
  • Add synthetic unit test stencil_write_mask_requires_a_reachable_op covering unreachable and reachable ops on both front and back faces (fails on main returning 0x0F instead of 0).

Verification

  • cargo test -p victory-game --locked: 16 passed, 0 failed.
  • cargo clippy -p victory-game --all-targets --locked -- -D warnings and --all-features: clean.
  • rustfmt --check --edition 2024 --config skip_children=true crates/victory-game/src/pipelines.rs: clean.
  • cargo test -p xtask --locked policy::tests::repository_passes_policy: passed.
<!-- greyfinch-sync:Marvisak/VIctory:issue:5507469089 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3131). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T09:32:00Z. --- ## Summary `pipelines::stencil_writes` (`crates/victory-game/src/pipelines.rs`) previously misclassified two unreachable stencil operations when deciding whether `D3D12_DEPTH_STENCIL_DESC1.StencilWriteMask` can be cleared to zero: 1. When `StencilFunc` is `NEVER` (`1`) with `StencilFailOp == KEEP` (`1`) and a non-`KEEP` `StencilDepthFailOp`, it checked `depth_fail != KEEP` and returned `true` even though the stencil test never passes. 2. When `StencilFunc` can pass, `StencilPassOp != KEEP`, and `DepthEnable != 0` with `DepthFunc == NEVER` (`1`), it fell through to `else { true }` even though the depth test never passes. ## Fix - Replace the branch condition with direct per-operation reachability checks matching D3D12 rules: - `fail`: `StencilFailOp != KEEP && StencilFunc != ALWAYS` - `depth_fail`: `StencilDepthFailOp != KEEP && StencilFunc != NEVER && depth_enabled && DepthFunc != ALWAYS` - `pass`: `StencilPassOp != KEEP && StencilFunc != NEVER && (!depth_enabled || DepthFunc != NEVER)` - Align `depth_enabled` (`c[0] != 0`) with `depth_stencil_desc`. - Add synthetic unit test `stencil_write_mask_requires_a_reachable_op` covering unreachable and reachable ops on both front and back faces (fails on `main` returning `0x0F` instead of `0`). ## Verification - `cargo test -p victory-game --locked`: 16 passed, 0 failed. - `cargo clippy -p victory-game --all-targets --locked -- -D warnings` and `--all-features`: clean. - `rustfmt --check --edition 2024 --config skip_children=true crates/victory-game/src/pipelines.rs`: clean. - `cargo test -p xtask --locked policy::tests::repository_passes_policy`: passed.
cloph-dsp commented 2026-11-26 10:05:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T09:48:10Z.


Independent local verification of head 49fa81e on Windows 11, Rust 1.100.0/MSVC:

  • cargo test --locked -p victory-game --lib pipelines::tests::stencil_write_mask_requires_a_reachable_op — 1 passed, 0 failed.
  • git diff --check origin/main HEAD — clean.

Synthetic pipeline state only; no game assets or DRM material used. AI-assisted (Hy4 flash).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7188492608 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3131#issuecomment-7188492608). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-26T09:48:10Z. --- Independent local verification of head `49fa81e` on Windows 11, Rust 1.100.0/MSVC: - `cargo test --locked -p victory-game --lib pipelines::tests::stencil_write_mask_requires_a_reachable_op` — 1 passed, 0 failed. - `git diff --check origin/main HEAD` — clean. Synthetic pipeline state only; no game assets or DRM material used. AI-assisted (Hy4 flash).
Dukestar69 commented 2026-11-26 13:30:44 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T13:30:44Z.

Review: APPROVED


Verified against origin/main (63bcca4):

  • cargo test -p victory-game -- --test-threads=1: 16/16 passed (including pipelines::tests::stencil_write_mask_requires_a_reachable_op).
  • cargo clippy -p victory-game --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly accounts for NEVER/ALWAYS comparison functions when determining stencil write-mask reachability.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6074983544 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3131#pullrequestreview-6074983544). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T13:30:44Z. **Review:** APPROVED --- Verified against `origin/main` (`63bcca4`): - `cargo test -p victory-game -- --test-threads=1`: 16/16 passed (including `pipelines::tests::stencil_write_mask_requires_a_reachable_op`). - `cargo clippy -p victory-game --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly accounts for `NEVER`/`ALWAYS` comparison functions when determining stencil write-mask reachability. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
AurumCarlos commented 2026-11-26 13:32:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-26T13:32:33Z.

Review: COMMENTED


Looks right to me, and much easier to read than the old nested condition. With a face as [FailOp, DepthFailOp, PassOp, Func]:

  • fail: reachable when the stencil test can fail (Func != ALWAYS).
  • depth fail: reachable when the stencil can pass (Func != NEVER) and the depth test can fail, which needs depth on and DepthFunc != ALWAYS. With depth off the depth test always passes, so DepthFailOp never runs.
  • pass: reachable when both tests can pass (Func != NEVER, and depth off or DepthFunc != NEVER).

That's the D3D12 rule for each op, and both cases from the description were real on main. For case 1, the old branch returned depth_fail != KEEP && … without looking at Func == NEVER. I checked the test table against these three rules, front and back.

c[0] != 0 for DepthEnable is also right: depth_stencil_desc writes c[0] as-is into a D3D12 BOOL, where any nonzero value is TRUE.

The same applies to StencilEnable, which still uses c[3] == 1 (in the last line of stencil_writes). Since depth_stencil_desc also passes c[3] through unchanged as a BOOL, a title that sets stencil enable to some other nonzero value gets stencil enabled on the host, but stencil_writes returns false, so the write mask is zeroed and its stencil writes are silently dropped. That's pre-existing, but it's the same class of bug as the c[0] & 1 you just fixed, so c[3] != 0 would fit in this PR, with one more test row (e.g. stencil enable 2 with a reachable op → WRITE_MASK).

Nit: in the description, "Replace the branch condition with direct per-operation reachability checks matching D3D12 rules:" ends with a colon but no list follows.

<!-- greyfinch-sync:Marvisak/VIctory:review:6075008321 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3131#pullrequestreview-6075008321). Originally posted by [AurumCarlos](https://github.com/AurumCarlos) on 2026-11-26T13:32:33Z. **Review:** COMMENTED --- Looks right to me, and much easier to read than the old nested condition. With a face as `[FailOp, DepthFailOp, PassOp, Func]`: - **fail**: reachable when the stencil test can fail (`Func != ALWAYS`). - **depth fail**: reachable when the stencil can pass (`Func != NEVER`) *and* the depth test can fail, which needs depth on and `DepthFunc != ALWAYS`. With depth off the depth test always passes, so `DepthFailOp` never runs. - **pass**: reachable when both tests can pass (`Func != NEVER`, and depth off or `DepthFunc != NEVER`). That's the D3D12 rule for each op, and both cases from the description were real on `main`. For case 1, the old branch returned `depth_fail != KEEP && …` without looking at `Func == NEVER`. I checked the test table against these three rules, front and back. `c[0] != 0` for `DepthEnable` is also right: `depth_stencil_desc` writes `c[0]` as-is into a D3D12 `BOOL`, where any nonzero value is TRUE. **The same applies to `StencilEnable`, which still uses `c[3] == 1`** (in the last line of `stencil_writes`). Since `depth_stencil_desc` also passes `c[3]` through unchanged as a `BOOL`, a title that sets stencil enable to some other nonzero value gets stencil enabled on the host, but `stencil_writes` returns `false`, so the write mask is zeroed and its stencil writes are silently dropped. That's pre-existing, but it's the same class of bug as the `c[0] & 1` you just fixed, so `c[3] != 0` would fit in this PR, with one more test row (e.g. stencil enable `2` with a reachable op → `WRITE_MASK`). Nit: in the description, "Replace the branch condition with direct per-operation reachability checks matching D3D12 rules:" ends with a colon but no list follows.
fuzeofficlal commented 2026-11-26 16:46:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T16:46:12Z.


Good catch, thanks @AurumCarlos! Pushed 8933db72 changing StencilEnable in stencil_writes from c[3] == 1 to c[3] != 0 so it matches depth_stencil_desc's D3D12 BOOL handling (just like DepthEnable), and expanded stencil_write_mask_requires_a_reachable_op to cover StencilEnable values 0, 1, 2, and u8::MAX.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7198072093 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3131#issuecomment-7198072093). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T16:46:12Z. --- Good catch, thanks @AurumCarlos! Pushed `8933db72` changing `StencilEnable` in `stencil_writes` from `c[3] == 1` to `c[3] != 0` so it matches `depth_stencil_desc`'s D3D12 `BOOL` handling (just like `DepthEnable`), and expanded `stencil_write_mask_requires_a_reachable_op` to cover `StencilEnable` values `0`, `1`, `2`, and `u8::MAX`.
futurepro62 commented 2026-11-27 11:33:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-27T11:33:59Z.


NVIDIA full-suite run, to go with the reports above by Dukestar69, cloph-dsp, since none of them names a GPU run. Tested exact head 8933db7 (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 867 passed, 0 failed, 10 ignored.
  • The new tests ran and passed: stencil_write_mask_requires_a_reachable_op.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7217221294 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3131#issuecomment-7217221294). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-27T11:33:59Z. --- NVIDIA full-suite run, to go with the reports above by Dukestar69, cloph-dsp, since none of them names a GPU run. Tested exact head `8933db7` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **867 passed, 0 failed, 10 ignored**. - The new tests ran and passed: `stencil_write_mask_requires_a_reachable_op`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
andrewpozdnakov7-jpg commented 2026-11-28 14:09:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T14:09:51Z.


One additional reachability case on the current 8933db7 head is a zero stencil read mask. It makes the two comparison operands equal, regardless of the stored value or stencil reference. LESS, GREATER and NOT_EQUAL then always fail. EQUAL, LESS_EQUAL and GREATER_EQUAL always pass.

Follow-up commit 1343d0c is directly on your head. It uses that rule when deciding whether to keep the write mask. The emitted comparison enum and read mask are unchanged.

I compared the reconstructed write mask to an independent CPU model across 768 generated states, covering both faces, all three stencil operations, eight comparisons, four depth settings and four read masks. Current main 63bcca4 disagreed in 126 cases. Your head disagreed in 54, all with a zero read mask. The follow-up agreed in all 768. It merged cleanly with #3048 at 79d344a and passed the same matrix there. The game crate passed 17 tests on the follow-up and 21 on the combination, with strict Clippy and asset policy passing on both.

I also verified the comparison rule directly with a one-sample draw and occlusion queries on WARP 1.0.22 with Agility 1.620.2. With reference 1 and stored stencil 0, a zero read mask passed EQUAL, LESS_EQUAL, GREATER_EQUAL and ALWAYS. The one-bit control instead passed GREATER, NOT_EQUAL, GREATER_EQUAL and ALWAYS. Sixteen GPU queries agreed with the expected values with the debug layer off and on. The GPU diagnostics suite with the probe included had 247 passes and one ignored in both modes.

Please retain the Git author if cherry-picking the follow-up. If folding it into another commit, please add Co-authored-by: andrewpozdnakov7-jpg <[email protected]>. AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7240463888 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3131#issuecomment-7240463888). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-28T14:09:51Z. --- One additional reachability case on the current `8933db7` head is a zero stencil read mask. It makes the two comparison operands equal, regardless of the stored value or stencil reference. LESS, GREATER and NOT_EQUAL then always fail. EQUAL, LESS_EQUAL and GREATER_EQUAL always pass. [Follow-up commit `1343d0c`](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/1343d0ca8c389aaa5e2bb33be1cf0deb876e6803) is directly on your head. It uses that rule when deciding whether to keep the write mask. The emitted comparison enum and read mask are unchanged. I compared the reconstructed write mask to an independent CPU model across 768 generated states, covering both faces, all three stencil operations, eight comparisons, four depth settings and four read masks. Current main `63bcca4` disagreed in 126 cases. Your head disagreed in 54, all with a zero read mask. The follow-up agreed in all 768. It merged cleanly with #3048 at `79d344a` and passed the same matrix there. The game crate passed 17 tests on the follow-up and 21 on the combination, with strict Clippy and asset policy passing on both. I also verified the comparison rule directly with a one-sample draw and occlusion queries on WARP 1.0.22 with Agility 1.620.2. With reference 1 and stored stencil 0, a zero read mask passed EQUAL, LESS_EQUAL, GREATER_EQUAL and ALWAYS. The one-bit control instead passed GREATER, NOT_EQUAL, GREATER_EQUAL and ALWAYS. Sixteen GPU queries agreed with the expected values with the debug layer off and on. The GPU diagnostics suite with the probe included had 247 passes and one ignored in both modes. Please retain the Git author if cherry-picking the follow-up. If folding it into another commit, please add `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>`. AI-assisted with OpenAI Codex.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3131:upstream-pr/3131'
git switch 'upstream-pr/3131'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3131'
git switch 'upstream-pr/3131'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3131'
git switch 'upstream-pr/3131'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3131'
git switch 'main'
git merge --squash 'upstream-pr/3131'
git switch 'main'
git merge --ff-only 'upstream-pr/3131'
git switch 'main'
git merge 'upstream-pr/3131'
git push origin 'main'
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!3129
No description provided.