From cbd488e15740a50f568b1894d9ee8f303fc6694a Mon Sep 17 00:00:00 2001 From: rogerman Date: Wed, 25 Jul 2018 15:18:36 -0700 Subject: [PATCH] SoftRasterizer: Force the number of rendering threads to be a power-of-two since any additional threads that are not power-of-two will be wasted. --- desmume/src/frontend/cocoa/cocoa_GPU.mm | 31 +------------------------ desmume/src/rasterize.cpp | 19 ++++++++++++++- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/desmume/src/frontend/cocoa/cocoa_GPU.mm b/desmume/src/frontend/cocoa/cocoa_GPU.mm index 8aa520031..291533b3f 100644 --- a/desmume/src/frontend/cocoa/cocoa_GPU.mm +++ b/desmume/src/frontend/cocoa/cocoa_GPU.mm @@ -448,36 +448,7 @@ public: if (numberThreads == 0) { isCPUCoreCountAuto = YES; - - if (numberCores >= 96) - { - numberCores = 128; - } - else if (numberCores >= 48) - { - numberCores = 64; - } - else if (numberCores >= 24) - { - numberCores = 32; - } - else if (numberCores >= 16) - { - numberCores = 16; - } - else if (numberCores >= 8) - { - numberCores = 8; - } - else if (numberCores >= 4) - { - numberCores = 4; - } - else if (numberCores >= 2) - { - numberCores = 2; - } - else + if (numberCores < 2) { numberCores = 1; } diff --git a/desmume/src/rasterize.cpp b/desmume/src/rasterize.cpp index 0617a0ac0..130c58637 100644 --- a/desmume/src/rasterize.cpp +++ b/desmume/src/rasterize.cpp @@ -1444,7 +1444,24 @@ SoftRasterizerRenderer::SoftRasterizerRenderer() _HACK_viewer_rasterizerUnit.SetSLI(0, 1, false); - _threadCount = CommonSettings.num_cores; + const size_t coreCount = CommonSettings.num_cores; + _threadCount = coreCount; + + // SoftRasterizer works best when the number of threads is a power-of-two. + // Ensure that the thread count is set to the previous power-of-two if the + // core count isn't already a power-of-two. + _threadCount--; + _threadCount |= (_threadCount >> 1); + _threadCount |= (_threadCount >> 2); + _threadCount |= (_threadCount >> 4); + _threadCount |= (_threadCount >> 8); + _threadCount |= (_threadCount >> 16); + _threadCount++; + + if (_threadCount != coreCount) + { + _threadCount >>= 1; + } if (_threadCount > SOFTRASTERIZER_MAX_THREADS) {