diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/Recorder.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/Recorder.hpp index 4fc3ff4c4..697ce09f8 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/Recorder.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/Recorder.hpp @@ -110,6 +110,8 @@ public: // overlapping in-flight batch's fence has signaled (its in-place GPU stores are final in host // memory; its completion stores are the caller's business, see VulkanDevice::CopyBuffer). bool PendingWriteSettled(std::uint64_t address, std::size_t bytes) const; + std::uint64_t LastWriteNote(std::uint64_t address, std::size_t bytes) const; + std::uint64_t NewestWriteNote(std::uint64_t address, std::size_t bytes) const; // Batch read tracking: the guest ranges the recorded work reads IN PLACE through a host import // and the GPU has not executed yet (a V# element bound in place, a region the GPU copies out of // an import, an address-based build's leased heaps, indirect arguments, a GPU-direct storage @@ -485,6 +487,7 @@ private: std::vector> kept; std::vector> completions; std::vector> writes; + std::vector writeNotes; // In-place reads (see NotePendingRead), dying with the batch: a finished batch's reads are done. struct Read { std::uint64_t begin; @@ -692,6 +695,7 @@ private: // snapshot (a rebuild from inside a completion must not drop them) until finish returns. std::vector finishing; std::uint64_t submissions = 0; + std::uint64_t writeNoteCount = 0; // Command buffers and fences of completed batches, reused by later ones (hundreds of batches per // frame would otherwise allocate and free their objects each time). std::vector> spare; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/DccMetadata.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/DccMetadata.cpp index fe70f3dc5..43d168e2e 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/DccMetadata.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/DccMetadata.cpp @@ -129,6 +129,7 @@ struct GpuKeyStore { std::uint64_t begin; std::uint64_t end; DccKeys keys = DccKeys::Uncompressed; + std::uint64_t note = 0; }; struct KeyStoreMemo { @@ -232,7 +233,7 @@ bool StoreUncompressedOnGpu(const Context& context, Recorder& recorder, std::uin std::lock_guard lock(memo.mutex); const auto end = begin + count; std::erase_if(memo.entries, [&](const GpuKeyStore& store) { return begin < store.end && store.begin < end; }); - if (MemoRecorder(memo) == &recorder) memo.entries.push_back({begin, end}); + if (MemoRecorder(memo) == &recorder) memo.entries.push_back({begin, end, DccKeys::Uncompressed, recorder.LastWriteNote(begin, count)}); } CountStore(Scans().gpuStores); TraceKeyStore("gpu", begin, count); @@ -257,11 +258,31 @@ void NoteKeysFillOnGpu(std::uint64_t begin, std::size_t count, DccKeys keys) { std::lock_guard lock(memo.mutex); const auto end = begin + count; std::erase_if(memo.entries, [&](const GpuKeyStore& store) { return begin < store.end && store.begin < end; }); - if (MemoRecorder(memo) != nullptr) memo.entries.push_back({begin, end, keys}); + auto* recorder = MemoRecorder(memo); + if (recorder != nullptr) memo.entries.push_back({begin, end, keys, GuestMemory::GpuMutex().HeldByThisThread() ? recorder->LastWriteNote(begin, count) : 0}); } namespace { +std::optional PendingStoreKeys(Recorder& recorder, std::uint64_t begin, std::size_t count) { + const auto end = begin + count; + std::uint64_t note = 0; + DccKeys keys = DccKeys::Mixed; + { + auto& memo = Memo(); + std::lock_guard lock(memo.mutex); + if (MemoRecorder(memo) != &recorder) return std::nullopt; + for (const auto& store : memo.entries) { + if (store.note == 0 || store.begin > begin || store.end < end) continue; + note = store.note; + keys = store.keys; + break; + } + } + if (note == 0 || AnyShadowedOverlaps(begin, count) || recorder.NewestWriteNote(begin, count) != note) return std::nullopt; + return keys; +} + // Whether the keys already read as uncompressed: scans the same bytes as ReadDccKeys, so it counts // as a scan too. bool AlreadyUncompressed(std::uint64_t metaAddress, std::size_t count) { @@ -424,6 +445,7 @@ DccKeys CurrentDccKeys(std::uint64_t metaAddress, std::uint64_t surfaceBytes) { const auto count = static_cast(surfaceBytes / KeyBytes); if (metaAddress != 0 && count != 0 && GuestMemory::GpuMutex().HeldByThisThread()) { if (auto* recorder = Recorder::Active(); recorder != nullptr && recorder->PendingWriteOverlaps(metaAddress, count)) { + if (const auto keys = PendingStoreKeys(*recorder, metaAddress, count)) return *keys; Recorder::CountSync(2); recorder->SyncThrough(metaAddress, count); } diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp index 61247fd3e..050e4791e 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp @@ -2001,6 +2001,7 @@ bool Recorder::noteWrite(std::uint64_t address, std::size_t bytes, bool ownLabel ensureOpen(); const auto end = address + bytes; open->writes.emplace_back(address, end); + open->writeNotes.push_back(++writeNoteCount); if (!ownLabel) markOverwritten(address, end); // A poller waiting on this range learns that the open batch may now hold its producer. if (activeRecorder == this) writeGeneration.fetch_add(1, std::memory_order_release); @@ -2025,6 +2026,7 @@ void Recorder::noteWriteOn(Batch& batch, std::uint64_t address, std::size_t byte // when the batch finishes, and the hook must sync for a CPU read until then). The generation // moves as well, so a poller re-consults the label table for a completion label. batch.writes.emplace_back(address, address + bytes); + batch.writeNotes.push_back(++writeNoteCount); if (!ownLabel) markOverwritten(address, address + bytes); if (activeRecorder == this) writeGeneration.fetch_add(1, std::memory_order_release); if (!SnapshotCovers(address, address + bytes)) publishPendingWrites(); @@ -2113,6 +2115,26 @@ bool Recorder::PendingWriteSettled(std::uint64_t address, std::size_t bytes) con return true; } +std::uint64_t Recorder::LastWriteNote(std::uint64_t address, std::size_t bytes) const { + if (open == nullptr || open->writes.empty() || open->writeNotes.size() != open->writes.size()) return 0; + if (open->writes.back() != std::pair{address, address + bytes}) return 0; + return open->writeNotes.back() == writeNoteCount ? writeNoteCount : 0; +} + +std::uint64_t Recorder::NewestWriteNote(std::uint64_t address, std::size_t bytes) const { + if (bytes == 0) return 0; + const auto end = address + bytes; + std::uint64_t newest = 0; + const auto scan = [&](const Batch& batch) { + for (std::size_t i = 0; i < batch.writes.size(); ++i) { + if (address < batch.writes[i].second && batch.writes[i].first < end) newest = std::max(newest, batch.writeNotes[i]); + } + }; + if (open != nullptr) scan(*open); + for (const auto& batch : inFlight) scan(*batch); + return newest; +} + bool Recorder::ReadTracking() { return ReadTrackingEnabled(); } diff --git a/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp b/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp index 21e16edf1..25013c719 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp @@ -1830,6 +1830,61 @@ void importWindowTests(const Device& device, Recorder& recorder) { #endif } +void pendingKeyStoreTests(const Device& device, Recorder& recorder) { + const auto& context = device.GetContext(); + if (context.hostImportAlignment == 0) { + std::cout << "host imports unavailable: pending key stores not tested\n"; + return; + } + constexpr std::size_t surfaceBytes = 65536; + constexpr std::size_t keyCount = surfaceBytes / 256; + constexpr std::size_t bytes = 65536; +#ifdef _WIN32 + void* block = VirtualAlloc(nullptr, bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); +#else + void* block = std::aligned_alloc(65536, bytes); +#endif + Require(block != nullptr, "cannot allocate the pending key store block"); + auto* keys = static_cast(block); + const auto address = reinterpret_cast(block); + { + GuestAllocations::Mutation mutation; + mutation.Add(block, bytes, true, true); + } + struct Unregister { + const Context& context; + void* block; + std::uint64_t address; + ~Unregister() { + { + GuestAllocations::Mutation mutation; + mutation.Remove(block); + } + HostImportFor(context, address, bytes); + } + } unregister{context, block, address}; + if (HostImportFor(context, address, bytes) == nullptr) { + std::cout << "host import of the pending key store block refused: pending key stores not tested\n"; + return; + } + recorder.Sync(); + std::memset(keys, 0x00, keyCount); + MarkDccUncompressed(context, address, surfaceBytes); + Require(recorder.PendingWriteOverlaps(address, keyCount) && keys[0] == 0x00, "the uncompressed key store did not stay pending"); + Require(CurrentDccKeys(address, surfaceBytes) == DccKeys::Uncompressed, "the keys after a pending uncompressed store do not read as uncompressed"); + Require(recorder.PendingWriteOverlaps(address, keyCount), "reading keys the driver's own pending store wrote waited for the GPU"); + Require(CurrentDccKeys(address + 16, surfaceBytes / 2) == DccKeys::Uncompressed, "a key range inside the pending store does not read as uncompressed"); + recorder.NotePendingWrite(address + 16, 16); + Require(CurrentDccKeys(address, surfaceBytes) == DccKeys::Uncompressed && !recorder.PendingWriteOverlaps(address, keyCount), "a key read with a later writer over the pending store did not wait for it"); + Require(std::all_of(keys, keys + keyCount, [](std::uint8_t key) { return key == 0xff; }), "the uncompressed key store did not land"); + recorder.NotePendingWrite(address, keyCount); + NoteKeysFillOnGpu(address, keyCount, DccKeys::Clear0001); + Require(CurrentDccKeys(address, surfaceBytes) == DccKeys::Clear0001 && recorder.PendingWriteOverlaps(address, keyCount), "the keys of a pending fill did not read as its keys without a wait"); + recorder.NotePendingWrite(address, 2 * keyCount); + Require(CurrentDccKeys(address, surfaceBytes) == DccKeys::Uncompressed && !recorder.PendingWriteOverlaps(address, keyCount), "a later wider writer over a pending fill was not waited for"); + recorder.Sync(); +} + void metadataPassTests(const Device& device, Recorder& recorder) { const auto& base = device.GetContext(); if (base.hostImportAlignment == 0) { @@ -2350,6 +2405,7 @@ int main() { minLodTests(device, recorder); firstLayerViewTests(device, recorder); metadataPassTests(device, recorder); + pendingKeyStoreTests(device, recorder); std::cout << "Recorder read tracking and label tests passed\n"; return 0; } catch (const std::exception& error) {