Changeset c1c2021 in opengl-game


Ignore:
Timestamp:
Sep 23, 2019, 12:32:48 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
502bd0b
Parents:
a0c5f28
Message:

In vulkangame, add code to create a logical device

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    ra0c5f28 rc1c2021  
    22
    33#include <iostream>
     4#include <set>
    45
    56#include "consts.hpp"
     
    9899   createVulkanSurface();
    99100   pickPhysicalDevice(deviceExtensions);
     101   createLogicalDevice(validationLayers, deviceExtensions);
    100102}
    101103
     
    142144      renderScene();
    143145   }
     146
     147   vkDeviceWaitIdle(device);
    144148}
    145149
     
    153157
    154158void VulkanGame::cleanup() {
     159   cleanupSwapChain();
     160
     161   vkDestroyDevice(device, nullptr);
     162   vkDestroySurfaceKHR(instance, surface, nullptr);
     163
    155164   if (ENABLE_VALIDATION_LAYERS) {
    156165      VulkanUtils::destroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
    157166   }
     167
    158168   vkDestroyInstance(instance, nullptr);
    159169
     
    164174   gui->shutdown();
    165175   delete gui;
     176}
     177
     178void VulkanGame::cleanupSwapChain() {
    166179}
    167180
     
    295308   return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy;
    296309}
     310
     311void VulkanGame::createLogicalDevice(
     312      const vector<const char*> validationLayers,
     313      const vector<const char*>& deviceExtensions) {
     314   QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
     315
     316   vector<VkDeviceQueueCreateInfo> queueCreateInfos;
     317   set<uint32_t> uniqueQueueFamilies = { indices.graphicsFamily.value(), indices.presentFamily.value() };
     318
     319   float queuePriority = 1.0f;
     320   for (uint32_t queueFamily : uniqueQueueFamilies) {
     321      VkDeviceQueueCreateInfo queueCreateInfo = {};
     322      queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
     323      queueCreateInfo.queueFamilyIndex = queueFamily;
     324      queueCreateInfo.queueCount = 1;
     325      queueCreateInfo.pQueuePriorities = &queuePriority;
     326
     327      queueCreateInfos.push_back(queueCreateInfo);
     328   }
     329
     330   VkPhysicalDeviceFeatures deviceFeatures = {};
     331   deviceFeatures.samplerAnisotropy = VK_TRUE;
     332
     333   VkDeviceCreateInfo createInfo = {};
     334   createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
     335   createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
     336   createInfo.pQueueCreateInfos = queueCreateInfos.data();
     337
     338   createInfo.pEnabledFeatures = &deviceFeatures;
     339
     340   createInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size());
     341   createInfo.ppEnabledExtensionNames = deviceExtensions.data();
     342
     343   // These fields are ignored  by up-to-date Vulkan implementations,
     344   // but it's a good idea to set them for backwards compatibility
     345   if (ENABLE_VALIDATION_LAYERS) {
     346      createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
     347      createInfo.ppEnabledLayerNames = validationLayers.data();
     348   } else {
     349      createInfo.enabledLayerCount = 0;
     350   }
     351
     352   if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) {
     353      throw runtime_error("failed to create logical device!");
     354   }
     355
     356   vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
     357   vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
     358}
  • vulkan-game.hpp

    ra0c5f28 rc1c2021  
    2828      VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
    2929      VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
     30      VkDevice device;
     31
     32      VkQueue graphicsQueue;
     33      VkQueue presentQueue;
    3034
    3135      bool initWindow(int width, int height, unsigned char guiFlags);
     
    4246      void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
    4347      bool isDeviceSuitable(VkPhysicalDevice device, const vector<const char*>& deviceExtensions);
     48      void createLogicalDevice(
     49         const vector<const char*> validationLayers,
     50         const vector<const char*>& deviceExtensions);
     51      void cleanupSwapChain();
    4452
    4553      static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
  • vulkan-ref.cpp

    ra0c5f28 rc1c2021  
    164164
    165165      VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
    166 /*** END OF REFACTORED CODE ***/
    167166      VkDevice device;
    168167
    169168      VkQueue graphicsQueue;
    170169      VkQueue presentQueue;
     170/*** END OF REFACTORED CODE ***/
    171171
    172172      VkSwapchainKHR swapChain;
     
    316316         createSurface();
    317317         pickPhysicalDevice();
     318         createLogicalDevice();
    318319/*** END OF REFACTORED CODE ***/
    319          createLogicalDevice();
    320320         createSwapChain();
    321321         createImageViews();
     
    561561         return requiredExtensions.empty();
    562562      }
    563 /*** END OF REFACTORED CODE ***/
    564563
    565564      void createLogicalDevice() {
     
    609608         vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
    610609      }
     610/*** END OF REFACTORED CODE ***/
    611611
    612612      void createSwapChain() {
     
    16741674      }
    16751675
     1676/*** START OF REFACTORED CODE ***/
    16761677      void mainLoop() {
    16771678         // TODO: Create some generic event-handling functions in game-gui-*
     
    16901691                  quit = true;
    16911692               }
     1693/*** END OF REFACTORED CODE ***/
    16921694               if (e.type == SDL_WINDOWEVENT) {
    16931695                  if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
     
    16961698                  }
    16971699               }
     1700/*** START OF REFACTORED CODE ***/
    16981701            }
    16991702
     
    17051708         vkDeviceWaitIdle(device);
    17061709      }
     1710/*** END OF REFACTORED CODE ***/
    17071711
    17081712      void drawFrame() {
     
    18531857      }
    18541858
     1859/*** START OF REFACTORED CODE ***/
    18551860      void cleanup() {
    18561861         cleanupSwapChain();
     1862/*** END OF REFACTORED CODE ***/
    18571863
    18581864         vkDestroySampler(device, textureSampler, nullptr);
     
    18801886
    18811887         vkDestroyCommandPool(device, commandPool, nullptr);
     1888/*** START OF REFACTORED CODE ***/
    18821889         vkDestroyDevice(device, nullptr);
    18831890         vkDestroySurfaceKHR(instance, surface, nullptr);
    18841891
    1885 /*** START OF REFACTORED CODE ***/
    18861892         if (enableValidationLayers) {
    18871893            DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
     
    19191925         gui->shutdown();
    19201926         delete gui;
     1927      }
     1928
     1929      void cleanupSwapChain() {
    19211930/*** END OF REFACTORED CODE ***/
    1922       }
    1923 
    1924       void cleanupSwapChain() {
    19251931         vkDestroyImageView(device, depthImageView, nullptr);
    19261932         vkDestroyImage(device, depthImage, nullptr);
     
    19481954            vkFreeMemory(device, uniformBuffersMemory[i], nullptr);
    19491955         }
    1950       }
     1956/*** START OF REFACTORED CODE ***/
     1957      }
     1958/*** END OF REFACTORED CODE ***/
    19511959
    19521960      void cleanupPipeline(GraphicsPipelineInfo& pipeline) {
Note: See TracChangeset for help on using the changeset viewer.