examples: Prevent invalid semaphore reuse across frames #1009
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.
Fixes #917
Closes #941
Closes #979
Caution
The example teaches bad practices when it comes to performance. I have a change in flight that switches them to triple-buffering which inevitably replaces the second commit. We may prefer going that route instead.
examples: Have a unique semaphore per swapchain image
This resolves
VUID-vkQueueSubmit-pSignalSemaphores-00067as explained at https://docs.vulkan.org/guide/latest/swapchain_semaphore_reuse.html.A unique semaphore should be used per swapchain image, because the presentation engine may still reference and use it before the image index it was used with is returned out of
acquire_next_image()again.Alternatives include waiting for the
Fencefrom presentation inVkSwapchainPresentFenceInfoKHR, added by theVK_EXT_swapchain_maintenance1/VK_KHR_swapchain_maintenance1extension but this is less performant than just using a unique semaphore per swapchain image.examples: Wait earlier for command-buffer fence to prevent semaphore reuse
This resolves
VUID-vkAcquireNextImageKHR-semaphore-01779whereacquire_next_image()is asked to signal a semaphore that may still be waited on by the currently-running submit.Since the next submit is reusing the existing command buffer and resources as well it must inevitably wait on a fence from the previous frame/submit, pulling out that fence to wait on it is the most practical approach despite teaching bad/slow practices.
Note that this fence was also used to "reuse" the setup command buffer in the
texturesample; this was instead generalized to use another command buffer without waiting for and signaling any fence.examples: Implement triple-buffering
This removes a per-frame stall and demonstrates how users should utilize Vulkan to keep the GPU fed with rendering work, as long as that work (and corresponding presentation requests) complete before we're getting 3 frames ahead.
Note that this does not implement proper frame throttling, and may render lots of discarded frames.
It is an alternative solution to solving
VUID-vkAcquireNextImageKHR-semaphore-01779by also using a uniqueSemaphorethat we know is no longer waited on via previously waiting on the relevantFence; although similar in nature to the previous solution.