From c38dcef6f61cf97bb7df852c280245146cb87782 Mon Sep 17 00:00:00 2001 From: assiduous Date: Thu, 26 Mar 2020 18:23:21 -0700 Subject: Enabled VSync on Vulkan (fixed https://github.com/DiligentGraphics/DiligentCore/issues/57) --- Graphics/GraphicsEngineVulkan/include/SwapChainVkImpl.hpp | 1 + Graphics/GraphicsEngineVulkan/src/SwapChainVkImpl.cpp | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'Graphics/GraphicsEngineVulkan') diff --git a/Graphics/GraphicsEngineVulkan/include/SwapChainVkImpl.hpp b/Graphics/GraphicsEngineVulkan/include/SwapChainVkImpl.hpp index e58c5f71..289dbe55 100644 --- a/Graphics/GraphicsEngineVulkan/include/SwapChainVkImpl.hpp +++ b/Graphics/GraphicsEngineVulkan/include/SwapChainVkImpl.hpp @@ -106,6 +106,7 @@ private: Uint32 m_SemaphoreIndex = 0; uint32_t m_BackBufferIndex = 0; bool m_IsMinimized = false; + bool m_VSyncEnabled = true; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineVulkan/src/SwapChainVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/SwapChainVkImpl.cpp index 5ed8c222..acee6307 100644 --- a/Graphics/GraphicsEngineVulkan/src/SwapChainVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/SwapChainVkImpl.cpp @@ -226,12 +226,13 @@ void SwapChainVkImpl::CreateVulkanSwapChain() m_SwapChainDesc.Width = swapchainExtent.width; m_SwapChainDesc.Height = swapchainExtent.height; - // Mailbox is the lowest latency non-tearing presentation mode - VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR; + // Mailbox is the lowest latency non-tearing presentation mode. + VkPresentModeKHR swapchainPresentMode = m_VSyncEnabled ? VK_PRESENT_MODE_FIFO_KHR : VK_PRESENT_MODE_MAILBOX_KHR; bool PresentModeSupported = std::find(presentModes.begin(), presentModes.end(), swapchainPresentMode) != presentModes.end(); if (!PresentModeSupported) { + VERIFY(swapchainPresentMode != VK_PRESENT_MODE_FIFO_KHR, "The FIFO present mode is guaranteed by the spec to be supported"); swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR; // The FIFO present mode is guaranteed by the spec to be supported VERIFY(std::find(presentModes.begin(), presentModes.end(), swapchainPresentMode) != presentModes.end(), "FIFO present mode must be supported"); @@ -542,6 +543,9 @@ IMPLEMENT_QUERY_INTERFACE(SwapChainVkImpl, IID_SwapChainVk, TSwapChainBase) void SwapChainVkImpl::Present(Uint32 SyncInterval) { + if (SyncInterval != 0 && SyncInterval != 1) + LOG_WARNING_MESSAGE_ONCE("Vulkan only supports 0 and 1 present intervals"); + auto pDeviceContext = m_wpDeviceContext.Lock(); if (!pDeviceContext) { @@ -612,9 +616,12 @@ void SwapChainVkImpl::Present(Uint32 SyncInterval) if (m_SemaphoreIndex >= m_SwapChainDesc.BufferCount) m_SemaphoreIndex = 0; - auto res = AcquireNextImage(pImmediateCtxVk); + bool EnableVSync = SyncInterval != 0; + + auto res = (m_VSyncEnabled == EnableVSync) ? AcquireNextImage(pImmediateCtxVk) : VK_ERROR_OUT_OF_DATE_KHR; if (res == VK_SUBOPTIMAL_KHR || res == VK_ERROR_OUT_OF_DATE_KHR) { + m_VSyncEnabled = EnableVSync; RecreateVulkanSwapchain(pImmediateCtxVk); m_SemaphoreIndex = m_SwapChainDesc.BufferCount - 1; // To start with 0 index when acquire next image -- cgit v1.2.3