From 0ab4c25343ba3f61cd738f6d85d577f6f12ce855 Mon Sep 17 00:00:00 2001
From: FlightlessMango <flightlessmangoyt@gmail.com>
Date: Mon, 5 Feb 2024 22:47:29 +0100
Subject: [PATCH] present_mode: account for app requested mode

---
 src/hud_elements.h | 10 ++++++++--
 src/vulkan.cpp     | 17 ++++++++++-------
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/src/hud_elements.h b/src/hud_elements.h
index 29035b52c3..61d1de0b14 100644
--- a/src/hud_elements.h
+++ b/src/hud_elements.h
@@ -141,14 +141,20 @@ class HudElements{
             {VK_PRESENT_MODE_FIFO_KHR, "FIFO"},
             {VK_PRESENT_MODE_FIFO_RELAXED_KHR, "FIFO Relaxed"},
             {VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR, "DEMAND"},
-            {VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR, "ONTINUOUS"}
+            {VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR, "CONTINUOUS"}
         };
 
+        VkPresentModeKHR cur_present_mode;
+
         std::string get_present_mode(){
             if (is_vulkan)
-                return presentModeMap[presentModes[params->vsync]];
+                return presentModeMap[cur_present_mode];
+            // TODO: the opengl side is probably not as solid.
+            // But it also might not be possible to figure out if vsync
+            // is on or off unless we specify it.
             else
                 return params->gl_vsync == 0 ? "OFF" : "ON";
+
         }
 };
 
diff --git a/src/vulkan.cpp b/src/vulkan.cpp
index 3de630340c..dd1454552f 100644
--- a/src/vulkan.cpp
+++ b/src/vulkan.cpp
@@ -1516,10 +1516,14 @@ static VkResult overlay_CreateSwapchainKHR(
    createInfo.imageUsage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
 
    struct device_data *device_data = FIND(struct device_data, device);
+   auto params = device_data->instance->params;
 
-   VkPresentModeKHR presentMode = HUDElements.presentModes[device_data->instance->params.vsync];
-   if (device_data->instance->params.vsync < 4)
-      createInfo.presentMode = presentMode;
+   if (device_data->instance->params.vsync < 4) {
+      HUDElements.cur_present_mode = HUDElements.presentModes[params.vsync];
+      createInfo.presentMode = HUDElements.cur_present_mode;
+   } else {
+      HUDElements.cur_present_mode = createInfo.presentMode;
+   }
 
    struct instance_data *instance_data =
       FIND(struct instance_data, device_data->physical_device);
@@ -1533,11 +1537,10 @@ static VkResult overlay_CreateSwapchainKHR(
       VkResult result = fpGetPhysicalDeviceSurfacePresentModesKHR(device_data->physical_device, pCreateInfo->surface, &presentModeCount, presentModes.data());
 
       if (result == VK_SUCCESS) {
-         if (IsPresentModeSupported(HUDElements.presentModes[device_data->instance->params.vsync], presentModes))
-            SPDLOG_DEBUG("Present mode: {}", HUDElements.get_present_mode());
+         if (IsPresentModeSupported(HUDElements.cur_present_mode, presentModes))
+            SPDLOG_DEBUG("Present mode: {}", HUDElements.presentModeMap[HUDElements.cur_present_mode]);
          else {
-            SPDLOG_DEBUG("Present mode is not supported: {}", HUDElements.get_present_mode());
-            device_data->instance->params.vsync = 3;
+            SPDLOG_DEBUG("Present mode is not supported: {}", HUDElements.presentModeMap[HUDElements.cur_present_mode]);
          }
       }
    }