Changeset c1d9b2a in opengl-game


Ignore:
Timestamp:
Sep 15, 2019, 5:44:41 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
df2cc24
Parents:
92cbc6a
Message:

In vulkangame, create a Vulkan instance and enable the Vulkan debug extension, and move the code that clears the screen to black into the main loop.

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • VulkanGame.vcxproj

    r92cbc6a rc1d9b2a  
    139139    <ClCompile Include="StackWalker.cpp" />
    140140    <ClCompile Include="vulkan-game.cpp" />
     141    <ClCompile Include="vulkan-utils.cpp" />
    141142  </ItemGroup>
    142143  <ItemGroup>
     
    150151    <ClInclude Include="StackWalker.h" />
    151152    <ClInclude Include="vulkan-game.hpp" />
     153    <ClInclude Include="vulkan-utils.hpp" />
    152154  </ItemGroup>
    153155  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  • vulkan-game.cpp

    r92cbc6a rc1d9b2a  
    55#include "consts.hpp"
    66#include "logger.hpp"
     7
     8#include "vulkan-utils.hpp"
    79
    810using namespace std;
     
    4446   }
    4547
    46    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
    47    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    48 
    49    SDL_RenderClear(renderer);
    50 
    51    SDL_RenderPresent(renderer);
    52 
    5348   initVulkan();
    5449   mainLoop();
     
    8176   cout << "Actual window size: (" << gui->getWindowWidth() << ", " << gui->getWindowHeight() << ")" << endl;
    8277
     78   renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
     79   if (renderer == nullptr) {
     80      cout << "Renderer could not be created!" << endl;
     81      cout << gui->getError() << endl;
     82      return RTWO_ERROR;
     83   }
     84
    8385   return RTWO_SUCCESS;
    8486}
    8587
    8688void VulkanGame::initVulkan() {
     89   const vector<const char*> validationLayers = {
     90      "VK_LAYER_KHRONOS_validation"
     91   };
     92
     93   createVulkanInstance(validationLayers);
     94   setupDebugMessenger();
    8795}
    8896
     
    9098   UIEvent e;
    9199   bool quit = false;
     100
     101   SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    92102
    93103   while (!quit) {
     
    123133         }
    124134      }
     135
     136      SDL_RenderClear(renderer);
     137      SDL_RenderPresent(renderer);
    125138   }
    126139}
    127140
    128141void VulkanGame::cleanup() {
     142   if (ENABLE_VALIDATION_LAYERS) {
     143      VulkanUtils::destroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
     144   }
     145   vkDestroyInstance(instance, nullptr);
     146
     147   SDL_DestroyRenderer(renderer);
     148   renderer = nullptr;
     149
    129150   gui->destroyWindow();
    130151   gui->shutdown();
    131152   delete gui;
    132153}
     154
     155void VulkanGame::createVulkanInstance(const vector<const char*> &validationLayers) {
     156   if (ENABLE_VALIDATION_LAYERS && !VulkanUtils::checkValidationLayerSupport(validationLayers)) {
     157      throw runtime_error("validation layers requested, but not available!");
     158   }
     159
     160   VkApplicationInfo appInfo = {};
     161   appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
     162   appInfo.pApplicationName = "Vulkan Game";
     163   appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
     164   appInfo.pEngineName = "No Engine";
     165   appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
     166   appInfo.apiVersion = VK_API_VERSION_1_0;
     167
     168   VkInstanceCreateInfo createInfo = {};
     169   createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
     170   createInfo.pApplicationInfo = &appInfo;
     171
     172   vector<const char*> extensions = gui->getRequiredExtensions();
     173   if (ENABLE_VALIDATION_LAYERS) {
     174      extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
     175   }
     176
     177   createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
     178   createInfo.ppEnabledExtensionNames = extensions.data();
     179
     180   cout << endl << "Extensions:" << endl;
     181   for (const char* extensionName : extensions) {
     182      cout << extensionName << endl;
     183   }
     184   cout << endl;
     185
     186   VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
     187   if (ENABLE_VALIDATION_LAYERS) {
     188      createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
     189      createInfo.ppEnabledLayerNames = validationLayers.data();
     190
     191      populateDebugMessengerCreateInfo(debugCreateInfo);
     192      createInfo.pNext = &debugCreateInfo;
     193   } else {
     194      createInfo.enabledLayerCount = 0;
     195
     196      createInfo.pNext = nullptr;
     197   }
     198
     199   if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
     200      throw runtime_error("failed to create instance!");
     201   }
     202}
     203
     204void VulkanGame::setupDebugMessenger() {
     205   if (!ENABLE_VALIDATION_LAYERS) return;
     206
     207   VkDebugUtilsMessengerCreateInfoEXT createInfo;
     208   populateDebugMessengerCreateInfo(createInfo);
     209
     210   if (VulkanUtils::createDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
     211      throw runtime_error("failed to set up debug messenger!");
     212   }
     213}
     214
     215void VulkanGame::populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
     216   createInfo = {};
     217   createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
     218   createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
     219   createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
     220   createInfo.pfnUserCallback = debugCallback;
     221}
     222
     223VKAPI_ATTR VkBool32 VKAPI_CALL VulkanGame::debugCallback(
     224      VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
     225      VkDebugUtilsMessageTypeFlagsEXT messageType,
     226      const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
     227      void* pUserData) {
     228   cerr << "validation layer: " << pCallbackData->pMessage << endl;
     229
     230   return VK_FALSE;
     231}
  • vulkan-game.hpp

    r92cbc6a rc1d9b2a  
    2222      SDL_version sdlVersion;
    2323      SDL_Window* window;
     24      SDL_Renderer* renderer;
     25
     26      VkInstance instance;
     27      VkDebugUtilsMessengerEXT debugMessenger;
    2428
    2529      bool initWindow(int width, int height, unsigned char guiFlags);
     
    2731      void mainLoop();
    2832      void cleanup();
     33
     34      void createVulkanInstance(const vector<const char*> &validationLayers);
     35      void setupDebugMessenger();
     36      void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
     37
     38      static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
     39            VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
     40            VkDebugUtilsMessageTypeFlagsEXT messageType,
     41            const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
     42            void* pUserData);
    2943};
    3044
Note: See TracChangeset for help on using the changeset viewer.