Skip to content

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

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
moong15:fix/agc-sample-stencil-as-unorm
Oct 15, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
moong15:fix/agc-sample-stencil-as-unorm

Conversation

@moong15

@moong15 moong15 commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

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:

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

@alfalfa-star

Copy link
Copy Markdown
Contributor

RIDE 4 (PPSA01599, UE4) stops at this on current main (d92a8da), 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 d92a8da, 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

Copy link
Copy Markdown
Contributor

007 First Light (PPSA11386) stops at frame 186 with this PR's error, on main d92a8da 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 2901df2 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.

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

AI-assisted: yes (Claude Code)

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

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

…view

Vulkan cannot view a stencil aspect as UNORM, so an 8-bit UNORM view of
a stencil plane threw. Copy the stencil aspect through a buffer into an
R8_UNORM image of the surface's extent and sample that. The copy is kept
on the surface and refreshed when the surface was written since, or while
it is the bound depth target.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adriadam10

Copy link
Copy Markdown
Collaborator

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.

@adriadam10
adriadam10 force-pushed the fix/agc-sample-stencil-as-unorm branch from 2901df2 to 47f7118 Compare October 15, 2026 12:56
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 15, 2026
@adriadam10
adriadam10 merged commit fa9365c into boykopovar:main Oct 15, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc*

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants