From e6e07e8f3f20d03d742b21bb32f55c5daf477ec0 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Tue, 20 Jul 2021 15:16:01 +1000 Subject: [PATCH] [client] main: use the monotonic clock to calculate the ups/fps The accumulated time is not the best way to do this as the timer function callback may not be exactly every 1000ms, by using the monotonic clock we will get more accurate results. --- client/src/main.c | 30 ++++++++++++++++++------------ client/src/main.h | 1 - 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/client/src/main.c b/client/src/main.c index 8c6b2f9f..4967bd73 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -97,17 +97,24 @@ static void lgInit(void) static bool fpsTimerFn(void * unused) { - const float renderTime = - (float)atomic_exchange_explicit(&g_state.renderTime, 0, - memory_order_acquire); + static uint64_t last; + if (!last) + { + last = microtime(); + return true; + } - const float fps = 1e9f / (renderTime / - (float)atomic_exchange_explicit(&g_state.renderCount, 0, - memory_order_acquire)); + const uint64_t renderCount = atomic_exchange_explicit(&g_state.renderCount, 0, + memory_order_acquire); + const uint64_t frameCount = atomic_exchange_explicit(&g_state.frameCount, 0, + memory_order_acquire); - const float ups = 1e9f / (renderTime / - (float)atomic_exchange_explicit(&g_state.frameCount, 0, - memory_order_acquire)); + const uint64_t time = microtime(); + const float elapsed = (float)(time - last) / 1e3; + last = time; + + const float fps = 1e3f / (elapsed / (float)renderCount); + const float ups = 1e3f / (elapsed / (float)frameCount); atomic_store_explicit(&g_state.fps, fps, memory_order_relaxed); atomic_store_explicit(&g_state.ups, ups, memory_order_relaxed); @@ -179,8 +186,7 @@ static int renderThread(void * unused) const uint64_t delta = t - g_state.lastRenderTime; g_state.lastRenderTime = t; - atomic_fetch_add_explicit(&g_state.renderTime , delta, memory_order_relaxed); - atomic_fetch_add_explicit(&g_state.renderCount, 1 , memory_order_relaxed); + atomic_fetch_add_explicit(&g_state.renderCount, 1, memory_order_relaxed); if (g_state.lastRenderTimeValid) { @@ -621,7 +627,7 @@ int main_frameThread(void * unused) if (g_state.lastFrameTimeValid) { - const float fdelta = (float)delta / 1000000.0f; + const float fdelta = (float)delta / 1e6f; ringbuffer_push(g_state.frameTimings, &fdelta); } g_state.lastFrameTimeValid = true; diff --git a/client/src/main.h b/client/src/main.h index 7513cf1e..914159d2 100644 --- a/client/src/main.h +++ b/client/src/main.h @@ -104,7 +104,6 @@ struct AppState RingBuffer renderTimings; RingBuffer frameTimings; - atomic_uint_least64_t renderTime; atomic_uint_least64_t renderCount, frameCount; _Atomic(float) fps, ups;