switch to async system
[vg.git] / vg.h
diff --git a/vg.h b/vg.h
index 7ddd04ef839871dc19b845c60a23d7a02043ec78..9ad04b1f659ca600fc9529fcd3399ef9f9dfea0b 100644 (file)
--- a/vg.h
+++ b/vg.h
 |IMP| |   vg_ui(void)
 |   | |      |
 |   |  '----'
-'___'
-
- .-.
-| ? |
-|   |  .-------------------------------------.
-|API| | vg_fatal_exit_loop( const char *err ) |
-|   |  '-------------------------------------'
-|   |         |
-|   |  .------+.
-|   | |         |
-|   | |         v
-|IMP|  '- vg_framebuffer_resize(void)
 '___'
 
 */
@@ -106,8 +94,8 @@ VG_STATIC void vg_print_backtrace(void)
     #include "vg_m.h"
     #include "vg_io.h"
     #include "vg_log.h"
-    #include "vg_async.h"
     #include "vg_steam.h"
+    #include <setjmp.h>
 
   //#define VG_SYNC_DEBUG
   #ifdef VG_SYNC_DEBUG
@@ -142,27 +130,22 @@ struct vg
    SDL_Window     *window;
    SDL_GLContext  gl_context;
 
-   SDL_SpinLock  sl_context;
-   SDL_sem      *sem_allow_exec,
-                *sem_exec_finished,
-                *sem_loader;
+   SDL_sem *sem_loader;        /* allows only one loader at a time */
 
-   SDL_threadID  thread_id_main,
-                 thread_id_loader,
-                 thread_id_with_opengl_context;
-   int           context_ownership_depth;
+   jmp_buf env_loader_exit;
 
-   int exec_context;
+   SDL_threadID  thread_id_main,
+                 thread_id_loader;
 
+   SDL_SpinLock sl_status;
    enum engine_status
    {
       k_engine_status_none,
+      k_engine_status_load_internal,
       k_engine_status_running,
       k_engine_status_crashed
    }
    engine_status;
-   const char *str_const_engine_err;
-   int         is_loaded;
 
    /* Window information */
    int window_x,
@@ -227,132 +210,33 @@ enum vg_thread_purpose
    k_thread_purpose_loader
 };
 
-VG_STATIC void vg_fatal_exit_loop( const char *error );
+#include "vg_async.h"
 
-/*
- * Checks if the engine is running
- */
-VG_STATIC void _vg_ensure_engine_running(void)
+VG_STATIC enum engine_status _vg_engine_status(void)
 {
-   /* Check if the engine is no longer running */
-   SDL_AtomicLock( &vg.sl_context );
+   SDL_AtomicLock( &vg.sl_status );
    enum engine_status status = vg.engine_status;
-   SDL_AtomicUnlock( &vg.sl_context );
+   SDL_AtomicUnlock( &vg.sl_status );
 
-   if( status != k_engine_status_running ){
-      while(1) {
-         VG_SYNC_LOG( "[%d] No longer running...\n");
-         SDL_Delay(1000);
-      }
-   }
+   return status;
 }
 
 VG_STATIC enum vg_thread_purpose vg_thread_purpose(void)
 {
-   SDL_AtomicLock( &vg.sl_context );
+   SDL_AtomicLock( &vg.sl_status );
 
    if( vg.thread_id_main == SDL_GetThreadID(NULL) ){
-      SDL_AtomicUnlock( &vg.sl_context );
+      SDL_AtomicUnlock( &vg.sl_status );
       return k_thread_purpose_main;
    }
    else{
-      SDL_AtomicUnlock( &vg.sl_context );
+      SDL_AtomicUnlock( &vg.sl_status );
       return k_thread_purpose_loader;
    }
 }
 
