From 19f52ab4039177e6b4e78f68de6b926f4924d4bb Mon Sep 17 00:00:00 2001 From: Zenodeon Date: Sun, 18 Oct 2026 02:22:16 +0400 Subject: [PATCH 1/4] feat(agc): add opt-in bounded submission progress --- core/libs/prx/libSceAgcDriver/CMakeLists.txt | 18 ++ .../Execution/include/Driver/Driver.hpp | 4 +- .../include/Driver/Queues/Submission.hpp | 2 + .../Driver/Queues/SubmissionProgress.hpp | 44 +++++ .../Execution/include/VulkanDevice.hpp | 2 + .../src/Driver/Packets/PacketExecution.cpp | 9 +- .../src/Driver/Queues/QueueWorker.cpp | 21 ++- .../src/Driver/Queues/Submission.cpp | 69 ++++++++ .../Execution/src/VulkanDevice.cpp | 8 + .../Graphics/include/Recorder.hpp | 1 + .../libSceAgcDriver/Graphics/src/Recorder.cpp | 14 ++ .../tests/execution/SubmitProgress.cpp | 156 ++++++++++++++++++ docs/dev/debt/libSceAgcDriver.md | 6 + 13 files changed, 341 insertions(+), 13 deletions(-) create mode 100644 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/SubmissionProgress.hpp create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/SubmitProgress.cpp diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index 65f1aff6c34..beb949eda34 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -1809,6 +1809,24 @@ target_include_directories(agc_driver_image_table_cache_bounds_tests PRIVATE $) +if(BUILD_TESTING) + add_test(NAME agc_submit_progress COMMAND agc_driver_submit_progress_tests) + set_tests_properties(agc_submit_progress PROPERTIES + ENVIRONMENT "APS5_SUBMIT_PROGRESS=1;ANYPS5_REQUIRE_VULKAN=1" TIMEOUT 30 SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + if(WIN32) + set_tests_properties(agc_submit_progress PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$;PATH=path_list_prepend:$") + endif() + add_test(NAME agc_submit_progress_no_timeline COMMAND agc_driver_submit_progress_tests --fallback-only) + set_tests_properties(agc_submit_progress_no_timeline PROPERTIES + ENVIRONMENT "APS5_SUBMIT_PROGRESS=1;APS5_NO_TIMELINE=1;ANYPS5_REQUIRE_VULKAN=1" + TIMEOUT 30 SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + if(WIN32) + set_tests_properties(agc_submit_progress_no_timeline PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$;PATH=path_list_prepend:$") + endif() +endif() + if(BUILD_TESTING) add_test(NAME agc_driver_store_pending_identity COMMAND agc_driver_store_over_untracked_tests --pending-identity) set_tests_properties(agc_driver_store_pending_identity PROPERTIES diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp index 8b66e28ae61..9f68dfe4bf5 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp @@ -106,9 +106,9 @@ class Driver { static void forgetUnfinishedWrites(QueueWorker& worker, std::span labelWrites); static bool& completionDeferred(); void deferCompletion(const Submission& submission); - void submitOpenWork(); + void submitOpenWork(const std::shared_ptr& progress = {}); void wakeQueueZeroForCompletions(); - void completeSubmission(std::uint64_t serial, std::uint64_t received, std::span labelWrites); + void completeSubmission(std::uint64_t serial, std::uint64_t received, std::span labelWrites, const std::shared_ptr& progress); static bool waitFree(const Submission& submission); bool queue0Before(std::uint64_t received) const; bool orderReleased(std::uint32_t queue, std::uint64_t received) const; 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 e71457e0f31..e45f794362d 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/Submission.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/Submission.hpp @@ -3,6 +3,7 @@ #include "prx/libSceAgcDriver/Execution/include/Driver/Shaders/ShaderRegistry.hpp" #include "prx/libSceAgcDriver/Execution/include/VideoOutput.hpp" +#include "prx/libSceAgcDriver/Execution/include/Driver/Queues/SubmissionProgress.hpp" #include #include #include @@ -22,6 +23,7 @@ struct Submission { std::uint64_t serial; std::uint32_t queue; std::vector commands; + std::shared_ptr progress; std::shared_ptr shaders; std::map> flips; diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/SubmissionProgress.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/SubmissionProgress.hpp new file mode 100644 index 00000000000..5194af7ce40 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/SubmissionProgress.hpp @@ -0,0 +1,44 @@ +#ifndef CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_DRIVER_QUEUES_SUBMISSIONPROGRESS_HPP +#define CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_DRIVER_QUEUES_SUBMISSIONPROGRESS_HPP + +#include +#include +#include +#include +#include +#include + +namespace AgcDriver { class VulkanDevice; } + +namespace AgcDriver::DriverDetail { + +class SubmissionProgress { +public: + using Clock = std::chrono::steady_clock; + using Ready = std::pair, std::uint64_t>; + + void Publish(std::shared_ptr device, std::uint64_t epoch) { + { + std::lock_guard lock(mutex); + result = {std::move(device), epoch}; + ready = true; + } + changed.notify_one(); + } + + Ready WaitUntil(Clock::time_point deadline) { + std::unique_lock lock(mutex); + if (!changed.wait_until(lock, deadline, [&] { return ready; })) return {}; + return result; + } + +private: + std::mutex mutex; + std::condition_variable changed; + Ready result; + bool ready = false; +}; + +} + +#endif diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp index 4b8f170a2e3..6886b27a446 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp @@ -86,6 +86,8 @@ class VulkanDevice { bool CanWaitUnlocked() const; std::uint64_t SubmitAndEpoch(); void WaitRecorded(std::uint64_t serial); + std::uint64_t SubmittedEpoch() const; + bool WaitRecordedFor(std::uint64_t serial, std::uint64_t timeoutNanos); void ReapRecorded(std::uint64_t serial); // Completes the batches that already finished (non-blocking); under the mutex. void ReapRecorded(); 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 077fef550a6..be0c2a67ed0 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp @@ -464,8 +464,8 @@ void Driver::execute(const Submission& submission) { return; } if ((std::atomic_ref(*const_cast(submission.rewindTail - 1)).load(std::memory_order_acquire) & 0x80000000u) != 0) { - DrawPipeline::Queue0().Enqueue([this] { - submitOpenWork(); + DrawPipeline::Queue0().Enqueue([this, progress = submission.progress] { + submitOpenWork(progress); wakeQueueZeroForCompletions(); }, {}); executeRewindTail(submission); @@ -488,7 +488,10 @@ void Driver::execute(const Submission& submission) { if (localDevice != nullptr) localDevice->SubmitRecorded(submission.queue == 0); } endTiming.Finish(); - if (submission.rewindTail != nullptr) executeRewindTail(submission); + if (submission.rewindTail != nullptr) { + if (submission.progress) submitOpenWork(submission.progress); + executeRewindTail(submission); + } } } diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/QueueWorker.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/QueueWorker.cpp index ed1cb376400..546be836442 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/QueueWorker.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/QueueWorker.cpp @@ -70,19 +70,23 @@ void Driver::deferCompletion(const Submission& submission) { queue0Uncommitted.push_back(received); } completionDeferred() = true; - DrawPipeline::Queue0().Enqueue([this, serial = submission.serial, received, labelWrites = submission.labelWrites] { - completeSubmission(serial, received, labelWrites); + DrawPipeline::Queue0().Enqueue([this, serial = submission.serial, received, labelWrites = submission.labelWrites, progress = submission.progress] { + completeSubmission(serial, received, labelWrites, progress); }, {}); } -void Driver::submitOpenWork() { - if (deferredLabels().labels.empty() && !Graphics::Recorder::PendingLabelSince().has_value() && Graphics::Recorder::RecordedWorkSinceSubmit() == 0) return; +void Driver::submitOpenWork(const std::shared_ptr& progress) { + const bool work = !deferredLabels().labels.empty() || Graphics::Recorder::PendingLabelSince().has_value() || Graphics::Recorder::RecordedWorkSinceSubmit() != 0; + if (!work && !progress) return; ++submissionCosts(0).endSubmits; GuestMemory::TagGpuLockSite(GuestMemory::GpuLockSite::End); std::lock_guard gpuLock(GuestMemory::GpuMutex()); const auto localDevice = device.Load(); - recordDeferredLabels(localDevice.get(), 0); - if (localDevice != nullptr) localDevice->SubmitRecorded(true); + if (work) { + recordDeferredLabels(localDevice.get(), 0); + if (localDevice != nullptr) localDevice->SubmitRecorded(true); + } + if (progress) progress->Publish(localDevice, localDevice ? localDevice->SubmittedEpoch() : 0); } void Driver::wakeQueueZeroForCompletions() { @@ -91,7 +95,7 @@ void Driver::wakeQueueZeroForCompletions() { changed.notify_all(); } -void Driver::completeSubmission(std::uint64_t serial, std::uint64_t received, std::span labelWrites) { +void Driver::completeSubmission(std::uint64_t serial, std::uint64_t received, std::span labelWrites, const std::shared_ptr& progress) { struct Complete { Driver& driver; std::uint64_t serial, received; @@ -109,7 +113,7 @@ void Driver::completeSubmission(std::uint64_t serial, std::uint64_t received, st if (notify) driver.changed.notify_all(); } } complete{*this, serial, received, labelWrites}; - submitOpenWork(); + submitOpenWork(progress); } const std::atomic*& Driver::workerQueued() { @@ -261,6 +265,7 @@ void Driver::run(std::uint32_t id) noexcept { if (profile && submission.enqueuedAt != std::chrono::steady_clock::time_point{}) costs.dequeueNs += static_cast(std::chrono::duration_cast(std::chrono::steady_clock::now() - submission.enqueuedAt).count()); } execute(submission); + if (submission.progress && submission.rewindTail == nullptr && !completionDeferred()) submitOpenWork(submission.progress); if (traceGpu) std::fprintf(stderr, "[gpu] %.1f done serial=%llu queue=0x%x\n", TraceMs(), static_cast(submission.serial), id); const auto completeStart = profile ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point{}; PerformanceContext completionContext(id == 0 ? frameTiming() : FrameTiming::Async()); 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 13d45c0f369..1ed123fb746 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp @@ -9,6 +9,7 @@ #include "prx/libSceAgcDriver/Execution/include/SubmissionCapture.hpp" #include "prx/libSceAgcDriver/Execution/include/ThreadPriority.hpp" #include "prx/libSceAgcDriver/Eq/include/Event.hpp" +#include "prx/libSceAgcDriver/Execution/include/ProfileOutput.hpp" #include "ThreadOwned.hpp" #include "prx/libc/include/HostThreadLocal.hpp" #include @@ -43,6 +44,51 @@ bool& submitsQueue0() { return HostThreadLocal(); } +bool progressEligible(const Submission& submission) { + if (submission.queue != 0 || !submission.flips.empty() + || !submission.renderingWaits.empty() || !submission.conditionalEnds.empty()) return false; + bool terminalRelease = false; + std::optional knownWrite; + for (std::size_t at = 0; at < submission.commands.size();) { + const auto packet = std::span(submission.commands).subspan(at); + const auto count = Pm4::PacketWords(packet[0]); + if (count == 0 || count > packet.size()) return false; + const auto opcode = (packet[0] >> 8u) & 0xffu; + const bool type3 = (packet[0] >> 30u) == 3; + if (type3 && (opcode == 0x3c || opcode == 0x93)) { + if (!knownWrite || Pm4::Predicated(packet[0]) || ((packet[1] >> 4u) & 3u) != 1u) return false; + const auto wait = Pm4::DecodeWait(packet.first(count)); + const auto bytes = knownWrite->Bytes(); + if (wait.address < knownWrite->address) return false; + const auto offset = wait.address - knownWrite->address; + if (offset > bytes.size() || wait.bytes > bytes.size() - offset) return false; + std::uint64_t value = 0; + std::memcpy(&value, bytes.data() + offset, wait.bytes); + if (!Pm4::WaitComparesValue(packet.first(count), value)) return false; + terminalRelease = false; + at += count; + continue; + } + if (type3 && opcode == 0x59 && submission.rewindTail != nullptr && at + count == submission.commands.size()) { + at += count; + continue; + } + if (!Pm4::FillerPacket(packet[0]) && !(type3 && opcode == 0x10) && !Pm4::IsTagMarker(packet.first(count))) { + if (type3 && (opcode == 0x49 || opcode == 0x37)) { + const auto write = Pm4::DecodeLabelWrite(packet.first(count)); + terminalRelease = opcode == 0x49 && write.has_value(); + if (write && !Pm4::Predicated(packet[0]) && (opcode == 0x37 || (packet[2] >> 29u) < 3)) knownWrite = *write; + else if (opcode != 0x49 || (packet[2] >> 29u) != 0) knownWrite.reset(); + } else { + terminalRelease = false; + if (!type3 || (opcode != 0x42 && opcode != 0x58)) knownWrite.reset(); + } + } + at += count; + } + return terminalRelease; +} + } std::deque& Driver::releasedTails() { @@ -366,6 +412,10 @@ void Driver::Submit(const Packet* packet, std::uint32_t queue, bool cpuFlip) { const auto validated = profile ? std::chrono::steady_clock::now() : start; auto* const capture = SubmissionCapture::Active(); auto captured = capture != nullptr ? SubmissionCapture::Prepare(queue, SubmissionKind::Submit, submission.commands, submission.conditionalEnds) : CapturedSubmission{}; + static const bool progressEnabled = std::getenv("APS5_SUBMIT_PROGRESS") != nullptr; + const auto progress = progressEnabled && progressEligible(submission) ? std::make_shared() : nullptr; + submission.progress = progress; + const auto progressDeadline = progress ? std::chrono::steady_clock::now() + std::chrono::milliseconds(2) : std::chrono::steady_clock::time_point{}; { std::unique_lock lock(mutex); rethrowFailure(); @@ -406,6 +456,25 @@ void Driver::Submit(const Packet* packet, std::uint32_t queue, bool cpuFlip) { } changed.notify_all(); if (capture != nullptr) writeCaptured(*this, *capture, captured); + if (progress) { + PerformanceTimer timing("Driver.SubmitProgress"); + const auto [localDevice, epoch] = progress->WaitUntil(progressDeadline); + const auto now = std::chrono::steady_clock::now(); + bool gpuReady = false; + if (localDevice && epoch != 0 && now < progressDeadline) { + const auto remaining = std::chrono::duration_cast(progressDeadline - now).count(); + gpuReady = localDevice->WaitRecordedFor(epoch, static_cast(remaining)); + } + if (profile) { + static std::atomic eligible{0}, ready{0}, expired{0}; + if (gpuReady) ready.fetch_add(1, std::memory_order_relaxed); + else expired.fetch_add(1, std::memory_order_relaxed); + const auto count = eligible.fetch_add(1, std::memory_order_relaxed) + 1; + if (count % 1000 == 0) AgcDriver::ProfilePrint_nid_no_patch("[submit-progress] %llu eligible terminal releases, %llu GPU-ready returns, %llu expired or unsupported\n", + static_cast(count), static_cast(ready.load()), static_cast(expired.load())); + } + CheckFailure(); + } } void Driver::SuspendPoint() { diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp index 1ebaafcc123..98dc3aec475 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp @@ -2202,6 +2202,14 @@ void VulkanDevice::WaitRecorded(std::uint64_t serial) { if (state->recorder) state->recorder->WaitSerial(serial); } +std::uint64_t VulkanDevice::SubmittedEpoch() const { + return state->recorder ? state->recorder->NewestSubmitted() : 0; +} + +bool VulkanDevice::WaitRecordedFor(std::uint64_t serial, std::uint64_t timeoutNanos) { + return state->recorder && state->recorder->WaitSerialFor(serial, timeoutNanos); +} + void VulkanDevice::ReapRecorded(std::uint64_t serial) { if (!state->recorder) return; // The drain's site in the [recorder] table is this call's caller (Driver::execute's drain, diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/Recorder.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/Recorder.hpp index f4d473d7a5f..b104957dcce 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/Recorder.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/Recorder.hpp @@ -205,6 +205,7 @@ class Recorder { // it touches only the immutable timeline semaphore (the caller keeps the device alive). Requires // HasTimeline(); the debug switch APS5_NO_TIMELINE makes the device create the recorder without one. void WaitSerial(std::uint64_t serial); + bool WaitSerialFor(std::uint64_t serial, std::uint64_t timeoutNanos); // Under the mutex: finishes (completions, release) the in-flight batches up to `serial`, from the // front only; tolerates batches another thread finished meanwhile. Later batches stay in flight. void FinishUpTo(std::uint64_t serial); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp index 0cf39a8ad79..246e34d1e88 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp @@ -3536,6 +3536,20 @@ void Recorder::WaitSerial(std::uint64_t serial) { Check(result, "vkWaitSemaphoresKHR recorder"); } +bool Recorder::WaitSerialFor(std::uint64_t serial, std::uint64_t timeoutNanos) { + PerformanceTimer frameTiming("Recorder.WaitSerialFor"); + if (timeline == VK_NULL_HANDLE || serial == 0) return false; + const UnlockedWaiter waiter{id}; + VkSemaphoreWaitInfoKHR wait{VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO_KHR}; + wait.semaphoreCount = 1; + wait.pSemaphores = &timeline; + wait.pValues = &serial; + const auto result = context.Function("vkWaitSemaphoresKHR")(context.device, &wait, timeoutNanos); + if (result == VK_TIMEOUT) return false; + Check(result, "vkWaitSemaphoresKHR submission progress"); + return true; +} + void Recorder::FinishUpTo(std::uint64_t serial) { // A drain: the wait already happened (unlocked), the fences are signaled, so the source-0 wait // recorded here is the bookkeeping only (a caller that did not wait first, WaitForLeases, shows diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/SubmitProgress.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/SubmitProgress.cpp new file mode 100644 index 00000000000..6708ff185e0 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/SubmitProgress.cpp @@ -0,0 +1,156 @@ +#include "VulkanTestDevice.hpp" +#include "prx/libSceAgcDriver/Execution/include/Driver.hpp" +#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" +#include "prx/libc/include/GuestAllocations.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#endif + +namespace { +void Require(bool condition, const char* message) { if (!condition) throw std::runtime_error(message); } +alignas(256) std::array Code{0xf4080200u, 0xfa000000u, 0xbe840381u, 0x9304ff04u, 0x0019660du, 0x8004ff04u, 0x3c6ef35fu, 0x80828102u, 0xbf078002u, 0xbf85fff9u, 0xbf8cc07fu, 0x7e000204u, 0xe0700000u, 0x80020000u, 0xbf810000u}; +std::uint32_t Low(const void* p) { return static_cast(reinterpret_cast(p)); } +std::uint32_t High(const void* p) { return static_cast(reinterpret_cast(p) >> 32u); } +void Set(std::vector& words, std::uint32_t reg, std::uint32_t value) { words.insert(words.end(), {0xc0017600u, reg, value}); } +void Dispatch(std::vector& words, const void* descriptor, std::uint32_t iterations = 32) { + const auto address = reinterpret_cast(Code.data()); + Set(words, 0x20c, static_cast(address >> 8u)); + Set(words, 0x20d, static_cast(address >> 40u)); + for (auto reg : {0x207u, 0x208u, 0x209u}) Set(words, reg, 1); + Set(words, 0x212, 0); Set(words, 0x213, 6); Set(words, 0x240, Low(descriptor)); Set(words, 0x241, High(descriptor)); Set(words, 0x242, iterations); + words.insert(words.end(), {0xc0031500u, 1, 1, 1, 0x8041u}); +} +void Release(std::vector& words, const void* label, std::uint32_t value) { + const std::uint32_t interrupt = value % 2 == 0 ? 2 : 0; + words.insert(words.end(), {0xc0064900u, 0x514u, (2u << 29u) | (interrupt << 24u), Low(label), High(label), value, 0u, 0u}); +} +void Await(const std::uint32_t* label, std::uint32_t value) { + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2); + while (*static_cast(label) != value) { + Require(std::chrono::steady_clock::now() < deadline, "submission lost its real GPU completion store"); + std::this_thread::yield(); + } + AgcDriverWaitIdle_nid_postfix(); +} +} + +int main(int argc, char**) { + void* block = nullptr; + try { + auto device = OpenVulkanTestDevice(); if (!device) return VulkanTestSkipped; device.reset(); +#ifdef _WIN32 + block = VirtualAlloc(nullptr, 65536, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); +#else + block = std::aligned_alloc(65536, 65536); +#endif + Require(block != nullptr, "cannot allocate submit-progress guest memory"); + std::memset(block, 0, 65536); + GuestAllocations::Mutation().Add(block, 65536, true, true, true); + auto* output = static_cast(block); + auto* descriptor = output + 64; auto* label = output + 128; auto* neighbor = output + 192; + descriptor[0] = Low(output); descriptor[1] = High(output) & 0xffffu; descriptor[2] = 16; descriptor[3] = 0x31016facu; + *neighbor = 0xabcdef12u; + const auto submit = [&](std::uint32_t value) { + *label = 0; label[1] = 0xfeedbeefu; + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(label), 8); + std::vector words; Dispatch(words, descriptor); + if (value % 2 != 0) { + auto* selfGate = output + 384; selfGate[0] = 0; selfGate[1] = 0; + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(selfGate), 8); + Release(words, selfGate, 1); + if (value % 3 == 0) words.insert(words.end(), {0xc0079300u, 0x13u, Low(selfGate), High(selfGate), 1, 0, 0xffffffffu, 0xffffffffu, 4}); + else words.insert(words.end(), {0xc0053c00u, 0x13u, Low(selfGate), High(selfGate), 1, 0xffffffffu, 4}); + } + Release(words, label, value); + Packet packet{words.data(), static_cast(words.size()), 0, {}}; + AgcDriver::Submit(&packet, 0); + }; + submit(1); Await(label, 1); + std::uint32_t ready = 0; + for (std::uint32_t value = 2; value < 66; ++value) { + submit(value); + ready += *static_cast(label) == value; + Await(label, value); + Require(*label == value, "submission lost its real GPU completion store"); + Require(label[1] == 0, "64-bit GPU marker did not replace its genuine CPU-written high word"); + Require(*neighbor == 0xabcdef12u, "progress wait damaged neighboring guest bytes"); + } + std::cout << ready << "/64 small dependent submissions completed at return\n"; + if (argc == 1) Require(ready >= 48, "small submissions return to guest polling before their GPU completion is available"); + std::uint32_t prefixesReady = 0; + for (std::uint32_t index = 0; index < 16; ++index) { + auto* continued = output + 320; + *continued = 0; *label = 0; + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(label), 8); + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(continued), 8); + std::vector prefix; Dispatch(prefix, descriptor); Release(prefix, label, 100 + index); + prefix.insert(prefix.end(), {0xc0005900u, 0u}); + const auto control = prefix.size() - 1; + Release(prefix, continued, 200 + index); + Packet prefixPacket{prefix.data(), static_cast(prefix.size()), 0, {}}; + AgcDriver::Submit(&prefixPacket, 0); + prefixesReady += *static_cast(label) == 100 + index; + Require(*continued == 0, "an unreleased REWIND continuation executed early"); + std::atomic logicalDone{false}, waiterFailed{false}; + std::thread waiter; + bool earlyLogicalCompletion = false; + if (index == 0) { + waiter = std::thread([&] { + try { AgcDriverWaitIdle_nid_postfix(); logicalDone.store(true, std::memory_order_release); } + catch (...) { waiterFailed.store(true, std::memory_order_release); } + }); + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + earlyLogicalCompletion = logicalDone.load(std::memory_order_acquire); + } + std::atomic_ref(prefix[control]).store(0x80000000u, std::memory_order_release); + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(&prefix[control]), 4); + Await(label, 100 + index); Await(continued, 200 + index); + if (waiter.joinable()) waiter.join(); + Require(!waiterFailed.load() && !earlyLogicalCompletion, "prefix acknowledgement completed the unreleased logical submission"); + } + std::cout << prefixesReady << "/16 finite REWIND prefixes completed at return\n"; + if (argc == 1) Require(prefixesReady >= 12, "a finite submitted prefix waits for its unreleased REWIND continuation before acknowledging progress"); + auto* gate = output + 256; *gate = 0; *label = 0; + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(gate), 4); + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(label), 4); + std::vector waiting{0xc0053c00u, 0x13u, Low(gate), High(gate), 1, 0xffffffffu, 0x19u}; + Release(waiting, label, 77); + Packet waitPacket{waiting.data(), static_cast(waiting.size()), 0, {}}; + AgcDriver::Submit(&waitPacket, 0); + Require(*label == 0, "dependent wait fabricated its completion"); + *gate = 1; + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(gate), 4); + Await(label, 77); + *label = 0; + AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(label), 4); + std::vector slow; Dispatch(slow, descriptor, 1u << 22u); Release(slow, label, 78); + Packet slowPacket{slow.data(), static_cast(slow.size()), 0, {}}; + const auto began = std::chrono::steady_clock::now(); + AgcDriver::Submit(&slowPacket, 0); + Require(std::chrono::steady_clock::now() - began < std::chrono::milliseconds(100), "progress budget became an unbounded GPU drain"); + Await(label, 78); + Require(*neighbor == 0xabcdef12u, "pending progress work damaged neighboring guest bytes"); + AgcDriverShutdown_nid_postfix(); + GuestAllocations::Mutation().Remove(block); +#ifdef _WIN32 + VirtualFree(block, 0, MEM_RELEASE); +#else + std::free(block); +#endif + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + try { AgcDriverShutdown_nid_postfix(); } catch (...) {} + return 1; + } +} diff --git a/docs/dev/debt/libSceAgcDriver.md b/docs/dev/debt/libSceAgcDriver.md index fa71f1ccbdc..a4c84117cf1 100644 --- a/docs/dev/debt/libSceAgcDriver.md +++ b/docs/dev/debt/libSceAgcDriver.md @@ -1,5 +1,11 @@ # libSceAgcDriver technical debt +### Experimental submission progress + +`APS5_SUBMIT_PROGRESS=1` enables bounded pacing for queue-zero finite prefixes ending in a decoded memory release. Unproven CP waits, flips and conditional command ranges retain normal asynchronous acceptance. For a final REWIND, the ticket acknowledges only the already-recorded prefix before waiting for the guest to release its continuation; it neither completes the logical submission nor executes the continuation early. The caller can wait for that prefix's specific recorder timeline within the remainder of a two-millisecond budget. Neither a timeout nor an unavailable timeline asserts completion, retires a resource, or changes the accepted submission. Guest sleep deadlines and the existing completion/write-back rules remain authoritative. The ticket keeps its device alive only while the call or queued commit needs it; the existing unlocked timeline-wait registration protects teardown. + +The switch remains off by default. A CP wait is eligible only when a preceding nonpredicated literal memory write covers its compared bytes and satisfies the existing PM4 comparison; unknown writes and GPU draw/dispatch work clear that local proof. Pacing does not elide the wait or replace its execution-time visibility rules. Pacing can reduce useful CPU/GPU overlap in asynchronous workloads, and Vulkan/condition-variable scheduling can return later than the requested timeout. Hardware measurements and cross-title validation are required before recommending it. Long work and genuine CPU dependencies must continue through the original path after the pacing budget expires. No whole-device wait is added. + ### Silent stubs - [sceAgcDriverRegisterDefaultOwner](../../../core/libs/prx/libSceAgcDriver/Unimplemented.cpp) (libSceAgcDriver) - takes one uint32 owner by value (OpenAGC `include/agcdriver.h`, SharpEmu `AgcExports.DriverResources.cs`) and returns 0 without recording it. PPSA21480 treats the code 0x8A6C9018 it returned before as fatal. Console behaviour is unverified: OpenAGC describes the function as a firmware 5.50 stub that returns 0x8A6C9018, SharpEmu returns 0 From 2197c5dabb0660a6f2c06397ec9bc24afcdd3ea1 Mon Sep 17 00:00:00 2001 From: Zenodeon Date: Sun, 18 Oct 2026 13:57:55 +0400 Subject: [PATCH 2/4] refactor(agc): separate submission progress helpers from flip changes --- .../src/Driver/Queues/Submission.cpp | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) 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 1ed123fb746..64539ce2826 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp @@ -1,3 +1,4 @@ +#include "prx/libSceAgcDriver/Execution/include/ProfileOutput.hpp" #include "prx/libSceAgcDriver/Execution/include/PerformanceTimer.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Queues/Submission.hpp" @@ -9,7 +10,6 @@ #include "prx/libSceAgcDriver/Execution/include/SubmissionCapture.hpp" #include "prx/libSceAgcDriver/Execution/include/ThreadPriority.hpp" #include "prx/libSceAgcDriver/Eq/include/Event.hpp" -#include "prx/libSceAgcDriver/Execution/include/ProfileOutput.hpp" #include "ThreadOwned.hpp" #include "prx/libc/include/HostThreadLocal.hpp" #include @@ -22,23 +22,6 @@ namespace AgcDriver::DriverDetail { namespace { -std::uint64_t captureTime() { - return static_cast(std::llround(TraceMs() * 1000.0)); -} - -void writeCaptured(Driver& driver, SubmissionCapture& capture, const CapturedSubmission& captured) { - try { - capture.Write(captured); - } catch (...) { - driver.ReportFailure(std::current_exception()); - throw; - } -} - -bool EopRelease(std::span words) { - return words.size() >= 8 && (words[0] >> 30u) == 3u && Pm4::PacketWords(words[0]) == 8u && ((words[0] >> 8u) & 0xffu) == 0x49u && ((words[2] >> 24u) & 7u) == 2u; -} - bool& submitsQueue0() { struct SubmitsQueue0Storage {}; return HostThreadLocal(); @@ -89,6 +72,23 @@ bool progressEligible(const Submission& submission) { return terminalRelease; } +std::uint64_t captureTime() { + return static_cast(std::llround(TraceMs() * 1000.0)); +} + +void writeCaptured(Driver& driver, SubmissionCapture& capture, const CapturedSubmission& captured) { + try { + capture.Write(captured); + } catch (...) { + driver.ReportFailure(std::current_exception()); + throw; + } +} + +bool EopRelease(std::span words) { + return words.size() >= 8 && (words[0] >> 30u) == 3u && Pm4::PacketWords(words[0]) == 8u && ((words[0] >> 8u) & 0xffu) == 0x49u && ((words[2] >> 24u) & 7u) == 2u; +} + } std::deque& Driver::releasedTails() { From 399035f544fecc2d1332eb62a911e2ce091b6691 Mon Sep 17 00:00:00 2001 From: Zenodeon Date: Mon, 19 Oct 2026 18:07:50 +0400 Subject: [PATCH 3/4] test(agc): remove scheduler-dependent submission readiness gates --- .../libSceAgcDriver/tests/execution/SubmitProgress.cpp | 4 +--- docs/dev/debt/libSceAgcDriver.md | 8 ++------ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/SubmitProgress.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/SubmitProgress.cpp index 6708ff185e0..1996782ef53 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/SubmitProgress.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/SubmitProgress.cpp @@ -44,7 +44,7 @@ void Await(const std::uint32_t* label, std::uint32_t value) { } } -int main(int argc, char**) { +int main() { void* block = nullptr; try { auto device = OpenVulkanTestDevice(); if (!device) return VulkanTestSkipped; device.reset(); @@ -86,7 +86,6 @@ int main(int argc, char**) { Require(*neighbor == 0xabcdef12u, "progress wait damaged neighboring guest bytes"); } std::cout << ready << "/64 small dependent submissions completed at return\n"; - if (argc == 1) Require(ready >= 48, "small submissions return to guest polling before their GPU completion is available"); std::uint32_t prefixesReady = 0; for (std::uint32_t index = 0; index < 16; ++index) { auto* continued = output + 320; @@ -119,7 +118,6 @@ int main(int argc, char**) { Require(!waiterFailed.load() && !earlyLogicalCompletion, "prefix acknowledgement completed the unreleased logical submission"); } std::cout << prefixesReady << "/16 finite REWIND prefixes completed at return\n"; - if (argc == 1) Require(prefixesReady >= 12, "a finite submitted prefix waits for its unreleased REWIND continuation before acknowledging progress"); auto* gate = output + 256; *gate = 0; *label = 0; AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(gate), 4); AgcDriver::GuestMemory::MarkWritten(reinterpret_cast(label), 4); diff --git a/docs/dev/debt/libSceAgcDriver.md b/docs/dev/debt/libSceAgcDriver.md index a4c84117cf1..24e61b1ea7b 100644 --- a/docs/dev/debt/libSceAgcDriver.md +++ b/docs/dev/debt/libSceAgcDriver.md @@ -1,11 +1,5 @@ # libSceAgcDriver technical debt -### Experimental submission progress - -`APS5_SUBMIT_PROGRESS=1` enables bounded pacing for queue-zero finite prefixes ending in a decoded memory release. Unproven CP waits, flips and conditional command ranges retain normal asynchronous acceptance. For a final REWIND, the ticket acknowledges only the already-recorded prefix before waiting for the guest to release its continuation; it neither completes the logical submission nor executes the continuation early. The caller can wait for that prefix's specific recorder timeline within the remainder of a two-millisecond budget. Neither a timeout nor an unavailable timeline asserts completion, retires a resource, or changes the accepted submission. Guest sleep deadlines and the existing completion/write-back rules remain authoritative. The ticket keeps its device alive only while the call or queued commit needs it; the existing unlocked timeline-wait registration protects teardown. - -The switch remains off by default. A CP wait is eligible only when a preceding nonpredicated literal memory write covers its compared bytes and satisfies the existing PM4 comparison; unknown writes and GPU draw/dispatch work clear that local proof. Pacing does not elide the wait or replace its execution-time visibility rules. Pacing can reduce useful CPU/GPU overlap in asynchronous workloads, and Vulkan/condition-variable scheduling can return later than the requested timeout. Hardware measurements and cross-title validation are required before recommending it. Long work and genuine CPU dependencies must continue through the original path after the pacing budget expires. No whole-device wait is added. - ### Silent stubs - [sceAgcDriverRegisterDefaultOwner](../../../core/libs/prx/libSceAgcDriver/Unimplemented.cpp) (libSceAgcDriver) - takes one uint32 owner by value (OpenAGC `include/agcdriver.h`, SharpEmu `AgcExports.DriverResources.cs`) and returns 0 without recording it. PPSA21480 treats the code 0x8A6C9018 it returned before as fatal. Console behaviour is unverified: OpenAGC describes the function as a firmware 5.50 stub that returns 0x8A6C9018, SharpEmu returns 0 @@ -59,6 +53,8 @@ The switch remains off by default. A CP wait is eligible only when a preceding n ### Functional +- [Experimental submission progress](../../../core/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/SubmissionProgress.hpp) (AGC driver) - the opt-in two-millisecond pacing budget is not derived from console hardware. Cross-title performance remains unvalidated; host scheduling can overshoot the timeout and pacing can reduce CPU/GPU overlap. + - [Workgroup count split](../../../core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp) (libSceAgcDriver) - a direct dispatch over `maxComputeWorkGroupCount` runs as `vkCmdDispatch` chunks, each bound to a pipeline variant whose workgroup offset specialization constants add the chunk's base to `WorkgroupId` and `GlobalInvocationId`. A variant is built when its chunk base is first recorded, not when the dispatch is prepared. A split of a shader with device-memory LDS throws: its slot base reads the chunk's `NumWorkgroups`, not the dispatch's. Chunks are ordered by a compute barrier, which the console does not have between workgroups of one dispatch. - [Texture cache budgets](../../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) (libSceAgcDriver) - the storage image cache's budget is what the reported heap budget leaves after the heap's use other than the cached storage images, the unused part of the sampled textures' 2 GiB floor and an eighth of the budget, capped at a quarter of the budget and at `TextureCacheBudget` (a quarter of the heap's size, at least 2 GiB), never below 512 MiB; the sampled cache's budget reserves `TextureCacheBudget` for the storage cache. So the sampled budget falls first while the sampled textures alive stay within their floor, and both fall together above it; each cache trims only at its next insert, a storage image is allocated before its insert reads the budget, and both budgets read the heap at most once a second, so a burst between two reads can still exceed the reported budget. The 2 GiB floor and the eighth are the sampled formula's values reused and the 512 MiB floor and the caps the earlier ones; none was measured beyond one title (PPSA11386) on one 10 GB GPU, and a title whose storage working set exceeds the shrunk budget re-creates storage images at each use. - [Polygon primitive type](../../../core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp) (AGC driver) - `VGT_PRIMITIVE_TYPE` 21 (`PrimitiveType::Polygon`) decodes to `VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN` and is treated as inactive for vertex point size, like triangle fans and strips. PPSA01317 (Gran Turismo 7) submits polygon draws; whether the console performs polygon-mode decomposition or custom vertex routing is unverified. From a2ca686a7d06d1b00fd36af1f051c1ee1e4acab3 Mon Sep 17 00:00:00 2001 From: Zenodeon Date: Mon, 19 Oct 2026 18:12:38 +0400 Subject: [PATCH 4/4] docs(agc): register the submission progress switch --- docs/dev/ENVIRONMENT.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/dev/ENVIRONMENT.md b/docs/dev/ENVIRONMENT.md index c7f4bf12e46..a11b9513678 100644 --- a/docs/dev/ENVIRONMENT.md +++ b/docs/dev/ENVIRONMENT.md @@ -363,6 +363,7 @@ The code reads environment names through `getenv`, `IsEnvironmentSet`, `kibSetti | `APS5_SUBMISSION_EPOCH_EACH` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp` | | bumps the guest-memory collect epoch on every submission instead of only when the submission is newer than the last bump | | `APS5_SUBMIT_AT_END` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp` | | submits recorded work at the end of a submission even when a worker is queued | | `APS5_SUSPEND_DRAIN` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpp` | | drains the device at a suspend point | +| `APS5_SUBMIT_PROGRESS` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp` | | enables opt-in pacing for eligible queue-zero submission prefixes within a shared nominal two-millisecond acknowledgement/timeline budget; timeout retains asynchronous acceptance | | `APS5_SYNC_COMPLETION_DRAWS` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp` | | keeps every draw with completion work synchronous | | `APS5_SYNC_DISPATCH` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp` | | waits for every dispatch instead of batching it | | `APS5_SYNC_DRAWS` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp` | | makes every draw synchronous |