Improved how threaded rendering is handled

This commit is contained in:
Magnus Norddahl 2016-06-10 18:43:49 +02:00
parent ffcfe0b54f
commit 07571da98c
4 changed files with 39 additions and 8 deletions

View file

@ -443,8 +443,11 @@ void R_SetDSColorMapLight(FColormap *base_colormap, float light, int shade);
void R_SetTranslationMap(lighttable_t *translation); void R_SetTranslationMap(lighttable_t *translation);
// Redirect drawer commands to worker threads
void R_BeginDrawerCommands();
// Wait until all drawers finished executing // Wait until all drawers finished executing
void R_FinishDrawerCommands(); void R_EndDrawerCommands();
class DrawerCommandQueue; class DrawerCommandQueue;
@ -530,13 +533,14 @@ class DrawerCommandQueue
std::condition_variable end_condition; std::condition_variable end_condition;
size_t finished_threads = 0; size_t finished_threads = 0;
bool no_threading = false; int threaded_render = 0;
DrawerThread single_core_thread; DrawerThread single_core_thread;
int num_passes = 2; int num_passes = 2;
int rows_in_pass = 540; int rows_in_pass = 540;
void StartThreads(); void StartThreads();
void StopThreads(); void StopThreads();
void Finish();
static DrawerCommandQueue *Instance(); static DrawerCommandQueue *Instance();
@ -551,7 +555,7 @@ public:
static void QueueCommand(Types &&... args) static void QueueCommand(Types &&... args)
{ {
auto queue = Instance(); auto queue = Instance();
if (queue->no_threading) if (queue->threaded_render == 0)
{ {
T command(std::forward<Types>(args)...); T command(std::forward<Types>(args)...);
command.Execute(&queue->single_core_thread); command.Execute(&queue->single_core_thread);
@ -565,9 +569,13 @@ public:
queue->commands.push_back(command); queue->commands.push_back(command);
} }
} }
// Redirects all drawing commands to worker threads until Finish is called
// Begin/End blocks can be nested.
static void Begin();
// Wait until all worker threads finished executing commands // Wait until all worker threads finished executing commands
static void Finish(); static void End();
}; };
#endif #endif

View file

@ -82,6 +82,21 @@ void* DrawerCommandQueue::AllocMemory(size_t size)
return data; return data;
} }
void DrawerCommandQueue::Begin()
{
auto queue = Instance();
queue->Finish();
queue->threaded_render++;
}
void DrawerCommandQueue::End()
{
auto queue = Instance();
queue->Finish();
if (queue->threaded_render > 0)
queue->threaded_render--;
}
void DrawerCommandQueue::Finish() void DrawerCommandQueue::Finish()
{ {
auto queue = Instance(); auto queue = Instance();
@ -3515,9 +3530,14 @@ public:
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
void R_FinishDrawerCommands() void R_BeginDrawerCommands()
{ {
DrawerCommandQueue::Finish(); DrawerCommandQueue::Begin();
}
void R_EndDrawerCommands()
{
DrawerCommandQueue::End();
} }
void R_DrawColumnP_RGBA_C() void R_DrawColumnP_RGBA_C()

View file

@ -960,6 +960,8 @@ void R_RenderViewToCanvas (AActor *actor, DCanvas *canvas,
r_swtruecolor = canvas->IsBgra(); r_swtruecolor = canvas->IsBgra();
R_InitColumnDrawers(); R_InitColumnDrawers();
} }
R_BeginDrawerCommands();
viewwidth = width; viewwidth = width;
RenderTarget = canvas; RenderTarget = canvas;
@ -979,7 +981,7 @@ void R_RenderViewToCanvas (AActor *actor, DCanvas *canvas,
R_SetupBuffer (); R_SetupBuffer ();
screen->Unlock (); screen->Unlock ();
R_FinishDrawerCommands(); R_EndDrawerCommands();
viewactive = savedviewactive; viewactive = savedviewactive;
r_swtruecolor = savedoutputformat; r_swtruecolor = savedoutputformat;

View file

@ -162,10 +162,11 @@ void FSoftwareRenderer::RenderView(player_t *player)
R_InitColumnDrawers(); R_InitColumnDrawers();
} }
R_BeginDrawerCommands();
R_RenderActorView (player->mo); R_RenderActorView (player->mo);
// [RH] Let cameras draw onto textures that were visible this frame. // [RH] Let cameras draw onto textures that were visible this frame.
FCanvasTextureInfo::UpdateAll (); FCanvasTextureInfo::UpdateAll ();
R_FinishDrawerCommands(); R_EndDrawerCommands();
} }
//========================================================================== //==========================================================================