Added validation layer injection, working on message callbacks per the vulkan docs

This commit is contained in:
Lillian Salehi 2024-10-04 19:56:58 -05:00
parent d97ac93a1f
commit 04997fa682
2 changed files with 71 additions and 15 deletions

View File

@ -1,5 +1,6 @@
CPPFLAGS=-g
LDFLAGS=-lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
DEBUGFLAGS=-DDEBUG
SRC=$(shell find . -name *.cpp)
OBJ=$(SRC:%.cpp=%.o)
@ -12,6 +13,10 @@ all: $(BIN)
.PHONY: run
run: $(BIN)
./$(BIN)
.PHONY: debug
debug: LDFLAGS+=$(DEBUGFLAGS)
debug: $(BIN)
./$(BIN)
.PHONY: dep
dep:
sudo pacman -S gcc glfw glm shaderc libxi libxxf86vm

View File

@ -1,3 +1,6 @@
#include <cstdint>
#include <cstring>
#include <vector>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
@ -10,6 +13,16 @@ class TriangleTestApplication {
const uint32_t WIDTH = 800;
const uint32_t HEIGHT = 600;
const std::vector<const char*> validationLayers = {
"VK_LAYER_KHRONOS_validation"
};
#ifdef DEBUG
const bool enableValidationLayers = true;
#else
const bool enableValidationLayers = false;
#endif
public:
void run() {
initWindow();
@ -34,6 +47,14 @@ private:
}
void initVulkan() {
createInstance();
}
void createInstance() {
if(enableValidationLayers && !checkValidationLayerSupport()) {
throw std::runtime_error("Validation layers requested, but not available!");
}
// Set application info for the vulkan instance!
VkApplicationInfo appInfo{};
@ -59,7 +80,12 @@ private:
createInfo.enabledExtensionCount = glfwExtensionCount;
createInfo.ppEnabledExtensionNames = glfwExtensions;
if(enableValidationLayers) { // If we have validation layers, add them now, otherwise set it to 0
createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
createInfo.ppEnabledLayerNames = validationLayers.data();
} else {
createInfo.enabledLayerCount = 0;
}
VkResult result = vkCreateInstance(&createInfo, nullptr, &instance); // Finally create the Vulkan instance, passing in the info to create from, and the global instance to use!
if(result != VK_SUCCESS) { // vkCreateInstance returns a VkResult, if its anything but success, we exit immediately. (VK_SUCCESS == 0)
@ -67,6 +93,30 @@ private:
}
}
bool checkValidationLayerSupport() { // This function is used to check Validation Layer Support, validation layers are the debug trace tools in the Vulkan SDK.
uint32_t layerCount; // layerCount will be used as the var to keep track of the number of requested validation layerk
vkEnumerateInstanceLayerProperties(&layerCount, nullptr); // Set layerCount to the number of validation layers requested when pProperties is NULLPTR
std::vector<VkLayerProperties> availableLayers(layerCount); // VkLayerProperties is a structure with data on the layername, desc, versions and etc.
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());// Now that we have a VkLayerProperties fed in, as well as the num. of properties, we can fill layerCount with the VkResult
for(const char* layerName : validationLayers) { // Pretty straightforward from here, just enumerate over all the VkResult data and see if we have any validationLayers
bool layerFound = false;
for(const auto& layerProperties : availableLayers) {
if(strcmp(layerName, layerProperties.layerName) == 0) {
layerFound = true;
break;
}
}
if(!layerFound) {
return false;
}
}
return true;
}
void mainLoop() {
// Update window whilst open
while (!glfwWindowShouldClose(window)) {
@ -80,6 +130,7 @@ private:
glfwDestroyWindow(window);
glfwTerminate();
}
};
int main() {