Changeset 90a424f in opengl-game for vulkan-game.cpp


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

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.