fix(agc): check stencil replay depth feedback against buffer descriptors #24735
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/24735"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by frankea on 2026-11-21T00:30:49Z.
What
On a device without
VK_EXT_shader_stencil_export, the stencil replay draws a pixel shader that exports a stencil value once per possible reference value. Before recording,prepareDrawrefuses a replay whose shader could read memory behind its own depth or stencil target ("stencil replay refuses depth or stencil feedback reads"). That check askedShaderResources::ReadsOverlap, which reports every region the draw reads in place through a host import. For a shader that reads memory through buffer device addresses, those regions are the whole shared address space: every registered GPU heap (GuestBufferMemory::InPlaceReadsOverlapcounts everyspace->baseregion, and the per-build form puts the same heaps in the build's own regions). So any depth target that lives in a registered heap was refused, whatever the shader actually reads.This PR checks the depth and stencil planes against what the draw declares instead:
GuestBufferMemory::DescriptorRangesOverlap: whether a buffer descriptor range of the build overlaps a range. These are the rangesAddReadableandAddWritablerecord for every V# the shader uses (readsandwrites). They are recorded whether or not the range falls inside the shared address space, so the check is the same in the cached-space form and the per-build form.ShaderResources::DescriptorRangesOverlapforwards to it.prepareDraw: the depth or stencil feedback check usesDescriptorRangesOverlapfor both planes. The draw inputs read in place (inputs.inPlaceReads) stay in the check, and so does the sampled depth-surface check that follows it (a T# whose image is a depth surface overlapping a bound plane, "stencil replay refuses sampled depth-surface feedback"). That T# check is the case where a real feedback loop is possible, since the texture can be served by the live depth image.Why a read through an address needs no refusal here:
VK_EXT_external_memory_host). The single-sample depth target is a driver image bound to its own device-local allocation (DepthSurface.cpp,vkAllocateMemory depth target), so the two never share memory.PublishToGuest,FlushDepthPlanes), which happens on a later guest read and never between replay passes:recordStencilReplayrecords all passes back to back. So every pass reads the same bytes, the ones a single draw that is not replayed would read.The colour target check before it ("stencil replay refuses attachment feedback reads") still uses
ReadsOverlap, so it has the same over-refusal for address-based shaders. No draw has hit it, and it is left as it was.Debt: a new Functional entry says that reads through buffer device addresses are not checked against the bound planes, that such a read of a bound plane sees the bytes published before the draw in every pass, that console behaviour for a shader reading its own depth through memory is not measured, and that the colour target check still counts the whole shared address space.
Tested
Unit test
tests/execution/StencilReplay.cpp, new cases (a 64 KiB heap registered as a GPU allocation holds a D32_SFLOAT_S8_UINT target, depth at +0 and stencil at +0x2000):global_load_dwordfrom the same heap, outside the planes), kills odd columns and exports depth 0.75 plus an unbounded operation value built from the loaded word and the column (DB_SHADER_CONTROL 0x45: Z_EXPORT_ENABLE, STENCIL_OP_VAL_EXPORT_ENABLE, late Z, KILL_ENABLE), with depth test and write on, compare GREATER_OR_EQUAL, stencil ALWAYS/KEEP/REPLACE/KEEP, write mask 0xff and no colour targets. That is 256 replay passes, or 10 where #24802's per-bit replay applies. Each kept column gets its own value, killed columns keep the old stencil, and the kept columns hold the exported depth;bf3390787e): non-indexed records with firstInstance 0xffffffff and one instance, or first 0xfffffffd and count 3, end exactly at 2^32 and replay; first 0xfffffffe with count 3, and count 0xffffffff with first 2, throw "draw indices overflow". Indexed records with firstInstance 0xffffffff and one instance replay, and two instances throw; a count of 0xffffffff at first index 3 is clamped to the bound indices, and a first index of 0xffffffff is skipped;bf3390787e): a cached build reads two buffers 0x80 bytes apart over the depth plane, and again over the stencil plane. Moving either one off the plane is still refused for the one left on it, and moving both is accepted. With two buffers over the same range, moving one is refused and moving both is accepted.Paired run on the RTX 4090 for this head,
bf3390787e. Both sides passed 3 of 3 runs on the RTX 4090, with the validation layer inserted in every test process and 0 VUIDs on either side.e0ef70639dbf3390787eagc_driver_stencil_replay,_no_gpl,_unsuppressedstencil_null_feedback,native_multisample_depthand the 45native_depth_*entries (native_depth_bda_clearamong them)native_depth_driver_queued_fillskipped (host imports are compared here, not write-watched)draw_offset_indices(plain, poisoned),empty_indirect_skip,indirect_draw_pending_producer(plain,_gpu_indirect_disabled),list_restart(plain, compacted),pipelined_indirect_draw,queued_indirect_read_order,rect_list_indirect,settled_indirect_read,sparse_indirect_indices(5 forms)VUID-lines; 0 validation errors or warningsHow it was run
ANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1,VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validationandVK_LOADER_DEBUG=layer.ctest --test-dir build -V -j1 --timeout 900 -R "^agc_driver_(stencil_replay|stencil_null_feedback|native_depth|native_multisample_depth|draw_offset_indices|empty_indirect_skip|indirect_draw_pending_producer|list_restart|pipelined_indirect_draw|queued_indirect_read_order|rect_list_indirect|settled_indirect_read|sparse_indirect_indices)".CMAKE_CXX_FLAGS_RELEASE=-O3 -DNDEBUG. CPU affinity was 0x3FFF, and the machine's GPU lock was held only during each side's runs.e0ef70639d, the main parent of merge78f69176be. This PR is stacked on #24572, which is not on main, so the main side is plain main and not #24572.The test tree was clean before the runs and was restored afterwards. Logs (per-run ctest output, build logs, test lists and GPU info for each side) are kept and can be attached on request.
Earlier rounds showed the check is what the new cases test: with the
prepareDrawline put back toReadsOverlap, the stencil replay tests fail with "stencil replay refuses depth or stencil feedback reads" (4090 and Mac), and with the stencil plane removed from the check, the stencil-plane case fails ("an export with a buffer over its own stencil plane was not refused", Mac, onfe7991546e). Helldiverr reproduced the first control on AMD RDNA4 (below). Earlier Mac runs (M5 Max, MoltenVK, validation layer loaded, 0 VUIDs) passed the stencil replay tests in all three forms on heads up tof60a477be8; they were not repeated forbf3390787e.Other testers, each with this PR's head merged onto main (the head carries #24572, so both PRs are tested together):
f60a477be8merged onto main6bc2a885f1: AMD Radeon AI PRO R9700 (gfx1201), driver 32.0.31044.16, Windows 11 Pro (build 26300), validation layer with synchronization validation. The three stencil replay forms,stencil_null_feedbackandnative_multisample_depthpassed 3 of 3, with 0VUID-lines and 0 synchronization hazards. With theprepareDrawline put back toReadsOverlap, the three replay forms fail with "stencil replay refuses depth or stencil feedback reads".f60a477be8merged onto main2c08875217: AMD Radeon RX 6700 XT, driver 32.0.21045.29003, Windows 11 (build 22621), no validation layer. The three replay forms passed 9 of 9.dc097bfb60merged onto main7ed059fc42: NVIDIA GeForce RTX 5090 (driver 617.42) and AMD Radeon gfx1036 iGPU (driver 32.0.21045.29003), Windows 11, validation layer 1.4.369. The same five tests passed 15 of 15 on each GPU, with and without the layer, and 0VUID-lines.Title: Onimusha: Way of the Sword (PPSA27836 01.000.001), Windows, RTX 4090, driver 621.14. On
ee764ca7fdplus #24325 plus #24572, the first story load stops on "stencil replay refuses depth or stencil feedback reads". The draw is the indirect fullscreen triangle #24572 describes: no colour targets, a single-sample D32_SFLOAT_S8_UINT target at 960x1080, depth GREATER_OR_EQUAL with write on, and an unbounded operation-value export. Its pixel shader never reads that target. It fetches a different 960x1080 image (an R32G32_UINT view of a compute output at another address) at the pixel position, exportstexel.yas depth, and makes one read through an address. The only overlap the check found was 2 of the 2,067 regions of the shared address space, which hold the depth and stencil planes; none of the shader's own regions and none of the draw inputs overlapped. With this PR on the same tree (one run, 30 min cap, CROSS pressed at the autosave notice, New Game, difficulty and control layout): the feedback refusal is gone, the draw logs the once-per-process "replays all 256 stencil reference values" line, and the title keeps loading. It then stops about 1,440 s in, about 5.5 min after the last CROSS started the load, on "sample-preserving guest publication of native multisample depth is not implemented for this owner" in compute shader 0x211143a900 (DepthSurface::PublishToGuest). An earlier diagnostic run that only skipped the space regions in this check stopped on the same error. Whether that owner is the surface the replay wrote is not established, and it is a separate issue.Checklist
main; no other open PR implements the same functions (stacked on #24572, see Depends on; no open or merged PR changes this check or handles feedback in the replay. #10193, #13928 and #13929 edit the shared address-space regions inGuestBufferMemorybut not the descriptor range lists this PR reads)NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Summary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T01:16:03Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change based on the code inspected; the reported hardware runs are author-supplied and were not rerun here.
What I ran
Reviewed head
fe7991546e4b05685c3dffc97d4e5bfac3230ec0. No commands or tests were run; repository context was read withrepo_read.pytools at the pinned head. Read the prepared description, commits, manifest, diff, and the included changed files except the oversizedGuestBufferMemory.cpp; read its changed lines through the reader. The manifest lists all nine changed files as included, with only that source file's full text excluded.Results
Findings
Blocking
None found in the inspected changes.
Non-blocking
None.
Not verified
No tests were run. The reported Windows and macOS hardware runs and game behavior were not independently verified. The complete text of
GuestBufferMemory.cppwas excluded by the review folder size limit; its changed implementation lines were read. The unmodified surrounding implementation and broader runtime behavior were not exhaustively reviewed.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T05:31:10Z.
Review: COMMENTED
Reviewed — no blocking findings
The targeted code supports the descriptor range check and indirect stencil replay changes, but this review is incomplete because not every changed line was inspected.
What I ran
Reviewed head
0b1a2a5f5710c7dc4c3c33c548a5dda9a195387dusing the prepared review bundle andrepo_read.py. No tests or build checks were run.Results
Findings
Blocking
None identified in the code inspected.
Non-blocking
None.
Not verified
The changed lines in
Draw.cppandStencilReplay.cppwere not all inspected.GuestBufferMemory.cppwas excluded from the prepared files; only the new overlap function and nearby descriptor-range recording code were read through the repository reader. The full suite, build, and console behavior were not independently verified. The author-reported hardware runs in the description were not run by this review.Files read:
Draw.hpp,GuestBufferMemory.hpp,ShaderResources.hpp, targeted ranges inDraw.cpp, targeted ranges inGuestBufferMemory.cpp, targeted ranges inStencilReplay.cpp, and all three changed debt documents. The review bundle’sdescription.md,commits.md,manifest.json,diff.patch, andREADME.mdwere also read.GuestBufferMemory.cppwas excluded from the bundle; the remainder of the changed source lines was not inspected.To get this to a good state
Nothing is required. Optional: inspect the remaining changed lines and run the relevant checks before making a formal approval decision.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T06:26:53Z.
Review: COMMENTED
Reviewed — comments
The descriptor-range feedback check is narrowly scoped, but indirect stencil replay forces potentially GPU-produced arguments through an unconditional CPU read without first resolving pending writers, which can replay stale records.
What I ran
Reviewed head
dc097bfb60ff525778b0c9ed881f014ef0d44f77. No commands or tests were run. Read the PR description, commits, diff, manifest,Draw.cpp,GuestBufferMemory.hpp, the changedGuestBufferMemory.cppimplementation, the stencil replay test, and the three changed debt documents. All changed files listed as reviewed in the manifest were read;files.jsonwas not supplied as a readable review document, but the file listing identifiesGuestBufferMemory.cppas excluded from the prepared folder, and its changed implementation was read through the pinned reader.Results
Findings
Blocking
None verified.
Non-blocking
core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp:4305— Stencil replay unconditionally selectsStencilReplay, bypassing thedecidechecks used by other indirect draws to flush pending storage-image writes, wait for pending labels or copied writes, and establish whether the arguments are safely imported. The CPU read at lines 4317–4321 callsFinishLeaseWrites(), but that does not visibly cover those per-range pending writers. If a GPU batch has written indirect records that are not yet visible to the CPU, stencil replay can consume stale arguments or count data. Preserve the required synchronization and flush decisions before reading records, or demonstrate that the existing hooks cover every producer path.Not verified
FinishLeaseWrites()was not confirmed for all producer paths.files.jsonwas not separately available to inspect; file inclusion and exclusion status came from the manifest and reader listing.To get this to a good state
Nothing is required. Optional: clarify or preserve indirect-record synchronization for stencil replay, and add coverage for records produced by pending GPU writes.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T04:24:24Z.
Review: APPROVED
Reviewed — looks good
The reviewed head is
f60a477be8e4ac5fdaaebaaf69a42a0edc3baf3d; inspected source supports merging, with no blocking findings in the files read.What I ran
No commands or tests were run;
repo_read.pyis a read-only source reader. The description reports author-run test results, which I did not independently reproduce.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
GuestBufferMemory.cppwas read only around the changed method; other context in that large file was not inspected.StencilReplay.cppand the debt-note files were included in the review bundle but not read in full, so their remaining behavior and content are not verified.files.jsonwas not available through the reader; the manifest lists eight changed files, seven included in the review folder and one excluded as too large. Console behavior and the reported tests were not independently verified.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-27T04:34:50Z.
Review: COMMENTED
Actionable comments posted: 5
3ae1bb8a-38e0-418e-96c3-f4d023da5a7bReviewing files that changed from the base of the PR and between
9e97e05ba9andfcaae18d75.core/libs/prx/libSceAgcDriver/Graphics/include/Draw.hppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/execution/StencilReplay.cppdocs/dev/debt/changes/fix-stencil-replay-depth-feedback.mddocs/dev/debt/changes/fix-stencil-replay-indirect-draws.mddocs/dev/debt/changes/fix-stencil-replay-multisample.mdIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-27T06:08:17Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this change; the implementation and included changed files support merging.
What I ran
Reviewed head
35d0c65b6a4e50d3f2b79b67462df657fe422041using the prepared diff andrepo_read.py. No build or tests were run during this review.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
The full text of
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppwas excluded because it is too large for the review folder; its changed helper was visible in the diff but its surrounding implementation was not read.core/libs/prx/libSceAgcDriver/tests/execution/StencilReplay.cppwas included but only its opening lines were read; the test implementation was not reviewed. Other included changed files were not read in full. Build, tests, and console behavior were not independently verified; hardware results in the description are author-reported.To get this to a good state
Nothing is required. Optional: review the omitted implementation and test sections and run the changed test suite on this head.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-27T06:53:14Z.
Review: COMMENTED
Reviewed — no blocking findings
The inspected changes show no verified blocking defect; the reported test runs are author-supplied and were not run in this review.
What I ran
No commands were run. I inspected the prepared diff and the changed code at head
2b93ded754e16ae7942ed4113242f7614b36e975using the repository reader.Results
Findings
Blocking
None verified.
Non-blocking
None.
Not verified
The full test suite and hardware behavior were not independently verified. I read the prepared description, commits, manifest, diff, and review README. I inspected the changed code in
Draw.hpp,GuestBufferMemory.hpp,ShaderResources.hpp,Draw.cpp,GuestBufferMemory.cpp, andStencilReplay.cpp, plus the three changed debt notes.GuestBufferMemory.cppwas excluded from the prepared files bundle and read through the repository reader. No changed file was left unread; test results and console behavior remain author-reported.To get this to a good state
Nothing is required. The author-reported tests are optional evidence to consider; they were not rerun in this review.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-29T05:29:09Z.
Review: COMMENTED
Actionable comments posted: 3
3b3be582-afc2-4af1-9d2e-4f8adcb28881Reviewing files that changed from the base of the PR and between
2b93ded754and78f69176be.core/libs/prx/libSceAgcDriver/Graphics/include/Draw.hppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/execution/StencilReplay.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-29T05:46:44Z.
Review: COMMENTED
Reviewed — comments
Changes are required before merge: verify the safety of allowing buffer device address reads to overlap a depth or stencil plane during stencil replay.
What I ran
Reviewed head
78f69176be424a4bbc9817d36f44f1bb3508d809using the prepared diff andrepo_read.py. No commands or tests were run.Results
Findings
Blocking
None verified.
Non-blocking
core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp:4563.Not verified
GuestBufferMemory.cppwas excluded from the review folder because it was too large. I read only lines 9221–9238 throughrepo_read.py, covering the added overlap method; the rest was not reviewed.Draw.cppwas read only around the changed feedback check. Other changed regions, including indirect record resolution and replay handling, were not reviewed.StencilReplay.cppwas read only around the indirect replay test additions; the full test file was not reviewed.Draw.hpp,GuestBufferMemory.hpp,ShaderResources.hpp, and both other changed debt documents were not read.files.jsonwas not available through the reader.To get this to a good state
Nothing is required. Optional: document or test the synchronization and visibility assumptions for address-based reads that overlap the bound depth or stencil planes.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-29T06:47:22Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the review supports merging, though the indirect replay bounds and superseded descriptor ranges could use further scrutiny.
What I ran
Reviewed head
48e30a6cc16df3f301a7acaf1cd7651a5de8239cusingrepo_read.pyto inspect the prepared diff, changed lines, and targeted source ranges. No build or tests were run in this review.Results
Findings
Blocking
None.
Non-blocking
ResolveIndirectRecordusesstd::uint32_tfor the resolved ranges, and the overflow check is applied only to replay paths. The current guard coversfirst + countandfirstInstance + instances; additional edge coverage for the indexed and non-indexed range arithmetic would make this boundary easier to maintain (core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp:2562,core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp:4644).core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp:9233).Not verified
No build, tests, validation layer, console behavior, or hardware run was performed here; those results in the description are author-reported. The full text was available and read for the included changed files. The changed files
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppandcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppwere excluded from the review folder due to size; their added lines and adjacent context were read throughrepo_read.py. Files excluded by rule are not separately enumerated in the material inspected.To get this to a good state
Nothing is required. Optional: add boundary tests for indexed/non-indexed indirect ranges and partially overlapping superseded descriptor ranges.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-29T17:12:04Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the points below are optional.
What I ran
bf3390787edda2733c8cc20893fe10c85391c038using the pinnedrepo_read.pyreader. No build or tests were run by me.description.md,commits.md,manifest.json,diff.patch, and the full text of the included changed files:Draw.hpp,GuestBufferMemory.hpp,ShaderResources.hpp,Draw.cpp,StencilReplay.cpp, and the three changed debt notes. Read the excluded implementation filesGuestBufferMemory.cppandShaderResources.cppthrough the reader for their changed functions.files.json; the reader’s file listing confirms the changed file set and identifies the two excluded implementation files.Results
Findings
Blocking
None.
Non-blocking
Draw.cpp:4622routes every stencil replay indirect draw through CPU record reads. The debt note documents the synchronization and performance limitation; a future GPU expansion path could avoid that wait.Draw.cpp:4554leaves the attachment feedback check on the broaderReadsOverlappredicate. The description and debt note call out its possible over-refusal for address-based shaders; consider a follow-up if this affects real draws.Not verified
repo_read.py.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-12-03T07:12:43Z.
Review: APPROVED
I read the diff at
bf3390787e. Windows 11, GCC 15.2, merged intomainatcde48b5800etogether with other open PRs (so not an isolated build), withANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1andVK_LAYER_KHRONOS_validationon an RTX 5080 (621.24) and a Radeon iGPU (26.11.2).agc_driver_stencil_replay,_no_gpland_unsuppressed: pass on both GPUs. The loader log shows the layer inserted and no "was not found" line, and neither run has aVUID-orSYNC-HAZARDline.main's): all three fail on both GPUs: "unsafe primitive routing was not refused" (the control also lacks the other routing PRs of the tree).The open thread is about a case that
mainhas too, and #27734 fixes it.Not checked: a title.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T21:43:13Z.
Review: COMMENTED
I followed the separate checks for descriptor ranges, relocated read-only buffers and draw inputs. The two-buffer tests guard against one relocated range accidentally exempting another reader. The remaining raw BDA case depends on all replay passes seeing the pre-draw guest bytes, as the debt fragment says. Please keep explicit runtime evidence of that ordering when rebasing on #24572. The paired stencil fixture is queued on lavapipe, and the Metal path remains outside the local run.
AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.