diff options
Diffstat (limited to 'src/platform')
| -rw-r--r-- | src/platform/opengl/opengl_device.cpp | 1 | ||||
| -rw-r--r-- | src/platform/vulkan/vulkan_common.h | 2 | ||||
| -rw-r--r-- | src/platform/vulkan/vulkan_swapchain.cpp | 14 |
3 files changed, 16 insertions, 1 deletions
diff --git a/src/platform/opengl/opengl_device.cpp b/src/platform/opengl/opengl_device.cpp index 8516a3c..82fdae1 100644 --- a/src/platform/opengl/opengl_device.cpp +++ b/src/platform/opengl/opengl_device.cpp @@ -230,6 +230,7 @@ namespace Donut::RHI } auto resize(int w, int h) -> void override { m_width = w; m_height = h; } auto wait_idle() -> void override { glFinish(); } + auto set_vsync(bool enabled) -> void override { glfwSwapInterval(enabled ? 1 : 0); } auto create_buffer(BufferType t, size_t size, const void* data) -> Ref<Buffer> override { return create_ref<GLBuffer>(t, size, data); } diff --git a/src/platform/vulkan/vulkan_common.h b/src/platform/vulkan/vulkan_common.h index 0faa107..5ae257e 100644 --- a/src/platform/vulkan/vulkan_common.h +++ b/src/platform/vulkan/vulkan_common.h @@ -267,6 +267,7 @@ namespace Donut::RHI auto shutdown() -> void override; auto resize(int width, int height) -> void override { m_framebuffer_resized = true; m_width = width; m_height = height; } auto wait_idle() -> void override { if (m_device) vkDeviceWaitIdle(m_device); } + auto set_vsync(bool enabled) -> void override { m_vsync = enabled; m_framebuffer_resized = true; } auto create_buffer(BufferType type, size_t size, const void* data) -> Ref<Buffer> override; auto create_texture(int w, int h, Format format, Filter filter, const void* data) -> Ref<Texture> override; @@ -305,6 +306,7 @@ namespace Donut::RHI GLFWwindow* m_window = nullptr; int m_width = 0, m_height = 0; bool m_framebuffer_resized = false; + bool m_vsync = true; std::string m_gpu_name; VkInstance m_instance = VK_NULL_HANDLE; diff --git a/src/platform/vulkan/vulkan_swapchain.cpp b/src/platform/vulkan/vulkan_swapchain.cpp index fe5bd38..5bf2f60 100644 --- a/src/platform/vulkan/vulkan_swapchain.cpp +++ b/src/platform/vulkan/vulkan_swapchain.cpp @@ -117,7 +117,19 @@ namespace Donut::RHI sci.imageExtent = m_extent; sci.imageArrayLayers = 1; sci.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; sci.preTransform = caps.currentTransform; sci.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; - sci.presentMode = VK_PRESENT_MODE_FIFO_KHR; sci.clipped = VK_TRUE; + // FIFO is always available (vsync). With vsync off, prefer IMMEDIATE + // (uncapped, may tear) then MAILBOX, if the surface supports them. + VkPresentModeKHR present_mode = VK_PRESENT_MODE_FIFO_KHR; + if (!m_vsync) + { + uint32_t pmc = 0; vkGetPhysicalDeviceSurfacePresentModesKHR(m_physical, m_surface, &pmc, nullptr); + std::vector<VkPresentModeKHR> modes(pmc); + vkGetPhysicalDeviceSurfacePresentModesKHR(m_physical, m_surface, &pmc, modes.data()); + auto has = [&](VkPresentModeKHR m){ return std::find(modes.begin(), modes.end(), m) != modes.end(); }; + if (has(VK_PRESENT_MODE_IMMEDIATE_KHR)) present_mode = VK_PRESENT_MODE_IMMEDIATE_KHR; + else if (has(VK_PRESENT_MODE_MAILBOX_KHR)) present_mode = VK_PRESENT_MODE_MAILBOX_KHR; + } + sci.presentMode = present_mode; sci.clipped = VK_TRUE; uint32_t fam[2] = { m_graphics_family, m_present_family }; if (m_graphics_family != m_present_family) { sci.imageSharingMode = VK_SHARING_MODE_CONCURRENT; sci.queueFamilyIndexCount = 2; sci.pQueueFamilyIndices = fam; } |
