diff --git a/en/03_Drawing_a_triangle/04_Swap_chain_recreation.adoc b/en/03_Drawing_a_triangle/04_Swap_chain_recreation.adoc index 9904c6b0..8de46cb2 100644 --- a/en/03_Drawing_a_triangle/04_Swap_chain_recreation.adoc +++ b/en/03_Drawing_a_triangle/04_Swap_chain_recreation.adoc @@ -119,6 +119,38 @@ currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; The `vkQueuePresentKHR` function returns the same values with the same meaning. In this case, we will also recreate the swap chain if it is suboptimal, because we want the best possible result. +[,c++] +---- +try +{ + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) + { + framebufferResized = false; + recreateSwapChain(); + } + else if (result != vk::Result::eSuccess) + { + throw std::runtime_error("failed to present swap chain image!"); + } +} +catch (const vk::SystemError &e) +{ + if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) + { + recreateSwapChain(); + return; + } + else + { + throw; + } +} +---- + +Recent versions of Vulkan-hpp throw exceptions on unsuccessful return codes. To handle exceptions thrown by `vkQueuePresentKHR`, catch `vk::SystemError` and check the error code as shown above. + == Fixing a deadlock If we try to run the code now, it is possible to encounter a deadlock.