2021-05-13 12:17:44 +00:00
|
|
|
#include "profiler.h"
|
|
|
|
#include "raylib.h"
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#define PROF_COLLATE_WINDOW 0.5
|
|
|
|
|
|
|
|
// NOTE(zaklaus): KEEP ORDER IN SYNC WITH profiler_kind ENUM !!!
|
|
|
|
static profiler profilers[] = {
|
2021-05-13 14:37:53 +00:00
|
|
|
{ .id = PROF_TOTAL_TIME, .name = "measured time" },
|
2021-05-13 14:15:21 +00:00
|
|
|
{ .id = PROF_MAIN_LOOP, .name = "main loop" },
|
2021-05-13 12:17:44 +00:00
|
|
|
{ .id = PROF_WORLD_WRITE, .name = "world write" },
|
|
|
|
{ .id = PROF_RENDER, .name = "render" },
|
2021-05-13 13:44:08 +00:00
|
|
|
{ .id = PROF_UPDATE_SYSTEMS, .name = "update systems" },
|
2021-05-13 12:17:44 +00:00
|
|
|
{ .id = PROF_ENTITY_LERP, .name = "entity lerp" },
|
2021-05-13 13:44:08 +00:00
|
|
|
{ .id = PROF_ENTITY_REMOVAL, .name = "entity removal" },
|
2021-07-26 15:53:18 +00:00
|
|
|
{ .id = PROF_INTEGRATE_POS, .name = "entity movement" },
|
2021-05-13 12:17:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static_assert((sizeof(profilers)/sizeof(profilers[0])) == MAX_PROF, "mismatched profilers");
|
|
|
|
|
|
|
|
void profiler_reset(profiler_kind id) {
|
|
|
|
profilers[id].num_invocations = 0;
|
|
|
|
profilers[id].total_time = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void profiler_start(profiler_kind id) {
|
|
|
|
profilers[id].start_time = GetTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
void profiler_stop(profiler_kind id) {
|
|
|
|
profilers[id].num_invocations += 1;
|
|
|
|
profilers[id].total_time += GetTime() - profilers[id].start_time;
|
|
|
|
profilers[id].start_time = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void profiler_collate() {
|
|
|
|
static double frame_counter = 0.0;
|
2021-05-13 14:37:53 +00:00
|
|
|
static uint64_t frames = 0;
|
2021-05-13 12:17:44 +00:00
|
|
|
|
|
|
|
frame_counter += GetFrameTime();
|
2021-05-13 14:37:53 +00:00
|
|
|
frames++;
|
2021-05-13 12:17:44 +00:00
|
|
|
|
|
|
|
if (frame_counter >= PROF_COLLATE_WINDOW) {
|
2021-05-13 14:37:53 +00:00
|
|
|
profilers[PROF_TOTAL_TIME].delta_time = frame_counter / (double)frames;
|
|
|
|
|
|
|
|
for (uint32_t i = PROF_MAIN_LOOP; i < MAX_PROF; i += 1) {
|
2021-05-13 12:17:44 +00:00
|
|
|
profiler *p = &profilers[i];
|
|
|
|
p->delta_time = p->num_invocations == 0 ? 0.0 : p->total_time / (double)p->num_invocations;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame_counter = 0.0;
|
2021-05-13 14:37:53 +00:00
|
|
|
frames = 0;
|
2021-05-13 12:17:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double profiler_delta(profiler_kind id) {
|
|
|
|
return profilers[id].delta_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const *profiler_name(profiler_kind id) {
|
|
|
|
return profilers[id].name;
|
|
|
|
}
|