From c93e5e4e465781470a0aff0d7235860ec77e88fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Thu, 13 May 2021 16:37:53 +0200 Subject: [PATCH] improve time profiling --- code/game/header/profiler.h | 1 + code/game/source/debug_ui.c | 1 + code/game/source/debug_ui_widgets.c | 9 +++++++++ code/game/source/profiler.c | 8 +++++++- 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/code/game/header/profiler.h b/code/game/header/profiler.h index 912df40..930c1a8 100644 --- a/code/game/header/profiler.h +++ b/code/game/header/profiler.h @@ -2,6 +2,7 @@ #include "system.h" typedef enum { + PROF_TOTAL_TIME, PROF_MAIN_LOOP, PROF_WORLD_WRITE, diff --git a/code/game/source/debug_ui.c b/code/game/source/debug_ui.c index 59ddf3a..b36c0ae 100644 --- a/code/game/source/debug_ui.c +++ b/code/game/source/debug_ui.c @@ -84,6 +84,7 @@ static debug_item items[] = { .list = { .items = (debug_item[]) { { .kind = DITEM_RAW, .val = PROF_MAIN_LOOP, .proc = DrawProfilerDelta }, + { .kind = DITEM_TEXT, .name = "unmesasured time", .proc = DrawUnmeasuredTime }, { .kind = DITEM_RAW, .val = PROF_WORLD_WRITE, .proc = DrawProfilerDelta }, { .kind = DITEM_RAW, .val = PROF_RENDER, .proc = DrawProfilerDelta }, { .kind = DITEM_RAW, .val = PROF_UPDATE_SYSTEMS, .proc = DrawProfilerDelta }, diff --git a/code/game/source/debug_ui_widgets.c b/code/game/source/debug_ui_widgets.c index 8e3760f..759f8fc 100644 --- a/code/game/source/debug_ui_widgets.c +++ b/code/game/source/debug_ui_widgets.c @@ -28,6 +28,15 @@ DrawColoredText(float xpos, float ypos, char const *text, Color color) { //~ NOTE(zaklaus): widgets +static inline debug_draw_result +DrawUnmeasuredTime(debug_item *it, float xpos, float ypos) { + (void)it; + float total_time = profiler_delta(PROF_TOTAL_TIME); + float acc_time = profiler_delta(PROF_MAIN_LOOP); + + return DrawFormattedText(xpos, ypos, TextFormat("%.02f ms", (total_time-acc_time) * 1000.0f)); +} + static inline debug_draw_result DrawDeltaTime(debug_item *it, float xpos, float ypos) { (void)it; diff --git a/code/game/source/profiler.c b/code/game/source/profiler.c index 59d0d52..7cc0467 100644 --- a/code/game/source/profiler.c +++ b/code/game/source/profiler.c @@ -6,6 +6,7 @@ // NOTE(zaklaus): KEEP ORDER IN SYNC WITH profiler_kind ENUM !!! static profiler profilers[] = { + { .id = PROF_TOTAL_TIME, .name = "measured time" }, { .id = PROF_MAIN_LOOP, .name = "main loop" }, { .id = PROF_WORLD_WRITE, .name = "world write" }, { .id = PROF_RENDER, .name = "render" }, @@ -33,16 +34,21 @@ void profiler_stop(profiler_kind id) { void profiler_collate() { static double frame_counter = 0.0; + static uint64_t frames = 0; frame_counter += GetFrameTime(); + frames++; if (frame_counter >= PROF_COLLATE_WINDOW) { - for (uint32_t i = 0; i < MAX_PROF; i += 1) { + profilers[PROF_TOTAL_TIME].delta_time = frame_counter / (double)frames; + + for (uint32_t i = PROF_MAIN_LOOP; i < MAX_PROF; i += 1) { profiler *p = &profilers[i]; p->delta_time = p->num_invocations == 0 ? 0.0 : p->total_time / (double)p->num_invocations; } frame_counter = 0.0; + frames = 0; } }