Skip to content

Commit

Permalink
Merge pull request #44557 from kevingranade/shadowcasting-benchmark
Browse files Browse the repository at this point in the history
shadowcasting benchmark
  • Loading branch information
ZhilkinSerg authored Oct 7, 2020
2 parents d4ab786 + cdaeab6 commit 82beb80
Showing 1 changed file with 86 additions and 10 deletions.
96 changes: 86 additions & 10 deletions tests/shadowcasting_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -340,12 +340,83 @@ static void shadowcasting_float_quad(
REQUIRE( passed );
}

static void do_3d_benchmark(
std::array<const float ( * )[MAPSIZE *SEEX][MAPSIZE *SEEY], OVERMAP_LAYERS> &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<float ( * )[MAPSIZE *SEEX][MAPSIZE *SEEY], OVERMAP_LAYERS> seen_caches;
std::array<const bool ( * )[MAPSIZE *SEEX][MAPSIZE *SEEY], OVERMAP_LAYERS> 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<float, sight_calc, sight_check, accumulate_transparency>(
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<std::chrono::microseconds>( 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<const float ( * )[MAPSIZE *SEEX][MAPSIZE *SEEY], OVERMAP_LAYERS> 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 );

Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit 82beb80

Please sign in to comment.