로깅과 vulkan validation layer 설정 끝

This commit is contained in:
2025-04-08 19:37:48 +09:00
parent 6647a5c2fc
commit f5ba3795f0
6 changed files with 68 additions and 9 deletions

View File

@@ -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)

View File

@@ -10,3 +10,4 @@
#include <string_view>
#include "utilities.h"
#include "spdlog/spdlog.h"

View File

@@ -11,6 +11,7 @@ class Graphics final {
void InitializeVulkan();
void CreateInstance();
void SetupDebugMessenger();
std::vector<gsl::czstring> GetRequiredInstanceExtentions();
@@ -22,6 +23,7 @@ class Graphics final {
static bool AreAllLayersSupported(gsl::span<gsl::czstring> extensions);
VkInstance instance_ = nullptr;
VkDebugUtilsMessengerEXT debug_messenger_;
gsl::not_null<Window *> window_;
bool validation_enabled_ = false;
};

View File

@@ -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

View File

@@ -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();
}

View File

@@ -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<PFN_vkCreateDebugUtilsMessengerEXT>(
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<PFN_vkDestroyDebugUtilsMessengerEXT>(
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_(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<gsl::czstring, 1> 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<gsl::czstring> Graphics::GetSuggestedInstanceExtentions() {
std::uint32_t glfw_extention_count = 0;
gsl::czstring* glfw_extentions =