Vulkan: Fix vsync behavior when throttler is temp disabled

This commit is contained in:
Stenzek 2016-10-02 22:09:19 +10:00
parent b193282830
commit 5e29508b8f
4 changed files with 32 additions and 17 deletions
Source/Core/VideoBackends/Vulkan

@ -890,7 +890,7 @@ void Renderer::CheckForSurfaceChange()
s_new_surface_handle); s_new_surface_handle);
if (surface != VK_NULL_HANDLE) if (surface != VK_NULL_HANDLE)
{ {
m_swap_chain = SwapChain::Create(s_new_surface_handle, surface); m_swap_chain = SwapChain::Create(s_new_surface_handle, surface, g_ActiveConfig.IsVSync());
if (!m_swap_chain) if (!m_swap_chain)
PanicAlert("Failed to create swap chain."); PanicAlert("Failed to create swap chain.");
} }
@ -917,7 +917,6 @@ void Renderer::CheckForSurfaceChange()
void Renderer::CheckForConfigChanges() void Renderer::CheckForConfigChanges()
{ {
// Compare g_Config to g_ActiveConfig to determine what has changed before copying. // Compare g_Config to g_ActiveConfig to determine what has changed before copying.
bool vsync_changed = (g_Config.bVSync != g_ActiveConfig.bVSync);
bool msaa_changed = (g_Config.iMultisamples != g_ActiveConfig.iMultisamples); bool msaa_changed = (g_Config.iMultisamples != g_ActiveConfig.iMultisamples);
bool ssaa_changed = (g_Config.bSSAA != g_ActiveConfig.bSSAA); bool ssaa_changed = (g_Config.bSSAA != g_ActiveConfig.bSSAA);
bool anisotropy_changed = (g_Config.iMaxAnisotropy != g_ActiveConfig.iMaxAnisotropy); bool anisotropy_changed = (g_Config.iMaxAnisotropy != g_ActiveConfig.iMaxAnisotropy);
@ -963,8 +962,11 @@ void Renderer::CheckForConfigChanges()
} }
// For vsync, we need to change the present mode, which means recreating the swap chain. // For vsync, we need to change the present mode, which means recreating the swap chain.
if (vsync_changed) if (m_swap_chain && g_ActiveConfig.IsVSync() != m_swap_chain->IsVSyncEnabled())
ResizeSwapChain(); {
g_command_buffer_mgr->WaitForGPUIdle();
m_swap_chain->SetVSync(g_ActiveConfig.IsVSync());
}
// Wipe sampler cache if force texture filtering or anisotropy changes. // Wipe sampler cache if force texture filtering or anisotropy changes.
if (anisotropy_changed || force_texture_filtering_changed) if (anisotropy_changed || force_texture_filtering_changed)

