imgui stuff
[vg.git] / vg_profiler.h
1 #ifndef VG_PROFILER_H
2 #define VG_PROFILER_H
3
4 #define VG_GAME
5 #include "vg.h"
6 #include "vg_platform.h"
7
8 #define VG_PROFILE_SAMPLE_COUNT 128
9
10 static int vg_profiler = 0;
11
12 struct vg_profile
13 {
14 const char *name;
15
16 u64 samples[ VG_PROFILE_SAMPLE_COUNT ];
17 u32 buffer_count, buffer_current;
18
19 enum profile_mode
20 {
21 k_profile_mode_frame,
22 k_profile_mode_accum
23 }
24 mode;
25
26 u64 start;
27 };
28
29 VG_STATIC void vg_profile_begin( struct vg_profile *profile )
30 {
31 profile->start = SDL_GetPerformanceCounter();
32 }
33
34 VG_STATIC void vg_profile_increment( struct vg_profile *profile )
35 {
36 profile->buffer_current ++;
37
38 if( profile->buffer_count < VG_PROFILE_SAMPLE_COUNT )
39 profile->buffer_count ++;
40
41 if( profile->buffer_current >= VG_PROFILE_SAMPLE_COUNT )
42 profile->buffer_current = 0;
43
44 profile->samples[ profile->buffer_current ] = 0;
45 }
46
47 VG_STATIC void vg_profile_end( struct vg_profile *profile )
48 {
49 u64 time_end = SDL_GetPerformanceCounter(),
50 delta = time_end - profile->start;
51
52 if( profile->mode == k_profile_mode_frame ){
53 profile->samples[ profile->buffer_current ] = delta;
54 vg_profile_increment( profile );
55 }
56 else{
57 profile->samples[ profile->buffer_current ] += delta;
58 }
59 }
60
61 #if 0
62 VG_STATIC void vg_profile_drawn( struct vg_profile **profiles, u32 count,
63 float budget, ui_rect panel, u32 colour_offset )
64 {
65 if( !vg_profiler )
66 return;
67
68 if( panel[2] == 0 )
69 panel[2] = 256;
70
71 if( panel[3] == 0 )
72 panel[3] = VG_PROFILE_SAMPLE_COUNT * 2;
73
74 float sh = panel[3] / VG_PROFILE_SAMPLE_COUNT,
75 sw = panel[2];
76
77 ui_fill_rect( panel, 0xa0000000 );
78
79 assert( count <= 8 );
80 double avgs[8];
81 int ptrs[8];
82
83 for( int i=0; i<count; i++ ){
84 ptrs[i] = profiles[i]->buffer_current;
85 avgs[i] = 0.0f;
86 }
87
88 u32 colours[] = { 0xff0000ff, 0xff00ff00, 0xff00ffff, 0xffff0000,
89 0xffff00ff, 0xffffff00 };
90
91 double rate_mul = 1000.0 / (double)SDL_GetPerformanceFrequency();
92
93 for( int i=0; i<VG_PROFILE_SAMPLE_COUNT-1; i++ ){
94 double total = 0.0;
95
96 for( int j=0; j<count; j++ ){
97 ptrs[j] --;
98
99 if( ptrs[j] < 0 )
100 ptrs[j] = VG_PROFILE_SAMPLE_COUNT-1;
101
102 double sample = (double)profiles[j]->samples[ptrs[j]] * rate_mul,
103 px = (total / (budget)) * sw,
104 wx = (sample / (budget)) * sw;
105
106 ui_rect block = { panel[0] + px, panel[1] + (float)i*sh,
107 wx, sh };
108
109 u32 colour = colours[ (j+colour_offset) % vg_list_size(colours) ];
110 ui_fill_rect( block, colour );
111
112 total += sample;
113 avgs[j] += sample;
114 }
115 }
116
117 char infbuf[64];
118
119 snprintf( infbuf, 64, "accuracy: %.7fms", rate_mul );
120 ui_text( (ui_rect){ panel[0] + 4,
121 panel[1] + 0 * 14, 0, 0 },
122 infbuf,
123 1,
124 k_text_align_left );
125
126 for( int i=0; i<count; i++ ){
127 snprintf( infbuf, 64, "%.4fms %s",
128 avgs[i] * (1.0f/(VG_PROFILE_SAMPLE_COUNT-1)),
129 profiles[i]->name );
130
131 ui_text( (ui_rect){ panel[0] + panel[2] + 4,
132 panel[1] + i * 14, 0, 0 },
133 infbuf,
134 1,
135 k_text_align_left );
136 }
137 }
138 #endif
139
140 VG_STATIC void vg_profiler_init(void)
141 {
142 VG_VAR_I32( vg_profiler, flags=VG_VAR_PERSISTENT );
143 }
144
145 #endif /* VG_PROFILER_H */