Skip to content

Commit

Permalink
197fps -> 210fps. Move to surface rendering, instead of texture rende…
Browse files Browse the repository at this point in the history
…ring.
  • Loading branch information
Jumbub committed Oct 14, 2021
1 parent 73977a1 commit a81d839
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 45 deletions.
10 changes: 5 additions & 5 deletions results/benchmark.txt
Original file line number Diff line number Diff line change
@@ -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
16 changes: 4 additions & 12 deletions src/board/loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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();

Expand Down
16 changes: 6 additions & 10 deletions src/board/sdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
21 changes: 3 additions & 18 deletions src/entrypoints/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a81d839

Please sign in to comment.