@ -24,8 +24,8 @@
namespace Vulkan namespace Vulkan
{ {
SwapChain::SwapChain(void* native_handle, VkSurfaceKHR surface) SwapChain::SwapChain(void* native_handle, VkSurfaceKHR surface, bool vsync)
: m_native_handle(native_handle), m_surface(surface) : m_native_handle(native_handle), m_surface(surface), m_vsync_enabled(vsync)
{ {
} }
@ -127,9 +127,10 @@ VkSurfaceKHR SwapChain::CreateVulkanSurface(VkInstance instance, void* hwnd)
#endif #endif
} }
std::unique_ptr<SwapChain> SwapChain::Create(void* native_handle, VkSurfaceKHR surface) std::unique_ptr<SwapChain> SwapChain::Create(void* native_handle, VkSurfaceKHR surface, bool vsync)
{ {
std::unique_ptr<SwapChain> swap_chain = std::make_unique<SwapChain>(native_handle, surface); std::unique_ptr<SwapChain> swap_chain =
std::make_unique<SwapChain>(native_handle, surface, vsync);
if (!swap_chain->CreateSwapChain() || !swap_chain->CreateRenderPass() || if (!swap_chain->CreateSwapChain() || !swap_chain->CreateRenderPass() ||
!swap_chain->SetupSwapChainImages()) !swap_chain->SetupSwapChainImages())
@ -198,7 +199,7 @@ bool SwapChain::SelectPresentMode()
}; };
// If vsync is enabled, prefer VK_PRESENT_MODE_FIFO_KHR. // If vsync is enabled, prefer VK_PRESENT_MODE_FIFO_KHR.
if (g_ActiveConfig.IsVSync()) if (m_vsync_enabled)
{ {
// Try for relaxed vsync first, since it's likely the VI won't line up with // Try for relaxed vsync first, since it's likely the VI won't line up with
// the refresh rate of the system exactly, so tearing once is better than // the refresh rate of the system exactly, so tearing once is better than
@ -456,11 +457,8 @@ VkResult SwapChain::AcquireNextImage(VkSemaphore available_semaphore)
bool SwapChain::ResizeSwapChain() bool SwapChain::ResizeSwapChain()
{ {
if (!CreateSwapChain())
return false;
DestroySwapChainImages(); DestroySwapChainImages();
if (!SetupSwapChainImages()) if (!CreateSwapChain() || !SetupSwapChainImages())
{ {
PanicAlert("Failed to re-configure swap chain images, this is fatal (for now)"); PanicAlert("Failed to re-configure swap chain images, this is fatal (for now)");
return false; return false;
@ -469,6 +467,16 @@ bool SwapChain::ResizeSwapChain()
return true; return true;
} }
bool SwapChain::SetVSync(bool enabled)
{
if (m_vsync_enabled == enabled)
return true;
// Resizing recreates the swap chain with the new present mode.
m_vsync_enabled = enabled;
return ResizeSwapChain();
}
bool SwapChain::RecreateSurface(void* native_handle) bool SwapChain::RecreateSurface(void* native_handle)
{ {
// Destroy the old swap chain, images, and surface. // Destroy the old swap chain, images, and surface.

@ -19,18 +19,19 @@ class ObjectCache;
class SwapChain class SwapChain
{ {
public: public:
SwapChain(void* native_handle, VkSurfaceKHR surface); SwapChain(void* native_handle, VkSurfaceKHR surface, bool vsync);
~SwapChain(); ~SwapChain();
// Creates a vulkan-renderable surface for the specified window handle. // Creates a vulkan-renderable surface for the specified window handle.
static VkSurfaceKHR CreateVulkanSurface(VkInstance instance, void* hwnd); static VkSurfaceKHR CreateVulkanSurface(VkInstance instance, void* hwnd);
// Create a new swap chain from a pre-existing surface. // Create a new swap chain from a pre-existing surface.
static std::unique_ptr<SwapChain> Create(void* native_handle, VkSurfaceKHR surface); static std::unique_ptr<SwapChain> Create(void* native_handle, VkSurfaceKHR surface, bool vsync);
void* GetNativeHandle() const { return m_native_handle; } void* GetNativeHandle() const { return m_native_handle; }
VkSurfaceKHR GetSurface() const { return m_surface; } VkSurfaceKHR GetSurface() const { return m_surface; }
VkSurfaceFormatKHR GetSurfaceFormat() const { return m_surface_format; } VkSurfaceFormatKHR GetSurfaceFormat() const { return m_surface_format; }
bool IsVSyncEnabled() const { return m_vsync_enabled; }
VkSwapchainKHR GetSwapChain() const { return m_swap_chain; } VkSwapchainKHR GetSwapChain() const { return m_swap_chain; }
VkRenderPass GetRenderPass() const { return m_render_pass; } VkRenderPass GetRenderPass() const { return m_render_pass; }
u32 GetWidth() const { return m_width; } u32 GetWidth() const { return m_width; }
@ -54,6 +55,9 @@ public:
bool RecreateSurface(void* native_handle); bool RecreateSurface(void* native_handle);
bool ResizeSwapChain(); bool ResizeSwapChain();
// Change vsync enabled state. This may fail as it causes a swapchain recreation.
bool SetVSync(bool enabled);
private: private:
bool SelectSurfaceFormat(); bool SelectSurfaceFormat();
bool SelectPresentMode(); bool SelectPresentMode();
@ -76,10 +80,11 @@ private:
VkFramebuffer framebuffer; VkFramebuffer framebuffer;
}; };
void* m_native_handle = nullptr; void* m_native_handle;
VkSurfaceKHR m_surface = VK_NULL_HANDLE; VkSurfaceKHR m_surface = VK_NULL_HANDLE;
VkSurfaceFormatKHR m_surface_format = {}; VkSurfaceFormatKHR m_surface_format = {};
VkPresentModeKHR m_present_mode = VK_PRESENT_MODE_RANGE_SIZE_KHR; VkPresentModeKHR m_present_mode = VK_PRESENT_MODE_RANGE_SIZE_KHR;
bool m_vsync_enabled;
VkSwapchainKHR m_swap_chain = VK_NULL_HANDLE; VkSwapchainKHR m_swap_chain = VK_NULL_HANDLE;
std::vector<SwapChainImage> m_swap_chain_images; std::vector<SwapChainImage> m_swap_chain_images;

@ -169,7 +169,7 @@ bool VideoBackend::Initialize(void* window_handle)
std::unique_ptr<SwapChain> swap_chain; std::unique_ptr<SwapChain> swap_chain;
if (surface != VK_NULL_HANDLE) if (surface != VK_NULL_HANDLE)
{ {
swap_chain = SwapChain::Create(window_handle, surface); swap_chain = SwapChain::Create(window_handle, surface, g_Config.IsVSync());
if (!swap_chain) if (!swap_chain)
{ {
PanicAlert("Failed to create Vulkan swap chain."); PanicAlert("Failed to create Vulkan swap chain.");