From 059c28d5c0f2ddcdce2fda620a82f0eed338b6db Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Tue, 22 Feb 2022 16:20:43 -0800 Subject: [PATCH] plugging more leaks --- src/Refresh_Driver_Vulkan.c | 262 ++++++++++++++++++++---------------- 1 file changed, 147 insertions(+), 115 deletions(-) diff --git a/src/Refresh_Driver_Vulkan.c b/src/Refresh_Driver_Vulkan.c index 6e97606..635eaa5 100644 --- a/src/Refresh_Driver_Vulkan.c +++ b/src/Refresh_Driver_Vulkan.c @@ -3895,18 +3895,17 @@ static void VULKAN_INTERNAL_RecreateSwapchain( ) { CreateSwapchainResult createSwapchainResult; - renderer->vkDeviceWaitIdle(renderer->logicalDevice); + VULKAN_Wait((Refresh_Renderer*)renderer); VULKAN_INTERNAL_DestroySwapchain(renderer, windowHandle); createSwapchainResult = VULKAN_INTERNAL_CreateSwapchain(renderer, windowHandle); if (createSwapchainResult == CREATE_SWAPCHAIN_FAIL) { - Refresh_LogError("Failed to recreate swapchain!"); return; } - renderer->vkDeviceWaitIdle(renderer->logicalDevice); + VULKAN_Wait((Refresh_Renderer*)renderer); } /* Command Buffers */ @@ -3980,10 +3979,25 @@ static void VULKAN_DestroyDevice( VULKAN_Wait(device->driverData); + SDL_free(renderer->submittedCommandBuffers); + VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->dummyVertexUniformBuffer->vulkanBuffer); VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->dummyFragmentUniformBuffer->vulkanBuffer); VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->dummyComputeUniformBuffer->vulkanBuffer); + SDL_free(renderer->dummyVertexUniformBuffer); + SDL_free(renderer->dummyFragmentUniformBuffer); + SDL_free(renderer->dummyComputeUniformBuffer); + + for (i = 0; i < renderer->transferBufferPool.availableBufferCount; i += 1) + { + VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->transferBufferPool.availableBuffers[i]->buffer); + SDL_free(renderer->transferBufferPool.availableBuffers[i]); + } + + SDL_free(renderer->transferBufferPool.availableBuffers); + SDL_DestroyMutex(renderer->transferBufferPool.lock); + for (i = 0; i < renderer->availableFenceCount; i += 1) { renderer->vkDestroyFence( @@ -3993,6 +4007,8 @@ static void VULKAN_DestroyDevice( ); } + SDL_free(renderer->availableFences); + for (i = 0; i < NUM_COMMAND_POOL_BUCKETS; i += 1) { commandPoolHashArray = renderer->commandPoolHashTable.buckets[i]; @@ -4015,11 +4031,23 @@ static void VULKAN_DestroyDevice( graphicsPipelineLayoutHashArray = renderer->graphicsPipelineLayoutHashTable.buckets[i]; for (j = 0; j < graphicsPipelineLayoutHashArray.count; j += 1) { + VULKAN_INTERNAL_DestroyDescriptorSetCache( + renderer, + graphicsPipelineLayoutHashArray.elements[j].value->vertexSamplerDescriptorSetCache + ); + + VULKAN_INTERNAL_DestroyDescriptorSetCache( + renderer, + graphicsPipelineLayoutHashArray.elements[j].value->fragmentSamplerDescriptorSetCache + ); + renderer->vkDestroyPipelineLayout( renderer->logicalDevice, graphicsPipelineLayoutHashArray.elements[j].value->pipelineLayout, NULL ); + + SDL_free(graphicsPipelineLayoutHashArray.elements[j].value); } if (graphicsPipelineLayoutHashArray.elements != NULL) @@ -4045,6 +4073,8 @@ static void VULKAN_DestroyDevice( computePipelineLayoutHashArray.elements[j].value->pipelineLayout, NULL ); + + SDL_free(computePipelineLayoutHashArray.elements[j].value); } if (computePipelineLayoutHashArray.elements != NULL) @@ -5695,7 +5725,7 @@ static Refresh_Buffer* VULKAN_CreateBuffer( Refresh_BufferUsageFlags usageFlags, uint32_t sizeInBytes ) { - VulkanBuffer* buffer = SDL_malloc(sizeof(VulkanBuffer)); + VulkanBuffer* buffer; VkBufferUsageFlags vulkanUsageFlags = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; @@ -7459,7 +7489,7 @@ static void VULKAN_INTERNAL_AllocateCommandBuffers( VkResult vulkanResult; uint32_t i; VkCommandBuffer *commandBuffers = SDL_stack_alloc(VkCommandBuffer, allocateCount); - VulkanCommandBuffer *currentVulkanCommandBuffer; + VulkanCommandBuffer *commandBuffer; vulkanCommandPool->inactiveCommandBufferCapacity += allocateCount; @@ -7490,23 +7520,117 @@ static void VULKAN_INTERNAL_AllocateCommandBuffers( for (i = 0; i < allocateCount; i += 1) { - currentVulkanCommandBuffer = SDL_malloc(sizeof(VulkanCommandBuffer)); - currentVulkanCommandBuffer->commandPool = vulkanCommandPool; - currentVulkanCommandBuffer->commandBuffer = commandBuffers[i]; - currentVulkanCommandBuffer->boundComputeBufferCount = 0; - currentVulkanCommandBuffer->transferBufferCapacity = 4; - currentVulkanCommandBuffer->transferBufferCount = 0; - currentVulkanCommandBuffer->transferBuffers = SDL_malloc( - currentVulkanCommandBuffer->transferBufferCapacity * sizeof(VulkanTransferBuffer*) + commandBuffer = SDL_malloc(sizeof(VulkanCommandBuffer)); + commandBuffer->commandPool = vulkanCommandPool; + commandBuffer->commandBuffer = commandBuffers[i]; + commandBuffer->boundComputeBufferCount = 0; + + commandBuffer->present = 0; + commandBuffer->presentWindowHandle = NULL; + commandBuffer->presentSwapchainImageIndex = 0; + commandBuffer->needNewSwapchain = 0; + + /* Transfer buffer tracking */ + + commandBuffer->transferBufferCapacity = 4; + commandBuffer->transferBufferCount = 0; + commandBuffer->transferBuffers = SDL_malloc( + commandBuffer->transferBufferCapacity * sizeof(VulkanTransferBuffer*) + ); + + /* Bound buffer tracking */ + + commandBuffer->boundUniformBufferCapacity = 16; + commandBuffer->boundUniformBufferCount = 0; + commandBuffer->boundUniformBuffers = SDL_malloc( + commandBuffer->boundUniformBufferCapacity * sizeof(VulkanUniformBuffer*) + ); + + /* Descriptor set tracking */ + + commandBuffer->boundDescriptorSetDataCapacity = 16; + commandBuffer->boundDescriptorSetDataCount = 0; + commandBuffer->boundDescriptorSetDatas = SDL_malloc( + commandBuffer->boundDescriptorSetDataCapacity * sizeof(DescriptorSetData) + ); + + /* Deferred destroy storage */ + + commandBuffer->renderTargetsToDestroyCapacity = 16; + commandBuffer->renderTargetsToDestroyCount = 0; + + commandBuffer->renderTargetsToDestroy = (VulkanRenderTarget**) SDL_malloc( + sizeof(VulkanRenderTarget*) * + commandBuffer->renderTargetsToDestroyCapacity + ); + + commandBuffer->texturesToDestroyCapacity = 16; + commandBuffer->texturesToDestroyCount = 0; + + commandBuffer->texturesToDestroy = (VulkanTexture**)SDL_malloc( + sizeof(VulkanTexture*) * + commandBuffer->texturesToDestroyCapacity + ); + + commandBuffer->buffersToDestroyCapacity = 16; + commandBuffer->buffersToDestroyCount = 0; + + commandBuffer->buffersToDestroy = (VulkanBuffer**) SDL_malloc( + sizeof(VulkanBuffer*) * + commandBuffer->buffersToDestroyCapacity + ); + + commandBuffer->graphicsPipelinesToDestroyCapacity = 16; + commandBuffer->graphicsPipelinesToDestroyCount = 0; + + commandBuffer->graphicsPipelinesToDestroy = (VulkanGraphicsPipeline**) SDL_malloc( + sizeof(VulkanGraphicsPipeline*) * + commandBuffer->graphicsPipelinesToDestroyCapacity + ); + + commandBuffer->computePipelinesToDestroyCapacity = 16; + commandBuffer->computePipelinesToDestroyCount = 0; + + commandBuffer->computePipelinesToDestroy = (VulkanComputePipeline**) SDL_malloc( + sizeof(VulkanComputePipeline*) * + commandBuffer->computePipelinesToDestroyCapacity + ); + + commandBuffer->shaderModulesToDestroyCapacity = 16; + commandBuffer->shaderModulesToDestroyCount = 0; + + commandBuffer->shaderModulesToDestroy = (VkShaderModule*) SDL_malloc( + sizeof(VkShaderModule) * + commandBuffer->shaderModulesToDestroyCapacity + ); + + commandBuffer->samplersToDestroyCapacity = 16; + commandBuffer->samplersToDestroyCount = 0; + + commandBuffer->samplersToDestroy = (VkSampler*) SDL_malloc( + sizeof(VkSampler) * + commandBuffer->samplersToDestroyCapacity + ); + + commandBuffer->framebuffersToDestroyCapacity = 16; + commandBuffer->framebuffersToDestroyCount = 0; + + commandBuffer->framebuffersToDestroy = (VulkanFramebuffer**) SDL_malloc( + sizeof(VulkanFramebuffer*) * + commandBuffer->framebuffersToDestroyCapacity + ); + + commandBuffer->renderPassesToDestroyCapacity = 16; + commandBuffer->renderPassesToDestroyCount = 0; + + commandBuffer->renderPassesToDestroy = (VkRenderPass*) SDL_malloc( + sizeof(VkRenderPass) * + commandBuffer->renderPassesToDestroyCapacity ); - currentVulkanCommandBuffer->present = 0; - currentVulkanCommandBuffer->presentWindowHandle = NULL; - currentVulkanCommandBuffer->presentSwapchainImageIndex = 0; - currentVulkanCommandBuffer->needNewSwapchain = 0; vulkanCommandPool->inactiveCommandBuffers[ vulkanCommandPool->inactiveCommandBufferCount - ] = currentVulkanCommandBuffer; + ] = commandBuffer; vulkanCommandPool->inactiveCommandBufferCount += 1; } @@ -7615,7 +7739,7 @@ static Refresh_CommandBuffer* VULKAN_AcquireCommandBuffer( SDL_UnlockMutex(renderer->acquireCommandBufferLock); - /* State tracking */ + /* Reset state */ commandBuffer->currentComputePipeline = NULL; commandBuffer->currentGraphicsPipeline = NULL; @@ -7624,97 +7748,10 @@ static Refresh_CommandBuffer* VULKAN_AcquireCommandBuffer( commandBuffer->fragmentUniformBuffer = NULL; commandBuffer->computeUniformBuffer = NULL; - /* Bound buffer tracking */ - - commandBuffer->boundUniformBufferCapacity = 16; - commandBuffer->boundUniformBufferCount = 0; - commandBuffer->boundUniformBuffers = SDL_malloc( - commandBuffer->boundUniformBufferCapacity * sizeof(VulkanUniformBuffer*) - ); - - /* Descriptor set tracking */ - - commandBuffer->boundDescriptorSetDataCapacity = 16; - commandBuffer->boundDescriptorSetDataCount = 0; - commandBuffer->boundDescriptorSetDatas = SDL_malloc( - commandBuffer->boundDescriptorSetDataCapacity * sizeof(DescriptorSetData) - ); - - /* Deferred destroy storage */ - - commandBuffer->renderTargetsToDestroyCapacity = 16; - commandBuffer->renderTargetsToDestroyCount = 0; - - commandBuffer->renderTargetsToDestroy = (VulkanRenderTarget**) SDL_malloc( - sizeof(VulkanRenderTarget*) * - commandBuffer->renderTargetsToDestroyCapacity - ); - - commandBuffer->texturesToDestroyCapacity = 16; - commandBuffer->texturesToDestroyCount = 0; - - commandBuffer->texturesToDestroy = (VulkanTexture**)SDL_malloc( - sizeof(VulkanTexture*) * - commandBuffer->texturesToDestroyCapacity - ); - - commandBuffer->buffersToDestroyCapacity = 16; - commandBuffer->buffersToDestroyCount = 0; - - commandBuffer->buffersToDestroy = (VulkanBuffer**) SDL_malloc( - sizeof(VulkanBuffer*) * - commandBuffer->buffersToDestroyCapacity - ); - - commandBuffer->graphicsPipelinesToDestroyCapacity = 16; - commandBuffer->graphicsPipelinesToDestroyCount = 0; - - commandBuffer->graphicsPipelinesToDestroy = (VulkanGraphicsPipeline**) SDL_malloc( - sizeof(VulkanGraphicsPipeline*) * - commandBuffer->graphicsPipelinesToDestroyCapacity - ); - - commandBuffer->computePipelinesToDestroyCapacity = 16; - commandBuffer->computePipelinesToDestroyCount = 0; - - commandBuffer->computePipelinesToDestroy = (VulkanComputePipeline**) SDL_malloc( - sizeof(VulkanComputePipeline*) * - commandBuffer->computePipelinesToDestroyCapacity - ); - - commandBuffer->shaderModulesToDestroyCapacity = 16; - commandBuffer->shaderModulesToDestroyCount = 0; - - commandBuffer->shaderModulesToDestroy = (VkShaderModule*) SDL_malloc( - sizeof(VkShaderModule) * - commandBuffer->shaderModulesToDestroyCapacity - ); - - commandBuffer->samplersToDestroyCapacity = 16; - commandBuffer->samplersToDestroyCount = 0; - - commandBuffer->samplersToDestroy = (VkSampler*) SDL_malloc( - sizeof(VkSampler) * - commandBuffer->samplersToDestroyCapacity - ); - - commandBuffer->framebuffersToDestroyCapacity = 16; - commandBuffer->framebuffersToDestroyCount = 0; - - commandBuffer->framebuffersToDestroy = (VulkanFramebuffer**) SDL_malloc( - sizeof(VulkanFramebuffer*) * - commandBuffer->framebuffersToDestroyCapacity - ); - - commandBuffer->renderPassesToDestroyCapacity = 16; - commandBuffer->renderPassesToDestroyCount = 0; - - commandBuffer->renderPassesToDestroy = (VkRenderPass*) SDL_malloc( - sizeof(VkRenderPass) * - commandBuffer->renderPassesToDestroyCapacity - ); - - /* init bound compute buffer array */ + commandBuffer->fixed = fixed; + commandBuffer->submitted = 0; + commandBuffer->present = 0; + commandBuffer->renderPassInProgress = 0; for (i = 0; i < MAX_BUFFER_BINDINGS; i += 1) { @@ -7722,11 +7759,6 @@ static Refresh_CommandBuffer* VULKAN_AcquireCommandBuffer( } commandBuffer->boundComputeBufferCount = 0; - commandBuffer->fixed = fixed; - commandBuffer->submitted = 0; - commandBuffer->present = 0; - commandBuffer->renderPassInProgress = 0; - VULKAN_INTERNAL_BeginCommandBuffer(renderer, commandBuffer); return (Refresh_CommandBuffer*) commandBuffer;