simplify gitignore
[vg.git] / src / vg / vg_loader.h
index 2ba707e0d03c83739c65451ee9a4ddbb5e18b784..72e4c3cad37fe8fb24309669df887197fb8ae0bd 100644 (file)
 
 #include "common.h"
 
-static struct vg_loader
-{
-   /* Synchronization */
-   vg_semaphore sem_loading;
-   vg_mutex mux_status;
+static struct vg_shader _shader_loader = 
+{
+   .name = "[vg] loader",
+   .link = NULL,
+   .vs = 
+   {
+      .orig_file = NULL,
+      .static_src = ""
+      "layout (location=0) in vec2 a_co;"
+      "out vec2 aUv;"
+      "void main()"
+      "{"
+         "gl_Position = vec4(a_co*2.0-1.0,0.0,1.0);"
+         "aUv = a_co;"
+      "}"
+   },
+   .fs = 
+   {
+      .orig_file = NULL,
+      .static_src = 
+      
+      "out vec4 FragColor;"
+      "uniform float uTime;"
+      "in vec2 aUv;"
+      
+      "void main()"
+      "{"
+         "float dither=fract(dot(vec2(171.0,231.0),gl_FragCoord.xy)/71.0)-0.5;"
+         "float grad = 1.0-(aUv.y*0.5+0.5);"
+         "float fmt1 = step( 0.5, grad+dither );"
 
-   enum loader_status
-   {
-      k_loader_status_loading,
-      k_loader_status_complete,
-      k_loader_status_fail
+         "vec3 col = 0.5+0.5*sin( uTime + aUv.xyx + vec3(0.0,2.0,4.0) );"
+
+         "FragColor = vec4(col*grad*fmt1,1.0);"
+      "}"
    }
-   status;
+};
 
+static struct vg_loader
+{
    /* Shutdown steps */
    struct loader_free_step
    {
       void (*fn_free)(void *);
       void *data;
    }
-   *step_buffer;
-   u32 step_count, step_cap, step_action;
+   step_buffer[16];
+   u32 step_count, step_action;
+
+   GLuint vao, vbo;
 }
 vg_loader;
 
-static int vg_loader_init(void)
+VG_STATIC void vg_loader_init(void)
 {
-   vg_semaphore_init( &vg_loader.sem_loading, 0 );
-   vg_mutex_init( &vg_loader.mux_status );
-   return 1;
+   float quad[] = { 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
+                    0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f };
+
+   glGenVertexArrays( 1, &vg_loader.vao );
+   glGenBuffers( 1, &vg_loader.vbo );
+   glBindVertexArray( vg_loader.vao );
+   glBindBuffer( GL_ARRAY_BUFFER, vg_loader.vbo );
+   glBufferData( GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW );
+   glBindVertexArray( vg_loader.vao );
+   glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*2, (void*)0 );
+   glEnableVertexAttribArray( 0 );
+
+   VG_CHECK_GL_ERR();
+
+   if( !vg_shader_compile( &_shader_loader ) )
+      vg_fatal_exit_loop( "failed to compile shader" );
 }
 
-static void vg_loader_free(void)
+VG_STATIC void vg_loader_free(void)
 {
-   vg_semaphore_wait( &vg_loader.sem_loading );
-   vg_semaphore_free( &vg_loader.sem_loading );
-   vg_mutex_free( &vg_loader.mux_status );
+   vg_info( "vg_loader_free\n" );
+   glDeleteVertexArrays( 1, &vg_loader.vao );
+   glDeleteBuffers( 1, &vg_loader.vbo );
 
    for( int i=0; i<vg_loader.step_count; i++ )
    {
       struct loader_free_step *step = 
          &vg_loader.step_buffer[vg_loader.step_count -1 -i];
 
+      vg_info( " -> %p\n", step->fn_free );
       step->fn_free( step->data );
    }
 
-   free( vg_loader.step_buffer );
+   vg_info( "done\n" );
 }
 
-static enum loader_status vg_loader_status(void)
-{
-   enum loader_status answer;
-
-   vg_mutex_lock( &vg_loader.mux_status );
-   answer = vg_loader.status;
-   vg_mutex_unlock( &vg_loader.mux_status );
-
-   return answer;
-}
-
-static float hue_to_rgb( float p, float q, float t )
+VG_STATIC float hue_to_rgb( float p, float q, float t )
 {
    if(t < 0.0f) t += 1.0f;
    if(t > 1.0f) t -= 1.0f;
@@ -82,36 +113,22 @@ static float hue_to_rgb( float p, float q, float t )
    return p;
 }
 
-static void vg_loader_render(void)
+VG_STATIC void vg_render_log(void)
 {
-   float h = vg_fractf(vg_time*0.1),
-         s = 0.2f,
-         l = 0.1f, //* (0.5f+vg_fractf(vg_time*40.0)*0.5f),
-         q = l < 0.5f ? l * (1.0f + s) : l + s - l * s,
-         p = 2.0f * l - q,
-         r = hue_to_rgb( p, q, h + 1.0f/3.0f ),
-         g = hue_to_rgb( p, q, h ),
-         b = hue_to_rgb( p, q, h - 1.0f/3.0f );
-
-   glClearColor( r, g, b, 1.0f );
-   glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
-
-
-
-   ui_begin( &ui_global_ctx, vg_window_x, vg_window_y );
-
+   ui_begin( vg.window_x, vg.window_y );
+   vg_mutex_lock( &log_print_mutex );
 
    int const fh = 14;
-   int lines_screen_max = ((vg_window_y/fh)-2),
+   int lines_screen_max = ((vg.window_y/fh)-2),
        lines_max_draw = VG_MIN( lines_screen_max, vg_list_size(vg_log.buffer) ),
        lines_to_draw  = VG_MIN( lines_max_draw, vg_log.buffer_line_count );
 
        int ptr = vg_log.buffer_line_current;
        
-       ui_global_ctx.cursor[0] = 0;
-       ui_global_ctx.cursor[1] = lines_to_draw*fh;
-   ui_global_ctx.cursor[3] = fh;
-       ui_fill_x( &ui_global_ctx );
+       vg_uictx.cursor[0] = 0;
+       vg_uictx.cursor[1] = lines_to_draw*fh;
+   vg_uictx.cursor[3] = fh;
+       ui_fill_x();
 
    for( int i=0; i<lines_to_draw; i ++ )
    {
@@ -120,76 +137,82 @@ static void vg_loader_render(void)
       if( ptr < 0 )
          ptr = vg_list_size( vg_log.buffer )-1;
       
-      ui_text( &ui_global_ctx, ui_global_ctx.cursor, 
-            vg_log.buffer[ptr], vg_console.scale, 0 );
-
-      ui_global_ctx.cursor[1] -= fh*vg_console.scale;
+      ui_text( vg_uictx.cursor, vg_log.buffer[ptr], 1, 0 );
+      vg_uictx.cursor[1] -= fh;
    }
 
-   ui_resolve( &ui_global_ctx );
-   ui_draw( &ui_global_ctx, NULL );
+   vg_mutex_unlock( &log_print_mutex );
+
+   ui_resolve();
+   ui_draw( NULL );
 }
 
+VG_STATIC void vg_loader_render(void)
+{
+   glViewport( 0,0, vg.window_x, vg.window_y );
+   glBindFramebuffer( GL_FRAMEBUFFER, 0 );
+   glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
+   glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
 
-static int vg_load_full(void);
+   glUseProgram( _shader_loader.id );
+       glUniform1f( glGetUniformLocation( _shader_loader.id, "uTime" ), vg.time );
 
-static void vg_loader_thread(void * nothing)
-{
-   /* Run client loader */
-   int res = vg_load_full();
+   glBindVertexArray( vg_loader.vao );
+   glDrawArrays( GL_TRIANGLES, 0, 6 );
 
-   /* Propogate status */
-   vg_mutex_lock( &vg_loader.mux_status );
-   if( res )
-   {
-      vg_loader.status = k_loader_status_complete;
-   }
-   else
-   {
-      vg_loader.status = k_loader_status_fail;
-   }
-   vg_mutex_unlock( &vg_loader.mux_status );
+   vg_render_log();
+}
 
-   vg_semaphore_post( &vg_loader.sem_loading );
+
+VG_STATIC void vg_load_full(void);
+
+VG_STATIC void vg_loader_thread(void * nothing)
+{
+   vg_thread_info.gl_context_level = 0;
+   vg_thread_info.purpose = k_thread_purpose_loader;
+   vg_set_thread_name( "[vg] Loader" );
+
+   /* Run client loader */
+   vg_load_full();
+   vg_semaphore_post( &vg.sem_loader );
 }
 
-static void vg_loader_start(void)
+VG_STATIC void vg_loader_start(void)
 {
+   vg_semaphore_wait( &vg.sem_loader );
    vg_thread_run( vg_loader_thread, NULL );
 }
 
-static void vg_free_libc_malloced( void *data )
+/* this is maybe probably unused now */
+VG_STATIC void vg_free_libc_malloced( void *data )
 {
    free( data );
 }
 
-static int vg_loader_push_free_step( struct loader_free_step step )
+VG_STATIC void vg_loader_push_free_step( struct loader_free_step step )
 {
-   void *buf = buffer_reserve( vg_loader.step_buffer, vg_loader.step_count, 
-                              &vg_loader.step_cap, 1, 
-                              sizeof( struct loader_free_step ) );
-
-   if( !buf )
-      return 0;
+   if( vg_loader.step_count == vg_list_size(vg_loader.step_buffer) )
+      vg_fatal_exit_loop( "Too many free steps" );
 
-   vg_loader.step_buffer = buf;
    vg_loader.step_buffer[ vg_loader.step_count ++ ] = step;
-   return 1;
 }
+
 /*
- * Schedule something to be freed
+ * Schedule something to be ran now, freed later. Checks in with engine status
  */
-__attribute__((warn_unused_result))
-static int vg_loader_highwater( void( *fn_free )(void *), void *data )
+VG_STATIC void vg_loader_highwater( void( *fn_load )(void), 
+                                 void( *fn_free )(void *), void *data )
 {
+   if( fn_load )
+      fn_load();
+
    if( fn_free )
    {
       struct loader_free_step step;
       step.data = data;
       step.fn_free = fn_free;
 
-      if( !vg_loader_push_free_step( step ) )
-         return 0;
+      vg_loader_push_free_step( step );
    }
    else
    {
@@ -199,152 +222,11 @@ static int vg_loader_highwater( void( *fn_free )(void *), void *data )
          step.data = data;
          step.fn_free = vg_free_libc_malloced;
 
-         if( !vg_loader_push_free_step( step ) )
-            return 0;
+         vg_loader_push_free_step( step );
       }
    }
 
-   vg_mutex_lock( &vg.mux_engine_status );
-
-   if( !vg.engine_running )
-   {
-      vg_mutex_unlock( &vg.mux_engine_status );
-      return 0;
-   }
-
-   vg_mutex_unlock( &vg.mux_engine_status );
-   return 1;
+   vg_ensure_engine_running();
 }
 
 #endif /* VG_LOADER_H */
-
-#if 0
-#ifndef LOADER_H
-#define LOADER_H
-
-#include "common.h"
-
-static struct loader
-{
-   MUTEX_TYPE mux;
-
-   struct loader_step
-   {
-      int (*fn_load)(void);
-      void (*fn_free)(void);
-
-      int require_opengl;
-      const char *name;
-   }
-   *step_buffer;
-   u32 step_count, step_cap, step_action, 
-       low_water_mark; /* What is the minumum number of systems we can have? */
-
-   enum loader_status
-   {
-      k_loader_status_loading,
-      k_loader_status_complete,
-      k_loader_status_fail
-   }
-   status;
-
-   int cancel;
-}
-loader;
-
-static void loader_add_stage( struct loader_step step )
-{
-   loader.step_buffer = buffer_reserve( loader.step_buffer, loader.step_count, 
-                                        &loader.step_cap,
-                                        1, sizeof( struct loader_step ) );
-
-   loader.step_buffer[ loader.step_count ++ ] = step;
-}
-
-static void loader_insert_stage( struct loader_step step )
-{
-   
-}
-
-static void loader_cancel(void)
-{
-   MUTEX_LOCK( loader.mux );
-   loader.cancel = 1;
-   MUTEX_UNLOCK( loader.mux );
-}
-
-static void loader_worker_thread( void *nothing )
-{
-   while(1)
-   {
-      vg_sleep_ms( 1000.0 );
-      vg_info( "... loader ....\n" );
-
-      if( loader.cancel )
-      {
-         return;
-      }
-   }
-}
-
-static void loader_begin(void)
-{
-   if( loader.step_count == 0 )
-   {
-      loader.status = k_loader_status_complete;
-      return;
-   }
-
-   loader.status = k_loader_status_loading;
-   vg_thread_run( loader_worker_thread, NULL );
-}
-
-static void loader_free(void)
-{
-   /* TODO */
-   for( int i=0; i<loader.step_count; i++ )
-   {
-      struct loader_step *step = &loader.step_buffer[ loader.step_count -i -1 ];
-      if( step->fn_free )
-         step->fn_free();
-   }
-}
-
-/*
- * Returns 0 if loading is not happening
- * Returns 1 if we are loading something
- */
-static int loader_update(void)
-{
-   MUTEX_LOCK( loader.mux );
-
-   if( loader.status == k_loader_status_complete )
-   {
-      MUTEX_UNLOCK( loader.mux );
-      return 0;
-   }
-   else
-   {
-      struct loader_step *cstep = &loader.step_buffer[ loader.step_action ];
-
-      if( cstep->require_opengl )
-      {
-         if( !cstep->fn_load() )
-         {
-            loader.cancel = 1;
-            MUTEX_UNLOCK( loader.mux );
-            
-            loader_free();
-            vg_exit();
-         }
-
-         loader.step_action ++;
-      }
-
-      MUTEX_UNLOCK( loader.mux );
-      return 1;
-   }
-}
-
-#endif /* LOADER_H */
-#endif