diff --git a/src/Refresh_Driver_Vulkan.c b/src/Refresh_Driver_Vulkan.c index 9ab5e97..94d927f 100644 --- a/src/Refresh_Driver_Vulkan.c +++ b/src/Refresh_Driver_Vulkan.c @@ -1695,6 +1695,7 @@ typedef struct VulkanRenderer static void VULKAN_INTERNAL_BeginCommandBuffer(VulkanRenderer *renderer, VulkanCommandBuffer *commandBuffer); static void VULKAN_Wait(Refresh_Renderer *driverData); static void VULKAN_Submit(Refresh_Renderer *driverData, uint32_t commandBufferCount, Refresh_CommandBuffer **pCommandBuffers); +static void VULKAN_INTERNAL_DestroyRenderTarget(VulkanRenderer *renderer, VulkanRenderTarget *renderTarget); /* Error Handling */ @@ -2601,51 +2602,6 @@ static void VULKAN_INTERNAL_ImageMemoryBarrier( /* Resource Disposal */ -/* TODO: destroy associated framebuffers and render targets */ -static void VULKAN_INTERNAL_DestroyTexture( - VulkanRenderer* renderer, - VulkanTexture* texture -) { - if (texture->allocation->dedicated) - { - renderer->vkFreeMemory( - renderer->logicalDevice, - texture->allocation->memory, - NULL - ); - - SDL_DestroyMutex(texture->allocation->memoryLock); - SDL_free(texture->allocation->freeRegions); - SDL_free(texture->allocation); - } - else - { - SDL_LockMutex(renderer->allocatorLock); - - VULKAN_INTERNAL_NewMemoryFreeRegion( - texture->allocation, - texture->offset, - texture->memorySize - ); - - SDL_UnlockMutex(renderer->allocatorLock); - } - - renderer->vkDestroyImageView( - renderer->logicalDevice, - texture->view, - NULL - ); - - renderer->vkDestroyImage( - renderer->logicalDevice, - texture->image, - NULL - ); - - SDL_free(texture); -} - static void VULKAN_INTERNAL_RemoveFramebuffersContainingView( VulkanRenderer *renderer, VkImageView view @@ -2682,6 +2638,90 @@ static void VULKAN_INTERNAL_RemoveFramebuffersContainingView( SDL_UnlockMutex(renderer->framebufferFetchLock); } +static void VULKAN_INTERNAL_RemoveRenderTargetsContainingTexture( + VulkanRenderer *renderer, + VulkanTexture *texture +) { + RenderTargetHash *hash; + int32_t i; + + SDL_LockMutex(renderer->renderTargetFetchLock); + + for (i = renderer->renderTargetHashArray.count - 1; i >= 0; i -= 1) + { + hash = &renderer->renderTargetHashArray.elements[i].key; + + if ((VulkanTexture*) hash->texture == texture) + { + VULKAN_INTERNAL_RemoveFramebuffersContainingView( + renderer, + renderer->renderTargetHashArray.elements[i].value->view + ); + + VULKAN_INTERNAL_DestroyRenderTarget( + renderer, + renderer->renderTargetHashArray.elements[i].value + ); + + RenderTargetHash_Remove( + &renderer->renderTargetHashArray, + i + ); + } + } + + SDL_UnlockMutex(renderer->renderTargetFetchLock); +} + +static void VULKAN_INTERNAL_DestroyTexture( + VulkanRenderer* renderer, + VulkanTexture* texture +) { + if (texture->allocation->dedicated) + { + renderer->vkFreeMemory( + renderer->logicalDevice, + texture->allocation->memory, + NULL + ); + + SDL_DestroyMutex(texture->allocation->memoryLock); + SDL_free(texture->allocation->freeRegions); + SDL_free(texture->allocation); + } + else + { + SDL_LockMutex(renderer->allocatorLock); + + VULKAN_INTERNAL_NewMemoryFreeRegion( + texture->allocation, + texture->offset, + texture->memorySize + ); + + SDL_UnlockMutex(renderer->allocatorLock); + } + + VULKAN_INTERNAL_RemoveRenderTargetsContainingTexture( + renderer, + texture + ); + + renderer->vkDestroyImageView( + renderer->logicalDevice, + texture->view, + NULL + ); + + renderer->vkDestroyImage( + renderer->logicalDevice, + texture->image, + NULL + ); + + SDL_free(texture); +} + static void VULKAN_INTERNAL_DestroyRenderTarget( VulkanRenderer *renderer, VulkanRenderTarget *renderTarget @@ -2767,6 +2807,9 @@ static void VULKAN_INTERNAL_DestroyCommandPool( { commandBuffer = commandPool->inactiveCommandBuffers[i]; + SDL_free(commandBuffer->presentDatas); + SDL_free(commandBuffer->waitSemaphores); + SDL_free(commandBuffer->signalSemaphores); SDL_free(commandBuffer->transferBuffers); SDL_free(commandBuffer->boundUniformBuffers); SDL_free(commandBuffer->boundDescriptorSetDatas); @@ -2848,9 +2891,9 @@ static void VULKAN_INTERNAL_DestroySwapchain( for (i = 0; i < swapchainData->imageCount; i += 1) { - VULKAN_INTERNAL_RemoveFramebuffersContainingView( + VULKAN_INTERNAL_RemoveRenderTargetsContainingTexture( renderer, - swapchainData->textures[i].view + &swapchainData->textures[i] ); renderer->vkDestroyImageView( @@ -4071,7 +4114,10 @@ static CreateSwapchainResult VULKAN_INTERNAL_CreateSwapchain( swapchainCreateInfo.imageColorSpace = swapchainData->surfaceFormat.colorSpace; swapchainCreateInfo.imageExtent = swapchainData->extent; swapchainCreateInfo.imageArrayLayers = 1; - swapchainCreateInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; + swapchainCreateInfo.imageUsage = + VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_TRANSFER_SRC_BIT | + VK_IMAGE_USAGE_TRANSFER_DST_BIT; swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; swapchainCreateInfo.queueFamilyIndexCount = 0; swapchainCreateInfo.pQueueFamilyIndices = NULL; @@ -4494,6 +4540,17 @@ static void VULKAN_DestroyDevice( NULL ); + VULKAN_INTERNAL_DestroyUniformBufferPool(renderer, renderer->vertexUniformBufferPool); + VULKAN_INTERNAL_DestroyUniformBufferPool(renderer, renderer->fragmentUniformBufferPool); + VULKAN_INTERNAL_DestroyUniformBufferPool(renderer, renderer->computeUniformBufferPool); + + for (i = renderer->swapchainDataCount - 1; i >= 0; i -= 1) + { + VULKAN_INTERNAL_DestroySwapchain(renderer, renderer->swapchainDatas[i]->windowHandle); + } + + SDL_free(renderer->swapchainDatas); + for (i = 0; i < renderer->framebufferHashArray.count; i += 1) { renderer->vkDestroyFramebuffer( @@ -4516,16 +4573,7 @@ static void VULKAN_DestroyDevice( SDL_free(renderer->renderPassHashArray.elements); - VULKAN_INTERNAL_DestroyUniformBufferPool(renderer, renderer->vertexUniformBufferPool); - VULKAN_INTERNAL_DestroyUniformBufferPool(renderer, renderer->fragmentUniformBufferPool); - VULKAN_INTERNAL_DestroyUniformBufferPool(renderer, renderer->computeUniformBufferPool); - - for (i = renderer->swapchainDataCount - 1; i >= 0; i -= 1) - { - VULKAN_INTERNAL_DestroySwapchain(renderer, renderer->swapchainDatas[i]->windowHandle); - } - - SDL_free(renderer->swapchainDatas); + SDL_free(renderer->renderTargetHashArray.elements); for (i = 0; i < VK_MAX_MEMORY_TYPES; i += 1) {