From 895c1c0eedb715020895387c200b122f0e026c36 Mon Sep 17 00:00:00 2001 From: Dean Galvin Date: Sat, 3 Oct 2026 19:19:32 +1300 Subject: [PATCH] fix(agc): record large memory-to-memory DMA_DATA copies on the GPU in queue order A DMA_DATA too large to resolve as a store drained the device and then copied on the submitting worker's thread. For a large range that copy takes milliseconds, and meanwhile another queue's worker records, submits and runs work that reads the destination in place. PPSA21564 copies each 12 MiB frame of its intro video into one of two textures with a single DMA_DATA on one queue and samples it on queue 0 without waiting for that queue; the GPU's own DMA finishes in microseconds, so the title never needed to. Here queue 0's upload of the texture ran while the CPU copy was still writing it, and a frame showed the new frame's luma over the chroma of the frame two before. Such a copy (both addresses memory and incrementing, ranges disjoint and non-empty) now goes through VulkanDevice::CopyBuffer like the copy kernel: a transfer between the host imports, recorded in order, so everything recorded after the packet reads the whole copy. The drain and CPU copy remain the fallback when either range is not host-imported. --- .../libSceAgcDriver/Execution/include/Pm4.hpp | 6 +++++ .../src/Driver/Packets/PacketMemory.cpp | 16 +++++++++++++ .../prx/libSceAgcDriver/Execution/src/Pm4.cpp | 10 ++++++++ core/libs/prx/libSceAgcDriver/tests/Pm4.cpp | 24 +++++++++++++++++++ 4 files changed, 56 insertions(+) diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp index dc2ea2691..3add10cd8 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp @@ -124,6 +124,12 @@ struct StoreWrite { std::span Bytes() const { return ownedBytes.empty() ? viewBytes : std::span(ownedBytes); } }; std::optional ResolveStore(std::span packet, const QueueState& queue, std::size_t limit); +struct MemoryCopy { + std::uint64_t source; + std::uint64_t destination; + std::size_t bytes; +}; +std::optional DecodeMemoryCopy(std::span packet); // A DISPATCH_INDIRECT's arguments: the guest address of its three group-count dwords (no memory // access, so the GPU can read them in place: VulkanDevice::DispatchIndirect), the DISPATCH_DIRECT // packet made by reading them there (through the checked guest memory path, which waits for diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketMemory.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketMemory.cpp index e5301b851..1b7c0b9c0 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketMemory.cpp @@ -7,6 +7,7 @@ #include "prx/libSceAgcDriver/Eq/include/Event.hpp" #include #include +#include namespace AgcDriver::DriverDetail { @@ -130,6 +131,21 @@ bool Driver::preparePacketMemory(const Submission& submission, QueueState& queue Graphics::Recorder::CloseLabelGroup(GuestMemory::TrackerGeneration()); } } + if (drained && opcode == 0x50) { + const auto copy = Pm4::DecodeMemoryCopy(packet); + if (copy.has_value() && copy->bytes > gpuStoreLimit && GuestMemory::Accessible(reinterpret_cast(copy->source), copy->bytes) && GuestMemory::Accessible(reinterpret_cast(copy->destination), copy->bytes, true)) { + GuestMemory::TagGpuLockSite(GuestMemory::GpuLockSite::Copy); + std::lock_guard gpuLock(GuestMemory::GpuMutex()); + if (const auto localDevice = device.Load()) { + recordDeferredLabels(localDevice.get(), submission.queue); + const auto outcome = localDevice->CopyBuffer(copy->destination, copy->source, copy->bytes, 0, std::numeric_limits::max(), 0, 0, submission.queue, [](std::span, std::uint64_t) {}); + if (outcome.path == 1 || outcome.path == 3) { + wroteOnGpu = true; + drained = false; + } + } + } + } if (drained) ++storesDrained; } diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp index 26207b5c0..a4336edc5 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp @@ -519,6 +519,16 @@ std::optional ResolveStore(std::span packet, co } } +std::optional DecodeMemoryCopy(std::span packet) { + if (packet.size() != 7 || ((packet[0] >> 8u) & 0xffu) != 0x50) return std::nullopt; + if (!memorySelector(dmaSource(packet)) || !memorySelector(dmaDestination(packet))) return std::nullopt; + const std::size_t bytes = packet[6] & 0x3ffffffu; + const auto source = address(packet[2], packet[3]); + const auto destination = address(packet[4], packet[5]); + if (bytes == 0 || (source < destination + bytes && destination < source + bytes)) return std::nullopt; + return MemoryCopy{source, destination, bytes}; +} + bool UsesGpuCacheBarrier(std::span packet) { require(!packet.empty() && ((packet[0] >> 8u) & 0xffu) == 0x58, "cache barrier requires ACQUIRE_MEM"); Validate(packet, 0); diff --git a/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp b/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp index 8a8ac97ba..68b7ba4e1 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp @@ -315,6 +315,29 @@ void testCopies() { #endif } +void testMemoryCopyDecode() { + using AgcDriver::Pm4::DecodeMemoryCopy; + constexpr std::uint64_t source = 0x1120000000ull, destination = 0x403d7b400ull; + const auto packet = [&](std::uint32_t control, std::uint64_t from, std::uint64_t to, std::uint32_t command) { + return makePacket(0x50, {control, static_cast(from), static_cast(from >> 32u), static_cast(to), static_cast(to >> 32u), command}); + }; + const auto copy = DecodeMemoryCopy(packet(0x60000000, source, destination, 0xbdd800)); + check(copy.has_value() && copy->source == source && copy->destination == destination && copy->bytes == 0xbdd800, "a memory-to-memory DMA_DATA did not decode as a copy"); + check(DecodeMemoryCopy(packet(0x00000000, source, destination, 64)).has_value(), "a DMA_DATA with memory selectors 0 did not decode as a copy"); + check(!DecodeMemoryCopy(packet(0x40000000, 0x44332211, destination, 64)).has_value(), "an immediate fill decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x60100000, source, 0x100, 64)).has_value(), "a DMA_DATA to the GDS decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x20000000, 0x100, destination, 64)).has_value(), "a DMA_DATA from the GDS decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x60000000, source, destination, 64 | (1u << 26u))).has_value(), "a register source decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x60000000, source, destination, 64 | (1u << 27u))).has_value(), "a register destination decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x60000000, source, destination, 64 | (1u << 28u))).has_value(), "a non-incrementing source decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x60000000, source, destination, 64 | (1u << 29u))).has_value(), "a non-incrementing destination decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x60000000, source, destination, 0)).has_value(), "an empty DMA_DATA decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x60000000, source, source + 32, 64)).has_value(), "overlapping ranges decoded as a copy"); + check(!DecodeMemoryCopy(packet(0x60000000, source + 32, source, 64)).has_value(), "overlapping ranges below the source decoded as a copy"); + check(DecodeMemoryCopy(packet(0x60000000, source, source + 64, 64)).has_value(), "adjacent ranges did not decode as a copy"); + check(!DecodeMemoryCopy(makePacket(0x40, {0x10101, 0, 0, 0, 0})).has_value(), "a COPY_DATA decoded as a DMA_DATA copy"); +} + void testMemorySynchronization() { struct MemoryState { std::uint32_t source = 0; @@ -520,6 +543,7 @@ int main(int argc, char** argv) { testIndirectDraw(); testMemory(); testCopies(); + testMemoryCopyDecode(); testMemorySynchronization(); testEventWrite(); testAcquireMem();