diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index 43ab4881..3b67faeb 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -761,6 +761,10 @@ VulkanExampleBase::VulkanExampleBase(bool enableValidation) { enableValidation = true; } + if (__argv[i] == std::string("-vsync")) + { + enableVSync = true; + } } #elif defined(__ANDROID__) // Vulkan library is loaded dynamically on Android @@ -1753,5 +1757,5 @@ void VulkanExampleBase::initSwapchain() void VulkanExampleBase::setupSwapChain() { - swapChain.create(setupCmdBuffer, &width, &height); + swapChain.create(setupCmdBuffer, &width, &height, enableVSync); } diff --git a/base/vulkanexamplebase.h b/base/vulkanexamplebase.h index 61d288e2..52a337c5 100644 --- a/base/vulkanexamplebase.h +++ b/base/vulkanexamplebase.h @@ -57,6 +57,8 @@ private: bool enableValidation = false; // Set to true when the debug marker extension is detected bool enableDebugMarkers = false; + // Set tot true if v-sync will be forced for the swapchain + bool enableVSync = false; // fps timer (one second interval) float fpsTimer = 0.0f; // Create application wide Vulkan instance diff --git a/base/vulkanswapchain.hpp b/base/vulkanswapchain.hpp index a1eb2982..126a3fc1 100644 --- a/base/vulkanswapchain.hpp +++ b/base/vulkanswapchain.hpp @@ -235,7 +235,7 @@ public: } // Create the swap chain and get images with given width and height - void create(VkCommandBuffer cmdBuffer, uint32_t *width, uint32_t *height, VkBool32 vsync = VK_FALSE) + void create(VkCommandBuffer cmdBuffer, uint32_t *width, uint32_t *height, bool vsync = false) { VkResult err; VkSwapchainKHR oldSwapchain = swapChain; @@ -275,22 +275,26 @@ public: // Select a present mode for the swapchain + // The VK_PRESENT_MODE_FIFO_KHR mode must always be present as per spec // This mode waits for the vertical blank ("v-sync") VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR; // If v-sync is not requested, try to find a mailbox mode if present // It's the lowest latency non-tearing present mode available - for (size_t i = 0; i < presentModeCount; i++) + if (!vsync) { - if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) + for (size_t i = 0; i < presentModeCount; i++) { - swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR; - break; - } - if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) && (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) - { - swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; + if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) + { + swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR; + break; + } + if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) && (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) + { + swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; + } } }