- remove vk_submit_multithread again as it seemed to have no effect on performance and only complicated the code

This commit is contained in:
Magnus Norddahl 2019-04-19 23:26:06 +02:00
parent 458da39c39
commit 779cb42578
3 changed files with 17 additions and 110 deletions

View file

@ -166,7 +166,7 @@ void VkRenderState::Apply(int dt)
if (mApplyCount >= vk_submit_size) if (mApplyCount >= vk_submit_size)
{ {
EndRenderPass(); EndRenderPass();
GetVulkanFrameBuffer()->FlushCommands(); GetVulkanFrameBuffer()->FlushCommands(false);
mApplyCount = 0; mApplyCount = 0;
} }

View file

@ -68,8 +68,6 @@ EXTERN_CVAR(Int, screenblocks)
EXTERN_CVAR(Bool, cl_capfps) EXTERN_CVAR(Bool, cl_capfps)
EXTERN_CVAR(Bool, gl_no_skyclear) EXTERN_CVAR(Bool, gl_no_skyclear)
CVAR(Bool, vk_submit_multithread, false, 0)
extern bool NoInterpolateView; extern bool NoInterpolateView;
extern int rendered_commandbuffers; extern int rendered_commandbuffers;
int current_rendered_commandbuffers; int current_rendered_commandbuffers;
@ -82,7 +80,6 @@ VulkanFrameBuffer::VulkanFrameBuffer(void *hMonitor, bool fullscreen, VulkanDevi
swapChain = std::make_unique<VulkanSwapChain>(device); swapChain = std::make_unique<VulkanSwapChain>(device);
mSwapChainImageAvailableSemaphore.reset(new VulkanSemaphore(device)); mSwapChainImageAvailableSemaphore.reset(new VulkanSemaphore(device));
mRenderFinishedSemaphore.reset(new VulkanSemaphore(device)); mRenderFinishedSemaphore.reset(new VulkanSemaphore(device));
mRenderFinishedFence.reset(new VulkanFence(device));
for (auto &semaphore : mSubmitSemaphore) for (auto &semaphore : mSubmitSemaphore)
semaphore.reset(new VulkanSemaphore(device)); semaphore.reset(new VulkanSemaphore(device));
@ -95,8 +92,6 @@ VulkanFrameBuffer::VulkanFrameBuffer(void *hMonitor, bool fullscreen, VulkanDevi
VulkanFrameBuffer::~VulkanFrameBuffer() VulkanFrameBuffer::~VulkanFrameBuffer()
{ {
StopSubmitThread();
// All descriptors must be destroyed before the descriptor pool in renderpass manager is destroyed // All descriptors must be destroyed before the descriptor pool in renderpass manager is destroyed
for (VkHardwareTexture *cur = VkHardwareTexture::First; cur; cur = cur->Next) for (VkHardwareTexture *cur = VkHardwareTexture::First; cur; cur = cur->Next)
cur->Reset(); cur->Reset();
@ -195,53 +190,7 @@ void VulkanFrameBuffer::DeleteFrameObjects()
FrameDeleteList.CommandBuffers.clear(); FrameDeleteList.CommandBuffers.clear();
} }
void VulkanFrameBuffer::StartSubmitThread() void VulkanFrameBuffer::FlushCommands(VulkanCommandBuffer **commands, size_t count, bool finish)
{
if (!mSubmitThread.joinable())
{
mSubmitThread = std::thread([this] { SubmitThreadMain(); });
}
}
void VulkanFrameBuffer::StopSubmitThread()
{
if (mSubmitThread.joinable())
{
std::unique_lock<std::mutex> lock(mSubmitMutex);
mSubmitExitFlag = true;
lock.unlock();
mSubmitCondVar.notify_all();
mSubmitThread.join();
mSubmitThread = {};
lock.lock();
mSubmitExitFlag = false;
}
}
void VulkanFrameBuffer::SubmitThreadMain()
{
while (true)
{
std::unique_lock<std::mutex> lock(mSubmitMutex);
mSubmitCondVar.wait(lock, [&]() { return mSubmitExitFlag || !mSubmitQueue.empty(); });
std::vector<VulkanCommandBuffer*> commands;
commands.swap(mSubmitQueue);
mSubmitItemsActive = commands.size();
lock.unlock();
FlushCommands(commands.data(), commands.size());
lock.lock();
mSubmitItemsActive = 0;
if (mSubmitQueue.empty())
mSubmitDone.notify_all();
if (mSubmitExitFlag)
break;
}
}
void VulkanFrameBuffer::FlushCommands(VulkanCommandBuffer **commands, size_t count)
{ {
int currentIndex = nextSubmitQueue % submitQueueSize; int currentIndex = nextSubmitQueue % submitQueueSize;
@ -259,12 +208,18 @@ void VulkanFrameBuffer::FlushCommands(VulkanCommandBuffer **commands, size_t cou
if (nextSubmitQueue > 0) if (nextSubmitQueue > 0)
submit.addWait(VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, mSubmitSemaphore[(nextSubmitQueue - 1) % submitQueueSize].get()); submit.addWait(VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, mSubmitSemaphore[(nextSubmitQueue - 1) % submitQueueSize].get());
if (finish && presentImageIndex != 0xffffffff)
{
submit.addWait(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, mSwapChainImageAvailableSemaphore.get());
submit.addSignal(mRenderFinishedSemaphore.get());
}
submit.addSignal(mSubmitSemaphore[currentIndex].get()); submit.addSignal(mSubmitSemaphore[currentIndex].get());
submit.execute(device, device->graphicsQueue, mSubmitFence[currentIndex].get()); submit.execute(device, device->graphicsQueue, mSubmitFence[currentIndex].get());
nextSubmitQueue++; nextSubmitQueue++;
} }
void VulkanFrameBuffer::FlushCommands() void VulkanFrameBuffer::FlushCommands(bool finish)
{ {
if (mDrawCommands || mTransferCommands) if (mDrawCommands || mTransferCommands)
{ {
@ -285,22 +240,9 @@ void VulkanFrameBuffer::FlushCommands()
FrameDeleteList.CommandBuffers.push_back(std::move(mDrawCommands)); FrameDeleteList.CommandBuffers.push_back(std::move(mDrawCommands));
} }
current_rendered_commandbuffers += (int)count; FlushCommands(commands, count, finish);
if (vk_submit_multithread) current_rendered_commandbuffers += (int)count;
{
StartSubmitThread();
std::unique_lock<std::mutex> lock(mSubmitMutex);
for (size_t i = 0; i < count; i++)
mSubmitQueue.push_back(commands[i]);
lock.unlock();
mSubmitCondVar.notify_all();
}
else
{
StopSubmitThread();
FlushCommands(commands, count);
}
} }
} }
@ -316,26 +258,7 @@ void VulkanFrameBuffer::WaitForCommands(bool finish)
mPostprocess->DrawPresentTexture(mOutputLetterbox, true, true); mPostprocess->DrawPresentTexture(mOutputLetterbox, true, true);
} }
FlushCommands(); FlushCommands(finish);
std::unique_lock<std::mutex> lock(mSubmitMutex);
mSubmitDone.wait(lock, [&]() { return mSubmitQueue.empty() && mSubmitItemsActive == 0; });
lock.unlock();
QueueSubmit submit;
if (nextSubmitQueue > 0)
{
submit.addWait(VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, mSubmitSemaphore[(nextSubmitQueue - 1) % submitQueueSize].get());
}
if (finish && presentImageIndex != 0xffffffff)
{
submit.addWait(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, mSwapChainImageAvailableSemaphore.get());
submit.addSignal(mRenderFinishedSemaphore.get());
}
submit.execute(device, device->graphicsQueue, mRenderFinishedFence.get());
if (finish) if (finish)
{ {
@ -345,12 +268,11 @@ void VulkanFrameBuffer::WaitForCommands(bool finish)
swapChain->QueuePresent(presentImageIndex, mRenderFinishedSemaphore.get()); swapChain->QueuePresent(presentImageIndex, mRenderFinishedSemaphore.get());
} }
VkFence waitFences[submitQueueSize + 1]; VkFence waitFences[submitQueueSize];
waitFences[0] = mRenderFinishedFence->fence;
for (int i = 0; i < submitQueueSize; i++) for (int i = 0; i < submitQueueSize; i++)
waitFences[i + 1] = mSubmitFence[i]->fence; waitFences[i] = mSubmitFence[i]->fence;
int numWaitFences = 1 + MIN(nextSubmitQueue, (int)submitQueueSize); int numWaitFences = MIN(nextSubmitQueue, (int)submitQueueSize);
vkWaitForFences(device->device, numWaitFences, waitFences, VK_TRUE, std::numeric_limits<uint64_t>::max()); vkWaitForFences(device->device, numWaitFences, waitFences, VK_TRUE, std::numeric_limits<uint64_t>::max());
vkResetFences(device->device, numWaitFences, waitFences); vkResetFences(device->device, numWaitFences, waitFences);

