From b060edc61bbcddbf7e9efaac1d055a68e956bd19 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Fri, 1 Mar 2024 00:50:22 +1000 Subject: [PATCH] VulkanDevice: Actually use all the swap chain semaphores --- src/util/vulkan_device.cpp | 9 +++++---- src/util/vulkan_device.h | 2 +- src/util/vulkan_swap_chain.cpp | 3 +++ 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/util/vulkan_device.cpp b/src/util/vulkan_device.cpp index 2a23b2951..ce0d6011e 100644 --- a/src/util/vulkan_device.cpp +++ b/src/util/vulkan_device.cpp @@ -1272,7 +1272,7 @@ void VulkanDevice::SubmitCommandBuffer(VulkanSwapChain* present_swap_chain /* = { DoSubmitCommandBuffer(m_current_frame, present_swap_chain); if (present_swap_chain) - DoPresent(present_swap_chain); + DoPresent(present_swap_chain, false); return; } @@ -1317,7 +1317,7 @@ void VulkanDevice::DoSubmitCommandBuffer(u32 index, VulkanSwapChain* present_swa } } -void VulkanDevice::DoPresent(VulkanSwapChain* present_swap_chain) +void VulkanDevice::DoPresent(VulkanSwapChain* present_swap_chain, bool acquire_next) { const VkPresentInfoKHR present_info = {VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, nullptr, @@ -1344,7 +1344,8 @@ void VulkanDevice::DoPresent(VulkanSwapChain* present_swap_chain) // Grab the next image as soon as possible, that way we spend less time blocked on the next // submission. Don't care if it fails, we'll deal with that at the presentation call site. // Credit to dxvk for the idea. - present_swap_chain->AcquireNextImage(); + if (acquire_next) + present_swap_chain->AcquireNextImage(); } void VulkanDevice::WaitForPresentComplete() @@ -1378,7 +1379,7 @@ void VulkanDevice::PresentThread() DoSubmitCommandBuffer(m_queued_present.command_buffer_index, m_queued_present.swap_chain); if (m_queued_present.swap_chain) - DoPresent(m_queued_present.swap_chain); + DoPresent(m_queued_present.swap_chain, true); m_present_done.store(true, std::memory_order_release); m_present_done_cv.notify_one(); } diff --git a/src/util/vulkan_device.h b/src/util/vulkan_device.h index ef3e00f94..d0f8b8727 100644 --- a/src/util/vulkan_device.h +++ b/src/util/vulkan_device.h @@ -379,7 +379,7 @@ private: void WaitForCommandBufferCompletion(u32 index); void DoSubmitCommandBuffer(u32 index, VulkanSwapChain* present_swap_chain); - void DoPresent(VulkanSwapChain* present_swap_chain); + void DoPresent(VulkanSwapChain* present_swap_chain, bool acquire_next); void WaitForPresentComplete(std::unique_lock& lock); void PresentThread(); void StartPresentThread(); diff --git a/src/util/vulkan_swap_chain.cpp b/src/util/vulkan_swap_chain.cpp index e12ecc873..e3b7835ca 100644 --- a/src/util/vulkan_swap_chain.cpp +++ b/src/util/vulkan_swap_chain.cpp @@ -598,6 +598,9 @@ VkResult VulkanSwapChain::AcquireNextImage() if (!m_swap_chain) return VK_ERROR_SURFACE_LOST_KHR; + // Use a different semaphore for each image. + m_current_semaphore = (m_current_semaphore + 1) % static_cast(m_semaphores.size()); + const VkResult res = vkAcquireNextImageKHR(VulkanDevice::GetInstance().GetVulkanDevice(), m_swap_chain, UINT64_MAX, m_semaphores[m_current_semaphore].available_semaphore, VK_NULL_HANDLE, &m_current_image);