Skip to content

Commit 9c35c9b

Browse files
committed
Correctly handle vk::Result::eOutOfDate from vk::raii::SwapchainKHR::acquireNextImage and vk::raii::Queue::presentKHR
1 parent 0e3035d commit 9c35c9b

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

attachments/17_swap_chain_recreation.cpp

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -511,13 +511,18 @@ class HelloTriangleApplication
511511

512512
auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr);
513513

514+
// Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result
515+
// here and does not need to be caught by an exception.
514516
if (result == vk::Result::eErrorOutOfDateKHR)
515517
{
516518
recreateSwapChain();
517519
return;
518520
}
521+
// On other success codes than eSuccess and eSuboptimalKHR we just throw an exception.
522+
// On any error code, aquireNextImage already threw an exception.
519523
if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR)
520524
{
525+
assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady);
521526
throw std::runtime_error("failed to acquire swap chain image!");
522527
}
523528

@@ -535,35 +540,23 @@ class HelloTriangleApplication
535540
.pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]};
536541
queue.submit(submitInfo, *inFlightFences[frameIndex]);
537542

538-
try
543+
const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1,
544+
.pWaitSemaphores = &*renderFinishedSemaphores[imageIndex],
545+
.swapchainCount = 1,
546+
.pSwapchains = &*swapChain,
547+
.pImageIndices = &imageIndex};
548+
result = queue.presentKHR(presentInfoKHR);
549+
// Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result
550+
// here and does not need to be caught by an exception.
551+
if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized)
539552
{
540-
const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1,
541-
.pWaitSemaphores = &*renderFinishedSemaphores[imageIndex],
542-
.swapchainCount = 1,
543-
.pSwapchains = &*swapChain,
544-
.pImageIndices = &imageIndex};
545-
result = queue.presentKHR(presentInfoKHR);
546-
if (result == vk::Result::eSuboptimalKHR || framebufferResized)
547-
{
548-
framebufferResized = false;
549-
recreateSwapChain();
550-
}
551-
else if (result != vk::Result::eSuccess)
552-
{
553-
throw std::runtime_error("failed to present swap chain image!");
554-
}
553+
framebufferResized = false;
554+
recreateSwapChain();
555555
}
556-
catch (const vk::SystemError &e)
556+
else
557557
{
558-
if (e.code().value() == static_cast<int>(vk::Result::eErrorOutOfDateKHR))
559-
{
560-
recreateSwapChain();
561-
return;
562-
}
563-
else
564-
{
565-
throw;
566-
}
558+
// There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception.
559+
assert(result == vk::Result::eSuccess);
567560
}
568561
frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT;
569562
}

attachments/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ endif()
1414

1515
find_package (glfw3 REQUIRED)
1616
find_package (glm REQUIRED)
17-
find_package (Vulkan REQUIRED)
17+
find_package (Vulkan 1.4.335 REQUIRED) # Require Vulkan SDK version 1.4.335 or higher
1818
find_package (tinyobjloader REQUIRED)
1919
find_package (tinygltf REQUIRED)
2020
find_package (KTX REQUIRED)
@@ -139,6 +139,9 @@ function (add_chapter CHAPTER_NAME)
139139
target_compile_definitions(${CHAPTER_NAME} PRIVATE USE_CPP20_MODULES=1)
140140
endif()
141141

142+
# Define VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS to treat VK_ERROR_OUT_OF_DATE_KHR as a success code
143+
target_compile_definitions(${CHAPTER_NAME} PRIVATE "VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS" )
144+
142145
if(WIN32)
143146
if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*")
144147
set_target_properties(${CHAPTER_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/${CHAPTER_NAME}")

0 commit comments

Comments
 (0)