bad char
[vg.git] / vg_profiler.c
1 #include "vg_platform.h"
2 #include "vg_profiler.h"
3 #include "vg_engine.h"
4 #include "vg_imgui.h"
5
6 int vg_profiler = 0;
7
8 void vg_profile_begin( struct vg_profile *profile )
9 {
10 profile->start = SDL_GetPerformanceCounter();
11 }
12
13 void vg_profile_increment( struct vg_profile *profile )
14 {
15 profile->buffer_current ++;
16
17 if( profile->buffer_count < VG_PROFILE_SAMPLE_COUNT )
18 profile->buffer_count ++;
19
20 if( profile->buffer_current >= VG_PROFILE_SAMPLE_COUNT )
21 profile->buffer_current = 0;
22
23 profile->samples[ profile->buffer_current ] = 0;
24 }
25
26 void vg_profile_end( struct vg_profile *profile )
27 {
28 u64 time_end = SDL_GetPerformanceCounter(),
29 delta = time_end - profile->start;
30
31 if( profile->mode == k_profile_mode_frame ){
32 profile->samples[ profile->buffer_current ] = delta;
33 vg_profile_increment( profile );
34 }
35 else{
36 profile->samples[ profile->buffer_current ] += delta;
37 }
38 }
39
40 void vg_profile_drawn( struct vg_profile **profiles, u32 count,
41 f64 budget, ui_rect panel,
42 int dir, i32 normalize )
43 {
44 if( panel[2] == 0 )
45 panel[2] = 256;
46
47 if( panel[3] == 0 )
48 panel[3] = VG_PROFILE_SAMPLE_COUNT * 2;
49
50 f64 sh = (f32)panel[3^dir] / (f32)VG_PROFILE_SAMPLE_COUNT,
51 sw = (f32)panel[2^dir];
52
53 ui_fill( panel, 0xa0000000 );
54
55 if( count > 8 ) vg_fatal_error( "Too many profiles\n" );
56
57 f64 avgs[8];
58 u32 colours[8];
59 for( u32 i=0; i<count; i ++ ){
60 avgs[i] = 0.0;
61 colours[i] = ui_colour( k_ui_red + ((i*3)&0xe) );
62 }
63
64 f64 rate_mul = 1000.0 / (f64)SDL_GetPerformanceFrequency();
65
66 for( i32 i=0; i<VG_PROFILE_SAMPLE_COUNT; i++ ){
67 f64 total = 0.0;
68
69 if( normalize ){
70 budget = 0.0;
71 for( u32 j=0; j<count; j++ )
72 budget += (f64)profiles[j]->samples[i] * rate_mul;
73 }
74
75 for( int j=0; j<count; j++ ){
76 f64 sample = (f64)profiles[j]->samples[i] * rate_mul,
77 px = (total / budget) * sw,
78 wx = (sample / budget) * sw;
79
80 ui_rect block;
81 block[0^dir] = panel[0^dir] + px;
82 block[1^dir] = panel[1^dir] + (f32)i*sh;
83 block[2^dir] = VG_MAX( 1, wx-1 );
84 block[3^dir] = ceilf(sh)-1;
85 ui_fill( block, colours[j] );
86
87 total += sample;
88 avgs[j] += sample;
89 }
90 }
91
92 char infbuf[64];
93
94 snprintf( infbuf, 64, "accuracy: %.7fms", rate_mul );
95 ui_text( (ui_rect){ panel[0] + 4,
96 panel[1] + panel[3] - 14, 500, 30 },
97 infbuf,
98 1,
99 k_ui_align_left, 0 );
100
101 for( int i=0; i<count; i++ ){
102 snprintf( infbuf, 64, "%.4fms %s",
103 avgs[i] * (1.0f/(VG_PROFILE_SAMPLE_COUNT-1)),
104 profiles[i]->name );
105
106 ui_text( (ui_rect){ panel[0] + 4,
107 panel[1] + panel[3] + 4 + i*14,
108 panel[2]-8, 14 },
109 infbuf, 1, k_ui_align_left, 0 );
110 }
111 }
112
113 void vg_profiler_init(void)
114 {
115 VG_VAR_I32( vg_profiler, flags=VG_VAR_PERSISTENT );
116 }