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