Major refactoring to fix spaghetti code, hook sanitizers in when debug building, and set up window surfaces.

This commit is contained in:
Lillian Salehi 2024-10-06 04:35:53 -05:00
parent 9a6a351e23
commit 3e0206b581
8 changed files with 185 additions and 147 deletions

View File

@ -1,6 +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 DEBUGFLAGS=-DDEBUG -fsanitize=address
SRC=$(shell find . -name *.cpp) SRC=$(shell find . -name *.cpp)
OBJ=$(SRC:%.cpp=%.o) OBJ=$(SRC:%.cpp=%.o)

View File

@ -1,31 +1,31 @@
#include "DeviceLibrary.h" #include "DeviceLibrary.h"
#include "debug/VulkanDebugLibs.h"
#include "global.h"
#include <cstdint> #include <cstdint>
#include <optional> #include <optional>
#include <ostream> #include <ostream>
#include <set>
#include <stdexcept> #include <stdexcept>
#include <vulkan/vulkan_core.h> #include <vulkan/vulkan_core.h>
using namespace AgnosiaEngine;
namespace DeviceControl {
VkSurfaceKHR surface;
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
VkPhysicalDeviceProperties deviceProperties; VkPhysicalDeviceProperties deviceProperties;
VkPhysicalDeviceFeatures deviceFeatures; VkPhysicalDeviceFeatures deviceFeatures;
VulkanDebugLibs debug;
VkQueue graphicsQueue;
#ifdef DEBUG VkQueue graphicsQueue;
const bool enableValidationLayers = true; VkQueue presentQueue;
#else
const bool enableValidationLayers = false;
#endif
struct QueueFamilyIndices { struct QueueFamilyIndices {
// We need to check that the Queue families support graphics operations and window presentation, sometimes they can support one or the other,
// therefore, we take into account both for completion.
std::optional<uint32_t> graphicsFamily; std::optional<uint32_t> graphicsFamily;
std::optional<uint32_t> presentFamily;
bool isComplete() { bool isComplete() {
return graphicsFamily.has_value(); return graphicsFamily.has_value() && presentFamily.has_value();
} }
}; };
@ -34,9 +34,10 @@ QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
// These store the flags, the amount of queued items in the family, and timestamp data. Queue families are simply group collections of tasks we want to get done. // These store the flags, the amount of queued items in the family, and timestamp data. Queue families are simply group collections of tasks we want to get done.
// Next, we check the flags of the queueFamily item, use a bitwise and to see if they match, i.e. support graphical operations, then return that to notify that we have at least one family that supports VK_QUEUE_GRAPHICS_BIT. // Next, we check the flags of the queueFamily item, use a bitwise and to see if they match, i.e. support graphical operations, then return that to notify that we have at least one family that supports VK_QUEUE_GRAPHICS_BIT.
// Which means this device supports graphical operations! // Which means this device supports graphical operations!
// We also do the same thing for window presentation, just check to see if its supported.
QueueFamilyIndices indices; QueueFamilyIndices indices;
uint32_t queueFamilyCount; uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr); vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount); std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
@ -47,6 +48,13 @@ QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
if(queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) { if(queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
indices.graphicsFamily = i; indices.graphicsFamily = i;
} }
VkBool32 presentSupport = false;
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
if(presentSupport) {
indices.presentFamily = i;
}
if(indices.isComplete()) { if(indices.isComplete()) {
break; break;
} }
@ -66,7 +74,6 @@ bool isDeviceSuitable(VkPhysicalDevice device) {
// is a queue family with the VK_QUEUE_GRAPHICS_BIT flipped! // is a queue family with the VK_QUEUE_GRAPHICS_BIT flipped!
QueueFamilyIndices indices = findQueueFamilies(device); QueueFamilyIndices indices = findQueueFamilies(device);
return deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU && deviceFeatures.multiViewport && deviceFeatures.geometryShader && indices.isComplete(); return deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU && deviceFeatures.multiViewport && deviceFeatures.geometryShader && indices.isComplete();
} }
@ -88,40 +95,61 @@ void DeviceLibrary::pickPhysicalDevice(VkInstance& instance) {
break; break;
} }
} }
if(physicalDevice == VK_NULL_HANDLE) { if(physicalDevice == VK_NULL_HANDLE) {
throw std::runtime_error("Failed to find a suitable GPU!"); throw std::runtime_error("Failed to find a suitable GPU!");
} }
} }
void DeviceLibrary::destroySurface(VkInstance& instance) {
vkDestroySurfaceKHR(instance, surface, nullptr);
std::cout << "Destroyed surface safely\n" << std::endl;
}
void DeviceLibrary::createSurface(VkInstance& instance, GLFWwindow* window) {
if(glfwCreateWindowSurface(instance, window, nullptr, &surface) != VK_SUCCESS) {
throw std::runtime_error("Failed to create window surface!!");
}
std::cout << "GLFW Window Surface created successfully\n" << std::endl;
}
void DeviceLibrary::createLogicalDevice(VkDevice& device) { void DeviceLibrary::createLogicalDevice(VkDevice& device) {
// Describe how many queues we want for a single family (1) here, right now we are solely interested in graphics capabilites, // Describe how many queues we want for a single family (1) here, right now we are solely interested in graphics capabilites,
// but Compute Shaders, transfer ops, decode and encode operations can also queued with setup! We also assign each queue a priority. // but Compute Shaders, transfer ops, decode and encode operations can also queued with setup! We also assign each queue a priority.
QueueFamilyIndices indices = findQueueFamilies(physicalDevice); QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
VkDeviceQueueCreateInfo queueCreateInfo{}; std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; std::set<uint32_t> uniqueQueueFamilies = {
queueCreateInfo.queueFamilyIndex = indices.graphicsFamily.value(); indices.graphicsFamily.value(),
queueCreateInfo.queueCount = 1; indices.presentFamily.value()
};
float queuePriority = 1.0f; float queuePriority = 1.0f;
queueCreateInfo.pQueuePriorities = &queuePriority; for(uint32_t queueFamily : uniqueQueueFamilies) {
VkDeviceQueueCreateInfo queueCreateSingularInfo = {};
VkDeviceCreateInfo createInfo{}; queueCreateSingularInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; queueCreateSingularInfo.queueFamilyIndex = queueFamily;
createInfo.pQueueCreateInfos = &queueCreateInfo; queueCreateSingularInfo.queueCount = 1;
createInfo.queueCreateInfoCount = 1; queueCreateSingularInfo.pQueuePriorities = &queuePriority;
createInfo.pEnabledFeatures = &deviceFeatures; queueCreateInfos.push_back(queueCreateSingularInfo);
createInfo.enabledExtensionCount = 0;
if(enableValidationLayers) {
createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
createInfo.ppEnabledLayerNames = validationLayers.data();
} else {
createInfo.enabledLayerCount = 0;
} }
if(vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) { VkDeviceCreateInfo createDeviceInfo = {};
VkPhysicalDeviceFeatures emptyFeatures = {};
createDeviceInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
createDeviceInfo.pQueueCreateInfos = queueCreateInfos.data();
createDeviceInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
createDeviceInfo.pEnabledFeatures = &emptyFeatures;
createDeviceInfo.enabledExtensionCount = 0;
if(Global::enableValidationLayers) {
createDeviceInfo.enabledLayerCount = static_cast<uint32_t>(Global::validationLayers.size());
createDeviceInfo.ppEnabledLayerNames = Global::validationLayers.data();
} else {
createDeviceInfo.enabledLayerCount = 0;
}
if(vkCreateDevice(physicalDevice, &createDeviceInfo, nullptr, &device) != VK_SUCCESS) {
throw std::runtime_error("Failed to create logical device"); throw std::runtime_error("Failed to create logical device");
} }
std::cout << "Created Logical device successfully!\n" << std::endl;
vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue); vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
}
} }

