diff --git a/tests/shadowcasting_test.cpp b/tests/shadowcasting_test.cpp index e5a0fbcd34b05..ed8b2ee3a2550 100644 --- a/tests/shadowcasting_test.cpp +++ b/tests/shadowcasting_test.cpp @@ -221,9 +221,9 @@ void print_grid_comparison( const point &offset, static void shadowcasting_runoff( const int iterations, const bool test_bresenham = false ) { - float seen_squares_control[MAPSIZE * SEEX][MAPSIZE * SEEY] = {{0}}; - float seen_squares_experiment[MAPSIZE * SEEX][MAPSIZE * SEEY] = {{0}}; - float transparency_cache[MAPSIZE * SEEX][MAPSIZE * SEEY] = {{0}}; + float seen_squares_control[MAPSIZE * SEEX][MAPSIZE * SEEY] = {}; + float seen_squares_experiment[MAPSIZE * SEEX][MAPSIZE * SEEY] = {}; + float transparency_cache[MAPSIZE * SEEX][MAPSIZE * SEEY] = {}; randomly_fill_transparency( transparency_cache ); @@ -290,9 +290,9 @@ static void shadowcasting_runoff( const int iterations, const bool test_bresenha static void shadowcasting_float_quad( const int iterations, const unsigned int denominator = DENOMINATOR ) { - float lit_squares_float[MAPSIZE * SEEX][MAPSIZE * SEEY] = {{0}}; - four_quadrants lit_squares_quad[MAPSIZE * SEEX][MAPSIZE * SEEY] = {{}}; - float transparency_cache[MAPSIZE * SEEX][MAPSIZE * SEEY] = {{0}}; + float lit_squares_float[MAPSIZE * SEEX][MAPSIZE * SEEY] = {}; + four_quadrants lit_squares_quad[MAPSIZE * SEEX][MAPSIZE * SEEY] = {}; + float transparency_cache[MAPSIZE * SEEX][MAPSIZE * SEEY] = {}; randomly_fill_transparency( transparency_cache, denominator ); @@ -340,12 +340,83 @@ static void shadowcasting_float_quad( REQUIRE( passed ); } +static void do_3d_benchmark( + std::array &transparency_caches, + const int iterations ) +{ + float seen_squares[OVERMAP_LAYERS][MAPSIZE * SEEX][MAPSIZE * SEEY] = {}; + bool floor_cache[OVERMAP_LAYERS][MAPSIZE * SEEX][MAPSIZE * SEEY] = {}; + + const tripoint origin( 65, 65, 0 ); + std::array seen_caches; + std::array floor_caches; + + for( int z = -OVERMAP_DEPTH; z <= OVERMAP_HEIGHT; z++ ) { + seen_caches[z + OVERMAP_DEPTH] = &seen_squares[z + OVERMAP_DEPTH]; + floor_caches[z + OVERMAP_DEPTH] = &floor_cache[z + OVERMAP_DEPTH]; + } + + const auto start = std::chrono::high_resolution_clock::now(); + for( int i = 0; i < iterations; i++ ) { + cast_zlight( + seen_caches, transparency_caches, floor_caches, origin, 0, 1.0 ); + } + const auto end = std::chrono::high_resolution_clock::now(); + + if( iterations > 1 ) { + const long long diff = + std::chrono::duration_cast( end - start ).count(); + printf( "cast_zlight() executed %d times in %lld microseconds.\n", + iterations, diff ); + } +} + +static void shadowcasting_3d_benchmark( const int iterations ) +{ + float transparency_cache[OVERMAP_LAYERS][MAPSIZE * SEEX][MAPSIZE * SEEY] = {{{0}}}; + std::array transparency_caches; + for( int z = -OVERMAP_DEPTH; z <= OVERMAP_HEIGHT; z++ ) { + randomly_fill_transparency( transparency_cache[z + OVERMAP_DEPTH] ); + transparency_caches[z + OVERMAP_DEPTH] = &transparency_cache[z + OVERMAP_DEPTH]; + } + do_3d_benchmark( transparency_caches, iterations ); + + // Flat plain + // TODO: add roofs + for( int z = -OVERMAP_DEPTH; z <= OVERMAP_HEIGHT; z++ ) { + float value_to_set = LIGHT_TRANSPARENCY_SOLID; + if( z < 0 ) { + value_to_set = LIGHT_TRANSPARENCY_SOLID; + } else { + value_to_set = LIGHT_TRANSPARENCY_OPEN_AIR; + } + for( auto &inner : transparency_cache[z + OVERMAP_DEPTH] ) { + for( float &square : inner ) { + square = value_to_set; + } + } + } + do_3d_benchmark( transparency_caches, iterations ); + + // Add some obstacles, a ring at distance 5 + float ( &ground_level )[MAPSIZE * SEEX][MAPSIZE * SEEY] = transparency_cache[OVERMAP_DEPTH]; + ground_level[60][65] = LIGHT_TRANSPARENCY_SOLID; + ground_level[63][63] = LIGHT_TRANSPARENCY_SOLID; + ground_level[65][60] = LIGHT_TRANSPARENCY_SOLID; + ground_level[68][63] = LIGHT_TRANSPARENCY_SOLID; + ground_level[65][70] = LIGHT_TRANSPARENCY_SOLID; + ground_level[68][68] = LIGHT_TRANSPARENCY_SOLID; + ground_level[70][65] = LIGHT_TRANSPARENCY_SOLID; + ground_level[63][68] = LIGHT_TRANSPARENCY_SOLID; + do_3d_benchmark( transparency_caches, iterations ); +} + static void shadowcasting_3d_2d( const int iterations ) { - float seen_squares_control[MAPSIZE * SEEX][MAPSIZE * SEEY] = {{0}}; - float seen_squares_experiment[MAPSIZE * SEEX][MAPSIZE * SEEY] = {{0}}; - float transparency_cache[MAPSIZE * SEEX][MAPSIZE * SEEY] = {{0}}; - bool floor_cache[MAPSIZE * SEEX][MAPSIZE * SEEY] = {{false}}; + float seen_squares_control[MAPSIZE * SEEX][MAPSIZE * SEEY] = {}; + float seen_squares_experiment[MAPSIZE * SEEX][MAPSIZE * SEEY] = {}; + float transparency_cache[MAPSIZE * SEEX][MAPSIZE * SEEY] = {}; + bool floor_cache[MAPSIZE * SEEX][MAPSIZE * SEEY] = {}; randomly_fill_transparency( transparency_cache ); @@ -1019,6 +1090,11 @@ TEST_CASE( "shadowcasting_3d_2d_performance", "[.]" ) shadowcasting_3d_2d( 100000 ); } +TEST_CASE( "shadowcasting_3d_performance", "[.]" ) +{ + shadowcasting_3d_benchmark( 10000 ); +} + TEST_CASE( "shadowcasting_float_quad_equivalence", "[shadowcasting]" ) { shadowcasting_float_quad( 1 );