Added layer validation in debug mode

This commit is contained in:
Warwick 2023-11-29 12:08:21 +00:00
parent c07eae32c3
commit a8cb724917
2 changed files with 35 additions and 8 deletions

View file

@ -7,7 +7,6 @@ YaveVulkanInstance::YaveVulkanInstance() { createInstance(); }
YaveVulkanInstance::~YaveVulkanInstance() { destroyInstance(); } YaveVulkanInstance::~YaveVulkanInstance() { destroyInstance(); }
void YaveVulkanInstance::createInstance() { void YaveVulkanInstance::createInstance() {
checkLayersValidSuppport();
VkApplicationInfo appInfo{}; VkApplicationInfo appInfo{};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "Hello Triangle"; appInfo.pApplicationName = "Hello Triangle";
@ -28,7 +27,7 @@ void YaveVulkanInstance::createInstance() {
createInfo.enabledExtensionCount = glfwExtensionCount; createInfo.enabledExtensionCount = glfwExtensionCount;
createInfo.ppEnabledExtensionNames = glfwExtensions; createInfo.ppEnabledExtensionNames = glfwExtensions;
createInfo.enabledLayerCount = 0; checkLayersValidSuppport(createInfo);
if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) { if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
throw std::runtime_error("failed to create vulkan instance!"); throw std::runtime_error("failed to create vulkan instance!");
@ -39,12 +38,39 @@ void YaveVulkanInstance::destroyInstance() {
vkDestroyInstance(this->instance, nullptr); vkDestroyInstance(this->instance, nullptr);
} }
bool YaveVulkanInstance::checkLayersValidSuppport() { bool YaveVulkanInstance::checkLayersValidSuppport(
// Continue if validation is disabled VkInstanceCreateInfo &createInfo) {
if (this->enableValidationLayers) { // Finish if validation is disabled (Release mode)
throw std::runtime_error("validation layers requested, but not available!"); if (!this->enableValidationLayers) {
return false; createInfo.enabledLayerCount = 0;
return true;
} }
// Get layers
uint32_t layerCount;
vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
std::vector<VkLayerProperties> availableLayers(layerCount);
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
// Validate layers
for (const char *layerName : validationLayers) {
bool layerFound = false;
for (const auto &layerProperties : availableLayers) {
if (strcmp(layerName, layerProperties.layerName) == 0) {
layerFound = true;
break;
}
}
if (!layerFound) {
return false;
}
}
return true;
createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
createInfo.ppEnabledLayerNames = validationLayers.data();
return true; return true;
} }

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <cstring>
#include <stdexcept> #include <stdexcept>
#include <vector> #include <vector>
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
@ -16,7 +17,7 @@ private:
void destroyInstance(); void destroyInstance();
// Validation support // Validation support
bool checkLayersValidSuppport(); bool checkLayersValidSuppport(VkInstanceCreateInfo &createInfo);
const std::vector<const char *> validationLayers = { const std::vector<const char *> validationLayers = {
"VK_LAYER_KHRONOS_validation"}; "VK_LAYER_KHRONOS_validation"};
#ifdef NDEBUG // Enable Validation in debug mode #ifdef NDEBUG // Enable Validation in debug mode