[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.
This commit is contained in:
Geoffrey McRae 2021-07-20 15:16:01 +10:00
parent 3f7261d7d9
commit e6e07e8f3f
2 changed files with 18 additions and 13 deletions

View File

@ -97,17 +97,24 @@ static void lgInit(void)
static bool fpsTimerFn(void * unused) static bool fpsTimerFn(void * unused)
{ {
const float renderTime = static uint64_t last;
(float)atomic_exchange_explicit(&g_state.renderTime, 0, if (!last)
{
last = microtime();
return true;
}
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); memory_order_acquire);
const float fps = 1e9f / (renderTime / const uint64_t time = microtime();
(float)atomic_exchange_explicit(&g_state.renderCount, 0, const float elapsed = (float)(time - last) / 1e3;
memory_order_acquire)); last = time;
const float ups = 1e9f / (renderTime / const float fps = 1e3f / (elapsed / (float)renderCount);
(float)atomic_exchange_explicit(&g_state.frameCount, 0, const float ups = 1e3f / (elapsed / (float)frameCount);
memory_order_acquire));
atomic_store_explicit(&g_state.fps, fps, memory_order_relaxed); 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.ups, ups, memory_order_relaxed);
@ -179,8 +186,7 @@ static int renderThread(void * unused)
const uint64_t delta = t - g_state.lastRenderTime; const uint64_t delta = t - g_state.lastRenderTime;
g_state.lastRenderTime = t; 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) if (g_state.lastRenderTimeValid)
{ {
@ -621,7 +627,7 @@ int main_frameThread(void * unused)
if (g_state.lastFrameTimeValid) if (g_state.lastFrameTimeValid)
{ {
const float fdelta = (float)delta / 1000000.0f; const float fdelta = (float)delta / 1e6f;
ringbuffer_push(g_state.frameTimings, &fdelta); ringbuffer_push(g_state.frameTimings, &fdelta);
} }
g_state.lastFrameTimeValid = true; g_state.lastFrameTimeValid = true;

View File

@ -104,7 +104,6 @@ struct AppState
RingBuffer renderTimings; RingBuffer renderTimings;
RingBuffer frameTimings; RingBuffer frameTimings;
atomic_uint_least64_t renderTime;
atomic_uint_least64_t renderCount, frameCount; atomic_uint_least64_t renderCount, frameCount;
_Atomic(float) fps, ups; _Atomic(float) fps, ups;