Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0dd1459

Browse files
committed
Fix bad vkResetFences placement in triangle chapter
1 parent dd3c347 commit 0dd1459

3 files changed

Lines changed: 18 additions & 14 deletions

File tree

code/15_hello_triangle.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,6 @@ class HelloTriangleApplication {
642642

643643
void drawFrame() {
644644
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
645-
vkResetFences(device, 1, &inFlightFences[currentFrame]);
646645

647646
uint32_t imageIndex;
648647
vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
@@ -668,6 +667,8 @@ class HelloTriangleApplication {
668667
submitInfo.signalSemaphoreCount = 1;
669668
submitInfo.pSignalSemaphores = signalSemaphores;
670669

670+
vkResetFences(device, 1, &inFlightFences[currentFrame]);
671+
671672
if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
672673
throw std::runtime_error("failed to submit draw command buffer!");
673674
}

en/03_Drawing_a_triangle/03_Drawing/02_Rendering_and_presentation.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,22 @@ void drawFrame() {
583583
}
584584
```
585585

586+
Because we now have more calls to `vkWaitForFences`, the `vkResetFences` call should be **moved**. It's best to simply call it right before actually using the fence:
587+
588+
```c++
589+
void drawFrame() {
590+
...
591+
592+
vkResetFences(device, 1, &inFlightFences[currentFrame]);
593+
594+
if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
595+
throw std::runtime_error("failed to submit draw command buffer!");
596+
}
597+
598+
...
599+
}
600+
```
601+
586602
We've now implemented all the needed synchronization to ensure that there are no more than two frames of work enqueued and that these frames are not accidentally using the same image. Note that it is fine for other parts of the code, like the final cleanup, to rely on more rough synchronization like `vkDeviceWaitIdle`. You should decide on which approach to use based on performance requirements.
587603

588604
To learn more about synchronization through examples, have a look at [this extensive overview](https://github.com/KhronosGroup/Vulkan-Docs/wiki/Synchronization-Examples#swapchain-image-acquire-and-present) by Khronos.

en/03_Drawing_a_triangle/04_Swap_chain_recreation.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,6 @@ If the swap chain turns out to be out of date when attempting to acquire an
168168
image, then it is no longer possible to present to it. Therefore we should
169169
immediately recreate the swap chain and try again in the next `drawFrame` call.
170170

171-
However, if we abort drawing at this point then the fence will never have
172-
been submitted with `vkQueueSubmit` and it'll be in an unexpected state when we
173-
try to wait for it later on. We could recreate the fences as part of swap chain
174-
recreation, but it's easier to move the `vkResetFences` call:
175-
176-
```c++
177-
vkResetFences(device, 1, &inFlightFences[currentFrame]);
178-
179-
if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
180-
throw std::runtime_error("failed to submit draw command buffer!");
181-
}
182-
```
183-
184171
You could also decide to do that if the swap chain is suboptimal, but I've
185172
chosen to proceed anyway in that case because we've already acquired an image.
186173
Both `VK_SUCCESS` and `VK_SUBOPTIMAL_KHR` are considered "success" return codes.

0 commit comments

Comments
 (0)