From: hgn Date: Fri, 8 Dec 2023 18:35:10 +0000 (+0000) Subject: performance wins! X-Git-Url: https://harrygodden.com/git/?p=carveJwlIkooP6JGAAIwe30JlM.git;a=commitdiff_plain;h=f97f731806ae51a3ebf5813fd304c0234676a10a performance wins! --- diff --git a/bvh.h b/bvh.h index 945d3cb..b04c3de 100644 --- a/bvh.h +++ b/bvh.h @@ -55,6 +55,7 @@ struct bh_tree{ }; struct bh_system{ + u32 system_type; void (*expand_bound)( void *user, boxf bound, u32 item_index ); float (*item_centroid)( void *user, u32 item_index, int axis ); void (*item_closest)( void *user, u32 item_index, v3f point, v3f closest ); @@ -71,13 +72,20 @@ struct bh_system{ int (*cast_ray)( void *user, u32 index, v3f co, v3f dir, ray_hit *hit ); }; +static float scene_bh_centroid( void *user, u32 item_index, int axis ); +static void scene_bh_swap( void *user, u32 ia, u32 ib ); +static void scene_bh_expand_bound( void *user, boxf bound, u32 item_index ); + static void bh_update_bounds( bh_tree *bh, u32 inode ){ bh_node *node = &bh->nodes[ inode ]; box_init_inf( node->bbx ); for( u32 i=0; icount; i++ ){ u32 idx = node->start+i; - bh->system->expand_bound( bh->user, node->bbx, idx ); + if( bh->system->system_type == 0x1 ) + scene_bh_expand_bound( bh->user, node->bbx, idx ); + else + bh->system->expand_bound( bh->user, node->bbx, idx ); } } @@ -96,10 +104,13 @@ static void bh_subdivide( bh_tree *bh, u32 inode ){ float split = node->bbx[0][axis] + extent[axis]*0.5f; float avg = 0.0; - for( u32 t=0; tcount; t++ ) - { + for( u32 t=0; tcount; t++ ){ u32 idx = node->start+t; - avg += bh->system->item_centroid( bh->user, idx, axis ); + + if( bh->system->system_type == 0x1 ) + avg += scene_bh_centroid( bh->user, idx, axis ); + else + avg += bh->system->item_centroid( bh->user, idx, axis ); } avg /= (float)node->count; split = avg; @@ -109,10 +120,20 @@ static void bh_subdivide( bh_tree *bh, u32 inode ){ j = i + node->count-1; while( i <= j ){ - if( bh->system->item_centroid( bh->user, i, axis ) < split ) + f32 centroid; + + if( bh->system->system_type == 0x1 ) + centroid = scene_bh_centroid( bh->user, i, axis ); + else + centroid = bh->system->item_centroid( bh->user, i, axis ); + + if( centroid < split ) i ++; else{ - bh->system->item_swap( bh->user, i, j ); + if( bh->system->system_type == 0x1 ) + scene_bh_swap( bh->user, i, j ); + else + bh->system->item_swap( bh->user, i, j ); j --; } } diff --git a/scene.h b/scene.h index 8aaafe9..ba23ffb 100644 --- a/scene.h +++ b/scene.h @@ -389,6 +389,7 @@ static bh_system bh_system_scene = .item_closest = scene_bh_closest, .item_swap = scene_bh_swap, .item_debug = scene_bh_debug, + .system_type = 0x1 }; /* diff --git a/world_gen.c b/world_gen.c index 543be6d..3d705ab 100644 --- a/world_gen.c +++ b/world_gen.c @@ -126,6 +126,9 @@ static void world_apply_procedural_foliage( world_instance *world, /* TODO: Quasirandom? */ vg_info( "Map area: %f. Max particles: %u\n", area, particles ); + + u64 t0 = SDL_GetPerformanceCounter(); +#if 0 for( u32 i=0; iscene_geo.bbx[0], co ); + + ray_hit hit; + hit.dist = INFINITY; + + if( ray_world( world, co, (v3f){0.0f,-1.0f,0.0f}, &hit, + k_material_flag_ghosts )){ + struct world_surface *m1 = ray_hit_surface( world, &hit ); + if((hit.normal[1] > 0.8f) && (m1 == mat) && + (hit.pos[1] > 0.0f+10.0f)){ + world_gen_add_blob( world, scene, &hit ); + count ++; + } + } + + } + } + } + +#endif + + + + u64 t1 = SDL_GetPerformanceCounter(), + utime_blobs = t1-t0, + ufreq = SDL_GetPerformanceFrequency(); + f64 ftime_blobs = ((f64)utime_blobs / (f64)ufreq)*1000.0; - vg_info( "%d foliage models added\n", count ); + vg_info( "%d foliage models added. %f%% (%fms)\n", count, + 100.0*((f64)count/(f64)particles), ftime_blobs); } static diff --git a/world_load.c b/world_load.c index 5ab2dfb..4c768bf 100644 --- a/world_load.c +++ b/world_load.c @@ -98,16 +98,39 @@ static void world_instance_load_mdl( u32 instance_id, const char *path ){ world->time += (world->info.timezone/24.0); /* process resources from pack */ + u64 t4 = SDL_GetPerformanceCounter(); world_gen_load_surfaces( world ); + u64 t5 = SDL_GetPerformanceCounter(); world_gen_routes_ent_init( world ); world_gen_entities_init( world ); + u64 t6 = SDL_GetPerformanceCounter(); /* main bulk */ + u64 t0 = SDL_GetPerformanceCounter(); world_gen_generate_meshes( world ); + u64 t1 = SDL_GetPerformanceCounter(); world_gen_routes_generate( instance_id ); + u64 t2 = SDL_GetPerformanceCounter(); world_gen_compute_light_indices( world ); + u64 t3 = SDL_GetPerformanceCounter(); mdl_close( meta ); + u64 utime_mesh = t1-t0, + utime_route = t2-t1, + utime_indices = t3-t2, + utime_tex = t5-t4, + utime_ent = t6-t5, + ufreq = SDL_GetPerformanceFrequency(); + + f64 ftime_mesh = ((f64)utime_mesh / (f64)ufreq)*1000.0, + ftime_route = ((f64)utime_route / (f64)ufreq)*1000.0, + ftime_ind = ((f64)utime_route / (f64)ufreq)*1000.0, + ftime_tex = ((f64)utime_tex / (f64)ufreq)*1000.0, + ftime_ent = ((f64)utime_ent / (f64)ufreq)*1000.0; + + vg_info( "wtime:mesh %.2fms route %.2fms ind %.2fms tex %.2fms ent %.2fms\n", + ftime_mesh, ftime_route, ftime_ind, ftime_tex, ftime_ent ); + /* init player position. * - this is overriden by the save state when(if) it loads */ ent_spawn *rp = world_find_spawn_by_name( world, "start" );