From f5ba3795f0e988de632c7a2677c4cc3702126d7f Mon Sep 17 00:00:00 2001 From: HappyTanuki Date: Tue, 8 Apr 2025 19:37:48 +0900 Subject: [PATCH] =?UTF-8?q?=EB=A1=9C=EA=B9=85=EA=B3=BC=20vulkan=20validati?= =?UTF-8?q?on=20layer=20=EC=84=A4=EC=A0=95=20=EB=81=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 9 +++++ include/precomp.h | 1 + include/vulkan/graphics.h | 2 ++ src/glfw/glfw_initialization.cpp | 7 ++++ src/main.cpp | 1 - src/vulkan/graphics.cpp | 57 +++++++++++++++++++++++++++----- 6 files changed, 68 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f400b2..d0d55c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,14 @@ FetchContent_Declare( ) FetchContent_MakeAvailable(glm) +FetchContent_Declare( + spdlog + GIT_REPOSITORY "https://github.com/gabime/spdlog.git" + GIT_TAG "v1.15.2" + GIT_SHALLOW ON +) +FetchContent_MakeAvailable(spdlog) + FetchContent_Declare( glfw GIT_REPOSITORY "https://github.com/glfw/glfw.git" @@ -40,6 +48,7 @@ target_link_libraries(VulkanEngine PRIVATE Vulkan::Vulkan) target_link_libraries(VulkanEngine PRIVATE glm) target_link_libraries(VulkanEngine PRIVATE glfw) target_link_libraries(VulkanEngine PRIVATE Microsoft.GSL::GSL) +target_link_libraries(VulkanEngine PRIVATE spdlog) target_include_directories(VulkanEngine PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include") target_compile_features(VulkanEngine PRIVATE cxx_std_20) diff --git a/include/precomp.h b/include/precomp.h index 29957a9..03052f7 100644 --- a/include/precomp.h +++ b/include/precomp.h @@ -10,3 +10,4 @@ #include #include "utilities.h" +#include "spdlog/spdlog.h" diff --git a/include/vulkan/graphics.h b/include/vulkan/graphics.h index fc95e78..ce7ff21 100644 --- a/include/vulkan/graphics.h +++ b/include/vulkan/graphics.h @@ -11,6 +11,7 @@ class Graphics final { void InitializeVulkan(); void CreateInstance(); + void SetupDebugMessenger(); std::vector GetRequiredInstanceExtentions(); @@ -22,6 +23,7 @@ class Graphics final { static bool AreAllLayersSupported(gsl::span extensions); VkInstance instance_ = nullptr; + VkDebugUtilsMessengerEXT debug_messenger_; gsl::not_null window_; bool validation_enabled_ = false; }; diff --git a/src/glfw/glfw_initialization.cpp b/src/glfw/glfw_initialization.cpp index e4dd0bd..297f4cf 100644 --- a/src/glfw/glfw_initialization.cpp +++ b/src/glfw/glfw_initialization.cpp @@ -7,8 +7,15 @@ #include "precomp.h" namespace veng { +void glfw_error_callback(std::int32_t error_code, gsl::czstring message) { + spdlog::error("Glfw Validation: {}", message); +} + GlfwInitialization::GlfwInitialization() { + glfwSetErrorCallback(glfw_error_callback); if (glfwInit() != GLFW_TRUE) std::exit(EXIT_FAILURE); } + GlfwInitialization::~GlfwInitialization() { glfwTerminate(); } + } // namespace veng diff --git a/src/main.cpp b/src/main.cpp index 229cb58..148b5f6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,7 +14,6 @@ std::int32_t main(std::int32_t argc, gsl::zstring* argv) { veng::Graphics graphics(&window); while (!window.ShouldClose()) { - glfwSwapBuffers(window.GetHandle()); glfwPollEvents(); } diff --git a/src/vulkan/graphics.cpp b/src/vulkan/graphics.cpp index 733909a..6271848 100644 --- a/src/vulkan/graphics.cpp +++ b/src/vulkan/graphics.cpp @@ -6,6 +6,29 @@ #include "precomp.h" +VKAPI_ATTR VkResult VKAPI_CALL vkCreateDebugUtilsMessengerEXT( + VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, + const VkAllocationCallbacks* pAllocator, + VkDebugUtilsMessengerEXT* pMessenger) { + PFN_vkCreateDebugUtilsMessengerEXT function = + reinterpret_cast( + vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT")); + if (function != nullptr) { + return function(instance, pCreateInfo, pAllocator, pMessenger); + } else { + return VK_ERROR_EXTENSION_NOT_PRESENT; + } +} + +VKAPI_ATTR void VKAPI_CALL vkDestroyDebugUtilsMessengerEXT( + VkInstance instance, VkDebugUtilsMessengerEXT messenger, + const VkAllocationCallbacks* pAllocator) { + PFN_vkDestroyDebugUtilsMessengerEXT function = + reinterpret_cast( + vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT")); + if (function != nullptr) function(instance, messenger, pAllocator); +} + namespace veng { static VKAPI_ATTR VkBool32 VKAPI_CALL @@ -13,11 +36,10 @@ ValidationCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageTypes, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void* pUserData) { - if (messageSeverity >= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) { - std::cerr << "Validation Error : " << pCallbackData->pMessage << std::endl; + if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) { + spdlog::warn("Vulkan Validation: {}", pCallbackData->pMessage); } else { - std::cout << "Validation Message : " << pCallbackData->pMessage - << std::endl; + spdlog::error("Vulkan Validation: {}", pCallbackData->pMessage); } return VK_FALSE; } @@ -27,8 +49,7 @@ static VkDebugUtilsMessengerCreateInfoEXT GetCreateMessengerInfo() { creation_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; creation_info.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | - VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | - VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT; + VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; creation_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT; creation_info.pfnUserCallback = ValidationCallback; @@ -45,10 +66,18 @@ Graphics::Graphics(gsl::not_null window) : window_(window) { } Graphics::~Graphics() { - if (instance_ != nullptr) vkDestroyInstance(instance_, nullptr); + if (instance_ != nullptr) { + if (debug_messenger_ != nullptr) { + vkDestroyDebugUtilsMessengerEXT(instance_, debug_messenger_, nullptr); + } + vkDestroyInstance(instance_, nullptr); + } } -void Graphics::InitializeVulkan() { CreateInstance(); } +void Graphics::InitializeVulkan() { + CreateInstance(); + SetupDebugMessenger(); +} void Graphics::CreateInstance() { std::array validation_layers = { @@ -91,6 +120,18 @@ void Graphics::CreateInstance() { if (result != VK_SUCCESS) std::exit(EXIT_FAILURE); } +void Graphics::SetupDebugMessenger() { + if (!validation_enabled_) return; + + VkDebugUtilsMessengerCreateInfoEXT info = GetCreateMessengerInfo(); + VkResult result = vkCreateDebugUtilsMessengerEXT(instance_, &info, nullptr, + &debug_messenger_); + if (result != VK_SUCCESS) { + spdlog::error("Cannot create debug messenger"); + return; + } +} + gsl::span Graphics::GetSuggestedInstanceExtentions() { std::uint32_t glfw_extention_count = 0; gsl::czstring* glfw_extentions =