192    VkSurfaceCapabilitiesKHR surfCaps;
 
  196    uint32_t presentModeCount;
 
  198    assert(presentModeCount > 0);
 
  200    std::vector<VkPresentModeKHR> presentModes(presentModeCount);
 
  203    VkExtent2D swapchainExtent = {};
 
  205    if (surfCaps.currentExtent.width == (uint32_t)-1)
 
  209        swapchainExtent.width = *width;
 
  210        swapchainExtent.height = *height;
 
  215        swapchainExtent = surfCaps.currentExtent;
 
  216        *width = surfCaps.currentExtent.width;
 
  217        *height = surfCaps.currentExtent.height;
 
  225    VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
 
  231        for (
size_t i = 0; i < presentModeCount; i++)
 
  233            if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR)
 
  235                swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
 
  238            if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) && (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR))
 
  240                swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
 
  246    uint32_t desiredNumberOfSwapchainImages = surfCaps.minImageCount + 1;
 
  247    if ((surfCaps.maxImageCount > 0) && (desiredNumberOfSwapchainImages > surfCaps.maxImageCount))
 
  249        desiredNumberOfSwapchainImages = surfCaps.maxImageCount;
 
  253    VkSurfaceTransformFlagsKHR preTransform;
 
  254    if (surfCaps.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR)
 
  257        preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
 
  261        preTransform = surfCaps.currentTransform;
 
  265    VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
 
  267    std::vector<VkCompositeAlphaFlagBitsKHR> compositeAlphaFlags = {
 
  268        VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
 
  269        VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
 
  270        VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
 
  271        VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
 
  273    for (
auto& compositeAlphaFlag : compositeAlphaFlags) {
 
  274        if (surfCaps.supportedCompositeAlpha & compositeAlphaFlag) {
 
  275            compositeAlpha = compositeAlphaFlag;
 
  280    VkSwapchainCreateInfoKHR swapchainCI = {};
 
  281    swapchainCI.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
 
  282    swapchainCI.pNext = NULL;
 
  284    swapchainCI.minImageCount = desiredNumberOfSwapchainImages;
 
  287    swapchainCI.imageExtent = { swapchainExtent.width, swapchainExtent.height };
 
  288    swapchainCI.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
 
  289    swapchainCI.preTransform = (VkSurfaceTransformFlagBitsKHR)preTransform;
 
  290    swapchainCI.imageArrayLayers = 1;
 
  291    swapchainCI.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
 
  292    swapchainCI.queueFamilyIndexCount = 0;
 
  293    swapchainCI.pQueueFamilyIndices = NULL;
 
  294    swapchainCI.presentMode = swapchainPresentMode;
 
  295    swapchainCI.oldSwapchain = oldSwapchain;
 
  297    swapchainCI.clipped = VK_TRUE;
 
  298    swapchainCI.compositeAlpha = compositeAlpha;
 
  301    if (surfCaps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
 
  302        swapchainCI.imageUsage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
 
  306    if (surfCaps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
 
  307        swapchainCI.imageUsage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
 
  314    if (oldSwapchain != VK_NULL_HANDLE) 
 
  332        VkImageViewCreateInfo colorAttachmentView = {};
 
  333        colorAttachmentView.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
 
  334        colorAttachmentView.pNext = NULL;
 
  336        colorAttachmentView.components = {
 
  337            VK_COMPONENT_SWIZZLE_R,
 
  338            VK_COMPONENT_SWIZZLE_G,
 
  339            VK_COMPONENT_SWIZZLE_B,
 
  340            VK_COMPONENT_SWIZZLE_A
 
  342        colorAttachmentView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
 
  343        colorAttachmentView.subresourceRange.baseMipLevel = 0;
 
  344        colorAttachmentView.subresourceRange.levelCount = 1;
 
  345        colorAttachmentView.subresourceRange.baseArrayLayer = 0;
 
  346        colorAttachmentView.subresourceRange.layerCount = 1;
 
  347        colorAttachmentView.viewType = VK_IMAGE_VIEW_TYPE_2D;
 
  348        colorAttachmentView.flags = 0;
 
  352        colorAttachmentView.image = 
buffers[i].image;