fix(agc): preserve ClearMRT color writes and validate stencil clears #1534

Merged
hamb3r merged 2 commits from upstream-pr/1534 into main 2026-10-10 01:09:58 +00:00
hamb3r commented 2026-10-08 11:27:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hamb3r on 2026-10-08T11:27:33Z.


What

Proposed replacement for #1479: retain the ClearMRT color outputs while clearing stencil, with writable-plane validation and regression coverage. The original implementation is included as a separate commit with Aryan Sharma's authorship preserved. This PR is standalone and does not require #1479 to merge first.

DB_RENDER_CONTROL = 0x22 sets STENCIL_CLEAR_ENABLE, not depth clear. Rejecting the whole draw drops its color writes as well. In God of War Sons of Sparta (PPSA28997), the uncleared scene target accumulates the previous frames' lighting and develops growing white highlights.

Rasterize the existing draw with ALWAYS comparison and stencil REPLACE on both faces, using DB_STENCIL_CLEAR. This retains color exports, write masks and scissor coverage. No extra GPU pass, allocation or synchronization is introduced.

Compared with #1479:

  • Require a present, writable stencil plane; read-only stencil and absent planes throw, including when both depth/stencil formats are disabled. The draw precheck also rejects invalid clear targets.
  • Select stencil-clear state before decoding ordinary stencil operations. Operations overridden by the clear can otherwise reject a valid draw before the replacement is applied.
  • Cover two color attachments with different write masks, scissor coverage, both stencil faces, disabled/enabled stencil tests, read-only depth, and continued rejection of depth clear/copy operations.

Register definitions: AMD PAL DB_RENDER_CONTROL; clear/read-only view setup: AMD PAL DepthStencilView::InitRegistersCommon. Vulkan replacement uses the reference value as specified by VkStencilOp. No shader instruction semantics are changed.

Tested

  • Windows 10, NVIDIA RTX 3080, God of War Sons of Sparta (PPSA28997): the local ClearMRT fix removes the accumulating white highlights in the title/menu screen and retains normal brightness on subsequent frames. Before/after captures from that game check are below.
  • MinGW-w64 GCC, Release: agc_driver_graphics passes after rebasing both commits onto main at b1c5f10d.
  • The new read-only stencil regression fails on #1479's original implementation. Adding only the read-only guard exposes a second failure: an overridden stencil add operation rejects the clear. Both pass with this change.
  • tools/check_conventions.py --base origin/main and git diff --check origin/main...HEAD pass.

The automated tests validate decoded graphics state; they do not read back rendered GPU pixels. The game captures demonstrate the underlying ClearMRT fix, rather than an A/B comparison of the additional validation changes. Their resolutions differ, and they are not an FPS benchmark.

Before:

Before: accumulating white highlights in God of War Sons of Sparta

After:

After: title screen with normal lighting after the ClearMRT fix

Checklist

  • Based on current main (b1c5f10d); overlapping open PRs checked — #1479 is explicitly proposed for replacement, with original authorship preserved.
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt — no silent stubs added
  • 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 — none added
  • Generic behaviour, not specific to one title
  • Depends on: none; proposed replacement for #1479
  • AI-assisted: yes

