Skip to content

Commit

Permalink
Fix #9559: benchspritesort is broken after #8481 (#9590)
Browse files Browse the repository at this point in the history
* Fix #9559: benchspritesort is broken after #8481

This takes multithreading into account as well

* Apply review fixes

* Fix detection of newer Google Benchmark (>= 1.5.0)

* Review fix
  • Loading branch information
janisozaur committed Feb 12, 2020
1 parent 962787a commit 1b76392
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/openrct2/CMakeLists.txt
Expand Up @@ -84,7 +84,7 @@ if (NOT DISABLE_TTF)
endif ()

if (NOT DISABLE_GOOGLE_BENCHMARK)
find_package(benchmark 1.4 QUIET)
find_package(benchmark)
if (benchmark_FOUND)
message("Found Google benchmark, enabling support")
set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_DEFINITIONS USE_BENCHMARK)
Expand Down
42 changes: 37 additions & 5 deletions src/openrct2/interface/Viewport.cpp
Expand Up @@ -827,9 +827,33 @@ void viewport_render(
#endif
}

static void viewport_fill_column(paint_session* session)
static void record_session(const paint_session* session, std::vector<paint_session>* recorded_sessions, size_t record_index)
{
// Perform a deep copy of the paint session, use relative offsets.
// This is done to extract the session for benchmark.
// Place the copied session at provided record_index, so the caller can decide which columns/paint sessions to copy; there
// is no column information embedded in the session itself.
(*recorded_sessions)[record_index] = (*session);
paint_session* session_copy = &recorded_sessions->at(record_index);

// Mind the offset needs to be calculated against the original `session`, not `session_copy`
for (auto& ps : session_copy->PaintStructs)
{
ps.basic.next_quadrant_ps = (paint_struct*)(ps.basic.next_quadrant_ps ? int(ps.basic.next_quadrant_ps - &session->PaintStructs[0].basic) : std::size(session->PaintStructs));
}
for (auto& quad : session_copy->Quadrants)
{
quad = (paint_struct*)(quad ? int(quad - &session->PaintStructs[0].basic) : std::size(session->Quadrants));
}
}

static void viewport_fill_column(paint_session* session, std::vector<paint_session>* recorded_sessions, size_t record_index)
{
paint_session_generate(session);
if (recorded_sessions != nullptr)
{
record_session(session, recorded_sessions, record_index);
}
paint_session_arrange(session);
}

Expand Down Expand Up @@ -876,7 +900,7 @@ static void viewport_paint_column(paint_session* session)
*/
void viewport_paint(
const rct_viewport* viewport, rct_drawpixelinfo* dpi, int16_t left, int16_t top, int16_t right, int16_t bottom,
std::vector<paint_session>* sessions)
std::vector<paint_session>* recorded_sessions)
{
uint32_t viewFlags = viewport->flags;
uint16_t width = right - left;
Expand Down Expand Up @@ -926,8 +950,15 @@ void viewport_paint(
_paintJobs.reset();
}

// Splits the area into 32 pixel columns and renders them
// Create space to record sessions and keep track which index is being drawn
size_t index = 0;
if (recorded_sessions != nullptr)
{
const uint16_t column_count = (rightBorder - floor2(dpi1.x, 32)) / 32 + 1;
recorded_sessions->resize(column_count);
}

// Splits the area into 32 pixel columns and renders them
for (x = floor2(dpi1.x, 32); x < rightBorder; x += 32, index++)
{
paint_session* session = paint_session_alloc(&dpi1, viewFlags);
Expand All @@ -954,11 +985,12 @@ void viewport_paint(

if (useMultithreading)
{
_paintJobs->AddTask([session]() -> void { viewport_fill_column(session); });
_paintJobs->AddTask(
[session, recorded_sessions, index]() -> void { viewport_fill_column(session, recorded_sessions, index); });
}
else
{
viewport_fill_column(session);
viewport_fill_column(session, recorded_sessions, index);
}
}

Expand Down

0 comments on commit 1b76392

Please sign in to comment.