From 99afafd7cf5dc6c60985855684bba5a2ce27e626 Mon Sep 17 00:00:00 2001 From: Eurya0x13 Date: Thu, 26 Nov 2026 14:14:38 +0800 Subject: [PATCH] [Perf] (MG_Backend/DirectGLES): reuse the frame fence wait result - Retire the already signaled slot without querying its status again. - Keep polling the other slots to preserve the completion watermark. --- .../MG_Backend/DirectGLES/DirectGLESSync.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/MobileGL/MG_Backend/DirectGLES/DirectGLESSync.cpp b/MobileGL/MG_Backend/DirectGLES/DirectGLESSync.cpp index 560efb8c..1300d8d3 100644 --- a/MobileGL/MG_Backend/DirectGLES/DirectGLESSync.cpp +++ b/MobileGL/MG_Backend/DirectGLES/DirectGLESSync.cpp @@ -450,7 +450,7 @@ namespace MobileGL::MG_Backend::DirectGLES { // and the serials are handed out to every native context of every session in turn, so the // highest signaled serial says nothing about an older fence of another context still // pending. A serial is complete when no live fence at or below it is pending. - void PollFrameFences() { + void PollFrameFences(const FrameFence* signaled = nullptr) { if (!g_GLESFuncs.glGetSynciv) return; Uint64 lowestPending = ~Uint64{0}; for (FrameFence& slot : g_frameFenceRing) { @@ -460,8 +460,10 @@ namespace MobileGL::MG_Backend::DirectGLES { continue; } GLint status = GL_SIGNALED; - GLsizei length = 0; - g_GLESFuncs.glGetSynciv(slot.sync, GL_SYNC_STATUS, 1, &length, &status); + if (&slot != signaled) { + GLsizei length = 0; + g_GLESFuncs.glGetSynciv(slot.sync, GL_SYNC_STATUS, 1, &length, &status); + } if (status == GL_SIGNALED) { if (g_GLESFuncs.glDeleteSync) g_GLESFuncs.glDeleteSync(slot.sync); slot.sync = nullptr; @@ -654,6 +656,7 @@ namespace MobileGL::MG_Backend::DirectGLES { // the same bound on the ES driver: after presenting S, wait for S+1-N to complete. The // platform's own queueBuffer throttle already waits for S-1, so only N = 1 is stricter // than what Android does by itself; larger N is the same contract stated, not a change. + const FrameFence* signaled = nullptr; const Uint32 framesInFlight = MG_Config::Features.FramesInFlight; if (presentedSerial != 0 && framesInFlight != 0 && framesInFlight < static_cast(kFrameFenceRingDepth) && g_GLESFuncs.glClientWaitSync && presentedSerial + 1 > framesInFlight) { @@ -661,11 +664,15 @@ namespace MobileGL::MG_Backend::DirectGLES { const FrameFence& oldest = g_frameFenceRing[target % kFrameFenceRingDepth]; if (oldest.sync && oldest.serial == target && oldest.contextGeneration == g_syncContextGeneration) { constexpr Uint64 kFramesInFlightWaitNs = 1000ull * 1000 * 1000; - (void)g_GLESFuncs.glClientWaitSync(oldest.sync, GL_SYNC_FLUSH_COMMANDS_BIT, kFramesInFlightWaitNs); + const GLenum status = + g_GLESFuncs.glClientWaitSync(oldest.sync, GL_SYNC_FLUSH_COMMANDS_BIT, kFramesInFlightWaitNs); + if (status == GL_ALREADY_SIGNALED || status == GL_CONDITION_SATISFIED) { + signaled = &oldest; + } } } - if (canFence) PollFrameFences(); + if (canFence) PollFrameFences(signaled); // After the watermark advanced: retire grown-away ring stores and record the // frame's ring high-water marks for slot reclamation.