@oneandonlydean @adriadam10 — proposing this as a replacement for #1479 because it preserves the original rasterized ClearMRT fix and its author's credit, while closing two reproduced decoder gaps: writes to read-only stencil and rejection by stencil operations that the clear overrides. It also adds MRT/scissor regressions and Windows 10 / RTX 3080 visual evidence. The overlap is explicit above so you can choose which PR to merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4786042686 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1534). Originally posted by [hamb3r](https://github.com/hamb3r) on 2026-10-08T11:27:33Z. --- ### What Proposed replacement for #1479: retain the ClearMRT color outputs while clearing stencil, with writable-plane validation and regression coverage. The original implementation is included as a separate commit with Aryan Sharma's authorship preserved. This PR is standalone and does not require #1479 to merge first. `DB_RENDER_CONTROL = 0x22` sets `STENCIL_CLEAR_ENABLE`, not depth clear. Rejecting the whole draw drops its color writes as well. In God of War Sons of Sparta (PPSA28997), the uncleared scene target accumulates the previous frames' lighting and develops growing white highlights. Rasterize the existing draw with `ALWAYS` comparison and stencil `REPLACE` on both faces, using `DB_STENCIL_CLEAR`. This retains color exports, write masks and scissor coverage. No extra GPU pass, allocation or synchronization is introduced. Compared with #1479: - Require a present, writable stencil plane; read-only stencil and absent planes throw, including when both depth/stencil formats are disabled. The draw precheck also rejects invalid clear targets. - Select stencil-clear state before decoding ordinary stencil operations. Operations overridden by the clear can otherwise reject a valid draw before the replacement is applied. - Cover two color attachments with different write masks, scissor coverage, both stencil faces, disabled/enabled stencil tests, read-only depth, and continued rejection of depth clear/copy operations. Register definitions: [AMD PAL DB_RENDER_CONTROL](https://github.com/GPUOpen-Drivers/pal/blob/dev/src/core/hw/gfxip/gfx9/chip/gfx9_plus_merged_registers.h); clear/read-only view setup: [AMD PAL DepthStencilView::InitRegistersCommon](https://github.com/GPUOpen-Drivers/pal/blob/dev/src/core/hw/gfxip/gfx9/gfx9DepthStencilView.cpp). Vulkan replacement uses the reference value as specified by [VkStencilOp](https://docs.vulkan.org/refpages/latest/refpages/source/VkStencilOp.html). No shader instruction semantics are changed. ### Tested - Windows 10, NVIDIA RTX 3080, God of War Sons of Sparta (PPSA28997): the local ClearMRT fix removes the accumulating white highlights in the title/menu screen and retains normal brightness on subsequent frames. Before/after captures from that game check are below. - MinGW-w64 GCC, Release: `agc_driver_graphics` passes after rebasing both commits onto `main` at `b1c5f10d`. - The new read-only stencil regression fails on #1479's original implementation. Adding only the read-only guard exposes a second failure: an overridden stencil add operation rejects the clear. Both pass with this change. - `tools/check_conventions.py --base origin/main` and `git diff --check origin/main...HEAD` pass. The automated tests validate decoded graphics state; they do not read back rendered GPU pixels. The game captures demonstrate the underlying ClearMRT fix, rather than an A/B comparison of the additional validation changes. Their resolutions differ, and they are not an FPS benchmark. Before: ![Before: accumulating white highlights in God of War Sons of Sparta](https://github.com/user-attachments/assets/cdfffdbb-059e-4d5f-b4d6-12c153356b43) After: ![After: title screen with normal lighting after the ClearMRT fix](https://github.com/user-attachments/assets/a8078554-f178-4f4b-a8c3-0e789376b28e) ### Checklist - [x] Based on current `main` (`b1c5f10d`); overlapping open PRs checked — #1479 is explicitly proposed for replacement, with original authorship preserved. - [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) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) — no silent stubs added - [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 — none added - [x] Generic behaviour, not specific to one title - [x] Depends on: none; proposed replacement for #1479 - [x] AI-assisted: yes @oneandonlydean @adriadam10 — proposing this as a replacement for #1479 because it preserves the original rasterized ClearMRT fix and its author's credit, while closing two reproduced decoder gaps: writes to read-only stencil and rejection by stencil operations that the clear overrides. It also adds MRT/scissor regressions and Windows 10 / RTX 3080 visual evidence. The overlap is explicit above so you can choose which PR to merge.
fisherjoey commented 2026-10-09 04:46:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-09T04:46:25Z.


Linux results, since this was tested on Windows only. Everything passes here.

Nobara 44 (Linux 7.2), GCC 16.2.1, Release, BUILD_TESTING=ON. This PR merged onto main f1451396 cleanly.

