From 5b5f18b9a8ed6dfff74216e92f3e85578b8c7b3b Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Sat, 23 Sep 2023 17:52:11 +0200 Subject: [PATCH] Fix release of Vulkan resources --- openVulkanoCpp/Host/GraphicsAppManager.cpp | 2 +- .../Vulkan/Resources/ManagedResource.hpp | 6 ++++++ .../Vulkan/Resources/ResourceManager.cpp | 16 +++++++++++++--- .../Vulkan/Resources/ResourceManager.hpp | 2 +- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/openVulkanoCpp/Host/GraphicsAppManager.cpp b/openVulkanoCpp/Host/GraphicsAppManager.cpp index 7f9f6ca..beee441 100644 --- a/openVulkanoCpp/Host/GraphicsAppManager.cpp +++ b/openVulkanoCpp/Host/GraphicsAppManager.cpp @@ -139,9 +139,9 @@ namespace openVulkanoCpp void GraphicsAppManager::ShutDown() { Logger::MANAGER->info("Shutting down ..."); + app->Close(); renderer->Close(); window->Close(); - app->Close(); if (platform) platform->Close(); windowTitleFormat = ""; Logger::MANAGER->info("Shutdown complete"); diff --git a/openVulkanoCpp/Vulkan/Resources/ManagedResource.hpp b/openVulkanoCpp/Vulkan/Resources/ManagedResource.hpp index b239e61..0dc45b7 100644 --- a/openVulkanoCpp/Vulkan/Resources/ManagedResource.hpp +++ b/openVulkanoCpp/Vulkan/Resources/ManagedResource.hpp @@ -33,6 +33,12 @@ namespace openVulkanoCpp::Vulkan { } + ~MemoryAllocation() + { + if (device && memory) + device.free(memory); + } + [[nodiscard]] size_t FreeSpace() const { return size - used; diff --git a/openVulkanoCpp/Vulkan/Resources/ResourceManager.cpp b/openVulkanoCpp/Vulkan/Resources/ResourceManager.cpp index f83c1c0..95b0e68 100644 --- a/openVulkanoCpp/Vulkan/Resources/ResourceManager.cpp +++ b/openVulkanoCpp/Vulkan/Resources/ResourceManager.cpp @@ -79,11 +79,21 @@ namespace openVulkanoCpp::Vulkan { device.freeCommandBuffers(cmdPools[i], 1, &cmdBuffers[i]); device.destroyCommandPool(cmdPools[i]); + device.destroy(semaphores[i]); } + cmdPools = nullptr; + cmdBuffers = nullptr; + semaphores = nullptr; device.destroyDescriptorPool(descriptorPool); + allocations.clear(); + lastAllocation = nullptr; + toFree.clear(); + recycleBuffers.clear(); + descriptorSetLayoutCache.clear(); shaders.clear(); cmdBuffers = nullptr; cmdPools = nullptr; + transferQueue = nullptr; device = nullptr; } @@ -311,18 +321,18 @@ namespace openVulkanoCpp::Vulkan MemoryAllocation* alloc = new MemoryAllocation(size, type, device); const vk::MemoryAllocateInfo allocInfo = { size, type }; alloc->memory = device.allocateMemory(allocInfo); - if (addToCache) allocations.push_back(alloc); + if (addToCache) allocations.emplace_back(alloc); return alloc; } MemoryAllocation* ResourceManager::GetFreeMemoryAllocation(size_t size, uint32_t type, bool createIfAllFull) { MemoryAllocation* alloc = nullptr; - for (MemoryAllocation* allocation : allocations) + for (auto& allocation : allocations) { if (allocation->type == type && allocation->FreeSpace() >= size) { - alloc = allocation; + alloc = allocation.get(); break; } } diff --git a/openVulkanoCpp/Vulkan/Resources/ResourceManager.hpp b/openVulkanoCpp/Vulkan/Resources/ResourceManager.hpp index 58b461d..f983dea 100644 --- a/openVulkanoCpp/Vulkan/Resources/ResourceManager.hpp +++ b/openVulkanoCpp/Vulkan/Resources/ResourceManager.hpp @@ -46,7 +46,7 @@ namespace openVulkanoCpp vk::CommandPool* cmdPools = nullptr; vk::CommandBuffer* cmdBuffers = nullptr; vk::Semaphore* semaphores = nullptr; - std::vector allocations; + std::vector> allocations; std::vector> shaders; MemoryAllocation* lastAllocation = nullptr; std::mutex mutex;