Fix release of Vulkan resources

This commit is contained in:
2023-09-23 17:52:11 +02:00
parent 1e693a184e
commit 5b5f18b9a8
4 changed files with 21 additions and 5 deletions

View File

@@ -33,6 +33,12 @@ namespace openVulkanoCpp::Vulkan
{
}
~MemoryAllocation()
{
if (device && memory)
device.free(memory);
}
[[nodiscard]] size_t FreeSpace() const
{
return size - used;

View File

@@ -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;
}
}

View File

@@ -46,7 +46,7 @@ namespace openVulkanoCpp
vk::CommandPool* cmdPools = nullptr;
vk::CommandBuffer* cmdBuffers = nullptr;
vk::Semaphore* semaphores = nullptr;
std::vector<MemoryAllocation*> allocations;
std::vector<std::unique_ptr<MemoryAllocation>> allocations;
std::vector<std::unique_ptr<VulkanShader>> shaders;
MemoryAllocation* lastAllocation = nullptr;
std::mutex mutex;