From de651c62839fe064394afc6c6e96da9be17ffd32 Mon Sep 17 00:00:00 2001 From: gookie <36005381+gookie-dev@users.noreply.github.com> Date: Wed, 7 Oct 2026 12:47:17 +0200 Subject: [PATCH] fix(agc): read indirect register lists when the command buffer is submitted The driver copies a submission's packets when it is submitted, but SET_SH_REG_INDIRECT, SET_UCONFIG_REG_INDIRECT and SET_CONTEXT_REG_INDIRECT read their (offset, value) lists from guest memory only when the queue worker reaches the packet. A title that reuses its per-frame memory once it expects the GPU to be done with it can rewrite a list in between, and the worker then throws on the data it finds there ("extended register semantics are not implemented (offset dword 0x3a888889)"). The lists are now read with the packets, after validation, in Submit and for a rewind tail, and the worker writes the registers from that copy. Pm4::Execute keeps reading the list itself for direct callers. PPSA21402 (Balatro) keeps three frame slots and does not wait for the GPU before reusing one: its slot labels start at 0xffffffff and the GPU only ever writes 0xffffffff to them, so the poll before a slot is reused never blocks. When the worker falls more than two frames behind, the title writes per-draw constants over a SET_CONTEXT_REG_INDIRECT list the worker has not read yet. The console's PFP reads the list when it reaches the packet; the difference is recorded in TechnicalDebt.md. --- .../Execution/include/Driver/Driver.hpp | 1 + .../include/Driver/Queues/Submission.hpp | 1 + .../libSceAgcDriver/Execution/include/Pm4.hpp | 3 ++ .../src/Driver/Packets/PacketExecution.cpp | 3 +- .../src/Driver/Queues/Submission.cpp | 15 ++++++++ .../prx/libSceAgcDriver/Execution/src/Pm4.cpp | 37 ++++++++++++------- core/libs/prx/libSceAgcDriver/tests/Pm4.cpp | 16 ++++++++ docs/dev/TechnicalDebt.md | 1 + 8 files changed, 63 insertions(+), 14 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp index cf2941e62..4117c3e84 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp @@ -68,6 +68,7 @@ private: static bool& onWorkerThread(); static void copyCommands(Submission& submission, const std::uint32_t* guest, std::size_t words); static bool copySegment(Submission& submission, const std::uint32_t* guest, std::size_t words, std::size_t& budget); + static void readRegisterLists(Submission& submission); void waitForFlipRoom(const Submission& submission); void reserveOutputs(Submission& submission); void executeRewindTail(const Submission& stalled); diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/Submission.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/Submission.hpp index cdc9f1683..9848ccbc1 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/Submission.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/Submission.hpp @@ -26,6 +26,7 @@ struct Submission { std::map> flips; std::map> renderingWaits; std::map conditionalEnds; + std::map> registerLists; bool suspend = false; bool waitFree = false; diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp index 7bc44cece..e6d2cbe13 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp @@ -86,6 +86,9 @@ inline std::size_t PacketWords(std::uint32_t header) { return FillerPacket(heade std::string_view UnsupportedReason(std::uint32_t header); void Validate(std::span packet, std::uint32_t queue); void Execute(std::span packet, QueueState& queue); +inline bool IndirectRegisterOpcode(std::uint32_t opcode) { return opcode == 0x63 || opcode == 0x64 || opcode == 0x9f; } +std::vector ReadIndirectRegisters(std::span packet); +void ExecuteIndirectRegisters(std::span packet, std::span pairs, QueueState& queue); bool AccessesMemory(std::uint32_t header); // Whether an ACQUIRE_MEM packet asks only for GPU cache actions (no CPU-visible memory // synchronization): such a packet needs a pipeline barrier, not a device drain. diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp index af3361308..4bb00617a 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp @@ -294,7 +294,8 @@ void Driver::execute(const Submission& submission) { dumpSampleCounters(packet[2] | (static_cast(packet[3]) << 32u)); } else if (opcode != 0x42 && opcode != 0x46 && opcode != 0x58) { if (!wroteOnGpu) { - Pm4::Execute(packet, queue); + if (Pm4::IndirectRegisterOpcode(opcode)) Pm4::ExecuteIndirectRegisters(packet, submission.registerLists.at(cursor), queue); + else Pm4::Execute(packet, queue); if (opcode == 0x49 || opcode == 0x37) { if (const auto label = Pm4::DecodeLabelWrite(packet)) noteLabelStore(label->address, label->Bytes(), ++eventSerial); } diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp index 08f5cee6c..53f8d7813 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp @@ -103,6 +103,19 @@ bool Driver::copySegment(Submission& submission, const std::uint32_t* guest, std return false; } +void Driver::readRegisterLists(Submission& submission) { + submission.registerLists.clear(); + for (std::size_t cursor = 0; cursor < submission.commands.size(); cursor += Pm4::PacketWords(submission.commands[cursor])) { + const auto header = submission.commands[cursor]; + if ((header >> 30u) != 3u || !Pm4::IndirectRegisterOpcode((header >> 8u) & 0xffu)) continue; + try { + submission.registerLists.emplace(cursor, Pm4::ReadIndirectRegisters(std::span(submission.commands).subspan(cursor, Pm4::PacketWords(header)))); + } catch (const std::exception& error) { + throw std::runtime_error("AGC driver: " + Pm4::Name(header) + " at DWORD " + std::to_string(cursor) + ": " + error.what()); + } + } +} + void Driver::waitForFlipRoom(const Submission& submission) { for (std::size_t cursor = 0; cursor < submission.commands.size(); cursor += Pm4::PacketWords(submission.commands[cursor])) { if (submission.commands[cursor] != FlipPacketHeader) continue; @@ -159,6 +172,7 @@ void Driver::executeRewindTail(const Submission& stalled) { tail.queue = stalled.queue; copyCommands(tail, stalled.rewindTail, stalled.rewindWords); validate(tail, stalled.rewindTail); + readRegisterLists(tail); waitForFlipRoom(tail); { std::lock_guard lock(mutex); @@ -190,6 +204,7 @@ void Driver::Submit(const Packet* packet, std::uint32_t queue) { } const auto copied = profile ? std::chrono::steady_clock::now() : start; validate(submission, descriptor.addr); + readRegisterLists(submission); waitForFlipRoom(submission); static const bool trace = std::getenv("APS5_TRACE_GPU") != nullptr; if (trace) std::fprintf(stderr, "[gpu] %.1f submit queue=0x%x dwords=%zu at %p\n", TraceMs(), queue, submission.commands.size(), static_cast(descriptor.addr)); diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp index 4292b133f..9c9b9bc51 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp @@ -716,6 +716,28 @@ DrawParameters ResolveDraw(std::span packet, const QueueSta return {address, count, indexSize, queue.instanceCount, packet.back(), true, indexOffset->second, 0}; } +std::vector ReadIndirectRegisters(std::span packet) { + require(packet.size() == 5 && IndirectRegisterOpcode((packet[0] >> 8u) & 0xffu), "expected indirect register packet"); + std::vector pairs(static_cast(packet[4]) * 2); + // Named for the [hooksync] attribution (the read goes through the flush hook). + const GuestMemory::ReadSiteScope site(GuestMemory::ReadSite::Registers); + GuestMemory::Read(address(packet[1], packet[2]), std::as_writable_bytes(std::span(pairs)), 4); + return pairs; +} + +void ExecuteIndirectRegisters(std::span packet, std::span pairs, QueueState& queue) { + const auto opcode = (packet[0] >> 8u) & 0xffu; + require(packet.size() == 5 && IndirectRegisterOpcode(opcode), "expected indirect register packet"); + require(pairs.size() == static_cast(packet[4]) * 2, "indirect register list does not match its packet"); + for (std::size_t i = 0; i < pairs.size(); i += 2) registerOffset(pairs[i]); + for (std::size_t i = 0; i < pairs.size(); i += 2) writeRegister(queue, opcode, registerOffset(pairs[i]), pairs[i + 1]); + if (queue.savedContext.has_value() && TraceContextState()) { + std::fprintf(stderr, "[context] indirect 0x%x:", opcode); + for (std::size_t i = 0; i < pairs.size(); i += 2) std::fprintf(stderr, " %x", registerOffset(pairs[i])); + std::fprintf(stderr, "\n"); + } +} + void Execute(std::span packet, QueueState& queue) { const auto opcode = (packet[0] >> 8u) & 0xffu; switch (opcode) { @@ -764,20 +786,9 @@ void Execute(std::span packet, QueueState& queue) { case 0x26: queue.indexBase = address(packet[1], packet[2]); return; case 0x2a: queue.indexType = packet[1]; return; case 0x2f: queue.instanceCount = packet[1]; return; - case 0x63: case 0x64: case 0x9f: { - std::vector pairs(static_cast(packet[4]) * 2); - // Named for the [hooksync] attribution (the read goes through the flush hook). - const GuestMemory::ReadSiteScope site(GuestMemory::ReadSite::Registers); - GuestMemory::Read(address(packet[1], packet[2]), std::as_writable_bytes(std::span(pairs)), 4); - for (std::size_t i = 0; i < pairs.size(); i += 2) registerOffset(pairs[i]); - for (std::size_t i = 0; i < pairs.size(); i += 2) writeRegister(queue, opcode, registerOffset(pairs[i]), pairs[i + 1]); - if (queue.savedContext.has_value() && TraceContextState()) { - std::fprintf(stderr, "[context] indirect 0x%x:", opcode); - for (std::size_t i = 0; i < pairs.size(); i += 2) std::fprintf(stderr, " %x", registerOffset(pairs[i])); - std::fprintf(stderr, "\n"); - } + case 0x63: case 0x64: case 0x9f: + ExecuteIndirectRegisters(packet, ReadIndirectRegisters(packet), queue); return; - } case 0x59: break; case 0x3c: case 0x93: { // The waited-on value is written by the CPU or another queue; poll it like the CP would. diff --git a/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp b/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp index 86640969c..e702a0094 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Pm4.cpp @@ -677,6 +677,21 @@ void submitWords(std::vector& words, std::uint32_t queue = 0) { check((queue == 0 ? sceAgcDriverSubmitDcb(&packet) : sceAgcDriverSubmitAcb(queue, &packet)) == 0, "conditional submission failed"); } +void testRegisterListsReadAtSubmission() { + alignas(8) static std::uint32_t gate = 0; + static std::uint32_t done = 0; + static std::array registers{0x10, 74}; + auto words = joinPackets({ + makePacket(0x3c, {0x13, low(&gate), high(&gate), 1, 0xffffffffu, 0x19}), + makePacket(0x9f, {low(registers.data()), high(registers.data()), 0x80000000, 1}), + writeWord(done, 1)}); + submitWords(words); + registers[0] = 0x3a888889; + std::atomic_ref(gate).store(1); + AgcDriverWaitIdle_nid_postfix(); + check(std::atomic_ref(done).load() == 1, "register list rewritten after submission was read by the worker"); +} + void testConditionalSubmission() { alignas(8) static std::uint32_t zero = 0, one = 1, condition = 0; static std::array results{}; @@ -946,6 +961,7 @@ int main(int argc, char** argv) { testPredication(); testUnwrittenUserData(); testDriverSubmission(); + testRegisterListsReadAtSubmission(); testPredicatedSubmission(); testConditionalSubmission(); testBranchSubmission(); diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 7b9455c33..ddcc71b03 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -261,6 +261,7 @@ Throughout the project, every function at every stage either **does exactly what - A [flip inside a COND_EXEC range](../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/CommandValidation.cpp) throws: the flip is reserved with the video output when the buffer is submitted, and how the console withdraws a skipped one is unknown. - A [COND_EXEC range](../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp) must hold whole packets and end inside its command buffer. A chained INDIRECT_BUFFER or a REWIND inside a range throws, as what the CP does with the skipped dwords then is unknown. - A [COND_INDIRECT_BUFFER](../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp) (`sceAgcCbBranch`) runs only with the always-pass compare function; a comparison or a predicated packet throws. Modes 1 and 2 run the first buffer, assumed to be the if-then and if-then-else modes of AMD's PM4 packet. [sceAgcCbBranch](../../core/libs/prx/libSceAgc/Cb/src/Branch.cpp) does not check the compare address of an always-pass branch or the address of an empty buffer, as PPSA14632 passes an uninitialised compare address and a null else buffer of 0 dwords. +- [SET_*_REG_INDIRECT](../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp) (libSceAgcDriver) reads its register list when the command buffer is submitted, with the packets. The console's graphics PFP reads it when it reaches the packet, so a list written after the submission (by the CPU, or by GPU work ahead of the packet behind PFP_SYNC_ME) is read here before that write. - [v_mullit_f32](../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) follows the hardware measured with the MODE register at IEEE=1 and denormals kept (a signaling NaN src0 is quieted, a positive denormal src2 counts as positive): the recompiler does not read MODE, so other IEEE/denormal settings are not modelled. - [v_fma_legacy_f32](../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) (VOP3 0x140) is assumed to be fused, which has not been measured on hardware. LLVM decodes this encoding as `v_mad_legacy_f32` for gfx1013, and `v_mac_legacy_f32` (VOP2 0x06) was measured to round its product before the add, so 0x140 may do the same. - The executable file that [relinker](../../core/relinker/elfpatcher/src/windows/WindowsPeWriter.cpp) generates opens the console when launched, which is inconvenient for playability.