Added a profiler.

This commit is contained in:
2026-08-07 12:28:18 -07:00
parent 14d87eac10
commit 47b9a9ae38
6 changed files with 207 additions and 63 deletions
+5 -2
View File
@@ -45,7 +45,7 @@ ifeq ($(OS),OS_MACOS)
-framework Metal -framework QuartzCore \ -framework Metal -framework QuartzCore \
-framework CoreHaptics -framework CoreHaptics
else ifeq ($(OS),OS_LINUX) else ifeq ($(OS),OS_LINUX)
FLAGS = -ggdb -D_POSIX_C_SOURCE=200809L -no-pie FLAGS = -ggdb -D_POSIX_C_SOURCE=200809L -no-pie -fno-omit-frame-pointer
INCLUDE = -I./libs/include/ INCLUDE = -I./libs/include/
LIBS = ./libs/lib/x86_64-linux/libSDL3.a -lm -lX11 LIBS = ./libs/lib/x86_64-linux/libSDL3.a -lm -lX11
FRAMEWORKS = FRAMEWORKS =
@@ -73,8 +73,11 @@ watch:
inotifywait -q -q -e 'modify' . && clear && make -B; \ inotifywait -q -q -e 'modify' . && clear && make -B; \
done done
perf:
perf record --call-graph dwarf ./$(EXE)
run: run:
@ ./$(EXE) @ clear; ./$(EXE)
clean: clean:
@ rm -rf $(EXE) $(EXE).dSYM libhandmade.so libhandmade.so.dSYM @ rm -rf $(EXE) $(EXE).dSYM libhandmade.so libhandmade.so.dSYM
+37 -41
View File
@@ -90,9 +90,6 @@ static void draw_rectangle(struct game_offscreen_buffer *buffer,
if(max_y > height) if(max_y > height)
max_y = height; max_y = height;
/*unsigned int color = (unsigned int)((round_float_to_int(r * 255.0f) << 16) |
(round_float_to_int(g * 255.0f) << 8) |
(round_float_to_int(b * 255.0f) << 0));*/
color = (unsigned int)((round_float_to_int(255.0f) << 24) | color = (unsigned int)((round_float_to_int(255.0f) << 24) |
(round_float_to_int(r * 255.0f) << 16) | (round_float_to_int(r * 255.0f) << 16) |
(round_float_to_int(g * 255.0f) << 8) | (round_float_to_int(g * 255.0f) << 8) |
@@ -337,19 +334,19 @@ static void move_player(struct game_state *game_state,
tile_map = game_state->world->tile_map; tile_map = game_state->world->tile_map;
if((dd_player.x != 0.0f) && (dd_player.y != 0.0f)) if((dd_player.x != 0.0f) && (dd_player.y != 0.0f))
dd_player = V2Multiply(0.7071067811865475244f, dd_player); dd_player = v2_multiply(0.7071067811865475244f, dd_player);
player_speed = 10.0f; player_speed = 10.0f;
dd_player = V2Multiply(player_speed, dd_player); dd_player = v2_multiply(player_speed, dd_player);
dd_player = V2Add(dd_player, V2Unary(V2Multiply(1.5f, entity->dp))); dd_player = v2_add(dd_player, v2_unary(v2_multiply(1.5f, entity->dp)));
old_player_p = entity->p; old_player_p = entity->p;
new_player_p = old_player_p; new_player_p = old_player_p;
player_delta = V2Add(V2Multiply(0.5f, V2Multiply(Square(dt), dd_player)), player_delta = v2_add(v2_multiply(0.5f, v2_multiply(square(dt), dd_player)),
V2Multiply(dt, entity->dp)); v2_multiply(dt, entity->dp));
new_player_p.offset = V2Add(player_delta, new_player_p.offset); new_player_p.offset = v2_add(player_delta, new_player_p.offset);
entity->dp = V2Add(V2Multiply(dt, dd_player), entity->dp); entity->dp = v2_add(v2_multiply(dt, dd_player), entity->dp);
new_player_p = recannonicalize_position(tile_map, new_player_p); new_player_p = recannonicalize_position(tile_map, new_player_p);
{ {
@@ -402,42 +399,44 @@ static void move_player(struct game_state *game_state,
r.y = -1; r.y = -1;
} }
entity->dp = V2Subtract(entity->dp, entity->dp = v2_subtract(entity->dp,
V2Multiply(1*InnerProduct(entity->dp, r), r)); v2_multiply(1*inner_product(entity->dp, r), r));
} else { } else {
entity->p = new_player_p; entity->p = new_player_p;
} }
#else #else
unsigned int MinTileX = 0; unsigned int min_tile_x = 0;
unsigned int MinTileY = 0; unsigned int min_tile_y = 0;
unsigned int OnePastMaxTileX = 0; unsigned int one_past_max_tile_x = 0;
unsigned int OnePastMaxTileY = 0; unsigned int one_past_max_tile_y = 0;
unsigned int abs_tile_z = game_state->PlayerP.abs_tile_z; unsigned int abs_tile_z = game_state->player_p.abs_tile_z;
tile_map_position BestPlayerP = game_state->PlayerP; tile_map_position best_player_p = game_state->player_p;
F32 BestDistance = LengthSq(player_delta); F32 best_distance = length_sq(player_delta);
for(unsigned int abs_tile_y = MinTileY; for(unsigned int abs_tile_y = min_tile_y;
abs_tile_y != OnePastMaxTileY; abs_tile_y != one_past_max_tile_y;
abs_tile_y++) abs_tile_y++)
{ {
for(unsigned int abs_tile_x = MinTileX; for(unsigned int abs_tile_x = min_tile_x;
abs_tile_x != OnePastMaxTileX; abs_tile_x != one_past_max_tile_x;
abs_tile_x++) abs_tile_x++)
{ {
tile_map_position TestTileP = tile_map_position test_tile_p =
CenteredTilePoint(abs_tile_x, abs_tile_y, abs_tile_z); centered_tile_point(abs_tile_x, abs_tile_y, abs_tile_z);
unsigned int TileValue = get_tile_value_by_pos(tile_map, TestTileP); unsigned int tile_value =
if(IsTileValueEmpty(TileValue)) { get_tile_value_by_pos(tile_map, test_tile_p);
v2 MinCorner = V2Multiply(-0.5f, if(is_tile_value_empty(tile_value)) {
v2 min_corner = v2_multiply(-0.5f,
(v2){ tile_map->tile_side_in_meters, (v2){ tile_map->tile_side_in_meters,
tile_map->tile_side_in_meters }); tile_map->tile_side_in_meters });
v2 MaxCorner = V2Multiply(-0.5f, v2 max_corner = v2_multiply(-0.5f,
(v2){ tile_map->tile_side_in_meters, (v2){ tile_map->tile_side_in_meters,
tile_map->tile_side_in_meters }); tile_map->tile_side_in_meters });
tile_map_difference RelNewPlayerP = tile_map_difference rel_new_player_p =
SubtractInF32(tile_map, &TestTileP, &new_player_p); subtract_in_f32(tile_map, &test_tile_p, &new_player_p);
v2 TestP = ClosestPointInRectangle(MinCorner, MaxCorner, v2 test_p = closest_point_in_rectangle(min_corner,
RelNewPlayerP); max_corner,
rel_new_player_p);
if(...) { if(...) {
} }
} }
@@ -846,10 +845,6 @@ void update_and_render(struct thread_context *thread,
} }
} }
/*draw_rectangle(buffer, 0.0f, 0.0f,
(F32)buffer->Width, (F32)buffer->Height,
0.0f, 1.0f, 0.0f);*/
draw_bitmap(buffer, &game_state->backdrop, 0.0f, 0.0f, 0, 0); draw_bitmap(buffer, &game_state->backdrop, 0.0f, 0.0f, 0, 0);
screen_center_x = 0.5f * (float)buffer->width; screen_center_x = 0.5f * (float)buffer->width;
@@ -894,8 +889,8 @@ void update_and_render(struct thread_context *thread,
Center.y = screen_center_y + meters_to_pixels * Center.y = screen_center_y + meters_to_pixels *
game_state->camera_p.offset.y - game_state->camera_p.offset.y -
(float)rel_row*(float)tile_side_in_pixels; (float)rel_row*(float)tile_side_in_pixels;
Min = V2Subtract(Center, TileSide); Min = v2_subtract(Center, TileSide);
Max = V2Add(Center, TileSide); Max = v2_add(Center, TileSide);
draw_rectangle(buffer, Min, Max, Gray, Gray, Gray); draw_rectangle(buffer, Min, Max, Gray, Gray, Gray);
} }
} }
@@ -934,10 +929,11 @@ void update_and_render(struct thread_context *thread,
player_ground_point_y - meters_to_pixels*entity->height; player_ground_point_y - meters_to_pixels*entity->height;
player_width_height.x = entity->width; player_width_height.x = entity->width;
player_width_height.x = entity->height; player_width_height.y = entity->height;
draw_rectangle(buffer, player_left_top, draw_rectangle(buffer, player_left_top,
V2Add(player_left_top, V2Multiply(meters_to_pixels, v2_add(player_left_top,
v2_multiply(meters_to_pixels,
player_width_height)), player_width_height)),
player_r, player_g, player_b); player_r, player_g, player_b);
+9 -9
View File
@@ -7,7 +7,7 @@ struct v2 {
float e[2]; float e[2];
}; };
struct v2 V2(float x, float y) struct v2 v2(float x, float y)
{ {
struct v2 result; struct v2 result;
@@ -17,7 +17,7 @@ struct v2 V2(float x, float y)
return result; return result;
} }
struct v2 V2Multiply(float a, struct v2 b) struct v2 v2_multiply(float a, struct v2 b)
{ {
struct v2 result; struct v2 result;
@@ -27,7 +27,7 @@ struct v2 V2Multiply(float a, struct v2 b)
return result; return result;
} }
struct v2 V2Unary(struct v2 a) struct v2 v2_unary(struct v2 a)
{ {
struct v2 result; struct v2 result;
@@ -37,7 +37,7 @@ struct v2 V2Unary(struct v2 a)
return result; return result;
} }
struct v2 V2Add(struct v2 a, struct v2 b) struct v2 v2_add(struct v2 a, struct v2 b)
{ {
struct v2 result; struct v2 result;
@@ -47,7 +47,7 @@ struct v2 V2Add(struct v2 a, struct v2 b)
return result; return result;
} }
struct v2 V2Subtract(struct v2 a, struct v2 b) struct v2 v2_subtract(struct v2 a, struct v2 b)
{ {
struct v2 result; struct v2 result;
@@ -57,7 +57,7 @@ struct v2 V2Subtract(struct v2 a, struct v2 b)
return result; return result;
} }
float Square(float a) float square(float a)
{ {
float result; float result;
@@ -66,15 +66,15 @@ float Square(float a)
return result; return result;
} }
float InnerProduct(struct v2 a, struct v2 b) float inner_product(struct v2 a, struct v2 b)
{ {
float result = a.x*b.x + a.y*b.y; float result = a.x*b.x + a.y*b.y;
return result; return result;
} }
float LengthSq(struct v2 a) float length_sq(struct v2 a)
{ {
float result = InnerProduct(a, a); float result = inner_product(a, a);
return result; return result;
} }
+2 -2
View File
@@ -225,9 +225,9 @@ subtract_in_float(struct tile_map *tile_map,
d_tile_z = (float)a->abs_tile_z - (float)b->abs_tile_z; d_tile_z = (float)a->abs_tile_z - (float)b->abs_tile_z;
result.d_xy = V2Add(V2Multiply(tile_map->tile_side_in_meters, result.d_xy = v2_add(v2_multiply(tile_map->tile_side_in_meters,
d_tile_xy), d_tile_xy),
V2Subtract(a->offset, b->offset)); v2_subtract(a->offset, b->offset));
/* TODO: Incomplete . . . */ /* TODO: Incomplete . . . */
result.d_z = tile_map->tile_side_in_meters*d_tile_z; result.d_z = tile_map->tile_side_in_meters*d_tile_z;
+128
View File
@@ -0,0 +1,128 @@
#ifndef PROFILER_H
#define PROFILER_H
#ifndef PROFILER
#define PROFILER 1
#endif
#if PROFILER
#include <stdlib.h> /* malloc */
#include <string.h> /* memset */
#include <time.h> /* clock_gettime */
#include <stdio.h>
unsigned long long profiler_get_time()
{
struct timespec ts;
if(clock_gettime(CLOCK_MONOTONIC_RAW, &ts) == 0) {
return (unsigned long long)ts.tv_sec * 1000000000ULL +
(unsigned long long)ts.tv_nsec;
}
return 0ULL;
}
struct profiler_block {
const char *name;
unsigned long long start;
unsigned long long end;
};
struct profiler_tree_node {
struct profiler_block data;
struct profiler_tree_node *parent; /* null for root node */
struct profiler_tree_node *prev_sibling; /* doubly-linked list */
struct profiler_tree_node *next_sibling;
int children_count;
struct profiler_tree_node *first_child;
struct profiler_tree_node *last_child;
};
struct profiler_tree_node *profiler_root_node = 0;
struct profiler_tree_node *profiler_last_node = 0;
static struct profiler_tree_node *profiler_tree_node(const char *name)
{
struct profiler_tree_node *node =
malloc(sizeof(struct profiler_tree_node));
memset(node, 0, sizeof(struct profiler_tree_node));
node->data.name = name;
return node;
}
void profiler_begin(const char *name)
{
if(!profiler_root_node) { /* create root node */
profiler_root_node = profiler_tree_node("TOTAL");
profiler_last_node = profiler_root_node;
}
struct profiler_tree_node *node = profiler_tree_node(name);
node->data.start = profiler_get_time();
node->parent = profiler_last_node;
if(!profiler_last_node->last_child) {
profiler_last_node->first_child = node;
profiler_last_node->last_child = node;
} else {
profiler_last_node->last_child->next_sibling = node;
node->prev_sibling = profiler_last_node->last_child;
profiler_last_node->last_child = node;
}
profiler_last_node->children_count++;
profiler_last_node = node;
}
static void profiler_do_print(struct profiler_tree_node *node,
int indent_level)
{
if(node) {
unsigned long long difference;
int i;
for(i = 0; i < indent_level; i++)
fprintf(stderr, " ");
difference = node->data.end - node->data.start;
fprintf(stderr, "[%s] %lld ns (%.6f ms)\n",
node->data.name,
difference,
(double)(difference) / 1000000.0);
if(node->first_child) {
profiler_do_print(node->first_child, indent_level+1);
}
if(node->next_sibling) {
profiler_do_print(node->next_sibling, indent_level);
}
}
}
void profiler_print()
{
profiler_do_print(profiler_root_node->first_child, 0);
}
void profiler_end()
{
profiler_last_node->data.end = profiler_get_time();
profiler_last_node = profiler_last_node->parent;
}
#else
#define profiler_begin(...)
#define profiler_end(...)
#define profiler_print(...)
#endif
#endif /* profiler_h */
+22 -5
View File
@@ -766,22 +766,32 @@ U64 __rdtsc()
} }
#endif #endif
#include "profiler.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct sdl_state sdl_state; struct sdl_state sdl_state;
SDL_Window *window; SDL_Window *window;
int i;
memset(&sdl_state, 0, sizeof(sdl_state)); memset(&sdl_state, 0, sizeof(sdl_state));
sdl_get_exe_dir_path(sdl_state.exe_dir_path, sizeof(sdl_state.exe_dir_path)); sdl_get_exe_dir_path(sdl_state.exe_dir_path,
sizeof(sdl_state.exe_dir_path));
sdl_get_dll_path(sdl_state.dll_path, sizeof(sdl_state.dll_path), sdl_get_dll_path(sdl_state.dll_path, sizeof(sdl_state.dll_path),
sdl_state.exe_dir_path); sdl_state.exe_dir_path);
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMEPAD)) { if(!SDL_Init(SDL_INIT_VIDEO)) {
/* TODO: This didn't work . . . */ /* TODO: This didn't work . . . */
} }
/* NOTE: It takes roughly half a second to initialize
* SDL_INIT_GAMEPAD. */
if(!SDL_InitSubSystem(SDL_INIT_GAMEPAD)) {
/* TODO: This didn't work . . . */
}
sdl_init_controllers(); sdl_init_controllers();
/* NOTE: Create hidden window so that a blank window doesn't appear /* NOTE: Create hidden window so that a blank window doesn't appear
@@ -793,6 +803,7 @@ int main(int argc, char *argv[])
SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE |
SDL_WINDOW_HIGH_PIXEL_DENSITY /*| SDL_WINDOW_HIGH_PIXEL_DENSITY /*|
SDL_WINDOW_ALWAYS_ON_TOP*/); SDL_WINDOW_ALWAYS_ON_TOP*/);
if(window) { if(window) {
const SDL_DisplayMode *display_mode; const SDL_DisplayMode *display_mode;
@@ -837,7 +848,8 @@ int main(int argc, char *argv[])
memset(&temp_context, 0, sizeof(temp_context)); memset(&temp_context, 0, sizeof(temp_context));
/* TODO: Make it a global. */ /* TODO: Make it a global. */
pointer_cursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT); pointer_cursor =
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT);
SDL_SetCursor(pointer_cursor); SDL_SetCursor(pointer_cursor);
#if !BUILD_INTERNAL #if !BUILD_INTERNAL
if(SDL_ShowCursor(SDL_DISABLE) < 0) { if(SDL_ShowCursor(SDL_DISABLE) < 0) {
@@ -872,6 +884,9 @@ int main(int argc, char *argv[])
sound_output.samples_per_second / 15; sound_output.samples_per_second / 15;
#if 0 #if 0
if(!SDL_InitSubSystem(SDL_INIT_AUDIO)) {
/* TODO: This didn't work . . . */
}
sdl_init_sound(sound_output.SamplesPerSecond, sdl_init_sound(sound_output.SamplesPerSecond,
sound_output.SamplesPerSecond * sound_output.BytesPerSample / sound_output.SamplesPerSecond * sound_output.BytesPerSample /
60); 60);
@@ -880,7 +895,8 @@ int main(int argc, char *argv[])
sound_output.Samples = calloc(sound_output.SamplesPerSecond, sound_output.Samples = calloc(sound_output.SamplesPerSecond,
sound_output.BytesPerSample); sound_output.BytesPerSample);
/*sound_output.Samples = malloc(sound_output.SecondaryBufferSize);*/ /*sound_output.Samples =
malloc(sound_output.SecondaryBufferSize);*/
/* NOTE: calloc auto clears to zero */ /* NOTE: calloc auto clears to zero */
/* sdl_clear_sound_buffer(&sound_output); */ /* sdl_clear_sound_buffer(&sound_output); */
#endif #endif
@@ -947,7 +963,8 @@ int main(int argc, char *argv[])
game = sdl_load_game_code(sdl_state.dll_path); game = sdl_load_game_code(sdl_state.dll_path);
SDL_ShowWindow(window); // Everything is ready; display the window. /* Everything is ready; display the window.*/
SDL_ShowWindow(window);
fps_last_counter = sdl_get_wall_clock(); fps_last_counter = sdl_get_wall_clock();