From 990f02d7c5b5ab4c08239fd0d7e09d836dc774d8 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Sat, 28 Jul 2018 04:57:23 +0200 Subject: [PATCH] - add support for specifying the exact thread count to r_multithreaded and r_scene_multithreaded --- src/polyrenderer/poly_renderthread.cpp | 6 +++-- src/swrenderer/drawers/r_draw.h | 2 +- src/swrenderer/drawers/r_thread.cpp | 32 +++++++++++++++++--------- src/swrenderer/drawers/r_thread.h | 7 +++--- src/swrenderer/scene/r_scene.cpp | 6 +++-- 5 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/polyrenderer/poly_renderthread.cpp b/src/polyrenderer/poly_renderthread.cpp index 55d1c8469..7febb4bb7 100644 --- a/src/polyrenderer/poly_renderthread.cpp +++ b/src/polyrenderer/poly_renderthread.cpp @@ -44,7 +44,7 @@ void PeekThreadedErrorPane(); #endif -EXTERN_CVAR(Bool, r_scene_multithreaded); +EXTERN_CVAR(Int, r_scene_multithreaded); PolyRenderThread::PolyRenderThread(int threadIndex) : MainThread(threadIndex == 0), ThreadIndex(threadIndex) { @@ -151,8 +151,10 @@ void PolyRenderThreads::RenderThreadSlices(int totalcount, std::functionStartThreads(); + // Add to queue and awaken worker threads std::unique_lock start_lock(queue->start_mutex); std::unique_lock end_lock(queue->end_mutex); - queue->StartThreads(); queue->active_commands.push_back(commands); queue->tasks_left += queue->threads.size(); end_lock.unlock(); @@ -171,22 +172,31 @@ void DrawerThreads::WorkerMain(DrawerThread *thread) void DrawerThreads::StartThreads() { - if (!threads.empty()) - return; + std::unique_lock lock(threads_mutex); int num_threads = std::thread::hardware_concurrency(); if (num_threads == 0) num_threads = 4; - threads.resize(num_threads); + if (r_multithreaded == 0) + num_threads = 1; + else if (r_multithreaded != 1) + num_threads = r_multithreaded; - for (int i = 0; i < num_threads; i++) + if (num_threads != (int)threads.size()) { - DrawerThreads *queue = this; - DrawerThread *thread = &threads[i]; - thread->core = i; - thread->num_cores = num_threads; - thread->thread = std::thread([=]() { queue->WorkerMain(thread); }); + StopThreads(); + + threads.resize(num_threads); + + for (int i = 0; i < num_threads; i++) + { + DrawerThreads *queue = this; + DrawerThread *thread = &threads[i]; + thread->core = i; + thread->num_cores = num_threads; + thread->thread = std::thread([=]() { queue->WorkerMain(thread); }); + } } } diff --git a/src/swrenderer/drawers/r_thread.h b/src/swrenderer/drawers/r_thread.h index dc7e0d8c4..c2e8b9c80 100644 --- a/src/swrenderer/drawers/r_thread.h +++ b/src/swrenderer/drawers/r_thread.h @@ -30,7 +30,7 @@ #include // Use multiple threads when drawing -EXTERN_CVAR(Bool, r_multithreaded) +EXTERN_CVAR(Int, r_multithreaded) class PolyTriangleThreadData; @@ -149,6 +149,7 @@ private: static DrawerThreads *Instance(); + std::mutex threads_mutex; std::vector threads; std::mutex start_mutex; @@ -181,7 +182,7 @@ public: void Push(Types &&... args) { DrawerThreads *threads = DrawerThreads::Instance(); - if (ThreadedRender && r_multithreaded) + if (r_multithreaded != 0) { void *ptr = AllocMemory(sizeof(T)); T *command = new (ptr)T(std::forward(args)...); @@ -194,8 +195,6 @@ public: } } - bool ThreadedRender = true; - private: // Allocate memory valid for the duration of a command execution void *AllocMemory(size_t size); diff --git a/src/swrenderer/scene/r_scene.cpp b/src/swrenderer/scene/r_scene.cpp index e9f973d64..1666eb003 100644 --- a/src/swrenderer/scene/r_scene.cpp +++ b/src/swrenderer/scene/r_scene.cpp @@ -66,7 +66,7 @@ void PeekThreadedErrorPane(); EXTERN_CVAR(Int, r_clearbuffer) EXTERN_CVAR(Int, r_debug_draw) -CVAR(Bool, r_scene_multithreaded, false, 0); +CVAR(Int, r_scene_multithreaded, 0, 0); CVAR(Bool, r_models, true, CVAR_ARCHIVE | CVAR_GLOBALCONFIG); bool r_modelscene = false; @@ -210,8 +210,10 @@ namespace swrenderer if (numThreads == 0) numThreads = 4; - if (!r_scene_multithreaded || !r_multithreaded) + if (r_scene_multithreaded == 0 || r_multithreaded == 0) numThreads = 1; + else if (r_scene_multithreaded != 1) + numThreads = r_scene_multithreaded; if (numThreads != (int)Threads.size()) {