Skip to content

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

Open
fuzeofficlal wants to merge 2 commits into
Marvisak:mainfrom
fuzeofficlal:fix/game-pipelines-stencil-writes-reachability
Open

fuzeofficlal wants to merge 2 commits into
Marvisak:mainfrom
fuzeofficlal:fix/game-pipelines-stencil-writes-reachability

Conversation

@fuzeofficlal

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown

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 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 AurumCarlos left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown

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

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

Copy link
Copy Markdown
Contributor

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 <andrewpozdnakov7-jpg@users.noreply.github.com>. AI-assisted with OpenAI Codex.

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.

6 participants