fix(agc): sample a depth surface's stencil plane through an R8_UNORM view #5781

Merged
moong15 merged 1 commit from upstream-pr/5781 into main 2026-10-15 16:50:42 +00:00
moong15 commented 2026-10-14 10:26:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by moong15 on 2026-10-14T10:26:35Z.


What

A title can sample the stencil plane of a depth surface through an 8-bit UNORM texture (guest format 1, R8_UNORM). Vulkan cannot view a stencil aspect as UNORM, so DepthSurface::Sampled threw for it; only R8_UINT views of the stencil plane were supported.

This adds that case for a full-size, single-level, single-slice view (the same shape the existing R8_UINT path accepts):

  • The stencil aspect is copied into a buffer (vkCmdCopyImageToBuffer), then from the buffer into an R8_UNORM image of the surface's extent (vkCmdCopyBufferToImage). Stencil bytes stay unchanged, so the shader reads s / 255, as on the console.
  • The image, its staging buffer and its views are kept on the surface and freed with it.
  • The copy is refreshed only when the surface was written since the last copy (written changed), or when the surface is the depth target currently bound for drawing. A copy taken while the surface is bound is not reused after it is unbound, as for the existing depth plane copies (slice == drawnDepth ? 0 : written). A second sample with no write in between reuses it.
  • Any other stencil view shape still throws as before.

Removed error, from Stellar Blade (PPSA13197 v01.141), during the first gameplay area:

