better performance feedback
[vg.git] / src / vg / vg_audio.h
index ae37b2e5078000476f2d2f879fbec4d872817609..9ed6cda66918322acb3c481c0b29af11172b2194 100644 (file)
@@ -122,6 +122,7 @@ static struct vg_audio_system
    void             *mem, *decode_mem;
    u32               mem_current,
                      mem_total;
+   u32               samples_last;
 
    /* synchro */
    int               sync_locked;
@@ -150,23 +151,21 @@ static struct vg_audio_system
    /* System queue, and access from thread 0 */
    audio_entity      entity_queue[SFX_MAX_SYSTEMS];
    int               queue_len;
-
-   char              performance_info[128],
-                     performance_sub0[64],
-                     performance_sub1[64];
-
    int               debug_ui;
 
    v3f               listener_pos,
                      listener_ears;
-
-   double            perf_ms_decode,
-                     perf_ms_mix;
-
-   u32               perf_measurements;
 }
 vg_audio;
 
+static struct vg_profile 
+   _vg_prof_audio_decode = {.mode = k_profile_mode_accum,
+                            .name = "[T2] audio_decode()"},
+   _vg_prof_audio_mix    = {.mode = k_profile_mode_accum,
+                            .name = "[T2] audio_mix()"},
+   vg_prof_audio_decode,
+   vg_prof_audio_mix;
+
 static void *audio_alloc( u32 size )
 {
    u32 new_current = vg_audio.mem_current + size;
@@ -559,8 +558,7 @@ stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis *f, float *buffer,
 
 static void audio_entity_get_samples( aatree_ptr id, u32 count, float *buf )
 {
-   struct timespec time_start, time_end;
-   clock_gettime( CLOCK_REALTIME, &time_start );
+   vg_profile_begin( &_vg_prof_audio_decode );
 
    struct active_audio_player *aap = &vg_audio.active_players[id];
    audio_entity *ent = &aap->ent;
@@ -623,9 +621,7 @@ static void audio_entity_get_samples( aatree_ptr id, u32 count, float *buf )
    }
 
    ent->cur = cursor;
-
-   clock_gettime( CLOCK_REALTIME, &time_end );
-   vg_audio.perf_ms_decode += vg_time_diff( time_start, time_end );
+   vg_profile_end( &_vg_prof_audio_decode );
 }
 
 static void audio_entity_mix( aatree_ptr id, float *buffer, 
@@ -644,8 +640,7 @@ static void audio_entity_mix( aatree_ptr id, float *buffer,
 
    audio_entity_get_samples( id, frame_count, pcf );
 
-   struct timespec time_start, time_end;
-   clock_gettime( CLOCK_REALTIME, &time_start );
+   vg_profile_begin( &_vg_prof_audio_mix );
 
    if( ent->info.flags & AUDIO_FLAG_SPACIAL_3D )
       audio_entity_spacialize( ent, &vol, &pan );
@@ -677,8 +672,7 @@ static void audio_entity_mix( aatree_ptr id, float *buffer,
       buffer_pos ++;
    }
 
-   clock_gettime( CLOCK_REALTIME, &time_end );
-   vg_audio.perf_ms_mix += vg_time_diff( time_start, time_end );
+   vg_profile_end( &_vg_prof_audio_mix );
 }
 
 /*
@@ -707,10 +701,6 @@ static void audio_mixer_callback( ma_device *pDevice, void *pOutBuf,
          audio_entity_mix( i, pOut32F, frame_count );
       }
    }
-   
-#if 0
-   vg_sleep_ms( 20 );
-#endif
 
    /* redistribute */
    audio_system_cleanup();
@@ -718,33 +708,16 @@ static void audio_mixer_callback( ma_device *pDevice, void *pOutBuf,
    /* TODO: what the hell is this? */
    (void)pInput;
    
-   /*
-    * Debug information
-    */
-   clock_gettime( CLOCK_REALTIME, &time_end );
-
-   double elapsed = vg_time_diff( time_start, time_end ),
-          budget  = ((double)frame_count / 44100.0) * 1000.0,
-          percent = (elapsed/budget) * 100.0;
-
-   snprintf( vg_audio.performance_info, 127, 
-                  "%.2fms/%.2fms (%.1f%%) (%u frames)",
-                  elapsed, budget, percent, frame_count );
 
-   vg_audio.perf_measurements ++;
-   if( vg_audio.perf_measurements >= 30 )
-   {
-      double ms_decode = vg_audio.perf_ms_decode * (1.0/30.0),
-             ms_mix    = vg_audio.perf_ms_mix    * (1.0/30.0);
-      
-      snprintf( vg_audio.performance_sub0, 63, "Decode %.2fms", ms_decode );
-      snprintf( vg_audio.performance_sub1, 63, "mix    %.2fms", ms_mix );
+   audio_lock();
+   vg_profile_increment( &_vg_prof_audio_decode );
+   vg_profile_increment( &_vg_prof_audio_mix );
 
-      vg_audio.perf_ms_decode = 0.0;
-      vg_audio.perf_ms_mix = 0.0;
+   vg_prof_audio_mix = _vg_prof_audio_mix;
+   vg_prof_audio_decode = _vg_prof_audio_decode;
 
-      vg_audio.perf_measurements = 0;
-   }
+   vg_audio.samples_last = frame_count;
+   audio_unlock();
 }
 
 /* Decompress entire vorbis stream into buffer */
@@ -1013,10 +986,6 @@ static void audio_debug_ui( m4x4f mtx_pv )
        infos[ SFX_MAX_SYSTEMS ];
        int num_systems = 0;
 
-   char perf[128],
-        psub0[64],
-        psub1[64];
-       
    audio_lock();
        
        for( int i=0; i<SFX_MAX_SYSTEMS; i ++ )
@@ -1036,26 +1005,22 @@ static void audio_debug_ui( m4x4f mtx_pv )
       snd->vol = ent->info.vol*100.0f;
       v3_copy( ent->info.world_position, snd->pos );
        }
