labs work
[vg.git] / vg_profiler.h
index dedea02ca887fb9ef6ca9bbddf2b8cd510a68201..699e2a72eadfdacac40abac419c53737554c34dd 100644 (file)
@@ -26,12 +26,12 @@ struct vg_profile
    u64 start;
 };
 
-VG_STATIC void vg_profile_begin( struct vg_profile *profile )
+static void vg_profile_begin( struct vg_profile *profile )
 {
    profile->start = SDL_GetPerformanceCounter();
 }
 
-VG_STATIC void vg_profile_increment( struct vg_profile *profile )
+static void vg_profile_increment( struct vg_profile *profile )
 {
    profile->buffer_current ++;
 
@@ -44,7 +44,7 @@ VG_STATIC void vg_profile_increment( struct vg_profile *profile )
    profile->samples[ profile->buffer_current ] = 0;
 }
 
-VG_STATIC void vg_profile_end( struct vg_profile *profile )
+static void vg_profile_end( struct vg_profile *profile )
 {
    u64 time_end = SDL_GetPerformanceCounter(),
        delta    = time_end - profile->start;
@@ -58,55 +58,51 @@ VG_STATIC void vg_profile_end( struct vg_profile *profile )
    }
 }
 
-VG_STATIC void vg_profile_drawn( struct vg_profile **profiles, u32 count,
-                              float budget, ui_rect panel, u32 colour_offset )
+static void vg_profile_drawn( struct vg_profile **profiles, u32 count,
+                              f64 budget, ui_rect panel,
+                              int dir, i32 normalize )
 {
-   if( !vg_profiler )
-      return;
-
    if( panel[2] == 0 )
       panel[2] = 256;
 
    if( panel[3] == 0 )
       panel[3] = VG_PROFILE_SAMPLE_COUNT * 2;
 
-   float sh = panel[3] / VG_PROFILE_SAMPLE_COUNT,
-         sw = panel[2];
+   f64 sh = (f32)panel[3^dir] / (f32)VG_PROFILE_SAMPLE_COUNT,
+       sw = (f32)panel[2^dir];
 
    ui_fill( panel, 0xa0000000 );
 
    assert( count <= 8 );
-   double avgs[8];
-   int    ptrs[8];
-
-   for( int i=0; i<count; i++ ){
-      ptrs[i] = profiles[i]->buffer_current;
-      avgs[i] = 0.0f;
+   f64 avgs[8];
+   u32 colours[8];
+   for( u32 i=0; i<count; i ++ ){
+      avgs[i] = 0.0;
+      colours[i] = ui_colour( k_ui_red + ((i*3)&0xe) );
    }
 
-   u32 colours[] = { 0xff0000ff, 0xff00ff00, 0xff00ffff, 0xffff0000,
-                     0xffff00ff, 0xffffff00 };
+   f64 rate_mul = 1000.0 / (f64)SDL_GetPerformanceFrequency();
 
-   double rate_mul = 1000.0 / (double)SDL_GetPerformanceFrequency();
+   for( i32 i=0; i<VG_PROFILE_SAMPLE_COUNT; i++ ){
+      f64 total = 0.0;
 
-   for( int i=0; i<VG_PROFILE_SAMPLE_COUNT-1; i++ ){
-      double total = 0.0;
+      if( normalize ){
+         budget = 0.0;
+         for( u32 j=0; j<count; j++ )
+            budget += (f64)profiles[j]->samples[i] * rate_mul;
+      }
 
       for( int j=0; j<count; j++ ){
-         ptrs[j] --;
-
-         if( ptrs[j] < 0 )
-            ptrs[j] = VG_PROFILE_SAMPLE_COUNT-1;
-
-         double sample  = (double)profiles[j]->samples[ptrs[j]] * rate_mul,
-                px      = (total  / (budget)) * sw,
-                wx      = (sample / (budget)) * sw;
-
-         ui_rect block = { panel[0] + px, panel[1] + (float)i*sh,
-                           wx,            sh };
+         f64 sample  = (f64)profiles[j]->samples[i] * rate_mul,
+                px   = (total  / budget) * sw,
+                wx   = (sample / budget) * sw;
 
-         u32 colour = colours[ (j+colour_offset) % vg_list_size(colours) ];
-         ui_fill( block, colour );
+         ui_rect block;
+         block[0^dir] = panel[0^dir] + px;
+         block[1^dir] = panel[1^dir] + (f32)i*sh;
+         block[2^dir] = VG_MAX( 1, wx-1 );
+         block[3^dir] = ceilf(sh)-1;
+         ui_fill( block, colours[j] );
 
          total += sample;
          avgs[j] += sample;
@@ -117,7 +113,7 @@ VG_STATIC void vg_profile_drawn( struct vg_profile **profiles, u32 count,
 
    snprintf( infbuf, 64, "accuracy: %.7fms", rate_mul );
    ui_text( (ui_rect){ panel[0] + 4,
-                       panel[1] + 0 * 14, 500, 30 }, 
+                       panel[1] + panel[3] - 14, 500, 30 }, 
                        infbuf,
                        1,
                        k_ui_align_left, 0 );
@@ -127,15 +123,14 @@ VG_STATIC void vg_profile_drawn( struct vg_profile **profiles, u32 count,
                         avgs[i] * (1.0f/(VG_PROFILE_SAMPLE_COUNT-1)),
                         profiles[i]->name );
 
-      ui_text( (ui_rect){ panel[0] + panel[2] + 4,
-                          panel[1] + i * 14, 0, 0 }, 
-                          infbuf,
-                          1,
-                          k_ui_align_left, 0 );
+      ui_text( (ui_rect){ panel[0] + 4,
+                          panel[1] + panel[3] + 4 + i*14, 
+                          panel[2]-8, 14 }, 
+                          infbuf, 1, k_ui_align_left, 0 );
    }
 }
 
-VG_STATIC void vg_profiler_init(void)
+static void vg_profiler_init(void)
 {
    VG_VAR_I32( vg_profiler, flags=VG_VAR_PERSISTENT );
 }