From 42d62d423ae602d57f85c0b9daddecdd00b4eb1f Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 1 Mar 2024 15:38:18 -0800 Subject: [PATCH] limit max frames in flight --- src/Refresh_Driver_Vulkan.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Refresh_Driver_Vulkan.c b/src/Refresh_Driver_Vulkan.c index c899c80..cc4d589 100644 --- a/src/Refresh_Driver_Vulkan.c +++ b/src/Refresh_Driver_Vulkan.c @@ -80,6 +80,7 @@ typedef struct VulkanExtensions #define UBO_BUFFER_SIZE 16777216 /* 16MB */ #define MAX_UBO_SECTION_SIZE 4096 /* 4KB */ #define DESCRIPTOR_POOL_STARTING_SIZE 128 +#define MAX_FRAMES_IN_FLIGHT 3 #define WINDOW_DATA "Refresh_VulkanWindowData" #define IDENTITY_SWIZZLE \ @@ -875,6 +876,8 @@ typedef struct VulkanSwapchainData /* Synchronization primitives */ VkSemaphore imageAvailableSemaphore; VkSemaphore renderFinishedSemaphore; + + uint32_t submissionsInFlight; } VulkanSwapchainData; typedef struct WindowData @@ -4639,6 +4642,7 @@ static uint8_t VULKAN_INTERNAL_CreateSwapchain( uint32_t i; swapchainData = SDL_malloc(sizeof(VulkanSwapchainData)); + swapchainData->submissionsInFlight = 0; /* Each swapchain must have its own surface. */ @@ -9616,6 +9620,12 @@ static Refresh_Texture* VULKAN_AcquireSwapchainTexture( swapchainData = windowData->swapchainData; + /* Drop frames if too many submissions in flight */ + if (swapchainData->submissionsInFlight >= MAX_FRAMES_IN_FLIGHT) + { + return NULL; + } + /* Window is claimed but swapchain is invalid! */ if (swapchainData == NULL) { @@ -10057,6 +10067,11 @@ static void VULKAN_INTERNAL_CleanCommandBuffer( /* Reset presentation data */ + for (i = 0; i < commandBuffer->presentDataCount; i += 1) + { + commandBuffer->presentDatas[i].windowData->swapchainData->submissionsInFlight -= 1; + } + commandBuffer->presentDataCount = 0; commandBuffer->waitSemaphoreCount = 0; commandBuffer->signalSemaphoreCount = 0; @@ -10255,6 +10270,10 @@ static void VULKAN_Submit( presentData->windowData ); } + else + { + presentData->windowData->swapchainData->submissionsInFlight += 1; + } } /* Check if we can perform any cleanups */