View file

@ -3,9 +3,6 @@
#include "gl_sysfb.h" #include "gl_sysfb.h"
#include "vk_device.h" #include "vk_device.h"
#include "vk_objects.h" #include "vk_objects.h"
#include <thread>
#include <mutex>
#include <condition_variable>
struct FRenderViewpoint; struct FRenderViewpoint;
class VkSamplerManager; class VkSamplerManager;
@ -37,7 +34,7 @@ public:
VkPostprocess *GetPostprocess() { return mPostprocess.get(); } VkPostprocess *GetPostprocess() { return mPostprocess.get(); }
VkRenderBuffers *GetBuffers() { return mActiveRenderBuffers; } VkRenderBuffers *GetBuffers() { return mActiveRenderBuffers; }
void FlushCommands(); void FlushCommands(bool finish);
unsigned int GetLightBufferBlockSize() const; unsigned int GetLightBufferBlockSize() const;
@ -114,10 +111,7 @@ private:
void CopyScreenToBuffer(int w, int h, void *data); void CopyScreenToBuffer(int w, int h, void *data);
void UpdateShadowMap(); void UpdateShadowMap();
void DeleteFrameObjects(); void DeleteFrameObjects();
void StartSubmitThread(); void FlushCommands(VulkanCommandBuffer **commands, size_t count, bool finish);
void StopSubmitThread();
void SubmitThreadMain();
void FlushCommands(VulkanCommandBuffer **commands, size_t count);
std::unique_ptr<VkShaderManager> mShaderManager; std::unique_ptr<VkShaderManager> mShaderManager;
std::unique_ptr<VkSamplerManager> mSamplerManager; std::unique_ptr<VkSamplerManager> mSamplerManager;
@ -138,17 +132,8 @@ private:
std::unique_ptr<VulkanSemaphore> mSwapChainImageAvailableSemaphore; std::unique_ptr<VulkanSemaphore> mSwapChainImageAvailableSemaphore;
std::unique_ptr<VulkanSemaphore> mRenderFinishedSemaphore; std::unique_ptr<VulkanSemaphore> mRenderFinishedSemaphore;
std::unique_ptr<VulkanFence> mRenderFinishedFence;
VkRenderBuffers *mActiveRenderBuffers = nullptr; VkRenderBuffers *mActiveRenderBuffers = nullptr;
std::thread mSubmitThread;
std::condition_variable mSubmitCondVar;
std::condition_variable mSubmitDone;
std::mutex mSubmitMutex;
std::vector<VulkanCommandBuffer*> mSubmitQueue;
size_t mSubmitItemsActive = 0;
bool mSubmitExitFlag = false;
}; };
inline VulkanFrameBuffer *GetVulkanFrameBuffer() { return static_cast<VulkanFrameBuffer*>(screen); } inline VulkanFrameBuffer *GetVulkanFrameBuffer() { return static_cast<VulkanFrameBuffer*>(screen); }