Skip to content

Commit c9f3ee2

Browse files
authored
Align docu in 04_Swap_chain_recreation.adoc with the sources in 17_swap_chain_recreation.cpp (#347)
1 parent 545bcee commit c9f3ee2

2 files changed

Lines changed: 27 additions & 23 deletions

File tree

attachments/17_swap_chain_recreation.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,8 @@ class HelloTriangleApplication
425425
{
426426
auto &commandBuffer = commandBuffers[frameIndex];
427427
commandBuffer.begin({});
428-
// Before starting rendering, transition the swapchain image to COLOR_ATTACHMENT_OPTIMAL
428+
429+
// Before starting rendering, transition the swapchain image to vk::ImageLayout::eColorAttachmentOptimal
429430
transition_image_layout(
430431
imageIndex,
431432
vk::ImageLayout::eUndefined,
@@ -453,7 +454,8 @@ class HelloTriangleApplication
453454
commandBuffer.setScissor(0, vk::Rect2D(vk::Offset2D(0, 0), swapChainExtent));
454455
commandBuffer.draw(3, 1, 0, 0);
455456
commandBuffer.endRendering();
456-
// After rendering, transition the swapchain image to PRESENT_SRC
457+
458+
// After rendering, transition the swapchain image to vk::ImageLayout::ePresentSrcKHR
457459
transition_image_layout(
458460
imageIndex,
459461
vk::ImageLayout::eColorAttachmentOptimal,
@@ -535,7 +537,7 @@ class HelloTriangleApplication
535537
}
536538
// On other success codes than eSuccess and eSuboptimalKHR we just throw an exception.
537539
// On any error code, aquireNextImage already threw an exception.
538-
else if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR)
540+
if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR)
539541
{
540542
assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady);
541543
throw std::runtime_error("failed to acquire swap chain image!");

en/03_Drawing_a_triangle/04_Swap_chain_recreation.adoc

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ Create a new `recreateSwapChain` function that calls `createSwapChain` and all t
1515

1616
[,c++]
1717
----
18-
void recreateSwapChain() {
18+
void recreateSwapChain()
19+
{
1920
device.waitIdle();
2021
2122
createSwapChain();
2223
createImageViews();
2324
}
2425
----
2526

26-
We first call `vkDeviceWaitIdle`, because just like in the last chapter, we shouldn't touch resources that may still be in use.
27+
We first call `vk::raii::Device::waitIdle`, because just like in the last chapter, we shouldn't touch resources that may still be in use.
2728
Obviously, we'll have to recreate the swap chain itself.
2829
The image views need to be recreated because they are based directly on the swap chain images.
2930

@@ -32,11 +33,12 @@ Let's call it `cleanupSwapChain`:
3233

3334
[,c++]
3435
----
35-
void cleanupSwapChain() {
36-
36+
void cleanupSwapChain()
37+
{
3738
}
3839
39-
void recreateSwapChain() {
40+
void recreateSwapChain()
41+
{
4042
device.waitIdle();
4143
4244
cleanupSwapChain();
@@ -55,12 +57,14 @@ We'll move the cleanup code of all objects that are recreated as part of a swap
5557

5658
[,c++]
5759
----
58-
void cleanupSwapChain() {
60+
void cleanupSwapChain()
61+
{
5962
swapChainImageViews.clear();
6063
swapChain = nullptr;
6164
}
6265
63-
void cleanup() {
66+
void cleanup()
67+
{
6468
cleanupSwapChain();
6569
6670
glfwDestroyWindow(window);
@@ -73,7 +77,7 @@ Note that in `chooseSwapExtent` we already query the new window resolution to ma
7377
That's all it takes to recreate the swap chain!
7478
However, the disadvantage of this approach is that we need to stop all renderings before creating the new swap chain.
7579
It is possible to create a new swap chain while drawing commands on an image from the old swap chain are still in-flight.
76-
You need to pass the previous swap chain to the `oldSwapchain` field in the `VkSwapchainCreateInfoKHR` struct and destroy the old swap chain as soon as you've finished using it.
80+
You need to pass the previous swap chain to the `oldSwapchain` field in the `vk::SwapchainCreateInfoKHR` struct and destroy the old swap chain as soon as you've finished using it.
7781

7882
== Suboptimal or out-of-date swap chain
7983

@@ -120,8 +124,6 @@ else
120124
// There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception.
121125
assert(result == vk::Result::eSuccess);
122126
}
123-
124-
frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT;
125127
----
126128

127129
The `vk::raii::Queue::presentKHR` function returns the same values with the same meaning.
@@ -135,9 +137,8 @@ const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1,
135137
.pSwapchains = &*swapChain,
136138
.pImageIndices = &imageIndex};
137139
result = queue.presentKHR(presentInfoKHR);
138-
if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized)
140+
if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR))
139141
{
140-
framebufferResized = false;
141142
recreateSwapChain();
142143
}
143144
else
@@ -176,7 +177,7 @@ if (result == vk::Result::eErrorOutOfDateKHR)
176177
recreateSwapChain();
177178
return;
178179
}
179-
else if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR)
180+
if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR)
180181
{
181182
assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady);
182183
throw std::runtime_error("failed to acquire swap chain image!");
@@ -194,16 +195,15 @@ First, add a new member variable that flags that a resize has happened:
194195

195196
[,c++]
196197
----
197-
std::vector<vk::raii::Fence> inFlightFences;
198-
199198
bool framebufferResized = false;
200199
----
201200

202201
The `drawFrame` function should then be modified to also check for this flag:
203202

204203
[,c++]
205204
----
206-
if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) {
205+
if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized)
206+
{
207207
framebufferResized = false;
208208
recreateSwapChain();
209209
}
@@ -218,7 +218,8 @@ Now, to actually detect resizes, we can use the `glfwSetFramebufferSizeCallback`
218218

219219
[,c++]
220220
----
221-
void initWindow() {
221+
void initWindow()
222+
{
222223
glfwInit();
223224
224225
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
@@ -227,8 +228,8 @@ void initWindow() {
227228
glfwSetFramebufferSizeCallback(window, framebufferResizeCallback);
228229
}
229230
230-
static void framebufferResizeCallback(GLFWwindow* window, int width, int height) {
231-
231+
static void framebufferResizeCallback(GLFWwindow* window, int width, int height)
232+
{
232233
}
233234
----
234235

@@ -247,7 +248,8 @@ This value can now be retrieved from within the callback with `glfwGetWindowUser
247248

248249
[,c++]
249250
----
250-
static void framebufferResizeCallback(GLFWwindow* window, int width, int height) {
251+
static void framebufferResizeCallback(GLFWwindow* window, int width, int height)
252+
{
251253
auto app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
252254
app->framebufferResized = true;
253255
}

0 commit comments

Comments
 (0)