- 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();
#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::function<void(Po
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())
{

View file

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

View file

@ -39,7 +39,7 @@
void PeekThreadedErrorPane();
#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);
/////////////////////////////////////////////////////////////////////////////
@ -66,10 +66,11 @@ void DrawerThreads::Execute(DrawerCommandQueuePtr commands)
auto queue = Instance();
queue->StartThreads();
// Add to queue and awaken worker threads
std::unique_lock<std::mutex> start_lock(queue->start_mutex);
std::unique_lock<std::mutex> 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<std::mutex> 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); });
}
}
}

View file

@ -30,7 +30,7 @@
#include <condition_variable>
// 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<DrawerThread> 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<Types>(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);

View file

@ -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())
{