Changeset b6127d2 in opengl-game


Ignore:
Timestamp:
Jul 3, 2019, 2:24:05 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
7dcd925
Parents:
ab65f84
Message:

Enable validation extensions

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • README.txt

    rab65f84 rb6127d2  
    6262Download the vulkan sdk (make sure VULKAN_SDK_PATH in the makefile points to it)
    6363
     64source setup-env.sh into your current shell
     65
    6466make vulkcangame && ./vulkangame
    6567
  • vulkan-game.cpp

    rab65f84 rb6127d2  
    2020using namespace std;
    2121using namespace glm;
     22
     23bool checkValidationLayerSupport();
     24vector<const char*> getRequiredExtensions(SDL_Window* window);
    2225
    2326const int SCREEN_WIDTH = 800;
     
    3437#endif
    3538
     39static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
     40      VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
     41      VkDebugUtilsMessageTypeFlagsEXT messageType,
     42      const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
     43      void* pUserData) {
     44   cerr << "validation layer: " << pCallbackData->pMessage << endl;
     45
     46   return VK_FALSE;
     47}
     48
     49VkResult CreateDebugUtilsMessengerEXT(VkInstance instance,
     50      const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
     51      const VkAllocationCallbacks* pAllocator,
     52      VkDebugUtilsMessengerEXT* pDebugMessenger) {
     53   auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(
     54      instance, "vkCreateDebugUtilsMessengerEXT");
     55
     56   if (func != nullptr) {
     57      return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
     58   } else {
     59      return VK_ERROR_EXTENSION_NOT_PRESENT;
     60   }
     61}
     62
    3663class VulkanGame {
    3764   public:
     
    5077
    5178      VkInstance instance;
     79      VkDebugUtilsMessengerEXT debugMessenger;
    5280
    5381      // both SDL and GLFW create window functions return NULL on failure
     
    77105      void initVulkan() {
    78106         createInstance();
     107         setupDebugMessenger();
    79108      }
    80109
    81110      void createInstance() {
     111         if (enableValidationLayers && !checkValidationLayerSupport()) {
     112            throw runtime_error("validation layers requested, but not available!");
     113         }
     114
    82115         VkApplicationInfo appInfo = {};
    83116         appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
     
    92125         createInfo.pApplicationInfo = &appInfo;
    93126
    94          uint32_t extensionCount;
    95          SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
    96 
    97          vector<const char*> extensionNames(extensionCount);
    98          SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensionNames.data());
    99 
    100          createInfo.enabledExtensionCount = extensionCount;
    101          createInfo.ppEnabledExtensionNames = extensionNames.data();
    102          createInfo.enabledLayerCount = 0;
     127         auto extensions = getRequiredExtensions(window);
     128         createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
     129         createInfo.ppEnabledExtensionNames = extensions.data();
     130
     131         if (enableValidationLayers) {
     132            createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
     133            createInfo.ppEnabledLayerNames = validationLayers.data();
     134         } else {
     135            createInfo.enabledLayerCount = 0;
     136         }
    103137
    104138         if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
    105139            throw runtime_error("failed to create instance!");
     140         }
     141      }
     142
     143      void setupDebugMessenger() {
     144         if (!enableValidationLayers) return;
     145
     146         VkDebugUtilsMessengerCreateInfoEXT createInfo = {};
     147         createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
     148         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;
     149         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;
     150         createInfo.pfnUserCallback = debugCallback;
     151         createInfo.pUserData = nullptr;
     152
     153         if (CreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
     154            throw runtime_error("failed to setup debug messenger!");
    106155         }
    107156      }
     
    156205};
    157206
     207vector<const char*> getRequiredExtensions(SDL_Window* window) {
     208   uint32_t extensionCount;
     209   SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
     210
     211   vector<const char*> extensions(extensionCount);
     212   SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data());
     213
     214   if (enableValidationLayers) {
     215      extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
     216   }
     217
     218   return extensions;
     219}
     220
     221bool checkValidationLayerSupport() {
     222   uint32_t layerCount;
     223   vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
     224
     225   vector<VkLayerProperties> availableLayers(layerCount);
     226   vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
     227
     228   for (const char* layerName : validationLayers) {
     229      bool layerFound = false;
     230
     231      for (const auto& layerProperties : availableLayers) {
     232         if (strcmp(layerName, layerProperties.layerName) == 0) {
     233            layerFound = true;
     234            break;
     235         }
     236      }
     237
     238      if (!layerFound) {
     239         return false;
     240      }
     241   }
     242
     243   return true;
     244}
     245
    158246int main() {
    159247
     
    163251   cout << "DEBUGGING IS ON" << endl;
    164252#endif
    165 
    166    /*
    167    glfwInit();
    168 
    169    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
    170    GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr);
    171 
    172    uint32_t extensionCount = 0;
    173    vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
    174 
    175    cout << extensionCount << " extensions supported" << endl;
    176    */
    177 
    178    /*
    179    while(!glfwWindowShouldClose(window)) {
    180       glfwPollEvents();
    181    }
    182 
    183    glfwDestroyWindow(window);
    184 
    185    glfwTerminate();
    186    */
    187 
    188253   /*
    189254   mat4 matrix;
Note: See TracChangeset for help on using the changeset viewer.