Merge pull request #1100 from gookie-dev/fix/agc-indirect-register-lists-at-submission

fix(agc): read indirect register lists when the command buffer is submitted
This commit is contained in:
Adrià
2026-10-08 09:14:02 +02:00
committed by GitHub
8 changed files with 63 additions and 14 deletions
@@ -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);
@@ -26,6 +26,7 @@ struct Submission {
std::map<std::size_t, std::shared_ptr<IFlipRequest>> flips;
std::map<std::size_t, std::shared_ptr<IRenderingWait>> renderingWaits;
std::map<std::size_t, std::size_t> conditionalEnds;
std::map<std::size_t, std::vector<std::uint32_t>> registerLists;
bool suspend = false;
bool waitFree = false;
@@ -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<const std::uint32_t> packet, std::uint32_t queue);
void Execute(std::span<const std::uint32_t> packet, QueueState& queue);
inline bool IndirectRegisterOpcode(std::uint32_t opcode) { return opcode == 0x63 || opcode == 0x64 || opcode == 0x9f; }
std::vector<std::uint32_t> ReadIndirectRegisters(std::span<const std::uint32_t> packet);
void ExecuteIndirectRegisters(std::span<const std::uint32_t> packet, std::span<const std::uint32_t> 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.
@@ -294,7 +294,8 @@ void Driver::execute(const Submission& submission) {
dumpSampleCounters(packet[2] | (static_cast<std::uint64_t>(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);
}
@@ -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<const std::uint32_t>(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<const void*>(descriptor.addr));
@@ -716,6 +716,28 @@ DrawParameters ResolveDraw(std::span<const std::uint32_t> packet, const QueueSta
return {address, count, indexSize, queue.instanceCount, packet.back(), true, indexOffset->second, 0};
}
std::vector<std::uint32_t> ReadIndirectRegisters(std::span<const std::uint32_t> packet) {
require(packet.size() == 5 && IndirectRegisterOpcode((packet[0] >> 8u) & 0xffu), "expected indirect register packet");
std::vector<std::uint32_t> pairs(static_cast<std::size_t>(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<const std::uint32_t> packet, std::span<const std::uint32_t> 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<std::size_t>(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<const std::uint32_t> packet, QueueState& queue) {
const auto opcode = (packet[0] >> 8u) & 0xffu;
switch (opcode) {
@@ -764,20 +786,9 @@ void Execute(std::span<const std::uint32_t> 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<std::uint32_t> pairs(static_cast<std::size_t>(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.
@@ -677,6 +677,21 @@ void submitWords(std::vector<std::uint32_t>& 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<std::uint32_t, 2> 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<std::uint32_t>(gate).store(1);
AgcDriverWaitIdle_nid_postfix();
check(std::atomic_ref<std::uint32_t>(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<std::uint32_t, 16> results{};
@@ -946,6 +961,7 @@ int main(int argc, char** argv) {
testPredication();
testUnwrittenUserData();
testDriverSubmission();
testRegisterListsReadAtSubmission();
testPredicatedSubmission();
testConditionalSubmission();
testBranchSubmission();
+1
View File
@@ -262,6 +262,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.