[agc] driver failure, continuing: AGC graphics: sampling the stencil plane of depth surface 0x205b310000 (2564x2048, vk format 130) as a 2564x2048 texture of guest format 1 (vk 9), tile mode 7, dimension 1, levels 0-0, slice 0 is not implemented (T# 21846900 c0100000 01ffc280 91b00204 00000000 00700000 002b0000 002278d3)
terminate called after throwing an instance of 'std::runtime_error'

Overlap check

No open or merged PR samples the stencil plane as UNORM. Searched open PRs and PRs merged since 2026-10-01 for stencil, R8_UNORM, DepthSurface, Sampled:

  • #1368 and #1395 (open) rework Sampled but still expect R8_UINT for the stencil plane. #1368 changes the same function, so whichever lands second needs a small rebase.
  • #4683 (merged) shares a plane's contents across stencil pairings. This PR is based on it: sampling reads the surface planeSurface returns, and a surface rebound after taking another pairing's planes bumps written, so the copy is refreshed.
  • #2325 (closed) accepted R8_UNORM for stencil storage, not sampling.
  • #2994 (closed) is a different stencil-plane sampling error (an RGBA8 view, FPV Simulator).

Tested

  • New case TestStencilUnormView in tests/DepthSurfaceReuse.cpp (part of agc_driver_graphics_tests). It checks that:
    • an R8_UINT view still samples without a copy;
    • an R8_UNORM view samples an R8_UNORM image filled from the stencil aspect of the depth image;
    • a second sample with no write in between reuses the copy;
    • the bound depth surface is copied on every sample;
    • after it is unbound, the copy is refreshed once, into the same image.
  • Without the change, the new case fails with the same error as the title (sampling the stencil plane of depth surface 0x74000000 (320x180, vk format 130) as a 320x180 texture of guest format 1 (vk 9) ... is not implemented). With it, agc_driver_graphics passes.
  • ctest (Release, all tests): 1140 of 1145 passed, 11 skipped. The 5 failures also fail on main on this machine without this change: agc_driver_division_output_modifiers, agc_driver_division_result_modifiers, agc_driver_f64_conversions and agc_driver_f64_transcendental crash inside the AMD Vulkan driver, and agc_driver_layered_volume_vertex_no_gpl fails (checked 3 times with and 3 times without this change).
  • tools/check_conventions.py --base origin/main: all checks passed
  • Stellar Blade: the build with this change went past the point where the error above ended the previous run. The same 2564x2048 R8_UNORM view of the stencil plane was created 3 times with no driver failure, and the run reached the first combat tutorial prompt (about 60k log lines, against about 57k before).

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
  • 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: none
  • Unblocks: PPSA13197 (no Game bring-up discussion yet)
  • GPUs tested: AMD Radeon RX 9070 XT, driver 32.0.32015.2008, Windows 11 Pro 26200 (MinGW GCC 15.2, Release)
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4856477924 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5781). Originally posted by [moong15](https://github.com/moong15) on 2026-10-14T10:26:35Z. --- ### What A title can sample the stencil plane of a depth surface through an 8-bit UNORM texture (guest format 1, `R8_UNORM`). Vulkan cannot view a stencil aspect as UNORM, so `DepthSurface::Sampled` threw for it; only `R8_UINT` views of the stencil plane were supported. This adds that case for a full-size, single-level, single-slice view (the same shape the existing `R8_UINT` path accepts): - The stencil aspect is copied into a buffer (`vkCmdCopyImageToBuffer`), then from the buffer into an `R8_UNORM` image of the surface's extent (`vkCmdCopyBufferToImage`). Stencil bytes stay unchanged, so the shader reads `s / 255`, as on the console. - The image, its staging buffer and its views are kept on the surface and freed with it. - The copy is refreshed only when the surface was written since the last copy (`written` changed), or when the surface is the depth target currently bound for drawing. A copy taken while the surface is bound is not reused after it is unbound, as for the existing depth plane copies (`slice == drawnDepth ? 0 : written`). A second sample with no write in between reuses it. - Any other stencil view shape still throws as before. Removed error, from Stellar Blade (PPSA13197 v01.141), during the first gameplay area: ``` [agc] driver failure, continuing: AGC graphics: sampling the stencil plane of depth surface 0x205b310000 (2564x2048, vk format 130) as a 2564x2048 texture of guest format 1 (vk 9), tile mode 7, dimension 1, levels 0-0, slice 0 is not implemented (T# 21846900 c0100000 01ffc280 91b00204 00000000 00700000 002b0000 002278d3) terminate called after throwing an instance of 'std::runtime_error' ``` ### Overlap check No open or merged PR samples the stencil plane as UNORM. Searched open PRs and PRs merged since 2026-10-01 for stencil, `R8_UNORM`, `DepthSurface`, `Sampled`: - #1368 and #1395 (open) rework `Sampled` but still expect `R8_UINT` for the stencil plane. #1368 changes the same function, so whichever lands second needs a small rebase. - #4683 (merged) shares a plane's contents across stencil pairings. This PR is based on it: sampling reads the surface `planeSurface` returns, and a surface rebound after taking another pairing's planes bumps `written`, so the copy is refreshed. - #2325 (closed) accepted `R8_UNORM` for stencil storage, not sampling. - #2994 (closed) is a different stencil-plane sampling error (an RGBA8 view, FPV Simulator). ### Tested - New case `TestStencilUnormView` in `tests/DepthSurfaceReuse.cpp` (part of `agc_driver_graphics_tests`). It checks that: - an `R8_UINT` view still samples without a copy; - an `R8_UNORM` view samples an `R8_UNORM` image filled from the stencil aspect of the depth image; - a second sample with no write in between reuses the copy; - the bound depth surface is copied on every sample; - after it is unbound, the copy is refreshed once, into the same image. - Without the change, the new case fails with the same error as the title (`sampling the stencil plane of depth surface 0x74000000 (320x180, vk format 130) as a 320x180 texture of guest format 1 (vk 9) ... is not implemented`). With it, `agc_driver_graphics` passes. - `ctest` (Release, all tests): 1140 of 1145 passed, 11 skipped. The 5 failures also fail on `main` on this machine without this change: `agc_driver_division_output_modifiers`, `agc_driver_division_result_modifiers`, `agc_driver_f64_conversions` and `agc_driver_f64_transcendental` crash inside the AMD Vulkan driver, and `agc_driver_layered_volume_vertex_no_gpl` fails (checked 3 times with and 3 times without this change). - `tools/check_conventions.py --base origin/main`: all checks passed - Stellar Blade: the build with this change went past the point where the error above ended the previous run. The same 2564x2048 `R8_UNORM` view of the stencil plane was created 3 times with no driver failure, and the run reached the first combat tutorial prompt (about 60k log lines, against about 57k before). ### Checklist - [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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical 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: none - [x] Unblocks: PPSA13197 (no Game bring-up discussion yet) - [x] GPUs tested: AMD Radeon RX 9070 XT, driver 32.0.32015.2008, Windows 11 Pro 26200 (MinGW GCC 15.2, Release) - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
alfalfa-star commented 2026-10-15 07:06:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-15T07:06:22Z.


RIDE 4 (PPSA01599, UE4) stops at this on current main (d92a8dad3), about 57 s in:

terminate called after throwing an instance of 'std::runtime_error'
  what():  AGC graphics: sampling the stencil plane of depth surface 0x2020e00000 (2688x1512, vk format 130) as a 2688x1512 texture of guest format 1 (vk 9), tile mode 7, dimension 1, levels 0-0, slice 0 is not implemented (T# 202ebd00 c0100000 0179c29f 91b00204 00000000 00700000 002b0000 00205660)

On main from 2026-10-11 the same draws were skipped ([gpu] skipped draw: ... sampling the stencil plane ...) and the title went on; now the throw ends the process.

With this PR's commit on main d92a8dad3, the stop is gone in three runs, with no stencil-plane refusal at all. In the run without host imports (see the next paragraph), the title reaches a race about 230 to 275 s in (Tsukuba with the YZF-R6, AI agents and the replay buffer) and runs to the 360 s limit. Only main's version of tests/DepthSurfaceReuse.cpp was used for this build, because that file conflicts with main. The rest of the commit applies cleanly.

The other two runs stopped at 117 and 140 s with amdgpu: Not enough memory for command submission! and VK_ERROR_DEVICE_LOST. The same error hit a Kena run today on a build without this PR, and the run with APS5_NO_HOST_IMPORT=1 has none, so I don't attribute it to this change. This PR adds one surface-sized R8_UNORM image and staging buffer per depth surface.

Linux (Arch, kernel 7.2.9), RX 9070 XT (RADV, Mesa 26.2.4), GCC Release; a local pad-script commit replays timed presses (input only).

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6186517556 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5781#issuecomment-6186517556). Originally posted by [alfalfa-star](https://github.com/alfalfa-star) on 2026-10-15T07:06:22Z. --- RIDE 4 (PPSA01599, UE4) stops at this on current `main` (d92a8dad3), about 57 s in: ``` terminate called after throwing an instance of 'std::runtime_error' what(): AGC graphics: sampling the stencil plane of depth surface 0x2020e00000 (2688x1512, vk format 130) as a 2688x1512 texture of guest format 1 (vk 9), tile mode 7, dimension 1, levels 0-0, slice 0 is not implemented (T# 202ebd00 c0100000 0179c29f 91b00204 00000000 00700000 002b0000 00205660) ``` On `main` from 2026-10-11 the same draws were skipped (`[gpu] skipped draw: ... sampling the stencil plane ...`) and the title went on; now the throw ends the process. With this PR's commit on `main` d92a8dad3, the stop is gone in three runs, with no stencil-plane refusal at all. In the run without host imports (see the next paragraph), the title reaches a race about 230 to 275 s in (Tsukuba with the YZF-R6, AI agents and the replay buffer) and runs to the 360 s limit. Only main's version of `tests/DepthSurfaceReuse.cpp` was used for this build, because that file conflicts with `main`. The rest of the commit applies cleanly. The other two runs stopped at 117 and 140 s with `amdgpu: Not enough memory for command submission!` and `VK_ERROR_DEVICE_LOST`. The same error hit a Kena run today on a build without this PR, and the run with `APS5_NO_HOST_IMPORT=1` has none, so I don't attribute it to this change. This PR adds one surface-sized `R8_UNORM` image and staging buffer per depth surface. Linux (Arch, kernel 7.2.9), RX 9070 XT (RADV, Mesa 26.2.4), GCC Release; a local pad-script commit replays timed presses (input only). AI-assisted: yes (Claude Code)
SleglJan commented 2026-10-15 08:52:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-15T08:52:41Z.


007 First Light (PPSA11386) stops at frame 186 with this PR's error, on main d92a8dad3 plus #5799 and local commits (input scripting, diagnostics, a workaround for an earlier #5461 throw; none touch the depth surface code):

sampling the stencil plane of depth surface (1472x828, vk format 130) as a 1472x828 texture of guest format 1 (vk 9), tile mode 7, dimension 1, levels 0-0, slice 0 is not implemented

With 2901df261 cherry-picked onto that stack (by then also carrying local shader-table and clip-distance commits), 007 gets past frame 186 with no stencil-plane error, through the menu and Resume, and into the level load, where it stops at frame 1188 with an unrelated error: registered program refers to an incompatible shader binary type.

  • As in the comment above, only tests/DepthSurfaceReuse.cpp conflicts, against d92a8dad3 and against 721db55dc: main's and this PR's test globals were added at the same place, and keeping both builds. DepthSurface.cpp merges cleanly.
  • With a local port of #454's earlier head (df13acfe9) in place of this PR, 007 also passes frame 186 and stops at the same later error. Neither run checks the sampled values.

Linux (CachyOS, kernel 7.2.10), RTX 3080 (NVIDIA 615.78.08), GCC 16.2.1, Release.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6188275256 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5781#issuecomment-6188275256). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-15T08:52:41Z. --- 007 First Light (PPSA11386) stops at frame 186 with this PR's error, on `main` d92a8dad3 plus #5799 and local commits (input scripting, diagnostics, a workaround for an earlier #5461 throw; none touch the depth surface code): `sampling the stencil plane of depth surface (1472x828, vk format 130) as a 1472x828 texture of guest format 1 (vk 9), tile mode 7, dimension 1, levels 0-0, slice 0 is not implemented` With 2901df261 cherry-picked onto that stack (by then also carrying local shader-table and clip-distance commits), 007 gets past frame 186 with no stencil-plane error, through the menu and Resume, and into the level load, where it stops at frame 1188 with an unrelated error: `registered program refers to an incompatible shader binary type`. - As in the comment above, only `tests/DepthSurfaceReuse.cpp` conflicts, against d92a8dad3 and against 721db55dc: main's and this PR's test globals were added at the same place, and keeping both builds. `DepthSurface.cpp` merges cleanly. - With a local port of #454's earlier head (df13acfe9) in place of this PR, 007 also passes frame 186 and stops at the same later error. Neither run checks the sampled values. Linux (CachyOS, kernel 7.2.10), RTX 3080 (NVIDIA 615.78.08), GCC 16.2.1, Release. AI-assisted: yes (Claude Code)
github-actions[bot] commented 2026-10-15 12:09:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:09:36Z.


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
PR 6465 core/libs/prx/libSceAgcDriver/tests/DepthSurfaceReuse.cpp L45

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1368 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
core/libs/prx/libSceAgcDriver/tests/DepthSurfaceReuse.cpp
PR 1395 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 5499 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191703879 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5781#issuecomment-6191703879). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T12:09:36Z. --- <!-- 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 | |---|---|---|---| | [PR 6465](https://www.github.com/boykopovar/AnyPS5/pull/6465) | `core/libs/prx/libSceAgcDriver/tests/DepthSurfaceReuse.cpp` [L45](https://github.com/boykopovar/AnyPS5/blob/ec1193dc39d9cf8a66fc4bb9807afe8632749ae5/core/libs/prx/libSceAgcDriver/tests/DepthSurfaceReuse.cpp#L45) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 1368](https://www.github.com/boykopovar/AnyPS5/pull/1368) | `core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp`<br>`core/libs/prx/libSceAgcDriver/tests/DepthSurfaceReuse.cpp` | | [PR 1395](https://www.github.com/boykopovar/AnyPS5/pull/1395) | `core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp` | | [PR 5499](https://www.github.com/boykopovar/AnyPS5/pull/5499) | `core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp` |
adriadam10 commented 2026-10-15 12:56:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T12:56:28Z.


Checked the R8_UNORM stencil-plane copy against current main. It conflicted in DepthSurface.cpp and DepthSurfaceReuse.cpp (SampledAccepts, native multisample sampling), so I rebased it and added what the new structure needs:

  • UnormStencilView(), accepted in SampledAccepts, so the retire check in DepthSurfaceTexture doesn't treat the R8_UNORM view as unaccepted and retire the surface through OverwrittenInMemory.
  • drawn defaults to false in Sampled; the native multisample path never reaches the stencil copy (stencil is rejected there).

agc_driver_graphics passes, and fails without the DepthSurface.cpp change. The libSceAgcDriver ctest scope is green except agc_driver_tessellation (SEGFAULT, also reported on main in #4683). The reports from @alfalfa-star (RIDE 4) and @SleglJan (007 First Light, gets past frame 186) match the fix. Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6192580954 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5781#issuecomment-6192580954). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-15T12:56:28Z. --- Checked the R8_UNORM stencil-plane copy against current `main`. It conflicted in `DepthSurface.cpp` and `DepthSurfaceReuse.cpp` (`SampledAccepts`, native multisample sampling), so I rebased it and added what the new structure needs: - `UnormStencilView()`, accepted in `SampledAccepts`, so the retire check in `DepthSurfaceTexture` doesn't treat the R8_UNORM view as unaccepted and retire the surface through `OverwrittenInMemory`. - `drawn` defaults to `false` in `Sampled`; the native multisample path never reaches the stencil copy (stencil is rejected there). `agc_driver_graphics` passes, and fails without the `DepthSurface.cpp` change. The libSceAgcDriver ctest scope is green except `agc_driver_tessellation` (SEGFAULT, also reported on main in #4683). The reports from @alfalfa-star (RIDE 4) and @SleglJan (007 First Light, gets past frame 186) match the fix. Merging once CI is green.
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!5781
No description provided.