View File

@ -1,9 +1,14 @@
#pragma once #pragma once
#include <vulkan/vulkan_core.h> #include "global.h"
namespace AgnosiaEngine { namespace DeviceControl {
class DeviceLibrary { class DeviceLibrary {
public: public:
void pickPhysicalDevice(VkInstance& instance); void pickPhysicalDevice(VkInstance& instance);
void createLogicalDevice(VkDevice& devicvee); void createLogicalDevice(VkDevice& device);
void createSurface(VkInstance& instance, GLFWwindow* window);
void destroySurface(VkInstance& instance);
}; };
} }

View File

@ -5,18 +5,11 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "../global.h" #include "../global.h"
#include "VulkanDebugLibs.h" using namespace Debug;
using namespace AgnosiaEngine;
#include <cstring> #include <cstring>
#include <vulkan/vulkan_core.h> #include <vulkan/vulkan_core.h>
// This ifdef checks if the build flag is present, hence whether to hook the debugger in at all.
#ifdef DEBUG
const bool enableValidationLayers = true;
#else
const bool enableValidationLayers = false;
#endif
// This is our messenger object! It handles passing along debug messages to the debug callback we will also set. // This is our messenger object! It handles passing along debug messages to the debug callback we will also set.
VkDebugUtilsMessengerEXT debugMessenger; VkDebugUtilsMessengerEXT debugMessenger;
// This is the set of "layers" to hook into. Basically, layers are used to tell the messenger what data we want, its a filter. *validation* is the general blanket layer to cover incorrect usage. // This is the set of "layers" to hook into. Basically, layers are used to tell the messenger what data we want, its a filter. *validation* is the general blanket layer to cover incorrect usage.
@ -31,7 +24,7 @@ std::vector<const char*> getRequiredExtensions() {
std::vector<const char*> extensions(glfwExtensions, glfwExtensions + glfwExtensionCount); std::vector<const char*> extensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
if(enableValidationLayers) { if(Global::enableValidationLayers) {
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
} }
return extensions; return extensions;
@ -51,6 +44,8 @@ static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
return VK_FALSE; return VK_FALSE;
} }
void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) { void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
// There is absolutely nothing about this i like, those long ass flags for messageType and Severity are just fucking hex values. Khronos should never cook again ToT // There is absolutely nothing about this i like, those long ass flags for messageType and Severity are just fucking hex values. Khronos should never cook again ToT
// On a serious note, this is just a struct to define the parameters of the debug messenger, nothing super special. // On a serious note, this is just a struct to define the parameters of the debug messenger, nothing super special.
@ -71,9 +66,9 @@ void VulkanDebugLibs::vulkanDebugSetup(VkInstanceCreateInfo& createInfo, VkInsta
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size()); createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
createInfo.ppEnabledExtensionNames = extensions.data(); createInfo.ppEnabledExtensionNames = extensions.data();
if(enableValidationLayers) { if(Global::enableValidationLayers) {
createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size()); createInfo.enabledLayerCount = static_cast<uint32_t>(Global::validationLayers.size());
createInfo.ppEnabledLayerNames = validationLayers.data(); createInfo.ppEnabledLayerNames = Global::validationLayers.data();
populateDebugMessengerCreateInfo(debugCreateInfo); populateDebugMessengerCreateInfo(debugCreateInfo);
createInfo.pNext = (VkDebugUtilsMessengerCreateInfoEXT*) &debugCreateInfo; createInfo.pNext = (VkDebugUtilsMessengerCreateInfoEXT*) &debugCreateInfo;
@ -90,7 +85,7 @@ void VulkanDebugLibs::vulkanDebugSetup(VkInstanceCreateInfo& createInfo, VkInsta
void VulkanDebugLibs::checkUnavailableValidationLayers() { void VulkanDebugLibs::checkUnavailableValidationLayers() {
// Check if we are trying to hook validation layers in without support. // Check if we are trying to hook validation layers in without support.
if(enableValidationLayers && !checkValidationLayerSupport()) { if(Global::enableValidationLayers && !checkValidationLayerSupport()) {
throw std::runtime_error("Validation layers request, but not available! Are your SDK path variables set?"); throw std::runtime_error("Validation layers request, but not available! Are your SDK path variables set?");
} }
} }
@ -106,7 +101,7 @@ bool VulkanDebugLibs::checkValidationLayerSupport() {
std::vector<VkLayerProperties> availableLayers(layerCount); std::vector<VkLayerProperties> availableLayers(layerCount);
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data()); vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
for(const char* layerName : validationLayers) { for(const char* layerName : Global::validationLayers) {
bool layerFound = false; bool layerFound = false;
for(const auto& layerProperties : availableLayers) { for(const auto& layerProperties : availableLayers) {
@ -153,7 +148,7 @@ void VulkanDebugLibs::DestroyDebugUtilsMessengerEXT(VkInstance instance,
void VulkanDebugLibs::setupDebugMessenger(VkInstance& vulkanInstance) { void VulkanDebugLibs::setupDebugMessenger(VkInstance& vulkanInstance) {
// This is a pretty simple function! we just pass in the values to build the debug messenger, populate the structure with the data we want, // This is a pretty simple function! we just pass in the values to build the debug messenger, populate the structure with the data we want,
// and safely create it, covering for runtime errors as per usual, this is the first thing that will be called! // and safely create it, covering for runtime errors as per usual, this is the first thing that will be called!
if(!enableValidationLayers) return; if(!Global::enableValidationLayers) return;
VkDebugUtilsMessengerCreateInfoEXT createInfo; VkDebugUtilsMessengerCreateInfoEXT createInfo;
populateDebugMessengerCreateInfo(createInfo); populateDebugMessengerCreateInfo(createInfo);

View File

@ -2,8 +2,9 @@
#include <vector> #include <vector>
#include <vulkan/vulkan_core.h> #include <vulkan/vulkan_core.h>
namespace AgnosiaEngine { namespace Debug {
class VulkanDebugLibs { class VulkanDebugLibs {
public: public:
void vulkanDebugSetup(VkInstanceCreateInfo& createInfo, VkInstance& instance); void vulkanDebugSetup(VkInstanceCreateInfo& createInfo, VkInstance& instance);
bool checkValidationLayerSupport(); bool checkValidationLayerSupport();

View File

@ -1,5 +1,13 @@
#include "global.h" #include "global.h"
namespace Global {
const std::vector<const char*> validationLayers = { const std::vector<const char*> validationLayers = {
"VK_LAYER_KHRONOS_validation" "VK_LAYER_KHRONOS_validation"
}; };
#ifdef DEBUG
const bool enableValidationLayers = true;
#else
const bool enableValidationLayers = false;
#endif
}

View File

@ -1,5 +1,13 @@
#pragma once #pragma once
#include "debug/VulkanDebugLibs.h"
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <vulkan/vulkan_core.h>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
namespace Global {
extern const std::vector<const char*> validationLayers; extern const std::vector<const char*> validationLayers;
extern const bool enableValidationLayers;
}

View File

@ -1,24 +1,14 @@
#include <vector>
#include <vulkan/vulkan_core.h>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include "debug/VulkanDebugLibs.h" #include "debug/VulkanDebugLibs.h"
#include "DeviceLibrary.h" #include "DeviceLibrary.h"
using namespace AgnosiaEngine; #include "debug/VulkanDebugLibs.h"
#include "global.h"
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#ifdef DEBUG
const bool enableValidationLayers = true;
#else
const bool enableValidationLayers = false;
#endif
const uint32_t WIDTH = 800; const uint32_t WIDTH = 800;
const uint32_t HEIGHT = 600; const uint32_t HEIGHT = 600;
@ -26,7 +16,6 @@ const uint32_t HEIGHT = 600;
class TriangleTestApplication { class TriangleTestApplication {
public: public:
void run() { void run() {
initWindow(); initWindow();
initVulkan(); initVulkan();
@ -35,11 +24,13 @@ public:
} }
private: private:
DeviceControl::DeviceLibrary deviceLibs;
Debug::VulkanDebugLibs debugController;
GLFWwindow* window; GLFWwindow* window;
VkInstance instance; VkInstance instance;
VulkanDebugLibs debug;
DeviceLibrary deviceLibs;
VkDevice device; VkDevice device;
// Initialize GLFW Window. First, Initialize GLFW lib, disable resizing for // Initialize GLFW Window. First, Initialize GLFW lib, disable resizing for
// now, and create window. // now, and create window.
void initWindow() { void initWindow() {
@ -53,13 +44,14 @@ private:
void initVulkan() { void initVulkan() {
createInstance(); createInstance();
debug.setupDebugMessenger(instance); // The debug messenger is out holy grail, it gives us Vulkan related debug info when built with the -DNDEBUG flag (as per the makefile) debugController.setupDebugMessenger(instance); // The debug messenger is out holy grail, it gives us Vulkan related debug info when built with the -DNDEBUG flag (as per the makefile)
deviceLibs.createSurface(instance, window);
deviceLibs.pickPhysicalDevice(instance); deviceLibs.pickPhysicalDevice(instance);
deviceLibs.createLogicalDevice(device); deviceLibs.createLogicalDevice(device);
} }
void createInstance() { void createInstance() {
debug.checkUnavailableValidationLayers(); // Check if there is a mistake with our Validation Layers. debugController.checkUnavailableValidationLayers(); // Check if there is a mistake with our Validation Layers.
// Set application info for the vulkan instance! // Set application info for the vulkan instance!
VkApplicationInfo appInfo{}; VkApplicationInfo appInfo{};
@ -69,13 +61,13 @@ private:
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 = "Agnosia Engine"; // Give an internal name for the engine running appInfo.pEngineName = "Agnosia 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_1; // 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.
debug.vulkanDebugSetup(createInfo, instance); // Handoff to the debug library to wrap the validation libs in! (And set the window up!) debugController.vulkanDebugSetup(createInfo, instance); // Handoff to the debug library to wrap the validation libs in! (And set the window up!)
} }
void mainLoop() { // This loop just updates the GLFW window. void mainLoop() { // This loop just updates the GLFW window.
@ -86,14 +78,15 @@ private:
void cleanup() { // Similar to the last handoff, destroy the debug util in a safe manner in the library! void cleanup() { // Similar to the last handoff, destroy the debug util in a safe manner in the library!
vkDestroyDevice(device, nullptr); vkDestroyDevice(device, nullptr);
if(enableValidationLayers) { if(Global::enableValidationLayers) {
debug.DestroyDebugUtilsMessengerEXT(instance, nullptr); debugController.DestroyDebugUtilsMessengerEXT(instance, nullptr);
} }
deviceLibs.destroySurface(instance);
vkDestroyInstance(instance, nullptr); vkDestroyInstance(instance, nullptr);
glfwDestroyWindow(window); glfwDestroyWindow(window);
glfwTerminate(); glfwTerminate();
} }
}; };
int main() { int main() {