add validation layers

This commit is contained in:
IlyaShurupov 2024-11-07 17:27:46 +03:00
parent 7c8e86f003
commit 1a2c31ab41

View file

@ -1,9 +1,19 @@
#define GLFW_INCLUDE_VULKAN
#include <cstring>
#include "GLFW/glfw3.h"
#include "iostream"
#include "stdexcept"
#include "cstdlib"
#include "vector"
const std::vector<const char*> 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<VkLayerProperties> 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 {};