enable validation layers
This commit is contained in:
parent
f47e0682d2
commit
46dc696668
1 changed files with 56 additions and 12 deletions
54
src/main.c
54
src/main.c
|
|
@ -1,4 +1,5 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
@ -7,28 +8,67 @@
|
||||||
#define GLFW_INCLUDE_VULKAN
|
#define GLFW_INCLUDE_VULKAN
|
||||||
#include <GLFW/glfw3.h>
|
#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 {
|
typedef struct Application {
|
||||||
GLFWwindow *window;
|
GLFWwindow *window;
|
||||||
VkInstance instance;
|
VkInstance instance;
|
||||||
} Application;
|
} Application;
|
||||||
|
|
||||||
|
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 true;
|
||||||
|
}
|
||||||
|
|
||||||
bool verifyGlfwExtensionSupport(uint32_t extensionCount,
|
bool verifyGlfwExtensionSupport(uint32_t extensionCount,
|
||||||
VkExtensionProperties *extentions,
|
VkExtensionProperties *extentions,
|
||||||
uint32_t glfwExtensionCount,
|
uint32_t glfwExtensionCount,
|
||||||
const char **glfwExtensions) {
|
const char **glfwExtensions) {
|
||||||
uint32_t correctExtensions = 0;
|
|
||||||
for (uint32_t i = 0; i < glfwExtensionCount; i++) {
|
for (uint32_t i = 0; i < glfwExtensionCount; i++) {
|
||||||
|
bool layerFound = false;
|
||||||
for (uint32_t j = 0; j < extensionCount; j++) {
|
for (uint32_t j = 0; j < extensionCount; j++) {
|
||||||
if (strcmp(glfwExtensions[i], extentions[j].extensionName) == 0) {
|
if (strcmp(glfwExtensions[i], extentions[j].extensionName) == 0) {
|
||||||
correctExtensions++;
|
layerFound = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!layerFound) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
return correctExtensions == glfwExtensionCount;
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void createInstance(Application *app) {
|
void createInstance(Application *app) {
|
||||||
|
if (enableValidationLayers && !checkValidationLayerSupport()) {
|
||||||
|
fprintf(stderr, "Validation layers requested, but not available!");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
VkApplicationInfo appInfo = {
|
VkApplicationInfo appInfo = {
|
||||||
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
|
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
|
||||||
.pApplicationName = "Hello Triangle",
|
.pApplicationName = "Hello Triangle",
|
||||||
|
|
@ -49,6 +89,10 @@ void createInstance(Application *app) {
|
||||||
.ppEnabledExtensionNames = glfwExtensions,
|
.ppEnabledExtensionNames = glfwExtensions,
|
||||||
.enabledLayerCount = 0,
|
.enabledLayerCount = 0,
|
||||||
};
|
};
|
||||||
|
if (enableValidationLayers) {
|
||||||
|
createInfo.enabledLayerCount = validationLayerCount;
|
||||||
|
createInfo.ppEnabledLayerNames = validationLayers;
|
||||||
|
}
|
||||||
|
|
||||||
VkResult result = vkCreateInstance(&createInfo, NULL, &app->instance);
|
VkResult result = vkCreateInstance(&createInfo, NULL, &app->instance);
|
||||||
if (result != VK_SUCCESS) {
|
if (result != VK_SUCCESS) {
|
||||||
|
|
@ -61,8 +105,8 @@ void createInstance(Application *app) {
|
||||||
VkExtensionProperties extensions[extensionCount];
|
VkExtensionProperties extensions[extensionCount];
|
||||||
vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, extensions);
|
vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, extensions);
|
||||||
|
|
||||||
if (!verifyGlfwExtensionSupport(extensionCount, extensions, glfwExtensionCount,
|
if (!verifyGlfwExtensionSupport(extensionCount, extensions,
|
||||||
glfwExtensions)) {
|
glfwExtensionCount, glfwExtensions)) {
|
||||||
fprintf(stderr, "Failed to find all required vulkan extentions for glfw\n");
|
fprintf(stderr, "Failed to find all required vulkan extentions for glfw\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue