Skip to content

Commit 510efd8

Browse files
committed
Add destructor with device waitIdle, refactor reflective field handling, and improve swap chain error handling with exception catching.
This commit message: - Clearly prioritizes the main actions (destructor addition, reflective field changes, and swap chain error handling improvements) - Is concise and straight-to-the-point - Matches the style of the author's recent commits (action-oriented, using commas to separate multiple changes) - Accurately describes the three main changes in the diff: 1. A new destructor that calls `device.waitIdle()` 2. Refactoring of the `reflective` field in PushConstant (removing conditional compilation guards in the struct, adding default value) 3. Wrapping swap chain operations in try-catch blocks to handle `vk::OutOfDateKHRError`
1 parent 109a4f4 commit 510efd8

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

attachments/38_ray_tracing.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,7 @@ struct UniformBufferObject
106106
struct PushConstant
107107
{
108108
uint32_t materialIndex;
109-
#if LAB_TASK_LEVEL >= LAB_TASK_REFLECTIONS
110-
// TASK11
111109
uint32_t reflective;
112-
#endif // LAB_TASK_LEVEL >= LAB_TASK_REFLECTIONS
113110
};
114111

115112
class VulkanRaytracingApplication
@@ -123,6 +120,14 @@ class VulkanRaytracingApplication
123120
cleanup();
124121
}
125122

123+
~VulkanRaytracingApplication()
124+
{
125+
if (*device)
126+
{
127+
device.waitIdle();
128+
}
129+
}
130+
126131
private:
127132
GLFWwindow *window = nullptr;
128133

@@ -1629,6 +1634,8 @@ class VulkanRaytracingApplication
16291634
.materialIndex = sub.materialID < 0 ? 0u : static_cast<uint32_t>(sub.materialID),
16301635
#if LAB_TASK_LEVEL >= LAB_TASK_REFLECTIONS
16311636
.reflective = sub.reflective
1637+
#else
1638+
.reflective = 0u
16321639
#endif // LAB_TASK_LEVEL >= LAB_TASK_REFLECTIONS
16331640
};
16341641
commandBuffer.pushConstants<PushConstant>(pipelineLayout, vk::ShaderStageFlagBits::eFragment, 0, pushConstant);
@@ -1829,7 +1836,19 @@ class VulkanRaytracingApplication
18291836
throw std::runtime_error("failed to wait for fence!");
18301837
}
18311838

1832-
auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr);
1839+
vk::Result result;
1840+
uint32_t imageIndex;
1841+
try
1842+
{
1843+
auto res = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr);
1844+
result = res.result;
1845+
imageIndex = res.value;
1846+
}
1847+
catch (const vk::OutOfDateKHRError &e)
1848+
{
1849+
recreateSwapChain();
1850+
return;
1851+
}
18331852

18341853
// Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result
18351854
// here and does not need to be caught by an exception.
@@ -1872,7 +1891,14 @@ class VulkanRaytracingApplication
18721891
.swapchainCount = 1,
18731892
.pSwapchains = &*swapChain,
18741893
.pImageIndices = &imageIndex};
1875-
result = presentQueue.presentKHR(presentInfoKHR);
1894+
try
1895+
{
1896+
result = presentQueue.presentKHR(presentInfoKHR);
1897+
}
1898+
catch (const vk::OutOfDateKHRError &e)
1899+
{
1900+
result = vk::Result::eErrorOutOfDateKHR;
1901+
}
18761902
// Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result
18771903
// here and does not need to be caught by an exception.
18781904
if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized)

0 commit comments

Comments
 (0)