| 939 | } |
| 940 | |
| 941 | void HelloVK::createSwapChain() { |
| 942 | SwapChainSupportDetails swapChainSupport = |
| 943 | querySwapChainSupport(physicalDevice); |
| 944 | |
| 945 | auto chooseSwapSurfaceFormat = |
| 946 | [](const std::vector<VkSurfaceFormatKHR>& availableFormats) { |
| 947 | for (const auto& availableFormat : availableFormats) { |
| 948 | if (availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB && |
| 949 | availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { |
| 950 | return availableFormat; |
| 951 | } |
| 952 | } |
| 953 | return availableFormats[0]; |
| 954 | }; |
| 955 | |
| 956 | VkSurfaceFormatKHR surfaceFormat = |
| 957 | chooseSwapSurfaceFormat(swapChainSupport.formats); |
| 958 | |
| 959 | // Please check |
| 960 | // https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkPresentModeKHR.html |
| 961 | // for a discourse on different present modes. |
| 962 | // |
| 963 | // VK_PRESENT_MODE_FIFO_KHR = Hard Vsync |
| 964 | // This is always supported on Android phones |
| 965 | VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR; |
| 966 | |
| 967 | uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1; |
| 968 | if (swapChainSupport.capabilities.maxImageCount > 0 && |
| 969 | imageCount > swapChainSupport.capabilities.maxImageCount) { |
| 970 | imageCount = swapChainSupport.capabilities.maxImageCount; |
| 971 | } |
| 972 | pretransformFlag = swapChainSupport.capabilities.currentTransform; |
| 973 | |
| 974 | VkSwapchainCreateInfoKHR createInfo{}; |
| 975 | createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; |
| 976 | createInfo.surface = surface; |
| 977 | createInfo.minImageCount = imageCount; |
| 978 | createInfo.imageFormat = surfaceFormat.format; |
| 979 | createInfo.imageColorSpace = surfaceFormat.colorSpace; |
| 980 | createInfo.imageExtent = displaySizeIdentity; |
| 981 | createInfo.imageArrayLayers = 1; |
| 982 | createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 983 | createInfo.preTransform = pretransformFlag; |
| 984 | |
| 985 | QueueFamilyIndices indices = findQueueFamilies(physicalDevice); |
| 986 | uint32_t queueFamilyIndices[] = {indices.graphicsFamily.value(), |
| 987 | indices.presentFamily.value()}; |
| 988 | |
| 989 | if (indices.graphicsFamily != indices.presentFamily) { |
| 990 | createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT; |
| 991 | createInfo.queueFamilyIndexCount = 2; |
| 992 | createInfo.pQueueFamilyIndices = queueFamilyIndices; |
| 993 | } else { |
| 994 | createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 995 | createInfo.queueFamilyIndexCount = 0; |
| 996 | createInfo.pQueueFamilyIndices = nullptr; |
| 997 | } |
| 998 | createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR; |