Merge pull request #441 from oneandonlydean/fix/agc-partial-block-write-coverage

fix(agc): keep a surface's results in the 64 KiB block it shares with its neighbour
This commit is contained in:
Dean
2026-10-04 11:32:24 +13:00
committed by GitHub
5 changed files with 202 additions and 3 deletions
@@ -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:$<TARGET_FILE_DIR:libc>")
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)
@@ -74,6 +74,7 @@ bool UnchangedSinceAll(std::span<const UnchangedQuery> 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
@@ -16,6 +16,7 @@
#include <stdexcept>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#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<DriverPiece, 4> pieces{};
};
std::unordered_map<std::uint64_t, DriverPieces> 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::uint32_t>(std::max(address, begin) - begin);
const auto to = static_cast<std::uint32_t>(std::min<std::uint64_t>(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<std::uint64_t>(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
@@ -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<std::size_t>(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<bool>& layers) {
const auto from = std::max(at, begin);
const auto to = std::min(at + block, end);
if (from >= to) continue;
if (changedBlocks[static_cast<std::size_t>((at - spanBegin) / block)] == GuestMemory::BlockWritten) {
const bool edge = from != at || to != at + block;
if (changedBlocks[static_cast<std::size_t>((at - spanBegin) / block)] == GuestMemory::BlockWritten && (!edge || GuestMemory::StoredOver(from, static_cast<std::size_t>(to - from), layerGeneration[layer]))) {
skippedAny = true;
skippedLayer[layer] = true;
++skipped;
@@ -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 <windows.h>
#else
#include <sys/mman.h>
#endif
#include <array>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <stdexcept>
#include <string>
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<std::uintptr_t>(raw);
const auto aligned = (begin + Block - 1) & ~static_cast<std::uintptr_t>(Block - 1);
if (aligned != begin) munmap(raw, aligned - begin);
if (aligned + bytes != begin + bytes + Block) munmap(reinterpret_cast<void*>(aligned + bytes), begin + Block - aligned);
void* block = reinterpret_cast<void*>(aligned);
GuestWriteWatch::GuestWriteWatchRegister_nid_postfix(block, bytes);
#endif
Require(Watched(reinterpret_cast<std::uint64_t>(block), bytes), "the test block is not watched");
return block;
}
void CheckSharedBlock() {
void* memory = AllocateWatched(3 * Block);
const auto base = reinterpret_cast<std::uint64_t>(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<std::size_t>(boundary - firstBegin)) != 0, "a driver store into watched memory is not stamped");
std::array<std::uint64_t, 2> generations{synced, synced};
std::array<std::uint8_t, 2> 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<std::size_t>(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<std::size_t>(sharedFirst), synced), "the first surface's own store is not seen over its part of the shared block");
Require(!StoredOver(boundary, static_cast<std::size_t>(secondEnd - boundary), synced), "the second surface reads as stored over");
const auto touched = MarkWritten(boundary + 0x100, 4);
Require(StoredOver(boundary, static_cast<std::size_t>(sharedSecond), synced), "a store over the second surface's bytes is not seen");
Require(!StoredOver(boundary, static_cast<std::size_t>(sharedSecond), touched), "a store older than the generation is seen");
const auto whole = TrackerGeneration();
MarkWritten(base + Block, Block);
Require(StoredOver(boundary, static_cast<std::size_t>(sharedSecond), whole), "a store over the whole block is not seen");
const auto afterWhole = TrackerGeneration();
MarkWritten(base + Block, static_cast<std::size_t>(sharedFirst));
Require(!StoredOver(boundary, static_cast<std::size_t>(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<std::uint64_t>(store) * 64, 4);
Require(StoredOver(boundary, static_cast<std::size_t>(sharedSecond), many), "forgotten driver stores are taken as missing the range");
const auto beforeCpu = CollectWrites(base, 3 * Block);
static_cast<volatile std::uint8_t*>(memory)[Block + 8] = 0x22;
CollectWritesUncached(base, 3 * Block);
Require(StoredOver(boundary, static_cast<std::size_t>(sharedSecond), beforeCpu), "a CPU store in the shared block is not seen");
std::array<std::uint8_t, 64> unwatched{};
const auto outside = reinterpret_cast<std::uint64_t>(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;
}