Skip to content

Instantly share code, notes, and snippets.

@dbechrd
Created July 4, 2026 01:01
Show Gist options
  • Select an option

  • Save dbechrd/b7848ecefff5086d528af0a2c34394df to your computer and use it in GitHub Desktop.

Select an option

Save dbechrd/b7848ecefff5086d528af0a2c34394df to your computer and use it in GitHub Desktop.
iprof impl
// NOTE(perf): This costs ~30k vertices.. oof.
SHOW_GRAPH :: false;
update_iprof :: () {
// TODO: Draw a "profiling..." thingy somewhere on screen (record icon in corner?)
// TODO: Disable all other keymaps while the profiler is open
if is_key_down_start(.F9) {
if is_key_down(.SHIFT) {
__Iprof.paused = false;
profile_started_at = now;
profiler_open = false;
} else if is_key_down(.CTRL) {
profiler_open = !profiler_open;
__Iprof.paused = !profiler_open;
}
}
if profile_started_at && profile_started_at + (127 * frame_dt * 1.1) < now {
__Iprof.paused = true;
profiler_open = true;
profile_started_at = 0;
}
// requires `-plug Iprof`
__Iprof.set_cursor_screen_coordinates(xx mouse_position.x, xx (window_size.y - mouse_position.y));
if is_key_down_start(.MOUSE_BUTTON_LEFT) {
__Iprof.graph_select_sample();
}
if is_key_down_start(#char "1") {
__Iprof.set_report_mode(.SELF_TIME);
}
if is_key_down_start(#char "2") {
__Iprof.set_report_mode(.HIERARCHICAL_TIME);
}
if is_key_down_start(#char "3") {
__Iprof.set_report_mode(.CALL_COUNT);
}
// I don't think you're supposed to use this as a hotkey
//if is_key_down_start(#char "4") {
// __Iprof.set_report_mode(.CALL_GRAPH);
//}
if is_key_down_start(.ARROW_UP) {
__Iprof.move_cursor(-1);
}
if is_key_down_start(.ARROW_DOWN) {
__Iprof.move_cursor(1);
}
if is_key_down_start(.ARROW_LEFT) {
__Iprof.move_frame(-1);
}
if is_key_down_start(.ARROW_RIGHT) {
__Iprof.move_frame(1);
}
if is_key_down_start(.ENTER) {
__Iprof.select();
}
if is_key_down_start(.BACKSPACE) {
__Iprof.select_parent();
}
if !__Iprof.paused {
__Iprof.update(true);
}
} @NoProfile
draw_iprof :: () {
if !__Iprof.paused {
radius := 8.0;
pos := Vector2.{window_size.x - 8.0 - radius*2, 8};
color := color_flash(Red, 0.5);
draw_circle(pos + .{radius, radius}, radius, color, segments=32);
}
if !profiler_open {
return;
}
draw_text :: (x: float, y: float, str: string, color: Vector4) {
set_shader_for_text();
yy := y;
if profiler_is_graph {
yy = window_size.y-y;
}
draw_text_shadowed(fnt_fixed_small, xx x, xx yy-4, str, color);
} @NoProfile
text_width :: (s: string) -> float {
return xx prepare_text(fnt_fixed_small, s);
} @NoProfile
draw_rectangle :: (x0: float, y0: float, x1: float, y1: float, color: Vector4) {
set_shader_for_color(true);
yy0, yy1 := y1, y0;
if profiler_is_graph {
yy0 = window_size.y-yy0;
yy1 = window_size.y-yy1;
}
draw_rect(x0, yy0, x1-x0, abs(yy1-yy0), color);
} @NoProfile
draw_line :: (p0: Vector2, p1: Vector2, p2: Vector2, p3: Vector2, color: Vector4) {
set_shader_for_color(true);
pp0 := p0; //Vector2.{p0.x, window_size.y-p0.y};
pp1 := p1; //Vector2.{p1.x, window_size.y-p1.y};
pp2 := p2; //Vector2.{p2.x, window_size.y-p2.y};
pp3 := p3; //Vector2.{p3.x, window_size.y-p3.y};
if profiler_is_graph {
pp0 = Vector2.{p0.x, window_size.y-p0.y};
pp1 = Vector2.{p1.x, window_size.y-p1.y};
pp2 = Vector2.{p2.x, window_size.y-p2.y};
pp3 = Vector2.{p3.x, window_size.y-p3.y};
}
immediate_quad(pp0, pp1, pp2, pp3, color);
} @NoProfile
graph_begin :: (x: float, y: float, width: float, height: float) {
} @NoProfile
graph_end :: (x: float, y: float, width: float, height: float) {
} @NoProfile
config := __Iprof.Config.{
line_spacing = xx (fnt_fixed_small.default_line_spacing * 1.2),
font_character_height = xx fnt_fixed_small.character_height,
draw_text = draw_text,
text_width = text_width,
draw_rectangle = draw_rectangle,
draw_line = draw_line,
graph_begin = graph_begin,
graph_end = graph_end,
};
// draw_rect(0, 0, xx window_size.x, xx window_size.y, Gray);
//----------------------------------------------------------------------------------------
// THE FUCKING LIST IS IN LEFT-HANDED coords from top-left corner
// THE FUCKING GRAPH IS IN RIGHT_HANDED coords from bottom-left corner
// WHYYYYYYYYYY JAIIIIIIIIIIIIIIIIIIIIIII!?!?!?!?
//----------------------------------------------------------------------------------------
width := window_size.y * 0.5;
graph_height := window_size.y * 0.05;
list_height := window_size.y * 0.5; //0.9;
#if 0 {
// full screen; graph left half and table right half
kx := window_size.x / 53.0;
ky := window_size.y / 54.0;
// profiler_is_graph = false;
__Iprof.draw(kx*27, ky*2, kx*25, ky*50, *config);
profiler_is_graph = true;
#if SHOW_GRAPH {
__Iprof.draw_graph(kx, ky*52, kx*25, ky*50, *config);
}
} else if 0 {
// top left of screen, graph on top of list
// profiler_is_graph = false;
__Iprof.draw(8, 8+graph_height+32, width, list_height, *config);
profiler_is_graph = true;
#if SHOW_GRAPH {
__Iprof.draw_graph(8, window_size.y-8.0, width, graph_height, *config);
}
} else if 0 {
// top right of screen, graph on top of list
// profiler_is_graph = false;
__Iprof.draw(window_size.x-8-width, 8+graph_height+32, width, list_height, *config);
profiler_is_graph = true;
#if SHOW_GRAPH {
__Iprof.draw_graph(window_size.x-8-width, window_size.y-8.0, width, graph_height, *config);
}
} else {
// bottom left of screen, graph on top of list
// profiler_is_graph = false;
__Iprof.draw(8, window_size.y-16-list_height, width, list_height, *config);
profiler_is_graph = true;
#if SHOW_GRAPH {
__Iprof.draw_graph(8, 16.0+list_height+32+graph_height, width, graph_height, *config);
}
}
} @NoProfile
#scope_file
profiler_open := false;
profile_started_at: float64;
profiler_is_graph: bool;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment