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

Open
github-bot wants to merge 2 commits from github-bot/gh-3131 into main AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 09:32 UTC · original
fuzeofficlal:fix/game-pipelines-stencil-writes-reachability → main · +61 −6 · 1 files · 2 commits · open

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.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 09:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/3131) `fuzeofficlal:fix/game-pipelines-stencil-writes-reachability` → `main` · +61 −6 · 1 files · 2 commits · open ## 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:08:10 +00:00
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-26 09:48 UTC · original

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

**@⁠cloph-dsp** on GitHub · 2026-11-26 09:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/3131#issuecomment-7188492608) 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).
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 13:30 UTC · original · approved these changes

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

**@⁠Dukestar69** on GitHub · 2026-11-26 13:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/3131#pullrequestreview-6074983544) · **approved these changes** 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).
Author
Owner

@⁠AurumCarlos on GitHub · 2026-11-26 13:32 UTC · original · reviewed

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.

**@⁠AurumCarlos** on GitHub · 2026-11-26 13:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/3131#pullrequestreview-6075008321) · **reviewed** 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.
Match depth_stencil_desc's D3D12 BOOL semantics for StencilEnable (c[3] != 0) instead of checking c[3] == 1, and cover non-1 non-zero values (2 and u8::MAX) as well as 0 in stencil_write_mask_requires_a_reachable_op.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 16:46 UTC · original

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 16:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/3131#issuecomment-7198072093) 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`.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3131/head:github-bot/gh-3131'
git switch 'github-bot/gh-3131'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3131'
git switch 'github-bot/gh-3131'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3131'
git switch 'github-bot/gh-3131'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3131'
git switch 'main'
git merge --squash 'github-bot/gh-3131'
git switch 'main'
git merge --ff-only 'github-bot/gh-3131'
git switch 'main'
git merge 'github-bot/gh-3131'
git push origin 'main'
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!3131
No description provided.