mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
fix(agc): complete a flip only once the frame's GPU work has finished
With APS5_FLIP_INFLIGHT (default 1) a presentation trailed on the GPU and the flip completed as soon as its blit was queued behind the frame's batches. The title reads that completion as "the GPU is done with the frame" and rewrites the frame's memory, but the recorded GPU work still reads guest memory in place when it runs. In PPSA21564 the per-draw constant ring of the next frames landed under vertex programs that had not run yet: a program read an integer sample count as float 1.0 from another draw and stepped a loop by its reciprocal, about 1e9 iterations, and other draws got wrong matrices. The presenter now queues the present and then waits for the frame's own fence, outside the GPU mutex, before reporting the flip, so the title cannot reuse the frame's memory while the GPU still reads it. Test: video_out_flip_tests aftergpu (needs a GPU and a display) submits a busy compute dispatch, an end-of-pipe label without an interrupt (which does not drain the device) and a flip, and checks that the flip completes no earlier than the label lands.
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -165,10 +165,9 @@ void FlipRequest::GpuReady(const std::shared_ptr<AgcDriver::FrameTiming>& 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);
|
||||
|
||||
@@ -7,16 +7,21 @@
|
||||
#include "prx/libSceAgcDriver/Submit/include/Dcb.hpp"
|
||||
#include "prx/libc/include/Shutdown.hpp"
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <limits>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#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<std::byte> 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<std::uint32_t, 12> 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<volatile std::uint32_t*>(mapped)[0];
|
||||
auto& label = static_cast<volatile std::uint32_t*>(mapped)[64];
|
||||
result = 0;
|
||||
label = 0;
|
||||
constexpr std::uint32_t iterations = 100000000u;
|
||||
const auto program = reinterpret_cast<std::uintptr_t>(code.data());
|
||||
const auto output = reinterpret_cast<std::uintptr_t>(&result);
|
||||
const auto target = reinterpret_cast<std::uintptr_t>(&label);
|
||||
std::vector<std::uint32_t> commands{
|
||||
0xc0027600, 0x20c, static_cast<std::uint32_t>(program >> 8u), static_cast<std::uint32_t>(program >> 40u),
|
||||
0xc0017600, 0x213, 5u << 1u,
|
||||
0xc0037600, 0x207, 32, 1, 1,
|
||||
0xc0057600, 0x240, static_cast<std::uint32_t>(output), static_cast<std::uint32_t>(output >> 32u) & 0xffffu, 4, 0x31016fac, iterations,
|
||||
0xc0031500, 1, 1, 1, 0x8041,
|
||||
0xc0064900, 0x514, 1u << 29u, static_cast<std::uint32_t>(target), static_cast<std::uint32_t>(target >> 32u), 1, 0, 0};
|
||||
Packet packet{commands.data(), static_cast<std::uint32_t>(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<std::int64_t> landed{0};
|
||||
const auto start = std::chrono::steady_clock::now();
|
||||
const auto since = [&] { return std::chrono::duration_cast<std::chrono::microseconds>(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");
|
||||
|
||||
Reference in New Issue
Block a user