Skip to content

Commit 6ab0ac4

Browse files
committed
Reduce usage of waitIdle in 15_hello_triangle
1 parent 017a919 commit 6ab0ac4

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

attachments/15_hello_triangle.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class HelloTriangleApplication {
6363
vk::raii::CommandBuffer commandBuffer = nullptr;
6464

6565
vk::raii::Semaphore presentCompleteSemaphore = nullptr;
66-
vk::raii::Semaphore renderFinishedSemaphore = nullptr;
66+
std::vector<vk::raii::Semaphore> renderFinishedSemaphores;
6767
vk::raii::Fence drawFence = nullptr;
6868

6969
std::vector<const char*> requiredDeviceExtension = {
@@ -101,13 +101,11 @@ class HelloTriangleApplication {
101101
glfwPollEvents();
102102
drawFrame();
103103
}
104-
105-
device.waitIdle();
106104
}
107105

108106
void cleanup() {
107+
device.waitIdle(); // wait for device to finish operations before destroying resources
109108
glfwDestroyWindow(window);
110-
111109
glfwTerminate();
112110
}
113111

@@ -451,27 +449,36 @@ class HelloTriangleApplication {
451449

452450
void createSyncObjects() {
453451
presentCompleteSemaphore =vk::raii::Semaphore(device, vk::SemaphoreCreateInfo());
454-
renderFinishedSemaphore = vk::raii::Semaphore(device, vk::SemaphoreCreateInfo());
452+
for (size_t i = 0; i < swapChainImages.size(); i++)
453+
{
454+
renderFinishedSemaphores.push_back(vk::raii::Semaphore(device, vk::SemaphoreCreateInfo()));
455+
}
455456
drawFence = vk::raii::Fence(device, {.flags = vk::FenceCreateFlagBits::eSignaled});
456457
}
457458

458459
void drawFrame() {
459-
queue.waitIdle();
460+
while (vk::Result::eTimeout == device.waitForFences(*drawFence, vk::True, UINT64_MAX))
461+
;
462+
device.resetFences(*drawFence);
460463

461464
auto [result, imageIndex] = swapChain.acquireNextImage( UINT64_MAX, *presentCompleteSemaphore, nullptr );
462465
recordCommandBuffer(imageIndex);
463466

464-
device.resetFences( *drawFence );
465467
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
466-
const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore,
467-
.pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffer,
468-
.signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore };
468+
const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1,
469+
.pWaitSemaphores = &*presentCompleteSemaphore,
470+
.pWaitDstStageMask = &waitDestinationStageMask,
471+
.commandBufferCount = 1,
472+
.pCommandBuffers = &*commandBuffer,
473+
.signalSemaphoreCount = 1,
474+
.pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]};
469475
queue.submit(submitInfo, *drawFence);
470-
while ( vk::Result::eTimeout == device.waitForFences( *drawFence, vk::True, UINT64_MAX ) )
471-
;
472476

473-
const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore,
474-
.swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex };
477+
const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1,
478+
.pWaitSemaphores = &*renderFinishedSemaphores[imageIndex],
479+
.swapchainCount = 1,
480+
.pSwapchains = &*swapChain,
481+
.pImageIndices = &imageIndex };
475482
result = queue.presentKHR( presentInfoKHR );
476483
switch ( result )
477484
{

0 commit comments

Comments
 (0)