Skip to content

Commit 9796ad7

Browse files
committed
Correctly handle vk::Result::eOutOfDate from vk::raii::SwapchainKHR::acquireNextImage and vk::raii::Queue::presentKHR
1 parent 682e879 commit 9796ad7

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
@@ -514,13 +514,18 @@ class HelloTriangleApplication
514514

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

517+
// Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result
518+
// here and does not need to be caught by an exception.
517519
if (result == vk::Result::eErrorOutOfDateKHR)
518520
{
519521
recreateSwapChain();
520522
return;
521523
}
524+
// On other success codes than eSuccess and eSuboptimalKHR we just throw an exception.
525+
// On any error code, aquireNextImage already threw an exception.
522526
if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR)
523527
{
528+
assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady);
524529
throw std::runtime_error("failed to acquire swap chain image!");
525530
}
526531

@@ -537,35 +542,23 @@ class HelloTriangleApplication
537542
.pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]};
538543
queue.submit(submitInfo, *inFlightFences[frameIndex]);
539544

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

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)