From 1a2c31ab41fb6f6684526284da2814d308ac5c82 Mon Sep 17 00:00:00 2001 From: IlyaShurupov Date: Thu, 7 Nov 2024 17:27:46 +0300 Subject: [PATCH] add validation layers --- main.cpp | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 32b8497..5d00ea5 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,19 @@ #define GLFW_INCLUDE_VULKAN + +#include #include "GLFW/glfw3.h" #include "iostream" #include "stdexcept" #include "cstdlib" +#include "vector" + +const std::vector validationLayers = { +#ifdef NDEBUG +#else + "VK_LAYER_KHRONOS_validation" +#endif +}; class Application { public: @@ -25,6 +35,7 @@ private: void initVulkan() { createInstance(); + // pickPhysicalDevice(); } void mainLoop() { @@ -41,6 +52,10 @@ private: void createInstance() { + if (!checkValidationLayerSupport()) { + throw std::runtime_error("no required validation layers present"); + } + uint32_t glfwExtensionCount = 0; const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); @@ -56,7 +71,8 @@ private: VkInstanceCreateInfo createInfo { .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, .pApplicationInfo = &appInfo, - .enabledLayerCount = 0, + .enabledLayerCount = (uint32_t) validationLayers.size(), + .ppEnabledLayerNames = validationLayers.data(), .enabledExtensionCount = glfwExtensionCount, .ppEnabledExtensionNames = glfwExtensions, }; @@ -66,6 +82,28 @@ private: } } + static bool checkValidationLayerSupport() { + uint32_t layerCount; + vkEnumerateInstanceLayerProperties(&layerCount, nullptr); + + std::vector availableLayers(layerCount); + vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data()); + + for (const auto& requestedLayer : validationLayers) { + bool presents = false; + for (auto &layer: availableLayers) { + if (strcmp(requestedLayer, layer.layerName) == 0) { + presents = true; + break; + } + } + + if (!presents) return false; + } + + return true; + } + private: GLFWwindow* window = nullptr; VkInstance instance {};