Skip to content

Commit

Permalink
Merge pull request #18351 from hrydgard/shutdown-fixes-better
Browse files Browse the repository at this point in the history
Better fix for shutdown crash
  • Loading branch information
hrydgard committed Oct 13, 2023
2 parents 88451f0 + 6357b95 commit a196c5e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
1 change: 1 addition & 0 deletions Common/File/FileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,7 @@ uint8_t *ReadLocalFile(const Path &filename, size_t *size) {
return nullptr;
}
fseek(file, 0, SEEK_SET);
// NOTE: If you find ~10 memory leaks from here, with very varying sizes, it might be the VFPU LUTs.
uint8_t *contents = new uint8_t[f_size + 1];
if (fread(contents, 1, f_size, file) != f_size) {
delete[] contents;
Expand Down
11 changes: 9 additions & 2 deletions Common/GPU/Vulkan/VulkanDescSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,22 @@ void VulkanDescSetPool::Reset() {
}

void VulkanDescSetPool::Destroy() {
_assert_msg_(vulkan_ != nullptr, "VulkanDescSetPool::Destroy without VulkanContext");

if (descPool_ != VK_NULL_HANDLE) {
vulkan_->Delete().QueueDeleteDescriptorPool(descPool_);
usage_ = 0;
}
sizes_.clear();
}

void VulkanDescSetPool::DestroyImmediately() {
if (descPool_ != VK_NULL_HANDLE) {
vkDestroyDescriptorPool(vulkan_->GetDevice(), descPool_, nullptr);
descPool_ = VK_NULL_HANDLE;
usage_ = 0;
}
sizes_.clear();
}

VkResult VulkanDescSetPool::Recreate(bool grow) {
_assert_msg_(vulkan_ != nullptr, "VulkanDescSetPool::Recreate without VulkanContext");

Expand Down
4 changes: 4 additions & 0 deletions Common/GPU/Vulkan/VulkanDescSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ class VulkanDescSetPool {
// Use only for the current frame.
bool Allocate(VkDescriptorSet *descriptorSets, int count, const VkDescriptorSetLayout *layouts);
void Reset();

// This queues up destruction.
void Destroy();
// This actually destroys immediately.
void DestroyImmediately();

bool IsDestroyed() const {
return !descPool_;
Expand Down
22 changes: 11 additions & 11 deletions Common/GPU/Vulkan/VulkanRenderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,20 +515,18 @@ void VulkanRenderManager::CompileThreadFunc() {
Task *task = new CreateMultiPipelinesTask(vulkan_, entries);
g_threadManager.EnqueueTask(task);
}

queueRunner_.NotifyCompileDone();
}
}

void VulkanRenderManager::DrainAndBlockCompileQueue() {
EndCurRenderStep();
std::unique_lock<std::mutex> lock(compileMutex_);
compileBlocked_ = true;
compileCond_.notify_all();
while (!compileQueue_.empty()) {
queueRunner_.WaitForCompileNotification();
}
FlushSync();
}

void VulkanRenderManager::ReleaseCompileQueue() {
Expand Down Expand Up @@ -1662,19 +1660,22 @@ VKRPipelineLayout *VulkanRenderManager::CreatePipelineLayout(BindingType *bindin
}

void VulkanRenderManager::DestroyPipelineLayout(VKRPipelineLayout *layout) {
for (int i = 0; i < VulkanContext::MAX_INFLIGHT_FRAMES; i++) {
layout->frameData[i].pool.Destroy();
}

vulkan_->Delete().QueueDeletePipelineLayout(layout->pipelineLayout);
vulkan_->Delete().QueueDeleteDescriptorSetLayout(layout->descriptorSetLayout);
for (auto iter = pipelineLayouts_.begin(); iter != pipelineLayouts_.end(); iter++) {
if (*iter == layout) {
pipelineLayouts_.erase(iter);
break;
}
}
delete layout;
vulkan_->Delete().QueueCallback([](VulkanContext *vulkan, void *userdata) {
VKRPipelineLayout *layout = (VKRPipelineLayout *)userdata;
for (int i = 0; i < VulkanContext::MAX_INFLIGHT_FRAMES; i++) {
layout->frameData[i].pool.DestroyImmediately();
}
vkDestroyPipelineLayout(vulkan->GetDevice(), layout->pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(vulkan->GetDevice(), layout->descriptorSetLayout, nullptr);

delete layout;
}, layout);
}

void VulkanRenderManager::FlushDescriptors(int frame) {
Expand All @@ -1694,7 +1695,6 @@ void VulkanRenderManager::ResetDescriptorLists(int frame) {
}

VKRPipelineLayout::~VKRPipelineLayout() {
_assert_(!pipelineLayout && !descriptorSetLayout);
_assert_(frameData[0].pool.IsDestroyed());
}

Expand Down

0 comments on commit a196c5e

Please sign in to comment.