Backends: Vulkan: fix swapchain synchronization issue (Fix for #8795 — alternative to #8842) #8923
+92
−75
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
TL;DR
Fixes a vulkan swapchain synchronization bug reported in #8795 by properly distinguishing
FrameIndex(used for command buffers, fences, and acquire semaphores) fromImageIndex(used for the current frame’s render-complete semaphore).Removes the unnecessary extra semaphore per image and waits fences before acquiring the next image.
Supersedes PR #8842 and ensures consistent behavior across all example vulkan backends (GLFW, SDL2, SDL3, Win32).
Description
This pull request provides a fix for issue #8795.
It supersedes PR #8842, which attempted to solve the problem but did not fully resolve it.
The reason why the problem doesn't occur every time is mentioned in the Vulkan guide on swapchain semaphore reuse
So, if the device (and the installed SDK with the validation layer) is running below the 1.4.313 the issue isn't reported.
Changes included
ImGui_ImplVulkanH_Window:VkCommandBuffer,VkFence,ImageAcquiredSemaphore, etc.vkAcquireNextImageKHR, used to retrieve the current frame’sRenderCompleteSemaphoreand as pImageIndices for the VkQueuePresentKHR.Why this works
As discussed in #8795, the Vulkan guide on swapchain semaphore reuse explains the problem well.
The
RenderCompleteSemaphoreand the 'VkQueuePresentKHR' depends on the acquired image index, while other objects should follow the frame ring index.Testing
Possible improvements
FrameRenderandFramePresentare nearly identical across all backends.Possible breaking changes
ImGui_ImplVulkanH_Windowand referenceSemaphoreCountorSemaphoreIndexmay need to update their code.SemaphoreIndexwas renamed for clarity, but it could also be kept unchanged if backward compatibility is preferred.