|
| 1 | +#include <cstddef> |
| 2 | + |
| 3 | +#include <benchmark/benchmark.h> |
| 4 | + |
| 5 | +#include "engine/render/light_render.hpp" |
| 6 | +#include "engine/surface.hpp" |
| 7 | +#include "lighting.h" |
| 8 | +#include "utils/log.hpp" |
| 9 | +#include "utils/paths.h" |
| 10 | +#include "utils/sdl_wrap.h" |
| 11 | + |
| 12 | +namespace devilution { |
| 13 | +namespace { |
| 14 | + |
| 15 | +void BM_BuildLightmap(benchmark::State &state) |
| 16 | +{ |
| 17 | + std::string benchmarkDataPath = paths::BasePath() + "test/fixtures/light_render_benchmark/dLight.dmp"; |
| 18 | + FILE *lightFile = std::fopen(benchmarkDataPath.c_str(), "rb"); |
| 19 | + if (lightFile != nullptr) { |
| 20 | + std::fread(&dLight[0][0], sizeof(uint8_t), MAXDUNX * MAXDUNY, lightFile); |
| 21 | + std::fclose(lightFile); |
| 22 | + } |
| 23 | + |
| 24 | + SDLSurfaceUniquePtr sdl_surface = SDLWrap::CreateRGBSurfaceWithFormat( |
| 25 | + /*flags=*/0, /*width=*/640, /*height=*/480, /*depth=*/8, SDL_PIXELFORMAT_INDEX8); |
| 26 | + if (sdl_surface == nullptr) { |
| 27 | + LogError("Failed to create SDL Surface: {}", SDL_GetError()); |
| 28 | + exit(1); |
| 29 | + } |
| 30 | + Surface out = Surface(sdl_surface.get()); |
| 31 | + |
| 32 | + Point tilePosition { 48, 44 }; |
| 33 | + Point targetBufferPosition { 0, -17 }; |
| 34 | + int viewportWidth = 640; |
| 35 | + int viewportHeight = 352; |
| 36 | + int rows = 25; |
| 37 | + int columns = 10; |
| 38 | + const uint8_t *outBuffer = out.at(0, 0); |
| 39 | + uint16_t outPitch = out.pitch(); |
| 40 | + const uint8_t *lightTables = LightTables[0].data(); |
| 41 | + size_t lightTableSize = LightTables[0].size(); |
| 42 | + |
| 43 | + size_t numViewportTiles = rows * columns; |
| 44 | + size_t numPixels = viewportWidth * viewportHeight; |
| 45 | + size_t numBytesProcessed = 0; |
| 46 | + size_t numItemsProcessed = 0; |
| 47 | + for (auto _ : state) { |
| 48 | + Lightmap lightmap = Lightmap::build(tilePosition, targetBufferPosition, |
| 49 | + viewportWidth, viewportHeight, rows, columns, |
| 50 | + outBuffer, outPitch, lightTables, lightTableSize); |
| 51 | + |
| 52 | + uint8_t lightLevel = *lightmap.getLightingAt(outBuffer + outPitch * 120 + 120); |
| 53 | + benchmark::DoNotOptimize(lightLevel); |
| 54 | + numItemsProcessed += numViewportTiles; |
| 55 | + numBytesProcessed += numPixels; |
| 56 | + } |
| 57 | + state.SetBytesProcessed(numBytesProcessed); |
| 58 | + state.SetItemsProcessed(numItemsProcessed); |
| 59 | +} |
| 60 | + |
| 61 | +BENCHMARK(BM_BuildLightmap); |
| 62 | + |
| 63 | +} // namespace |
| 64 | +} // namespace devilution |
0 commit comments