@@ -64,11 +64,10 @@ class HelloTriangleApplication
6464 vk::raii::CommandPool commandPool = nullptr ;
6565 std::vector<vk::raii::CommandBuffer> commandBuffers;
6666
67- std::vector<vk::raii::Semaphore> presentCompleteSemaphore ;
68- std::vector<vk::raii::Semaphore> renderFinishedSemaphore ;
67+ std::vector<vk::raii::Semaphore> presentCompleteSemaphores ;
68+ std::vector<vk::raii::Semaphore> renderFinishedSemaphores ;
6969 std::vector<vk::raii::Fence> inFlightFences;
70- uint32_t semaphoreIndex = 0 ;
71- uint32_t currentFrame = 0 ;
70+ uint32_t frameIndex = 0 ;
7271
7372 bool framebufferResized = false ;
7473
@@ -411,7 +410,8 @@ class HelloTriangleApplication
411410
412411 void recordCommandBuffer (uint32_t imageIndex)
413412 {
414- commandBuffers[currentFrame].begin ({});
413+ auto &commandBuffer = commandBuffers[frameIndex];
414+ commandBuffer.begin ({});
415415 // Before starting rendering, transition the swapchain image to COLOR_ATTACHMENT_OPTIMAL
416416 transition_image_layout (
417417 imageIndex,
@@ -434,12 +434,12 @@ class HelloTriangleApplication
434434 .layerCount = 1 ,
435435 .colorAttachmentCount = 1 ,
436436 .pColorAttachments = &attachmentInfo};
437- commandBuffers[currentFrame] .beginRendering (renderingInfo);
438- commandBuffers[currentFrame] .bindPipeline (vk::PipelineBindPoint::eGraphics, *graphicsPipeline);
439- commandBuffers[currentFrame] .setViewport (0 , vk::Viewport (0 .0f , 0 .0f , static_cast <float >(swapChainExtent.width ), static_cast <float >(swapChainExtent.height ), 0 .0f , 1 .0f ));
440- commandBuffers[currentFrame] .setScissor (0 , vk::Rect2D (vk::Offset2D (0 , 0 ), swapChainExtent));
441- commandBuffers[currentFrame] .draw (3 , 1 , 0 , 0 );
442- commandBuffers[currentFrame] .endRendering ();
437+ commandBuffer .beginRendering (renderingInfo);
438+ commandBuffer .bindPipeline (vk::PipelineBindPoint::eGraphics, *graphicsPipeline);
439+ commandBuffer .setViewport (0 , vk::Viewport (0 .0f , 0 .0f , static_cast <float >(swapChainExtent.width ), static_cast <float >(swapChainExtent.height ), 0 .0f , 1 .0f ));
440+ commandBuffer .setScissor (0 , vk::Rect2D (vk::Offset2D (0 , 0 ), swapChainExtent));
441+ commandBuffer .draw (3 , 1 , 0 , 0 );
442+ commandBuffer .endRendering ();
443443 // After rendering, transition the swapchain image to PRESENT_SRC
444444 transition_image_layout (
445445 imageIndex,
@@ -450,7 +450,7 @@ class HelloTriangleApplication
450450 vk::PipelineStageFlagBits2::eColorAttachmentOutput, // srcStage
451451 vk::PipelineStageFlagBits2::eBottomOfPipe // dstStage
452452 );
453- commandBuffers[currentFrame] .end ();
453+ commandBuffer .end ();
454454 }
455455
456456 void transition_image_layout (
@@ -482,32 +482,34 @@ class HelloTriangleApplication
482482 .dependencyFlags = {},
483483 .imageMemoryBarrierCount = 1 ,
484484 .pImageMemoryBarriers = &barrier};
485- commandBuffers[currentFrame ].pipelineBarrier2 (dependency_info);
485+ commandBuffers[frameIndex ].pipelineBarrier2 (dependency_info);
486486 }
487487
488488 void createSyncObjects ()
489489 {
490- presentCompleteSemaphore.clear ();
491- renderFinishedSemaphore.clear ();
492- inFlightFences.clear ();
490+ assert (presentCompleteSemaphores.empty () && renderFinishedSemaphores.empty () && inFlightFences.empty ());
493491
494492 for (size_t i = 0 ; i < swapChainImages.size (); i++)
495493 {
496- presentCompleteSemaphore.emplace_back (device, vk::SemaphoreCreateInfo ());
497- renderFinishedSemaphore.emplace_back (device, vk::SemaphoreCreateInfo ());
494+ renderFinishedSemaphores.emplace_back (device, vk::SemaphoreCreateInfo ());
498495 }
499496
500497 for (size_t i = 0 ; i < MAX_FRAMES_IN_FLIGHT; i++)
501498 {
499+ presentCompleteSemaphores.emplace_back (device, vk::SemaphoreCreateInfo ());
502500 inFlightFences.emplace_back (device, vk::FenceCreateInfo{.flags = vk::FenceCreateFlagBits::eSignaled});
503501 }
504502 }
505503
506504 void drawFrame ()
507505 {
508- while (vk::Result::eTimeout == device.waitForFences (*inFlightFences[currentFrame], vk::True, UINT64_MAX))
506+ // Note: inFlightFences, presentCompleteSemaphores, and commandBuffers are indexed by frameIndex,
507+ // while renderFinishedSemaphores is indexed by imageIndex
508+ while (vk::Result::eTimeout == device.waitForFences (*inFlightFences[frameIndex], vk::True, UINT64_MAX))
509509 ;
510- auto [result, imageIndex] = swapChain.acquireNextImage (UINT64_MAX, *presentCompleteSemaphore[semaphoreIndex], nullptr );
510+ device.resetFences (*inFlightFences[frameIndex]);
511+
512+ auto [result, imageIndex] = swapChain.acquireNextImage (UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr );
511513
512514 if (result == vk::Result::eErrorOutOfDateKHR)
513515 {
@@ -519,19 +521,29 @@ class HelloTriangleApplication
519521 throw std::runtime_error (" failed to acquire swap chain image!" );
520522 }
521523
522- device.resetFences (*inFlightFences[currentFrame ]);
523- commandBuffers[currentFrame ].reset ();
524+ device.resetFences (*inFlightFences[frameIndex ]);
525+ commandBuffers[frameIndex ].reset ();
524526 recordCommandBuffer (imageIndex);
525527
526528 vk::PipelineStageFlags waitDestinationStageMask (vk::PipelineStageFlagBits::eColorAttachmentOutput);
527- const vk::SubmitInfo submitInfo{.waitSemaphoreCount = 1 , .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1 , .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1 , .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex]};
528- queue.submit (submitInfo, *inFlightFences[currentFrame]);
529+ const vk::SubmitInfo submitInfo{.waitSemaphoreCount = 1 ,
530+ .pWaitSemaphores = &*presentCompleteSemaphores[frameIndex],
531+ .pWaitDstStageMask = &waitDestinationStageMask,
532+ .commandBufferCount = 1 ,
533+ .pCommandBuffers = &*commandBuffers[frameIndex],
534+ .signalSemaphoreCount = 1 ,
535+ .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]};
536+ queue.submit (submitInfo, *inFlightFences[frameIndex]);
529537
530538 try
531539 {
532- const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1 , .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1 , .pSwapchains = &*swapChain, .pImageIndices = &imageIndex};
540+ const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1 ,
541+ .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex],
542+ .swapchainCount = 1 ,
543+ .pSwapchains = &*swapChain,
544+ .pImageIndices = &imageIndex};
533545 result = queue.presentKHR (presentInfoKHR);
534- if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result:: eSuboptimalKHR || framebufferResized)
546+ if (result == vk::Result::eSuboptimalKHR || framebufferResized)
535547 {
536548 framebufferResized = false ;
537549 recreateSwapChain ();
@@ -553,8 +565,7 @@ class HelloTriangleApplication
553565 throw ;
554566 }
555567 }
556- semaphoreIndex = (semaphoreIndex + 1 ) % presentCompleteSemaphore.size ();
557- currentFrame = (currentFrame + 1 ) % MAX_FRAMES_IN_FLIGHT;
568+ frameIndex = (frameIndex + 1 ) % MAX_FRAMES_IN_FLIGHT;
558569 }
559570
560571 [[nodiscard]] vk::raii::ShaderModule createShaderModule (const std::vector<char > &code) const
0 commit comments