enforce alignment
authorhgn <hgodden00@gmail.com>
Mon, 26 Dec 2022 15:39:33 +0000 (15:39 +0000)
committerhgn <hgodden00@gmail.com>
Mon, 26 Dec 2022 15:39:33 +0000 (15:39 +0000)
build.c
bvh.h
common.h
model.h
shaders/terrain.h
shaders/terrain_fast.fs [new file with mode: 0644]

diff --git a/build.c b/build.c
index c0a79b654a6d36fe94deb373207fbd542ce2ec09..fe51c054436b0fb8f0a31e18f2c4a4ce43a3edec 100644 (file)
--- a/build.c
+++ b/build.c
@@ -128,7 +128,7 @@ void build_shaders(void)
    _shader( "standard",  "shaders/standard.vs",  "shaders/standard.fs" );
    _shader( "vblend",    "shaders/standard.vs",  "shaders/vblend.fs" );
    _shader( "scoretext", "shaders/scoretext.vs", "shaders/vblend.fs" );
-   _shader( "terrain",   "shaders/standard.vs",  "shaders/terrain.fs" );
+   _shader( "terrain",   "shaders/standard.vs",  "shaders/terrain_fast.fs" );
    _shader( "alphatest", "shaders/standard.vs",  "shaders/std_alphatest.fs" );
    _shader( "route",     "shaders/standard.vs",  "shaders/route.fs" );
    _shader( "menu",      "shaders/standard.vs",  "shaders/menu.fs" );
diff --git a/bvh.h b/bvh.h
index 491756acb0db460df2d5e697002b9b5e5e560169..41a4108c908ff345a06978c39c313b06a06bc27a 100644 (file)
--- a/bvh.h
+++ b/bvh.h
@@ -155,7 +155,7 @@ VG_STATIC bh_tree *bh_create( void *lin_alloc, bh_system *system,
    }
 
    u32 totsize = sizeof(bh_tree) + sizeof(bh_node)*(item_count*2-1);
-   bh_tree *bh = vg_linear_alloc( lin_alloc, totsize );
+   bh_tree *bh = vg_linear_alloc( lin_alloc, vg_align8(totsize) );
    bh->system = system;
    bh->user = user;
    bh->max_per_leaf = max_per_leaf;
index bac81a78aa09ae898a0e018b9326d1fd9ab0632b..bb70596c8c30334587a45f47ee9e4c19dd0e4b58 100644 (file)
--- a/common.h
+++ b/common.h
@@ -8,7 +8,8 @@
 #define VG_TIMESTEP_FIXED (1.0/60.0)
 #define VG_3D
 #define VG_GAME
-#define VG_STATIC static
+//#define VG_STATIC static
+#define VG_STATIC
 #define VG_FRAMEBUFFER_RESIZE 1
 #include "vg/vg.h"
 #include "submodules/anyascii/impl/c/anyascii.c"
