Changeset 90a424f in opengl-game


Ignore:
Timestamp:
Sep 16, 2019, 7:04:53 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
fe5c3ba
Parents:
c6fec84
Message:

In vulkangame, add code to create a Vulkan surface and pick a physical device

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    rc6fec84 r90a424f  
    9393   createVulkanInstance(validationLayers);
    9494   setupDebugMessenger();
     95   createVulkanSurface();
     96   pickPhysicalDevice();
    9597}
    9698
     
    230232   return VK_FALSE;
    231233}
     234
     235void VulkanGame::createVulkanSurface() {
     236   if (gui->createVulkanSurface(instance, &surface) == RTWO_ERROR) {
     237      throw runtime_error("failed to create window surface!");
     238   }
     239}
     240
     241void VulkanGame::pickPhysicalDevice() {
     242   uint32_t deviceCount = 0;
     243   vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
     244
     245   if (deviceCount == 0) {
     246      throw runtime_error("failed to find GPUs with Vulkan support!");
     247   }
     248
     249   vector<VkPhysicalDevice> devices(deviceCount);
     250   vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
     251
     252   cout << endl << "Graphics cards:" << endl;
     253   for (const VkPhysicalDevice& device : devices) {
     254      if (isDeviceSuitable(device)) {
     255         physicalDevice = device;
     256         break;
     257      }
     258   }
     259   cout << endl;
     260
     261   if (physicalDevice == VK_NULL_HANDLE) {
     262      throw runtime_error("failed to find a suitable GPU!");
     263   }
     264}
     265
     266bool VulkanGame::isDeviceSuitable(VkPhysicalDevice device) {
     267   const vector<const char*> deviceExtensions = {
     268      VK_KHR_SWAPCHAIN_EXTENSION_NAME
     269   };
     270
     271   VkPhysicalDeviceProperties deviceProperties;
     272   vkGetPhysicalDeviceProperties(device, &deviceProperties);
     273
     274   cout << "Device: " << deviceProperties.deviceName << endl;
     275
     276   QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(device, surface);
     277   bool extensionsSupported = VulkanUtils::checkDeviceExtensionSupport(device, deviceExtensions);
     278   bool swapChainAdequate = false;
     279
     280   if (extensionsSupported) {
     281      SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(device, surface);
     282      swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
     283   }
     284
     285   VkPhysicalDeviceFeatures supportedFeatures;
     286   vkGetPhysicalDeviceFeatures(device, &supportedFeatures);
     287
     288   return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy;
     289}
  • vulkan-game.hpp

    rc6fec84 r90a424f  
    2626      VkInstance instance;
    2727      VkDebugUtilsMessengerEXT debugMessenger;
     28      VkSurfaceKHR surface;
     29      VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
    2830
    2931      bool initWindow(int width, int height, unsigned char guiFlags);
     
    3537      void setupDebugMessenger();
    3638      void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
     39      void createVulkanSurface();
     40      void pickPhysicalDevice();
     41      bool isDeviceSuitable(VkPhysicalDevice device);
    3742
    3843      static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
  • vulkan-ref.cpp

    rc6fec84 r90a424f  
    3232const int MAX_FRAMES_IN_FLIGHT = 2;
    3333
     34/*** START OF REFACTORED CODE ***/
    3435#ifdef NDEBUG
    3536   const bool enableValidationLayers = false;
     
    6061    vector<VkPresentModeKHR> presentModes;
    6162};
     63/*** END OF REFACTORED CODE ***/
    6264
    6365struct Vertex {
     
    159161      VkInstance instance;
    160162      VkDebugUtilsMessengerEXT debugMessenger;
     163      VkSurfaceKHR surface;
     164
     165      VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
    161166/*** END OF REFACTORED CODE ***/
    162       VkSurfaceKHR surface;
    163 
    164       VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
    165167      VkDevice device;
    166168
     
    312314         createInstance();
    313315         setupDebugMessenger();
    314 /*** END OF REFACTORED CODE ***/
    315316         createSurface();
    316317         pickPhysicalDevice();
     318/*** END OF REFACTORED CODE ***/
    317319         createLogicalDevice();
    318320         createSwapChain();
     
    491493         createInfo.pfnUserCallback = debugCallback;
    492494      }
    493       /*** END OF REFACTORED CODE ***/
    494495
    495496      void createSurface() {
     
    560561         return requiredExtensions.empty();
    561562      }
     563/*** END OF REFACTORED CODE ***/
    562564
    563565      void createLogicalDevice() {
     
    661663      }
    662664
     665/*** START OF REFACTORED CODE ***/
    663666      SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device) {
    664667         SwapChainSupportDetails details;
     
    684687         return details;
    685688      }
     689/*** END OF REFACTORED CODE ***/
    686690
    687691      VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats) {
     
    10341038      }
    10351039
     1040/*** START OF REFACTORED CODE ***/
    10361041      QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
    10371042         QueueFamilyIndices indices;
     
    10651070         return indices;
    10661071      }
     1072/*** END OF REFACTORED CODE ***/
    10671073
    10681074      void createDepthResources() {
  • vulkan-utils.cpp

    rc6fec84 r90a424f  
    11#include "vulkan-utils.hpp"
     2
     3#include <set>
     4#include <string>
    25
    36bool VulkanUtils::checkValidationLayerSupport(const vector<const char*> &validationLayers) {
     
    4851   }
    4952}
     53
     54QueueFamilyIndices VulkanUtils::findQueueFamilies(VkPhysicalDevice device, VkSurfaceKHR surface) {
     55   QueueFamilyIndices indices;
     56
     57   uint32_t queueFamilyCount = 0;
     58   vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
     59
     60   vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
     61   vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
     62
     63   int i = 0;
     64   for (const auto& queueFamily : queueFamilies) {
     65      if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
     66         indices.graphicsFamily = i;
     67      }
     68
     69      VkBool32 presentSupport = false;
     70      vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
     71
     72      if (queueFamily.queueCount > 0 && presentSupport) {
     73         indices.presentFamily = i;
     74      }
     75
     76      if (indices.isComplete()) {
     77         break;
     78      }
     79
     80      i++;
     81   }
     82
     83   return indices;
     84}
     85
     86bool VulkanUtils::checkDeviceExtensionSupport(VkPhysicalDevice device, const vector<const char*>& deviceExtensions) {
     87   uint32_t extensionCount;
     88   vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
     89
     90   vector<VkExtensionProperties> availableExtensions(extensionCount);
     91   vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, availableExtensions.data());
     92
     93   set<string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
     94
     95   for (const auto& extension : availableExtensions) {
     96      requiredExtensions.erase(extension.extensionName);
     97   }
     98
     99   return requiredExtensions.empty();
     100}
     101
     102SwapChainSupportDetails VulkanUtils::querySwapChainSupport(VkPhysicalDevice device, VkSurfaceKHR surface) {
     103   SwapChainSupportDetails details;
     104
     105   vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
     106
     107   uint32_t formatCount;
     108   vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr);
     109
     110   if (formatCount != 0) {
     111      details.formats.resize(formatCount);
     112      vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data());
     113   }
     114
     115   uint32_t presentModeCount;
     116   vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr);
     117
     118   if (presentModeCount != 0) {
     119      details.presentModes.resize(presentModeCount);
     120      vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes.data());
     121   }
     122
     123   return details;
     124}
  • vulkan-utils.hpp

    rc6fec84 r90a424f  
    22#define _VULKAN_UTILS_H
    33
     4#include <optional>
    45#include <vector>
    56
     
    78
    89using namespace std;
     10
     11struct QueueFamilyIndices {
     12   optional<uint32_t> graphicsFamily;
     13   optional<uint32_t> presentFamily;
     14
     15   bool isComplete() {
     16      return graphicsFamily.has_value() && presentFamily.has_value();
     17   }
     18};
     19
     20struct SwapChainSupportDetails {
     21   VkSurfaceCapabilitiesKHR capabilities;
     22   vector<VkSurfaceFormatKHR> formats;
     23   vector<VkPresentModeKHR> presentModes;
     24};
    925
    1026class VulkanUtils {
     
    2036            VkDebugUtilsMessengerEXT debugMessenger,
    2137            const VkAllocationCallbacks* pAllocator);
     38
     39      static QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device, VkSurfaceKHR surface);
     40      static bool checkDeviceExtensionSupport(VkPhysicalDevice device, const vector<const char*>& deviceExtensions);
     41      static SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device, VkSurfaceKHR surface);
    2242};
    2343
Note: See TracChangeset for help on using the changeset viewer.