Skip to content

Commit ac5882b

Browse files
authored
docs: added try-catch code in swapchain recreation chapter (#235)
* docs: added try-catch code in swapchain recreation chapter * docs: minor change to swap chain recreation
1 parent a67ef9e commit ac5882b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

en/03_Drawing_a_triangle/04_Swap_chain_recreation.adoc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,38 @@ frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT;
119119
The `vkQueuePresentKHR` function returns the same values with the same meaning.
120120
In this case, we will also recreate the swap chain if it is suboptimal, because we want the best possible result.
121121

122+
[,c++]
123+
----
124+
try
125+
{
126+
const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex};
127+
result = queue.presentKHR(presentInfoKHR);
128+
if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized)
129+
{
130+
framebufferResized = false;
131+
recreateSwapChain();
132+
}
133+
else if (result != vk::Result::eSuccess)
134+
{
135+
throw std::runtime_error("failed to present swap chain image!");
136+
}
137+
}
138+
catch (const vk::SystemError &e)
139+
{
140+
if (e.code().value() == static_cast<int>(vk::Result::eErrorOutOfDateKHR))
141+
{
142+
recreateSwapChain();
143+
return;
144+
}
145+
else
146+
{
147+
throw;
148+
}
149+
}
150+
----
151+
152+
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.
153+
122154
== Fixing a deadlock
123155

124156
If we try to run the code now, it is possible to encounter a deadlock.

0 commit comments

Comments
 (0)