From 83ce2a36198d10237655e3c316dcc880c85ab58b Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Sun, 6 Mar 2022 18:42:13 -0800 Subject: [PATCH 1/3] swapchain recreation and framebuffer destroy fix --- src/Refresh_Driver_Vulkan.c | 175 ++++++++++++++++++++++++++++-------- 1 file changed, 140 insertions(+), 35 deletions(-) diff --git a/src/Refresh_Driver_Vulkan.c b/src/Refresh_Driver_Vulkan.c index f7a6022..0e2d247 100644 --- a/src/Refresh_Driver_Vulkan.c +++ b/src/Refresh_Driver_Vulkan.c @@ -751,6 +751,12 @@ typedef struct VulkanRenderTarget VkSampleCountFlags multisampleCount; } VulkanRenderTarget; +typedef struct VulkanFramebuffer +{ + VkFramebuffer framebuffer; + SDL_atomic_t referenceCount; +} VulkanFramebuffer; + typedef struct VulkanSwapchainData { /* Window surface */ @@ -769,9 +775,6 @@ typedef struct VulkanSwapchainData VulkanTexture *textures; uint32_t imageCount; - /* Recreate flag */ - uint8_t needsRecreate; - /* Synchronization primitives */ VkSemaphore imageAvailableSemaphore; VkSemaphore renderFinishedSemaphore; @@ -1051,7 +1054,7 @@ typedef struct FramebufferHash typedef struct FramebufferHashMap { FramebufferHash key; - VkFramebuffer value; + VulkanFramebuffer *value; } FramebufferHashMap; typedef struct FramebufferHashArray @@ -1103,7 +1106,7 @@ static inline uint8_t FramebufferHash_Compare( return 1; } -static inline VkFramebuffer FramebufferHashArray_Fetch( +static inline VulkanFramebuffer* FramebufferHashArray_Fetch( FramebufferHashArray *arr, FramebufferHash *key ) { @@ -1124,7 +1127,7 @@ static inline VkFramebuffer FramebufferHashArray_Fetch( static inline void FramebufferHashArray_Insert( FramebufferHashArray *arr, FramebufferHash key, - VkFramebuffer value + VulkanFramebuffer *value ) { FramebufferHashMap map; map.key = key; @@ -1520,6 +1523,10 @@ typedef struct VulkanCommandBuffer uint32_t usedComputePipelineCount; uint32_t usedComputePipelineCapacity; + VulkanFramebuffer **usedFramebuffers; + uint32_t usedFramebufferCount; + uint32_t usedFramebufferCapacity; + /* Shader modules have references tracked by pipelines */ VkFence inFlightFence; @@ -1697,6 +1704,10 @@ typedef struct VulkanRenderer uint32_t shaderModulesToDestroyCount; uint32_t shaderModulesToDestroyCapacity; + VulkanFramebuffer **framebuffersToDestroy; + uint32_t framebuffersToDestroyCount; + uint32_t framebuffersToDestroyCapacity; + SDL_mutex *allocatorLock; SDL_mutex *disposeLock; SDL_mutex *submitLock; @@ -2719,10 +2730,57 @@ static void VULKAN_INTERNAL_TrackComputePipeline( ) } +static void VULKAN_INTERNAL_TrackFramebuffer( + VulkanRenderer *renderer, + VulkanCommandBuffer *commandBuffer, + VulkanFramebuffer *framebuffer +) { + TRACK_RESOURCE( + framebuffer, + VulkanFramebuffer*, + usedFramebuffers, + usedFramebufferCount, + usedFramebufferCapacity + ); +} + #undef TRACK_RESOURCE /* Resource Disposal */ +static void VULKAN_INTERNAL_QueueDestroyFramebuffer( + VulkanRenderer *renderer, + VulkanFramebuffer *framebuffer +) { + SDL_LockMutex(renderer->disposeLock); + + EXPAND_ARRAY_IF_NEEDED( + renderer->framebuffersToDestroy, + VulkanFramebuffer*, + renderer->framebuffersToDestroyCount + 1, + renderer->framebuffersToDestroyCapacity, + renderer->framebuffersToDestroyCapacity * 2 + ) + + renderer->framebuffersToDestroy[renderer->framebuffersToDestroyCount] = framebuffer; + renderer->framebuffersToDestroyCount += 1; + + SDL_UnlockMutex(renderer->disposeLock); +} + +static void VULKAN_INTERNAL_DestroyFramebuffer( + VulkanRenderer *renderer, + VulkanFramebuffer *framebuffer +) { + renderer->vkDestroyFramebuffer( + renderer->logicalDevice, + framebuffer->framebuffer, + NULL + ); + + SDL_free(framebuffer); +} + static void VULKAN_INTERNAL_RemoveFramebuffersContainingView( VulkanRenderer *renderer, VkImageView view @@ -2740,10 +2798,9 @@ static void VULKAN_INTERNAL_RemoveFramebuffersContainingView( { if (hash->colorAttachmentViews[i] == view) { - renderer->vkDestroyFramebuffer( - renderer->logicalDevice, - renderer->framebufferHashArray.elements[i].value, - NULL + VULKAN_INTERNAL_QueueDestroyFramebuffer( + renderer, + renderer->framebufferHashArray.elements[i].value ); FramebufferHashArray_Remove( @@ -2945,6 +3002,7 @@ static void VULKAN_INTERNAL_DestroyCommandPool( SDL_free(commandBuffer->usedSamplers); SDL_free(commandBuffer->usedGraphicsPipelines); SDL_free(commandBuffer->usedComputePipelines); + SDL_free(commandBuffer->usedFramebuffers); SDL_free(commandBuffer); } @@ -4408,8 +4466,6 @@ static CreateSwapchainResult VULKAN_INTERNAL_CreateSwapchain( &swapchainData->renderFinishedSemaphore ); - swapchainData->needsRecreate = 0; - SDL_SetWindowData(windowHandle, WINDOW_SWAPCHAIN_DATA, swapchainData); if (renderer->swapchainDataCount >= renderer->swapchainDataCapacity) @@ -4684,10 +4740,9 @@ static void VULKAN_DestroyDevice( for (i = 0; i < renderer->framebufferHashArray.count; i += 1) { - renderer->vkDestroyFramebuffer( - renderer->logicalDevice, - renderer->framebufferHashArray.elements[i].value, - NULL + VULKAN_INTERNAL_DestroyFramebuffer( + renderer, + renderer->framebufferHashArray.elements[i].value ); } @@ -7779,7 +7834,7 @@ static VkRenderPass VULKAN_INTERNAL_FetchRenderPass( return renderPass; } -static VkFramebuffer VULKAN_INTERNAL_FetchFramebuffer( +static VulkanFramebuffer* VULKAN_INTERNAL_FetchFramebuffer( VulkanRenderer *renderer, VkRenderPass renderPass, Refresh_ColorAttachmentInfo *colorAttachmentInfos, @@ -7788,7 +7843,7 @@ static VkFramebuffer VULKAN_INTERNAL_FetchFramebuffer( uint32_t width, uint32_t height ) { - VkFramebuffer framebuffer; + VulkanFramebuffer *vulkanFramebuffer; VkFramebufferCreateInfo framebufferInfo; VkResult result; VkImageView imageViewAttachments[2 * MAX_COLOR_TARGET_BINDINGS + 1]; @@ -7850,17 +7905,21 @@ static VkFramebuffer VULKAN_INTERNAL_FetchFramebuffer( hash.width = width; hash.height = height; - framebuffer = FramebufferHashArray_Fetch( + vulkanFramebuffer = FramebufferHashArray_Fetch( &renderer->framebufferHashArray, &hash ); - if (framebuffer != VK_NULL_HANDLE) + if (vulkanFramebuffer != NULL) { SDL_UnlockMutex(renderer->framebufferFetchLock); - return framebuffer; + return vulkanFramebuffer; } + vulkanFramebuffer = SDL_malloc(sizeof(VulkanFramebuffer)); + + SDL_AtomicSet(&vulkanFramebuffer->referenceCount, 0); + /* Create a new framebuffer */ for (i = 0; i < colorAttachmentCount; i += 1) @@ -7918,7 +7977,7 @@ static VkFramebuffer VULKAN_INTERNAL_FetchFramebuffer( renderer->logicalDevice, &framebufferInfo, NULL, - &framebuffer + &vulkanFramebuffer->framebuffer ); if (result == VK_SUCCESS) @@ -7926,17 +7985,18 @@ static VkFramebuffer VULKAN_INTERNAL_FetchFramebuffer( FramebufferHashArray_Insert( &renderer->framebufferHashArray, hash, - framebuffer + vulkanFramebuffer ); } else { LogVulkanResultAsError("vkCreateFramebuffer", result); - framebuffer = VK_NULL_HANDLE; + SDL_free(vulkanFramebuffer); + vulkanFramebuffer = NULL; } SDL_UnlockMutex(renderer->framebufferFetchLock); - return framebuffer; + return vulkanFramebuffer; } static void VULKAN_INTERNAL_SetCurrentViewport( @@ -8028,7 +8088,7 @@ static void VULKAN_BeginRenderPass( VulkanRenderer* renderer = (VulkanRenderer*) driverData; VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer; VkRenderPass renderPass; - VkFramebuffer framebuffer; + VulkanFramebuffer *framebuffer; VulkanTexture *texture; VkClearValue *clearValues; @@ -8095,6 +8155,8 @@ static void VULKAN_BeginRenderPass( framebufferHeight ); + VULKAN_INTERNAL_TrackFramebuffer(renderer, vulkanCommandBuffer, framebuffer); + /* Layout transitions */ for (i = 0; i < colorAttachmentCount; i += 1) @@ -8169,8 +8231,8 @@ static void VULKAN_BeginRenderPass( VkRenderPassBeginInfo renderPassBeginInfo; renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; renderPassBeginInfo.pNext = NULL; - renderPassBeginInfo.renderPass = (VkRenderPass) renderPass; - renderPassBeginInfo.framebuffer = framebuffer; + renderPassBeginInfo.renderPass = renderPass; + renderPassBeginInfo.framebuffer = framebuffer->framebuffer; renderPassBeginInfo.renderArea.extent.width = renderArea->w; renderPassBeginInfo.renderArea.extent.height = renderArea->h; renderPassBeginInfo.renderArea.offset.x = renderArea->x; @@ -8715,6 +8777,12 @@ static void VULKAN_INTERNAL_AllocateCommandBuffers( commandBuffer->usedComputePipelineCapacity * sizeof(VulkanComputePipeline*) ); + commandBuffer->usedFramebufferCapacity = 4; + commandBuffer->usedFramebufferCount = 0; + commandBuffer->usedFramebuffers = SDL_malloc( + commandBuffer->usedFramebufferCapacity * sizeof(VulkanFramebuffer*) + ); + vulkanCommandPool->inactiveCommandBuffers[ vulkanCommandPool->inactiveCommandBufferCount ] = commandBuffer; @@ -8913,7 +8981,22 @@ static Refresh_Texture* VULKAN_AcquireSwapchainTexture( &swapchainImageIndex ); - if (acquireResult == VK_SUCCESS || acquireResult == VK_SUBOPTIMAL_KHR) + /* Swapchain is suboptimal, let's try to recreate */ + if (acquireResult == VK_SUBOPTIMAL_KHR) + { + VULKAN_INTERNAL_RecreateSwapchain(renderer, windowHandle); + + acquireResult = renderer->vkAcquireNextImageKHR( + renderer->logicalDevice, + swapchainData->swapchain, + UINT64_MAX, + swapchainData->imageAvailableSemaphore, + VK_NULL_HANDLE, + &swapchainImageIndex + ); + } + + if (acquireResult == VK_SUCCESS) { swapchainTexture = &swapchainData->textures[swapchainImageIndex]; @@ -8973,11 +9056,6 @@ static Refresh_Texture* VULKAN_AcquireSwapchainTexture( vulkanCommandBuffer->signalSemaphores[vulkanCommandBuffer->signalSemaphoreCount] = swapchainData->renderFinishedSemaphore; vulkanCommandBuffer->signalSemaphoreCount += 1; - - if (acquireResult == VK_SUBOPTIMAL_KHR) - { - swapchainData->needsRecreate = 1; - } } } @@ -9104,6 +9182,20 @@ static void VULKAN_INTERNAL_PerformPendingDestroys( } } + for (i = renderer->framebuffersToDestroyCount - 1; i >= 0; i -= 1) + { + if (SDL_AtomicGet(&renderer->framebuffersToDestroy[i]->referenceCount) == 0) + { + VULKAN_INTERNAL_DestroyFramebuffer( + renderer, + renderer->framebuffersToDestroy[i] + ); + + renderer->framebuffersToDestroy[i] = renderer->framebuffersToDestroy[renderer->framebuffersToDestroyCount - 1]; + renderer->framebuffersToDestroyCount -= 1; + } + } + SDL_UnlockMutex(renderer->disposeLock); } @@ -9217,6 +9309,12 @@ static void VULKAN_INTERNAL_CleanCommandBuffer( } commandBuffer->usedComputePipelineCount = 0; + for (i = 0; i < commandBuffer->usedFramebufferCount; i += 1) + { + SDL_AtomicDecRef(&commandBuffer->usedFramebuffers[i]->referenceCount); + } + commandBuffer->usedFramebufferCount = 0; + /* Return command buffer to pool */ SDL_LockMutex(renderer->acquireCommandBufferLock); @@ -9378,7 +9476,7 @@ static void VULKAN_Submit( &presentInfo ); - if (presentResult != VK_SUCCESS || presentData->swapchainData->needsRecreate) + if (presentResult != VK_SUCCESS) { VULKAN_INTERNAL_RecreateSwapchain(renderer, presentData->swapchainData->windowHandle); } @@ -10624,6 +10722,13 @@ static Refresh_Device* VULKAN_CreateDevice( renderer->shaderModulesToDestroyCapacity ); + renderer->framebuffersToDestroyCapacity = 16; + renderer->framebuffersToDestroyCount = 0; + renderer->framebuffersToDestroy = SDL_malloc( + sizeof(VulkanFramebuffer*) * + renderer->framebuffersToDestroyCapacity + ); + return result; } -- 2.25.1 From 74c5ac984c1b2f246f6bdb8ff2783d626c021933 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Sun, 6 Mar 2022 22:20:06 -0800 Subject: [PATCH 2/3] swapchain recreate timing fix --- include/Refresh.h | 1 + src/Refresh_Driver_Vulkan.c | 261 ++++++++++++++++++------------------ 2 files changed, 134 insertions(+), 128 deletions(-) diff --git a/include/Refresh.h b/include/Refresh.h index e5640b4..ca70429 100644 --- a/include/Refresh.h +++ b/include/Refresh.h @@ -988,6 +988,7 @@ REFRESHAPI void Refresh_QueueDestroyGraphicsPipeline( * The area affected by the render pass. * All load, store and resolve operations are restricted * to the given rectangle. + * If NULL, a sensible default will be chosen. * colorAttachmentInfos: * A pointer to an array of Refresh_ColorAttachmentInfo structures * that contains render targets and clear values. May be NULL. diff --git a/src/Refresh_Driver_Vulkan.c b/src/Refresh_Driver_Vulkan.c index 0e2d247..14242fe 100644 --- a/src/Refresh_Driver_Vulkan.c +++ b/src/Refresh_Driver_Vulkan.c @@ -169,13 +169,6 @@ typedef enum VulkanResourceAccessType RESOURCE_ACCESS_TYPES_COUNT } VulkanResourceAccessType; -typedef enum CreateSwapchainResult -{ - CREATE_SWAPCHAIN_FAIL, - CREATE_SWAPCHAIN_SUCCESS, - CREATE_SWAPCHAIN_SURFACE_ZERO, -} CreateSwapchainResult; - /* Conversions */ static const uint8_t DEVICE_PRIORITY[] = @@ -4095,7 +4088,7 @@ static uint8_t VULKAN_INTERNAL_ChooseSwapPresentMode( return 1; } -static CreateSwapchainResult VULKAN_INTERNAL_CreateSwapchain( +static uint8_t VULKAN_INTERNAL_CreateSwapchain( VulkanRenderer *renderer, void *windowHandle ) { @@ -4124,7 +4117,7 @@ static CreateSwapchainResult VULKAN_INTERNAL_CreateSwapchain( "SDL_Vulkan_CreateSurface failed: %s", SDL_GetError() ); - return CREATE_SWAPCHAIN_FAIL; + return 0; } if (!VULKAN_INTERNAL_QuerySwapChainSupport( @@ -4149,7 +4142,7 @@ static CreateSwapchainResult VULKAN_INTERNAL_CreateSwapchain( } SDL_free(swapchainData); Refresh_LogError("Device does not support swap chain creation"); - return CREATE_SWAPCHAIN_FAIL; + return 0; } swapchainData->swapchainFormat = VK_FORMAT_R8G8B8A8_UNORM; @@ -4191,7 +4184,7 @@ static CreateSwapchainResult VULKAN_INTERNAL_CreateSwapchain( } SDL_free(swapchainData); Refresh_LogError("Device does not support swap chain format"); - return CREATE_SWAPCHAIN_FAIL; + return 0; } } @@ -4216,7 +4209,7 @@ static CreateSwapchainResult VULKAN_INTERNAL_CreateSwapchain( } SDL_free(swapchainData); Refresh_LogError("Device does not support swap chain present mode"); - return CREATE_SWAPCHAIN_FAIL; + return 0; } SDL_Vulkan_GetDrawableSize( @@ -4247,7 +4240,8 @@ static CreateSwapchainResult VULKAN_INTERNAL_CreateSwapchain( SDL_free(swapchainSupportDetails.presentModes); } SDL_free(swapchainData); - return CREATE_SWAPCHAIN_SURFACE_ZERO; + /* Not an error, just Windows minimize behavior! */ + return 0; } if (swapchainSupportDetails.capabilities.currentExtent.width != UINT32_MAX) @@ -4280,7 +4274,7 @@ static CreateSwapchainResult VULKAN_INTERNAL_CreateSwapchain( } SDL_free(swapchainData); Refresh_LogError("No fallback swapchain size available!"); - return CREATE_SWAPCHAIN_FAIL; + return 0; } } @@ -4353,7 +4347,7 @@ static CreateSwapchainResult VULKAN_INTERNAL_CreateSwapchain( ); SDL_free(swapchainData); LogVulkanResultAsError("vkCreateSwapchainKHR", vulkanResult); - return CREATE_SWAPCHAIN_FAIL; + return 0; } renderer->vkGetSwapchainImagesKHR( @@ -4376,7 +4370,7 @@ static CreateSwapchainResult VULKAN_INTERNAL_CreateSwapchain( NULL ); SDL_free(swapchainData); - return CREATE_SWAPCHAIN_FAIL; + return 0; } swapchainImages = SDL_stack_alloc(VkImage, swapchainData->imageCount); @@ -4424,7 +4418,7 @@ static CreateSwapchainResult VULKAN_INTERNAL_CreateSwapchain( SDL_free(swapchainData->textures); SDL_free(swapchainData); LogVulkanResultAsError("vkCreateImageView", vulkanResult); - return CREATE_SWAPCHAIN_FAIL; + return 0; } swapchainData->textures[i].resourceAccessType = RESOURCE_ACCESS_NONE; @@ -4480,26 +4474,16 @@ static CreateSwapchainResult VULKAN_INTERNAL_CreateSwapchain( renderer->swapchainDatas[renderer->swapchainDataCount] = swapchainData; renderer->swapchainDataCount += 1; - return CREATE_SWAPCHAIN_SUCCESS; + return 1; } static void VULKAN_INTERNAL_RecreateSwapchain( VulkanRenderer* renderer, void *windowHandle ) { - CreateSwapchainResult createSwapchainResult; - - VULKAN_Wait((Refresh_Renderer*)renderer); - + VULKAN_Wait((Refresh_Renderer*) renderer); VULKAN_INTERNAL_DestroySwapchain(renderer, windowHandle); - createSwapchainResult = VULKAN_INTERNAL_CreateSwapchain(renderer, windowHandle); - - if (createSwapchainResult == CREATE_SWAPCHAIN_FAIL) - { - return; - } - - VULKAN_Wait((Refresh_Renderer*)renderer); + VULKAN_INTERNAL_CreateSwapchain(renderer, windowHandle); } /* Command Buffers */ @@ -8233,13 +8217,24 @@ static void VULKAN_BeginRenderPass( renderPassBeginInfo.pNext = NULL; renderPassBeginInfo.renderPass = renderPass; renderPassBeginInfo.framebuffer = framebuffer->framebuffer; - renderPassBeginInfo.renderArea.extent.width = renderArea->w; - renderPassBeginInfo.renderArea.extent.height = renderArea->h; - renderPassBeginInfo.renderArea.offset.x = renderArea->x; - renderPassBeginInfo.renderArea.offset.y = renderArea->y; renderPassBeginInfo.pClearValues = clearValues; renderPassBeginInfo.clearValueCount = clearCount; + if (renderArea != NULL) + { + renderPassBeginInfo.renderArea.extent.width = renderArea->w; + renderPassBeginInfo.renderArea.extent.height = renderArea->h; + renderPassBeginInfo.renderArea.offset.x = renderArea->x; + renderPassBeginInfo.renderArea.offset.y = renderArea->y; + } + else + { + renderPassBeginInfo.renderArea.extent.width = framebufferWidth; + renderPassBeginInfo.renderArea.extent.height = framebufferHeight; + renderPassBeginInfo.renderArea.offset.x = 0; + renderPassBeginInfo.renderArea.offset.y = 0; + } + renderer->vkCmdBeginRenderPass( vulkanCommandBuffer->commandBuffer, &renderPassBeginInfo, @@ -8935,6 +8930,25 @@ static Refresh_CommandBuffer* VULKAN_AcquireCommandBuffer( return (Refresh_CommandBuffer*) commandBuffer; } +static VulkanSwapchainData* VULKAN_INTERNAL_FetchSwapchainData( + VulkanRenderer *renderer, + void *windowHandle +) { + VulkanSwapchainData *swapchainData = NULL; + + swapchainData = (VulkanSwapchainData*) SDL_GetWindowData(windowHandle, WINDOW_SWAPCHAIN_DATA); + + if (swapchainData == NULL) + { + if (VULKAN_INTERNAL_CreateSwapchain(renderer, windowHandle)) + { + swapchainData = (VulkanSwapchainData*) SDL_GetWindowData(windowHandle, WINDOW_SWAPCHAIN_DATA); + } + } + + return swapchainData; +} + static Refresh_Texture* VULKAN_AcquireSwapchainTexture( Refresh_Renderer *driverData, Refresh_CommandBuffer *commandBuffer, @@ -8943,35 +8957,39 @@ static Refresh_Texture* VULKAN_AcquireSwapchainTexture( VulkanRenderer *renderer = (VulkanRenderer*) driverData; VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer; uint32_t swapchainImageIndex; - VulkanSwapchainData *swapchainData = NULL; - CreateSwapchainResult createSwapchainResult = 0; - uint8_t validSwapchainExists = 1; + VulkanSwapchainData *swapchainData; VkResult acquireResult = VK_SUCCESS; VulkanTexture *swapchainTexture = NULL; VulkanPresentData *presentData; - swapchainData = (VulkanSwapchainData*) SDL_GetWindowData(windowHandle, WINDOW_SWAPCHAIN_DATA); + swapchainData = VULKAN_INTERNAL_FetchSwapchainData(renderer, windowHandle); if (swapchainData == NULL) { - createSwapchainResult = VULKAN_INTERNAL_CreateSwapchain(renderer, windowHandle); - - if (createSwapchainResult == CREATE_SWAPCHAIN_FAIL) - { - validSwapchainExists = 0; - } - else if (createSwapchainResult == CREATE_SWAPCHAIN_SURFACE_ZERO) - { - validSwapchainExists = 0; - } - else - { - swapchainData = (VulkanSwapchainData*) SDL_GetWindowData(windowHandle, WINDOW_SWAPCHAIN_DATA); - } + return NULL; } - if (validSwapchainExists) + acquireResult = renderer->vkAcquireNextImageKHR( + renderer->logicalDevice, + swapchainData->swapchain, + UINT64_MAX, + swapchainData->imageAvailableSemaphore, + VK_NULL_HANDLE, + &swapchainImageIndex + ); + + /* Swapchain is suboptimal, let's try to recreate */ + if (acquireResult == VK_SUBOPTIMAL_KHR) { + VULKAN_INTERNAL_RecreateSwapchain(renderer, windowHandle); + + swapchainData = VULKAN_INTERNAL_FetchSwapchainData(renderer, windowHandle); + + if (swapchainData == NULL) + { + return NULL; + } + acquireResult = renderer->vkAcquireNextImageKHR( renderer->logicalDevice, swapchainData->swapchain, @@ -8981,84 +8999,71 @@ static Refresh_Texture* VULKAN_AcquireSwapchainTexture( &swapchainImageIndex ); - /* Swapchain is suboptimal, let's try to recreate */ - if (acquireResult == VK_SUBOPTIMAL_KHR) + if (acquireResult != VK_SUCCESS) { - VULKAN_INTERNAL_RecreateSwapchain(renderer, windowHandle); - - acquireResult = renderer->vkAcquireNextImageKHR( - renderer->logicalDevice, - swapchainData->swapchain, - UINT64_MAX, - swapchainData->imageAvailableSemaphore, - VK_NULL_HANDLE, - &swapchainImageIndex - ); - } - - if (acquireResult == VK_SUCCESS) - { - swapchainTexture = &swapchainData->textures[swapchainImageIndex]; - - VULKAN_INTERNAL_ImageMemoryBarrier( - renderer, - vulkanCommandBuffer->commandBuffer, - RESOURCE_ACCESS_COLOR_ATTACHMENT_WRITE, - VK_IMAGE_ASPECT_COLOR_BIT, - 0, - 1, - 0, - 1, - 0, - swapchainTexture->image, - &swapchainTexture->resourceAccessType - ); - - /* Set up present struct */ - - if (vulkanCommandBuffer->presentDataCount == vulkanCommandBuffer->presentDataCapacity) - { - vulkanCommandBuffer->presentDataCapacity += 1; - vulkanCommandBuffer->presentDatas = SDL_realloc( - vulkanCommandBuffer->presentDatas, - vulkanCommandBuffer->presentDataCapacity * sizeof(VkPresentInfoKHR) - ); - } - - presentData = &vulkanCommandBuffer->presentDatas[vulkanCommandBuffer->presentDataCount]; - vulkanCommandBuffer->presentDataCount += 1; - - presentData->swapchainData = swapchainData; - presentData->swapchainImageIndex = swapchainImageIndex; - - /* Set up present semaphores */ - - if (vulkanCommandBuffer->waitSemaphoreCount == vulkanCommandBuffer->waitSemaphoreCapacity) - { - vulkanCommandBuffer->waitSemaphoreCapacity += 1; - vulkanCommandBuffer->waitSemaphores = SDL_realloc( - vulkanCommandBuffer->waitSemaphores, - vulkanCommandBuffer->waitSemaphoreCapacity * sizeof(VkSemaphore) - ); - } - - vulkanCommandBuffer->waitSemaphores[vulkanCommandBuffer->waitSemaphoreCount] = swapchainData->imageAvailableSemaphore; - vulkanCommandBuffer->waitSemaphoreCount += 1; - - if (vulkanCommandBuffer->signalSemaphoreCount == vulkanCommandBuffer->signalSemaphoreCapacity) - { - vulkanCommandBuffer->signalSemaphoreCapacity += 1; - vulkanCommandBuffer->signalSemaphores = SDL_realloc( - vulkanCommandBuffer->signalSemaphores, - vulkanCommandBuffer->signalSemaphoreCapacity * sizeof(VkSemaphore) - ); - } - - vulkanCommandBuffer->signalSemaphores[vulkanCommandBuffer->signalSemaphoreCount] = swapchainData->renderFinishedSemaphore; - vulkanCommandBuffer->signalSemaphoreCount += 1; + return NULL; } } + swapchainTexture = &swapchainData->textures[swapchainImageIndex]; + + VULKAN_INTERNAL_ImageMemoryBarrier( + renderer, + vulkanCommandBuffer->commandBuffer, + RESOURCE_ACCESS_COLOR_ATTACHMENT_WRITE, + VK_IMAGE_ASPECT_COLOR_BIT, + 0, + 1, + 0, + 1, + 0, + swapchainTexture->image, + &swapchainTexture->resourceAccessType + ); + + /* Set up present struct */ + + if (vulkanCommandBuffer->presentDataCount == vulkanCommandBuffer->presentDataCapacity) + { + vulkanCommandBuffer->presentDataCapacity += 1; + vulkanCommandBuffer->presentDatas = SDL_realloc( + vulkanCommandBuffer->presentDatas, + vulkanCommandBuffer->presentDataCapacity * sizeof(VkPresentInfoKHR) + ); + } + + presentData = &vulkanCommandBuffer->presentDatas[vulkanCommandBuffer->presentDataCount]; + vulkanCommandBuffer->presentDataCount += 1; + + presentData->swapchainData = swapchainData; + presentData->swapchainImageIndex = swapchainImageIndex; + + /* Set up present semaphores */ + + if (vulkanCommandBuffer->waitSemaphoreCount == vulkanCommandBuffer->waitSemaphoreCapacity) + { + vulkanCommandBuffer->waitSemaphoreCapacity += 1; + vulkanCommandBuffer->waitSemaphores = SDL_realloc( + vulkanCommandBuffer->waitSemaphores, + vulkanCommandBuffer->waitSemaphoreCapacity * sizeof(VkSemaphore) + ); + } + + vulkanCommandBuffer->waitSemaphores[vulkanCommandBuffer->waitSemaphoreCount] = swapchainData->imageAvailableSemaphore; + vulkanCommandBuffer->waitSemaphoreCount += 1; + + if (vulkanCommandBuffer->signalSemaphoreCount == vulkanCommandBuffer->signalSemaphoreCapacity) + { + vulkanCommandBuffer->signalSemaphoreCapacity += 1; + vulkanCommandBuffer->signalSemaphores = SDL_realloc( + vulkanCommandBuffer->signalSemaphores, + vulkanCommandBuffer->signalSemaphoreCapacity * sizeof(VkSemaphore) + ); + } + + vulkanCommandBuffer->signalSemaphores[vulkanCommandBuffer->signalSemaphoreCount] = swapchainData->renderFinishedSemaphore; + vulkanCommandBuffer->signalSemaphoreCount += 1; + return (Refresh_Texture*) swapchainTexture; } @@ -10337,7 +10342,7 @@ static Refresh_Device* VULKAN_CreateDevice( renderer->swapchainDataCapacity * sizeof(VulkanSwapchainData*) ); - if (VULKAN_INTERNAL_CreateSwapchain(renderer, presentationParameters->deviceWindowHandle) != CREATE_SWAPCHAIN_SUCCESS) + if (!VULKAN_INTERNAL_CreateSwapchain(renderer, presentationParameters->deviceWindowHandle)) { Refresh_LogError("Failed to create swapchain"); return NULL; -- 2.25.1 From 2b092ca38757c1462fd6f8f06aa3b194e9bd7b75 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Sun, 6 Mar 2022 22:27:00 -0800 Subject: [PATCH 3/3] fix swapchain framebuffer leak on exit --- src/Refresh_Driver_Vulkan.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Refresh_Driver_Vulkan.c b/src/Refresh_Driver_Vulkan.c index 14242fe..3a71ea6 100644 --- a/src/Refresh_Driver_Vulkan.c +++ b/src/Refresh_Driver_Vulkan.c @@ -4556,6 +4556,15 @@ static void VULKAN_DestroyDevice( VULKAN_Wait(device->driverData); + for (i = renderer->swapchainDataCount - 1; i >= 0; i -= 1) + { + VULKAN_INTERNAL_DestroySwapchain(renderer, renderer->swapchainDatas[i]->windowHandle); + } + + SDL_free(renderer->swapchainDatas); + + VULKAN_Wait(device->driverData); + SDL_free(renderer->submittedCommandBuffers); VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->dummyVertexUniformBuffer->vulkanBuffer); @@ -4715,13 +4724,6 @@ static void VULKAN_DestroyDevice( 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) { VULKAN_INTERNAL_DestroyFramebuffer( -- 2.25.1