-/*
- * Sync execution so that the OpenGL context is switched onto this thread.
- * Anything after this call will be in a valid context.
- */
-VG_STATIC void vg_acquire_thread_sync(void)
-{
-   /* We dont want to do anything if this is the main thread */
-
-   if( vg_thread_purpose() == k_thread_purpose_loader ){
-      VG_SYNC_LOG( "[%d] vg_acquire_thread_sync()\n" );
-      _vg_ensure_engine_running();
-
-      SDL_AtomicLock( &vg.sl_context );
-      if( vg.context_ownership_depth == 0 ){
-         vg.context_ownership_depth ++;
-         vg.exec_context = 1;
-         SDL_AtomicUnlock( &vg.sl_context );
-         
-         /* wait until told we can go */
-         VG_SYNC_LOG( "[%d] Waiting to acuire sync.\n" );
-         SDL_SemWait( vg.sem_allow_exec );
-         
-         _vg_ensure_engine_running();
-
-         SDL_GL_MakeCurrent( vg.window, vg.gl_context );
-         VG_SYNC_LOG( "[%d] granted\n" );
-      }
-      else{
-         vg.context_ownership_depth ++;
-         VG_SYNC_LOG( "[%d] granted\n" );
-         SDL_AtomicUnlock( &vg.sl_context );
-      }
-   }
-}
-
-/*
- * Signify that we are done with the OpenGL context in this thread.
- * Anything after this call will be in an undefined context.
- */
-VG_STATIC void vg_release_thread_sync(void)
-{
-   if( vg_thread_purpose() == k_thread_purpose_loader ){
-      VG_SYNC_LOG( "[%d] vg_release_thread_sync()\n" );
-
-      SDL_AtomicLock( &vg.sl_context );
-      vg.context_ownership_depth --;
-
-      if( vg.context_ownership_depth == 0 ){
-         SDL_AtomicUnlock( &vg.sl_context );
-         VG_SYNC_LOG( "[%d] Releasing context.\n" );
-         SDL_GL_MakeCurrent( NULL, NULL );
-         SDL_SemPost( vg.sem_exec_finished );
-      }
-      else
-         SDL_AtomicUnlock( &vg.sl_context );
-   }
-}
-
-VG_STATIC void _vg_run_synced(void)
-{
-   SDL_AtomicLock( &vg.sl_context );
-
-   if( vg.exec_context != 0 ){
-      VG_SYNC_LOG( "[%d] _vg_run_synced() (%d).\n", vg.exec_context );
-      vg.exec_context = 0;
-      SDL_AtomicUnlock( &vg.sl_context );
-
-      /* allow operations to go */
-      SDL_GL_MakeCurrent( NULL, NULL );
-      SDL_SemPost( vg.sem_allow_exec );
-
-      /* wait for operations to complete */
-      VG_SYNC_LOG( "[%d] Waiting for content.\n" );
-      SDL_SemWait( vg.sem_exec_finished );
-      
-      /* check if we killed the engine */
-      _vg_ensure_engine_running();
-      
-      /* re-engage main thread */
-      VG_SYNC_LOG( "[%d] Re-engaging.\n" );
-      SDL_GL_MakeCurrent( vg.window, vg.gl_context );
-   }
-   else{
-      VG_SYNC_LOG( "[%d] Nothing to do.\n" );
-      SDL_AtomicUnlock( &vg.sl_context );
-   }
-}
-
 VG_STATIC void _vg_opengl_sync_init(void)
 {
-   vg.sem_allow_exec = SDL_CreateSemaphore(0);
-   vg.sem_exec_finished = SDL_CreateSemaphore(0);
    vg.sem_loader = SDL_CreateSemaphore(1);
 }
 
@@ -387,21 +271,36 @@ VG_STATIC void vg_checkgl( const char *src_info )
    }
 
    if( fail )
-      vg_fatal_exit_loop( "OpenGL Error" );
+      vg_fatal_error( "OpenGL Error" );
+}
+
+VG_STATIC void async_vg_bake_shaders( void *payload, u32 size )
+{
+   vg_shaders_compile();
 }
 
 VG_STATIC void vg_bake_shaders(void)
 {
-   vg_acquire_thread_sync();
    vg_console_reg_cmd( "reload_shaders", vg_shaders_live_recompile, NULL );
 
-   vg_shaders_compile();
-   vg_release_thread_sync();
+   vg_async_item *call = vg_async_alloc(0);
+   vg_async_dispatch( call, async_vg_bake_shaders );
 }
 
