enable validation layers

This commit is contained in:
Warwick 2024-10-10 17:39:16 +01:00
parent f47e0682d2
commit 46dc696668

View file

@ -1,4 +1,5 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -7,28 +8,67 @@
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#ifdef NDEBUG
const bool enableValidationLayers = false;
#else
const bool enableValidationLayers = true;
#endif
const char *validationLayers[] = {"VK_LAYER_KHRONOS_validation"};
size_t validationLayerCount =
sizeof(validationLayers) / sizeof(validationLayers[0]);
typedef struct Application {
GLFWwindow *window;
VkInstance instance;
} Application;
bool verifyGlfwExtensionSupport(uint32_t extensionCount,
VkExtensionProperties *extentions,
uint32_t glfwExtensionCount,
const char **glfwExtensions) {
uint32_t correctExtensions = 0;
for (uint32_t i = 0; i < glfwExtensionCount; i++) {
for (uint32_t j = 0; j < extensionCount; j++) {
if (strcmp(glfwExtensions[i], extentions[j].extensionName) == 0) {
correctExtensions++;
bool checkValidationLayerSupport() {
uint32_t layerCount;
vkEnumerateInstanceLayerProperties(&layerCount, NULL);
VkLayerProperties availableLayers[layerCount];
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers);
for (int i = 0; i < validationLayerCount; i++) {
bool layerFound = false;
for (int j = 0; j < layerCount; j++) {
if (strcmp(availableLayers[j].layerName, validationLayers[i]) == 0) {
layerFound = true;
break;
}
}
if (!layerFound) {
return false;
}
}
return correctExtensions == glfwExtensionCount;
return true;
}
bool verifyGlfwExtensionSupport(uint32_t extensionCount,
VkExtensionProperties *extentions,
uint32_t glfwExtensionCount,
const char **glfwExtensions) {
for (uint32_t i = 0; i < glfwExtensionCount; i++) {
bool layerFound = false;
for (uint32_t j = 0; j < extensionCount; j++) {
if (strcmp(glfwExtensions[i], extentions[j].extensionName) == 0) {
layerFound = true;
break;
}
}
if (!layerFound) {
return false;
}
}
return true;
}
void createInstance(Application *app) {
if (enableValidationLayers && !checkValidationLayerSupport()) {
fprintf(stderr, "Validation layers requested, but not available!");
exit(EXIT_FAILURE);
}
VkApplicationInfo appInfo = {
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pApplicationName = "Hello Triangle",
@ -49,6 +89,10 @@ void createInstance(Application *app) {
.ppEnabledExtensionNames = glfwExtensions,
.enabledLayerCount = 0,
};
if (enableValidationLayers) {
createInfo.enabledLayerCount = validationLayerCount;
createInfo.ppEnabledLayerNames = validationLayers;
}
VkResult result = vkCreateInstance(&createInfo, NULL, &app->instance);
if (result != VK_SUCCESS) {
@ -61,8 +105,8 @@ void createInstance(Application *app) {
VkExtensionProperties extensions[extensionCount];
vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, extensions);
if (!verifyGlfwExtensionSupport(extensionCount, extensions, glfwExtensionCount,
glfwExtensions)) {
if (!verifyGlfwExtensionSupport(extensionCount, extensions,
glfwExtensionCount, glfwExtensions)) {
fprintf(stderr, "Failed to find all required vulkan extentions for glfw\n");
exit(EXIT_FAILURE);
}