-   strcpy( perf, vg_audio.performance_info );
-   strcpy( psub0, vg_audio.performance_sub0 );
-   strcpy( psub1, vg_audio.performance_sub1 );
+
+   float budget = ((double)vg_audio.samples_last / 44100.0) * 1000.0;
+   vg_profile_drawn( (struct vg_profile *[]){ &vg_prof_audio_decode,
+                                              &vg_prof_audio_mix }, 2, 
+                     budget, (ui_rect){ 4, VG_PROFILE_SAMPLE_COUNT*2 + 8,
+                                        250, 0 }, 3 );
 
    audio_unlock();
 
+   char perf[128];
+       
    /* Draw UI */
-   ui_global_ctx.cursor[0] = 10;
-   ui_global_ctx.cursor[1] = 10;
+   ui_global_ctx.cursor[0] = 258;
+   ui_global_ctx.cursor[1] = VG_PROFILE_SAMPLE_COUNT*2+8+24+12;
    ui_global_ctx.cursor[2] = 150;
    ui_global_ctx.cursor[3] = 12;
-
-   ui_text( &ui_global_ctx, ui_global_ctx.cursor, perf, 1, 0 );
-   ui_global_ctx.cursor[1] += 20;
-
-   ui_text( &ui_global_ctx, ui_global_ctx.cursor, psub0, 1, 0 );
-   ui_global_ctx.cursor[1] += 20;
-
-   ui_text( &ui_global_ctx, ui_global_ctx.cursor, psub1, 1, 0 );
-   ui_global_ctx.cursor[1] += 20;
    
    float usage = (float)vg_audio.mem_current / (1024.0f*1024.0f),
          total = (float)vg_audio.mem_total   / (1024.0f*1024.0f),
@@ -1110,8 +1075,8 @@ static void audio_debug_ui( m4x4f mtx_pv )
             v2_add( wpos, (v2f){ 0.5f, 0.5f }, wpos );
             
             ui_rect wr;
-            wr[0] = wpos[0] * vg_window_x;
-            wr[1] = (1.0f-wpos[1]) * vg_window_y;
+            wr[0] = wpos[0] * vg.window_x;
+            wr[1] = (1.0f-wpos[1]) * vg.window_y;
             wr[2] = 100;
             wr[3] = 17;