Added validation layer injection, working on message callbacks per the vulkan docs
This commit is contained in:
5
Makefile
5
Makefile
@@ -1,5 +1,6 @@
|
|||||||
CPPFLAGS=-g
|
CPPFLAGS=-g
|
||||||
LDFLAGS=-lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
|
LDFLAGS=-lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
|
||||||
|
DEBUGFLAGS=-DDEBUG
|
||||||
SRC=$(shell find . -name *.cpp)
|
SRC=$(shell find . -name *.cpp)
|
||||||
OBJ=$(SRC:%.cpp=%.o)
|
OBJ=$(SRC:%.cpp=%.o)
|
||||||
|
|
||||||
@@ -12,6 +13,10 @@ all: $(BIN)
|
|||||||
.PHONY: run
|
.PHONY: run
|
||||||
run: $(BIN)
|
run: $(BIN)
|
||||||
./$(BIN)
|
./$(BIN)
|
||||||
|
.PHONY: debug
|
||||||
|
debug: LDFLAGS+=$(DEBUGFLAGS)
|
||||||
|
debug: $(BIN)
|
||||||
|
./$(BIN)
|
||||||
.PHONY: dep
|
.PHONY: dep
|
||||||
dep:
|
dep:
|
||||||
sudo pacman -S gcc glfw glm shaderc libxi libxxf86vm
|
sudo pacman -S gcc glfw glm shaderc libxi libxxf86vm
|
||||||
|
75
src/main.cpp
75
src/main.cpp
@@ -1,3 +1,6 @@
|
|||||||
|
#include <cstdint>
|
||||||
|
#include <cstring>
|
||||||
|
#include <vector>
|
||||||
#define GLFW_INCLUDE_VULKAN
|
#define GLFW_INCLUDE_VULKAN
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
@@ -10,6 +13,16 @@ class TriangleTestApplication {
|
|||||||
const uint32_t WIDTH = 800;
|
const uint32_t WIDTH = 800;
|
||||||
const uint32_t HEIGHT = 600;
|
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:
|
public:
|
||||||
void run() {
|
void run() {
|
||||||
initWindow();
|
initWindow();
|
||||||
@@ -34,19 +47,27 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void initVulkan() {
|
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!
|
// Set application info for the vulkan instance!
|
||||||
VkApplicationInfo appInfo{};
|
VkApplicationInfo appInfo{};
|
||||||
|
|
||||||
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; // Tell vulkan that appInfo is a Application Info structure
|
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; // Tell vulkan that appInfo is a Application Info structure
|
||||||
appInfo.pApplicationName = "Triangle Test"; // Give the struct a name to use
|
appInfo.pApplicationName = "Triangle Test"; // Give the struct a name to use
|
||||||
appInfo.applicationVersion = VK_MAKE_VERSION(1,0,0); // Create a Major Minor Patch version number for the application!
|
appInfo.applicationVersion = VK_MAKE_VERSION(1,0,0); // Create a Major Minor Patch version number for the application!
|
||||||
appInfo.pEngineName = "No Engine"; // Give an internal name for the engine running
|
appInfo.pEngineName = "No Engine"; // Give an internal name for the engine running
|
||||||
appInfo.engineVersion = VK_MAKE_VERSION(1,0,0); // Similar to the App version, give vulkan an *engine* version
|
appInfo.engineVersion = VK_MAKE_VERSION(1,0,0); // Similar to the App version, give vulkan an *engine* version
|
||||||
appInfo.apiVersion = VK_API_VERSION_1_0; // Tell vulkan what the highest API version we will allow this program to run on
|
appInfo.apiVersion = VK_API_VERSION_1_0; // Tell vulkan what the highest API version we will allow this program to run on
|
||||||
|
|
||||||
VkInstanceCreateInfo createInfo{}; // Define parameters of new vulkan instance
|
VkInstanceCreateInfo createInfo{}; // Define parameters of new vulkan instance
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; // Tell vulkan this is a info structure
|
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; // Tell vulkan this is a info structure
|
||||||
createInfo.pApplicationInfo = &appInfo; // We just created a new appInfo structure, so we pass the pointer to it.
|
createInfo.pApplicationInfo = &appInfo; // We just created a new appInfo structure, so we pass the pointer to it.
|
||||||
|
|
||||||
// This gets a little weird, Vulkan is platform agnostic, so you need to figure out what extensions to interface with the current system are needed
|
// This gets a little weird, Vulkan is platform agnostic, so you need to figure out what extensions to interface with the current system are needed
|
||||||
// So, to figure out what extension codes and how many to use, feed the pointer into *glfwGetRequiredInstanceExtensions*, which will get the necessary extensions!
|
// So, to figure out what extension codes and how many to use, feed the pointer into *glfwGetRequiredInstanceExtensions*, which will get the necessary extensions!
|
||||||
@@ -59,14 +80,43 @@ private:
|
|||||||
createInfo.enabledExtensionCount = glfwExtensionCount;
|
createInfo.enabledExtensionCount = glfwExtensionCount;
|
||||||
createInfo.ppEnabledExtensionNames = glfwExtensions;
|
createInfo.ppEnabledExtensionNames = glfwExtensions;
|
||||||
|
|
||||||
createInfo.enabledLayerCount = 0;
|
if(enableValidationLayers) { // If we have validation layers, add them now, otherwise set it to 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!
|
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)
|
if(result != VK_SUCCESS) { // vkCreateInstance returns a VkResult, if its anything but success, we exit immediately. (VK_SUCCESS == 0)
|
||||||
throw std::runtime_error("Failed to create vulkan instance!");
|
throw std::runtime_error("Failed to create vulkan instance!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
void mainLoop() {
|
||||||
// Update window whilst open
|
// Update window whilst open
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
@@ -80,6 +130,7 @@ private:
|
|||||||
glfwDestroyWindow(window);
|
glfwDestroyWindow(window);
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
Reference in New Issue
Block a user