Skip to content

Shaders: pixel programs the console tests depth before keep early depth - #2085

Open
0xkn0wh3r3 wants to merge 3 commits into
Marvisak:mainfrom
0xkn0wh3r3:shader/early-depth-before-shader
Open

0xkn0wh3r3 wants to merge 3 commits into
Marvisak:mainfrom
0xkn0wh3r3:shader/early-depth-before-shader

Conversation

@0xkn0wh3r3

@0xkn0wh3r3 0xkn0wh3r3 commented Nov 25, 2026 •

Copy link
Copy Markdown

The problem

Some console pixel programs run only for pixels that pass the depth test. On the host they ran for hidden pixels too, and kept what those pixels wrote.

A console pixel program compiled with [earlydepthstencil] runs with DB_SHADER_CONTROL.DEPTH_BEFORE_SHADER set (bit 12, as AMD's register headers name it, e.g. Linux's gc_10_3_0_sh_mask.h; bit 16 next to it is the ordered-section bit the translator already reads). Depth and stencil are then tested before the program runs, so it runs only for pixels that pass, even when it writes unordered views.

The translation never set DXIL's matching flag (ForceEarlyDepthStencil, shader flags bit 3, DxilShaderFlags.h). A host runs a pixel program that writes unordered views with the depth test after it. So for such programs it ran every hidden pixel too and kept those pixels' writes:

  • work the console skips;
  • writes the console never makes (counters, visibility or decal buffers written for occluded pixels).

The fix

  • emit/lower.rs: pixel programs with DEPTH_BEFORE_SHADER get flags::EARLY_DEPTH (new in emit/shader.rs, bit 3).
  • A program with the bit that also exports depth keeps the flag. The console tests the rasterized depth first then too, and DXIL's validator has no rule against the pair.

Programs without the bit are unchanged.

Testing

New tests in emit::synthetic_tests:

  • pixel_programs_tested_before_they_run_keep_that_order: lowers a pixel program that stores (1, 0, 0, 1) to an unordered view, with and without the bit, and draws it: one triangle at depth 0.5 over a D32 buffer, tested LESS.

    draw RTX 5070 Ti WARP
    depth cleared to 1 (visible), flagged stores stores
    depth cleared to 0 (hidden), flagged nothing stored nothing stored
    hidden, same program without the bit (main's translation) stores stores

    The first row shows the draw, depth test and view binding work. The last shows the host runs an unflagged program before the depth test and keeps its store, so the test tells the two apart. With the flag-setting line removed, the test fails.

  • a_program_tested_first_that_exports_depth_keeps_the_flag: the flag with an SV_Depth output; both devices create and draw the pipeline.

The test host gains:

  • a depth-tested graphics pipeline (Host::depth_tested_pipeline);
  • a vertex shader taking its position from the vertex (position_vs; the stub's position is zero);
  • a draw over a depth buffer with a texture UAV read back (Host::draw_behind_depth).

Runs:

Not covered

  • No dxil.dll on this machine, so the validator branch of the tests did not run.
  • Not measured in a title run (no game data here).

A console pixel program compiled with `[earlydepthstencil]` runs with
`DB_SHADER_CONTROL.DEPTH_BEFORE_SHADER` (bit 12, as AMD's register
headers name it; the bit 16 next to it is the ordered-section bit the
translator already reads): depth and stencil are tested before it runs,
so it runs only for pixels that pass, even when it writes unordered
views. The translation never set DXIL's matching flag
(`ForceEarlyDepthStencil`, shader flags bit 3). A host runs a pixel
program that writes unordered views with the depth test after it, so for
such programs it ran every hidden pixel too and kept those pixels'
writes: work the console skips, and writes it never makes (counters,
visibility or decal buffers written for occluded pixels).

The flag is now set for pixel programs with the bit.

The new test lowers a pixel program that stores to an unordered view
with and without the bit, checks the flag follows it, and has the
hardware device and WARP take the flagged program into a graphics
pipeline (both did, on an RTX 5070 Ti and WARP).
The test of the early-depth flag checked only that the flag follows
DEPTH_BEFORE_SHADER and that the host takes the program. It now draws
it: one triangle at depth 0.5 over a depth buffer, tested LESS, with the
program storing (1, 0, 0, 1) to an unordered view.

- Depth cleared to 1 (visible): the flagged program stores, so the
  draw, the test and the view binding work.
- Depth cleared to 0 (hidden): the flagged program stores nothing.
- Hidden, the same program without the bit: the host runs it before the
  depth test and keeps its store. This is what the flag prevents, and
  shows the test tells the two apart.

All three hold on the RTX 5070 Ti and on WARP.

A program with the bit that also exports depth keeps the flag: the
console tests the rasterized depth first then too, DXIL's validator has
no rule against the pair, and both devices take and draw it (new test).

The test host gains a depth-tested graphics pipeline, a vertex shader
that takes its position from the vertex (the stub's is zero) and a draw
over a depth buffer with a texture UAV, read back.

@DenRakEiw DenRakEiw 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 locally merged onto origin/main (940d32f, Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC, with dxil.dll validator):

  • cargo test -p victory-shader --lib pixel_programs_tested_before_they_run_keep_that_order — passed on both RTX 5090 hardware and VICTORY_D3D12_WARP=1
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean preservation of early depth testing for pixel shaders whose console flags request it, verified end-to-end with draw_behind_depth on hardware and WARP. Looks great.

AI-assisted (Gemini 4 Neon).

@jeremygold02

Copy link
Copy Markdown
Contributor

Triage verdict — Keep early-depth lowering, initialize the UAV readback fixture (3202102)

Evidence

  • @0xkn0wh3r3 preserves the console’s early-depth flag in DXIL. The new fixture covers the observable difference: a depth-rejected pixel leaves the UAV alone with the flag and writes without it. The fixture clears depth but does not explicitly seed the committed UAV, so the no-store expectation currently relies on its initial contents. UAV fixture · Depth clear

Checks and limits

  • Both exact-head tests passed on hardware D3D12 and WARP. A disposable extension explicitly validated the flagged SV_Depth shader with DXIL validation and its draw also passed.
  • The SV_Depth fixture disables depth writes and uses equal rasterized and exported depth. That establishes acceptance of the combination. It leaves stored-depth behavior untested. Depth-export fixture

Next step

  • Seed the UAV to a known value and order that initialization before the draw, so no-store is deterministic. If validating depth-export parity later, use different rasterized and exported values with depth writes enabled and inspect the stored depth.

This verification was AI-assisted.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, cargo test -p victory-shader --locked.

144 passed / 0 failed / 2 ignored. Pixel programs the console tests depth before keep early — early-depth-test ordering fix; pairs with #2085's console-behavior theme (#2043 discard lanes).

zhaibingye added a commit to zhaibingye/VIctory that referenced this pull request Nov 25, 2026
Preserve the native early-depth indication in pixel DXIL. The source synthetic depth-tested draw distinguishes occluded UAV stores on RTX and WARP. Linux shader tests and Windows workspace test source cross-check passed on the merged tree.
Review (Marvisak#2085): the hidden-pixel check expected the UAV texel to read
zero, which rested on the committed texture's initial contents.
`Host::draw_behind_depth` now clears the UAV to a known value
(`Host::UAV_SEED`) before the draw, with a UAV barrier ordering the
clear first, and the tests expect that value where nothing was stored:
hidden pixels of the flagged program, and every texel but the one
written.
@0xkn0wh3r3

Copy link
Copy Markdown
Author

Thanks @jeremygold02, done in 3c76d58. draw_behind_depth now clears the UAV to a known value (Host::UAV_SEED, [0.25, 0.5, 0.75, 0.125]) before the draw, with a UAV barrier ordering the clear first. The tests expect that value wherever nothing was stored: the hidden pixels of the flagged program, and every texel except the one written. So "no store" no longer depends on what a new texture happens to contain. Both tests pass on an RTX 5070 Ti. Separate depth-export parity, with different rasterised and exported depths and depth writes on, is a good follow-up; I have left it out of this PR.

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head 3c76d58 onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

In the lowering setup (emit/lower.rs), pixel programs whose DB_SHADER_CONTROL has DEPTH_BEFORE_SHADER (bit 12) set now get DXIL's ForceEarlyDepthStencil flag (shader flags bit 3, new flags::EARLY_DEPTH in emit/shader.rs). Without it, the host runs programs that write UAVs for depth-rejected pixels too. Bit 12 matches AMD's public GFX10 register layout, next to bit 16, which lower.rs already reads for POPS. The new synthetic_tests draw behind a depth buffer and now seed the UAV first (Host::UAV_SEED), so 'nothing stored' no longer depends on what a new texture happens to contain.

Open point: a program that also exports depth keeps the flag, and only its pipeline creation is tested. D3D's early depth ignores the shader's depth output, so stored-depth behaviour in that case is unverified. CI: no check runs on the current head (not run).


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants