Changeset 502bd0b in opengl-game for vulkan-game.cpp


Ignore:
Timestamp:
Sep 23, 2019, 2:02:47 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
91c89f7
Parents:
c1c2021
Message:

In vulkangame, add code to create a swap chain

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    rc1c2021 r502bd0b  
    100100   pickPhysicalDevice(deviceExtensions);
    101101   createLogicalDevice(validationLayers, deviceExtensions);
     102   createSwapChain();
    102103}
    103104
     
    177178
    178179void VulkanGame::cleanupSwapChain() {
     180   vkDestroySwapchainKHR(device, swapChain, nullptr);
    179181}
    180182
     
    357359   vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
    358360}
     361
     362void VulkanGame::createSwapChain() {
     363   SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(physicalDevice, surface);
     364
     365   VkSurfaceFormatKHR surfaceFormat = VulkanUtils::chooseSwapSurfaceFormat(swapChainSupport.formats);
     366   VkPresentModeKHR presentMode = VulkanUtils::chooseSwapPresentMode(swapChainSupport.presentModes);
     367   VkExtent2D extent = VulkanUtils::chooseSwapExtent(swapChainSupport.capabilities, gui->getWindowWidth(), gui->getWindowHeight());
     368
     369   uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
     370   if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {
     371      imageCount = swapChainSupport.capabilities.maxImageCount;
     372   }
     373
     374   VkSwapchainCreateInfoKHR createInfo = {};
     375   createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
     376   createInfo.surface = surface;
     377   createInfo.minImageCount = imageCount;
     378   createInfo.imageFormat = surfaceFormat.format;
     379   createInfo.imageColorSpace = surfaceFormat.colorSpace;
     380   createInfo.imageExtent = extent;
     381   createInfo.imageArrayLayers = 1;
     382   createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
     383
     384   QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
     385   uint32_t queueFamilyIndices[] = { indices.graphicsFamily.value(), indices.presentFamily.value() };
     386
     387   if (indices.graphicsFamily != indices.presentFamily) {
     388      createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
     389      createInfo.queueFamilyIndexCount = 2;
     390      createInfo.pQueueFamilyIndices = queueFamilyIndices;
     391   }
     392   else {
     393      createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
     394      createInfo.queueFamilyIndexCount = 0;
     395      createInfo.pQueueFamilyIndices = nullptr;
     396   }
     397
     398   createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
     399   createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
     400   createInfo.presentMode = presentMode;
     401   createInfo.clipped = VK_TRUE;
     402   createInfo.oldSwapchain = VK_NULL_HANDLE;
     403
     404   if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapChain) != VK_SUCCESS) {
     405      throw runtime_error("failed to create swap chain!");
     406   }
     407
     408   vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
     409   swapChainImages.resize(imageCount);
     410   vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());
     411
     412   swapChainImageFormat = surfaceFormat.format;
     413   swapChainExtent = extent;
     414}
Note: See TracChangeset for help on using the changeset viewer.