- add support for specifying the exact thread count to r_multithreaded and r_scene_multithreaded

This commit is contained in:
Magnus Norddahl 2018-07-28 04:57:23 +02:00
parent c60b4239ed
commit 990f02d7c5
5 changed files with 33 additions and 20 deletions

View File

@ -44,7 +44,7 @@
void PeekThreadedErrorPane(); void PeekThreadedErrorPane();
#endif #endif
EXTERN_CVAR(Bool, r_scene_multithreaded); EXTERN_CVAR(Int, r_scene_multithreaded);
PolyRenderThread::PolyRenderThread(int threadIndex) : MainThread(threadIndex == 0), ThreadIndex(threadIndex) PolyRenderThread::PolyRenderThread(int threadIndex) : MainThread(threadIndex == 0), ThreadIndex(threadIndex)
{ {
@ -151,8 +151,10 @@ void PolyRenderThreads::RenderThreadSlices(int totalcount, std::function<void(Po
if (numThreads == 0) if (numThreads == 0)
numThreads = 4; numThreads = 4;
if (!r_scene_multithreaded || !r_multithreaded) if (r_scene_multithreaded == 0 || r_multithreaded == 0)
numThreads = 1; numThreads = 1;
else if (r_scene_multithreaded != 1)
numThreads = r_scene_multithreaded;
if (numThreads != (int)Threads.size()) if (numThreads != (int)Threads.size())
{ {

View File

@ -8,7 +8,7 @@
struct FSWColormap; struct FSWColormap;
struct FLightNode; struct FLightNode;
EXTERN_CVAR(Bool, r_multithreaded); EXTERN_CVAR(Int, r_multithreaded);
EXTERN_CVAR(Bool, r_magfilter); EXTERN_CVAR(Bool, r_magfilter);
EXTERN_CVAR(Bool, r_minfilter); EXTERN_CVAR(Bool, r_minfilter);
EXTERN_CVAR(Bool, r_mipmap); EXTERN_CVAR(Bool, r_mipmap);

View File

@ -39,7 +39,7 @@
void PeekThreadedErrorPane(); void PeekThreadedErrorPane();
#endif #endif
CVAR(Bool, r_multithreaded, true, CVAR_ARCHIVE | CVAR_GLOBALCONFIG); CVAR(Int, r_multithreaded, 1, CVAR_ARCHIVE | CVAR_GLOBALCONFIG);
CVAR(Int, r_debug_draw, 0, 0); CVAR(Int, r_debug_draw, 0, 0);
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
@ -66,10 +66,11 @@ void DrawerThreads::Execute(DrawerCommandQueuePtr commands)
auto queue = Instance(); auto queue = Instance();
queue->StartThreads();
// Add to queue and awaken worker threads // Add to queue and awaken worker threads
std::unique_lock<std::mutex> start_lock(queue->start_mutex); std::unique_lock<std::mutex> start_lock(queue->start_mutex);
std::unique_lock<std::mutex> end_lock(queue->end_mutex); std::unique_lock<std::mutex> end_lock(queue->end_mutex);
queue->StartThreads();
queue->active_commands.push_back(commands); queue->active_commands.push_back(commands);
queue->tasks_left += queue->threads.size(); queue->tasks_left += queue->threads.size();
end_lock.unlock(); end_lock.unlock();
@ -171,22 +172,31 @@ void DrawerThreads::WorkerMain(DrawerThread *thread)
void DrawerThreads::StartThreads() void DrawerThreads::StartThreads()
{ {
if (!threads.empty()) std::unique_lock<std::mutex> lock(threads_mutex);
return;
int num_threads = std::thread::hardware_concurrency(); int num_threads = std::thread::hardware_concurrency();
if (num_threads == 0) if (num_threads == 0)
num_threads = 4; 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; StopThreads();
DrawerThread *thread = &threads[i];
thread->core = i; threads.resize(num_threads);
thread->num_cores = num_threads;
thread->thread = std::thread([=]() { queue->WorkerMain(thread); }); 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); });
}
} }
} }

View File

@ -30,7 +30,7 @@
#include <condition_variable> #include <condition_variable>
// Use multiple threads when drawing // Use multiple threads when drawing
EXTERN_CVAR(Bool, r_multithreaded) EXTERN_CVAR(Int, r_multithreaded)
class PolyTriangleThreadData; class PolyTriangleThreadData;
@ -149,6 +149,7 @@ private:
static DrawerThreads *Instance(); static DrawerThreads *Instance();
std::mutex threads_mutex;
std::vector<DrawerThread> threads; std::vector<DrawerThread> threads;
std::mutex start_mutex; std::mutex start_mutex;
@ -181,7 +182,7 @@ public:
void Push(Types &&... args) void Push(Types &&... args)
{ {
DrawerThreads *threads = DrawerThreads::Instance(); DrawerThreads *threads = DrawerThreads::Instance();
if (ThreadedRender && r_multithreaded) if (r_multithreaded != 0)
{ {
void *ptr = AllocMemory(sizeof(T)); void *ptr = AllocMemory(sizeof(T));
T *command = new (ptr)T(std::forward<Types>(args)...); T *command = new (ptr)T(std::forward<Types>(args)...);
@ -194,8 +195,6 @@ public:
} }
} }
bool ThreadedRender = true;
private: private:
// Allocate memory valid for the duration of a command execution // Allocate memory valid for the duration of a command execution
void *AllocMemory(size_t size); void *AllocMemory(size_t size);

View File

@ -66,7 +66,7 @@ void PeekThreadedErrorPane();
EXTERN_CVAR(Int, r_clearbuffer) EXTERN_CVAR(Int, r_clearbuffer)
EXTERN_CVAR(Int, r_debug_draw) 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); CVAR(Bool, r_models, true, CVAR_ARCHIVE | CVAR_GLOBALCONFIG);
bool r_modelscene = false; bool r_modelscene = false;
@ -210,8 +210,10 @@ namespace swrenderer
if (numThreads == 0) if (numThreads == 0)
numThreads = 4; numThreads = 4;
if (!r_scene_multithreaded || !r_multithreaded) if (r_scene_multithreaded == 0 || r_multithreaded == 0)
numThreads = 1; numThreads = 1;
else if (r_scene_multithreaded != 1)
numThreads = r_scene_multithreaded;
if (numThreads != (int)Threads.size()) if (numThreads != (int)Threads.size())
{ {