force model array alignment
authorhgn <hgodden00@gmail.com>
Fri, 24 Nov 2023 10:03:26 +0000 (10:03 +0000)
committerhgn <hgodden00@gmail.com>
Fri, 24 Nov 2023 10:03:26 +0000 (10:03 +0000)
font.h
menu.h
model.h
player_render.c
world_load.c

diff --git a/font.h b/font.h
index 3bda50039a964220b4ff67194568d9f3ce8c39e2..a5449f906a6860225a1d31aa93ceff4859ed568f 100644 (file)
--- a/font.h
+++ b/font.h
@@ -82,11 +82,11 @@ static void font3d_load( font3d *font, const char *mdl_path, void *alloc ){
 
    vg_linear_clear( vg_mem.scratch );
    mdl_array_ptr fonts;
-   mdl_load_array( &font->mdl, &fonts, "ent_font", vg_mem.scratch );
+   MDL_LOAD_ARRAY( &font->mdl, &fonts, ent_font, vg_mem.scratch );
    font->info = *((ent_font *)mdl_arritm(&fonts,0));
 
-   mdl_load_array( &font->mdl, &font->font_variants, "ent_font_variant", alloc);
-   mdl_load_array( &font->mdl, &font->glyphs, "ent_glyph", alloc );
+   MDL_LOAD_ARRAY( &font->mdl, &font->font_variants, ent_font_variant, alloc);
+   MDL_LOAD_ARRAY( &font->mdl, &font->glyphs, ent_glyph, alloc );
 
    vg_linear_clear( vg_mem.scratch );
 
diff --git a/menu.h b/menu.h
index aa8e609b31c0cfbf119f4d5d6a3bfb3bef24223a..53cb52d30dc4010c007adb3665af42dbb491f07b 100644 (file)
--- a/menu.h
+++ b/menu.h
@@ -141,9 +141,9 @@ static void menu_init(void){
 
    vg_linear_clear( vg_mem.scratch );
 
-   mdl_load_array( &menu.model, &menu.items,   "ent_menuitem", alloc );
-   mdl_load_array( &menu.model, &menu.markers, "ent_marker", alloc );
-   mdl_load_array( &menu.model, &menu.cameras, "ent_camera", alloc );
+   MDL_LOAD_ARRAY( &menu.model, &menu.items,   ent_menuitem, alloc );
+   MDL_LOAD_ARRAY( &menu.model, &menu.markers, ent_marker, alloc );
+   MDL_LOAD_ARRAY( &menu.model, &menu.cameras, ent_camera, alloc );
 
    vg_linear_clear( vg_mem.scratch );
 
diff --git a/model.h b/model.h
index da0122397601ecfa84e6b63858858acb13819a09..5608becf70fb90c4621c8d0556cd23a477bffbf0 100644 (file)
--- a/model.h
+++ b/model.h
@@ -63,6 +63,8 @@ struct mdl_vert
 
 #pragma pack(pop)
 
+typedef u32 mdl_indice;
+
 typedef struct mdl_context mdl_context;
 typedef struct mdl_array_ptr mdl_array_ptr;
 typedef struct mdl_vert mdl_vert;
@@ -281,15 +283,6 @@ static void mdl_load_fatal_corrupt( mdl_context *mdl )
  * Model implementation
  */
 
-static u32 mdl_query_array_size( mdl_array *arr )
-{
-   if( arr->item_count ){
-      u32 size = arr->item_size*arr->item_count;
-      return vg_align8(size);
-   }
-   else return 0;
-}
-
 static const char *mdl_pstr( mdl_context *mdl, u32 pstr );
 static 
 void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst )
@@ -307,29 +300,42 @@ void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst )
 
 /* TODO: Rename these */
 static void mdl_load_array_file_buffer( mdl_context *mdl, mdl_array *arr, 
-                                           void *buffer )
+                                        void *buffer, u32 stride )
 {
    if( arr->item_count ){
       fseek( mdl->file, arr->file_offset, SEEK_SET );
-      u64 l = fread( buffer, arr->item_size*arr->item_count, 1, mdl->file );
 
-      if( l != 1 ) mdl_load_fatal_corrupt( mdl );
+      if( stride == arr->item_size ){
+         u64 l = fread( buffer, arr->item_size*arr->item_count, 1, mdl->file );
+         if( l != 1 ) mdl_load_fatal_corrupt( mdl );
+      }
+      else {
+         assert( stride >= arr->item_size );
+
+         for( u32 i=0; i<arr->item_count; i++ ){
+            u64 l = fread( buffer+i*stride, arr->item_size, 1, mdl->file );
+            if( l != 1 ) mdl_load_fatal_corrupt( mdl );
+         }
+      }
    }
 }
 
