Merge pull request #439 from oneandonlydean/fix/agc-dma-copy-on-gpu

fix(agc): record large memory-to-memory DMA_DATA copies on the GPU in queue order
This commit is contained in:
Dean
2026-10-04 11:32:07 +13:00
committed by GitHub
4 changed files with 56 additions and 0 deletions
@@ -124,6 +124,12 @@ struct StoreWrite {
std::span<const std::byte> Bytes() const { return ownedBytes.empty() ? viewBytes : std::span<const std::byte>(ownedBytes); }
};
std::optional<StoreWrite> ResolveStore(std::span<const std::uint32_t> packet, const QueueState& queue, std::size_t limit);
struct MemoryCopy {
std::uint64_t source;
std::uint64_t destination;
std::size_t bytes;
};
std::optional<MemoryCopy> DecodeMemoryCopy(std::span<const std::uint32_t> 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
@@ -7,6 +7,7 @@
#include "prx/libSceAgcDriver/Eq/include/Event.hpp"
#include <cstdlib>
#include <cstring>
#include <limits>
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<const void*>(copy->source), copy->bytes) && GuestMemory::Accessible(reinterpret_cast<const void*>(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<std::size_t>::max(), 0, 0, submission.queue, [](std::span<const std::byte>, std::uint64_t) {});
if (outcome.path == 1 || outcome.path == 3) {
wroteOnGpu = true;
drained = false;
}
}
}
}
if (drained) ++storesDrained;
}
@@ -519,6 +519,16 @@ std::optional<StoreWrite> ResolveStore(std::span<const std::uint32_t> packet, co
}
}
std::optional<MemoryCopy> DecodeMemoryCopy(std::span<const std::uint32_t> 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<const std::uint32_t> packet) {
require(!packet.empty() && ((packet[0] >> 8u) & 0xffu) == 0x58, "cache barrier requires ACQUIRE_MEM");
Validate(packet, 0);
@@ -338,6 +338,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<std::uint32_t>(from), static_cast<std::uint32_t>(from >> 32u), static_cast<std::uint32_t>(to), static_cast<std::uint32_t>(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;
@@ -544,6 +567,7 @@ int main(int argc, char** argv) {
testIndirectDraw();
testMemory();
testCopies();
testMemoryCopyDecode();
testMemorySynchronization();
testEventWrite();
testAcquireMem();