diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp index 63822c2643fd..ed44b12ac767 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp @@ -137,6 +137,8 @@ class Driver { static ShaderMemory::PendingWrite QueryPendingSpan(std::uint64_t address, std::size_t bytes, std::span known); std::shared_ptr RegisteredShaders(); static void SetCaptureCheckHook(void (*hook)()); + static void SetDrawEntryHook(void (*hook)()); + static void NotePendingWaitForTest(std::span packet); static void SetDispatchItemHook(void (*hook)(std::span)); private: @@ -367,9 +369,12 @@ class Driver { std::array words; std::size_t count; std::uint64_t received; + bool landed = false; }; static std::vector& pendingWaits(); - static void notePendingWait(std::span packet, std::uint32_t queue, std::uint64_t received); + static void notePendingWait(std::span packet, std::uint64_t received); + static void dropPendingWaits(std::uint64_t awaited, bool satisfiedOnly); + static bool unsatisfiedWaitPending(); void landPendingWaits(std::uint32_t queue); struct ResolvedDispatch { std::uint64_t address = 0, key = 0, indirectArguments = 0, forgetAtCapture = 0; @@ -427,8 +432,9 @@ class Driver { void recordQueuedLabelsBeforeRead(std::uint32_t queue); bool recordQueuedLabelsAfterCapture(std::uint32_t queue, std::span regions); static void runCaptureCheckHook(); + static void runDrawEntryHook(); static bool capturedCurrent(std::span captured, const ShaderSnapshot& code, const PreparedShaderState::UserDataLink* linking); - static bool capturedMemoryChanged(std::span regions, std::span compared = {}, const PreparedShaderState::UserDataLink* linking = nullptr); + static bool capturedMemoryChanged(std::span regions, std::span compared, const PreparedShaderState::UserDataLink* linking, bool required); void noteQueuedLabels(std::uint32_t queue); void recordQueuedLabelsByTry(std::uint32_t queue, std::atomic& counter); static void recordQueuedLabelsFromHook(); diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Synchronization/SynchronizationStatistics.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Synchronization/SynchronizationStatistics.hpp index 3d43fb90b1fa..4a977e11a802 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Synchronization/SynchronizationStatistics.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Synchronization/SynchronizationStatistics.hpp @@ -18,7 +18,7 @@ extern std::atomic labelFallbacks[5]; extern std::atomic queuedLabels, labelGroups, immediateLabels; -extern std::atomic packetLockRecords, packetLockSubmits, packetLockDeferred, packetSubmitDeferred, captureTryRecords, captureRetries, captureChangeRetries, suspendPoints; +extern std::atomic packetLockRecords, packetLockSubmits, packetLockDeferred, packetSubmitDeferred, captureTryRecords, captureRetries, captureChangeRetries, captureChecks, captureChecksSkipped, suspendPoints; extern std::atomic recordTriesSkipped; diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/ShaderMemory.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/ShaderMemory.hpp index 9864a554f80e..e25a14903eb7 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/include/ShaderMemory.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/ShaderMemory.hpp @@ -86,6 +86,7 @@ class ShaderMemory { static SupplyCounts Supplies(); [[nodiscard]] std::vector> SuppliedBlocks() const; [[nodiscard]] std::size_t Bytes() const; + [[nodiscard]] bool ServedKnownValues() const { return servedKnown; } struct VouchCounts { std::uint64_t calls, blocks, refused, words; }; @@ -184,6 +185,7 @@ class ShaderMemory { std::uint32_t forceSync = 0; std::uint64_t branchReadsRefused = 0; bool waitBranches = false; + bool servedKnown = false; }; // The positions, among a dispatch-cache variant's stored words, of the pure flat-SRT leaves a diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Dispatch/Dispatch.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Dispatch/Dispatch.cpp index 70dfaae7be35..36338b793bfa 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Dispatch/Dispatch.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Dispatch/Dispatch.cpp @@ -366,6 +366,7 @@ void Driver::dispatchSplitTableCall(QueueState& queue, std::span packet, const Submission& submission, std::uint64_t indirectArguments) { + const bool behindWait = unsatisfiedWaitPending(); PerformanceTimer timing("Driver.Dispatch"); const auto address = (static_cast(readRegister(queue.shader, 0x20c)) << 8u) | (static_cast(readRegister(queue.shader, 0x20d) & 0xffu) << 40u); if (SkippedShader(address) || RefusedDispatch(address)) return; @@ -570,6 +571,7 @@ void Driver::dispatch(QueueState& queue, std::span packet, std::shared_ptr shaderMemory; std::shared_ptr linking; + const auto compareRequired = [&] { return behindWait || snapshot.header.empty() || (shaderMemory != nullptr && shaderMemory->ServedKnownValues()); }; std::vector captured; std::vector liveWords; @@ -918,7 +920,7 @@ void Driver::dispatch(QueueState& queue, std::span packet, } if (pipelined) { runCaptureCheckHook(); - if (capturedMemoryChanged(captured, {}, captureLinking)) { + if (capturedMemoryChanged(captured, {}, captureLinking, compareRequired())) { const RecaptureScope recapture; dispatch(queue, packet, submission, indirectArguments); return; @@ -1044,7 +1046,7 @@ void Driver::dispatch(QueueState& queue, std::span packet, if (!captureChecked) { captureChecked = true; runCaptureCheckHook(); - if (capturedMemoryChanged(captured, snapshots, captureLinking)) { + if (capturedMemoryChanged(captured, snapshots, captureLinking, compareRequired())) { gpuLock.unlock(); const RecaptureScope recapture; dispatch(queue, packet, submission, indirectArguments); diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp index 3e8477437ce2..659b7652b1a1 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp @@ -136,6 +136,7 @@ struct DrawTail { Pm4::DrawParameters drawParameters{}; Pm4::DrawParameters resolvedParameters{}; std::vector userPatches; + bool behindWait = false; bool pipelined = false; bool traceIndirect = false; std::uint64_t dumpTarget = 0; @@ -341,6 +342,8 @@ DrawVerdict Driver::draw(QueueState& queue, std::span packe static const bool profile = std::getenv("APS5_PROFILE_DRAW") != nullptr; auto tailHolder = std::make_shared(); auto& tail = *tailHolder; + runDrawEntryHook(); + tail.behindWait = unsatisfiedWaitPending(); tail.borderColorTable = Graphics::BorderColorTableBase(queue.context, Graphics::GraphicsBorderColorBase); auto& phaseMs = tail.phaseMs; auto& captures = tail.captures; @@ -881,6 +884,9 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { auto& vertexInfos = tail.vertexInfos; auto& decodeReads = tail.decodeReads; auto& shaderMemory = tail.shaderMemory; + const auto compareRequired = [&] { + return tail.behindWait || !tail.userPatches.empty() || (shaderMemory != nullptr && shaderMemory->ServedKnownValues()) || std::any_of(tail.programs->begin(), tail.programs->end(), [](const DrawProgram& program) { return !program.nullPixel && program.binary.header.empty(); }); + }; auto& results = tail.results; auto& compiledStages = tail.compiledStages; auto& stages = tail.stages; @@ -1273,7 +1279,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { drainBeforeInline(); lockForDraw(); runCaptureCheckHook(); - if (tail.queue != nullptr && capturedMemoryChanged(memory, snapshots)) { + if (tail.queue != nullptr && capturedMemoryChanged(memory, snapshots, nullptr, compareRequired())) { gpuLock.unlock(); const RecaptureScope recapture; return draw(*tail.queue, tail.packet, submission, tail.packetOffset, rejected); @@ -1415,7 +1421,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { } if (pipelined && pipelinedIndirect && record == lastDrawable && !gpuLock.owns_lock() && deferredLabels().labels.empty()) { runCaptureCheckHook(); - if (tail.queue != nullptr && capturedMemoryChanged(memory)) { + if (tail.queue != nullptr && capturedMemoryChanged(memory, {}, nullptr, compareRequired())) { const RecaptureScope recapture; return draw(*tail.queue, tail.packet, submission, tail.packetOffset, rejected); } @@ -1440,7 +1446,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { lockForDraw(); if (!recordedOne) { runCaptureCheckHook(); - if (tail.queue != nullptr && capturedMemoryChanged(memory, snapshots)) { + if (tail.queue != nullptr && capturedMemoryChanged(memory, snapshots, nullptr, compareRequired())) { gpuLock.unlock(); const RecaptureScope recapture; return draw(*tail.queue, tail.packet, submission, tail.packetOffset, rejected); @@ -1486,7 +1492,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { } if (pipelined && (!drawParameters.indirect || pipelinedIndirect) && (tail.queue == nullptr || deferredLabels().labels.empty())) { runCaptureCheckHook(); - if (capturedMemoryChanged(memory)) { + if (capturedMemoryChanged(memory, {}, nullptr, compareRequired())) { const RecaptureScope recapture; if (tail.queue != nullptr) return draw(*tail.queue, tail.packet, submission, tail.packetOffset, rejected); restartFinishedDraw(tail); @@ -1513,7 +1519,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) { if (drawParameters.indirect) drainBeforeInline(); lockForDraw(); runCaptureCheckHook(); - if (tail.queue != nullptr && capturedMemoryChanged(memory, snapshots)) { + if (tail.queue != nullptr && capturedMemoryChanged(memory, snapshots, nullptr, compareRequired())) { gpuLock.unlock(); const RecaptureScope recapture; return draw(*tail.queue, tail.packet, submission, tail.packetOffset, rejected); diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketFlush.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketFlush.cpp index 835a66a89af0..604d4a304868 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketFlush.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketFlush.cpp @@ -105,7 +105,7 @@ void Driver::flushBetweenPackets(std::uint32_t queue, std::uint32_t header, bool lastReport = reportNow; const auto stores = Graphics::Recorder::StoreCounts(); const auto failed = [](TrySite site) { return static_cast(triesFailed[site].load()); }; - AgcDriver::ProfilePrint_nid_no_patch("[labels] batch flushes between packets: %llu at the next packet, %llu by deadline, %llu by size cap (%llu well overdue, waited for the mutex); %llu boundary reaps; record tries skipped (self-locking next) %llu; failed tries: label %llu, flush %llu, reap %llu, poll %llu, idle %llu, capture %llu, poll-record %llu; at the packet's own lock: %llu label groups (%.0f ms), %llu submits (%.0f ms incl. queue 0's reaps), left to it by a failed try: %llu groups, %llu submits; after a capture: %llu groups recorded by a try, %llu packets redone for a queued label over the capture, %llu for captured memory that changed before recording; %llu suspend points; stores: %llu in %llu runs (%llu joined, %llu replaced, %llu WAW barriers, %llu joins refused; %llu runs recorded at submit, %llu in place before a writer or reader); DCC key stores: %llu queued in %llu runs (%llu before a writer, %llu joined a queued range); queued labels: %llu noted (%llu over a recorded entry), %llu same-queue wait hits, %llu groups recorded from a poll loop, %llu from the flush hook (%llu hook accesses, %llu inside a completion skipped)\n", static_cast(packetFlushes.load()), static_cast(deadlineFlushes.load()), static_cast(capFlushes.load()), static_cast(overdueLocks.load()), static_cast(boundaryReaps.load()), static_cast(recordTriesSkipped.load()), failed(TryLabel), failed(TryFlush), failed(TryReap), failed(TryPoll), failed(TryIdle), failed(TryCapture), failed(TryPollRecord), static_cast(packetLockRecords.load()), packetLockRecordUs.load() / 1000.0, static_cast(packetLockSubmits.load()), packetLockSubmitUs.load() / 1000.0, static_cast(packetLockDeferred.load()), static_cast(packetSubmitDeferred.load()), static_cast(captureTryRecords.load()), static_cast(captureRetries.load()), static_cast(captureChangeRetries.load()), static_cast(suspendPoints.load()), static_cast(stores.stores), static_cast(stores.runs), static_cast(stores.joined), static_cast(stores.replaced), static_cast(stores.wawBarriers), static_cast(stores.joinsRefused), static_cast(stores.runsAtSubmit), static_cast(stores.runsForced), static_cast(stores.keyStores), static_cast(stores.keyStoreRuns), static_cast(stores.keyStoreRunsForWriter), static_cast(stores.keyStoresJoined), static_cast(stores.queuedNoted), static_cast(stores.queuedOverRecorded), static_cast(stores.queuedHits), static_cast(pollLabelRecords.load()), static_cast(hookLabelRecords.load()), static_cast(stores.queuedHookRecords), static_cast(stores.queuedHookInCompletion)); + AgcDriver::ProfilePrint_nid_no_patch("[labels] batch flushes between packets: %llu at the next packet, %llu by deadline, %llu by size cap (%llu well overdue, waited for the mutex); %llu boundary reaps; record tries skipped (self-locking next) %llu; failed tries: label %llu, flush %llu, reap %llu, poll %llu, idle %llu, capture %llu, poll-record %llu; at the packet's own lock: %llu label groups (%.0f ms), %llu submits (%.0f ms incl. queue 0's reaps), left to it by a failed try: %llu groups, %llu submits; after a capture: %llu groups recorded by a try, %llu packets redone for a queued label over the capture, %llu for captured memory that changed before recording; capture checks %llu run, %llu skipped (no unlanded wait); %llu suspend points; stores: %llu in %llu runs (%llu joined, %llu replaced, %llu WAW barriers, %llu joins refused; %llu runs recorded at submit, %llu in place before a writer or reader); DCC key stores: %llu queued in %llu runs (%llu before a writer, %llu joined a queued range); queued labels: %llu noted (%llu over a recorded entry), %llu same-queue wait hits, %llu groups recorded from a poll loop, %llu from the flush hook (%llu hook accesses, %llu inside a completion skipped)\n", static_cast(packetFlushes.load()), static_cast(deadlineFlushes.load()), static_cast(capFlushes.load()), static_cast(overdueLocks.load()), static_cast(boundaryReaps.load()), static_cast(recordTriesSkipped.load()), failed(TryLabel), failed(TryFlush), failed(TryReap), failed(TryPoll), failed(TryIdle), failed(TryCapture), failed(TryPollRecord), static_cast(packetLockRecords.load()), packetLockRecordUs.load() / 1000.0, static_cast(packetLockSubmits.load()), packetLockSubmitUs.load() / 1000.0, static_cast(packetLockDeferred.load()), static_cast(packetSubmitDeferred.load()), static_cast(captureTryRecords.load()), static_cast(captureRetries.load()), static_cast(captureChangeRetries.load()), static_cast(captureChecks.load()), static_cast(captureChecksSkipped.load()), static_cast(suspendPoints.load()), static_cast(stores.stores), static_cast(stores.runs), static_cast(stores.joined), static_cast(stores.replaced), static_cast(stores.wawBarriers), static_cast(stores.joinsRefused), static_cast(stores.runsAtSubmit), static_cast(stores.runsForced), static_cast(stores.keyStores), static_cast(stores.keyStoreRuns), static_cast(stores.keyStoreRunsForWriter), static_cast(stores.keyStoresJoined), static_cast(stores.queuedNoted), static_cast(stores.queuedOverRecorded), static_cast(stores.queuedHits), static_cast(pollLabelRecords.load()), static_cast(hookLabelRecords.load()), static_cast(stores.queuedHookRecords), static_cast(stores.queuedHookInCompletion)); } } diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/DeferredLabels.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/DeferredLabels.cpp index 52f057487287..6d6dfbd0d6a6 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/DeferredLabels.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/DeferredLabels.cpp @@ -203,6 +203,7 @@ bool Driver::recordQueuedLabelsAfterCapture(std::uint32_t queue, std::span captureCheckHook{nullptr}; +std::atomic drawEntryHook{nullptr}; bool& recapturing() { static thread_local bool active = false; @@ -219,8 +220,21 @@ void Driver::runCaptureCheckHook() { if (const auto hook = captureCheckHook.load(std::memory_order_acquire)) hook(); } -bool Driver::capturedMemoryChanged(std::span regions, std::span compared, const PreparedShaderState::UserDataLink* linking) { +void Driver::SetDrawEntryHook(void (*hook)()) { + drawEntryHook.store(hook, std::memory_order_release); +} + +void Driver::runDrawEntryHook() { + if (const auto hook = drawEntryHook.load(std::memory_order_acquire)) hook(); +} + +bool Driver::capturedMemoryChanged(std::span regions, std::span compared, const PreparedShaderState::UserDataLink* linking, bool required) { if (recapturing()) return false; + if (!required && !unsatisfiedWaitPending()) { + ++captureChecksSkipped; + return false; + } + ++captureChecks; const bool mark = compared.size() == regions.size(); std::vector vouched; if (linking != nullptr && vouchedValidation() && !validateLegacy()) linking->VouchRegions(regions, vouched); diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/SynchronizationStatistics.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/SynchronizationStatistics.cpp index 1b13651e3c19..dd76b3d59af5 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/SynchronizationStatistics.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/SynchronizationStatistics.cpp @@ -11,7 +11,7 @@ std::atomic labelFallbacks[5] = {}; std::atomic queuedLabels{0}, labelGroups{0}, immediateLabels{0}; -std::atomic packetLockRecords{0}, packetLockSubmits{0}, packetLockDeferred{0}, packetSubmitDeferred{0}, captureTryRecords{0}, captureRetries{0}, captureChangeRetries{0}, suspendPoints{0}; +std::atomic packetLockRecords{0}, packetLockSubmits{0}, packetLockDeferred{0}, packetSubmitDeferred{0}, captureTryRecords{0}, captureRetries{0}, captureChangeRetries{0}, captureChecks{0}, captureChecksSkipped{0}, suspendPoints{0}; std::atomic recordTriesSkipped{0}; diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/WaitMemory.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/WaitMemory.cpp index 2a0d42a851c5..be90af2673d2 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/WaitMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/WaitMemory.cpp @@ -28,6 +28,16 @@ namespace AgcDriver::DriverDetail { namespace { +std::uint64_t AwaitedAddress(std::span packet) { + const auto bytes = Pm4::WaitAwaitedBytes(packet); + return (packet[2] & (bytes == 8 ? ~7u : ~3u)) | (static_cast(packet[3]) << 32u); +} + +bool LandsWaits(std::uint32_t queue) { + static const bool allQueues = std::getenv("APS5_LAND_WAITS_ALL_QUEUES") != nullptr; + return allQueues || queue != 0; +} + using WaitExitNames = std::array, SubmissionQueueEnd>; constexpr WaitExitNames NameWaitExits(std::string_view prefix) { @@ -124,15 +134,14 @@ std::vector& Driver::pendingWaits() { return HostThreadLocal, WaitsTag>(); } -void Driver::notePendingWait(std::span packet, std::uint32_t queue, std::uint64_t received) { +void Driver::notePendingWait(std::span packet, std::uint64_t received) { if (packet.size() > std::tuple_size_v) return; - static const bool allQueues = std::getenv("APS5_LAND_WAITS_ALL_QUEUES") != nullptr; - if (!allQueues && queue == 0) return; if (Pm4::WaitSatisfiedUnchecked(packet)) return; auto& waits = pendingWaits(); for (const auto& wait : waits) { - if (wait.count == packet.size() && std::equal(packet.begin(), packet.end(), wait.words.begin())) return; + if (!wait.landed && wait.count == packet.size() && std::equal(packet.begin(), packet.end(), wait.words.begin())) return; } + dropPendingWaits(AwaitedAddress(packet), false); PendingWait wait{}; std::copy(packet.begin(), packet.end(), wait.words.begin()); wait.count = packet.size(); @@ -140,31 +149,54 @@ void Driver::notePendingWait(std::span packet, std::uint32_ waits.push_back(wait); } +void Driver::dropPendingWaits(std::uint64_t awaited, bool satisfiedOnly) { + auto& waits = pendingWaits(); + if (waits.empty()) return; + std::erase_if(waits, [awaited, satisfiedOnly](const PendingWait& wait) { + const auto packet = std::span(wait.words).first(wait.count); + return AwaitedAddress(packet) == awaited && (!satisfiedOnly || Pm4::WaitSatisfiedUnchecked(packet)); + }); +} + +bool Driver::unsatisfiedWaitPending() { + const auto& waits = pendingWaits(); + return std::any_of(waits.begin(), waits.end(), [](const PendingWait& wait) { return !Pm4::WaitSatisfiedUnchecked(std::span(wait.words).first(wait.count)); }); +} + +void Driver::NotePendingWaitForTest(std::span packet) { + notePendingWait(packet, 0); +} + void Driver::landPendingWaits(std::uint32_t queue) { auto& waits = pendingWaits(); if (waits.empty()) return; static const bool profile = std::getenv("APS5_PROFILE_DRAW") != nullptr; struct LandStats { - std::uint64_t lands = 0, waited = 0; + std::uint64_t lands = 0, waited = 0, dropped = 0; double waitedMs = 0, maxMs = 0; std::chrono::steady_clock::time_point reported = std::chrono::steady_clock::now(); }; static thread_local LandStats stats; - const PacketHistory none{}; - for (const auto& wait : waits) { - const auto packet = std::span(wait.words).first(wait.count); - ++stats.lands; - if (Pm4::WaitSatisfiedUnchecked(packet)) continue; - const auto started = std::chrono::steady_clock::now(); - waitMemory(packet, queue, none, wait.received, false, true); - const auto ms = std::chrono::duration(std::chrono::steady_clock::now() - started).count(); - ++stats.waited; - stats.waitedMs += ms; - stats.maxMs = std::max(stats.maxMs, ms); + if (LandsWaits(queue)) { + const PacketHistory none{}; + for (std::size_t index = 0; index < waits.size(); ++index) { + auto& wait = waits[index]; + if (wait.landed) continue; + wait.landed = true; + const auto packet = std::span(wait.words).first(wait.count); + ++stats.lands; + if (Pm4::WaitSatisfiedUnchecked(packet)) continue; + const auto started = std::chrono::steady_clock::now(); + waitMemory(packet, queue, none, wait.received, false, true); + const auto ms = std::chrono::duration(std::chrono::steady_clock::now() - started).count(); + ++stats.waited; + stats.waitedMs += ms; + stats.maxMs = std::max(stats.maxMs, ms); + } } - waits.clear(); + stats.dropped += std::erase_if(waits, [](const PendingWait& wait) { return Pm4::WaitSatisfiedUnchecked(std::span(wait.words).first(wait.count)); }); if (profile && std::chrono::steady_clock::now() - stats.reported > std::chrono::seconds(10)) { - AgcDriver::ProfilePrint_nid_no_patch("[landwait] queue 0x%x (10 s): %llu waits landed before a guest read, %llu had to wait for the GPU: %.1f ms in total, max %.2f ms\n", queue, static_cast(stats.lands), static_cast(stats.waited), stats.waitedMs, stats.maxMs); + AgcDriver::ProfilePrint_nid_no_patch("[landwait] queue 0x%x (10 s): %llu waits landed before a guest read, %llu had to wait for the GPU: %.1f ms in total, max %.2f ms; %llu dropped once memory satisfied them, %zu still pending\n", queue, static_cast(stats.lands), static_cast(stats.waited), stats.waitedMs, stats.maxMs, static_cast(stats.dropped), waits.size()); stats = LandStats{}; } } @@ -246,6 +278,9 @@ void Driver::waitMemory(std::span packet, std::uint32_t que ownStoreUnlanded = true; return false; }; + const auto passedByMemory = [&] { + if (queue == 0 && !requireMemory) dropPendingWaits(awaited, true); + }; const auto orderByStore = [&] { std::uint32_t writer = queue; if (!requireMemory && storedSince(packet, awaited, awaitedBytes, received, queue, &writer)) { @@ -261,16 +296,18 @@ void Driver::waitMemory(std::span packet, std::uint32_t que if (memorySatisfied()) { ++outcomes.atEntry; orderByStore(); + passedByMemory(); return; } if (heldAtSubmit) { epochPoint.inherit = false; attribution.Exit("held_at_submit"); + passedByMemory(); return; } if (std::uint32_t writer = queue; !requireMemory && storedSince(packet, awaited, awaitedBytes, received, queue, &writer)) { attribution.Exit(StoreWaitExit(writer)); - if (writer != queue) notePendingWait(packet, queue, received); + if (writer != queue) notePendingWait(packet, received); orderedBy(writer); return; } @@ -316,7 +353,7 @@ void Driver::waitMemory(std::span packet, std::uint32_t que ++outcomes.fromRecorderSameQueue; if (!hit->late) epochPoint.bump = false; } else { - notePendingWait(packet, queue, received); + notePendingWait(packet, received); if (!hit->late) epochPoint.since = storeOrderingSince(packet, awaited, awaitedBytes, received, queue); } return true; @@ -473,7 +510,7 @@ void Driver::waitMemory(std::span packet, std::uint32_t que while (!memorySatisfied()) { if (std::uint32_t writer = queue; !requireMemory && storedSince(packet, awaited, awaitedBytes, received, queue, &writer)) { attribution.Exit(StoreWaitExit(writer)); - if (writer != queue) notePendingWait(packet, queue, received); + if (writer != queue) notePendingWait(packet, received); orderedBy(writer); return; } @@ -593,6 +630,7 @@ void Driver::waitMemory(std::span packet, std::uint32_t que } ++outcomes.polled; orderByStore(); + passedByMemory(); } } diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/ShaderMemory.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/ShaderMemory.cpp index 501b3e61bae4..dadb413df962 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/ShaderMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/ShaderMemory.cpp @@ -440,6 +440,7 @@ bool ShaderMemory::read(void* context, std::uint64_t address, std::uint32_t* val if (policy == PendingWrite::KnownValue || policy == PendingWrite::VerifyKnownValue) { // The query stored the known dword in `word`. ++CaptureTotals().wordsKnown; + self.servedKnown = true; if (policy == PendingWrite::VerifyKnownValue) { std::uint32_t verifiedWord = 0; const auto waitsBefore = hookWaits(); diff --git a/core/libs/prx/libSceAgcDriver/tests/LinkedCodeDependencies.cpp b/core/libs/prx/libSceAgcDriver/tests/LinkedCodeDependencies.cpp index 2e3e94cd978f..9efef44c6c49 100644 --- a/core/libs/prx/libSceAgcDriver/tests/LinkedCodeDependencies.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/LinkedCodeDependencies.cpp @@ -83,7 +83,7 @@ struct DriverTestAccess { static bool Final(std::span captured, bool pipelined, const PreparedShaderState::UserDataLink* linking) { std::vector snapshots; if (!pipelined) for (const auto& region : captured) snapshots.push_back({region.guestAddress, region.bytes}); - const bool current = !Driver::capturedMemoryChanged(captured, snapshots, linking); + const bool current = !Driver::capturedMemoryChanged(captured, snapshots, linking, true); if (current && !pipelined && std::any_of(snapshots.begin(), snapshots.end(), [](const auto& snapshot) { return !snapshot.compared; })) throw std::runtime_error("a final proof did not mark its snapshot current"); return current; } diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/DispatchRecaptureOnChange.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/DispatchRecaptureOnChange.cpp index 74c3c0972c61..be8f849a244f 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/DispatchRecaptureOnChange.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DispatchRecaptureOnChange.cpp @@ -31,6 +31,7 @@ namespace { constexpr std::uint32_t Lanes = 32; constexpr std::uint32_t BufferBytes = Lanes * 4u; constexpr std::size_t BlockBytes = 65536; +constexpr std::size_t LabelWord = 0x400; alignas(256) constexpr std::array TableCode{0xf4080200u, 0xfa000000u, 0xbf8cc07fu, 0x34020082u, 0xe0301000u, 0x80020201u, 0xbf8c3f70u, 0x4a060481u, 0xe0701000u, 0x80010301u, 0xbf810000u}; @@ -78,9 +79,20 @@ std::array BufferDescriptor(const void* data) { const GuestBlock* Table = nullptr; const GuestBlock* Moved = nullptr; std::atomic TableChangeArmed{false}; +std::atomic WaitArmed{false}; +std::atomic SatisfyArmed{false}; std::atomic TableChanges{0}; +std::array WaitPacket{}; + +void ArmWait(const GuestBlock& block, std::uint32_t reference) { + const auto label = reinterpret_cast(block.Words() + LabelWord); + WaitPacket = {0xc0053c00u, 0x13u, static_cast(label), static_cast(label >> 32u), reference, 0xffffffffu, 10u}; + WaitArmed.store(true); +} void ChangeTableBeforeCheck() { + if (WaitArmed.exchange(false)) AgcDriver::DriverDetail::Driver::NotePendingWaitForTest(WaitPacket); + if (SatisfyArmed.exchange(false)) Table->Words()[LabelWord] = WaitPacket[4]; if (!TableChangeArmed.exchange(false)) return; const auto descriptor = BufferDescriptor(Moved->Words()); std::memcpy(Table->Words(), descriptor.data(), sizeof(descriptor)); @@ -104,10 +116,60 @@ void Dispatch(const GuestBlock& table, const GuestBlock& output) { AgcDriver::GuestMemory::FlushGpuWrites(reinterpret_cast(output.Words()), BufferBytes); } +void SubmitWait(const GuestBlock& block, std::uint32_t reference) { + const auto label = reinterpret_cast(block.Words() + LabelWord); + std::vector commands{0xc0053c00u, 0x13u, static_cast(label), static_cast(label >> 32u), reference, 0xffffffffu, 10u}; + Packet packet{commands.data(), static_cast(commands.size()), 0, {}}; + Expect(sceAgcDriverSubmitDcb(&packet) == 0, "the wait submission failed"); + AgcDriverWaitIdle_nid_postfix(); +} + std::uint32_t Lane(const GuestBlock& output, std::uint32_t lane) { return output.Words()[lane]; } +struct Counts { + std::uint64_t retries, checks, skipped; +}; + +Counts CaptureCounts() { + return {AgcDriver::DriverDetail::captureChangeRetries.load(), AgcDriver::DriverDetail::captureChecks.load(), AgcDriver::DriverDetail::captureChecksSkipped.load()}; +} + +Counts Since(const Counts& before) { + const auto now = CaptureCounts(); + return {now.retries - before.retries, now.checks - before.checks, now.skipped - before.skipped}; +} + +void ExpectSkipped(const GuestBlock& table, const GuestBlock& output, const std::string& what) { + const auto before = CaptureCounts(); + Dispatch(table, output); + const auto counts = Since(before); + std::printf("%s: lane 0 stored 0x%x, %llu capture check(s) run, %llu skipped, %llu packet(s) prepared again\n", what.c_str(), Lane(output, 0), static_cast(counts.checks), static_cast(counts.skipped), static_cast(counts.retries)); + Expect(counts.checks == 0, what + ": the dispatch compared its captured memory with no unsatisfied wait"); + Expect(counts.skipped >= 1, what + ": the dispatch did not skip its capture check"); + Expect(counts.retries == 0, what + ": a dispatch whose captured memory did not change was prepared again"); +} + +void ExpectCompared(const GuestBlock& table, const GuestBlock& output, std::uint32_t reference, const std::string& what) { + const auto before = CaptureCounts(); + ArmWait(table, reference); + Dispatch(table, output); + const auto counts = Since(before); + std::printf("%s: lane 0 stored 0x%x, %llu capture check(s) run, %llu packet(s) prepared again\n", what.c_str(), Lane(output, 0), static_cast(counts.checks), static_cast(counts.retries)); + Expect(counts.checks == 1, what + ": the dispatch ran " + std::to_string(counts.checks) + " capture checks, not one"); + Expect(counts.retries == 0, what + ": a dispatch whose captured memory did not change was prepared again"); +} + +void ExpectStillCompared(const GuestBlock& table, const GuestBlock& output, const std::string& what) { + const auto before = CaptureCounts(); + Dispatch(table, output); + const auto counts = Since(before); + std::printf("%s: lane 0 stored 0x%x, %llu capture check(s) run, %llu skipped\n", what.c_str(), Lane(output, 0), static_cast(counts.checks), static_cast(counts.skipped)); + Expect(counts.checks == 1, what + ": the dispatch ran " + std::to_string(counts.checks) + " capture checks, not one"); + Expect(counts.retries == 0, what + ": a dispatch whose captured memory did not change was prepared again"); +} + void ExpectOutput(const GuestBlock& output, std::uint32_t base, const std::string& what) { for (std::uint32_t lane = 0; lane < Lanes; ++lane) Expect(Lane(output, lane) == base + lane + 1u, what + ": lane " + std::to_string(lane) + " stored " + std::to_string(Lane(output, lane)) + ", expected " + std::to_string(base + lane + 1u)); } @@ -146,6 +208,7 @@ int main() { std::printf("the first dispatch stored 0x%x in lane 0 through the table's first V#\n", Lane(output, 0)); const auto retries = AgcDriver::DriverDetail::captureChangeRetries.load(); + ArmWait(table, 1); TableChangeArmed.store(true); Dispatch(table, output); const auto redone = AgcDriver::DriverDetail::captureChangeRetries.load() - retries; @@ -154,12 +217,41 @@ int main() { ExpectOutput(output, 0x2000u, "the dispatch whose table changed before it was recorded"); Expect(redone == 1, "the dispatch was prepared again " + std::to_string(redone) + " times, not once"); - const auto steady = AgcDriver::DriverDetail::captureChangeRetries.load(); - Dispatch(table, output); - const auto again = AgcDriver::DriverDetail::captureChangeRetries.load() - steady; - std::printf("the same dispatch with its table unchanged: lane 0 stored 0x%x, %llu packet(s) prepared again\n", Lane(output, 0), static_cast(again)); + ExpectCompared(table, output, 1, "the same dispatch behind the unsatisfied wait with its table unchanged"); + ExpectOutput(output, 0x2000u, "the dispatch behind an unsatisfied wait with an unchanged table"); + + table.Words()[LabelWord] = 1; + ExpectSkipped(table, output, "the same dispatch after memory satisfied the wait, table unchanged"); ExpectOutput(output, 0x2000u, "the dispatch with an unchanged table"); - Expect(again == 0, "a dispatch whose captured memory did not change was prepared again"); + + table.Words()[LabelWord] = 0; + ExpectCompared(table, output, 1, "a dispatch behind a wait for 1 on a label holding 0"); + ExpectCompared(table, output, 2, "a dispatch behind a later wait for 2 on the same label"); + table.Words()[LabelWord] = 2; + ExpectSkipped(table, output, "the same dispatch after memory satisfied the later wait, which replaced the earlier one"); + ExpectOutput(output, 0x2000u, "the dispatch after the later wait was satisfied"); + + table.Words()[LabelWord] = 0; + ExpectCompared(table, output, 2, "a dispatch behind a wait for 2 on a label holding 0"); + table.Words()[LabelWord] = 1; + SubmitWait(table, 1); + ExpectStillCompared(table, output, "a dispatch after memory satisfied a graphics wait for 1 on that label but not the noted wait for 2"); + ExpectOutput(output, 0x2000u, "the dispatch behind the wait for 2 memory has not satisfied"); + table.Words()[LabelWord] = 2; + ExpectSkipped(table, output, "the same dispatch after memory satisfied the wait for 2"); + + table.Words()[LabelWord] = 0; + ExpectCompared(table, output, 1, "a dispatch behind a wait for 1 on a label holding 0"); + const auto satisfiedBefore = CaptureCounts(); + Moved = &first; + SatisfyArmed.store(true); + TableChangeArmed.store(true); + Dispatch(table, output); + const auto satisfied = Since(satisfiedBefore); + std::printf("a dispatch captured behind that wait, whose table's V# moved back and whose wait memory satisfied after its capture: lane 0 stored 0x%x, %llu capture check(s) run, %llu packet(s) prepared again\n", Lane(output, 0), static_cast(satisfied.checks), static_cast(satisfied.retries)); + Expect(satisfied.checks == 1, "a dispatch captured behind an unsatisfied wait that memory satisfied before its check ran " + std::to_string(satisfied.checks) + " capture checks, not one"); + ExpectOutput(output, 0x1000u, "the dispatch whose table changed while memory satisfied the wait it was captured behind"); + Expect(satisfied.retries == 1, "the dispatch captured behind a wait memory satisfied before its check was prepared again " + std::to_string(satisfied.retries) + " times, not once"); AgcDriver::DriverDetail::Driver::SetCaptureCheckHook(nullptr); AgcDriverShutdown_nid_postfix(); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/DrawRecaptureOnChange.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/DrawRecaptureOnChange.cpp index c2ced27de4fe..403ad5ad7a72 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/DrawRecaptureOnChange.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DrawRecaptureOnChange.cpp @@ -42,6 +42,7 @@ constexpr std::size_t TableOffset = 0x300; constexpr std::size_t MovedOffset = 0x800; constexpr std::size_t ShiftOffset = 0x900; constexpr std::size_t StillOffset = 0xa00; +constexpr std::size_t LabelOffset = 0xc00; constexpr std::size_t ColorBytes = 65536; constexpr std::uint32_t RenderTargetTileMode = 0x1b; constexpr std::size_t FetchBufferTableOffset = 0x000; @@ -195,9 +196,22 @@ void WriteDescriptor(const GuestBlock& block, std::size_t slot, std::array TableChangeArmed{false}; +std::atomic WaitArmed{false}; std::atomic TableChanges{0}; +std::array WaitPacket{}; + +void ArmWait(const GuestBlock& block, std::size_t offset, std::uint32_t reference) { + const auto label = block.At(offset); + WaitPacket = {0xc0053c00u, 0x13u, static_cast(label), static_cast(label >> 32u), reference, 0xffffffffu, 10u}; + WaitArmed.store(true); +} + +void SetLabel(const GuestBlock& block, std::size_t offset, std::uint32_t value) { + std::memcpy(block.Bytes(offset), &value, sizeof(value)); +} void ChangeTableBeforeCheck() { + if (WaitArmed.exchange(false)) AgcDriver::DriverDetail::Driver::NotePendingWaitForTest(WaitPacket); if (!TableChangeArmed.exchange(false)) return; WriteDescriptor(*TableBlock, 0, VertexDescriptor(TableBlock->At(MovedOffset))); TableChanges.fetch_add(1); @@ -252,6 +266,7 @@ std::atomic FetchCheckedOnFinisher{false}; std::atomic FetchChanges{0}; void ChangeFetchBufferBeforeCheck() { + if (WaitArmed.exchange(false)) AgcDriver::DriverDetail::Driver::NotePendingWaitForTest(WaitPacket); if (!FetchChangeArmed.exchange(false)) return; FetchCheckedOnFinisher.store(AgcDriver::DriverDetail::DrawFinisher::OnFinisherThread()); WriteFetchDescriptor(*FetchBlock, 0, SlotDescriptor(FetchBlock->At(CurrentSlotsOffset))); @@ -337,24 +352,47 @@ void ChangedTableIsBound(const GuestBlock& vertices, const GuestBlock& data, con std::memset(target.Bytes(0), 0, ColorBytes); WriteDescriptor(vertices, 0, {}); const auto retries = AgcDriver::DriverDetail::captureChangeRetries.load(); + ArmWait(vertices, LabelOffset, 1); TableChangeArmed.store(true); SubmitDraw(vertices, data, target); const auto brightest = Brightest(target); const auto redone = AgcDriver::DriverDetail::captureChangeRetries.load() - retries; - std::printf("a draw whose descriptor table changed from a zero V# to a valid one after its capture: the table changed %d time(s), brightest color byte %u (%s), %llu packet(s) prepared again\n", TableChanges.load(), brightest, brightest == 255u ? "the valid V# was bound" : "the zero V# captured before the change was bound", static_cast(redone)); + std::printf("a draw behind an unsatisfied wait whose descriptor table changed from a zero V# to a valid one after its capture: the table changed %d time(s), brightest color byte %u (%s), %llu packet(s) prepared again\n", TableChanges.load(), brightest, brightest == 255u ? "the valid V# was bound" : "the zero V# captured before the change was bound", static_cast(redone)); Expect(TableChanges.load() == 1, "the check seam did not run before the draw was recorded"); Expect(brightest == 255u, "the draw bound the zero V# captured before the table changed, not the valid V# in memory when it was recorded"); Expect(redone == 1, "the draw was prepared again " + std::to_string(redone) + " times, not once"); } +void WaitedUnchangedTableIsCompared(const GuestBlock& vertices, const GuestBlock& data, const GuestBlock& target) { + std::memset(target.Bytes(0), 0, ColorBytes); + const auto retries = AgcDriver::DriverDetail::captureChangeRetries.load(); + const auto checks = AgcDriver::DriverDetail::captureChecks.load(); + ArmWait(vertices, LabelOffset, 1); + SubmitDraw(vertices, data, target); + const auto brightest = Brightest(target); + const auto redone = AgcDriver::DriverDetail::captureChangeRetries.load() - retries; + const auto compared = AgcDriver::DriverDetail::captureChecks.load() - checks; + std::printf("the same draw behind the unsatisfied wait with its table unchanged: brightest color byte %u, %llu capture check(s) run, %llu packet(s) prepared again\n", brightest, static_cast(compared), static_cast(redone)); + Expect(brightest == 255u, "the draw behind an unsatisfied wait with an unchanged valid table did not draw"); + Expect(compared == 1, "a draw behind an unsatisfied wait ran " + std::to_string(compared) + " capture checks, not one"); + Expect(redone == 0, "a draw whose captured memory did not change was prepared again"); +} + void UnchangedTableIsNotRedone(const GuestBlock& vertices, const GuestBlock& data, const GuestBlock& target) { std::memset(target.Bytes(0), 0, ColorBytes); + SetLabel(vertices, LabelOffset, 1); const auto retries = AgcDriver::DriverDetail::captureChangeRetries.load(); + const auto checks = AgcDriver::DriverDetail::captureChecks.load(); + const auto skipped = AgcDriver::DriverDetail::captureChecksSkipped.load(); SubmitDraw(vertices, data, target); const auto brightest = Brightest(target); const auto redone = AgcDriver::DriverDetail::captureChangeRetries.load() - retries; - std::printf("the same draw with its table unchanged: brightest color byte %u, %llu packet(s) prepared again\n", brightest, static_cast(redone)); + const auto compared = AgcDriver::DriverDetail::captureChecks.load() - checks; + const auto passed = AgcDriver::DriverDetail::captureChecksSkipped.load() - skipped; + std::printf("the same draw after memory satisfied the wait, table unchanged: brightest color byte %u, %llu capture check(s) run, %llu skipped, %llu packet(s) prepared again\n", brightest, static_cast(compared), static_cast(passed), static_cast(redone)); Expect(brightest == 255u, "the draw with an unchanged valid table did not draw"); + Expect(compared == 0, "a draw with no unsatisfied wait compared its captured memory"); + Expect(passed >= 1, "a draw with no unsatisfied wait did not skip its capture check"); Expect(redone == 0, "a draw whose captured memory did not change was prepared again"); } @@ -431,17 +469,54 @@ void FetchOffsetIsFoldedOnceOnRecapture(const GuestBlock& fetch, const GuestBloc WriteFetchDescriptor(fetch, 0, SlotDescriptor(fetch.At(StaleSlotsOffset))); ClearFetchOutput(output); retries = AgcDriver::DriverDetail::captureChangeRetries.load(); + ArmWait(fetch, LabelOffset, 1); FetchChangeArmed.store(true); SubmitFetchDraw(fetch, output, data, target); const auto changed = ReadFetchOutput(output); const auto redone = AgcDriver::DriverDetail::captureChangeRetries.load() - retries; - std::printf("the same draw with its vertex V# moved after its capture (checked on the %s): the V# changed %d time(s), wrote%s, %llu packet(s) prepared again\n", FetchCheckedOnFinisher.load() ? "draw finisher" : "queue worker", FetchChanges.load(), WrittenSlots(changed).c_str(), static_cast(redone)); + std::printf("the same draw behind an unsatisfied wait with its vertex V# moved after its capture (checked on the %s): the V# changed %d time(s), wrote%s, %llu packet(s) prepared again\n", FetchCheckedOnFinisher.load() ? "draw finisher" : "queue worker", FetchChanges.load(), WrittenSlots(changed).c_str(), static_cast(redone)); Expect(FetchChanges.load() == 1, "the check seam did not run before the fetching draw was recorded"); if (AgcDriver::DriverDetail::DrawFinisher::Enabled() && AgcDriver::DriverDetail::DrawPipeline::Depth() != 0) Expect(FetchCheckedOnFinisher.load(), "the fetching draw was checked on the queue worker, not the draw finisher, so restartFinishedDraw was not exercised"); Expect(changed == control, "the recaptured draw differs from the control: slots " + std::to_string(FetchVertexOffset) + " further on mean the retry folded s16 into the first vertex again, slots " + std::to_string(StaleSlotShift) + " further on mean it bound the V# captured before the change"); Expect(redone == 1, "the draw was prepared again " + std::to_string(redone) + " times, not once"); } +std::atomic EntryWaitArmed{false}; +std::atomic SatisfyArmed{false}; + +void NoteWaitOnDrawEntry() { + if (EntryWaitArmed.exchange(false)) AgcDriver::DriverDetail::Driver::NotePendingWaitForTest(WaitPacket); +} + +void SatisfyWaitAndChangeFetchBufferBeforeCheck() { + if (SatisfyArmed.exchange(false)) SetLabel(*FetchBlock, LabelOffset, WaitPacket[4]); + ChangeFetchBufferBeforeCheck(); +} + +void WorkerWaitSatisfiedBeforeCheckIsCompared(const GuestBlock& fetch, const GuestBlock& output, const GuestBlock& data, const GuestBlock& target) { + FetchBlock = &fetch; + SetLabel(fetch, LabelOffset, 0); + ArmWait(fetch, LabelOffset, 1); + WaitArmed.store(false); + EntryWaitArmed.store(true); + SatisfyArmed.store(true); + AgcDriver::DriverDetail::Driver::SetDrawEntryHook(&NoteWaitOnDrawEntry); + AgcDriver::DriverDetail::Driver::SetCaptureCheckHook(&SatisfyWaitAndChangeFetchBufferBeforeCheck); + WriteFetchDescriptor(fetch, 0, SlotDescriptor(fetch.At(StaleSlotsOffset))); + ClearFetchOutput(output); + const auto changes = FetchChanges.load(); + const auto retries = AgcDriver::DriverDetail::captureChangeRetries.load(); + FetchChangeArmed.store(true); + SubmitFetchDraw(fetch, output, data, target); + AgcDriver::DriverDetail::Driver::SetDrawEntryHook(nullptr); + const auto changed = ReadFetchOutput(output); + const auto redone = AgcDriver::DriverDetail::captureChangeRetries.load() - retries; + std::printf("a draw that starts behind a wait its queue worker remembers, with the wait satisfied and its vertex V# moved before the check (checked on the %s): wrote%s, %llu packet(s) prepared again\n", FetchCheckedOnFinisher.load() ? "draw finisher" : "queue worker", WrittenSlots(changed).c_str(), static_cast(redone)); + Expect(FetchChanges.load() == changes + 1, "the check seam did not run before the fetching draw was recorded"); + Expect(changed == FoldedOnce(), "the draw bound the V# captured before the change: the wait its queue worker held when it started did not keep the compare on once memory satisfied it"); + Expect(redone == 1, "the draw was prepared again " + std::to_string(redone) + " times, not once"); +} + } int main() { @@ -471,10 +546,12 @@ int main() { CodeBlock = &code; AgcDriver::DriverDetail::Driver::SetCaptureCheckHook(&ChangeTableBeforeCheck); ChangedTableIsBound(vertices, data, target); + WaitedUnchangedTableIsCompared(vertices, data, target); UnchangedTableIsNotRedone(vertices, data, target); AgcDriver::DriverDetail::Driver::SetCaptureCheckHook(&ChangeCodeBeforeCheck); ChangedRawCodeIsRecompiled(vertices, data, target, code); FetchOffsetIsFoldedOnceOnRecapture(fetch, output, data, target); + WorkerWaitSatisfiedBeforeCheckIsCompared(fetch, output, data, target); AgcDriver::DriverDetail::Driver::SetCaptureCheckHook(nullptr); AgcDriverShutdown_nid_postfix(); std::puts("draw recapture: a draw whose captured descriptor table or unregistered program code changed before it was recorded uses the memory in place when it is recorded, folding its vertex offset once"); diff --git a/docs/dev/debt/changes/perf-agc-recapture-gate.md b/docs/dev/debt/changes/perf-agc-recapture-gate.md new file mode 100644 index 000000000000..25872dfe2e2d --- /dev/null +++ b/docs/dev/debt/changes/perf-agc-recapture-gate.md @@ -0,0 +1,5 @@ +# libSceAgcDriver + +## change that a compute queue passes on another queue's recorded label store + +- A [WAIT_REG_MEM](../../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/WaitMemory.cpp) (libSceAgcDriver) the graphics queue passes on another queue's unexecuted label store is only remembered, not landed (landing costs PPSA01341 a quarter of its frame rate), so a fill behind it can be read early. A draw or dispatch that starts while its worker holds a remembered wait memory has not satisfied compares its captured memory with guest memory before it is recorded or queued, on the draw finisher too, and is prepared again once if they differ (`capture checks` in `[labels]`); with no such wait it compares only for a relocated draw-cache hit, a program with no registered header, or a capture served from a queued store's known values. Other draws and dispatches read a CPU write after their capture early, as does any draw a fill landing after its compare. A newly remembered wait replaces the one on its label. `APS5_LAND_WAITS_ALL_QUEUES=1` lands them too.