commit af00f285c86ce4472719b44a9879ab307613b2f8 parent 34a46c6b035b409687bc4cadd37453b927cbd46c Author: Michael Savage <mikejsavage@gmail.com> Date: Sun Dec 4 15:17:56 +0200 Reduce terrain preprocessor's max memory usage Diffstat:
pp.cc | | | 82 | ++++++++++++++++++++++++++++++++++++++++++------------------------------------- |
diff --git a/pp.cc b/pp.cc @@ -13,7 +13,6 @@ #include "stb_image.h" #include "platform_io.h" #include "linear_algebra.h" -// #include "stb_image_write.h" #include "lz4.h" #include "lz4hc.h" @@ -117,15 +116,13 @@ static void write_compressed_file( const void * data, size_t len, const char * f delete[] compressed; } +template< typename T > static void write_tile( - MemoryArena * arena, const std::string & dir, - const array2d< u8 > heightmap, const array2d< float > horizons, - const array2d< v3 > normals, int tx, int ty + MemoryArena * arena, const std::string & dir, const std::string & extension, + const array2d< T > data, T border, int tx, int ty ) { MEMARENA_SCOPED_CHECKPOINT( arena ); - array2d< u8 > tile_heightmap = memarena_push_array2d( arena, u8, OUT_SIZE, OUT_SIZE ); - array2d< float > tile_horizons = memarena_push_array2d( arena, float, OUT_SIZE, OUT_SIZE ); - array2d< v3 > tile_normals = memarena_push_array2d( arena, v3, OUT_SIZE, OUT_SIZE ); + array2d< T > tile = memarena_push_array2d( arena, T, OUT_SIZE, OUT_SIZE ); const u32 left = tx * TILE_SIZE; const u32 top = ty * TILE_SIZE; @@ -135,30 +132,18 @@ static void write_tile( int global_x = left + local_x; int global_y = top + local_y; - if( heightmap.in_range( global_x, global_y ) ) { - tile_heightmap( local_x, local_y ) = heightmap( global_x, global_y ); - tile_horizons( local_x, local_y ) = horizons( global_x, global_y ); - tile_normals( local_x, local_y ) = normals( global_x, global_y ); + if( data.in_range( global_x, global_y ) ) { + tile( local_x, local_y ) = data( global_x, global_y ); } else { - tile_heightmap( local_x, local_y ) = 0; - tile_horizons( local_x, local_y ) = 0; - tile_normals( local_x, local_y ) = v3( 0 ); + tile( local_x, local_y ) = border; } } } - printf( "writing heightmap for (%d, %d)\n", tx, ty ); - write_compressed_file( tile_heightmap.ptr(), tile_heightmap.bytes(), - "%s/%d_%d_heightmap.lz4", dir.c_str(), tx, ty ); - - printf( "writing horizons for (%d, %d)\n", tx, ty ); - write_compressed_file( tile_horizons.ptr(), tile_horizons.bytes(), - "%s/%d_%d_horizons.lz4", dir.c_str(), tx, ty ); - - printf( "writing normals for (%d, %d)\n", tx, ty ); - write_compressed_file( tile_normals.ptr(), tile_normals.bytes(), - "%s/%d_%d_normals.lz4", dir.c_str(), tx, ty ); + printf( "writing %d_%d%s\n", tx, ty, extension.c_str() ); + write_compressed_file( tile.ptr(), tile.bytes(), + "%s/%d_%d%s", dir.c_str(), tx, ty, extension.c_str() ); } static u8 arena_memory[ megabytes( 16 ) ]; @@ -190,28 +175,49 @@ int main( int argc, char ** argv ) { ASSERT( ferror( dims ) == 0 ); fclose( dims ); - float * horizons_memory = new float[ w * h ]; - v3 * normals_memory = new v3[ w * h ]; const array2d< u8 > heightmap( heightmap_memory, w, h ); - // TODO: quantize horizons. maybe we only need to store the convex hull too - array2d< float > horizons( horizons_memory, w, h ); - // TODO: normals can be v2 since z >= 0 on a heightmap - array2d< v3 > normals( normals_memory, w, h ); + + { + for( int ty = 0; ty < h / TILE_SIZE; ty++ ) { + for( int tx = 0; tx < w / TILE_SIZE; tx++ ) { + write_tile( &arena, dir, "_heightmap.lz4", heightmap, u8( 0 ), tx, ty ); + } + } + } printf( "generating horizons\n" ); - compute_horizons( &arena, heightmap, horizons ); + { + // TODO: quantize horizons. maybe we only need to store the convex hull too + float * horizons_memory = new float[ w * h ]; + array2d< float > horizons( horizons_memory, w, h ); + + compute_horizons( &arena, heightmap, horizons ); + + for( int ty = 0; ty < h / TILE_SIZE; ty++ ) { + for( int tx = 0; tx < w / TILE_SIZE; tx++ ) { + write_tile( &arena, dir, "_horizons.lz4", horizons, 0.0f, tx, ty ); + } + } + + delete[] horizons_memory; + } + printf( "generating normals\n" ); - compute_normals( heightmap, normals ); + { + // TODO: normals can be v2 since z >= 0 on a heightmap + v3 * normals_memory = new v3[ w * h ]; + array2d< v3 > normals( normals_memory, w, h ); + + compute_normals( heightmap, normals ); - for( int ty = 0; ty < h / TILE_SIZE; ty++ ) { - for( int tx = 0; tx < w / TILE_SIZE; tx++ ) { - write_tile( &arena, dir, heightmap, horizons, normals, tx, ty ); + for( int ty = 0; ty < h / TILE_SIZE; ty++ ) { + for( int tx = 0; tx < w / TILE_SIZE; tx++ ) { + write_tile( &arena, dir, "_normals.lz4", normals, v3( 0.0f, 0.0f, 1.0f ), tx, ty ); + } } } stbi_image_free( heightmap_memory ); - delete[] horizons_memory; - delete[] normals_memory; return 0; }