main f1451396 main + this PR
RTX 5070 Ti, driver 595.91.07, ctest -R agc_driver -j1 207 / 207 207 / 207
RADV, Ryzen 5 7600X iGPU (gfx1036), Mesa 26.2.1, same 207 / 207 207 / 207
CPU tests (-E agc_driver) 286 / 287 286 / 287

The CPU failure is guest_dynamic_loader, which fails the same way on main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6074452516 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1534#issuecomment-6074452516). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-09T04:46:25Z. --- Linux results, since this was tested on Windows only. Everything passes here. Nobara 44 (Linux 7.2), GCC 16.2.1, Release, `BUILD_TESTING=ON`. This PR merged onto `main` f1451396 cleanly. | | `main` f1451396 | `main` + this PR | |---|---|---| | RTX 5070 Ti, driver 595.91.07, `ctest -R agc_driver -j1` | 207 / 207 | 207 / 207 | | RADV, Ryzen 5 7600X iGPU (gfx1036), Mesa 26.2.1, same | 207 / 207 | 207 / 207 | | CPU tests (`-E agc_driver`) | 286 / 287 | 286 / 287 | The CPU failure is `guest_dynamic_loader`, which fails the same way on `main`.
XairRrR commented 2026-10-09 11:55:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-09T11:55:24Z.


Uncharted: Legacy of Thieves Collection (PPSA05684) clears with draws that set DB_RENDER_CONTROL to 0x3 (depth and stencil) and stops on main with:

depth/stencil clear, copy, resummarize or decompress draws (DB_RENDER_CONTROL) is unsupported (register 0x0 = 0x00000003)

I opened the depth half on top of this PR as #2219 (Depends on #1534): the draw is rasterized as submitted with an always-passing depth write and the viewport depth range pinned to DB_DEPTH_CLEAR, so it covers only what the title's geometry covers.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6080349219 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1534#issuecomment-6080349219). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-09T11:55:24Z. --- Uncharted: Legacy of Thieves Collection (PPSA05684) clears with draws that set `DB_RENDER_CONTROL` to 0x3 (depth and stencil) and stops on `main` with: ``` depth/stencil clear, copy, resummarize or decompress draws (DB_RENDER_CONTROL) is unsupported (register 0x0 = 0x00000003) ``` I opened the depth half on top of this PR as #2219 (`Depends on #1534`): the draw is rasterized as submitted with an always-passing depth write and the viewport depth range pinned to `DB_DEPTH_CLEAR`, so it covers only what the title's geometry covers.
github-actions[bot] commented 2026-10-09 22:04:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T22:04:38Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#2179 core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp L852-854
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090021827 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1534#issuecomment-6090021827). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T22:04:38Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #2179 | `core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp` [L852-854](https://github.com/boykopovar/AnyPS5/blob/7703fda15e4ab4025dd817eae5964315e6dd134b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp#L852-L854) | | |
adriadam10 commented 2026-10-10 01:09:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T01:09:55Z.


Checked: build + ctest on the merged tree (agc_driver_graphics and agc_driver_prepared_graphics pass); the new test fails without the State.cpp change; stencil-clear state is chosen before ordinary stencil ops, read-only/absent stencil throws in both DecodeState and the DrawRejection precheck, depth clear/copy still rejected. Linux results from @fisherjoey agree. Merging.

Note: #2179 touches the depthSurface line in DrawRejection and will need a rebase on top of this.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6092016090 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1534#issuecomment-6092016090). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T01:09:55Z. --- Checked: build + ctest on the merged tree (agc_driver_graphics and agc_driver_prepared_graphics pass); the new test fails without the State.cpp change; stencil-clear state is chosen before ordinary stencil ops, read-only/absent stencil throws in both `DecodeState` and the `DrawRejection` precheck, depth clear/copy still rejected. Linux results from @fisherjoey agree. Merging. Note: #2179 touches the `depthSurface` line in `DrawRejection` and will need a rebase on top of this.
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!1534
No description provided.