From 41bb083a8453990bdb6528fd87c6c6be9837a165 Mon Sep 17 00:00:00 2001 From: asuessenbach Date: Wed, 22 Oct 2025 20:56:24 +0200 Subject: [PATCH 1/2] Reduce usage of waitIdle in 15_hello_triangle --- attachments/15_hello_triangle.cpp | 35 ++++++++++++++++++------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/attachments/15_hello_triangle.cpp b/attachments/15_hello_triangle.cpp index de98ba03..8f5f55dc 100644 --- a/attachments/15_hello_triangle.cpp +++ b/attachments/15_hello_triangle.cpp @@ -63,7 +63,7 @@ class HelloTriangleApplication { vk::raii::CommandBuffer commandBuffer = nullptr; vk::raii::Semaphore presentCompleteSemaphore = nullptr; - vk::raii::Semaphore renderFinishedSemaphore = nullptr; + std::vector renderFinishedSemaphores; vk::raii::Fence drawFence = nullptr; std::vector requiredDeviceExtension = { @@ -101,13 +101,11 @@ class HelloTriangleApplication { glfwPollEvents(); drawFrame(); } - - device.waitIdle(); + device.waitIdle(); // wait for device to finish operations before destroying resources } void cleanup() { glfwDestroyWindow(window); - glfwTerminate(); } @@ -451,27 +449,36 @@ class HelloTriangleApplication { void createSyncObjects() { presentCompleteSemaphore =vk::raii::Semaphore(device, vk::SemaphoreCreateInfo()); - renderFinishedSemaphore = vk::raii::Semaphore(device, vk::SemaphoreCreateInfo()); + for (size_t i = 0; i < swapChainImages.size(); i++) + { + renderFinishedSemaphores.emplace_back(device, vk::SemaphoreCreateInfo()); + } drawFence = vk::raii::Fence(device, {.flags = vk::FenceCreateFlagBits::eSignaled}); } void drawFrame() { - queue.waitIdle(); + while (vk::Result::eTimeout == device.waitForFences(*drawFence, vk::True, UINT64_MAX)) + ; + device.resetFences(*drawFence); auto [result, imageIndex] = swapChain.acquireNextImage( UINT64_MAX, *presentCompleteSemaphore, nullptr ); recordCommandBuffer(imageIndex); - device.resetFences( *drawFence ); vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput ); - const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore, - .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffer, - .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore }; + const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, + .pWaitSemaphores = &*presentCompleteSemaphore, + .pWaitDstStageMask = &waitDestinationStageMask, + .commandBufferCount = 1, + .pCommandBuffers = &*commandBuffer, + .signalSemaphoreCount = 1, + .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *drawFence); - while ( vk::Result::eTimeout == device.waitForFences( *drawFence, vk::True, UINT64_MAX ) ) - ; - const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore, - .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; + const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex }; result = queue.presentKHR( presentInfoKHR ); switch ( result ) { From 6a4c0ccc15cf136982588b82fe96efb1b1ad0402 Mon Sep 17 00:00:00 2001 From: asuessenbach Date: Wed, 29 Oct 2025 14:21:17 +0100 Subject: [PATCH 2/2] Revert changes in drawFrame, add some comment to waitIdle --- attachments/15_hello_triangle.cpp | 33 ++++++++++++------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/attachments/15_hello_triangle.cpp b/attachments/15_hello_triangle.cpp index 8f5f55dc..13f5bef3 100644 --- a/attachments/15_hello_triangle.cpp +++ b/attachments/15_hello_triangle.cpp @@ -63,7 +63,7 @@ class HelloTriangleApplication { vk::raii::CommandBuffer commandBuffer = nullptr; vk::raii::Semaphore presentCompleteSemaphore = nullptr; - std::vector renderFinishedSemaphores; + vk::raii::Semaphore renderFinishedSemaphore = nullptr; vk::raii::Fence drawFence = nullptr; std::vector requiredDeviceExtension = { @@ -106,6 +106,7 @@ class HelloTriangleApplication { void cleanup() { glfwDestroyWindow(window); + glfwTerminate(); } @@ -449,36 +450,28 @@ class HelloTriangleApplication { void createSyncObjects() { presentCompleteSemaphore =vk::raii::Semaphore(device, vk::SemaphoreCreateInfo()); - for (size_t i = 0; i < swapChainImages.size(); i++) - { - renderFinishedSemaphores.emplace_back(device, vk::SemaphoreCreateInfo()); - } + renderFinishedSemaphore = vk::raii::Semaphore(device, vk::SemaphoreCreateInfo()); drawFence = vk::raii::Fence(device, {.flags = vk::FenceCreateFlagBits::eSignaled}); } void drawFrame() { - while (vk::Result::eTimeout == device.waitForFences(*drawFence, vk::True, UINT64_MAX)) - ; - device.resetFences(*drawFence); + queue.waitIdle(); // NOTE: for simplicity, wait for the queue to be idle before starting the frame + // In the next chapter you see how to use multiple frames in flight and fences to sync auto [result, imageIndex] = swapChain.acquireNextImage( UINT64_MAX, *presentCompleteSemaphore, nullptr ); recordCommandBuffer(imageIndex); + device.resetFences( *drawFence ); vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput ); - const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, - .pWaitSemaphores = &*presentCompleteSemaphore, - .pWaitDstStageMask = &waitDestinationStageMask, - .commandBufferCount = 1, - .pCommandBuffers = &*commandBuffer, - .signalSemaphoreCount = 1, - .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; + const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore, + .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffer, + .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore }; queue.submit(submitInfo, *drawFence); + while ( vk::Result::eTimeout == device.waitForFences( *drawFence, vk::True, UINT64_MAX ) ) + ; - const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex }; + const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore, + .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; result = queue.presentKHR( presentInfoKHR ); switch ( result ) {