Skip to content

Commit 4001ea0

Browse files
CreateSwapChain function fixes
- 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. - Rearranged code to ensure consistency in the number of swapchains created to match capability of device. - We check the number of swapchains supported and allocate memory to match this number.
1 parent 45d57c9 commit 4001ea0

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

ZEngine/ZEngine/Hardwares/VulkanDevice.cpp

Lines changed: 12 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 = {};
@@ -1099,13 +1089,23 @@ namespace ZEngine::Hardwares
10991089
uint32_t image_count = 0;
11001090
ZENGINE_VALIDATE_ASSERT(vkGetSwapchainImagesKHR(LogicalDevice, SwapchainHandle, &image_count, nullptr) == VK_SUCCESS, "Failed to get Images count from Swapchain")
11011091

1102-
if (image_count < SwapchainImageCount)
1092+
if (image_count != SwapchainImageCount)
11031093
{
11041094
ZENGINE_CORE_WARN("Max Swapchain image count supported is {}, but requested {}", image_count, SwapchainImageCount);
11051095
SwapchainImageCount = image_count;
11061096
ZENGINE_CORE_WARN("Swapchain image count has changed from {} to {}", SwapchainImageCount, image_count);
11071097
}
11081098

1099+
if (SwapchainImageViews.capacity() <= 0)
1100+
{
1101+
SwapchainImageViews.init(Arena, SwapchainImageCount, SwapchainImageCount);
1102+
}
1103+
1104+
if (SwapchainFramebuffers.capacity() <= 0)
1105+
{
1106+
SwapchainFramebuffers.init(Arena, SwapchainImageCount, SwapchainImageCount);
1107+
}
1108+
11091109
Array<VkImage> SwapchainImages = {};
11101110
SwapchainImages.init(scratch.Arena, SwapchainImageCount, SwapchainImageCount);
11111111
ZENGINE_VALIDATE_ASSERT(vkGetSwapchainImagesKHR(LogicalDevice, SwapchainHandle, &SwapchainImageCount, SwapchainImages.data()) == VK_SUCCESS, "Failed to get VkImages from Swapchain")

0 commit comments

Comments
 (0)