From a6708cef5099c83ff14816442de0954934f10276 Mon Sep 17 00:00:00 2001 From: hgn Date: Mon, 26 Dec 2022 15:39:33 +0000 Subject: [PATCH] enforce alignment --- build.c | 2 +- bvh.h | 2 +- common.h | 3 +- model.h | 10 +++---- shaders/terrain.h | 4 +-- shaders/terrain_fast.fs | 62 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 shaders/terrain_fast.fs diff --git a/build.c b/build.c index c0a79b6..fe51c05 100644 --- 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 491756a..41a4108 100644 --- 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; diff --git a/common.h b/common.h index bac81a7..bb70596 100644 --- 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 9bdc2bc..be0666a 100644 --- 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 ); diff --git a/shaders/terrain.h b/shaders/terrain.h index b2730fb..8b9452e 100644 --- a/shaders/terrain.h +++ b/shaders/terrain.h @@ -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 index 0000000..fd4e141 --- /dev/null +++ b/shaders/terrain_fast.fs @@ -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 ); +} -- 2.25.1