Permalink
Browse files

Vulkan multithread: Fix race condition.

  • Loading branch information...
hrydgard committed Nov 5, 2017
1 parent 2f305f9 commit 702e354a6aec88fdbdcd60351baf80989ec09c66
Showing with 9 additions and 3 deletions.
  1. +8 −2 ext/native/thin3d/VulkanRenderManager.cpp
  2. +1 −1 ext/native/thin3d/VulkanRenderManager.h
@@ -187,11 +187,14 @@ void VulkanRenderManager::CreateBackbuffers() {
curWidth_ = -1;
curHeight_ = -1;
VLOG("Backbuffers Created");
// Start the thread.
if (useThread) {
run_ = true;
// Won't necessarily be 0.
threadInitFrame_ = vulkan_->GetCurFrame();
VLOG("starting thread");
thread_ = std::thread(&VulkanRenderManager::ThreadFunc, this);
}
}
@@ -212,6 +215,7 @@ void VulkanRenderManager::StopThread(bool shutdown) {
}
}
thread_.join();
VLOG("thread joined.");
// Resignal fences for next time around - must be done after join.
for (int i = 0; i < vulkan_->GetInflightFrames(); i++) {
@@ -243,6 +247,7 @@ void VulkanRenderManager::DestroyBackbuffers() {
framebuffers_.clear();
swapchainImages_.clear();
VLOG("Backbuffers Destroyed");
}
VulkanRenderManager::~VulkanRenderManager() {
@@ -283,8 +288,8 @@ void VulkanRenderManager::ThreadFunc() {
}
VLOG("PULL: frame[%d].readyForRun = false", threadFrame);
frameData.readyForRun = false;
if (!run_) // quick exit if bailing.
return;
// Previously we had a quick exit here that avoided calling Run() if run_ was suddenly false,
// but that created a race condition where frames could end up not finished properly on resize etc.

This comment has been minimized.

Show comment
Hide comment
@unknownbrackets

unknownbrackets Nov 5, 2017

Collaborator

You're right, this is probably better, probably never end up recreating the fences now.

Though, I wonder how far away we are from DeviceLost in Vulkan...

-[Unknown]

@unknownbrackets

unknownbrackets Nov 5, 2017

Collaborator

You're right, this is probably better, probably never end up recreating the fences now.

Though, I wonder how far away we are from DeviceLost in Vulkan...

-[Unknown]

This comment has been minimized.

Show comment
Hide comment
@hrydgard

hrydgard Nov 5, 2017

Owner

Hm, right, need to deal with all of that in Android at least probably..

@hrydgard

hrydgard Nov 5, 2017

Owner

Hm, right, need to deal with all of that in Android at least probably..

// Only increment next time if we're done.
nextFrame = frameData.type == VKRRunType::END;
@@ -298,6 +303,7 @@ void VulkanRenderManager::ThreadFunc() {
}
void VulkanRenderManager::BeginFrame() {
VLOG("BeginFrame");
VkDevice device = vulkan_->GetDevice();
int curFrame = vulkan_->GetCurFrame();
@@ -250,7 +250,7 @@ class VulkanRenderManager {
// Swapchain.
bool hasBegun = false;
uint32_t curSwapchainImage;
uint32_t curSwapchainImage = -1;
};
FrameData frameData_[VulkanContext::MAX_INFLIGHT_FRAMES];

0 comments on commit 702e354

Please sign in to comment.