mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(agc): register driver eq events and deliver end-of-pipe interrupts
sceAgcDriverAddEqEvent and sceAgcDriverDeleteEqEvent were NotImplemented, so a title that waits on its equeue for the GPU to reach the end of a command buffer could never be woken. AddEqEvent now registers a graphics-core event (filter -14, ident = the given id, EV_CLEAR, data counting the interrupts since the last wait) on the kernel equeue and remembers the registration; DeleteEqEvent removes both. An invalid equeue or deleting an event that is not registered throws. The driver delivers the interrupt when it executes a RELEASE_MEM whose INT_SEL is non-zero. Such a packet is not deferred as a GPU label: the recorded work before it is drained, its memory write (if any) is done, and then the event registered with the id of the issuing queue is triggered (0 for the graphics ring, the ACB queue id for compute), which matches how titles derive their event ids from the ring they submit on.
This commit is contained in:
@@ -106,6 +106,7 @@ target_link_libraries(libSceAgcDriver PRIVATE
|
||||
shader_recompiler_agc_driver
|
||||
SDL2-static
|
||||
libc
|
||||
libkernel
|
||||
)
|
||||
|
||||
set_target_properties(libSceAgcDriver PROPERTIES CXX_EXTENSIONS OFF)
|
||||
@@ -125,6 +126,7 @@ target_include_directories(agc_driver_tests PRIVATE
|
||||
|
||||
target_link_libraries(agc_driver_tests PRIVATE
|
||||
libSceAgcDriver
|
||||
libkernel
|
||||
libc
|
||||
)
|
||||
|
||||
|
||||
@@ -1,4 +1,12 @@
|
||||
#ifndef CORE_LIBS_PRX_LIBSCEAGCDRIVER_EQ_INCLUDE_EVENT_HPP
|
||||
#define CORE_LIBS_PRX_LIBSCEAGCDRIVER_EQ_INCLUDE_EVENT_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include "SceTypes.hpp"
|
||||
|
||||
void AgcDriverDeliverEopInterrupt(std::uint32_t queue);
|
||||
|
||||
extern "C" int APS5_VABI sceAgcDriverAddEqEvent(KernelEqueue eq, int id, void* udata);
|
||||
extern "C" int APS5_VABI sceAgcDriverDeleteEqEvent(KernelEqueue eq, int id);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,25 +1,82 @@
|
||||
#include "prx/libSceAgcDriver/Eq/include/Event.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <mutex>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "SceTypes.hpp"
|
||||
#include "prx/libc/include/General.hpp"
|
||||
#include "prx/libkernel/Equeue/Equeue.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int16_t EvfiltGraphicsCore = -14;
|
||||
|
||||
struct Registration {
|
||||
KernelEqueue eq;
|
||||
int id;
|
||||
};
|
||||
|
||||
std::mutex g_mutex;
|
||||
std::vector<Registration> g_registrations;
|
||||
|
||||
}
|
||||
|
||||
void AgcDriverDeliverEopInterrupt(std::uint32_t queue) {
|
||||
std::vector<Registration> registrations;
|
||||
{
|
||||
std::lock_guard lock(g_mutex);
|
||||
registrations = g_registrations;
|
||||
}
|
||||
for (const auto& registration : registrations) {
|
||||
if (registration.id == static_cast<int>(queue)) {
|
||||
EqueueTriggerEvent_nid_postfix(registration.eq, static_cast<uintptr_t>(registration.id), EvfiltGraphicsCore, nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
int APS5_VABI sceAgcDriverAddEqEvent(KernelEqueue eq, int id, void* udata) {
|
||||
(void)eq;
|
||||
(void)id;
|
||||
(void)udata;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
if (eq == 0 || !EqueuePin_nid_postfix(eq)) {
|
||||
throw std::runtime_error(std::string(__func__) + ": invalid event queue");
|
||||
}
|
||||
KernelEqueueEvent event{};
|
||||
event.event.ident = static_cast<uintptr_t>(id);
|
||||
event.event.filter = EvfiltGraphicsCore;
|
||||
event.event.flags = EV_ADD | EV_CLEAR;
|
||||
event.event.udata = udata;
|
||||
event.filter.triggerFunc = [](KernelEqueueEvent* e, void*) {
|
||||
e->event.data = e->triggered ? e->event.data + 1 : 1;
|
||||
e->triggered = true;
|
||||
};
|
||||
event.filter.resetFunc = [](KernelEqueueEvent* e) {
|
||||
e->triggered = false;
|
||||
e->event.data = 0;
|
||||
};
|
||||
const int result = EqueueAddEvent_nid_postfix(eq, event);
|
||||
if (result != EQUEUE_OK) {
|
||||
throw std::runtime_error(std::string(__func__) + ": event queue rejected the event");
|
||||
}
|
||||
std::lock_guard lock(g_mutex);
|
||||
if (std::none_of(g_registrations.begin(), g_registrations.end(), [&](const Registration& r) { return r.eq == eq && r.id == id; })) {
|
||||
g_registrations.push_back({eq, id});
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI sceAgcDriverDeleteEqEvent(KernelEqueue eq, int id) {
|
||||
(void)eq;
|
||||
(void)id;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
{
|
||||
std::lock_guard lock(g_mutex);
|
||||
std::erase_if(g_registrations, [&](const Registration& r) { return r.eq == eq && r.id == id; });
|
||||
}
|
||||
if (EqueueDeleteEvent_nid_postfix(eq, static_cast<uintptr_t>(id), EvfiltGraphicsCore) != EQUEUE_OK) {
|
||||
throw std::runtime_error(std::string(__func__) + ": event is not registered");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "prx/libSceAgcDriver/Execution/include/PerformanceTimer.hpp"
|
||||
#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp"
|
||||
#include "prx/libSceAgcDriver/Execution/include/ShaderMemory.hpp"
|
||||
#include "prx/libSceAgcDriver/Eq/include/Event.hpp"
|
||||
#include "prx/libSceAgcDriver/Execution/include/Pm4.hpp"
|
||||
#include "prx/libSceAgcDriver/Execution/include/QueueState.hpp"
|
||||
#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp"
|
||||
@@ -5748,7 +5749,8 @@ private:
|
||||
// Labels that store nothing (RELEASE_MEM without data select or destination:
|
||||
// interrupt-only) need no drain because Pm4::Execute is a no-op for them. The label
|
||||
// counters and the [sync] report live at namespace scope (see reportSync).
|
||||
if (!drainAll && (opcode == 0x49 || opcode == 0x37)) {
|
||||
const bool endOfPipeInterrupt = opcode == 0x49 && ((packet[2] >> 24u) & 7u) != 0;
|
||||
if (!drainAll && !endOfPipeInterrupt && (opcode == 0x49 || opcode == 0x37)) {
|
||||
if (const auto label = Pm4::DecodeLabelWrite(packet)) {
|
||||
const auto bytes = label->Bytes();
|
||||
if (DeferLabels() && bytes.size() <= DeferredLabel::Capacity && bytes.size() % 4 == 0 && label->address % 4 == 0) {
|
||||
@@ -6037,6 +6039,7 @@ private:
|
||||
finishDrawPacket(drawn);
|
||||
} else if (opcode != 0x42 && opcode != 0x46 && opcode != 0x58) {
|
||||
if (!wroteOnGpu) Pm4::Execute(packet, queue);
|
||||
if (endOfPipeInterrupt) AgcDriverDeliverEopInterrupt(submission.queue);
|
||||
}
|
||||
if (drawPacket) Graphics::Recorder::CountRecordedWork();
|
||||
cursor += count;
|
||||
|
||||
@@ -704,7 +704,7 @@ void Execute(std::span<const std::uint32_t> packet, QueueState& queue) {
|
||||
}
|
||||
case 0x49: {
|
||||
// Earlier work has drained by the time the driver reaches this packet, so the end-of-pipe
|
||||
// write can happen immediately. Interrupt delivery is not modeled.
|
||||
// write can happen immediately.
|
||||
const auto dataSelect = packet[2] >> 29u;
|
||||
const auto destination = address(packet[3], packet[4]);
|
||||
if (dataSelect == 0 || destination == 0) return;
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include "prx/libSceAgcDriver/Submit/include/Dcb.hpp"
|
||||
#include "prx/libSceAgcDriver/Submit/include/Acb.hpp"
|
||||
#include "prx/libSceAgcDriver/Eq/include/Query.hpp"
|
||||
#include "prx/libSceAgcDriver/Eq/include/Event.hpp"
|
||||
#include "prx/libkernel/Equeue/Equeue.hpp"
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
@@ -12,6 +14,9 @@
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
extern "C" int APS5_VABI sceKernelCreateEqueue(KernelEqueue* eq, const char* name);
|
||||
extern "C" int APS5_VABI sceKernelDeleteEqueue(KernelEqueue eq);
|
||||
|
||||
static_assert(sizeof(Packet) == 16);
|
||||
static_assert(offsetof(Packet, addr) == 0);
|
||||
static_assert(offsetof(Packet, dw_num) == 8);
|
||||
@@ -144,6 +149,41 @@ void testSubmissions() {
|
||||
AgcDriverWaitIdle_nid_postfix();
|
||||
}
|
||||
|
||||
void testEndOfPipeInterrupts() {
|
||||
KernelEqueue eq = 0;
|
||||
check(sceKernelCreateEqueue(&eq, "AGC test") == 0, "event queue creation failed");
|
||||
auto owner = EqueuePin_nid_postfix(eq);
|
||||
int graphicsTag = 0;
|
||||
int computeTag = 0;
|
||||
check(sceAgcDriverAddEqEvent(eq, 0, &graphicsTag) == 0, "graphics event registration failed");
|
||||
check(sceAgcDriverAddEqEvent(eq, 0x20, &computeTag) == 0, "compute event registration failed");
|
||||
expectFailure([] { sceAgcDriverAddEqEvent(0, 0, nullptr); });
|
||||
std::array<std::uint32_t, 8> words{0xc0064900, 0, 1u << 24u, 0, 0, 0, 0, 0};
|
||||
Packet packet{words.data(), static_cast<std::uint32_t>(words.size()), 0, {}};
|
||||
check(sceAgcDriverSubmitDcb(&packet) == 0 && sceAgcDriverSubmitDcb(&packet) == 0, "interrupt submit failed");
|
||||
AgcDriverWaitIdle_nid_postfix();
|
||||
std::array<KernelEvent, 2> events{};
|
||||
check(owner->GetTriggeredEvents(events.data(), 2) == 1, "graphics end-of-pipe interrupt was not delivered to its queue only");
|
||||
check(events[0].filter == -14 && events[0].udata == &graphicsTag && events[0].data == 2 && sceAgcDriverGetEqEventType(events.data()) == 0, "graphics end-of-pipe event encoding is wrong");
|
||||
check(owner->GetTriggeredEvents(events.data(), 2) == 0, "delivered interrupt was not cleared");
|
||||
check(sceAgcDriverSubmitAcb(0x20, &packet) == 0, "compute interrupt submit failed");
|
||||
AgcDriverWaitIdle_nid_postfix();
|
||||
check(owner->GetTriggeredEvents(events.data(), 2) == 1 && events[0].udata == &computeTag && sceAgcDriverGetEqEventType(events.data()) == 0x20, "compute end-of-pipe interrupt missing");
|
||||
words[2] = 0;
|
||||
check(sceAgcDriverSubmitDcb(&packet) == 0, "plain release submit failed");
|
||||
AgcDriverWaitIdle_nid_postfix();
|
||||
check(owner->GetTriggeredEvents(events.data(), 2) == 0, "release without INT_SEL raised an interrupt");
|
||||
check(sceAgcDriverDeleteEqEvent(eq, 0) == 0, "graphics event deletion failed");
|
||||
expectFailure([&] { sceAgcDriverDeleteEqEvent(eq, 0); });
|
||||
words[2] = 1u << 24u;
|
||||
check(sceAgcDriverSubmitDcb(&packet) == 0, "interrupt submit after deletion failed");
|
||||
AgcDriverWaitIdle_nid_postfix();
|
||||
check(owner->GetTriggeredEvents(events.data(), 2) == 0, "deleted event still received interrupts");
|
||||
check(sceAgcDriverDeleteEqEvent(eq, 0x20) == 0, "compute event deletion failed");
|
||||
owner.reset();
|
||||
check(sceKernelDeleteEqueue(eq) == 0, "event queue deletion failed");
|
||||
}
|
||||
|
||||
void testWorkerFailure() {
|
||||
std::array<std::uint32_t, 5> words{0xc0031500, 1, 1, 1, 0x41};
|
||||
Packet packet{words.data(), static_cast<std::uint32_t>(words.size()), 0, {}};
|
||||
@@ -168,6 +208,7 @@ int main() {
|
||||
testValidation();
|
||||
testClearState();
|
||||
testSubmissions();
|
||||
testEndOfPipeInterrupts();
|
||||
testWorkerFailure();
|
||||
check(expectFailure([] { LibcRunShutdown_nid_postfix(); }).find("required shader register") != std::string::npos, "shutdown lost worker failure");
|
||||
std::puts("AGC driver submit tests passed");
|
||||
|
||||
Reference in New Issue
Block a user