@@ -91,8 +91,9 @@ static VkCommandBuffer g_MainCommandBuffers[COMMAND_BUFFER_COUNT];
9191static VkFence g_MainCommandBufferExecutedFences[COMMAND_BUFFER_COUNT];
9292VkFence g_ImmediateFence;
9393static uint32_t g_NextCommandBufferIndex;
94- // Notice we need as many semaphores as there are swapchain images
95- static std::vector<VkSemaphore> g_hImageAvailableSemaphores;
94+ // Signaled by vkAcquireNextImageKHR.
95+ static VkSemaphore g_hImageAvailableSemaphores[COMMAND_BUFFER_COUNT];
96+ // Notice we need as many semaphores as there are swapchain images.
9697static std::vector<VkSemaphore> g_hRenderFinishedSemaphores;
9798static uint32_t g_SwapchainImageCount = 0 ;
9899static uint32_t g_SwapchainImageIndex = 0 ;
@@ -1365,45 +1366,58 @@ static void CreateSwapchain()
13651366
13661367 // Destroy the old semaphores and create new ones
13671368
1368- if (g_hImageAvailableSemaphores.size () < g_SwapchainImageCount) {
1369- g_hImageAvailableSemaphores.resize (g_SwapchainImageCount);
1370- }
13711369 if (g_hRenderFinishedSemaphores.size () < g_SwapchainImageCount) {
13721370 g_hRenderFinishedSemaphores.resize (g_SwapchainImageCount);
13731371 }
13741372
13751373 VkSemaphoreCreateInfo semaphoreInfo = { VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO };
13761374
1377- for (std::size_t swapchain_img_index = 0 ; swapchain_img_index < g_SwapchainImageCount; swapchain_img_index++) {
1378- if (g_hImageAvailableSemaphores.at (swapchain_img_index) != VK_NULL_HANDLE) {
1379- vkDestroySemaphore (g_hDevice, g_hImageAvailableSemaphores[swapchain_img_index], g_Allocs);
1380- g_hImageAvailableSemaphores[swapchain_img_index] = VK_NULL_HANDLE;
1375+ for (std::size_t i = COMMAND_BUFFER_COUNT; i--; )
1376+ {
1377+ if (g_hImageAvailableSemaphores[i] != VK_NULL_HANDLE)
1378+ {
1379+ vkDestroySemaphore (g_hDevice, g_hImageAvailableSemaphores[i], g_Allocs);
1380+ g_hImageAvailableSemaphores[i] = VK_NULL_HANDLE;
13811381 }
1382+ }
1383+ for (std::size_t swapchain_img_index = 0 ; swapchain_img_index < g_SwapchainImageCount; swapchain_img_index++) {
13821384 if (g_hRenderFinishedSemaphores.at (swapchain_img_index) != VK_NULL_HANDLE) {
13831385 vkDestroySemaphore (g_hDevice, g_hRenderFinishedSemaphores[swapchain_img_index], g_Allocs);
13841386 g_hRenderFinishedSemaphores[swapchain_img_index] = VK_NULL_HANDLE;
13851387 }
13861388 }
13871389
1388- for (std::size_t swapchain_img_index = 0 ; swapchain_img_index < g_SwapchainImageCount; swapchain_img_index++) {
1389- ERR_GUARD_VULKAN (vkCreateSemaphore (g_hDevice, &semaphoreInfo, g_Allocs, &g_hImageAvailableSemaphores[swapchain_img_index]));
1390- std::string semaphoreName = " g_hImageAvailableSemaphores[" + std::to_string (swapchain_img_index) + " ]" ;
1391- SetDebugUtilsObjectName (VK_OBJECT_TYPE_SEMAPHORE, reinterpret_cast <std::uint64_t >(g_hImageAvailableSemaphores[swapchain_img_index]), semaphoreName);
1390+ for (std::size_t i = 0 ; i < COMMAND_BUFFER_COUNT; ++i)
1391+ {
1392+ ERR_GUARD_VULKAN (vkCreateSemaphore (g_hDevice, &semaphoreInfo, g_Allocs, &g_hImageAvailableSemaphores[i]));
1393+ std::string semaphoreName = " g_hImageAvailableSemaphores[" + std::to_string (i) + " ]" ;
1394+ SetDebugUtilsObjectName (VK_OBJECT_TYPE_SEMAPHORE,
1395+ reinterpret_cast <std::uint64_t >(g_hImageAvailableSemaphores[i]), semaphoreName);
1396+ }
13921397
1398+ for (std::size_t swapchain_img_index = 0 ; swapchain_img_index < g_SwapchainImageCount; swapchain_img_index++)
1399+ {
13931400 ERR_GUARD_VULKAN (vkCreateSemaphore (g_hDevice, &semaphoreInfo, g_Allocs, &g_hRenderFinishedSemaphores[swapchain_img_index]));
1394- semaphoreName = " g_hRenderFinishedSemaphores[" + std::to_string (swapchain_img_index) + " ]" ;
1395- SetDebugUtilsObjectName (VK_OBJECT_TYPE_SEMAPHORE, reinterpret_cast <std::uint64_t >(g_hRenderFinishedSemaphores[swapchain_img_index]), semaphoreName);
1401+ std::string semaphoreName = " g_hRenderFinishedSemaphores[" + std::to_string (swapchain_img_index) + " ]" ;
1402+ SetDebugUtilsObjectName (VK_OBJECT_TYPE_SEMAPHORE,
1403+ reinterpret_cast <std::uint64_t >(g_hRenderFinishedSemaphores[swapchain_img_index]), semaphoreName);
13961404 }
13971405}
13981406
13991407static void DestroySwapchain (bool destroyActualSwapchain)
14001408{
1401- for (std::size_t swapchain_img_index = 0 ; swapchain_img_index < g_SwapchainImageCount; swapchain_img_index++) {
1402- if (g_hImageAvailableSemaphores.at (swapchain_img_index) != VK_NULL_HANDLE) {
1403- vkDestroySemaphore (g_hDevice, g_hImageAvailableSemaphores[swapchain_img_index], g_Allocs);
1404- g_hImageAvailableSemaphores[swapchain_img_index] = VK_NULL_HANDLE;
1409+ for (std::size_t i = 0 ; i < COMMAND_BUFFER_COUNT; i++)
1410+ {
1411+ if (g_hImageAvailableSemaphores[i] != VK_NULL_HANDLE)
1412+ {
1413+ vkDestroySemaphore (g_hDevice, g_hImageAvailableSemaphores[i], g_Allocs);
1414+ g_hImageAvailableSemaphores[i] = VK_NULL_HANDLE;
14051415 }
1406- if (g_hRenderFinishedSemaphores.at (swapchain_img_index) != VK_NULL_HANDLE) {
1416+ }
1417+ for (std::size_t swapchain_img_index = 0 ; swapchain_img_index < g_SwapchainImageCount; swapchain_img_index++)
1418+ {
1419+ if (g_hRenderFinishedSemaphores.at (swapchain_img_index) != VK_NULL_HANDLE)
1420+ {
14071421 vkDestroySemaphore (g_hDevice, g_hRenderFinishedSemaphores[swapchain_img_index], g_Allocs);
14081422 g_hRenderFinishedSemaphores[swapchain_img_index] = VK_NULL_HANDLE;
14091423 }
@@ -2405,9 +2419,11 @@ static void DrawFrame()
24052419 ERR_GUARD_VULKAN ( vkBeginCommandBuffer (hCommandBuffer, &commandBufferBeginInfo) );
24062420 SetDebugUtilsObjectName (VK_OBJECT_TYPE_COMMAND_BUFFER, reinterpret_cast <std::uint64_t >(hCommandBuffer), " hCommandBuffer" );
24072421
2422+ const VkSemaphore imageAvailableSemaphore = g_hImageAvailableSemaphores[cmdBufIndex];
2423+
24082424 // Acquire swapchain image
24092425 uint32_t imageIndex = 0 ;
2410- VkResult res = vkAcquireNextImageKHR (g_hDevice, g_hSwapchain, UINT64_MAX, g_hImageAvailableSemaphores. at (g_SwapchainImageIndex) , VK_NULL_HANDLE, &imageIndex);
2426+ VkResult res = vkAcquireNextImageKHR (g_hDevice, g_hSwapchain, UINT64_MAX, imageAvailableSemaphore , VK_NULL_HANDLE, &imageIndex);
24112427 if (res == VK_ERROR_OUT_OF_DATE_KHR)
24122428 {
24132429 RecreateSwapChain ();
@@ -2485,7 +2501,7 @@ static void DrawFrame()
24852501
24862502 // Submit command buffer
24872503
2488- VkSemaphore submitWaitSemaphores[] = { g_hImageAvailableSemaphores. at (g_SwapchainImageIndex) };
2504+ VkSemaphore submitWaitSemaphores[] = { imageAvailableSemaphore };
24892505 VkPipelineStageFlags submitWaitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
24902506 VkSemaphore submitSignalSemaphores[] = { g_hRenderFinishedSemaphores.at (g_SwapchainImageIndex)};
24912507 VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO };
0 commit comments