From b5f769a3a5de62f891ef1c7be6aac17cd62155e8 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Tue, 7 Apr 2026 15:49:59 -0500 Subject: [PATCH] recreateSwapchain: create/destroy renderSemaphores --- src/main.cpp | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 5f9b987..a3514e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,24 @@ #include "new.h" #include "file.h" +template +inline static constexpr T min(T a, T b) +{ + return (a < b) ? a : b; +} + +template +inline static constexpr T max(T a, T b) +{ + return (a > b) ? a : b; +} + +template +inline static constexpr T clamp(T n, T minVal, T maxVal) +{ + return min(max(n, minVal), maxVal); +} + #define SDL_CHECK(f) \ { \ bool result = (f); \ @@ -236,6 +254,12 @@ void recreateSwapchain(VkSurfaceFormatKHR surfaceFormat, VkFormat depthFormat, V } free(swapchainImageViews); } + if (renderSemaphores != nullptr) { + for (uint32_t i = 0; i < swapchainImageCount; i++) { + vkDestroySemaphore(device, renderSemaphores[i], nullptr); + } + free(renderSemaphores); + } VK_CHECK(vkGetSwapchainImagesKHR(device, swapchain, &swapchainImageCount, nullptr)); swapchainImages = NewM(swapchainImageCount); @@ -260,6 +284,18 @@ void recreateSwapchain(VkSurfaceFormatKHR surfaceFormat, VkFormat depthFormat, V vkDestroySwapchainKHR(device, swapchainCreateInfo.oldSwapchain, nullptr); } + ////////////////////////////////////////////////////////////////////// + // render semaphores + ////////////////////////////////////////////////////////////////////// + + VkSemaphoreCreateInfo semaphoreCreateInfo{ + .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO + }; + renderSemaphores = NewM(swapchainImageCount); + for (uint32_t i = 0; i < swapchainImageCount; i++) { + VK_CHECK(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &renderSemaphores[i])); + } + ////////////////////////////////////////////////////////////////////// // depth ////////////////////////////////////////////////////////////////////// @@ -651,10 +687,6 @@ int main() VK_CHECK(vkCreateFence(device, &fenceCreateInfo, nullptr, &fences[i])); VK_CHECK(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &presentSemaphores[i])); } - renderSemaphores = NewM(swapchainImageCount); - for (uint32_t i = 0; i < swapchainImageCount; i++) { - VK_CHECK(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &renderSemaphores[i])); - } ////////////////////////////////////////////////////////////////////// // command