diff --git a/model.h b/model.h
index 9bdc2bca58b32c8563cb14dbe6a6be77fd867e9e..be0666af89e530fdfc9003b786c485ee87b702f2 100644 (file)
--- a/model.h
+++ b/model.h
@@ -449,8 +449,8 @@ VG_STATIC void mdl_load_mesh_data( mdl_context *mdl, void *lin_alloc )
 {
    assert( mdl->file );
    
-   u64 size_verts = mdl->info.vertex_count * sizeof(mdl_vert),
-       size_index = mdl->info.indice_count * sizeof(u32);
+   u64 size_verts = vg_align8( mdl->info.vertex_count * sizeof(mdl_vert) ),
+       size_index = vg_align8( mdl->info.indice_count * sizeof(u32) );
 
    mdl->vertex_buffer = vg_linear_alloc( lin_alloc, size_verts );
    mdl->index_buffer  = vg_linear_alloc( lin_alloc, size_index );
@@ -479,7 +479,7 @@ VG_STATIC void mdl_load_anim_data( mdl_context *mdl, void *lin_alloc )
    if( mdl->info.keyframe_count == 0 )
       return;
    
-   u64 size_kf = mdl->info.keyframe_count * sizeof(mdl_keyframe);
+   u64 size_kf = vg_align8( mdl->info.keyframe_count * sizeof(mdl_keyframe) );
    mdl->keyframe_buffer = vg_linear_alloc( lin_alloc, size_kf );
                                                     
    fseek( mdl->file, mdl->info.keyframe_offset, SEEK_SET );
@@ -500,7 +500,7 @@ VG_STATIC void mdl_load_pack_data( mdl_context *mdl, void *lin_alloc )
    if( mdl->info.pack_size == 0 )
       return;
    
-   mdl->pack = vg_linear_alloc( lin_alloc, mdl->info.pack_size );
+   mdl->pack = vg_linear_alloc( lin_alloc, vg_align8( mdl->info.pack_size ) );
    fseek( mdl->file, mdl->info.pack_offset, SEEK_SET );
 
    u64 l = fread( mdl->pack, mdl->info.pack_size, 1, mdl->file );
@@ -525,7 +525,7 @@ VG_STATIC mdl_context *mdl_load_full( void *lin_alloc, const char *path )
    mdl_open( &temp_ctx, path );
 
    /* create allocator */
-   u32 tot_size = temp_ctx.info.file_length + sizeof( mdl_context );
+   u32 tot_size = temp_ctx.info.file_length + sizeof( mdl_context ) + 64;
    void *data = vg_create_linear_allocator( lin_alloc, tot_size, 
                                             VG_MEMORY_SYSTEM );
 
index b2730fba9ebc4ab69ed67f166affcba6aefada36..8b9452e09c96bb0f65adf1ed11a9a4f5db509de5 100644 (file)
@@ -218,8 +218,8 @@ static struct vg_shader _shader_terrain = {
 "   \n"
 "   // Creating normal patches\n"
 "   vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
-"   vec3 qnorm = normalize(floor(aNorm*4.0+modnorm)*0.25) + vec3(0.001,0.0,0.0);\n"
-"   vec2 dir = normalize(qnorm.xz);\n"
+"   vec3 qnorm = floor(aNorm*4.0+modnorm)*0.25 + vec3(0.001,0.0,0.0);\n"
+"   vec2 dir = qnorm.xz;\n"
 "   vec2 uvdiffuse = aCo.xz * 0.02;\n"
 "   uvdiffuse = mat2(dir.y, dir.x, -dir.x, dir.y) * uvdiffuse;\n"
 "   \n"
diff --git a/shaders/terrain_fast.fs b/shaders/terrain_fast.fs
new file mode 100644 (file)
index 0000000..fd4e141
--- /dev/null
@@ -0,0 +1,62 @@
+uniform sampler2D uTexGarbage;
+uniform sampler2D uTexGradients;
+uniform vec3 uCamera;
+uniform vec3 uSandColour;
+uniform vec2 uBlendOffset;
+
+in vec4 aColour;
+in vec2 aUv;
+in vec3 aNorm;
+in vec3 aCo;
+in vec3 aWorldCo;
+
+#include "common_world.glsl"
+#include "motion_vectors_fs.glsl"
+
+void main()
+{
+   compute_motion_vectors();
+
+   // Colour
+   // ------
+   vec3 vfrag = vec3(0.5,0.5,0.5);
+
+   // ws modulation
+   vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 );
+   
+   // Creating normal patches
+   vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;
+   vec3 qnorm = floor(aNorm*4.0+modnorm)*0.25 + vec3(0.001,0.0,0.0);
+   vec2 dir = qnorm.xz;
+   vec2 uvdiffuse = aCo.xz * 0.02;
+   uvdiffuse = mat2(dir.y, dir.x, -dir.x, dir.y) * uvdiffuse;
+   
+   // Patch local noise
+   vec4 rgarbage = texture( uTexGarbage, uvdiffuse );
+
+   // Colour blending
+   float amtgrass = step(qnorm.y,0.6);
+   float amtsand = min(max((aCo.y - 10.0) * -0.1,0.0)*qnorm.y,1.0);
+   vec2 uvgradients = aUv + vec2( amtgrass + rgarbage.a*0.8 )*uBlendOffset;
+   vfrag = texture( uTexGradients, uvgradients ).rgb;
+   vfrag = mix( vfrag, uSandColour, amtsand );
+
+   qnorm = mix( qnorm, aNorm, amtsand );
+   
+   if( g_light_preview == 1 )
+   {
+      vfrag = vec3(0.5);
+   }
+
+   // Lighting
+   vec3 halfview = uCamera - aCo;
+   float fdist = length( halfview );
+   halfview /= fdist;
+
+   vfrag = do_light_diffuse( vfrag, qnorm );
+   vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );
+   vfrag = do_light_shadowing( vfrag );
+   vfrag = apply_fog( vfrag, fdist );
+
+   oColour = vec4(vfrag, 1.0 );
+}