chaos caused by async
[carveJwlIkooP6JGAAIwe30JlM.git] / world_render.h
index b1972ba546cf7cafd4774294559c7a39eaabbfc2..7fb47b0dc2cb580c9ee3fd1a8521d04643018412 100644 (file)
@@ -8,62 +8,60 @@
 #include "camera.h"
 #include "world.h"
 
-/* FIXME */
-VG_STATIC vg_tex2d tex_terrain_noise = { .path = "textures/garbage.qoi",
-                                         .flags = VG_TEXTURE_NEAREST };
+VG_STATIC GLuint tex_terrain_noise;
 
-VG_STATIC void world_render_init(void)
+VG_STATIC void async_world_render_init( void *payload, u32 size )
 {
-   vg_info( "Loading default world textures\n" );
-
-   vg_acquire_thread_sync();
-   {
-      vg_tex2d_init( (vg_tex2d *[]){ &tex_terrain_noise }, 1 );
-
-
-      vg_info( "Allocate uniform buffers\n" );
-      for( int i=0; i<4; i++ )
-      {
-         world_instance *world = &world_global.worlds[i];
-         world->ubo_bind_point = i;
+   vg_info( "Allocate uniform buffers\n" );
+   for( int i=0; i<4; i++ ){
+      world_instance *world = &world_global.worlds[i];
+      world->ubo_bind_point = i;
+
+      glGenBuffers( 1, &world->ubo_lighting );
+      glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
+      glBufferData( GL_UNIFORM_BUFFER, sizeof(struct ub_world_lighting), 
+                    NULL, GL_DYNAMIC_DRAW );
+
+      glBindBufferBase( GL_UNIFORM_BUFFER, i, world->ubo_lighting );
+      VG_CHECK_GL_ERR();
+   }
 
-         glGenBuffers( 1, &world->ubo_lighting );
-         glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
-         glBufferData( GL_UNIFORM_BUFFER, sizeof(struct ub_world_lighting), 
-                       NULL, GL_DYNAMIC_DRAW );
+   vg_info( "Allocate frame buffers\n" );
+   for( int i=0; i<4; i++ ){
+      world_instance *world = &world_global.worlds[i];
+      struct framebuffer *fb = &world->heightmap;
+
+      fb->display_name = NULL;
+      fb->link = NULL;
+      fb->fixed_w = 1024;
+      fb->fixed_h = 1024;
+      fb->resolution_div = 0;
+
+      fb->attachments[0].display_name     = NULL;
+      fb->attachments[0].purpose = k_framebuffer_attachment_type_texture;
+      fb->attachments[0].internalformat   = GL_RG16F;
+      fb->attachments[0].format           = GL_RG;
+      fb->attachments[0].type             = GL_FLOAT;
+      fb->attachments[0].attachment       = GL_COLOR_ATTACHMENT0;
+
+      fb->attachments[1].purpose = k_framebuffer_attachment_type_none;
+      fb->attachments[2].purpose = k_framebuffer_attachment_type_none;
+      fb->attachments[3].purpose = k_framebuffer_attachment_type_none;
+      fb->attachments[4].purpose = k_framebuffer_attachment_type_none;
+
+      render_fb_allocate( fb );
+   }
+}
 
-         glBindBufferBase( GL_UNIFORM_BUFFER, i, world->ubo_lighting );
-         VG_CHECK_GL_ERR();
-      }
+VG_STATIC void world_render_init(void)
+{
+   vg_info( "Loading default world textures\n" );
 
-      vg_info( "Allocate frame buffers\n" );
-      for( int i=0; i<4; i++ )
-      {
-         world_instance *world = &world_global.worlds[i];
-         struct framebuffer *fb = &world->heightmap;
-
-         fb->display_name = NULL;
-         fb->link = NULL;
-         fb->fixed_w = 1024;
-         fb->fixed_h = 1024;
-         fb->resolution_div = 0;
-
-         fb->attachments[0].display_name     = NULL;
-         fb->attachments[0].purpose = k_framebuffer_attachment_type_texture;
-         fb->attachments[0].internalformat   = GL_RG16F;
-         fb->attachments[0].format           = GL_RG;
-         fb->attachments[0].type             = GL_FLOAT;
-         fb->attachments[0].attachment       = GL_COLOR_ATTACHMENT0;
-
-         fb->attachments[1].purpose = k_framebuffer_attachment_type_none;
-         fb->attachments[2].purpose = k_framebuffer_attachment_type_none;
-         fb->attachments[3].purpose = k_framebuffer_attachment_type_none;
-         fb->attachments[4].purpose = k_framebuffer_attachment_type_none;
-
-         render_fb_allocate( fb );
-      }
-   }
-   vg_release_thread_sync();
+   vg_tex2d_load_qoi_async_file( "textures/garbage.qoi", 
+                                 VG_TEX2D_NEAREST|VG_TEX2D_REPEAT, 
+                                 &tex_terrain_noise );
+   vg_async_item *call = vg_async_alloc(0);
+   vg_async_dispatch( call, async_world_render_init );
 }
 
 VG_STATIC void world_link_lighting_ub( world_instance *world, GLuint shader )
