From c8524bb82696efe9321e26ee3157077c0dbdbe12 Mon Sep 17 00:00:00 2001 From: ukrainianspy-12 Date: Tue, 24 Nov 2026 03:11:40 -0800 Subject: [PATCH 1/4] fix(agc): correct write-watch lifetime and stencil invalidation Keep explicit-unwatch intent until the range itself changes, preserve genuine write stamps through internal rewatches, and leave resident multisample DCC copies alone during import settling. Co-Authored-By: Claude Code --- .../Execution/include/GuestMemory.hpp | 2 + .../Execution/src/GuestMemory.cpp | 103 +++++++++++++++++- .../Graphics/src/DepthSurface.cpp | 3 +- .../Graphics/src/GuestBufferMemory.cpp | 11 +- .../libSceAgcDriver/tests/WriteTracking.cpp | 77 +++++++++++++ .../tests/execution/StencilPlaneMemory.cpp | 34 ++++++ 6 files changed, 221 insertions(+), 9 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp index c4421b596c9e..7bee287b2478 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp @@ -69,7 +69,9 @@ void WriteChangedKeepingGuest(std::uint64_t address, std::span // UnchangedSince is always false, so callers fall back to comparing bytes. bool WriteWatched(); void Unwatch(std::uint64_t address, std::size_t bytes); +void UnwatchImportBookkeeping(std::uint64_t address, std::size_t bytes); void Rewatch(std::span> ranges); +void RewatchSettled(std::span> ranges); bool ImportWatched(std::uint64_t address, std::size_t bytes, const std::function& import, bool staysWatched = true, bool importMayWriteGuest = true); void SetUnwatchHook(void (*hook)(std::uint64_t address, std::size_t bytes)); bool UnwatchWritesProven(); diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp index 238041183a07..58469c1ca98d 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #ifdef _WIN32 @@ -842,6 +843,26 @@ std::size_t ImagePagesSize() { return Pages().image.size; } +namespace { +std::mutex explicitUnwatchMutex; +std::unordered_set explicitUnwatchPages; +std::atomic forgetOnInvalidation{false}; + +void clearExplicitUnwatchPages(std::uintptr_t address, std::size_t bytes) { + constexpr std::uint64_t page = 4096; + if (bytes == 0 || bytes > std::numeric_limits::max() - address) return; + std::lock_guard lock(explicitUnwatchMutex); + for (auto at = address & ~(page - 1); at < address + bytes; at += page) explicitUnwatchPages.erase(at); +} +} + +void ForgetPages(std::uintptr_t address, std::size_t bytes); + +void invalidateAllocatedMemory(std::uintptr_t address, std::size_t bytes) { + clearExplicitUnwatchPages(address, bytes); + if (forgetOnInvalidation.load(std::memory_order_relaxed)) ForgetPages(address, bytes); +} + void ForgetPages(std::uintptr_t address, std::size_t bytes) { bumpSubscriptions(address, bytes, WriteBumpSource::Forget); forgetCalls.fetch_add(1, std::memory_order_relaxed); @@ -912,7 +933,8 @@ void PageStates::initialize() { imageCached = image.allocate(); } #endif - if (arenaCached || imageCached || WriteIndexEnabled()) GuestAllocations::GuestAllocationsSetInvalidator_nid_postfix(&ForgetPages); + forgetOnInvalidation.store(arenaCached || imageCached || WriteIndexEnabled(), std::memory_order_relaxed); + GuestAllocations::GuestAllocationsSetInvalidator_nid_postfix(&invalidateAllocatedMemory); if (arenaCached || imageCached) GuestArena::GuestArenaSetAccessObserver_nid_postfix(&ForgetInaccessiblePages); }); ready.store(true, std::memory_order_release); @@ -1668,6 +1690,8 @@ struct WriteTracker { std::unordered_map driverPieces; std::unordered_map rewatchStamps; std::atomic anyRewatchStamp{false}; + std::unordered_map genuineStamps; + std::atomic anyGenuineStamp{false}; struct CpuPages { std::array pages{}; @@ -2141,7 +2165,14 @@ void stampWrittenRun(void* context, std::uintptr_t begin, std::uintptr_t end) { else { const auto generation = tracker.generation.load(std::memory_order_relaxed); noteWrittenRun(runs, begin, end, generation); - for (auto block = tracker.blockOf(begin); block <= tracker.blockOf(end - 1); ++block) tracker.stamp(block, generation, runs.kind); + for (auto block = tracker.blockOf(begin); block <= tracker.blockOf(end - 1); ++block) { + tracker.stamp(block, generation, runs.kind); + if (runs.kind == StampKind::Cpu) { + auto& genuine = tracker.genuineStamps[block]; + genuine = std::max(genuine, generation); + tracker.anyGenuineStamp.store(true, std::memory_order_release); + } + } } #ifndef _WIN32 collectDirtyRuns.fetch_add(1, std::memory_order_relaxed); @@ -2163,7 +2194,13 @@ void stampWindowRuns(StampRuns& runs) { const bool kept = held[index++] != 0; noteWrittenRun(runs, static_cast(page), static_cast(page + WritePageBytes), generation, !kept); if (!kept) tracker.notePages(tracker.windowPages, page, page + WritePageBytes, generation); - tracker.stamp(tracker.blockOf(page), generation, kept ? runs.window->held : runs.kind); + const auto block = tracker.blockOf(page); + tracker.stamp(block, generation, kept ? runs.window->held : runs.kind); + if (runs.kind == StampKind::Cpu && !kept) { + auto& genuine = tracker.genuineStamps[block]; + genuine = std::max(genuine, generation); + tracker.anyGenuineStamp.store(true, std::memory_order_release); + } } } } @@ -2930,13 +2967,33 @@ void SetFreshCollectHook(FreshCollectHook hook) { } #endif -void Unwatch(std::uint64_t address, std::size_t bytes) { +namespace { + +void unwatchRange(std::uint64_t address, std::size_t bytes, bool explicitGuest) { if (bytes == 0 || bytes > std::numeric_limits::max() - address) return; + Pages().initialize(); beforeUnwatch(address, bytes); + constexpr std::uint64_t page = 4096; + std::vector pages; + if (explicitGuest) for (auto at = address & ~(page - 1); at < address + bytes; at += page) pages.push_back(at); auto& tracker = Tracker(); const auto lock = lockTracker(tracker); tracker.initialize(); unwatchLocked(tracker, address, bytes); + if (explicitGuest) { + std::lock_guard mark(explicitUnwatchMutex); + for (const auto at : pages) explicitUnwatchPages.insert(at); + } +} + +} + +void Unwatch(std::uint64_t address, std::size_t bytes) { + unwatchRange(address, bytes, true); +} + +void UnwatchImportBookkeeping(std::uint64_t address, std::size_t bytes) { + unwatchRange(address, bytes, false); } void Rewatch(std::span> ranges) { @@ -2949,9 +3006,13 @@ void Rewatch(std::span> ranges) { for (const auto& [begin, end] : ranges) { if (end <= begin || begin < tracker.base || end - tracker.base > tracker.size || (begin - tracker.base) % WritePageBytes != 0 || (end - tracker.base) % WritePageBytes != 0) continue; const auto bytes = static_cast(end - begin); - if (tracker.coverage.Covers(begin, bytes)) continue; + if (tracker.coverage.Covers(begin, bytes)) { + clearExplicitUnwatchPages(begin, bytes); + continue; + } GuestArena::GuestArenaCompareWhenShared_nid_no_patch(static_cast(begin), bytes); if (!walkWrites(tracker, begin, end, StampKind::Cpu) || !tracker.coverage.Restore(begin, bytes)) continue; + clearExplicitUnwatchPages(begin, bytes); ++tracker.generation; tracker.noteCpuStore(begin, end, tracker.generation); for (auto block = tracker.blockOf(begin); block <= tracker.blockOf(end - 1); ++block) { @@ -2968,6 +3029,34 @@ void Rewatch(std::span> ranges) { #endif } +void RewatchSettled(std::span> ranges) { +#ifdef _WIN32 + constexpr std::uint64_t page = WritePageBytes; + std::vector> filtered; + { + std::lock_guard mark(explicitUnwatchMutex); + for (const auto& [address, limit] : ranges) { + if (limit <= address || limit > std::numeric_limits::max() - page + 1) continue; + const auto begin = address & ~(page - 1); + const auto end = (limit + page - 1) & ~(page - 1); + std::uint64_t run = 0; + for (auto at = begin; at < end; at += page) { + if (explicitUnwatchPages.contains(at)) { + if (run != 0) { + filtered.emplace_back(run, at); + run = 0; + } + } else if (run == 0) run = at; + } + if (run != 0) filtered.emplace_back(run, end); + } + } + Rewatch(filtered); +#else + Rewatch(ranges); +#endif +} + bool ImportWatched(std::uint64_t address, std::size_t bytes, const std::function& import, bool staysWatched, bool importMayWriteGuest) { if (bytes == 0 || bytes > std::numeric_limits::max() - address) return import(); if (!staysWatched) { @@ -3079,6 +3168,10 @@ bool OnlyRewatchedSince(std::uint64_t address, std::size_t bytes, std::uint64_t if (tracker.newestUnstampedStamp(address, end) > generation) return false; bool rewatched = false; for (auto block = tracker.blockOf(address); block <= tracker.blockOf(end - 1); ++block) { + if (tracker.anyGenuineStamp.load(std::memory_order_acquire)) { + const auto genuine = tracker.genuineStamps.find(block); + if (genuine != tracker.genuineStamps.end() && genuine->second > generation) return false; + } const auto stamp = tracker.stampOf(block); if (stamp <= generation) continue; const auto found = tracker.rewatchStamps.find(block); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp index 345cbc8c6427..64a8b960315b 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp @@ -2010,7 +2010,8 @@ class DepthSurface { const auto bytes = static_cast(StencilMirrorBytes()); GuestMemory::CollectWrites(resource.baseAddress, bytes); const auto generation = GuestMemory::TrackerGeneration(); - const bool changed = stencilGeneration != 0u && GuestMemory::WrittenSince(resource.baseAddress, bytes, stencilGeneration); + const bool changed = stencilGeneration != 0u && GuestMemory::WrittenSince(resource.baseAddress, bytes, stencilGeneration) && + !GuestMemory::OnlyRewatchedSince(resource.baseAddress, bytes, stencilGeneration); stencilGeneration = generation; if (!changed || (pendingClear & VK_IMAGE_ASPECT_STENCIL_BIT) != 0) return; StorageTexture::NoteRefreshWork(); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp index 863a899e38f7..4c95968bb376 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp @@ -16,6 +16,7 @@ #include "prx/libSceAgcDriver/Graphics/include/ResidentMirror.hpp" #include "prx/libSceAgcDriver/Graphics/include/ReferenceStorage.hpp" #include "prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp" +#include "prx/libSceAgcDriver/Graphics/include/MultisampleTarget.hpp" #include "prx/libSceAgcDriver/Graphics/include/UnitShadow.hpp" #include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" #include "prx/libSceAgcDriver/Execution/include/CaptureTrace.hpp" @@ -1523,6 +1524,10 @@ void rewatchSettledImports(HostImports& state) { const auto stop = (base + import->second.bytes + settleBlockBytes - 1) & ~(settleBlockBytes - 1); if ((first != base || stop != base + import->second.bytes) && comparedImportShares(state, base, first, stop)) continue; const std::array, 1> span{{{first, stop}}}; + if (MultisampleDccUnwatchLocksGpu(span)) { + ++state.settlesKeptCompared; + continue; + } static const bool keepUnderImages = std::getenv("APS5_NO_REWATCH_UNDER_IMAGES") != nullptr; if (keepUnderImages && StorageTexture::AnyLiveOverlaps(span)) { ++state.settlesKeptCompared; @@ -1537,7 +1542,7 @@ void rewatchSettledImports(HostImports& state) { } state.settleReady.clear(); if (ranges.empty()) return; - GuestMemory::Rewatch(ranges); + GuestMemory::RewatchSettled(ranges); for (const auto base : bases) { const auto import = state.imports.find(base); if (import == state.imports.end() || !GuestMemory::Watched(base, static_cast(import->second.bytes))) continue; @@ -2057,7 +2062,7 @@ void rewatchRetired(const HostImports& state, std::vector kept; const auto check = [&](std::map::iterator it) { if (sameRange(it->second, lease)) { - if (it->second.unwatched) GuestMemory::Unwatch(it->first, it->second.bytes); + if (it->second.unwatched) GuestMemory::UnwatchImportBookkeeping(it->first, it->second.bytes); kept.push_back(it->first); return std::next(it); } diff --git a/core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp b/core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp index ce34cd954b37..985f0103dedb 100644 --- a/core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp @@ -919,9 +919,15 @@ void CheckRewatch() { BumpCollectEpoch(); const auto before = CollectWrites(base, 4 * Block); Require(before != 0, "the test range was not collected"); + GuestAllocations::Mutation allocation; + allocation.Add(memory, 4 * Block, true, true, true); Unwatch(imported, 2 * Block); const auto unwatched = UnwatchSerial(); Require(!Watched(imported, Block) && UntrackedHint(imported, Block) != 0, "the unwatched block does not read as untracked"); + const std::array, 1> explicitSpan{{{imported, imported + Block}}}; + RewatchSettled(explicitSpan); + Require(!Watched(imported, Block), "a settled rewatch over an explicitly unwatched block returned it to the write watch"); + allocation.Remove(memory); bytes[Block + 8] = 0x22; const std::array, 1> unaligned{{{imported + 1, imported + Block}}}; Rewatch(unaligned); @@ -989,6 +995,76 @@ void CheckRewatchKeepsDriverStores(LostDriverStore kind) { GuestArena::GuestArenaRelease_nid_postfix(memory, Bytes); } +void CheckRewatchMarkerLifetime() { + constexpr std::uint64_t page = 4096; + void* memory = GuestArena::GuestArenaAllocate_nid_postfix(4 * Block, Block); + GuestArena::GuestArenaCommit_nid_postfix(memory, 4 * Block, PAGE_READWRITE, 16384); + const auto base = reinterpret_cast(memory); + Require(Watched(base, 4 * Block), "the marker lifetime test range is not watched"); + std::memset(memory, 0x44, 4 * Block); + BumpCollectEpoch(); + const auto before = CollectWrites(base, 4 * Block); + Require(before != 0, "the marker lifetime test range was not collected"); + using Span = std::array, 1>; + const Span first{{{base, base + page}}}; + const Span mixed{{{base + 1, base + 2 * page - 1}}}; + + void* unrelated = GuestArena::GuestArenaAllocate_nid_postfix(4 * Block, Block); + GuestArena::GuestArenaCommit_nid_postfix(unrelated, Block, PAGE_READWRITE, Block); + { + GuestAllocations::Mutation target; + target.Add(memory, 4 * Block, true, true, true); + } + Unwatch(base, page); + Require(!Watched(base, page), "the explicit-unwatch marker was not established"); + { + GuestAllocations::Mutation mutation; + mutation.Add(unrelated, Block, true, true, true); + } + RewatchSettled(first); + Require(!Watched(base, page), "an unrelated allocation mutation discarded explicit-unwatch intent"); + GuestAllocations::GuestAllocationsInvalidate_nid_postfix(reinterpret_cast(unrelated), Block); + RewatchSettled(first); + Require(!Watched(base, page), "an unrelated invalidation discarded explicit-unwatch intent"); + + Unwatch(base + page, page); + GuestAllocations::GuestAllocationsInvalidate_nid_postfix(base + page, page); + RewatchSettled(mixed); + Require(!Watched(base, page) && Watched(base + page, page), "a mixed settled rewatch did not restore only the unmarked page"); + + Rewatch(first); + Require(Watched(base, page), "a public rewatch did not clear the explicit-unwatch marker"); + Unwatch(base, page); + { + GuestAllocations::Mutation remap; + remap.Protect(memory, page, true, true, true, [] {}); + } + RewatchSettled(first); + Require(Watched(base, page), "a remapped explicitly unwatched page kept its marker"); + + Unwatch(base, page); + { + GuestAllocations::Mutation removal; + removal.Remove(memory); + } + { + GuestAllocations::Mutation addition; + addition.Add(memory, 4 * Block, true, true, true); + } + RewatchSettled(first); + Require(Watched(base, page), "a freed and reused address kept an explicit-unwatch marker"); + + UnwatchImportBookkeeping(base, page); + Require(!Watched(base, page), "bookkeeping unwatch did not remove the watch"); + RewatchSettled(first); + Require(Watched(base, page), "bookkeeping unwatch created an explicit-unwatch marker"); + + GuestArena::GuestArenaReset_nid_postfix(unrelated, Block); + GuestArena::GuestArenaRelease_nid_postfix(unrelated, 4 * Block); + GuestArena::GuestArenaReset_nid_postfix(memory, 4 * Block); + GuestArena::GuestArenaRelease_nid_postfix(memory, 4 * Block); +} + using RewatchSpan = std::array, 1>; RewatchSpan BlockSpan(std::uint64_t address, std::size_t bytes) { @@ -2978,6 +3054,7 @@ int main(int argc, char** argv) { CheckPrivateMappingReuse(); CheckRewatch(); for (const auto kind : {LostDriverStore::Piece, LostDriverStore::PossiblePiece, LostDriverStore::Whole, LostDriverStore::PossibleWhole}) CheckRewatchKeepsDriverStores(kind); + CheckRewatchMarkerLifetime(); CheckUntrackedHint(); CheckHostHoleInChild(HostHoleMode, ChildHostHole()); CheckHostHoleInChild(MetadataCommitMode, ProbedHostHole()); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/StencilPlaneMemory.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/StencilPlaneMemory.cpp index 9af5921e8492..56285f01bd57 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/StencilPlaneMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/StencilPlaneMemory.cpp @@ -188,6 +188,39 @@ void* AllocateSharedBlocks(std::size_t bytes) { #endif } +void CheckRewatchAfterStencilWrite(AgcDriver::VulkanDevice& device, const DepthFastClearHarness::Shaders& shaders, std::uint32_t swizzle, int mode) { + auto* blocks = static_cast(AllocateBlocks(DepthBytes + StencilBytes)); + auto* depth = reinterpret_cast(blocks); + auto* stencil = reinterpret_cast(blocks + DepthBytes); + std::fill(depth, depth + PlaneTexels, 1.0f); + Fill(stencil, 0u); + AgcDriver::GuestMemory::BumpCollectEpoch(); + const auto stencilBase = reinterpret_cast(stencil); + const Surface surface{reinterpret_cast(depth), stencilBase, 0, false, VK_FORMAT_D32_SFLOAT_S8_UINT, 0, 0, swizzle}; + const auto draw = [&](const DrawOptions& options) { return Draw(device, shaders, surface, options, PlaneExtent, Colors); }; + Require(draw({.depthTest = false, .stencilTest = true, .stencil = Stencil(VK_COMPARE_OP_ALWAYS, VK_STENCIL_OP_REPLACE, 0x18u)}) == PlaneTexels, "the rewatch stencil setup draw did not cover the plane"); + Require(!AgcDriver::Graphics::FlushDepthPlanes(stencilBase, StencilBytes).empty(), "the rewatch stencil setup did not store the plane"); + device.WaitIdle(); + Require(Holding(stencil, 0x18u) == StencilBytes, "the rewatch stencil setup did not reach guest memory"); + Require(draw({.depthTest = false, .stencilTest = true, .stencil = Stencil(VK_COMPARE_OP_ALWAYS, VK_STENCIL_OP_REPLACE, 0x2bu)}) == PlaneTexels, "the divergent rewatch stencil draw did not cover the plane"); + device.WaitIdle(); + + if (mode != 0) { + Fill(stencil, mode == 1 ? 0x18u : 0x77u); + Require(AgcDriver::GuestMemory::CollectWrites(stencilBase, StencilBytes) != 0, "the rewatch stencil guest write was not collected"); + } + if (AgcDriver::GuestMemory::Watched(stencilBase, StencilBytes)) { + AgcDriver::GuestMemory::Unwatch(stencilBase, StencilBytes); + const std::array, 1> span{{{stencilBase, stencilBase + StencilBytes}}}; + AgcDriver::GuestMemory::Rewatch(span); + } + Require(draw({.depthTest = false, .stencilTest = false}) == PlaneTexels, "the read-only rebind after a stencil rewatch did not cover the plane"); + static_cast(AgcDriver::Graphics::FlushDepthPlanes(stencilBase, StencilBytes)); + device.WaitIdle(); + const auto expected = mode == 0 ? 0x2bu : mode == 1 ? 0x18u : 0x77u; + Require(Holding(stencil, expected) == StencilBytes, mode == 0 ? "a rewatch alone was treated as a guest stencil write" : mode == 1 ? "a genuine same-value stencil write before a rewatch was lost" : "a changed-value stencil write before a rewatch was lost"); +} + void CheckReusedStencilPlane(AgcDriver::VulkanDevice& device, const DepthFastClearHarness::Shaders& shaders, std::uint32_t swizzle) { auto* blocks = static_cast(AllocateSharedBlocks(DepthBytes + StencilBytes)); auto* depth = reinterpret_cast(blocks); @@ -272,6 +305,7 @@ int main() { CheckConsumedStencilClear(*device, shaders, swizzle, true); CheckConsumedStencilClear(*device, shaders, swizzle, false); CheckGuestWriteBeforeRepeatedClears(*device, shaders, swizzle); + for (int mode = 0; mode != 3; ++mode) CheckRewatchAfterStencilWrite(*device, shaders, swizzle, mode); std::size_t readOnlyFailures = 0; const auto checkReadOnly = [&](const DrawOptions& options, const char* name) { From 703bf032cd0febdfddc2e9e0fc7358dbaee59093 Mon Sep 17 00:00:00 2001 From: ukrainianspy-12 Date: Tue, 24 Nov 2026 16:23:38 -0800 Subject: [PATCH 2/4] test(agc): skip rewatch regression assertions without write watch Check watch availability before collecting guest writes in the new stencil scenario, and register a no-write-watch run that still checks the GPU result. Co-Authored-By: Claude Code --- .../agc_driver_stencil_plane_memory_tests.cmake | 10 ++++++++++ .../tests/execution/StencilPlaneMemory.cpp | 17 ++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_stencil_plane_memory_tests.cmake diff --git a/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_stencil_plane_memory_tests.cmake b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_stencil_plane_memory_tests.cmake new file mode 100644 index 000000000000..f6d8edc90b25 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_stencil_plane_memory_tests.cmake @@ -0,0 +1,10 @@ +if(BUILD_TESTING) + add_test(NAME agc_driver_stencil_plane_memory_no_write_watch COMMAND agc_driver_stencil_plane_memory_tests --rewatch-only) + set_tests_properties(agc_driver_stencil_plane_memory_no_write_watch PROPERTIES + ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PIPELINE_JOURNAL=1;APS5_NO_WRITE_WATCH=1" + SKIP_RETURN_CODE 77 TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + if(WIN32) + set_tests_properties(agc_driver_stencil_plane_memory_no_write_watch PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/StencilPlaneMemory.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/StencilPlaneMemory.cpp index 56285f01bd57..6dd48004271c 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/StencilPlaneMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/StencilPlaneMemory.cpp @@ -205,11 +205,12 @@ void CheckRewatchAfterStencilWrite(AgcDriver::VulkanDevice& device, const DepthF Require(draw({.depthTest = false, .stencilTest = true, .stencil = Stencil(VK_COMPARE_OP_ALWAYS, VK_STENCIL_OP_REPLACE, 0x2bu)}) == PlaneTexels, "the divergent rewatch stencil draw did not cover the plane"); device.WaitIdle(); - if (mode != 0) { + const bool watched = AgcDriver::GuestMemory::WriteWatched() && AgcDriver::GuestMemory::Watched(stencilBase, StencilBytes); + if (mode != 0 && watched) { Fill(stencil, mode == 1 ? 0x18u : 0x77u); Require(AgcDriver::GuestMemory::CollectWrites(stencilBase, StencilBytes) != 0, "the rewatch stencil guest write was not collected"); } - if (AgcDriver::GuestMemory::Watched(stencilBase, StencilBytes)) { + if (watched) { AgcDriver::GuestMemory::Unwatch(stencilBase, StencilBytes); const std::array, 1> span{{{stencilBase, stencilBase + StencilBytes}}}; AgcDriver::GuestMemory::Rewatch(span); @@ -217,8 +218,9 @@ void CheckRewatchAfterStencilWrite(AgcDriver::VulkanDevice& device, const DepthF Require(draw({.depthTest = false, .stencilTest = false}) == PlaneTexels, "the read-only rebind after a stencil rewatch did not cover the plane"); static_cast(AgcDriver::Graphics::FlushDepthPlanes(stencilBase, StencilBytes)); device.WaitIdle(); - const auto expected = mode == 0 ? 0x2bu : mode == 1 ? 0x18u : 0x77u; - Require(Holding(stencil, expected) == StencilBytes, mode == 0 ? "a rewatch alone was treated as a guest stencil write" : mode == 1 ? "a genuine same-value stencil write before a rewatch was lost" : "a changed-value stencil write before a rewatch was lost"); + const auto expected = !watched || mode == 0 ? 0x2bu : mode == 1 ? 0x18u : 0x77u; + const char* failure = !watched ? "a stencil plane without write tracking kept the wrong GPU result" : mode == 0 ? "a rewatch alone was treated as a guest stencil write" : mode == 1 ? "a genuine same-value stencil write before a rewatch was lost" : "a changed-value stencil write before a rewatch was lost"; + Require(Holding(stencil, expected) == StencilBytes, failure); } void CheckReusedStencilPlane(AgcDriver::VulkanDevice& device, const DepthFastClearHarness::Shaders& shaders, std::uint32_t swizzle) { @@ -279,7 +281,7 @@ void CheckReusedStencilPlane(AgcDriver::VulkanDevice& device, const DepthFastCle } -int main() { +int main(int argc, char** argv) { try { const auto device = OpenVulkanTestDevice(); if (!device) return VulkanTestSkipped; @@ -302,6 +304,11 @@ int main() { Require(Holding(stencilPlane, 0x18u) == StencilBytes, "a read of the stencil plane after a draw wrote stencil 0x18 does not see it in guest memory"); Require(AgcDriver::Graphics::FlushDepthPlanes(stencilBase, StencilBytes).empty(), "a second read of the stored stencil plane stored it again"); + if (argc == 2 && std::string(argv[1]) == "--rewatch-only") { + for (int mode = 0; mode != 3; ++mode) CheckRewatchAfterStencilWrite(*device, shaders, swizzle, mode); + return 0; + } + CheckConsumedStencilClear(*device, shaders, swizzle, true); CheckConsumedStencilClear(*device, shaders, swizzle, false); CheckGuestWriteBeforeRepeatedClears(*device, shaders, swizzle); From 59b26b8f50ffb0be51833b1404b89fc3e96ec4b9 Mon Sep 17 00:00:00 2001 From: ukrainianspy-12 Date: Tue, 24 Nov 2026 16:41:12 -0800 Subject: [PATCH 3/4] fix(agc): gate stencil rewatch test on backend capability Rewatch is a no-op off Windows, so a write-watched non-Windows run must not perform the rewatch-specific stencil scenario. Expose RewatchSupported and use it to gate guest-write assertions in the regression. Co-Authored-By: Claude Code --- .../prx/libSceAgcDriver/Execution/include/GuestMemory.hpp | 1 + .../prx/libSceAgcDriver/Execution/src/GuestMemory.cpp | 8 ++++++++ .../tests/execution/StencilPlaneMemory.cpp | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp index 7bee287b2478..d08ac7aff4bf 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp @@ -70,6 +70,7 @@ void WriteChangedKeepingGuest(std::uint64_t address, std::span bool WriteWatched(); void Unwatch(std::uint64_t address, std::size_t bytes); void UnwatchImportBookkeeping(std::uint64_t address, std::size_t bytes); +bool RewatchSupported(); void Rewatch(std::span> ranges); void RewatchSettled(std::span> ranges); bool ImportWatched(std::uint64_t address, std::size_t bytes, const std::function& import, bool staysWatched = true, bool importMayWriteGuest = true); diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp index 58469c1ca98d..e7c35fa1a6db 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp @@ -2996,6 +2996,14 @@ void UnwatchImportBookkeeping(std::uint64_t address, std::size_t bytes) { unwatchRange(address, bytes, false); } +bool RewatchSupported() { +#ifdef _WIN32 + return true; +#else + return false; +#endif +} + void Rewatch(std::span> ranges) { #ifdef _WIN32 auto& tracker = Tracker(); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/StencilPlaneMemory.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/StencilPlaneMemory.cpp index 6dd48004271c..628122ea17c7 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/StencilPlaneMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/StencilPlaneMemory.cpp @@ -205,7 +205,7 @@ void CheckRewatchAfterStencilWrite(AgcDriver::VulkanDevice& device, const DepthF Require(draw({.depthTest = false, .stencilTest = true, .stencil = Stencil(VK_COMPARE_OP_ALWAYS, VK_STENCIL_OP_REPLACE, 0x2bu)}) == PlaneTexels, "the divergent rewatch stencil draw did not cover the plane"); device.WaitIdle(); - const bool watched = AgcDriver::GuestMemory::WriteWatched() && AgcDriver::GuestMemory::Watched(stencilBase, StencilBytes); + const bool watched = AgcDriver::GuestMemory::RewatchSupported() && AgcDriver::GuestMemory::WriteWatched() && AgcDriver::GuestMemory::Watched(stencilBase, StencilBytes); if (mode != 0 && watched) { Fill(stencil, mode == 1 ? 0x18u : 0x77u); Require(AgcDriver::GuestMemory::CollectWrites(stencilBase, StencilBytes) != 0, "the rewatch stencil guest write was not collected"); From 5b5c7676c98571a9d29888130ba839d6e1819169 Mon Sep 17 00:00:00 2001 From: ukrainianspy-12 Date: Fri, 27 Nov 2026 17:26:00 -0800 Subject: [PATCH 4/4] fix(agc): keep reconciled imports settleable Co-Authored-By: Claude Code --- .../Graphics/src/GuestBufferMemory.cpp | 2 +- .../tests/execution/StencilPlaneMemory.cpp | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp index 4c95968bb376..9d2d5490ca1e 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp @@ -3167,7 +3167,7 @@ bool refreshedFromLoggedChanges(const Context& context, HostImports& state) { } const auto alignment = context.hostImportAlignment; for (const auto* entry : checked) { - if (entry->unwatched) GuestMemory::Unwatch(entry->base, entry->bytes); + if (entry->unwatched) GuestMemory::UnwatchImportBookkeeping(entry->base, entry->bytes); if (entry->joined) continue; const auto desired = desiredRun(local, entry->base, alignment); if (!desired || desired->first > entry->base || entry->base + entry->bytes > desired->second || peekImport(state, desired->first, desired->second) != nullptr) continue; diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/StencilPlaneMemory.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/StencilPlaneMemory.cpp index 628122ea17c7..4481ce0ae499 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/StencilPlaneMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/StencilPlaneMemory.cpp @@ -1,6 +1,7 @@ #include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" #include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" #include "prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp" +#include "prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hpp" #include "prx/libSceAgcDriver/Graphics/include/TextureTiling.hpp" #include "prx/libc/include/GuestAllocations.hpp" #include "prx/libc/include/GuestArena.hpp" @@ -223,6 +224,44 @@ void CheckRewatchAfterStencilWrite(AgcDriver::VulkanDevice& device, const DepthF Require(Holding(stencil, expected) == StencilBytes, failure); } +void CheckReconciledImportSettles(AgcDriver::VulkanDevice& device) { + if (!AgcDriver::GuestMemory::RewatchSupported() || !AgcDriver::GuestMemory::WriteWatched()) { + std::puts("the reconciled import cannot settle without the Windows write watch; skipped"); + return; + } + constexpr std::uint64_t Window = 8ull << 20u; + auto* raw = AllocateBlocks(static_cast(2 * Window)); + const auto base = (reinterpret_cast(raw) + Window - 1) & ~static_cast(Window - 1); + auto* memory = reinterpret_cast(base); + const auto neighbour = base + Window - Block; + Require(AgcDriver::GuestMemory::Watched(base, Block), "the reconciled import test range is not write-watched"); + const auto context = device.graphicsContext(); + AgcDriver::Graphics::SetHostImportWindow(Window); + AgcDriver::Graphics::SetImportWatch(context, AgcDriver::Graphics::ImportWatch::Unwatch); + { + GuestAllocations::Mutation partitioned; + partitioned.Remove(raw); + partitioned.Add(memory, Block, true, true, true); + partitioned.Add(reinterpret_cast(neighbour), Block, true, true, true); + } + Require(AgcDriver::Graphics::HostImportFor(context, base, Block, AgcDriver::Graphics::ImportRequester::Access) != nullptr, "the reconciled import test range was not imported"); + Require(!AgcDriver::GuestMemory::Watched(base, Block), "the reconciled import stayed write-watched"); + const auto proof = AgcDriver::Graphics::HostImportsIdentity(context); + { + GuestAllocations::Mutation changed; + changed.Protect(reinterpret_cast(neighbour), Block, true, true, true, [] {}); + } + Require(AgcDriver::Graphics::HostImportsReconciledUnchanged(context, proof), "the reconciled import test lost its import proof"); + const std::array, 1> span{{{base, base + Block}}}; + AgcDriver::GuestMemory::RewatchSettled(span); + Require(AgcDriver::GuestMemory::Watched(base, Block), "a reconciled import could not settle back onto the write watch"); + AgcDriver::Graphics::ClearHostImports(device.Device()); + GuestAllocations::Mutation().Remove(memory); + GuestAllocations::Mutation().Remove(reinterpret_cast(neighbour)); + AgcDriver::Graphics::SetHostImportWindow(0); + AgcDriver::Graphics::SetImportWatch(context, AgcDriver::Graphics::ImportWatch::Watch); +} + void CheckReusedStencilPlane(AgcDriver::VulkanDevice& device, const DepthFastClearHarness::Shaders& shaders, std::uint32_t swizzle) { auto* blocks = static_cast(AllocateSharedBlocks(DepthBytes + StencilBytes)); auto* depth = reinterpret_cast(blocks); @@ -293,6 +332,7 @@ int main(int argc, char** argv) { Fill(stencilPlane, 0u); const auto stencilBase = reinterpret_cast(stencilPlane); const auto shaders = Compile(*device); + CheckReconciledImportSettles(*device); const auto swizzle = AgcDriver::Graphics::XorSwizzleMode(AgcDriver::Graphics::TextureTileMode::kZ64KBX); const Surface surface{reinterpret_cast(depthPlane), stencilBase, 0, false, VK_FORMAT_D32_SFLOAT_S8_UINT, 0, 0, swizzle}; const auto draw = [&](const DrawOptions& options) { return Draw(*device, shaders, surface, options, PlaneExtent, Colors); };