diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index aa9532b25..faddc28c9 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -211,6 +211,24 @@ target_link_libraries(agc_shader_memory_tests PRIVATE shader_recompiler_agc_driv set_target_properties(agc_shader_memory_tests PROPERTIES CXX_EXTENSIONS OFF) configure_windows_unwind(agc_shader_memory_tests) +add_test_executable(agc_write_tracking_tests EXCLUDE_FROM_ALL + tests/WriteTracking.cpp + Execution/src/GuestMemory.cpp +) +target_include_directories(agc_write_tracking_tests PRIVATE ${LIBS_INCLUDE_DIR}) +target_link_libraries(agc_write_tracking_tests PRIVATE shader_recompiler_agc_driver libc) +set_target_properties(agc_write_tracking_tests PROPERTIES CXX_EXTENSIONS OFF) +configure_windows_unwind(agc_write_tracking_tests) +if(BUILD_TESTING) + set_target_properties(agc_write_tracking_tests PROPERTIES EXCLUDE_FROM_ALL OFF) + add_test(NAME agc_write_tracking COMMAND agc_write_tracking_tests) + set_tests_properties(agc_write_tracking PROPERTIES SKIP_RETURN_CODE 77) + if(WIN32) + set_tests_properties(agc_write_tracking PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() + add_test_executable(agc_structurizer_tests EXCLUDE_FROM_ALL tests/Structurizer.cpp) target_link_libraries(agc_structurizer_tests PRIVATE shader_recompiler_agc_driver libc) set_target_properties(agc_structurizer_tests PROPERTIES CXX_EXTENSIONS OFF) diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp index d13886926..6fc189b80 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp @@ -74,6 +74,7 @@ bool UnchangedSinceAll(std::span queries); // Returns the generation the blocks were stamped with (0 when the arena is not write-watched): // UnchangedSince(range, it) holds until the next store over the range. std::uint64_t MarkWritten(std::uint64_t address, std::size_t bytes); +bool StoredOver(std::uint64_t address, std::size_t bytes, std::uint64_t generation); // Collect epoch: within one epoch a range already collected is not walked again, CollectWrites // returns the current generation instead. A guest write landing between two collects of the same // epoch is seen by the next epoch, which is the ordering real hardware gives a CPU write made while a diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp index d8ca9bad0..e17302cb5 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #ifdef _WIN32 #ifndef NOMINMAX @@ -708,6 +709,44 @@ struct WriteTracker { #endif } + std::uint64_t blockBegin(std::uint64_t block) const { +#ifdef _WIN32 + return base + block * WriteBlockBytes; +#else + return block * WriteBlockBytes; +#endif + } + + struct DriverPiece { + std::uint32_t generation = 0; + std::uint32_t begin = 0; + std::uint32_t end = 0; + }; + struct DriverPieces { + std::uint32_t whole = 0; + std::uint32_t dropped = 0; + std::uint32_t next = 0; + std::array pieces{}; + }; + std::unordered_map driverPieces; + + void noteDriverStore(std::uint64_t block, std::uint64_t address, std::uint64_t end, std::uint32_t stampGeneration) { + const auto begin = blockBegin(block); + const auto from = static_cast(std::max(address, begin) - begin); + const auto to = static_cast(std::min(end, begin + WriteBlockBytes) - begin); + if (from == 0 && to == WriteBlockBytes) { + if (const auto found = driverPieces.find(block); found != driverPieces.end()) { + found->second = DriverPieces{}; + found->second.whole = stampGeneration; + } + return; + } + auto& entry = driverPieces[block]; + auto& slot = entry.pieces[entry.next++ % entry.pieces.size()]; + entry.dropped = std::max(entry.dropped, slot.generation); + slot = {stampGeneration, from, to}; + } + std::uint32_t stampOf(std::uint64_t block) const { #ifdef _WIN32 return blocks[block]; @@ -1023,10 +1062,38 @@ std::uint64_t MarkWritten(std::uint64_t address, std::size_t bytes) { const auto first = tracker.blockOf(address); const auto last = tracker.blockOf(address + bytes - 1); ++tracker.generation; - for (auto block = first; block <= last; ++block) tracker.stamp(block, tracker.generation, StampKind::Driver); + for (auto block = first; block <= last; ++block) { + tracker.stamp(block, tracker.generation, StampKind::Driver); + tracker.noteDriverStore(block, address, address + bytes, tracker.generation); + } return tracker.generation; } +bool StoredOver(std::uint64_t address, std::size_t bytes, std::uint64_t generation) { + auto& tracker = Tracker(); + const auto lock = lockTracker(tracker); + tracker.initialize(); + if (!tracker.watched || generation == 0 || bytes == 0 || !tracker.covers(address, bytes)) return true; + const auto end = address + bytes; + const auto first = tracker.blockOf(address); + const auto last = tracker.blockOf(end - 1); + for (auto block = first; block <= last; ++block) { + if (tracker.writtenStampOf(block) <= generation) continue; + if (tracker.cpuStampOf(block) > generation) return true; + const auto found = tracker.driverPieces.find(block); + if (found == tracker.driverPieces.end()) return true; + const auto& entry = found->second; + if (entry.whole > generation || entry.dropped > generation) return true; + const auto begin = tracker.blockBegin(block); + const auto from = std::max(address, begin) - begin; + const auto to = std::min(end, begin + WriteBlockBytes) - begin; + for (const auto& piece : entry.pieces) { + if (piece.generation > generation && piece.begin < to && from < piece.end) return true; + } + } + return false; +} + std::uint64_t TrackerGeneration() { auto& tracker = Tracker(); // Under the mutex: a collect bumps the generation before its walk, and a walk still running diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp index a3f312e8b..eddcc2550 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp @@ -1143,7 +1143,11 @@ bool StorageTexture::Refresh() { // unit's stamps say nothing, so it is stored. const auto droppable = [&](std::uint32_t unit) { if (keysChanged && IsDccClear(keys)) return true; - return tracked && layerGeneration[unit] != 0 && unit < stampedBlocks.size() && stampedBlocks[unit] == GuestMemory::BlockWritten; + if (!tracked || layerGeneration[unit] == 0 || unit >= stampedBlocks.size() || stampedBlocks[unit] != GuestMemory::BlockWritten) return false; + const auto begin = layerBegin(unit); + const auto bytes = layerBytes(unit); + const bool edge = begin % 65536 != 0 || bytes != 65536; + return !edge || GuestMemory::StoredOver(begin, static_cast(bytes), layerGeneration[unit]); }; // A clear code -> uncompressed flip on an image with results pending: an unstamped pending // unit's results ARE the uncompressed texels (nothing wrote its memory since), so it keeps @@ -3057,7 +3061,8 @@ void StorageTexture::writeBackLayers(const std::vector& layers) { const auto from = std::max(at, begin); const auto to = std::min(at + block, end); if (from >= to) continue; - if (changedBlocks[static_cast((at - spanBegin) / block)] == GuestMemory::BlockWritten) { + const bool edge = from != at || to != at + block; + if (changedBlocks[static_cast((at - spanBegin) / block)] == GuestMemory::BlockWritten && (!edge || GuestMemory::StoredOver(from, static_cast(to - from), layerGeneration[layer]))) { skippedAny = true; skippedLayer[layer] = true; ++skipped; diff --git a/core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp b/core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp new file mode 100644 index 000000000..0269fffa8 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp @@ -0,0 +1,108 @@ +#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" +#include "prx/libc/include/GuestArena.hpp" +#include "prx/libc/include/GuestWriteWatch.hpp" +#ifdef _WIN32 +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#else +#include +#endif +#include +#include +#include +#include +#include +#include + +namespace { + +using namespace AgcDriver::GuestMemory; + +constexpr std::size_t Block = 65536; + +void Require(bool condition, const std::string& message) { + if (!condition) throw std::runtime_error(message); +} + +void* AllocateWatched(std::size_t bytes) { +#ifdef _WIN32 + void* block = GuestArena::GuestArenaAllocate_nid_postfix(bytes, Block); + GuestArena::GuestArenaCommit_nid_postfix(block, bytes, PAGE_READWRITE, bytes); +#else + void* raw = mmap(nullptr, bytes + Block, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (raw == MAP_FAILED) throw std::runtime_error("cannot map the watched block"); + const auto begin = reinterpret_cast(raw); + const auto aligned = (begin + Block - 1) & ~static_cast(Block - 1); + if (aligned != begin) munmap(raw, aligned - begin); + if (aligned + bytes != begin + bytes + Block) munmap(reinterpret_cast(aligned + bytes), begin + Block - aligned); + void* block = reinterpret_cast(aligned); + GuestWriteWatch::GuestWriteWatchRegister_nid_postfix(block, bytes); +#endif + Require(Watched(reinterpret_cast(block), bytes), "the test block is not watched"); + return block; +} + +void CheckSharedBlock() { + void* memory = AllocateWatched(3 * Block); + const auto base = reinterpret_cast(memory); + const auto boundary = base + 2 * Block - 0xb800; + const auto firstBegin = base; + const auto secondEnd = base + 3 * Block; + const auto sharedFirst = boundary - (base + Block); + const auto sharedSecond = base + 2 * Block - boundary; + std::memset(memory, 0x11, 3 * Block); + + const auto synced = CollectWrites(base, 3 * Block); + Require(synced != 0, "the test block is not collected"); + + Require(MarkWritten(firstBegin, static_cast(boundary - firstBegin)) != 0, "a driver store into watched memory is not stamped"); + std::array generations{synced, synced}; + std::array changed{}; + Require(ChangedBlocks(base + Block, 2 * Block, generations, changed) && changed[0] == BlockWritten && changed[1] == BlockUnchanged, "the shared block does not read as written"); + Require(!StoredOver(boundary, static_cast(sharedSecond), synced), "a store of the neighbour's bytes counts for the second surface's part of the shared block"); + Require(StoredOver(base + Block, static_cast(sharedFirst), synced), "the first surface's own store is not seen over its part of the shared block"); + Require(!StoredOver(boundary, static_cast(secondEnd - boundary), synced), "the second surface reads as stored over"); + + const auto touched = MarkWritten(boundary + 0x100, 4); + Require(StoredOver(boundary, static_cast(sharedSecond), synced), "a store over the second surface's bytes is not seen"); + Require(!StoredOver(boundary, static_cast(sharedSecond), touched), "a store older than the generation is seen"); + + const auto whole = TrackerGeneration(); + MarkWritten(base + Block, Block); + Require(StoredOver(boundary, static_cast(sharedSecond), whole), "a store over the whole block is not seen"); + const auto afterWhole = TrackerGeneration(); + MarkWritten(base + Block, static_cast(sharedFirst)); + Require(!StoredOver(boundary, static_cast(sharedSecond), afterWhole), "a partial store after a whole-block store is taken for the whole block"); + + const auto many = TrackerGeneration(); + for (int store = 0; store < 6; ++store) MarkWritten(base + Block + static_cast(store) * 64, 4); + Require(StoredOver(boundary, static_cast(sharedSecond), many), "forgotten driver stores are taken as missing the range"); + + const auto beforeCpu = CollectWrites(base, 3 * Block); + static_cast(memory)[Block + 8] = 0x22; + CollectWritesUncached(base, 3 * Block); + Require(StoredOver(boundary, static_cast(sharedSecond), beforeCpu), "a CPU store in the shared block is not seen"); + + std::array unwatched{}; + const auto outside = reinterpret_cast(unwatched.data()); + if (!Watched(outside, unwatched.size())) Require(StoredOver(outside, unwatched.size(), TrackerGeneration()), "an unwatched range reads as not stored over"); +} + +} + +int main() { + try { + if (!WriteWatched()) { + std::cout << "no write watching: skipped\n"; + return 77; + } + CheckSharedBlock(); + } catch (const std::exception& error) { + std::cerr << "write tracking test failed: " << error.what() << "\n"; + return 1; + } + std::cout << "write tracking tests passed\n"; + return 0; +}