From 680f9d088cff050075f87e8bb290a7ad9a697db0 Mon Sep 17 00:00:00 2001 From: hgn Date: Thu, 26 Sep 2024 22:38:25 +0100 Subject: [PATCH] move profilers & audio inspector to magi --- vg_audio.c | 260 ++++++++++++++++++++++++++++--------------------- vg_audio.h | 12 +-- vg_audio_dsp.c | 27 +---- vg_audio_dsp.h | 4 - vg_engine.c | 70 +++++-------- vg_magi.c | 101 +++++++++++++++++-- vg_magi.h | 5 + vg_mem_view.c | 24 +++-- vg_profiler.c | 70 ++++++++++--- vg_profiler.h | 18 +++- 10 files changed, 355 insertions(+), 236 deletions(-) diff --git a/vg_audio.c b/vg_audio.c index e7bc7e5..b3e79be 100644 --- a/vg_audio.c +++ b/vg_audio.c @@ -26,6 +26,28 @@ static struct vg_profile vg_prof_audio_mix, vg_prof_audio_dsp; + +static f64 _vg_audio_budget() +{ + audio_lock(); + f64 ms = ((double)vg_audio.samples_last / 44100.0) * 1000.0; + audio_unlock(); + + return ms; +} + +struct vg_profile_set static _vg_prof_audio = +{ + .name = "audio", + .count = 3, + .get_budget = _vg_audio_budget, + .list = + { + &vg_prof_audio_decode, &vg_prof_audio_mix, &vg_prof_audio_dsp + } +}; + + /* * These functions are called from the main thread and used to prevent bad * access. TODO: They should be no-ops in release builds. @@ -104,42 +126,6 @@ void vg_audio_device_init(void) } } -void vg_audio_register(void) -{ - vg_console_reg_var( "debug_audio", &vg_audio.debug_ui, - k_var_dtype_i32, VG_VAR_CHEAT ); - vg_console_reg_var( "debug_dsp", &vg_audio.debug_dsp, - k_var_dtype_i32, VG_VAR_CHEAT ); - vg_console_reg_var( "volume", &vg_audio.external_global_volume, - k_var_dtype_f32, VG_VAR_PERSISTENT ); - vg_console_reg_var( "vg_audio_device", &vg_audio.device_choice, - k_var_dtype_str, VG_VAR_PERSISTENT ); - vg_console_reg_var( "vg_dsp", &vg_audio.dsp_enabled, - k_var_dtype_i32, VG_VAR_PERSISTENT ); -} - -void vg_audio_init(void) -{ - /* allocate memory */ - /* 32mb fixed */ - vg_audio.audio_pool = - vg_create_linear_allocator( vg_mem.rtmemory, 1024*1024*32, - VG_MEMORY_SYSTEM ); - - /* fixed */ - u32 decode_size = AUDIO_DECODE_SIZE * AUDIO_CHANNELS; - vg_audio.decode_buffer = vg_linear_alloc( vg_mem.rtmemory, decode_size ); - - vg_dsp_init(); - vg_audio_device_init(); -} - -void vg_audio_free(void) -{ - vg_dsp_free(); - SDL_CloseAudioDevice( vg_audio.sdl_output_device ); -} - /* * thread 1 */ @@ -715,7 +701,8 @@ static void audio_channel_mix( audio_channel *ch, float *buffer ) vg_profile_end( &_vg_prof_audio_mix ); } -static void audio_mixer_callback( void *user, u8 *stream, int byte_count ){ +static void audio_mixer_callback( void *user, u8 *stream, int byte_count ) +{ /* * Copy data and move edit flags to commit flags * ------------------------------------------------------------- */ @@ -728,13 +715,15 @@ static void audio_mixer_callback( void *user, u8 *stream, int byte_count ){ vg_audio.internal_listener_velocity ); vg_audio.internal_global_volume = vg_audio.external_global_volume; - for( int i=0; iallocated ) continue; - if( ch->activity == k_channel_activity_alive ){ + if( ch->activity == k_channel_activity_alive ) + { if( (ch->cursor >= ch->source_length) && !(ch->flags & AUDIO_FLAG_LOOP) ) { @@ -756,7 +745,8 @@ static void audio_mixer_callback( void *user, u8 *stream, int byte_count ){ } /* process new channels */ - if( ch->activity == k_channel_activity_reset ){ + if( ch->activity == k_channel_activity_reset ) + { ch->_ = ch->editable_state; ch->cursor = 0; ch->source_length = 0; @@ -769,23 +759,25 @@ static void audio_mixer_callback( void *user, u8 *stream, int byte_count ){ ch->editable_state.relinquished = ch->_.relinquished; - if( ch->editble_state_write_mask & AUDIO_EDIT_VOLUME ){ + if( ch->editble_state_write_mask & AUDIO_EDIT_VOLUME ) + { ch->_.volume = ch->editable_state.volume; ch->_.volume_target = ch->editable_state.volume; } - else{ + else ch->editable_state.volume = ch->_.volume; - } - if( ch->editble_state_write_mask & AUDIO_EDIT_VOLUME_SLOPE ){ + if( ch->editble_state_write_mask & AUDIO_EDIT_VOLUME_SLOPE ) + { ch->volume_movement_start = ch->_.volume; ch->volume_movement = 0; ch->_.volume_target = ch->editable_state.volume_target; ch->_.volume_rate = ch->editable_state.volume_rate; } - else{ + else + { ch->editable_state.volume_target = ch->_.volume_target; ch->editable_state.volume_rate = ch->_.volume_rate; } @@ -797,7 +789,8 @@ static void audio_mixer_callback( void *user, u8 *stream, int byte_count ){ ch->editable_state.sampling_rate = ch->_.sampling_rate; - if( ch->editble_state_write_mask & AUDIO_EDIT_LFO_ATTACHMENT ){ + if( ch->editble_state_write_mask & AUDIO_EDIT_LFO_ATTACHMENT ) + { ch->_.lfo = ch->editable_state.lfo; ch->_.lfo_amount = ch->editable_state.lfo_amount; } @@ -819,13 +812,16 @@ static void audio_mixer_callback( void *user, u8 *stream, int byte_count ){ ch->editble_state_write_mask = 0x00; } - for( int i=0; ieditble_state_write_mask & AUDIO_EDIT_LFO_WAVE ){ + if( lfo->editble_state_write_mask & AUDIO_EDIT_LFO_WAVE ) + { lfo->_.wave_type = lfo->editable_state.wave_type; - if( lfo->_.wave_type == k_lfo_polynomial_bipolar ){ + if( lfo->_.wave_type == k_lfo_polynomial_bipolar ) + { lfo->_.polynomial_coefficient = lfo->editable_state.polynomial_coefficient; lfo->sqrt_polynomial_coefficient = @@ -833,15 +829,18 @@ static void audio_mixer_callback( void *user, u8 *stream, int byte_count ){ } } - if( lfo->editble_state_write_mask & AUDIO_EDIT_LFO_PERIOD ){ - if( lfo->_.period ){ + if( lfo->editble_state_write_mask & AUDIO_EDIT_LFO_PERIOD ) + { + if( lfo->_.period ) + { float t = lfo->time; t/= (float)lfo->_.period; lfo->_.period = lfo->editable_state.period; lfo->time = lfo->_.period * t; } - else{ + else + { lfo->time = 0; lfo->_.period = lfo->editable_state.period; } @@ -856,10 +855,12 @@ static void audio_mixer_callback( void *user, u8 *stream, int byte_count ){ /* * Process spawns * ------------------------------------------------------------- */ - for( int i=0; iactivity == k_channel_activity_wake ){ + if( ch->activity == k_channel_activity_wake ) + { if( audio_channel_load_source( ch ) ) ch->activity = k_channel_activity_alive; else @@ -948,14 +949,14 @@ static void audio_mixer_callback( void *user, u8 *stream, int byte_count ){ vg_profile_increment( &_vg_prof_audio_mix ); vg_profile_increment( &_vg_prof_dsp ); - vg_prof_audio_mix = _vg_prof_audio_mix; - vg_prof_audio_decode = _vg_prof_audio_decode; - vg_prof_audio_dsp = _vg_prof_dsp; - - vg_audio.samples_last = frame_count; + if( vg_audio.inspector_open ) + { + vg_prof_audio_mix = _vg_prof_audio_mix; + vg_prof_audio_decode = _vg_prof_audio_decode; + vg_prof_audio_dsp = _vg_prof_dsp; - if( vg_audio.debug_dsp ) - vg_dsp_update_texture(); + vg_audio.samples_last = frame_count; + } audio_unlock(); } @@ -1098,55 +1099,22 @@ static void audio_require_clip_loaded( audio_clip *clip ) /* * Debugging */ +struct vg_audio_view_data +{ + i32 view_3d; +}; -void audio_debug_ui( ui_context *ctx, -#ifdef VG_3D - m4x4f -#else - m3x3f -#endif - mtx_pv ){ +static void cb_vg_audio_view( ui_context *ctx, ui_rect rect, + struct vg_magi_panel *magi ) +{ + struct vg_audio_view_data *vd = magi->data; - if( !vg_audio.debug_ui ) - return; + ui_rect left, panel; + ui_split( rect, k_ui_axis_v, 256, 2, left, panel ); + ui_checkbox( ctx, left, "3D labels", &vd->view_3d ); audio_lock(); - - glBindTexture( GL_TEXTURE_2D, vg_dsp.view_texture ); - glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 256, 256, - GL_RGBA, GL_UNSIGNED_BYTE, - vg_dsp.view_texture_buffer ); - - /* - * Profiler - * ----------------------------------------------------------------------- - */ - - float budget = ((double)vg_audio.samples_last / 44100.0) * 1000.0; - vg_profile_drawn( ctx, - (struct vg_profile *[]){ &vg_prof_audio_decode, - &vg_prof_audio_mix, - &vg_prof_audio_dsp}, 3, - budget, (ui_rect){ 4, VG_PROFILE_SAMPLE_COUNT*2 + 8, - 512, 0 }, 0, 0 ); - - char perf[128]; - - /* Draw UI */ - ui_rect window = { - 0, - 0, - 800, - AUDIO_CHANNELS * 18 - }; - - if( vg_audio.debug_dsp ) - { - ui_rect view_thing = { 4, vg.window_y-512-4, 512, 512 }; - ui_image( ctx, view_thing, &vg_dsp.view_texture ); - } - ui_rect overlap_buffer[ AUDIO_CHANNELS ]; u32 overlap_length = 0; @@ -1156,11 +1124,15 @@ void audio_debug_ui( ui_context *ctx, audio_channel *ch = &vg_audio.channels[i]; ui_rect row; - ui_split( window, k_ui_axis_h, 18, 1, row, window ); + ui_split( panel, k_ui_axis_h, 18, 1, row, panel ); + + bool show_row = ui_clip( rect, row, row ); if( !ch->allocated ) { - ui_fill( ctx, row, 0x50333333 ); + if( show_row ) + ui_fill( ctx, row, 0x50333333 ); + continue; } @@ -1206,17 +1178,20 @@ void audio_debug_ui( ui_context *ctx, ch->editable_state.volume, ch->name ); - ui_fill( ctx, row, 0xa0000000 | ch->colour ); - ui_text( ctx, row, perf, 1, k_ui_align_middle_left, 0 ); + if( show_row ) + { + ui_fill( ctx, row, 0xa0000000 | ch->colour ); + ui_text( ctx, row, perf, 1, k_ui_align_middle_left, 0 ); + } #ifdef VG_3D - if( AUDIO_FLAG_SPACIAL_3D ) + if( vd->view_3d && (ch->flags & AUDIO_FLAG_SPACIAL_3D) ) { v4f wpos; v3_copy( ch->editable_state.spacial_falloff, wpos ); wpos[3] = 1.0f; - m4x4_mulv( mtx_pv, wpos, wpos ); + m4x4_mulv( vg.pv, wpos, wpos ); if( wpos[3] > 0.0f ) { @@ -1258,3 +1233,66 @@ void audio_debug_ui( ui_context *ctx, audio_unlock(); } + +static void cb_vg_audio_close( struct vg_magi_panel *me ) +{ + vg_audio.inspector_open = 0; + free( me->data ); +} + +static int cmd_vg_audio( int argc, const char *argv[] ) +{ + if( vg_audio.inspector_open ) + { + vg_error( "Only 1 audio inspector at a time.\n" ); + return 0; + } + + vg_audio.inspector_open = 1; + ui_px w = 800, h=8*18; + struct vg_magi_panel *magi = _vg_magi_open( w,h, VG_MAGI_ALL ); + magi->title = "Audio inspector"; + + struct vg_audio_view_data *vd = malloc(sizeof(struct vg_audio_view_data)); + vd->view_3d = 0; + + magi->data = vd; + magi->ui_cb = cb_vg_audio_view; + magi->close_cb = cb_vg_audio_close; + return 1; +} + +void vg_audio_register(void) +{ + vg_console_reg_cmd( "vg_audio", cmd_vg_audio, NULL ); + vg_console_reg_var( "volume", &vg_audio.external_global_volume, + k_var_dtype_f32, VG_VAR_PERSISTENT ); + vg_console_reg_var( "vg_audio_device", &vg_audio.device_choice, + k_var_dtype_str, VG_VAR_PERSISTENT ); + vg_console_reg_var( "vg_dsp", &vg_audio.dsp_enabled, + k_var_dtype_i32, VG_VAR_PERSISTENT ); +} + +void vg_audio_init(void) +{ + _vg_profile_reg_set( &_vg_prof_audio ); + + /* allocate memory */ + /* 32mb fixed */ + vg_audio.audio_pool = + vg_create_linear_allocator( vg_mem.rtmemory, 1024*1024*32, + VG_MEMORY_SYSTEM ); + + /* fixed */ + u32 decode_size = AUDIO_DECODE_SIZE * AUDIO_CHANNELS; + vg_audio.decode_buffer = vg_linear_alloc( vg_mem.rtmemory, decode_size ); + + vg_dsp_init(); + vg_audio_device_init(); +} + +void vg_audio_free(void) +{ + SDL_CloseAudioDevice( vg_audio.sdl_output_device ); +} + diff --git a/vg_audio.h b/vg_audio.h index a579487..f660cbb 100644 --- a/vg_audio.h +++ b/vg_audio.h @@ -174,7 +174,8 @@ struct vg_audio_system } channels[ AUDIO_CHANNELS ]; - int debug_ui, debug_ui_3d, debug_dsp, dsp_enabled; + bool inspector_open; + int dsp_enabled; v3f internal_listener_pos, internal_listener_ears, @@ -223,12 +224,3 @@ int audio_oneshot( audio_clip *clip, f32 volume, f32 pan ); void audio_set_lfo_wave( int id, enum lfo_wave_type type, f32 coefficient ); void audio_set_lfo_frequency( int id, float freq ); int audio_channel_load_source( audio_channel *ch ); - -void audio_debug_ui( ui_context *ctx, - -#ifdef VG_3D - m4x4f -#else - m3x3f -#endif - mtx_pv ); diff --git a/vg_audio_dsp.c b/vg_audio_dsp.c index e362b3e..cd49883 100644 --- a/vg_audio_dsp.c +++ b/vg_audio_dsp.c @@ -131,22 +131,10 @@ static struct dsp_lpf __echos_lpf[8]; #endif static struct dsp_schroeder __diffusion_chain[8]; -static void async_vg_dsp_alloc_texture( void *payload, u32 size ) +void vg_dsp_init( void ) { - glGenTextures( 1, &vg_dsp.view_texture ); - glBindTexture( GL_TEXTURE_2D, vg_dsp.view_texture ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, - GL_RGBA, GL_UNSIGNED_BYTE, vg_dsp.view_texture_buffer ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); -} - -void vg_dsp_init( void ){ vg_rand_seed( &vg_dsp.rand, 461 ); vg_dsp.buffer = vg_linear_alloc( vg_mem.rtmemory, 1024*1024*1 ); - vg_dsp.view_texture_buffer = vg_linear_alloc( vg_mem.rtmemory, 512*512 ); - - vg_async_call( async_vg_dsp_alloc_texture, NULL, 0 ); /* temporary global design */ dsp_init_lpf( &__lpf_mud_free, 125.0f ); @@ -286,16 +274,3 @@ void dsp_update_tunings(void) vg_dsp.echo_tunings[i] *= volumes[i]; } } - -void vg_dsp_free( void ) -{ - glDeleteTextures( 1, &vg_dsp.view_texture ); -} - -void vg_dsp_update_texture( void ) -{ - for( int i=0; i<512*512; i++ ){ - float v = vg_clampf( vg_dsp.buffer[i] * 0.5f + 0.5f, 0.0f, 1.0f ); - vg_dsp.view_texture_buffer[i] = v * 255.0f; - } -} diff --git a/vg_audio_dsp.h b/vg_audio_dsp.h index e3239eb..4a1e9ae 100644 --- a/vg_audio_dsp.h +++ b/vg_audio_dsp.h @@ -11,9 +11,6 @@ struct vg_dsp float *buffer; u32 allocations; - u8 *view_texture_buffer; - GLuint view_texture; - float echo_distances[14], echo_tunings[8], reverb_wet_mix, @@ -51,7 +48,6 @@ void vg_dsp_init( void ); void vg_dsp_free( void ); void dsp_update_tunings(void); void vg_dsp_process( float *stereo_in, float *stereo_out ); -void vg_dsp_update_texture( void ); f32 dsp_biquad_process( struct dsp_biquad *bq, f32 xn ); void dsp_init_biquad_butterworth_lpf( struct dsp_biquad *bq, f32 fc ); diff --git a/vg_engine.c b/vg_engine.c index 9b0c1bf..920bdd8 100644 --- a/vg_engine.c +++ b/vg_engine.c @@ -71,6 +71,24 @@ static struct vg_profile vg_prof_update = {.name="update()"}, vg_prof_render = {.name="render()"}, vg_prof_swap = {.name="swap"}; +static f64 _vg_gameloop_budget() +{ + int frame_target = vg.display_refresh_rate; + if( vg.fps_limit > 0 ) frame_target = vg.fps_limit; + return (1.0/(f64)frame_target)*1000.0; +} + +struct vg_profile_set static _vg_prof_gameloop = +{ + .name = "gameloop", + .get_budget = _vg_gameloop_budget, + .count = 3, + .list = + { + &vg_prof_update, &vg_prof_render, &vg_prof_swap + } +}; + static void async_vg_bake_shaders( void *payload, u32 size ) { vg_shaders_compile(); @@ -124,6 +142,8 @@ vg_info(" ' ' '--' [] '----- '----- ' ' '---' " vg_loader_step( vg_input_init, vg_input_free ); vg_loader_step( vg_lines_init, NULL ); vg_loader_step( vg_rb_view_init, NULL ); + _vg_profile_reg_set( &_vg_prof_gameloop ); + #ifndef VG_NO_AUDIO vg_loader_step( vg_audio_init, vg_audio_free ); #endif @@ -310,53 +330,7 @@ static void _vg_gameloop_render(void) if( vg.settings_open ) vg_settings_gui( &vg_ui.ctx ); - - /* vg tools */ -#ifndef VG_NO_AUDIO - audio_debug_ui( &vg_ui.ctx, vg.pv ); -#endif - - /* profiling TODO MOVE TO OWN FILE */ - if( vg_profiler ){ - int frame_target = vg.display_refresh_rate; - if( vg.fps_limit > 0 ) frame_target = vg.fps_limit; - vg_profile_drawn( - &vg_ui.ctx, - (struct vg_profile *[]){ - &vg_prof_update,&vg_prof_render,&vg_prof_swap}, 3, - (1.0f/(float)frame_target)*1000.0f, - (ui_rect){ 4, 4, 250, 0 }, 0, 0 - ); - char perf[256]; - - snprintf( perf, 255, - "x: %d y: %d\n" - "refresh: %d (%.1fms)\n" - "samples: %d\n" - "iterations: %d (acc: %.3fms%%)\n" - "time: real(%.2f) delta(%.2f) rate(%.2f)\n" - " extrap(%.2f) frame(%.2f) spin( "PRINTF_U64" )\n", - vg.window_x, vg.window_y, - frame_target, (1.0f/(float)frame_target)*1000.0f, - vg.samples, - vg.fixed_iterations, - (vg.time_fixed_accumulator/VG_TIMESTEP_FIXED)*100.0f, - vg.time_real, vg.time_delta, vg.time_rate, - vg.time_fixed_extrapolate, vg.time_frame_delta, - vg.time_spinning ); - - ui_text( &vg_ui.ctx, - (ui_rect){258,4,900,900},perf,1,0,k_ui_align_left); - } - -#if 0 - if( vg_mem_view && vg_loader_availible() ) - { - vg_mem_view_ui( &vg_ui.ctx, (ui_rect){400,32, - vg.window_x-400,vg.window_y-64}, - vg_mem.rtmemory, 0, "rtmemory" ); - } -#endif + _vg_magi_render( &vg_ui.ctx ); ui_postrender( &vg_ui.ctx, vg.time_frame_delta ); @@ -1152,7 +1126,7 @@ static void vg_settings_audio_apply(void) { struct ui_enum_opt *selected = NULL, *oi; - for( int i=0; iflags = flags; panel->ui_cb = NULL; panel->close_cb = NULL; + panel->min_w = w; + panel->min_h = h; if( flags & VG_MAGI_PERSISTENT ) strcpy( panel->cmd, vg_console.input ); @@ -65,17 +67,41 @@ void _vg_magi_render( ui_context *ctx ) } else if( _vg_magi.mode == k_magi_mode_resize ) { - ptop->rect[2] = _vg_magi.drag_original[2] + ctx->mouse_delta[0]; - ptop->rect[3] = _vg_magi.drag_original[3] + ctx->mouse_delta[1]; + ui_px dx = ctx->mouse_delta[0]; + if( _vg_magi.drag_left ) dx = -dx; - if( ptop->rect[2] < ptop->min_w ) ptop->rect[2] = ptop->min_w; - if( ptop->rect[3] < ptop->min_h ) ptop->rect[3] = ptop->min_h; + ptop->rect[2] = _vg_magi.drag_original[2] + dx; + if( ptop->rect[2] < ptop->min_w ) + { + ptop->rect[2] = ptop->min_w; + dx = ptop->min_w - _vg_magi.drag_original[2]; + } + + if( _vg_magi.drag_left ) + ptop->rect[0] = _vg_magi.drag_original[0] - dx; + + if( !ptop->minimized ) + { + ui_px dy = ctx->mouse_delta[1]; + if( _vg_magi.drag_top ) dy = -dy; + + ptop->rect[3] = _vg_magi.drag_original[3] + dy; + if( ptop->rect[3] < ptop->min_h ) + { + ptop->rect[3] = ptop->min_h; + dy = ptop->min_h - _vg_magi.drag_original[3]; + } + + if( _vg_magi.drag_top ) + ptop->rect[1] = _vg_magi.drag_original[1] - dy; + } } if( ui_click_up( ctx, UI_MOUSE_ANY ) ) _vg_magi.mode = k_magi_mode_none; } + i32 j=0; for( i32 i=0; i<_vg_magi.panel_count; i ++ ) { struct vg_magi_panel *panel = &_vg_magi.panels[ i ]; @@ -84,6 +110,14 @@ void _vg_magi_render( ui_context *ctx ) ui_split( panel->rect, k_ui_axis_h, 28, 0, title, rect ); ui_fill( ctx, title, ui_opacity( ui_colour( ctx, k_ui_bg+7 ), 0.9f ) ); + ui_rect min_button, quit_button; + ui_split( title, k_ui_axis_v, title[2]-title[3], 2, title, quit_button ); + int should_close = ui_button_text( ctx, quit_button, "X", 1 ); + + ui_split( title, k_ui_axis_v, title[2]-title[3], 2, title, min_button ); + int should_min = ui_button_text( ctx, min_button, + panel->minimized? "+": "-", 1 ); + if( panel->flags & VG_MAGI_PERSISTENT ) { ui_text( ctx, title, panel->cmd, 1, k_ui_align_middle_center, @@ -114,6 +148,14 @@ void _vg_magi_render( ui_context *ctx ) if( panel->flags & VG_MAGI_RESIZEABLE ) { _vg_magi.mode = k_magi_mode_resize; + _vg_magi.drag_top = 0; + _vg_magi.drag_left = 0; + + if( ctx->mouse[0] < panel->rect[0]+panel->rect[2]/2 ) + _vg_magi.drag_left = 1; + if( ctx->mouse[1] < panel->rect[1]+panel->rect[3]/2 ) + _vg_magi.drag_top = 1; + rect_copy( panel->rect, _vg_magi.drag_original ); } } @@ -125,8 +167,38 @@ void _vg_magi_render( ui_context *ctx ) /* TODO: disable interaction */ } - panel->ui_cb( ctx, rect, panel ); + if( !panel->minimized ) + panel->ui_cb( ctx, rect, panel ); + + if( should_close == 1 ) + { + if( panel->close_cb ) + panel->close_cb( panel ); + + continue; + } + + if( should_min == 1 ) + { + panel->minimized ^= 0x1; + if( panel->minimized ) + { + panel->sh = panel->rect[3]; + panel->rect[3] = 32; + } + else + { + panel->rect[3] = panel->sh; + } + } + + if( j != i ) + _vg_magi.panels[ j ] = _vg_magi.panels[ i ]; + + j ++; } + + _vg_magi.panel_count = j; } void vg_magi_restore(void) @@ -151,8 +223,10 @@ void vg_magi_save(void) if( magi->flags & VG_MAGI_PERSISTENT ) { fprintf( fp, "%s\n", magi->cmd ); - fprintf( fp, "magi_pos %d %d %d %d\n", - magi->rect[0], magi->rect[1], magi->rect[2], magi->rect[3] ); + fprintf( fp, "magi_pos %d %d %d %d %d\n", + magi->rect[0], magi->rect[1], magi->rect[2], + magi->minimized? magi->sh: magi->rect[3], + (i32)magi->minimized ); } } @@ -169,16 +243,23 @@ static int cmd_vg_magi_dim( int argc, const char *argv[] ) if( argc == 0 ) { - vg_error( "Usage: magi_pos x y w h\n" ); + vg_error( "Usage: magi_pos x y w h \n" ); return 0; } struct vg_magi_panel *magi = &_vg_magi.panels[ _vg_magi.panel_count-1 ]; for( int i=0; i<4; i ++ ) - { if( argc >= i+1 ) magi->rect[i] = atoi( argv[i] ); - else break; + + if( argc >= 5 ) + { + magi->minimized = atoi( argv[4] ); + if( magi->minimized ) + { + magi->sh = magi->rect[3]; + magi->rect[3] = 32; + } } return 1; diff --git a/vg_magi.h b/vg_magi.h index 34c5f4a..3d480a0 100644 --- a/vg_magi.h +++ b/vg_magi.h @@ -13,6 +13,8 @@ struct vg_magi bool minimized; const char *title; ui_rect rect; + ui_px sh; + u32 flags; void *data; @@ -32,6 +34,9 @@ struct vg_magi k_magi_mode_resize } mode; + + bool drag_top, drag_left; + ui_rect drag_original; ui_px drag_start[2]; } diff --git a/vg_mem_view.c b/vg_mem_view.c index 9f882f6..c36913e 100644 --- a/vg_mem_view.c +++ b/vg_mem_view.c @@ -211,17 +211,6 @@ static void vg_mem_view_ui( ui_context *ctx, ui_rect rect, } } -static struct -{ - const char *name; - void **buffer; -} -_vg_mem_named_buffers[] = -{ - { "rtmemory", &vg_mem.rtmemory }, - { "scratch", &vg_mem.scratch } -}; - struct mem_view_data { void *main_buffer[8], @@ -372,6 +361,17 @@ static void cb_mem_view_close( struct vg_magi_panel *me ) free( me->data ); } +static struct +{ + const char *name; + void **buffer; +} +_vg_mem_named_buffers[] = +{ + { "rtmemory", &vg_mem.rtmemory }, + { "scratch", &vg_mem.scratch } +}; + static int cmd_vg_mem_view( int argc, const char *argv[] ) { if( argc == 1 ) @@ -401,8 +401,6 @@ static int cmd_vg_mem_view( int argc, const char *argv[] ) magi->data = mv; magi->ui_cb = cb_vg_mem_view; magi->close_cb = cb_mem_view_close; - magi->min_w = w; - magi->min_h = h; return 1; } } diff --git a/vg_profiler.c b/vg_profiler.c index 4785743..a4f2cca 100644 --- a/vg_profiler.c +++ b/vg_profiler.c @@ -3,7 +3,7 @@ #include "vg_engine.h" #include "vg_ui/imgui.h" -int vg_profiler = 0; +struct _vg_profiler _vg_profiler; void vg_profile_begin( struct vg_profile *profile ) { @@ -28,13 +28,13 @@ void vg_profile_end( struct vg_profile *profile ) u64 time_end = SDL_GetPerformanceCounter(), delta = time_end - profile->start; - if( profile->mode == k_profile_mode_frame ){ + if( profile->mode == k_profile_mode_frame ) + { profile->samples[ profile->buffer_current ] = delta; vg_profile_increment( profile ); } - else{ + else profile->samples[ profile->buffer_current ] += delta; - } } void vg_profile_drawn( ui_context *ctx, struct vg_profile **profiles, u32 count, @@ -97,11 +97,10 @@ void vg_profile_drawn( ui_context *ctx, struct vg_profile **profiles, u32 count, snprintf( infbuf, 64, "accuracy: %.7fms", rate_mul ); ui_text( ctx, - (ui_rect){ panel[0] + 4, - panel[1] + panel[3] - 14, 500, 30 }, + (ui_rect){ panel[0], panel[1], panel[2]-4, 24 }, infbuf, 1, - k_ui_align_left, 0 ); + k_ui_align_right, 0 ); for( int i=0; iname ); ui_text( ctx, - (ui_rect){ panel[0] + 4, - panel[1] + panel[3] + 4 + i*14, - panel[2]-8, 14 }, - infbuf, 1, k_ui_align_left, 0 ); + (ui_rect){ panel[0], panel[1] + 24 + i*14, + panel[2]-4, 14 }, + infbuf, 1, k_ui_align_right, 0 ); + } +} + +void _vg_profile_reg_set( struct vg_profile_set *set ) +{ + VG_ASSERT(_vg_profiler.named_count < VG_ARRAY_LEN(_vg_profiler.named_sets)); + _vg_profiler.named_sets[ _vg_profiler.named_count ++ ] = set; +} + +static void cb_vg_profiler( ui_context *ctx, ui_rect rect, + struct vg_magi_panel *magi ) +{ + struct vg_profile_set *ps = magi->data; + vg_profile_drawn( ctx, ps->list, ps->count, ps->get_budget(), rect, 0, 0 ); +} + +static int cmd_vg_profile( int argc, const char *argv[] ) +{ + if( argc == 1 ) + { + for( u32 i=0; i<_vg_profiler.named_count; i ++ ) + { + struct vg_profile_set *ps = _vg_profiler.named_sets[i]; + if( !strcmp( argv[0], ps->name ) ) + { + ui_px w = 256, h = VG_PROFILE_SAMPLE_COUNT * 2; + struct vg_magi_panel *magi = + _vg_magi_open( w,h, VG_MAGI_MOVEABLE|VG_MAGI_PERSISTENT ); + magi->title = "Profiler"; + magi->data = ps; + magi->ui_cb = cb_vg_profiler; + magi->close_cb = NULL; + return 1; + } + } } + else + vg_error( "Usage: vg_profile \n" ); + + return 0; +} + +static void cmd_vg_profile_poll( int argc, const char *argv[] ) +{ + const char *term = argv[ argc-1 ]; + + if( argc == 1 ) + for( u32 i=0; i<_vg_profiler.named_count; i ++ ) + console_suggest_score_text( _vg_profiler.named_sets[i]->name,term,0 ); } void vg_profiler_init(void) { - VG_VAR_I32( vg_profiler, flags=VG_VAR_PERSISTENT ); + vg_console_reg_cmd( "vg_profile", cmd_vg_profile, cmd_vg_profile_poll ); } diff --git a/vg_profiler.h b/vg_profiler.h index 4cad16b..d2a7f36 100644 --- a/vg_profiler.h +++ b/vg_profiler.h @@ -3,8 +3,6 @@ #include "vg_ui/imgui.h" #define VG_PROFILE_SAMPLE_COUNT 128 -extern int vg_profiler; - struct vg_profile { const char *name; @@ -22,6 +20,20 @@ struct vg_profile u64 start; }; +struct _vg_profiler +{ + struct vg_profile_set + { + const char *name; + u32 count; + f64 (*get_budget)(void); + struct vg_profile *list[]; + } + *named_sets[ 8 ]; + u32 named_count; +} +extern _vg_profiler; + void vg_profile_begin( struct vg_profile *profile ); void vg_profile_increment( struct vg_profile *profile ); void vg_profile_end( struct vg_profile *profile ); @@ -29,3 +41,5 @@ void vg_profile_drawn( ui_context *ctx, struct vg_profile **profiles, u32 count, f64 budget, ui_rect panel, int dir, i32 normalize ); void vg_profiler_init(void); + +void _vg_profile_reg_set( struct vg_profile_set *set ); -- 2.25.1