summaryrefslogtreecommitdiff
path: root/src/util/profiler.cpp
blob: 86c1810af79202f9bc80d0a3f94662b1241026af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#if defined(DEBUG) || defined(PROFILER)
#include "profiler.h"
#include "../render/gl_2d_font.h"
#include "../game/vars.h"
#include "string.h"

CVAR* prof_overlay = var_new( "prof_overlay", 0 );
PROFILER_GLOBAL gprof;

void __profiler_intern_draw_tree( PROFILER_LIST_ENTRY* e, GL_FONT* font, I32* x, I32* y ) {
  GL_DATA* gl = gl_instance();
  static GL_SHADER_PROGRAM** gl2d = gl->programs.where( fn( GL_SHADER_PROGRAM** p ) {
    return STR( (*p)->name ) == "2d" ;
  } );

  if( !gl2d )
    return;

  STR line;
  if( e->parent ) {
    U64 parent_dur = e->parent->duration;
    U64 percent = (F64)parent_dur / e->duration * 100.f;
    line = STR( "%s -> duration: %.2fms [%.0f%%]",
      e->name,
      (F32)e->duration / TICK_RESOLUTION * 1000.f,
      percent
    );
  } else {
    line = STR( "%s -> duration: %.2fms",
      e->name,
      (F32)e->duration / TICK_RESOLUTION * 1000.f
    );
  }

  gl_font_draw( font, *gl2d, VEC2{ (F32)*x + 1, (F32)*y + 1 }, line, CLR::BLACK() );
  gl_font_draw( font, *gl2d, VEC2{ (F32)*x, (F32)*y }, line, CLR::WHITE() );

  *y += font->size + 1;
  *x += 20;
  for( auto& it : e->children )
    __profiler_intern_draw_tree( &it, font, x, y );
  *x -= 20;
}

void __profiler_intern_draw_overlay( struct GL_FONT* f ) {
  if( !var_geti( prof_overlay ) )
    return;

  I32 x = 50, y = 50;
  for( auto& it : gprof.frames )
    __profiler_intern_draw_tree( &it, f, &x, &y );
}


#endif