fix(agc): put back depth and stencil of quad-masked pixels #28229

Open
Meh7an wants to merge 1 commit from upstream-pr/28229 into main
Meh7an commented 2026-11-29 10:23:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Meh7an on 2026-11-29T10:23:01Z.


What

Stacked on #28218: its commit is the first one here, and this PR is the second commit only.

With #28218, a draw that applies a quad sample mask in the pixel shader under early depth or stencil tests, from a pixel program that writes memory, runs under EarlyFragmentTests. The early tests then also run for the masked pixels, so #28218 still refuses such a draw when it writes depth or stencil: the masked pixels would get the draw's depth and stencil, which on the console they never reach.

This PR lifts that refusal. For such a draw (prepareDraw, MaskedDepth), the depth-stencil target is copied before the draw's render pass (RecordMaskedDepthSave). After the pass, one pass puts the copy's depth back through gl_FragDepth, and its stencil through VK_EXT_shader_stencil_export, or one pass per bit without it, into the masked pixels only (RecordMaskedDepthRestore). The kept pixels are therefore tested and written in primitive order as on the console, and the masked ones end unchanged.

  • The save and the restore reuse the sample-order depth pass: Depth is split into Save and the draw, and the pass gets single-sample variants of its three fragment programs (DepthSingle, StencilExportSingle, StencilBitsSingle) plus sample shading only above one sample.
  • The draw's quad pixel mask is part of its render pass key, so draws with the same mask share the copy and the restore pass and no other draw joins that pass. A draw with a restore is not turned into a draw recipe.
  • DepthSurfaceMaskedTarget finds the resident depth surface the copy is taken from.
  • MaskedDepthRestoreRefusal keeps the refusal where the restore is not implemented: a multisampled or layered depth target, or a host depth format other than the ones the sample-order depth pass writes back through gl_FragDepth (16-bit, or 32-bit float). Like that pass, the restore does not keep a subnormal or NaN depth value on the RTX 2080 Ti's driver, as found in the review of #12120.

The debt fragment of #28218 is updated to describe the restore.

