Skip to content

Commit 4bd8019

Browse files
Fix deadlock in resizing from resetting the fence too early
Because acquiring the swapchain image index may cause drawFrame to return early, it was possible to cause the next vkWaitForFences to deadlock. By delaying fence resetting till after acquiring, it prevents the deadlock.
1 parent 6296501 commit 4bd8019

15 files changed

+47
-14
lines changed

code/17_swap_chain_recreation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,6 @@ class HelloTriangleApplication {
671671

672672
void drawFrame() {
673673
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
674-
vkResetFences(device, 1, &inFlightFences[currentFrame]);
675674

676675
uint32_t imageIndex;
677676
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
@@ -683,6 +682,7 @@ class HelloTriangleApplication {
683682
throw std::runtime_error("failed to acquire swap chain image!");
684683
}
685684

685+
vkResetFences(device, 1, &inFlightFences[currentFrame]);
686686

687687
vkResetCommandBuffer(commandBuffers[currentFrame], /*VkCommandBufferResetFlagBits*/ 0);
688688
recordCommandBuffer(commandBuffers[currentFrame], imageIndex);

code/18_vertex_input.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,6 @@ class HelloTriangleApplication {
716716

717717
void drawFrame() {
718718
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
719-
vkResetFences(device, 1, &inFlightFences[currentFrame]);
720719

721720
uint32_t imageIndex;
722721
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
@@ -728,6 +727,7 @@ class HelloTriangleApplication {
728727
throw std::runtime_error("failed to acquire swap chain image!");
729728
}
730729

730+
vkResetFences(device, 1, &inFlightFences[currentFrame]);
731731

732732
vkResetCommandBuffer(commandBuffers[currentFrame], /*VkCommandBufferResetFlagBits*/ 0);
733733
recordCommandBuffer(commandBuffers[currentFrame], imageIndex);

code/19_vertex_buffer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,6 @@ class HelloTriangleApplication {
772772

773773
void drawFrame() {
774774
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
775-
vkResetFences(device, 1, &inFlightFences[currentFrame]);
776775

777776
uint32_t imageIndex;
778777
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
@@ -784,6 +783,7 @@ class HelloTriangleApplication {
784783
throw std::runtime_error("failed to acquire swap chain image!");
785784
}
786785

786+
vkResetFences(device, 1, &inFlightFences[currentFrame]);
787787

788788
vkResetCommandBuffer(commandBuffers[currentFrame], /*VkCommandBufferResetFlagBits*/ 0);
789789
recordCommandBuffer(commandBuffers[currentFrame], imageIndex);

code/20_staging_buffer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,6 @@ class HelloTriangleApplication {
820820

821821
void drawFrame() {
822822
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
823-
vkResetFences(device, 1, &inFlightFences[currentFrame]);
824823

825824
uint32_t imageIndex;
826825
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
@@ -832,6 +831,7 @@ class HelloTriangleApplication {
832831
throw std::runtime_error("failed to acquire swap chain image!");
833832
}
834833

834+
vkResetFences(device, 1, &inFlightFences[currentFrame]);
835835

836836
vkResetCommandBuffer(commandBuffers[currentFrame], /*VkCommandBufferResetFlagBits*/ 0);
837837
recordCommandBuffer(commandBuffers[currentFrame], imageIndex);

code/21_index_buffer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,6 @@ class HelloTriangleApplication {
853853

854854
void drawFrame() {
855855
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
856-
vkResetFences(device, 1, &inFlightFences[currentFrame]);
857856

858857
uint32_t imageIndex;
859858
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
@@ -865,6 +864,7 @@ class HelloTriangleApplication {
865864
throw std::runtime_error("failed to acquire swap chain image!");
866865
}
867866

867+
vkResetFences(device, 1, &inFlightFences[currentFrame]);
868868

869869
vkResetCommandBuffer(commandBuffers[currentFrame], /*VkCommandBufferResetFlagBits*/ 0);
870870
recordCommandBuffer(commandBuffers[currentFrame], imageIndex);

code/22_descriptor_layout.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,6 @@ class HelloTriangleApplication {
922922

923923
void drawFrame() {
924924
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
925-
vkResetFences(device, 1, &inFlightFences[currentFrame]);
926925

927926
uint32_t imageIndex;
928927
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
@@ -936,6 +935,7 @@ class HelloTriangleApplication {
936935

937936
updateUniformBuffer(currentFrame);
938937

938+
vkResetFences(device, 1, &inFlightFences[currentFrame]);
939939

940940
vkResetCommandBuffer(commandBuffers[currentFrame], /*VkCommandBufferResetFlagBits*/ 0);
941941
recordCommandBuffer(commandBuffers[currentFrame], imageIndex);

code/23_descriptor_sets.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,6 @@ class HelloTriangleApplication {
980980

981981
void drawFrame() {
982982
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
983-
vkResetFences(device, 1, &inFlightFences[currentFrame]);
984983

985984
uint32_t imageIndex;
986985
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
@@ -994,6 +993,7 @@ class HelloTriangleApplication {
994993

995994
updateUniformBuffer(currentFrame);
996995

996+
vkResetFences(device, 1, &inFlightFences[currentFrame]);
997997

998998
vkResetCommandBuffer(commandBuffers[currentFrame], /*VkCommandBufferResetFlagBits*/ 0);
999999
recordCommandBuffer(commandBuffers[currentFrame], imageIndex);

code/24_texture_image.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,6 @@ class HelloTriangleApplication {
11351135

11361136
void drawFrame() {
11371137
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
1138-
vkResetFences(device, 1, &inFlightFences[currentFrame]);
11391138

11401139
uint32_t imageIndex;
11411140
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
@@ -1149,6 +1148,7 @@ class HelloTriangleApplication {
11491148

11501149
updateUniformBuffer(currentFrame);
11511150

1151+
vkResetFences(device, 1, &inFlightFences[currentFrame]);
11521152

11531153
vkResetCommandBuffer(commandBuffers[currentFrame], /*VkCommandBufferResetFlagBits*/ 0);
11541154
recordCommandBuffer(commandBuffers[currentFrame], imageIndex);

code/25_sampler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,6 @@ class HelloTriangleApplication {
11731173

11741174
void drawFrame() {
11751175
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
1176-
vkResetFences(device, 1, &inFlightFences[currentFrame]);
11771176

11781177
uint32_t imageIndex;
11791178
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
@@ -1187,6 +1186,7 @@ class HelloTriangleApplication {
11871186

11881187
updateUniformBuffer(currentFrame);
11891188

1189+
vkResetFences(device, 1, &inFlightFences[currentFrame]);
11901190

11911191
vkResetCommandBuffer(commandBuffers[currentFrame], /*VkCommandBufferResetFlagBits*/ 0);
11921192
recordCommandBuffer(commandBuffers[currentFrame], imageIndex);

code/26_texture_mapping.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,6 @@ class HelloTriangleApplication {
12031203

12041204
void drawFrame() {
12051205
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
1206-
vkResetFences(device, 1, &inFlightFences[currentFrame]);
12071206

12081207
uint32_t imageIndex;
12091208
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
@@ -1217,6 +1216,7 @@ class HelloTriangleApplication {
12171216

12181217
updateUniformBuffer(currentFrame);
12191218

1219+
vkResetFences(device, 1, &inFlightFences[currentFrame]);
12201220

12211221
vkResetCommandBuffer(commandBuffers[currentFrame], /*VkCommandBufferResetFlagBits*/ 0);
12221222
recordCommandBuffer(commandBuffers[currentFrame], imageIndex);

0 commit comments

Comments
 (0)