From a81d839cc3d99b3d7293040a3e36cba7a46a33fe Mon Sep 17 00:00:00 2001 From: Jamie Bray Date: Fri, 15 Oct 2021 02:34:44 +0800 Subject: [PATCH] 197fps -> 210fps. Move to surface rendering, instead of texture rendering. --- results/benchmark.txt | 10 +++++----- src/board/loop.h | 16 ++++------------ src/board/sdl.h | 16 ++++++---------- src/entrypoints/benchmark.cpp | 21 +++------------------ 4 files changed, 18 insertions(+), 45 deletions(-) diff --git a/results/benchmark.txt b/results/benchmark.txt index 160a94c..338464c 100644 --- a/results/benchmark.txt +++ b/results/benchmark.txt @@ -1,8 +1,8 @@ ------------------------------------------------------------------------------------------------ Benchmark Time CPU Iterations ------------------------------------------------------------------------------------------------ -BM_NextBoard/iterations:3000/process_time/real_time 4.21 ms 16.3 ms 3000 -BM_RenderBoard/process_time/real_time 10.6 ms 10.6 ms 61 -Renders per second: 31.1944 -Boards per second: 197.433 -BM_Main/iterations:1/repeats:1/process_time/real_time 10.1 s 36.9 s 1 +BM_NextBoard/iterations:3000/process_time/real_time 4.01 ms 15.4 ms 3000 +BM_RenderBoard/process_time/real_time 8.66 ms 8.58 ms 72 +Renders per second: 31.1941 +Boards per second: 210.061 +BM_Main/iterations:1/repeats:1/process_time/real_time 9.52 s 34.6 s 1 diff --git a/src/board/loop.h b/src/board/loop.h index 75d9d35..36862ad 100644 --- a/src/board/loop.h +++ b/src/board/loop.h @@ -12,8 +12,7 @@ using namespace std::chrono; struct LoopMeta { SDL_Window* window = nullptr; - SDL_Renderer* renderer = nullptr; - SDL_Texture* texture = nullptr; + SDL_Surface* surface = nullptr; BoardMeta* board = nullptr; }; @@ -31,10 +30,8 @@ LoopMeta setup() { loop.window = SDL_CreateWindow( "Game of Speed", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (int)loop.board->width, (int)loop.board->height, SDL_WINDOW_MAXIMIZED); - loop.renderer = SDL_CreateRenderer(loop.window, -1, SDL_RENDERER_ACCELERATED); - loop.texture = createTexture( - loop.renderer, (int)loop.board->width, (int)loop.board->height); + loop.surface = SDL_GetWindowSurface(loop.window); return loop; } @@ -43,8 +40,6 @@ void loop(LoopMeta loop, long maxComputations) { auto totalTimer = start(); auto& window = loop.window; - auto& renderer = loop.renderer; - auto& texture = loop.texture; auto& board = *loop.board; std::mutex boardMutex; @@ -69,7 +64,7 @@ void loop(LoopMeta loop, long maxComputations) { // Render loop while (running && computations < maxComputations) { auto renderTimer = start(); - renderBoardSdl(board, renderer, texture); + renderBoardSdl(board, window, (int*)loop.surface->pixels); renders++; SDL_Event event; @@ -89,8 +84,7 @@ void loop(LoopMeta loop, long maxComputations) { int width, height; SDL_GetWindowSize(window, &width, &height); benchmarkBoard(board, (uint)width, (uint)height); - SDL_DestroyTexture(texture); - texture = createTexture(renderer, width, height); + loop.surface = SDL_GetWindowSurface(window); } else if ( event.type == SDL_KEYDOWN && event.key.keysym.scancode == SDL_SCANCODE_J) { @@ -120,8 +114,6 @@ void loop(LoopMeta loop, long maxComputations) { } void shutdown(LoopMeta loop) { - SDL_DestroyTexture(loop.texture); - SDL_DestroyRenderer(loop.renderer); SDL_DestroyWindow(loop.window); SDL_Quit(); diff --git a/src/board/sdl.h b/src/board/sdl.h index 7e3f896..28acdb2 100644 --- a/src/board/sdl.h +++ b/src/board/sdl.h @@ -15,24 +15,20 @@ createTexture(SDL_Renderer* renderer, const int& width, const int& height) { void renderBoardSdl( BoardMeta& board, - SDL_Renderer* renderer, - SDL_Texture* texture) { + SDL_Window* window, + int* pixels) { const auto& output = board.output; - const auto& render = board.render; - const auto& width = board.width; // I would LOVE it if someone could figure out how to create a "bool" pixel // format to use with SDL. Then I wouldn't need to do this memcpy trash. for (unsigned int i = 0; i < board.width * board.height; i++) { if (output[i] == ALIVE) { - render[i] = ALIVE_RENDER; + *pixels = INT32_MAX; } else { - render[i] = DEAD_RENDER; + *pixels = 0; } + pixels++; } - static_assert(sizeof(render[0]) == sizeof(Uint32)); - SDL_UpdateTexture(texture, NULL, &render[0], (int)(width * sizeof(Uint32))); - SDL_RenderCopy(renderer, texture, nullptr, nullptr); - SDL_RenderPresent(renderer); + SDL_UpdateWindowSurface(window); } diff --git a/src/entrypoints/benchmark.cpp b/src/entrypoints/benchmark.cpp index 0fc4384..1599514 100644 --- a/src/entrypoints/benchmark.cpp +++ b/src/entrypoints/benchmark.cpp @@ -25,26 +25,11 @@ BENCHMARK(BM_NextBoard) ->Iterations(3000); static void BM_RenderBoard(benchmark::State& state) { - BoardMeta board(TEST_WIDTH, TEST_HEIGHT); - benchmarkBoard(board, TEST_WIDTH, TEST_HEIGHT); - nextBoard(board); - - // Initialize graphics - SDL_Init(SDL_INIT_VIDEO); - SDL_Renderer* renderer; - SDL_Window* window; - SDL_CreateWindowAndRenderer(TEST_WIDTH, TEST_HEIGHT, 0, &window, &renderer); - auto texture = createTexture(renderer, TEST_WIDTH, TEST_HEIGHT); - + auto meta = setup(); for (auto _ : state) { - renderBoardSdl(board, renderer, texture); + renderBoardSdl(*meta.board, meta.window, (int*)meta.surface->pixels); } - - // Destroy graphics - SDL_DestroyTexture(texture); - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); - SDL_Quit(); + shutdown(meta); } BENCHMARK(BM_RenderBoard) ->Unit(benchmark::kMillisecond)