replace VG_STATIC -> static
[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 static void vg_profile_begin( struct vg_profile *profile )
30 {
31 profile->start = SDL_GetPerformanceCounter();
32 }
33
34 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 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 static void vg_profile_drawn( struct vg_profile **profiles, u32 count,
62 float budget, ui_rect panel, u32 colour_offset )
63 {
64 if( !vg_profiler )
65 return;
66
67 if( panel[2] == 0 )
68 panel[2] = 256;
69
70 if( panel[3] == 0 )
71 panel[3] = VG_PROFILE_SAMPLE_COUNT * 2;
72
73 float sh = panel[3] / VG_PROFILE_SAMPLE_COUNT,
74 sw = panel[2];
75
76 ui_fill( panel, 0xa0000000 );
77
78 assert( count <= 8 );
79 double avgs[8];
80 int ptrs[8];
81
82 for( int i=0; i<count; i++ ){
83 ptrs[i] = profiles[i]->buffer_current;
84 avgs[i] = 0.0f;
85 }
86
87 u32 colours[] = { 0xff0000ff, 0xff00ff00, 0xff00ffff, 0xffff0000,
88 0xffff00ff, 0xffffff00 };
89
90 double rate_mul = 1000.0 / (double)SDL_GetPerformanceFrequency();
91
92 for( int i=0; i<VG_PROFILE_SAMPLE_COUNT-1; i++ ){
93 double total = 0.0;
94
95 for( int j=0; j<count; j++ ){
96 ptrs[j] --;
97
98 if( ptrs[j] < 0 )
99 ptrs[j] = VG_PROFILE_SAMPLE_COUNT-1;
100
101 double sample = (double)profiles[j]->samples[ptrs[j]] * rate_mul,
102 px = (total / (budget)) * sw,
103 wx = (sample / (budget)) * sw;
104
105 ui_rect block = { panel[0] + px, panel[1] + (float)i*sh,
106 wx, sh };
107
108 u32 colour = colours[ (j+colour_offset) % vg_list_size(colours) ];
109 ui_fill( block, colour );
110
111 total += sample;
112 avgs[j] += sample;
113 }
114 }
115
116 char infbuf[64];
117
118 snprintf( infbuf, 64, "accuracy: %.7fms", rate_mul );
119 ui_text( (ui_rect){ panel[0] + 4,
120 panel[1] + 0 * 14, 500, 30 },
121 infbuf,
122 1,
123 k_ui_align_left, 0 );
124
125 for( int i=0; i<count; i++ ){
126 snprintf( infbuf, 64, "%.4fms %s",
127 avgs[i] * (1.0f/(VG_PROFILE_SAMPLE_COUNT-1)),
128 profiles[i]->name );
129
130 ui_text( (ui_rect){ panel[0] + panel[2] + 4,
131 panel[1] + i * 14, 0, 0 },
132 infbuf,
133 1,
134 k_ui_align_left, 0 );
135 }
136 }
137
138 static void vg_profiler_init(void)
139 {
140 VG_VAR_I32( vg_profiler, flags=VG_VAR_PERSISTENT );
141 }
142
143 #endif /* VG_PROFILER_H */