Tested

  • Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0, NVIDIA GeForce RTX 2080 Ti with driver 621.40, Vulkan SDK 1.4.363.1.
  • agc_driver_sample_mask_early_stores (from #28218) now covers draws that write depth or stencil, over five quad pixel masks, for one triangle and for two overlapping triangles in one draw:
    • a storing early-Z program with depth writes and a stencil REPLACE draws and stores where the mask keeps the pixel and the test passes;
    • depth afterwards is the draw's there and the old depth elsewhere, and stencil is the draw's reference there, still 0 at the masked pixels and the corner's reference where the depth test failed;
    • an unmasked draw right after it tests the depth the masked pixels had before;
    • the same holds when the storing draw is recorded into a pass, and an unmasked draw recorded right after it does not join its pass;
    • a stencil-only write is put back the same way.
  • agc_driver_sample_mask_depth_stencil now expects the storing early-Z program with depth writes to store only in the kept pixels. sampleOrderTests (agc_graphics_profile_draw) checks MaskedDepthRestoreRefusal: a single-sampled 32-bit float target with stencil and a stencil-only write are accepted, and layered, multisampled and 24-bit depth targets are refused.
  • Mutants, each caught by agc_driver_sample_mask_early_stores: a restore that does nothing fails at "quad pixel mask 1, one triangle, depth is the draw's where the mask kept the pixel and the test passed, and the old depth elsewhere: pixel (3, 0) is 255, expected kept", and one that puts back every pixel fails at the same check with "pixel (2, 0) is 64, expected drawn".
  • The 28 ctest entries over sample masks, sample order, alpha to coverage, pixel sample iteration, multisample retirement, native multisample depth, the pipeline journal and pipeline libraries ran with ctest --test-dir build -R <those names> -V, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation and VK_LOADER_DEBUG=layer (validation layer 1.4.363.1). On this change all 28 pass. On #28218 alone, the same base 06f1befb65, the 26 of them it has pass. The loader inserts the layer in every test that creates a Vulkan instance, neither log has was not found but was requested by env var VK_INSTANCE_LAYERS, and neither has a VUID- line.
  • PPSA38500: every run used to end at the sample mask refusal at its G-buffer pass, whose quad-masked draws (22 states) write depth and stencil to a single-sampled D32_SFLOAT_S8_UINT target. With both parts on the bring-up tree, the title draws them and reaches its first world frame (bring-up run 157).

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #28218
  • Fixes: none
  • Unblocks: PPSA38500 #20383
  • GPUs tested: NVIDIA GeForce RTX 2080 Ti, driver 621.40, Windows 11 Pro 10.0.26300
  • AI-assisted: yes

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of partial sample masks with early depth and stencil tests, including preserving depth results for supported draws.
    • Added single-sample rendering paths for depth and stencil operations.
    • Draws that cannot be handled safely are rejected, including certain cases involving sample-counting queries.
  • Tests

    • Expanded coverage for masked shader stores, depth and stencil writes, and early- versus late-test behavior.
  • Documentation

    • Added notes describing sample-mask behavior and its limitations.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5549794317 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28229). Originally posted by [Meh7an](https://github.com/Meh7an) on 2026-11-29T10:23:01Z. --- ### What Stacked on #28218: its commit is the first one here, and this PR is the second commit only. With #28218, a draw that applies a quad sample mask in the pixel shader under early depth or stencil tests, from a pixel program that writes memory, runs under `EarlyFragmentTests`. The early tests then also run for the masked pixels, so #28218 still refuses such a draw when it writes depth or stencil: the masked pixels would get the draw's depth and stencil, which on the console they never reach. This PR lifts that refusal. For such a draw (`prepareDraw`, `MaskedDepth`), the depth-stencil target is copied before the draw's render pass (`RecordMaskedDepthSave`). After the pass, one pass puts the copy's depth back through `gl_FragDepth`, and its stencil through `VK_EXT_shader_stencil_export`, or one pass per bit without it, into the masked pixels only (`RecordMaskedDepthRestore`). The kept pixels are therefore tested and written in primitive order as on the console, and the masked ones end unchanged. - The save and the restore reuse the sample-order depth pass: `Depth` is split into `Save` and the draw, and the pass gets single-sample variants of its three fragment programs (`DepthSingle`, `StencilExportSingle`, `StencilBitsSingle`) plus sample shading only above one sample. - The draw's quad pixel mask is part of its render pass key, so draws with the same mask share the copy and the restore pass and no other draw joins that pass. A draw with a restore is not turned into a draw recipe. - `DepthSurfaceMaskedTarget` finds the resident depth surface the copy is taken from. - `MaskedDepthRestoreRefusal` keeps the refusal where the restore is not implemented: a multisampled or layered depth target, or a host depth format other than the ones the sample-order depth pass writes back through `gl_FragDepth` (16-bit, or 32-bit float). Like that pass, the restore does not keep a subnormal or NaN depth value on the RTX 2080 Ti's driver, as found in the review of #12120. The debt fragment of #28218 is updated to describe the restore. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> - Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0, NVIDIA GeForce RTX 2080 Ti with driver 621.40, Vulkan SDK 1.4.363.1. - `agc_driver_sample_mask_early_stores` (from #28218) now covers draws that write depth or stencil, over five quad pixel masks, for one triangle and for two overlapping triangles in one draw: - a storing early-Z program with depth writes and a stencil REPLACE draws and stores where the mask keeps the pixel and the test passes; - depth afterwards is the draw's there and the old depth elsewhere, and stencil is the draw's reference there, still 0 at the masked pixels and the corner's reference where the depth test failed; - an unmasked draw right after it tests the depth the masked pixels had before; - the same holds when the storing draw is recorded into a pass, and an unmasked draw recorded right after it does not join its pass; - a stencil-only write is put back the same way. - `agc_driver_sample_mask_depth_stencil` now expects the storing early-Z program with depth writes to store only in the kept pixels. `sampleOrderTests` (`agc_graphics_profile_draw`) checks `MaskedDepthRestoreRefusal`: a single-sampled 32-bit float target with stencil and a stencil-only write are accepted, and layered, multisampled and 24-bit depth targets are refused. - Mutants, each caught by `agc_driver_sample_mask_early_stores`: a restore that does nothing fails at "quad pixel mask 1, one triangle, depth is the draw's where the mask kept the pixel and the test passed, and the old depth elsewhere: pixel (3, 0) is 255, expected kept", and one that puts back every pixel fails at the same check with "pixel (2, 0) is 64, expected drawn". - The 28 ctest entries over sample masks, sample order, alpha to coverage, pixel sample iteration, multisample retirement, native multisample depth, the pipeline journal and pipeline libraries ran with `ctest --test-dir build -R <those names> -V`, with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` and `VK_LOADER_DEBUG=layer` (validation layer 1.4.363.1). On this change all 28 pass. On #28218 alone, the same base 06f1befb652, the 26 of them it has pass. The loader inserts the layer in every test that creates a Vulkan instance, neither log has `was not found but was requested by env var VK_INSTANCE_LAYERS`, and neither has a `VUID-` line. - PPSA38500: every run used to end at the sample mask refusal at its G-buffer pass, whose quad-masked draws (22 states) write depth and stencil to a single-sampled `D32_SFLOAT_S8_UINT` target. With both parts on the bring-up tree, the title draws them and reaches its first world frame (bring-up run 157). ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #28218 - [x] Fixes: none - [x] Unblocks: PPSA38500 #20383 - [x] GPUs tested: NVIDIA GeForce RTX 2080 Ti, driver 621.40, Windows 11 Pro 10.0.26300 - [x] AI-assisted: yes <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Improved handling of partial sample masks with early depth and stencil tests, including preserving depth results for supported draws. * Added single-sample rendering paths for depth and stencil operations. * Draws that cannot be handled safely are rejected, including certain cases involving sample-counting queries. * **Tests** * Expanded coverage for masked shader stores, depth and stencil writes, and early- versus late-test behavior. * **Documentation** * Added notes describing sample-mask behavior and its limitations. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
MarcZX10 commented 2026-11-29 10:34:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-29T10:34:49Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the remaining point is optional, and one changed test file could not be read in full.

What I ran

Reviewed head d7018563da936789b9587945581d9d95f2f83b1e using the prepared diff and the pinned repository reader. No commands or tests were run in this review.

Results

Check Result Note
Changed code review passed Core restore and pass lifecycle inspected
Tests not run Author reports 28 targeted ctest entries passed
Full changed-file coverage not run tests/Graphics.cpp was excluded as too large

Findings

Blocking

None.

Non-blocking

  • Graphics/src/Draw.cpp:3180 distinguishes a masked pass from other render passes by mixing a mask value into the existing pass key. This appears to prevent an unmasked draw from joining the restore pass, as intended; consider using an explicit key component if the pass-key representation is extended later, so the distinction is structural rather than hash-based.

Not verified

  • The full text of core/libs/prx/libSceAgcDriver/tests/Graphics.cpp was excluded by the review folder size rule; its changed hunks were inspected in diff.patch only.
  • No build, tests, Vulkan validation, or console run was performed by this reviewer. The reported Windows 11 / RTX 2080 Ti tests and PPSA38500 bring-up result are author-reported.
  • All other changed files listed in manifest.json were read from the prepared full texts or targeted pinned-head ranges, including tests/execution/SampleMaskEarlyStores.cpp and the debt note.

To get this to a good state

Nothing is required. Optional: consider representing the masked-pass discriminator explicitly in the pass key.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6102493937 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28229#pullrequestreview-6102493937). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-29T10:34:49Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the remaining point is optional, and one changed test file could not be read in full. ## What I ran Reviewed head `d7018563da936789b9587945581d9d95f2f83b1e` using the prepared diff and the pinned repository reader. No commands or tests were run in this review. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | Core restore and pass lifecycle inspected | | Tests | not run | Author reports 28 targeted ctest entries passed | | Full changed-file coverage | not run | `tests/Graphics.cpp` was excluded as too large | ## Findings ### Blocking None. ### Non-blocking - `Graphics/src/Draw.cpp:3180` distinguishes a masked pass from other render passes by mixing a mask value into the existing pass key. This appears to prevent an unmasked draw from joining the restore pass, as intended; consider using an explicit key component if the pass-key representation is extended later, so the distinction is structural rather than hash-based. ## Not verified - The full text of `core/libs/prx/libSceAgcDriver/tests/Graphics.cpp` was excluded by the review folder size rule; its changed hunks were inspected in `diff.patch` only. - No build, tests, Vulkan validation, or console run was performed by this reviewer. The reported Windows 11 / RTX 2080 Ti tests and PPSA38500 bring-up result are author-reported. - All other changed files listed in `manifest.json` were read from the prepared full texts or targeted pinned-head ranges, including `tests/execution/SampleMaskEarlyStores.cpp` and the debt note. ## To get this to a good state Nothing is required. Optional: consider representing the masked-pass discriminator explicitly in the pass key.
Plyxen commented 2026-12-03 06:05:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-12-03T06:05:37Z.

Review: APPROVED


I read the diff at d7018563da. Windows 11, GCC 15.2, merged into main at cf556db18c3 together with other open PRs (so not an isolated build), with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 and VK_LAYER_KHRONOS_validation on an RTX 5080 (621.24) and a Radeon iGPU (26.11.2).

  • agc_driver_sample_mask_depth_stencil, agc_driver_sample_mask_early_stores and the 60 tests of agc_driver_graphics_tests: pass on both GPUs. The loader log shows the layer inserted and no "was not found" line, and neither run has a VUID- or SYNC-HAZARD line.
  • Control (same tree and tests, the changed files of those PRs put back to main's): the first two fail on both GPUs: "a sample mask that differs between the pixels of a quad, from a pixel program with early depth or stencil tests that writes memory, is unsupported" (the control also lacks #28218).

Not checked: a title.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6164496836 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28229#pullrequestreview-6164496836). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-12-03T06:05:37Z. **Review:** APPROVED --- I read the diff at `d7018563da`. Windows 11, GCC 15.2, merged into `main` at `cf556db18c3` together with other open PRs (so not an isolated build), with `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1` and `VK_LAYER_KHRONOS_validation` on an RTX 5080 (621.24) and a Radeon iGPU (26.11.2). - `agc_driver_sample_mask_depth_stencil`, `agc_driver_sample_mask_early_stores` and the 60 tests of `agc_driver_graphics_tests`: pass on both GPUs. The loader log shows the layer inserted and no "was not found" line, and neither run has a `VUID-` or `SYNC-HAZARD` line. - Control (same tree and tests, the changed files of those PRs put back to `main`'s): the first two fail on both GPUs: "a sample mask that differs between the pixels of a quad, from a pixel program with early depth or stencil tests that writes memory, is unsupported" (the control also lacks #28218). Not checked: a title. AI-assisted: yes (Claude Code)
ligmaSec commented 2026-12-03 20:11:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T20:11:53Z.

Review: COMMENTED


The fixture checks that a masked early-Z draw and a later unmasked draw use separate passes. The recording logic can also continue a pass across draws with the same mask, where it saves depth once and restores the excluded pixels only at pass end. Could the fixture issue two consecutive same-mask draws with distinct depth/stencil values and assert both that the pass continues and that the masked pixels retain their original values afterward? This covers the lifetime of the saved attachment across the continuation.

Source review at d7018563da. The head trial-merges onto d2e04ef9b3. Paired Vulkan execution is queued.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6177655978 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28229#pullrequestreview-6177655978). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T20:11:53Z. **Review:** COMMENTED --- The fixture checks that a masked early-Z draw and a later unmasked draw use separate passes. The recording logic can also continue a pass across draws with the same mask, where it saves depth once and restores the excluded pixels only at pass end. Could the fixture issue two consecutive same-mask draws with distinct depth/stencil values and assert both that the pass continues and that the masked pixels retain their original values afterward? This covers the lifetime of the saved attachment across the continuation. Source review at `d7018563da`. The head trial-merges onto `d2e04ef9b3`. Paired Vulkan execution is queued. AI-assisted: yes.
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/28229:upstream-pr/28229'
git switch 'upstream-pr/28229'

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/28229'
git switch 'upstream-pr/28229'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/28229'
git switch 'upstream-pr/28229'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/28229'
git switch 'main'
git merge --squash 'upstream-pr/28229'
git switch 'main'
git merge --ff-only 'upstream-pr/28229'
git switch 'main'
git merge 'upstream-pr/28229'
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
boykopovar/AnyPS5!28229
No description provided.