From 4d0158c0ec5c213d4d9f4a9a6bb8a68df7a9eb1a Mon Sep 17 00:00:00 2001 From: kearwood Date: Thu, 15 Sep 2022 00:26:23 -0700 Subject: [PATCH] Added KRSurface::m_inFlightFences. Now properly handling multiple frames in flight and waiting on fences when the CPU catches up. --- kraken/KRSurface.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/kraken/KRSurface.cpp b/kraken/KRSurface.cpp index 5887a4b..e33eacd 100644 --- a/kraken/KRSurface.cpp +++ b/kraken/KRSurface.cpp @@ -46,6 +46,7 @@ KRSurface::KRSurface(KRContext& context) , m_surface(VK_NULL_HANDLE) , m_imageAvailableSemaphores{VK_NULL_HANDLE} , m_renderFinishedSemaphores{VK_NULL_HANDLE} + , m_inFlightFences{VK_NULL_HANDLE} , m_frameIndex(0) { m_forwardOpaquePass = std::make_unique(context); @@ -78,6 +79,11 @@ KrResult KRSurface::initialize() VkSemaphoreCreateInfo semaphoreInfo{}; semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; + + VkFenceCreateInfo fenceInfo{}; + fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; + fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; + for(int i = 0; i < KRENGINE_MAX_FRAMES_IN_FLIGHT; i++) { if (vkCreateSemaphore(device->m_logicalDevice, &semaphoreInfo, nullptr, &m_imageAvailableSemaphores[i]) != VK_SUCCESS) { return KR_ERROR_VULKAN; @@ -85,6 +91,9 @@ KrResult KRSurface::initialize() if (vkCreateSemaphore(device->m_logicalDevice, &semaphoreInfo, nullptr, &m_renderFinishedSemaphores[i]) != VK_SUCCESS) { return KR_ERROR_VULKAN; } + if (vkCreateFence(device->m_logicalDevice, &fenceInfo, nullptr, &m_inFlightFences[i]) != VK_SUCCESS) { + return KR_ERROR_VULKAN; + } } return createSwapChain(); @@ -118,6 +127,11 @@ void KRSurface::destroy() vkDestroySemaphore(device->m_logicalDevice, m_imageAvailableSemaphores[i], nullptr); m_imageAvailableSemaphores[i] = VK_NULL_HANDLE; } + + if (device && m_inFlightFences[i] != VK_NULL_HANDLE) { + vkDestroyFence(device->m_logicalDevice, m_inFlightFences[i], nullptr); + m_inFlightFences[i] = VK_NULL_HANDLE; + } } if (m_surface != VK_NULL_HANDLE) {