Changeset 502bd0b in opengl-game


Ignore:
Timestamp:
Sep 23, 2019, 2:02:47 AM (5 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

Files:
5 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}
  • vulkan-game.hpp

    rc1c2021 r502bd0b  
    3333      VkQueue presentQueue;
    3434
     35      VkSwapchainKHR swapChain;
     36      vector<VkImage> swapChainImages;
     37      VkFormat swapChainImageFormat;
     38      VkExtent2D swapChainExtent;
     39
    3540      bool initWindow(int width, int height, unsigned char guiFlags);
    3641      void initVulkan();
     
    4954         const vector<const char*> validationLayers,
    5055         const vector<const char*>& deviceExtensions);
     56      void createSwapChain();
    5157      void cleanupSwapChain();
    5258
  • vulkan-ref.cpp

    rc1c2021 r502bd0b  
    168168      VkQueue graphicsQueue;
    169169      VkQueue presentQueue;
    170 /*** END OF REFACTORED CODE ***/
    171170
    172171      VkSwapchainKHR swapChain;
     
    174173      VkFormat swapChainImageFormat;
    175174      VkExtent2D swapChainExtent;
     175/*** END OF REFACTORED CODE ***/
    176176      vector<VkImageView> swapChainImageViews;
    177177      vector<VkFramebuffer> swapChainFramebuffers;
     
    608608         vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
    609609      }
    610 /*** END OF REFACTORED CODE ***/
    611610
    612611      void createSwapChain() {
     
    663662      }
    664663
    665 /*** START OF REFACTORED CODE ***/
    666664      SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device) {
    667665         SwapChainSupportDetails details;
     
    19481946         }
    19491947
     1948/*** START OF REFACTORED CODE ***/
    19501949         vkDestroySwapchainKHR(device, swapChain, nullptr);
     1950/*** END OF REFACTORED CODE ***/
    19511951
    19521952         for (size_t i = 0; i < swapChainImages.size(); i++) {
  • vulkan-utils.cpp

    rc1c2021 r502bd0b  
    125125   return details;
    126126}
     127
     128VkSurfaceFormatKHR VulkanUtils::chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats) {
     129   for (const auto& availableFormat : availableFormats) {
     130      if (availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
     131         return availableFormat;
     132      }
     133   }
     134
     135   return availableFormats[0];
     136}
     137
     138VkPresentModeKHR VulkanUtils::chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes) {
     139   VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR;
     140
     141   for (const auto& availablePresentMode : availablePresentModes) {
     142      if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
     143         return availablePresentMode;
     144      }
     145      else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
     146         bestMode = availablePresentMode;
     147      }
     148   }
     149
     150   return bestMode;
     151}
     152
     153VkExtent2D VulkanUtils::chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, int width, int height) {
     154   if (capabilities.currentExtent.width != numeric_limits<uint32_t>::max()) {
     155      return capabilities.currentExtent;
     156   } else {
     157      VkExtent2D actualExtent = {
     158         static_cast<uint32_t>(width),
     159         static_cast<uint32_t>(height)
     160      };
     161
     162      actualExtent.width = std::max(capabilities.minImageExtent.width, std::min(capabilities.maxImageExtent.width, actualExtent.width));
     163      actualExtent.height = std::max(capabilities.minImageExtent.height, std::min(capabilities.maxImageExtent.height, actualExtent.height));
     164
     165      return actualExtent;
     166   }
     167}
  • vulkan-utils.hpp

    rc1c2021 r502bd0b  
    4040      static bool checkDeviceExtensionSupport(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
    4141      static SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface);
     42      static VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats);
     43      static VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes);
     44      static VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, int width, int height);
    4245};
    4346
Note: See TracChangeset for help on using the changeset viewer.