@@ -106,27 +104,30 @@ VG_STATIC void render_world_depth( world_instance *world, camera *cam );
 
 VG_STATIC void bind_terrain_noise(void)
 {
-   vg_tex2d_bind( &tex_terrain_noise, 0 );
+   glActiveTexture( GL_TEXTURE0 );
+   glBindTexture( GL_TEXTURE_2D, tex_terrain_noise );
 }
 
-typedef void (*func_bind_point)( world_instance *world, 
-                                 struct world_surface *mat );
+struct world_pass{
+   camera *cam;
+   enum mdl_shader shader;
+   enum geo_type geo_type;
 
-VG_STATIC void world_render_if( world_instance *world,
-                                enum mdl_shader shader, 
-                                enum geo_type geo_type,
-                                func_bind_point bind_point )
+   void (*fn_bind_textures)( world_instance *world, 
+                             struct world_surface *mat );
+   void (*fn_set_mdl)( m4x3f mdl );
+   void (*fn_set_uPvmPrev)( m4x4f pvm );
+};
+
+VG_STATIC void world_render_if( world_instance *world, struct world_pass *pass )
 {
-   
-   for( int i=0; i<world->surface_count; i++ )
-   {
+   for( int i=0; i<world->surface_count; i++ ){
       struct world_surface *mat = &world->surfaces[i];
 
-      if( mat->info.shader == shader )
-      {
+      if( mat->info.shader == pass->shader ){
          mdl_submesh *sm;
 
-         if( geo_type == k_geo_type_solid )
+         if( pass->geo_type == k_geo_type_solid )
             sm = &mat->sm_geo;
          else
             sm = &mat->sm_no_collide;
@@ -134,28 +135,55 @@ VG_STATIC void world_render_if( world_instance *world,
          if( !sm->indice_count )
             continue;
 
-         bind_point( world, mat );
+         m4x3f mmdl;
+         m4x3_identity( mmdl );
+         pass->fn_set_mdl( mmdl );
+         pass->fn_set_uPvmPrev( pass->cam->mtx_prev.pv );
+
+         pass->fn_bind_textures( world, mat );
          mdl_draw_submesh( sm );
+
+         for( u32 j=0; j<mdl_arrcount( &world->ent_traffic ); j++ ){
+            ent_traffic *traffic = mdl_arritm( &world->ent_traffic, j );
+            
+            for( u32 k=0; k<traffic->submesh_count; k++ ){
+               sm = mdl_arritm( &world->meta.submeshs, 
+                                 traffic->submesh_start+k );
+
+               q_m3x3( traffic->transform.q, mmdl );
+               v3_copy( traffic->transform.co, mmdl[3] );
+
+               m4x4f m4mdl;
+               m4x3_expand( mmdl, m4mdl );
+               m4x4_mul( pass->cam->mtx_prev.pv, m4mdl, m4mdl );
+
+               pass->fn_set_mdl( mmdl );
+               pass->fn_set_uPvmPrev( m4mdl );
+
+               mdl_draw_submesh( sm );
+            }
+         }
       }
    }
 }
 
 VG_STATIC 
-void world_render_both_stages(  world_instance *world,
-                                enum mdl_shader shader,
-                                func_bind_point bind_point )
+void world_render_both_stages( world_instance *world, struct world_pass *pass )
 {
    mesh_bind( &world->mesh_geo );
-   world_render_if( world, shader, k_geo_type_solid, bind_point );
+   pass->geo_type = k_geo_type_solid;
+   world_render_if( world, pass );
 
    glDisable( GL_CULL_FACE );
    mesh_bind( &world->mesh_no_collide );
-   world_render_if( world, shader, k_geo_type_nonsolid, bind_point );
+   pass->geo_type = k_geo_type_nonsolid;
+   world_render_if( world, pass );
    glEnable( GL_CULL_FACE );
 }
 
 VG_STATIC void bindpoint_diffuse_texture1( world_instance *world,
                                            struct world_surface *mat )
+                                         
 {
    glActiveTexture( GL_TEXTURE1 );
    glBindTexture( GL_TEXTURE_2D, world->textures[ mat->info.tex_diffuse ] );
@@ -163,9 +191,6 @@ VG_STATIC void bindpoint_diffuse_texture1( world_instance *world,
 
 VG_STATIC void render_world_vb( world_instance *world, camera *cam )
 {
-   m4x3f identity_matrix;
-   m4x3_identity( identity_matrix );
-
    shader_scene_vertex_blend_use();
    shader_scene_vertex_blend_uTexGarbage(0);
    shader_scene_vertex_blend_uTexGradients(1);
@@ -177,27 +202,29 @@ VG_STATIC void render_world_vb( world_instance *world, camera *cam )
    world_bind_light_index( world, _shader_scene_vertex_blend.id,
                                 _uniform_scene_vertex_blend_uLightsIndex, 4 );
 
-   vg_tex2d_bind( &tex_terrain_noise, 0 );
+   glActiveTexture( GL_TEXTURE0 );
+   glBindTexture( GL_TEXTURE_2D, tex_terrain_noise );
 
    shader_scene_vertex_blend_uPv( cam->mtx.pv );
-   shader_scene_vertex_blend_uPvmPrev( cam->mtx_prev.pv );
-   shader_scene_vertex_blend_uMdl( identity_matrix );
    shader_scene_vertex_blend_uCamera( cam->transform[3] );
 
-   world_render_both_stages( world, k_shader_standard_vertex_blend,
-                             bindpoint_diffuse_texture1 );
+   struct world_pass pass = {
+      .shader = k_shader_standard_vertex_blend,
+      .cam = cam,
+      .fn_bind_textures = bindpoint_diffuse_texture1,
+      .fn_set_mdl = shader_scene_vertex_blend_uMdl,
+      .fn_set_uPvmPrev = shader_scene_vertex_blend_uPvmPrev,
+   };
+
+   world_render_both_stages( world, &pass );
 }
 
 VG_STATIC void render_world_standard( world_instance *world, camera *cam )
 {
-   m4x3f identity_matrix;
-   m4x3_identity( identity_matrix );
-
    shader_scene_standard_use();
    shader_scene_standard_uTexGarbage(0);
    shader_scene_standard_uTexMain(1);
    shader_scene_standard_uPv( cam->mtx.pv );
-   shader_scene_standard_uPvmPrev( cam->mtx_prev.pv );
 
    world_link_lighting_ub( world, _shader_scene_standard.id );
    world_bind_position_texture( world, _shader_scene_standard.id, 
@@ -208,24 +235,25 @@ VG_STATIC void render_world_standard( world_instance *world, camera *cam )
                                 _uniform_scene_standard_uLightsIndex, 4 );
 
    bind_terrain_noise();
-
-   shader_scene_standard_uMdl( identity_matrix );
    shader_scene_standard_uCamera( cam->transform[3] );
    
-   world_render_both_stages( world, k_shader_standard,
-                             bindpoint_diffuse_texture1 );
+   struct world_pass pass = {
+      .shader = k_shader_standard,
+      .cam = cam,
+      .fn_bind_textures = bindpoint_diffuse_texture1,
+      .fn_set_mdl = shader_scene_standard_uMdl,
+      .fn_set_uPvmPrev = shader_scene_standard_uPvmPrev,
+   };
+
+   world_render_both_stages( world, &pass );
 }
 
 VG_STATIC void render_world_alphatest( world_instance *world, camera *cam )
 {
-   m4x3f identity_matrix;
-   m4x3_identity( identity_matrix );
-
    shader_scene_standard_alphatest_use();
    shader_scene_standard_alphatest_uTexGarbage(0);
    shader_scene_standard_alphatest_uTexMain(1);
    shader_scene_standard_alphatest_uPv( cam->mtx.pv );
-   shader_scene_standard_alphatest_uPvmPrev( cam->mtx_prev.pv );
 
    world_link_lighting_ub( world, _shader_scene_standard_alphatest.id );
    world_bind_position_texture( world, _shader_scene_standard_alphatest.id, 
@@ -238,13 +266,20 @@ VG_STATIC void render_world_alphatest( world_instance *world, camera *cam )
 
    bind_terrain_noise();
 
-   shader_scene_standard_alphatest_uMdl( identity_matrix );
+   
    shader_scene_standard_alphatest_uCamera( cam->transform[3] );
 
    glDisable(GL_CULL_FACE);
-   
-   world_render_both_stages( world, k_shader_standard_cutout,
-                             bindpoint_diffuse_texture1 );
+
+   struct world_pass pass = {
+      .shader = k_shader_standard_cutout,
+      .cam = cam,
+      .fn_bind_textures = bindpoint_diffuse_texture1,
+      .fn_set_mdl = shader_scene_standard_alphatest_uMdl,
+      .fn_set_uPvmPrev = shader_scene_standard_alphatest_uPvmPrev,
+   };
+
+   world_render_both_stages( world, &pass );
 
    glEnable(GL_CULL_FACE);
 }
@@ -261,9 +296,6 @@ VG_STATIC void bindpoint_terrain( world_instance *world,
 
 VG_STATIC void render_terrain( world_instance *world, camera *cam )
 {
-   m4x3f identity_matrix;
-   m4x3_identity( identity_matrix );
-
    shader_scene_terrain_use();
    shader_scene_terrain_uTexGarbage(0);
    shader_scene_terrain_uTexGradients(1);
@@ -276,15 +308,21 @@ VG_STATIC void render_terrain( world_instance *world, camera *cam )
    world_bind_light_index( world, _shader_scene_terrain.id,
                            _uniform_scene_terrain_uLightsIndex, 4 );
 
-   vg_tex2d_bind( &tex_terrain_noise, 0 );
+   glActiveTexture( GL_TEXTURE0 );
+   glBindTexture( GL_TEXTURE_2D, tex_terrain_noise );
 
    shader_scene_terrain_uPv( cam->mtx.pv );
-   shader_scene_terrain_uPvmPrev( cam->mtx_prev.pv );
-
-   shader_scene_terrain_uMdl( identity_matrix );
    shader_scene_terrain_uCamera( cam->transform[3] );
 
-   world_render_both_stages( world, k_shader_terrain_blend, bindpoint_terrain );
+   struct world_pass pass = {
+      .shader = k_shader_terrain_blend,
+      .cam = cam,
+      .fn_bind_textures = bindpoint_terrain,
+      .fn_set_mdl = shader_scene_terrain_uMdl,
+      .fn_set_uPvmPrev = shader_scene_terrain_uPvmPrev,
+   };
+
+   world_render_both_stages( world, &pass );
 }
 
 VG_STATIC void render_sky( world_instance *world, camera *cam )
@@ -323,7 +361,8 @@ VG_STATIC void render_sky( world_instance *world, camera *cam )
    shader_model_sky_uTexGarbage(0);
    world_link_lighting_ub( world, _shader_model_sky.id );
 
-   vg_tex2d_bind( &tex_terrain_noise, 0 );
+   glActiveTexture( GL_TEXTURE0 );
+   glBindTexture( GL_TEXTURE_2D, tex_terrain_noise );
 
    glDepthMask( GL_FALSE );
    glDisable( GL_DEPTH_TEST );
@@ -339,7 +378,6 @@ VG_STATIC void render_world_gates( world_instance *world, camera *cam,
                                    int layer_depth )
 {
    float closest = INFINITY;
-
    struct ent_gate *gate = NULL;
 
    for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
@@ -387,8 +425,7 @@ VG_STATIC void world_prerender( world_instance *world )
    state->g_debug_indices = k_debug_light_indices;
    state->g_light_preview = k_light_preview;
    state->g_debug_complexity = k_debug_light_complexity;
-
-   state->g_time_of_day = vg_fractf( g_time );
+state->g_time_of_day = vg_fractf( g_time );
    state->g_day_phase   = cosf( state->g_time_of_day * VG_PIf * 2.0f );
    state->g_sunset_phase= cosf( state->g_time_of_day * VG_PIf * 4.0f + VG_PIf );