From 3c0ebd54ecad905abaab6c78e89c5aa3ad6c92f8 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Tue, 20 Jul 2021 15:37:51 +1000 Subject: [PATCH] [client] main: improve frame rate sync lock with the guest If the guest is not sending frames at a constant rate, the minimum FPS timeout may expire drawing an additional frame. This change calculates the average ups frame time over the past 100ms and adds this to the timeout value allowing this value to be dynamic. --- client/src/main.c | 23 +++++++++++++++-------- client/src/main.h | 2 +- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/client/src/main.c b/client/src/main.c index e0aedff4..d16194ad 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -100,7 +100,7 @@ static bool fpsTimerFn(void * unused) static uint64_t last; if (!last) { - last = microtime(); + last = nanotime(); return true; } @@ -109,15 +109,19 @@ static bool fpsTimerFn(void * unused) const uint64_t frameCount = atomic_exchange_explicit(&g_state.frameCount, 0, memory_order_acquire); - const uint64_t time = microtime(); - const float elapsed = (float)(time - last) / 1e3; + const uint64_t time = nanotime(); + const float elapsedNs = time - last; last = time; - const float fps = 1e3f / (elapsed / (float)renderCount); - const float ups = 1e3f / (elapsed / (float)frameCount); + const float elapsedMs = (float)elapsedNs / 1e6; - atomic_store_explicit(&g_state.fps, fps, memory_order_relaxed); - atomic_store_explicit(&g_state.ups, ups, memory_order_relaxed); + const float fps = 1e3f / (elapsedMs / (float)renderCount); + const float ups = 1e3f / (elapsedMs / (float)frameCount); + const uint64_t upsTime = elapsedNs / frameCount; + + atomic_store_explicit(&g_state.fps , fps , memory_order_relaxed); + atomic_store_explicit(&g_state.ups , ups , memory_order_relaxed); + atomic_store_explicit(&g_state.upsTime, upsTime, memory_order_relaxed); return true; } @@ -153,9 +157,12 @@ static int renderThread(void * unused) { if (g_params.fpsMin != 0) { + const uint64_t upsTime = atomic_load_explicit(&g_state.upsTime, + memory_order_relaxed); + lgWaitEventAbs(e_frame, &time); clock_gettime(CLOCK_MONOTONIC, &time); - tsAdd(&time, g_state.frameTime); + tsAdd(&time, g_state.frameTime + upsTime); } int resize = atomic_load(&g_state.lgrResize); diff --git a/client/src/main.h b/client/src/main.h index 914159d2..8b6005a6 100644 --- a/client/src/main.h +++ b/client/src/main.h @@ -104,7 +104,7 @@ struct AppState RingBuffer renderTimings; RingBuffer frameTimings; - atomic_uint_least64_t renderCount, frameCount; + atomic_uint_least64_t renderCount, frameCount, upsTime; _Atomic(float) fps, ups; uint64_t resizeTimeout;