Skip to content

Commit

Permalink
Vulkan multithread: Fix race condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Nov 5, 2017
1 parent 2f305f9 commit 702e354
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions ext/native/thin3d/VulkanRenderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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++) {
Expand Down Expand Up @@ -243,6 +247,7 @@ void VulkanRenderManager::DestroyBackbuffers() {
framebuffers_.clear();

swapchainImages_.clear();
VLOG("Backbuffers Destroyed");
}

VulkanRenderManager::~VulkanRenderManager() {
Expand Down Expand Up @@ -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.

Copy link
@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.

Copy link
@hrydgard

hrydgard Nov 5, 2017

Author 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;
Expand All @@ -298,6 +303,7 @@ void VulkanRenderManager::ThreadFunc() {
}

void VulkanRenderManager::BeginFrame() {
VLOG("BeginFrame");
VkDevice device = vulkan_->GetDevice();

int curFrame = vulkan_->GetCurFrame();
Expand Down
2 changes: 1 addition & 1 deletion ext/native/thin3d/VulkanRenderManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class VulkanRenderManager {

// Swapchain.
bool hasBegun = false;
uint32_t curSwapchainImage;
uint32_t curSwapchainImage = -1;
};
FrameData frameData_[VulkanContext::MAX_INFLIGHT_FRAMES];

Expand Down

0 comments on commit 702e354

Please sign in to comment.