diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Presentation.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Presentation.cpp index edd502d77..994626128 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Presentation.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Presentation.cpp @@ -44,6 +44,7 @@ void Driver::Present(const PresentationWindow& window, const DisplayBuffer* buff try { bool submitted = false; bool presentable = false; + bool trailing = false; double waitedMs = 0; { std::unique_lock replacing(deviceReplacement, std::defer_lock); @@ -102,9 +103,14 @@ void Driver::Present(const PresentationWindow& window, const DisplayBuffer* buff presenting->QueuePresent(); timing.Mark("queue_present"); + trailing = !syncFlip; submitted = false; } } + if (trailing) { + waitedMs += presenting->FinishPresent(); + timing.Mark("render_fence_wait"); + } if (submitted) { waitedMs = presenting->FinishPresent(); timing.Mark("render_fence_wait"); diff --git a/core/libs/prx/libSceVideoOut/src/VideoOutDriver.cpp b/core/libs/prx/libSceVideoOut/src/VideoOutDriver.cpp index ee5fa36a2..b6d5471d2 100644 --- a/core/libs/prx/libSceVideoOut/src/VideoOutDriver.cpp +++ b/core/libs/prx/libSceVideoOut/src/VideoOutDriver.cpp @@ -165,10 +165,9 @@ void FlipRequest::GpuReady(const std::shared_ptr& frameT queue->changed.notify_all(); // The worker is done once the request is queued: hardware does not stall the command processor // on a flip, and the title observes completion through flipPendingNum, which processFlip drops - // once the presentation is queued behind the frame's GPU work (up to APS5_FLIP_INFLIGHT - // presentations may still be executing then, see Driver::Present). A presentation failure - // reaches the worker through ReportFailure at its next packet. Debug aid: APS5_SYNC_FLIP=1 - // parks the worker until the presenter is done, as before. + // once the frame's GPU work and its presentation have completed (see Driver::Present). A + // presentation failure reaches the worker through ReportFailure at its next packet. Debug aid: + // APS5_SYNC_FLIP=1 parks the worker until the presenter is done, as before. static const bool syncFlip = std::getenv("APS5_SYNC_FLIP") != nullptr; if (!syncFlip) return; std::unique_lock lock(cfg->mutex); diff --git a/core/libs/prx/libSceVideoOut/tests/Flip.cpp b/core/libs/prx/libSceVideoOut/tests/Flip.cpp index ba4c375fa..4cbb735c9 100644 --- a/core/libs/prx/libSceVideoOut/tests/Flip.cpp +++ b/core/libs/prx/libSceVideoOut/tests/Flip.cpp @@ -7,16 +7,21 @@ #include "prx/libSceAgcDriver/Submit/include/Dcb.hpp" #include "prx/libc/include/Shutdown.hpp" #include +#include #include +#include #include #include #include +#include #include #undef main extern "C" int APS5_VABI sceKernelCreateEqueue(KernelEqueue* eq, const char* name); extern "C" int APS5_VABI sceKernelDeleteEqueue(KernelEqueue eq); +extern "C" int APS5_VABI sceKernelAllocateDirectMemory(std::int64_t, std::int64_t, std::size_t, std::size_t, int, std::int64_t*); +extern "C" int APS5_VABI sceKernelMapDirectMemory(void**, std::size_t, int, int, std::int64_t, std::size_t); namespace { @@ -298,6 +303,87 @@ void testReleaseVblank() { LibcRunShutdown_nid_postfix(); } +std::uint32_t busyLoopResult(std::uint32_t iterations) { + std::uint32_t value = 1; + for (std::uint32_t i = 0; i < iterations; ++i) value = value * value + 1u; + return value; +} + +void testFlipAfterGpuWork() { + const int handle = sceVideoOutOpen(255, 0, 0, nullptr); + auto cfg = VideoOutDriver::Get().GetConfig(handle); + std::vector allocation(6 * 65536 + 65535); + const auto storage = alignedBuffer(allocation); + fillBuffer(storage, 259, 137); + VideoOutBuffers buffer{storage.data(), nullptr, {nullptr, nullptr}}; + VideoOutBufferAttribute2 attribute{}; + sceVideoOutSetBufferAttribute2(&attribute, 0x8000000000000000ull, 0, 259, 137, 0, 0, 0); + sceVideoOutRegisterBuffers2(handle, 0, 0, &buffer, 1, &attribute, 0, nullptr); + alignas(256) static const std::array code{ + 0xbe850380, 0x7e040281, 0xd5690002, 0x00020502, 0x4a040481, 0x80058105, + 0xbf070405, 0xbf85fffa, 0x7e020280, 0xe0701000, 0x80000201, 0xbf810000}; + Shader shader{}; + shader.file_header = 0x34333231; + shader.version = 0x18; + shader.header_size = sizeof(Shader); + shader.shader_size = sizeof(code); + shader.code = code.data(); + AgcDriverRegisterShader_nid_postfix(&shader); + std::int64_t physical = 0; + void* mapped = nullptr; + check(sceKernelAllocateDirectMemory(0, 0x7fffffffffll, 65536, 65536, 0, &physical) == 0, "direct memory allocation failed"); + check(sceKernelMapDirectMemory(&mapped, 65536, 0x33, 0, physical, 65536) == 0, "direct memory mapping failed"); + auto& result = static_cast(mapped)[0]; + auto& label = static_cast(mapped)[64]; + result = 0; + label = 0; + constexpr std::uint32_t iterations = 100000000u; + const auto program = reinterpret_cast(code.data()); + const auto output = reinterpret_cast(&result); + const auto target = reinterpret_cast(&label); + std::vector commands{ + 0xc0027600, 0x20c, static_cast(program >> 8u), static_cast(program >> 40u), + 0xc0017600, 0x213, 5u << 1u, + 0xc0037600, 0x207, 32, 1, 1, + 0xc0057600, 0x240, static_cast(output), static_cast(output >> 32u) & 0xffffu, 4, 0x31016fac, iterations, + 0xc0031500, 1, 1, 1, 0x8041, + 0xc0064900, 0x514, 1u << 29u, static_cast(target), static_cast(target >> 32u), 1, 0, 0}; + Packet packet{commands.data(), static_cast(commands.size()), 0, {}}; + check(sceVideoOutSubmitFlip(handle, 0, 1, 4) == 0, "the first flip was not accepted"); + { + std::unique_lock lock(cfg->mutex); + const bool done = cfg->vblankCond.wait_for(lock, std::chrono::seconds(30), [&] { return cfg->failure || cfg->flipStatus.count == 1; }); + if (cfg->failure) std::rethrow_exception(cfg->failure); + check(done, "the first flip did not complete"); + } + std::atomic landed{0}; + const auto start = std::chrono::steady_clock::now(); + const auto since = [&] { return std::chrono::duration_cast(std::chrono::steady_clock::now() - start).count(); }; + std::thread poller([&] { + while (label != 1 && since() < 30000000) std::this_thread::yield(); + if (label == 1) landed.store(since()); + }); + check(sceAgcDriverSubmitDcb(&packet) == 0, "the GPU work was not accepted"); + check(sceVideoOutSubmitFlip(handle, 0, 1, 5) == 0, "the flip was not accepted"); + std::int64_t flipped = 0; + { + std::unique_lock lock(cfg->mutex); + const bool done = cfg->vblankCond.wait_for(lock, std::chrono::seconds(30), [&] { return cfg->failure || cfg->flipStatus.count == 2; }); + flipped = since(); + if (cfg->failure) std::rethrow_exception(cfg->failure); + check(done, "the flip did not complete"); + } + poller.join(); + AgcDriverWaitIdle_nid_postfix(); + std::printf("GPU work landed at %.1f ms, flip completed at %.1f ms\n", landed.load() / 1000.0, flipped / 1000.0); + check(landed.load() >= 100000, "the GPU work did not land or was too short to order against the flip"); + check(result == busyLoopResult(iterations), "the GPU work stored the wrong result"); + check(flipped + 20000 >= landed.load(), "the flip completed before the GPU work submitted ahead of it"); + sceVideoOutUnregisterBuffers(handle, 0); + sceVideoOutClose(handle); + LibcRunShutdown_nid_postfix(); +} + } int main(int argc, char** argv) { @@ -307,6 +393,7 @@ int main(int argc, char** argv) { else if (argc == 2 && std::string(argv[1]) == "present") testPresentation(false); else if (argc == 2 && std::string(argv[1]) == "backtoback") testBackToBack(); else if (argc == 2 && std::string(argv[1]) == "pacing") testReleaseVblank(); + else if (argc == 2 && std::string(argv[1]) == "aftergpu") testFlipAfterGpuWork(); else if (argc == 2 && std::string(argv[1]) == "unavailable") testPresentation(true); else testLifetime(argc == 2 && std::string(argv[1]) == "reopen"); std::puts("VideoOut flip tests passed");