-static void mdl_load_array_file( mdl_context *mdl, mdl_array_ptr *ptr,
-                                    mdl_array *arr, void *lin_alloc )
+static void mdl_load_array_file( mdl_context *mdl, mdl_array_ptr *ptr, 
+                                 mdl_array *arr, void *lin_alloc, u32 stride )
 {
+   assert( stride >= arr->item_size );
+
    if( arr->item_count ){
-      u32 size = arr->item_size*arr->item_count;
+      u32 size = stride*arr->item_count;
       ptr->data = vg_linear_alloc( lin_alloc, vg_align8(size) );
-      mdl_load_array_file_buffer( mdl, arr, ptr->data );
+      mdl_load_array_file_buffer( mdl, arr, ptr->data, stride );
    }
-   else
+   else{
       ptr->data = NULL;
-
+   }
+   
+   ptr->stride = stride;
    ptr->count = arr->item_count;
-   ptr->stride = arr->item_size;
 }
 
 static void *mdl_arritm( mdl_array_ptr *arr, u32 index )
@@ -355,13 +361,13 @@ static mdl_array *mdl_find_array( mdl_context *mdl, const char *name )
    return NULL;
 }
 
-static int mdl_load_array( mdl_context *mdl, mdl_array_ptr *ptr,
-                              const char *name, void *lin_alloc )
+static int _mdl_load_array( mdl_context *mdl, mdl_array_ptr *ptr,
+                            const char *name, void *lin_alloc, u32 stride )
 {
    mdl_array *arr = mdl_find_array( mdl, name );
 
    if( arr ){
-      mdl_load_array_file( mdl, ptr, arr, lin_alloc );
+      mdl_load_array_file( mdl, ptr, arr, lin_alloc, stride );
       return 1;
    }
    else{
@@ -372,35 +378,35 @@ static int mdl_load_array( mdl_context *mdl, mdl_array_ptr *ptr,
    }
 }
 
-static int mdl_load_mesh_block( mdl_context *mdl, void *lin_alloc )
-{
+#define MDL_LOAD_ARRAY( MDL, PTR, STRUCT, ALLOCATOR ) \
+   _mdl_load_array( MDL, PTR, #STRUCT, ALLOCATOR, sizeof(STRUCT) )
+
+static int mdl_load_mesh_block( mdl_context *mdl, void *lin_alloc ){
    int success = 1;
 
-   success &= mdl_load_array( mdl, &mdl->verts,    "mdl_vert",   lin_alloc );
-   success &= mdl_load_array( mdl, &mdl->indices,  "mdl_indice", lin_alloc );
+   success &= MDL_LOAD_ARRAY( mdl, &mdl->verts,    mdl_vert,   lin_alloc );
+   success &= MDL_LOAD_ARRAY( mdl, &mdl->indices,  mdl_indice, lin_alloc );
 
    return success;
 }
 
-static int mdl_load_metadata_block( mdl_context *mdl, void *lin_alloc )
-{
+static int mdl_load_metadata_block( mdl_context *mdl, void *lin_alloc ){
    int success = 1;
 
-   success &= mdl_load_array( mdl, &mdl->strings,   "strings",      lin_alloc );
-   success &= mdl_load_array( mdl, &mdl->meshs,     "mdl_mesh",     lin_alloc );
-   success &= mdl_load_array( mdl, &mdl->submeshs,  "mdl_submesh",  lin_alloc );
-   success &= mdl_load_array( mdl, &mdl->materials, "mdl_material", lin_alloc );
-   success &= mdl_load_array( mdl, &mdl->textures,  "mdl_texture",  lin_alloc );
-   success &= mdl_load_array( mdl, &mdl->armatures, "mdl_armature", lin_alloc );
-   success &= mdl_load_array( mdl, &mdl->bones,     "mdl_bone",     lin_alloc );
-   success &= mdl_load_array( mdl, &mdl->animations,"mdl_animation",lin_alloc );
+   success &= _mdl_load_array( mdl, &mdl->strings, "strings", lin_alloc, 1 );
+   success &= MDL_LOAD_ARRAY( mdl, &mdl->meshs,     mdl_mesh,     lin_alloc );
+   success &= MDL_LOAD_ARRAY( mdl, &mdl->submeshs,  mdl_submesh,  lin_alloc );
+   success &= MDL_LOAD_ARRAY( mdl, &mdl->materials, mdl_material, lin_alloc );
+   success &= MDL_LOAD_ARRAY( mdl, &mdl->textures,  mdl_texture,  lin_alloc );
+   success &= MDL_LOAD_ARRAY( mdl, &mdl->armatures, mdl_armature, lin_alloc );
+   success &= MDL_LOAD_ARRAY( mdl, &mdl->bones,     mdl_bone,     lin_alloc );
+   success &= MDL_LOAD_ARRAY( mdl, &mdl->animations,mdl_animation,lin_alloc );
 
    return success;
 }
 
-static int mdl_load_animation_block( mdl_context *mdl, void *lin_alloc )
-{
-   return mdl_load_array( mdl, &mdl->keyframes, "mdl_keyframe", lin_alloc );
+static int mdl_load_animation_block( mdl_context *mdl, void *lin_alloc ){
+   return MDL_LOAD_ARRAY( mdl, &mdl->keyframes, mdl_keyframe, lin_alloc );
 }
 
 /*
@@ -428,7 +434,8 @@ static void mdl_open( mdl_context *mdl, const char *path, void *lin_alloc )
       vg_fatal_error( "Legacy model version incompatable" );
    }
 
-   mdl_load_array_file( mdl, &mdl->index, &mdl->info.index, lin_alloc );
+   mdl_load_array_file( mdl, &mdl->index, &mdl->info.index, lin_alloc,
+                        sizeof(mdl_array) );
 
    mdl_array *pack = mdl_find_array( mdl, "pack" );
    if( pack ) mdl->pack_base_offset = pack->file_offset;
@@ -595,15 +602,14 @@ static void async_mdl_load_glmesh( void *payload, u32 size )
                            job->indices, job->indice_count );
 }
 
-/* TODO: Find out if this needs deprecating in favour of the new full loader */
 static void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh )
 {
    mdl_array *arr_vertices = mdl_find_array( mdl, "mdl_vert" );
    mdl_array *arr_indices = mdl_find_array( mdl, "mdl_indice" );
 
    if( arr_vertices && arr_indices ){
-      u32 size_verts   = vg_align8(mdl_query_array_size( arr_vertices )),
-          size_indices = vg_align8(mdl_query_array_size( arr_indices )),
+      u32 size_verts   = vg_align8(sizeof(mdl_vert)*arr_vertices->item_count),
+          size_indices = vg_align8(sizeof(mdl_indice)*arr_indices->item_count),
           size_hdr     = vg_align8(sizeof(struct payload_glmesh_load)),
           total        = size_hdr + size_verts + size_indices;
 
@@ -618,8 +624,10 @@ static void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh )
       job->vertex_count = arr_vertices->item_count;
       job->indice_count = arr_indices->item_count;
 
-      mdl_load_array_file_buffer( mdl, arr_vertices, job->verts );
-      mdl_load_array_file_buffer( mdl, arr_indices, job->indices );
+      mdl_load_array_file_buffer( mdl, arr_vertices, 
+                                  job->verts, sizeof(mdl_vert) );
+      mdl_load_array_file_buffer( mdl, arr_indices, job->indices, 
+                                  sizeof(mdl_indice) );
 
       /*
        * Unpack the indices (if there are meshes)
index b0f35e1d81151f0bb7e50bb9a09eb82bc06898c9..b0416f5f3e927c946ebbe7a7ea6b46719dea1c61 100644 (file)
@@ -15,6 +15,9 @@
 #include "shaders/model_board_view.h"
 #include "depth_compare.h"
 
+#include "network.h"
+#include "player_remote.h"
+
 static void player_load_animation_reference( const char *path ){
    mdl_context *meta = &localplayer.skeleton_meta;
    mdl_open( meta, path, vg_mem.rtmemory );
@@ -86,7 +89,7 @@ static void player_board_load( struct player_board *board,
    dynamic_model_load( &ctx, &board->mdl, path );
 
    mdl_array_ptr markers;
-   mdl_load_array( &ctx, &markers, "ent_marker", vg_mem.scratch );
+   MDL_LOAD_ARRAY( &ctx, &markers, ent_marker, vg_mem.scratch );
 
    /* TODO: you get put into a new section, the above is standard mdl loads. */
    for( int i=0; i<4; i++ )
index d7f4afb8a3b3fd3088f129760fa22df40cacbbd9..18f405e03735e2cd3ac5caf7fe010c0a26488c08 100644 (file)
@@ -45,33 +45,32 @@ static void world_instance_load_mdl( u32 instance_id, const char *path ){
    mdl_load_animation_block( meta, world->heap );
    mdl_load_mesh_block( meta, world->heap );
 
-   /* TODO: Make this a table? */
-   mdl_load_array( meta, &world->ent_gate,      "ent_gate",       heap );
-   mdl_load_array( meta, &world->ent_camera,    "ent_camera",     heap );
-   mdl_load_array( meta, &world->ent_spawn,     "ent_spawn",      heap );
-   mdl_load_array( meta, &world->ent_light,     "ent_light",      heap );
-   mdl_load_array( meta, &world->ent_route_node,"ent_route_node", heap );
-   mdl_load_array( meta, &world->ent_path_index,"ent_path_index", heap );
-   mdl_load_array( meta, &world->ent_checkpoint,"ent_checkpoint", heap );
-   mdl_load_array( meta, &world->ent_route,     "ent_route",      heap );
-   mdl_load_array( meta, &world->ent_water,     "ent_water",      heap );
-   mdl_load_array( meta, &world->ent_audio_clip,"ent_audio_clip", heap );
-   mdl_load_array( meta, &world->ent_audio,     "ent_audio",      heap );
-   mdl_load_array( meta, &world->ent_volume,    "ent_volume",     heap );
-   mdl_load_array( meta, &world->ent_traffic,   "ent_traffic",    heap );
-   mdl_load_array( meta, &world->ent_marker,    "ent_marker",     heap );
-   mdl_load_array( meta, &world->ent_skateshop, "ent_skateshop",  heap );
-   mdl_load_array( meta, &world->ent_swspreview,"ent_swspreview", heap );
-   mdl_load_array( meta, &world->ent_ccmd,      "ent_ccmd",       heap );
-   mdl_load_array( meta, &world->ent_objective, "ent_objective",  heap );
-   mdl_load_array( meta, &world->ent_challenge, "ent_challenge",  heap );
-   mdl_load_array( meta, &world->ent_relay,     "ent_relay",      heap );
-   mdl_load_array( meta, &world->ent_cubemap,   "ent_cubemap",    heap );
-   mdl_load_array( meta, &world->ent_miniworld, "ent_miniworld",  heap );
-   mdl_load_array( meta, &world->ent_prop,      "ent_prop",  heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_gate,      ent_gate,       heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_camera,    ent_camera,     heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_spawn,     ent_spawn,      heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_light,     ent_light,      heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_route_node,ent_route_node, heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_path_index,ent_path_index, heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_checkpoint,ent_checkpoint, heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_route,     ent_route,      heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_water,     ent_water,      heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_audio_clip,ent_audio_clip, heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_audio,     ent_audio,      heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_volume,    ent_volume,     heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_traffic,   ent_traffic,    heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_marker,    ent_marker,     heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_skateshop, ent_skateshop,  heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_swspreview,ent_swspreview, heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_ccmd,      ent_ccmd,       heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_objective, ent_objective,  heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_challenge, ent_challenge,  heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_relay,     ent_relay,      heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_cubemap,   ent_cubemap,    heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_miniworld, ent_miniworld,  heap );
+   MDL_LOAD_ARRAY( meta, &world->ent_prop,      ent_prop,       heap );
 
    mdl_array_ptr infos;
-   mdl_load_array( meta, &infos, "ent_worldinfo", vg_mem.scratch );
+   MDL_LOAD_ARRAY( meta, &infos, ent_worldinfo, vg_mem.scratch );
 
    world->skybox = k_skybox_default;
    if( mdl_arrcount(&infos) ){
@@ -109,10 +108,13 @@ static void world_instance_load_mdl( u32 instance_id, const char *path ){
     *   - this is overriden by the save state when(if) it loads */
    ent_spawn *rp = world_find_spawn_by_name( world, "start" );
    if( !rp ) rp = world_find_closest_spawn( world, (v3f){0.0f,0.0f,0.0f} );
-
-   /* TODO: fallback to searching for a safe location using raycasts */
-   assert(rp);
-   v3_copy( rp->transform.co, world->player_co );
+   if( rp )
+      v3_copy( rp->transform.co, world->player_co );
+   else{
+      /* FIXME: we need to find a safe place to put the player_co using
+       * raycasts. */
+      v3_zero( world->player_co );
+   }
 
    /* allocate leaderboard buffers */
    u32 bs = mdl_arrcount(&world->ent_route)*sizeof(struct leaderboard_cache);