Finished validation layer stuffs

This commit is contained in:
Mars 2024-09-28 14:54:39 -04:00
parent 3b3cb5bec3
commit b1c49e705b
Signed by untrusted user: pupbrained
GPG key ID: 874E22DF2F9DFCB5
5 changed files with 1841 additions and 22 deletions

View file

@ -9,7 +9,7 @@ AllowShortLoopsOnASingleLine: true
BasedOnStyle: Chromium BasedOnStyle: Chromium
BinPackArguments: false BinPackArguments: false
BinPackParameters: false BinPackParameters: false
ColumnLimit: 100 ColumnLimit: 110
ConstructorInitializerIndentWidth: 2 ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2 ContinuationIndentWidth: 2
Cpp11BracedListStyle: false Cpp11BracedListStyle: false

3
.envrc
View file

@ -1 +1,2 @@
use_flake export NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM=1
use_flake . --impure

View file

@ -111,6 +111,9 @@
] ]
++ deps; ++ deps;
VULKAN_SDK = "${vulkan-headers}";
VK_LAYER_PATH = "${vulkan-validation-layers}/share/vulkan/explicit_layer.d";
shellHook = '' shellHook = ''
export PATH="${llvmPackages_18.clang-tools.override {enableLibcxx = true;}}/bin:$PATH" export PATH="${llvmPackages_18.clang-tools.override {enableLibcxx = true;}}/bin:$PATH"
''; '';

View file

