swapchain recreation and framebuffer destroy fix
continuous-integration/drone/push Build is passing Details

pull/18/head
cosmonaut 2022-03-06 18:42:13 -08:00
parent 22236607f7
commit 83ce2a3619
1 changed files with 140 additions and 35 deletions

View File

@ -751,6 +751,12 @@ typedef struct VulkanRenderTarget
VkSampleCountFlags multisampleCount; VkSampleCountFlags multisampleCount;
} VulkanRenderTarget; } VulkanRenderTarget;
typedef struct VulkanFramebuffer
{
VkFramebuffer framebuffer;
SDL_atomic_t referenceCount;
} VulkanFramebuffer;
typedef struct VulkanSwapchainData typedef struct VulkanSwapchainData
{ {
/* Window surface */ /* Window surface */
@ -769,9 +775,6 @@ typedef struct VulkanSwapchainData
VulkanTexture *textures; VulkanTexture *textures;
uint32_t imageCount; uint32_t imageCount;
/* Recreate flag */
uint8_t needsRecreate;
/* Synchronization primitives */ /* Synchronization primitives */
VkSemaphore imageAvailableSemaphore; VkSemaphore imageAvailableSemaphore;
VkSemaphore renderFinishedSemaphore; VkSemaphore renderFinishedSemaphore;
@ -1051,7 +1054,7 @@ typedef struct FramebufferHash
typedef struct FramebufferHashMap typedef struct FramebufferHashMap
{ {
FramebufferHash key; FramebufferHash key;
VkFramebuffer value; VulkanFramebuffer *value;
} FramebufferHashMap; } FramebufferHashMap;
typedef struct FramebufferHashArray typedef struct FramebufferHashArray
@ -1103,7 +1106,7 @@ static inline uint8_t FramebufferHash_Compare(
return 1; return 1;
} }
static inline VkFramebuffer FramebufferHashArray_Fetch( static inline VulkanFramebuffer* FramebufferHashArray_Fetch(
FramebufferHashArray *arr, FramebufferHashArray *arr,
FramebufferHash *key FramebufferHash *key
) { ) {
@ -1124,7 +1127,7 @@ static inline VkFramebuffer FramebufferHashArray_Fetch(
static inline void FramebufferHashArray_Insert( static inline void FramebufferHashArray_Insert(
FramebufferHashArray *arr, FramebufferHashArray *arr,
FramebufferHash key, FramebufferHash key,
VkFramebuffer value VulkanFramebuffer *value
) { ) {
FramebufferHashMap map; FramebufferHashMap map;
map.key = key; map.key = key;
@ -1520,6 +1523,10 @@ typedef struct VulkanCommandBuffer
uint32_t usedComputePipelineCount; uint32_t usedComputePipelineCount;
uint32_t usedComputePipelineCapacity; uint32_t usedComputePipelineCapacity;
VulkanFramebuffer **usedFramebuffers;
uint32_t usedFramebufferCount;
uint32_t usedFramebufferCapacity;
/* Shader modules have references tracked by pipelines */ /* Shader modules have references tracked by pipelines */
VkFence inFlightFence; VkFence inFlightFence;
@ -1697,6 +1704,10 @@ typedef struct VulkanRenderer
uint32_t shaderModulesToDestroyCount; uint32_t shaderModulesToDestroyCount;
uint32_t shaderModulesToDestroyCapacity; uint32_t shaderModulesToDestroyCapacity;
VulkanFramebuffer **framebuffersToDestroy;
uint32_t framebuffersToDestroyCount;
uint32_t framebuffersToDestroyCapacity;
SDL_mutex *allocatorLock; SDL_mutex *allocatorLock;
SDL_mutex *disposeLock; SDL_mutex *disposeLock;
SDL_mutex *submitLock; 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 #undef TRACK_RESOURCE
/* Resource Disposal */ /* 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( static void VULKAN_INTERNAL_RemoveFramebuffersContainingView(
VulkanRenderer *renderer, VulkanRenderer *renderer,
VkImageView view VkImageView view
@ -2740,10 +2798,9 @@ static void VULKAN_INTERNAL_RemoveFramebuffersContainingView(
{ {
if (hash->colorAttachmentViews[i] == view) if (hash->colorAttachmentViews[i] == view)
{ {
renderer->vkDestroyFramebuffer( VULKAN_INTERNAL_QueueDestroyFramebuffer(
renderer->logicalDevice, renderer,
renderer->framebufferHashArray.elements[i].value, renderer->framebufferHashArray.elements[i].value
NULL
); );
FramebufferHashArray_Remove( FramebufferHashArray_Remove(
@ -2945,6 +3002,7 @@ static void VULKAN_INTERNAL_DestroyCommandPool(
SDL_free(commandBuffer->usedSamplers); SDL_free(commandBuffer->usedSamplers);
SDL_free(commandBuffer->usedGraphicsPipelines); SDL_free(commandBuffer->usedGraphicsPipelines);
SDL_free(commandBuffer->usedComputePipelines); SDL_free(commandBuffer->usedComputePipelines);
SDL_free(commandBuffer->usedFramebuffers);
SDL_free(commandBuffer); SDL_free(commandBuffer);
} }
@ -4408,8 +4466,6 @@ static CreateSwapchainResult VULKAN_INTERNAL_CreateSwapchain(
&swapchainData->renderFinishedSemaphore &swapchainData->renderFinishedSemaphore
); );
swapchainData->needsRecreate = 0;
SDL_SetWindowData(windowHandle, WINDOW_SWAPCHAIN_DATA, swapchainData); SDL_SetWindowData(windowHandle, WINDOW_SWAPCHAIN_DATA, swapchainData);
if (renderer->swapchainDataCount >= renderer->swapchainDataCapacity) if (renderer->swapchainDataCount >= renderer->swapchainDataCapacity)
@ -4684,10 +4740,9 @@ static void VULKAN_DestroyDevice(
for (i = 0; i < renderer->framebufferHashArray.count; i += 1) for (i = 0; i < renderer->framebufferHashArray.count; i += 1)
{ {
renderer->vkDestroyFramebuffer( VULKAN_INTERNAL_DestroyFramebuffer(
renderer->logicalDevice, renderer,
renderer->framebufferHashArray.elements[i].value, renderer->framebufferHashArray.elements[i].value
NULL
); );
} }
@ -7779,7 +7834,7 @@ static VkRenderPass VULKAN_INTERNAL_FetchRenderPass(
return renderPass; return renderPass;
} }
static VkFramebuffer VULKAN_INTERNAL_FetchFramebuffer( static VulkanFramebuffer* VULKAN_INTERNAL_FetchFramebuffer(
VulkanRenderer *renderer, VulkanRenderer *renderer,
VkRenderPass renderPass, VkRenderPass renderPass,
Refresh_ColorAttachmentInfo *colorAttachmentInfos, Refresh_ColorAttachmentInfo *colorAttachmentInfos,
@ -7788,7 +7843,7 @@ static VkFramebuffer VULKAN_INTERNAL_FetchFramebuffer(
uint32_t width, uint32_t width,
uint32_t height uint32_t height
) { ) {
VkFramebuffer framebuffer; VulkanFramebuffer *vulkanFramebuffer;
VkFramebufferCreateInfo framebufferInfo; VkFramebufferCreateInfo framebufferInfo;
VkResult result; VkResult result;
VkImageView imageViewAttachments[2 * MAX_COLOR_TARGET_BINDINGS + 1]; VkImageView imageViewAttachments[2 * MAX_COLOR_TARGET_BINDINGS + 1];
@ -7850,17 +7905,21 @@ static VkFramebuffer VULKAN_INTERNAL_FetchFramebuffer(
hash.width = width; hash.width = width;
hash.height = height; hash.height = height;
framebuffer = FramebufferHashArray_Fetch( vulkanFramebuffer = FramebufferHashArray_Fetch(
&renderer->framebufferHashArray, &renderer->framebufferHashArray,
&hash &hash
); );
if (framebuffer != VK_NULL_HANDLE) if (vulkanFramebuffer != NULL)
{ {
SDL_UnlockMutex(renderer->framebufferFetchLock); SDL_UnlockMutex(renderer->framebufferFetchLock);
return framebuffer; return vulkanFramebuffer;
} }
vulkanFramebuffer = SDL_malloc(sizeof(VulkanFramebuffer));
SDL_AtomicSet(&vulkanFramebuffer->referenceCount, 0);
/* Create a new framebuffer */ /* Create a new framebuffer */
for (i = 0; i < colorAttachmentCount; i += 1) for (i = 0; i < colorAttachmentCount; i += 1)
@ -7918,7 +7977,7 @@ static VkFramebuffer VULKAN_INTERNAL_FetchFramebuffer(
renderer->logicalDevice, renderer->logicalDevice,
&framebufferInfo, &framebufferInfo,
NULL, NULL,
&framebuffer &vulkanFramebuffer->framebuffer
); );
if (result == VK_SUCCESS) if (result == VK_SUCCESS)
@ -7926,17 +7985,18 @@ static VkFramebuffer VULKAN_INTERNAL_FetchFramebuffer(
FramebufferHashArray_Insert( FramebufferHashArray_Insert(
&renderer->framebufferHashArray, &renderer->framebufferHashArray,
hash, hash,
framebuffer vulkanFramebuffer
); );
} }
else else
{ {
LogVulkanResultAsError("vkCreateFramebuffer", result); LogVulkanResultAsError("vkCreateFramebuffer", result);
framebuffer = VK_NULL_HANDLE; SDL_free(vulkanFramebuffer);
vulkanFramebuffer = NULL;
} }
SDL_UnlockMutex(renderer->framebufferFetchLock); SDL_UnlockMutex(renderer->framebufferFetchLock);
return framebuffer; return vulkanFramebuffer;
} }
static void VULKAN_INTERNAL_SetCurrentViewport( static void VULKAN_INTERNAL_SetCurrentViewport(
@ -8028,7 +8088,7 @@ static void VULKAN_BeginRenderPass(
VulkanRenderer* renderer = (VulkanRenderer*) driverData; VulkanRenderer* renderer = (VulkanRenderer*) driverData;
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer; VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
VkRenderPass renderPass; VkRenderPass renderPass;
VkFramebuffer framebuffer; VulkanFramebuffer *framebuffer;
VulkanTexture *texture; VulkanTexture *texture;
VkClearValue *clearValues; VkClearValue *clearValues;
@ -8095,6 +8155,8 @@ static void VULKAN_BeginRenderPass(
framebufferHeight framebufferHeight
); );
VULKAN_INTERNAL_TrackFramebuffer(renderer, vulkanCommandBuffer, framebuffer);
/* Layout transitions */ /* Layout transitions */
for (i = 0; i < colorAttachmentCount; i += 1) for (i = 0; i < colorAttachmentCount; i += 1)
@ -8169,8 +8231,8 @@ static void VULKAN_BeginRenderPass(
VkRenderPassBeginInfo renderPassBeginInfo; VkRenderPassBeginInfo renderPassBeginInfo;
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassBeginInfo.pNext = NULL; renderPassBeginInfo.pNext = NULL;
renderPassBeginInfo.renderPass = (VkRenderPass) renderPass; renderPassBeginInfo.renderPass = renderPass;
renderPassBeginInfo.framebuffer = framebuffer; renderPassBeginInfo.framebuffer = framebuffer->framebuffer;
renderPassBeginInfo.renderArea.extent.width = renderArea->w; renderPassBeginInfo.renderArea.extent.width = renderArea->w;
renderPassBeginInfo.renderArea.extent.height = renderArea->h; renderPassBeginInfo.renderArea.extent.height = renderArea->h;
renderPassBeginInfo.renderArea.offset.x = renderArea->x; renderPassBeginInfo.renderArea.offset.x = renderArea->x;
@ -8715,6 +8777,12 @@ static void VULKAN_INTERNAL_AllocateCommandBuffers(
commandBuffer->usedComputePipelineCapacity * sizeof(VulkanComputePipeline*) commandBuffer->usedComputePipelineCapacity * sizeof(VulkanComputePipeline*)
); );
commandBuffer->usedFramebufferCapacity = 4;
commandBuffer->usedFramebufferCount = 0;
commandBuffer->usedFramebuffers = SDL_malloc(
commandBuffer->usedFramebufferCapacity * sizeof(VulkanFramebuffer*)
);
vulkanCommandPool->inactiveCommandBuffers[ vulkanCommandPool->inactiveCommandBuffers[
vulkanCommandPool->inactiveCommandBufferCount vulkanCommandPool->inactiveCommandBufferCount
] = commandBuffer; ] = commandBuffer;
@ -8913,7 +8981,22 @@ static Refresh_Texture* VULKAN_AcquireSwapchainTexture(
&swapchainImageIndex &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]; swapchainTexture = &swapchainData->textures[swapchainImageIndex];
@ -8973,11 +9056,6 @@ static Refresh_Texture* VULKAN_AcquireSwapchainTexture(
vulkanCommandBuffer->signalSemaphores[vulkanCommandBuffer->signalSemaphoreCount] = swapchainData->renderFinishedSemaphore; vulkanCommandBuffer->signalSemaphores[vulkanCommandBuffer->signalSemaphoreCount] = swapchainData->renderFinishedSemaphore;
vulkanCommandBuffer->signalSemaphoreCount += 1; 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); SDL_UnlockMutex(renderer->disposeLock);
} }
@ -9217,6 +9309,12 @@ static void VULKAN_INTERNAL_CleanCommandBuffer(
} }
commandBuffer->usedComputePipelineCount = 0; 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 */ /* Return command buffer to pool */
SDL_LockMutex(renderer->acquireCommandBufferLock); SDL_LockMutex(renderer->acquireCommandBufferLock);
@ -9378,7 +9476,7 @@ static void VULKAN_Submit(
&presentInfo &presentInfo
); );
if (presentResult != VK_SUCCESS || presentData->swapchainData->needsRecreate) if (presentResult != VK_SUCCESS)
{ {
VULKAN_INTERNAL_RecreateSwapchain(renderer, presentData->swapchainData->windowHandle); VULKAN_INTERNAL_RecreateSwapchain(renderer, presentData->swapchainData->windowHandle);
} }
@ -10624,6 +10722,13 @@ static Refresh_Device* VULKAN_CreateDevice(
renderer->shaderModulesToDestroyCapacity renderer->shaderModulesToDestroyCapacity
); );
renderer->framebuffersToDestroyCapacity = 16;
renderer->framebuffersToDestroyCount = 0;
renderer->framebuffersToDestroy = SDL_malloc(
sizeof(VulkanFramebuffer*) *
renderer->framebuffersToDestroyCapacity
);
return result; return result;
} }