From 188dcf9711fa981b28120a219bd487ebcf44df1a Mon Sep 17 00:00:00 2001 From: Jordan Halase Date: Thu, 7 Nov 2019 13:37:18 -0600 Subject: Move fences into SwapchainVulkan --- main.c | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/main.c b/main.c index 6701e43..b486b5c 100755 --- a/main.c +++ b/main.c @@ -183,12 +183,14 @@ struct VulkanDev { /** Application-specific swapchain behavior */ struct SwapchainVulkan { + struct SwapchainVulkan *pNext; // Linked list of retired swapchains VkSwapchainKHR rawSwapchain; VkSurfaceFormatKHR surfaceFormat; uint32_t nImages; VkExtent2D extent; VkImage *images; VkImageView *imageViews; + VkFence *fences; }; struct SemaphoreVulkan { @@ -202,7 +204,6 @@ struct FenceVulkan { struct SyncVulkan { struct SemaphoreVulkan semaphore; - struct FenceVulkan fence; }; /** Vulkan application that uses Pugl for windows and events @@ -1508,6 +1509,16 @@ int rvkCreateSwapchain(struct RenderVulkan *vk, const int width, const int heigh rvkSetErrMsg(vk, "Could not allocate command buffers: %d", result); return -1; } + VkFenceCreateInfo fenceInfo = { 0 }; + fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; + fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; + vk->swapchain.fences = calloc(vk->swapchain.nImages, sizeof(*vk->swapchain.fences)); + for (uint32_t i = 0; i < vk->swapchain.nImages; ++i) { + if ((result = vk->dev->vkCreateFence(vk->device, &fenceInfo, ALLOC_VK, &vk->swapchain.fences[i]))) { + rvkSetErrMsg(vk, "Could not create render finished fence: %d", result); + return -1; + } + } if (recordCommandBuffers(vk)) { return -1; } @@ -1516,6 +1527,17 @@ int rvkCreateSwapchain(struct RenderVulkan *vk, const int width, const int heigh void rvkDestroySwapchain(struct RenderVulkan *vk) { + if (vk->swapchain.fences) { + for (uint32_t i = 0; i < vk->swapchain.nImages; ++i) { + if (vk->swapchain.fences[i]) { + vk->dev->vkDestroyFence(vk->device, + vk->swapchain.fences[i], + ALLOC_VK); + } + } + free(vk->swapchain.fences); + vk->swapchain.fences = NULL; + } freeCommandBuffers(vk); destroySwapchainImageViews(vk->device, vk->dev, &vk->swapchain); rvkDestroyRawSwapchain(vk); @@ -1528,33 +1550,11 @@ int rvkCreateSyncObjects(struct RenderVulkan *vk) semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; vk->dev->vkCreateSemaphore(vk->device, &semaphoreInfo, ALLOC_VK, &vk->sync.semaphore.presentComplete); vk->dev->vkCreateSemaphore(vk->device, &semaphoreInfo, ALLOC_VK, &vk->sync.semaphore.renderFinished); - VkFenceCreateInfo fenceInfo = { 0 }; - fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; - fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; - vk->sync.fence.swapchain = calloc(vk->swapchain.nImages, sizeof(*vk->sync.fence.swapchain)); - VkResult result; - for (uint32_t i = 0; i < vk->swapchain.nImages; ++i) { - if ((result = vk->dev->vkCreateFence(vk->device, &fenceInfo, ALLOC_VK, &vk->sync.fence.swapchain[i]))) { - rvkSetErrMsg(vk, "Could not create render finished fence: %d", result); - return -1; - } - } return 0; } void rvkDestroySyncObjects(struct RenderVulkan *vk) { - if (vk->sync.fence.swapchain) { - for (uint32_t i = 0; i < vk->swapchain.nImages; ++i) { - if (vk->sync.fence.swapchain[i]) { - vk->dev->vkDestroyFence(vk->device, - vk->sync.fence.swapchain[i], - ALLOC_VK); - } - } - free(vk->sync.fence.swapchain); - vk->sync.fence.swapchain = NULL; - } if (vk->sync.semaphore.renderFinished) { vk->dev->vkDestroySemaphore(vk->device, vk->sync.semaphore.renderFinished, ALLOC_VK); vk->sync.semaphore.renderFinished = VK_NULL_HANDLE; @@ -1690,9 +1690,9 @@ PuglStatus onDisplay(PuglView *view) * persistently mapped uniform buffer ranges. */ vk->dev->vkWaitForFences(vk->device, - 1, &vk->sync.fence.swapchain[imageIndex], + 1, &vk->swapchain.fences[imageIndex], VK_TRUE, UINT64_MAX); - vk->dev->vkResetFences(vk->device, 1, &vk->sync.fence.swapchain[imageIndex]); + vk->dev->vkResetFences(vk->device, 1, &vk->swapchain.fences[imageIndex]); const VkPipelineStageFlags waitStage = VK_PIPELINE_STAGE_TRANSFER_BIT; @@ -1708,7 +1708,7 @@ PuglStatus onDisplay(PuglView *view) if ((result = vk->dev->vkQueueSubmit(vk->graphicsQueue, 1, &submitInfo, - vk->sync.fence.swapchain[imageIndex] + vk->swapchain.fences[imageIndex] ))) { rvkSetErrMsg(vk, "Could not submit to queue: %d", result); return PUGL_FAILURE; -- cgit v1.2.1