@ -1,11 +1,14 @@
#include <cstdlib> #include <cstdlib>
#include <fmt/format.h> #include <fmt/format.h>
#include <iostream> #include <iostream>
#include <span>
#include <vulkan/vulkan_core.h>
#define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_NONE
#define GLFW_INCLUDE_VULKAN #define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "util/magic_enum.hpp"
#include "util/types.h" #include "util/types.h"
constexpr u32 WIDTH = 800; constexpr u32 WIDTH = 800;
@ -19,6 +22,35 @@ const bool enableValidationLayers = false;
const bool enableValidationLayers = true; const bool enableValidationLayers = true;
#endif #endif
fn CreateDebugUtilsMessengerEXT(
VkInstance instance,
const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkDebugUtilsMessengerEXT* pDebugMessenger
) -> VkResult {
auto func = std::bit_cast<PFN_vkCreateDebugUtilsMessengerEXT>(
vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT")
);
if (func == nullptr)
return VK_ERROR_EXTENSION_NOT_PRESENT;
return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
}
fn DestroyDebugUtilsMessengerEXT(
VkInstance instance,
VkDebugUtilsMessengerEXT debugMessenger,
const VkAllocationCallbacks* pAllocator
) -> void {
auto func = std::bit_cast<PFN_vkDestroyDebugUtilsMessengerEXT>(
vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT")
);
if (func != nullptr)
func(instance, debugMessenger, pAllocator);
}
class Application { class Application {
public: public:
fn run() -> void { fn run() -> void {
@ -29,8 +61,9 @@ class Application {
} }
private: private:
VkInstance mInstance; VkDebugUtilsMessengerEXT mDebugMessenger;
GLFWwindow* mWindow; VkInstance mInstance;
GLFWwindow* mWindow;
static fn checkValidationLayerSupport() -> bool { static fn checkValidationLayerSupport() -> bool {
u32 layerCount = 0; u32 layerCount = 0;
@ -44,8 +77,7 @@ class Application {
for (const auto& layerProperties : availableLayers) { for (const auto& layerProperties : availableLayers) {
if (strcmp( if (strcmp(
static_cast<const char*>(layerName), static_cast<const char*>(layerName), static_cast<const char*>(layerProperties.layerName)
static_cast<const char*>(layerProperties.layerName)
) == 0) { ) == 0) {
layerFound = true; layerFound = true;
break; break;
@ -69,10 +101,49 @@ class Application {
return extensions; return extensions;
} }
static fn getRequiredExtensions() -> std::vector<const char*> {
u32 glfwExtensionCount = 0;
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
std::vector<const char*> extensions;
if (glfwExtensions) {
std::span<const char*> extSpan(glfwExtensions, glfwExtensionCount);
extensions.assign(extSpan.begin(), extSpan.end());
}
if (enableValidationLayers)
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
return extensions;
}
static VKAPI_ATTR fn VKAPI_CALL debugCallback(
// Severity - verbose, info, warning, error
VkDebugUtilsMessageSeverityFlagBitsEXT /*messageSeverity*/,
// Message Type - general, validation, performance
VkDebugUtilsMessageTypeFlagsEXT /*messageType*/,
// Callback Data - Contains details of the message
// * pMessage - The message as a null-terminated string
// * pObjects - Array of related objects
// * objectCount - Number of related objects
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
// User Data - Any extra data the user may want to pass to the function
void* /*pUserData*/
) -> VkBool32 /* If true, abort the call */ {
std::cerr << "validation layer: " << pCallbackData->pMessage << std::endl;
return VK_FALSE;
}
fn createInstance() -> void { fn createInstance() -> void {
if (enableValidationLayers && !checkValidationLayerSupport()) if (enableValidationLayers && !checkValidationLayerSupport())
throw std::runtime_error("Validation layers requested, but not available!"); throw std::runtime_error("Validation layers requested, but not available!");
// General Metadata
VkApplicationInfo appInfo {}; VkApplicationInfo appInfo {};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; // Used for pNext appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; // Used for pNext
appInfo.pApplicationName = "Hello Triangle"; appInfo.pApplicationName = "Hello Triangle";
@ -81,33 +152,38 @@ class Application {
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0); appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_0; appInfo.apiVersion = VK_API_VERSION_1_0;
uint32_t glfwExtensionCount = 0; // Information used by vkCreateInstance
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
VkInstanceCreateInfo createInfo {}; VkInstanceCreateInfo createInfo {};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo; createInfo.pApplicationInfo = &appInfo;
createInfo.enabledExtensionCount = glfwExtensionCount;
createInfo.ppEnabledExtensionNames = glfwExtensions; VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo {};
if (enableValidationLayers) { if (enableValidationLayers) {
createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size()); createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
createInfo.ppEnabledLayerNames = validationLayers.data(); createInfo.ppEnabledLayerNames = validationLayers.data();
populateDebugMessengerCreateInfo(debugCreateInfo);
createInfo.pNext = (VkDebugUtilsMessengerCreateInfoEXT*)&debugCreateInfo;
} else { } else {
createInfo.enabledLayerCount = 0; createInfo.enabledLayerCount = 0;
createInfo.pNext = nullptr;
} }
std::vector<const char*> requiredExtensions; // Fixes macOS crashes
std::vector<const char*> extensions = getRequiredExtensions();
requiredExtensions.emplace_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME); extensions.emplace_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
createInfo.flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; createInfo.flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
createInfo.enabledExtensionCount = static_cast<u32>(extensions.size());
createInfo.ppEnabledExtensionNames = extensions.data();
createInfo.enabledExtensionCount = static_cast<u32>(requiredExtensions.size()); // Finally, create the instance (and throw an error if it fails)
createInfo.ppEnabledExtensionNames = requiredExtensions.data(); if (auto result = vkCreateInstance(&createInfo, nullptr, &mInstance); result != VK_SUCCESS)
throw std::runtime_error(
if (vkCreateInstance(&createInfo, nullptr, &mInstance) != VK_SUCCESS) string("Failed to create instance! Error: ") + string(magic_enum::enum_name(result))
throw std::runtime_error("failed to create instance!"); );
} }
fn initWindow() -> void { fn initWindow() -> void {
@ -123,13 +199,43 @@ class Application {
mWindow = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); mWindow = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
} }
fn initVulkan() -> void { createInstance(); } fn initVulkan() -> void {
createInstance();
setupDebugMessenger();
}
static fn populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) -> void {
createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
createInfo.pfnUserCallback = debugCallback;
}
fn setupDebugMessenger() -> void {
if (!enableValidationLayers)
return;
VkDebugUtilsMessengerCreateInfoEXT createInfo;
populateDebugMessengerCreateInfo(createInfo);
if (CreateDebugUtilsMessengerEXT(mInstance, &createInfo, nullptr, &mDebugMessenger) != VK_SUCCESS) {
throw std::runtime_error("Failed to set up debug messenger!");
}
}
fn mainLoop() -> void { fn mainLoop() -> void {
while (!glfwWindowShouldClose(mWindow)) glfwPollEvents(); while (!glfwWindowShouldClose(mWindow)) glfwPollEvents();
} }
fn cleanup() -> void { fn cleanup() -> void {
if (enableValidationLayers)
DestroyDebugUtilsMessengerEXT(mInstance, mDebugMessenger, nullptr);
vkDestroyInstance(mInstance, nullptr); vkDestroyInstance(mInstance, nullptr);
glfwDestroyWindow(mWindow); glfwDestroyWindow(mWindow);
glfwTerminate(); glfwTerminate();

1709
src/util/magic_enum.hpp Normal file

File diff suppressed because it is too large Load diff