Skip to content

Commit 2638537

Browse files
HTuanPhongslouken
authored andcommitted
Fix vulkan gpu resize lag on windows
1 parent 134b477 commit 2638537

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

src/gpu/vulkan/SDL_gpu_vulkan.c

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3164,7 +3164,7 @@ static void VULKAN_INTERNAL_DestroySampler(
31643164
SDL_free(vulkanSampler);
31653165
}
31663166

3167-
static void VULKAN_INTERNAL_DestroySwapchain(
3167+
static void VULKAN_INTERNAL_DestroySwapchainImage(
31683168
VulkanRenderer *renderer,
31693169
WindowData *windowData)
31703170
{
@@ -3190,22 +3190,6 @@ static void VULKAN_INTERNAL_DestroySwapchain(
31903190
SDL_free(windowData->textureContainers);
31913191
windowData->textureContainers = NULL;
31923192

3193-
if (windowData->swapchain) {
3194-
renderer->vkDestroySwapchainKHR(
3195-
renderer->logicalDevice,
3196-
windowData->swapchain,
3197-
NULL);
3198-
windowData->swapchain = VK_NULL_HANDLE;
3199-
}
3200-
3201-
if (windowData->surface) {
3202-
renderer->vkDestroySurfaceKHR(
3203-
renderer->instance,
3204-
windowData->surface,
3205-
NULL);
3206-
windowData->surface = VK_NULL_HANDLE;
3207-
}
3208-
32093193
for (i = 0; i < MAX_FRAMES_IN_FLIGHT; i += 1) {
32103194
if (windowData->imageAvailableSemaphore[i]) {
32113195
renderer->vkDestroySemaphore(
@@ -3230,6 +3214,33 @@ static void VULKAN_INTERNAL_DestroySwapchain(
32303214
windowData->imageCount = 0;
32313215
}
32323216

3217+
static void VULKAN_INTERNAL_DestroySwapchain(
3218+
VulkanRenderer *renderer,
3219+
WindowData *windowData)
3220+
{
3221+
if (windowData == NULL) {
3222+
return;
3223+
}
3224+
3225+
VULKAN_INTERNAL_DestroySwapchainImage(renderer, windowData);
3226+
3227+
if (windowData->swapchain) {
3228+
renderer->vkDestroySwapchainKHR(
3229+
renderer->logicalDevice,
3230+
windowData->swapchain,
3231+
NULL);
3232+
windowData->swapchain = VK_NULL_HANDLE;
3233+
}
3234+
3235+
if (windowData->surface) {
3236+
renderer->vkDestroySurfaceKHR(
3237+
renderer->instance,
3238+
windowData->surface,
3239+
NULL);
3240+
windowData->surface = VK_NULL_HANDLE;
3241+
}
3242+
}
3243+
32333244
static void VULKAN_INTERNAL_DestroyGraphicsPipelineResourceLayout(
32343245
VulkanRenderer *renderer,
32353246
VulkanGraphicsPipelineResourceLayout *resourceLayout)
@@ -4484,17 +4495,20 @@ static Uint32 VULKAN_INTERNAL_CreateSwapchain(
44844495

44854496
windowData->frameCounter = 0;
44864497

4487-
SDL_VideoDevice *_this = SDL_GetVideoDevice();
4488-
SDL_assert(_this && _this->Vulkan_CreateSurface);
4498+
// We dont have to create surface again on recreate swapchain
4499+
if (windowData->surface == VK_NULL_HANDLE) {
4500+
SDL_VideoDevice *_this = SDL_GetVideoDevice();
4501+
SDL_assert(_this && _this->Vulkan_CreateSurface);
44894502

4490-
// Each swapchain must have its own surface.
4491-
if (!_this->Vulkan_CreateSurface(
4492-
_this,
4493-
windowData->window,
4494-
renderer->instance,
4495-
NULL, // FIXME: VAllocationCallbacks
4496-
&windowData->surface)) {
4497-
return false;
4503+
// Each swapchain must have its own surface.
4504+
if (!_this->Vulkan_CreateSurface(
4505+
_this,
4506+
windowData->window,
4507+
renderer->instance,
4508+
NULL, // FIXME: VAllocationCallbacks
4509+
&windowData->surface)) {
4510+
return false;
4511+
}
44984512
}
44994513
SDL_assert(windowData->surface);
45004514

@@ -4663,14 +4677,17 @@ static Uint32 VULKAN_INTERNAL_CreateSwapchain(
46634677
swapchainCreateInfo.compositeAlpha = compositeAlphaFlag;
46644678
swapchainCreateInfo.presentMode = SDLToVK_PresentMode[windowData->presentMode];
46654679
swapchainCreateInfo.clipped = VK_TRUE;
4666-
swapchainCreateInfo.oldSwapchain = VK_NULL_HANDLE;
4667-
4680+
swapchainCreateInfo.oldSwapchain = windowData->swapchain;
46684681
vulkanResult = renderer->vkCreateSwapchainKHR(
46694682
renderer->logicalDevice,
46704683
&swapchainCreateInfo,
46714684
NULL,
46724685
&windowData->swapchain);
46734686

4687+
if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
4688+
renderer->vkDestroySwapchainKHR(renderer->logicalDevice, swapchainCreateInfo.oldSwapchain, NULL);
4689+
}
4690+
46744691
if (swapchainSupportDetails.formatsLength > 0) {
46754692
SDL_free(swapchainSupportDetails.formats);
46764693
}
@@ -4684,6 +4701,7 @@ static Uint32 VULKAN_INTERNAL_CreateSwapchain(
46844701
windowData->surface,
46854702
NULL);
46864703
windowData->surface = VK_NULL_HANDLE;
4704+
windowData->swapchain = VK_NULL_HANDLE;
46874705
CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateSwapchainKHR, false);
46884706
}
46894707

@@ -9890,7 +9908,7 @@ static Uint32 VULKAN_INTERNAL_RecreateSwapchain(
98909908
}
98919909
}
98929910

9893-
VULKAN_INTERNAL_DestroySwapchain(renderer, windowData);
9911+
VULKAN_INTERNAL_DestroySwapchainImage(renderer, windowData);
98949912
return VULKAN_INTERNAL_CreateSwapchain(renderer, windowData);
98959913
}
98969914

0 commit comments

Comments
 (0)