-void test_async_runner( void *payload, u32 size )
+void async_internal_complete( void *payload, u32 size )
 {
-   vg_success( "Async call test (%p, %u)\n", payload, size );
+   vg_success( "Internal async setup complete\n" );
+   SDL_AtomicLock( &vg.sl_status );
+
+   if( vg.engine_status == k_engine_status_crashed ){
+      SDL_AtomicUnlock( &vg.sl_status );
+      return;
+   }
+   else{
+      vg.engine_status = k_engine_status_running;
+   }
+
+   SDL_AtomicUnlock( &vg.sl_status );
 }
 
 VG_STATIC void _vg_load_full(void)
@@ -414,16 +313,15 @@ VG_STATIC void _vg_load_full(void)
    vg_loader_step( vg_audio_init, vg_audio_free );
    vg_loader_step( vg_profiler_init, NULL );
 
+   vg_async_item *test_async = vg_async_alloc( 0 );
+   vg_async_dispatch( test_async, async_internal_complete );
+
    /* client */
    vg_load();
-
-   vg_async_item *test_async = vg_async_alloc( 0 );
-   vg_async_dispatch( test_async, test_async_runner );
 }
 
 VG_STATIC void _vg_process_events(void)
 {
-   /* Update timers */
    v2_zero( vg.mouse_wheel );
    v2_zero( vg.mouse_delta );
 
@@ -520,66 +418,64 @@ VG_STATIC void _vg_gameloop_render(void)
 {
    vg_profile_begin( &vg_prof_render );
 
-   if( vg.is_loaded ){
-      /* render */
-      vg.engine_stage = k_engine_stage_rendering;
-      vg_render();
+   /* render */
+   vg.engine_stage = k_engine_stage_rendering;
+   vg_render();
 
-      /* ui */
-      vg.engine_stage = k_engine_stage_ui;
-      {
-         ui_begin( vg.window_x, vg.window_y );
+   /* ui */
+   vg.engine_stage = k_engine_stage_ui;
+   {
+      ui_begin( vg.window_x, vg.window_y );
 
-         /* TODO */
-         ui_set_mouse( vg.mouse_pos[0], vg.mouse_pos[1], 0 );
+      /* TODO */
+      ui_set_mouse( vg.mouse_pos[0], vg.mouse_pos[1], 0 );
 
-         int frame_target = vg.display_refresh_rate;
+      int frame_target = vg.display_refresh_rate;
 
-         if( vg.fps_limit > 0 ){
-            frame_target = vg.fps_limit;
-         }
+      if( vg.fps_limit > 0 ){
+         frame_target = vg.fps_limit;
+      }
+      
+      vg_profile_drawn( 
+            (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
+      );
+
+      if( vg_profiler ){
+         char perf[256];
          
-         vg_profile_drawn( 
-               (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
-         );
-
-         if( vg_profiler ){
-            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"
+         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"
 #ifdef _WIN32
-                  "      extrap(%.2f) frame(%.2f) spin( %llu )\n",
+               "      extrap(%.2f) frame(%.2f) spin( %llu )\n",
 #else
-                  "      extrap(%.2f) frame(%.2f) spin( %lu )\n",
+               "      extrap(%.2f) frame(%.2f) spin( %lu )\n",
 #endif
-                  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, vg.time_delta, vg.time_rate,
-                  vg.time_fixed_extrapolate, vg.time_frame_delta,
-                  vg.time_spinning );
-
-            ui_text( (ui_rect){258, 4+24+12+12,0,0},perf, 1,0);
-         }
-
-         /* FIXME */
-         audio_debug_ui( vg.pv );
-         vg_ui();
-         _vg_console_draw();
-         
-         ui_resolve();
-         ui_draw( NULL );
+               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, vg.time_delta, vg.time_rate,
+               vg.time_fixed_extrapolate, vg.time_frame_delta,
+               vg.time_spinning );
+
+         ui_text( (ui_rect){258, 4+24+12+12,0,0},perf, 1,0);
       }
+
+      /* FIXME */
+      audio_debug_ui( vg.pv );
+      vg_ui();
+      _vg_console_draw();
+      
+      ui_resolve();
+      ui_draw( NULL );
    }
 
    vg_profile_end( &vg_prof_render );
@@ -651,6 +547,29 @@ VG_STATIC int vg_framefilter( double dt )
    return 0;
 }
 
+VG_STATIC int _vg_crashscreen(void)
+{
+   if( vg.window_should_close )
+      return 1;
+
+   if( vg_getkey( SDLK_ESCAPE ) )
+      return 1;
+
+   glBindFramebuffer( GL_FRAMEBUFFER, 0 );
+   glEnable(GL_BLEND);
+   glDisable(GL_DEPTH_TEST);
+   glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
+   glBlendEquation(GL_FUNC_ADD);
+
+   glClearColor( 0.15f + sinf(vg.time)*0.1f, 0.0f, 0.0f,1.0f );
+   glClear( GL_COLOR_BUFFER_BIT );
+   glViewport( 0,0, vg.window_x, vg.window_y );
+
+   _vg_render_log();
+
+   return 0;
+}
+
 VG_STATIC void _vg_gameloop(void)
 {
    //vg.time_fixed_accumulator = 0.75f * (1.0f/60.0f);
@@ -674,30 +593,32 @@ VG_STATIC void _vg_gameloop(void)
 
       vg_profile_begin( &vg_prof_swap );
       SDL_GL_SwapWindow( vg.window );
-      _vg_run_synced();
       vg_profile_end( &vg_prof_swap );
 
-      vg.time_delta = vg.time_frame_delta * vg.time_rate;
-      vg.time += vg.time_delta;
-
-      vg_run_async_checked();
-      _vg_process_events();
+      enum engine_status status = _vg_engine_status();
 
-      if( vg.window_should_close )
-         break;
-      
-      if( vg.is_loaded ){
-         if( !post_start ){
-            vg_start();
-            post_start = 1;
-         }
+      if( status == k_engine_status_crashed ){
+         if( _vg_crashscreen() )
+            break;
       }
       else{
-         _vg_loader_render();
-      }
+         vg.time_delta = vg.time_frame_delta * vg.time_rate;
+         vg.time += vg.time_delta;
 
-      _vg_gameloop_update();
-      _vg_gameloop_render();
+         vg_run_async_checked();
+         _vg_process_events();
+
+         if( vg.window_should_close )
+            break;
+         
+         if( status == k_engine_status_running ){
+            _vg_gameloop_update();
+            _vg_gameloop_render();
+         }
+         else{
+            _vg_loader_render();
+         }
+      }
 
       vg.time_frame_delta = 0.0;
       vg.time_spinning = 0;
@@ -872,6 +793,24 @@ VG_STATIC void _vg_init_window( const char *window_name )
 #endif
 }
 
+VG_STATIC void _vg_terminate(void)
+{
+   /* Shutdown */
+   _vg_console_write_persistent();
+
+   SDL_AtomicLock( &vg.sl_status );
+   vg.engine_status = k_engine_status_none;
+   SDL_AtomicUnlock( &vg.sl_status );
+
+   _vg_loader_free();
+
+   vg_success( "If you see this it means everything went.. \"well\".....\n" );
+
+   SDL_GL_DeleteContext( vg.gl_context );
+   SDL_Quit();
+   exit(0);
+}
+
 VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name )
 {
    _vg_process_launch_opts_internal( argc, argv );
@@ -892,110 +831,45 @@ VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name )
    _vg_ui_init();
    _vg_loader_init();
 
-   vg.engine_status = k_engine_status_running;
+   vg.engine_status = k_engine_status_load_internal;
 
    _vg_opengl_sync_init();
     vg_loader_start( _vg_load_full );
    _vg_gameloop();
-
-   /* Shutdown */
-   _vg_console_write_persistent();
-
-   SDL_AtomicLock( &vg.sl_context );
-   vg.engine_status = k_engine_status_none;
-   SDL_AtomicUnlock( &vg.sl_context );
-
-   _vg_loader_free();
-
-   vg_success( "If you see this it means everything went.. \"well\".....\n" );
-
-   SDL_GL_DeleteContext( vg.gl_context );
-   SDL_Quit();
+   _vg_terminate();
 }
 
-/*
- * Immediately transfer away from calling thread into a safe loop, signal for 
- * others to shutdown, then free everything once the user closes the window.
- *
- * FIXME(bug): glfwWindowShouldClose() never returns 1 in windows via wine, ONLY
- *             when calling the program from outside its normal directory.
- */
-VG_STATIC void vg_fatal_exit_loop( const char *error )
+VG_STATIC void vg_fatal_error( const char *fmt, ... )
 {
-   /* 
-    *    https://www.gnu.org/software/libc/manual/html_node/Backtraces.html
-    *    thanks gnu <3
-    *
-    *    TODO: this on windows?
-    */
+   va_list args;
+   va_start( args, fmt );
+   _vg_log_write( stderr, KRED "  fatal" KWHT "| " KRED, fmt, args );
+   va_end( args );
 
    vg_print_backtrace();
-   vg_error( "Fatal error: %s\n", error );
 
-   SDL_AtomicLock( &vg.sl_context );
-   if( vg.engine_status == k_engine_status_none ){
-      SDL_AtomicUnlock( &vg.sl_context );
+   SDL_AtomicLock( &vg.sl_status );
+   vg.engine_status = k_engine_status_crashed;
+   SDL_AtomicUnlock( &vg.sl_status );
 
-      /* TODO: Correct shutdown before other systems */
-      exit(0);
+   if( vg_thread_purpose() == k_thread_purpose_loader ){
+      longjmp( vg.env_loader_exit, 1 );
    }
    else{
-      SDL_AtomicUnlock( &vg.sl_context );
-      
-      vg_acquire_thread_sync();
-
-      SDL_AtomicLock( &vg.sl_context );
-      vg.engine_status = k_engine_status_crashed;
-      vg.str_const_engine_err = error;
-      SDL_AtomicUnlock( &vg.sl_context );
-
-      /* Notify other thread for curtosey */
-      if( vg_thread_purpose() == k_thread_purpose_main ){
-         SDL_AtomicLock( &vg.sl_context );
-
-         if( vg.exec_context != 0 )
-            SDL_SemPost( vg.sem_allow_exec );
-
-         SDL_AtomicUnlock( &vg.sl_context );
-      }
-
-      vg_audio_free();
-
-      while(1){
-         _vg_process_events();
-         if( vg.window_should_close )
-            break;
-
-         if( vg_getkey( SDLK_ESCAPE ) )
-            break;
-
-         glBindFramebuffer( GL_FRAMEBUFFER, 0 );
-         glEnable(GL_BLEND);
-         glDisable(GL_DEPTH_TEST);
-         glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
-         glBlendEquation(GL_FUNC_ADD);
-
-         glClearColor( 0.15f + sinf(vg.time)*0.1f, 0.0f, 0.0f,1.0f );
-         glClear( GL_COLOR_BUFFER_BIT );
-         glViewport( 0,0, vg.window_x, vg.window_y );
-
-         _vg_render_log();
-         SDL_GL_SwapWindow( vg.window );
-      }
-
-      /* Can now shutdown and EXIT */
-      _vg_loader_free();
-      SDL_GL_DeleteContext( vg.gl_context );
-      SDL_Quit();
-      exit(0);
+      vg_error( "There is no jump to the error runner thing yet! bai bai\n" );
+      _vg_terminate();
    }
 }
 
 #else /* VG_GAME */
 
-VG_STATIC void vg_fatal_exit_loop( const char *error )
+#include "vg_log.h"
+VG_STATIC void vg_fatal_error( const char *fmt, ... )
 {
-   vg_error( "Fatal error: %s\n", error );
+   va_list args;
+   va_start( args, fmt );
+   _vg_log_write( stderr, KRED "  fatal" KWHT "| " KRED, fmt, args );
+   va_end( args );
    exit(0);
 }