bad char
[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 {
54 profile->samples[ profile->buffer_current ] = delta;
55 vg_profile_increment( profile );
56 }
57 else
58 {
59 profile->samples[ profile->buffer_current ] += delta;
60 }
61 }
62
63 VG_STATIC void vg_profile_drawn( struct vg_profile **profiles, u32 count,
64 float budget, ui_rect panel, u32 colour_offset )
65 {
66 if( !vg_profiler )
67 return;
68
69 if( panel[2] == 0 )
70 panel[2] = 256;
71
72 if( panel[3] == 0 )
73 panel[3] = VG_PROFILE_SAMPLE_COUNT * 2;
74
75 float sh = panel[3] / VG_PROFILE_SAMPLE_COUNT,
76 sw = panel[2];
77
78 ui_fill_rect( panel, 0xa0000000 );
79
80 assert( count <= 8 );
81 float avgs[8];
82 int ptrs[8];
83
84 for( int i=0; i<count; i++ )
85 {
86 ptrs[i] = profiles[i]->buffer_current;
87 avgs[i] = 0.0f;
88 }
89
90 u32 colours[] = { 0xff0000ff, 0xff00ff00, 0xff00ffff, 0xffff0000,
91 0xffff00ff, 0xffffff00 };
92
93 double rate_mul = 1000.0 / (double)SDL_GetPerformanceFrequency();
94
95 for( int i=0; i<VG_PROFILE_SAMPLE_COUNT-1; i++ )
96 {
97 float total = 0.0f;
98
99 for( int j=0; j<count; j++ )
100 {
101 ptrs[j] --;
102
103 if( ptrs[j] < 0 )
104 ptrs[j] = VG_PROFILE_SAMPLE_COUNT-1;
105
106 float sample = (double)profiles[j]->samples[ptrs[j]] * rate_mul,
107 px = (total / (budget)) * sw,
108 wx = (sample / (budget)) * sw;
109
110 ui_rect block = { panel[0] + px, panel[1] + (float)i*sh,
111 wx, sh };
112
113 u32 colour = colours[ (j+colour_offset) % vg_list_size(colours) ];
114 ui_fill_rect( block, colour );
115
116 total += sample;
117 avgs[j] += sample;
118 }
119 }
120
121 char infbuf[64];
122
123 for( int i=0; i<count; i++ )
124 {
125 snprintf( infbuf, 64, "%.4fms %s",
126 avgs[i] * (1.0f/(VG_PROFILE_SAMPLE_COUNT-1)),
127 profiles[i]->name );
128
129 ui_text( (ui_rect){ panel[0] + panel[2] + 4,
130 panel[1] + i * 14, 0, 0 },
131 infbuf,
132 1,
133 k_text_align_left );
134 }
135 }
136
137 VG_STATIC void vg_profiler_init(void)
138 {
139 vg_convar_push( (struct vg_convar){
140 .name = "vg_profiler",
141 .data = &vg_profiler,
142 .data_type = k_convar_dtype_i32,
143 .opt_i32 = { .min=0, .max=1, .clamp=1 },
144 .persistent = 1
145 });
146 }
147
148 #endif /* VG_PROFILER_H */