Skip to content

Commit 44d348d

Browse files
CreateSwapChain function fixes (#487)
- Guard against device capabilities with maxImageCount == 0, which is defined to mean that there is no limit to the number of images, the only limitation is memory availability. - We check the number of swapchains supported and allocate memory to match this number. - Use of two separate scratch Arenas in order to successfully update the SwapchainFramebuffers and SwapchainImageViews with the actual number of supported Swapchains from the current device Co-authored-by: Mathew Benson <mathew@benson.co.ke>
1 parent 5f6a704 commit 44d348d

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

ZEngine/ZEngine/Hardwares/VulkanDevice.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,22 +1064,12 @@ namespace ZEngine::Hardwares
10641064
SwapchainImageHeight = capabilities.currentExtent.height;
10651065
}
10661066

1067-
auto min_image_count = std::clamp(capabilities.minImageCount, capabilities.minImageCount + 1, capabilities.maxImageCount);
1067+
auto min_image_count = std::clamp(capabilities.minImageCount, capabilities.minImageCount, capabilities.maxImageCount == 0 ? capabilities.minImageCount + 1 : capabilities.maxImageCount);
10681068
VkSwapchainCreateInfoKHR swapchain_create_info = {
10691069
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, .pNext = nullptr, .surface = Surface, .minImageCount = min_image_count, .imageFormat = SurfaceFormat.format, .imageColorSpace = SurfaceFormat.colorSpace, .imageExtent = VkExtent2D{.width = SwapchainImageWidth, .height = SwapchainImageHeight},
10701070
.imageArrayLayers = 1, .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, .preTransform = capabilities.currentTransform, .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, .presentMode = PresentMode, .clipped = VK_TRUE
10711071
};
10721072

1073-
if (SwapchainImageViews.capacity() <= 0)
1074-
{
1075-
SwapchainImageViews.init(Arena, SwapchainImageCount, SwapchainImageCount);
1076-
}
1077-
1078-
if (SwapchainFramebuffers.capacity() <= 0)
1079-
{
1080-
SwapchainFramebuffers.init(Arena, SwapchainImageCount, SwapchainImageCount);
1081-
}
1082-
10831073
auto scratch = ZGetScratch(Arena);
10841074

10851075
Array<uint32_t> family_indice = {};
@@ -1096,16 +1086,30 @@ namespace ZEngine::Hardwares
10961086

10971087
ZENGINE_VALIDATE_ASSERT(vkCreateSwapchainKHR(LogicalDevice, &swapchain_create_info, nullptr, &SwapchainHandle) == VK_SUCCESS, "Failed to create Swapchain")
10981088

1089+
ZReleaseScratch(scratch);
1090+
10991091
uint32_t image_count = 0;
11001092
ZENGINE_VALIDATE_ASSERT(vkGetSwapchainImagesKHR(LogicalDevice, SwapchainHandle, &image_count, nullptr) == VK_SUCCESS, "Failed to get Images count from Swapchain")
11011093

1102-
if (image_count < SwapchainImageCount)
1094+
if (image_count != SwapchainImageCount)
11031095
{
11041096
ZENGINE_CORE_WARN("Max Swapchain image count supported is {}, but requested {}", image_count, SwapchainImageCount);
11051097
SwapchainImageCount = image_count;
11061098
ZENGINE_CORE_WARN("Swapchain image count has changed from {} to {}", SwapchainImageCount, image_count);
11071099
}
11081100

1101+
if (SwapchainImageViews.capacity() <= 0)
1102+
{
1103+
SwapchainImageViews.init(Arena, SwapchainImageCount, SwapchainImageCount);
1104+
}
1105+
1106+
if (SwapchainFramebuffers.capacity() <= 0)
1107+
{
1108+
SwapchainFramebuffers.init(Arena, SwapchainImageCount, SwapchainImageCount);
1109+
}
1110+
1111+
scratch = ZGetScratch(Arena);
1112+
11091113
Array<VkImage> SwapchainImages = {};
11101114
SwapchainImages.init(scratch.Arena, SwapchainImageCount, SwapchainImageCount);
11111115
ZENGINE_VALIDATE_ASSERT(vkGetSwapchainImagesKHR(LogicalDevice, SwapchainHandle, &SwapchainImageCount, SwapchainImages.data()) == VK_SUCCESS, "Failed to get VkImages from Swapchain")

0 commit comments

Comments
 (0)