diff options
-rwxr-xr-x | main.c | 77 |
1 files changed, 40 insertions, 37 deletions
@@ -225,7 +225,7 @@ struct RenderVulkan { VkDevice device; VkQueue graphicsQueue; VkCommandPool commandPool; - struct SwapchainVulkan swapchain; + struct SwapchainVulkan *swapchain; VkCommandBuffer *commandBuffers; struct SyncVulkan sync; }; @@ -1269,11 +1269,11 @@ static int rvkCreateRawSwapchain(struct RenderVulkan *vk, int width, int height) VK_COLOR_SPACE_SRGB_NONLINEAR_KHR }; if (surfaceFormats[i].format == VK_FORMAT_UNDEFINED) { - vk->swapchain.surfaceFormat = want; + vk->swapchain->surfaceFormat = want; break; } if (surfaceFormats[i].format == want.format && surfaceFormats[i].colorSpace == want.colorSpace) { - vk->swapchain.surfaceFormat = want; + vk->swapchain->surfaceFormat = want; break; } } @@ -1310,20 +1310,20 @@ static int rvkCreateRawSwapchain(struct RenderVulkan *vk, int width, int height) printf("Using present mode:\t\t`%s`\t(%d)\n", strPresentMode(presentMode), presentMode); // TODO: Clamp - vk->swapchain.extent.width = width; - vk->swapchain.extent.height = height; + vk->swapchain->extent.width = width; + vk->swapchain->extent.height = height; - vk->swapchain.nImages = surfaceCapabilities.minImageCount; - //vk->swapchain.nImages = surfaceCapabilities.maxImageCount; + vk->swapchain->nImages = surfaceCapabilities.minImageCount; + //vk->swapchain->nImages = surfaceCapabilities.maxImageCount; - printf("Using %d swapchain images\n", vk->swapchain.nImages); + printf("Using %d swapchain images\n", vk->swapchain->nImages); VkSwapchainCreateInfoKHR createInfo = { 0 }; createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; createInfo.surface = vk->surface; - createInfo.minImageCount = vk->swapchain.nImages; - createInfo.imageFormat = vk->swapchain.surfaceFormat.format; - createInfo.imageExtent = vk->swapchain.extent; + createInfo.minImageCount = vk->swapchain->nImages; + createInfo.imageFormat = vk->swapchain->surfaceFormat.format; + createInfo.imageExtent = vk->swapchain->extent; createInfo.imageArrayLayers = 1; createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; @@ -1332,7 +1332,7 @@ static int rvkCreateRawSwapchain(struct RenderVulkan *vk, int width, int height) createInfo.presentMode = presentMode; createInfo.clipped = VK_TRUE; createInfo.oldSwapchain = VK_NULL_HANDLE; // TODO - if ((result = vk->dev->vkCreateSwapchainKHR(vk->device, &createInfo, ALLOC_VK, &vk->swapchain.rawSwapchain))) { + if ((result = vk->dev->vkCreateSwapchainKHR(vk->device, &createInfo, ALLOC_VK, &vk->swapchain->rawSwapchain))) { rvkSetErrMsg(vk, "Could not create swapchain: %d", result); return -1; } @@ -1341,8 +1341,8 @@ static int rvkCreateRawSwapchain(struct RenderVulkan *vk, int width, int height) static void rvkDestroyRawSwapchain(struct RenderVulkan *vk) { - if (vk->swapchain.rawSwapchain) { - vk->dev->vkDestroySwapchainKHR(vk->device, vk->swapchain.rawSwapchain, ALLOC_VK); + if (vk->swapchain->rawSwapchain) { + vk->dev->vkDestroySwapchainKHR(vk->device, vk->swapchain->rawSwapchain, ALLOC_VK); memset(&vk->swapchain, 0, sizeof(vk->swapchain)); } } @@ -1413,8 +1413,8 @@ static VkResult allocateCommandBuffers(struct RenderVulkan *vk) allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocInfo.commandPool = vk->commandPool; allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; - allocInfo.commandBufferCount = vk->swapchain.nImages; - vk->commandBuffers = malloc(vk->swapchain.nImages * sizeof(vk->commandBuffers)); + allocInfo.commandBufferCount = vk->swapchain->nImages; + vk->commandBuffers = malloc(vk->swapchain->nImages * sizeof(vk->commandBuffers)); return vk->dev->vkAllocateCommandBuffers(vk->device, &allocInfo, vk->commandBuffers); } @@ -1423,7 +1423,7 @@ static void freeCommandBuffers(struct RenderVulkan *vk) if (vk->commandBuffers) { vk->dev->vkFreeCommandBuffers(vk->device, vk->commandPool, - vk->swapchain.nImages, + vk->swapchain->nImages, vk->commandBuffers); free(vk->commandBuffers); vk->commandBuffers = NULL; @@ -1449,7 +1449,7 @@ static int recordCommandBuffers(struct RenderVulkan *vk) beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT; - for (uint32_t i = 0; i < vk->swapchain.nImages; ++i) { + for (uint32_t i = 0; i < vk->swapchain->nImages; ++i) { VkImageMemoryBarrier toClearBarrier = { 0 }; toClearBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; toClearBarrier.srcAccessMask = VK_ACCESS_MEMORY_READ_BIT; @@ -1458,7 +1458,7 @@ static int recordCommandBuffers(struct RenderVulkan *vk) toClearBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; toClearBarrier.srcQueueFamilyIndex = vk->graphicsIndex; toClearBarrier.dstQueueFamilyIndex = vk->graphicsIndex; - toClearBarrier.image = vk->swapchain.images[i]; + toClearBarrier.image = vk->swapchain->images[i]; toClearBarrier.subresourceRange = range; VkImageMemoryBarrier toPresentBarrier = { 0 }; @@ -1469,7 +1469,7 @@ static int recordCommandBuffers(struct RenderVulkan *vk) toPresentBarrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; toPresentBarrier.srcQueueFamilyIndex = vk->graphicsIndex; toPresentBarrier.dstQueueFamilyIndex = vk->graphicsIndex; - toPresentBarrier.image = vk->swapchain.images[i]; + toPresentBarrier.image = vk->swapchain->images[i]; toPresentBarrier.subresourceRange = range; vk->dev->vkBeginCommandBuffer(vk->commandBuffers[i], &beginInfo); @@ -1480,7 +1480,7 @@ static int recordCommandBuffers(struct RenderVulkan *vk) 0, NULL, 0, NULL, 1, &toClearBarrier); - vk->dev->vkCmdClearColorImage(vk->commandBuffers[i], vk->swapchain.images[i], + vk->dev->vkCmdClearColorImage(vk->commandBuffers[i], vk->swapchain->images[i], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clearValue, 1, &range); vk->dev->vkCmdPipelineBarrier(vk->commandBuffers[i], VK_PIPELINE_STAGE_TRANSFER_BIT, @@ -1497,11 +1497,12 @@ static int recordCommandBuffers(struct RenderVulkan *vk) int rvkCreateSwapchain(struct RenderVulkan *vk, const int width, const int height) { + vk->swapchain = calloc(1, sizeof(*vk->swapchain)); if (rvkCreateRawSwapchain(vk, width, height)) { return -1; } VkResult result; - if ((result = createSwapchainImageViews(vk->device, vk->dev, &vk->swapchain))) { + if ((result = createSwapchainImageViews(vk->device, vk->dev, vk->swapchain))) { rvkSetErrMsg(vk, "Could not create swapchain image views: %d", result); return -1; } @@ -1512,9 +1513,9 @@ int rvkCreateSwapchain(struct RenderVulkan *vk, const int width, const int heigh 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]))) { + 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; } @@ -1527,20 +1528,22 @@ 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]) { + 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], + vk->swapchain->fences[i], ALLOC_VK); } } - free(vk->swapchain.fences); - vk->swapchain.fences = NULL; + free(vk->swapchain->fences); + vk->swapchain->fences = NULL; } freeCommandBuffers(vk); - destroySwapchainImageViews(vk->device, vk->dev, &vk->swapchain); + destroySwapchainImageViews(vk->device, vk->dev, vk->swapchain); rvkDestroyRawSwapchain(vk); + free(vk->swapchain); + vk->swapchain = NULL; } /** Creates any semaphores or fences for this application. */ @@ -1673,7 +1676,7 @@ PuglStatus onDisplay(PuglView *view) // TODO: Swapchain recreation when resizing, minimizing, etc. if ((result = vk->dev->vkAcquireNextImageKHR( vk->device, - vk->swapchain.rawSwapchain, + vk->swapchain->rawSwapchain, UINT64_MAX, vk->sync.semaphore.presentComplete, VK_NULL_HANDLE, @@ -1690,9 +1693,9 @@ PuglStatus onDisplay(PuglView *view) * persistently mapped uniform buffer ranges. */ vk->dev->vkWaitForFences(vk->device, - 1, &vk->swapchain.fences[imageIndex], + 1, &vk->swapchain->fences[imageIndex], VK_TRUE, UINT64_MAX); - vk->dev->vkResetFences(vk->device, 1, &vk->swapchain.fences[imageIndex]); + vk->dev->vkResetFences(vk->device, 1, &vk->swapchain->fences[imageIndex]); const VkPipelineStageFlags waitStage = VK_PIPELINE_STAGE_TRANSFER_BIT; @@ -1708,7 +1711,7 @@ PuglStatus onDisplay(PuglView *view) if ((result = vk->dev->vkQueueSubmit(vk->graphicsQueue, 1, &submitInfo, - vk->swapchain.fences[imageIndex] + vk->swapchain->fences[imageIndex] ))) { rvkSetErrMsg(vk, "Could not submit to queue: %d", result); return PUGL_FAILURE; @@ -1719,7 +1722,7 @@ PuglStatus onDisplay(PuglView *view) presentInfo.waitSemaphoreCount = 1; presentInfo.pWaitSemaphores = &vk->sync.semaphore.renderFinished; presentInfo.swapchainCount = 1; - presentInfo.pSwapchains = &vk->swapchain.rawSwapchain; + presentInfo.pSwapchains = &vk->swapchain->rawSwapchain; presentInfo.pImageIndices = &imageIndex; presentInfo.pResults = NULL; |