From 543912f12082c2c04434d7aec93ba3567581422f Mon Sep 17 00:00:00 2001 From: Luigi Rapetta <48331636+rapfamily4@users.noreply.github.com> Date: Tue, 25 Nov 2025 16:32:07 +0100 Subject: [PATCH 1/2] docs: added try-catch code in swapchain recreation chapter --- .../04_Swap_chain_recreation.adoc | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) 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..b9096349 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 the Vulkan CPP header 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. From 17c2610e146eb045ec589c421f6b2d7c874e0034 Mon Sep 17 00:00:00 2001 From: Luigi Rapetta <48331636+rapfamily4@users.noreply.github.com> Date: Tue, 2 Dec 2025 21:01:47 +0100 Subject: [PATCH 2/2] docs: minor change to swap chain recreation --- en/03_Drawing_a_triangle/04_Swap_chain_recreation.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 b9096349..8de46cb2 100644 --- a/en/03_Drawing_a_triangle/04_Swap_chain_recreation.adoc +++ b/en/03_Drawing_a_triangle/04_Swap_chain_recreation.adoc @@ -149,7 +149,7 @@ catch (const vk::SystemError &e) } ---- -Recent versions of the Vulkan CPP header throw exceptions on unsuccessful return codes. To handle exceptions thrown by `vkQueuePresentKHR`, catch `vk::SystemError` and check the error code as shown above. +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