Chronos 0.0
A advanced 2D rendering and animation system
Loading...
Searching...
No Matches
validation.cpp
Go to the documentation of this file.
1/*
2Copyright (c) 2024 Rahul Satish Vadhyar
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
21*/
22
23#ifdef ENABLE_VULKAN_VALIDATION_LAYERS
24#include <iostream>
25#include "validation.hpp"
26
27bool Chronos::Engine::checkValidationLayerSupport()
28{
29 uint32_t layerCount;
30 vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
31
32 std::vector<VkLayerProperties> availableLayers(layerCount);
33 vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
34
35 for (const char* layerName : validationLayers) {
36 bool layerFound = false;
37
38 for (const auto& layerProperties : availableLayers) {
39 if (strcmp(layerName, layerProperties.layerName) == 0) {
40 layerFound = true;
41 break;
42 }
43 }
44 if (!layerFound) {
45 return false;
46 }
47 }
48 return true;
49}
50#ifndef WIN32
51#pragma GCC diagnostic push
52#pragma GCC diagnostic ignored "-Wunused-variable"
53#endif
54VKAPI_ATTR VkBool32 VKAPI_CALL Chronos::Engine::debugCallback(
55 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
56 VkDebugUtilsMessageTypeFlagsEXT messageType,
57 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void* pUserData)
58{
59 /*
60 Quick reference to vulkan error handling:
61 messageSeverity: Severity of the message
62 VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT: Diagnostic message
63 VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT: Informational message
64 like the creation of a resource
65 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT: Message about
66 behavior that is not necessarily an error, but very likely a bug in your
67 application VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT: Message about
68 behavior that is invalid and may cause crashes
69
70 messageType: Type of the message
71 VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT: Some event has happened
72 that is unrelated to the specification or performance
73 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT: Something has
74 happened that violates the specification or indicates a possible mistake
75 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT: Potential
76 non-optimal use of Vulkan
77
78 pCallbackData: Struct containing details about the message itself
79 pMessage: The debug message as a null-terminated string
80 pObjects: Array of Vulkan object handles related to the message
81 objectCount: Number of objects in array
82 pMessageIdName: Unique identifier of the message (only for
83 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) messageIdNumber: Number
84 representation of the message ID pQueueLabels: Array of VkDebugUtilsLabelEXT
85 structs related to the objects in pObjects queueLabelCount: Number of
86 elements in pQueueLabels pCmdBufLabels: Array of VkDebugUtilsLabelEXT
87 structs related to the objects in pObjects cmdBufLabelCount: Number of
88 elements in pCmdBufLabels pUserData: The pUserData value that was specified
89 when calling vkCreateDebugUtilsMessengerEXT
90 */
91 std::cerr << "validation layer: " << pCallbackData->pMessage << std::endl;
92 return VK_FALSE;
93}
94#ifndef WIN32
95#pragma GCC diagnostic pop
96#endif
97
98// function to create the debug messenger
99VkResult Chronos::Engine::CreateDebugUtilsMessengerEXT(VkInstance instance,
100 const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
101 const VkAllocationCallbacks* pAllocator,
102 VkDebugUtilsMessengerEXT* pDebugMessenger)
103{
104 auto func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(
105 instance, "vkCreateDebugUtilsMessengerEXT");
106 if (func != nullptr)
107 return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
108 else
109 return VK_ERROR_EXTENSION_NOT_PRESENT;
110}
111
112// function to destroy the debug messenger
113void Chronos::Engine::DestroyDebugUtilsMessengerEXT(VkInstance instance,
114 VkDebugUtilsMessengerEXT debugMessenger,
115 const VkAllocationCallbacks* pAllocator)
116{
117 auto func = (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(
118 instance, "vkDestroyDebugUtilsMessengerEXT");
119 if (func != nullptr)
120 func(instance, debugMessenger, pAllocator);
121}
122
123void Chronos::Engine::populateDebugMessengerCreateInfo(
124 VkDebugUtilsMessengerCreateInfoEXT& createInfo)
125{
126 createInfo = {};
127 createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
128
129 // set the severity of the messages to be sent to the callback
130 createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT
131 | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT
132 | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT
133 | VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
134
135 // set the type of messages to be sent to the callback
136 createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT
137 | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT
138 | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
139
140 // set the callback function
141 createInfo.pfnUserCallback = debugCallback;
142}
143#endif
Contains the functions needed for initalization of vulkan validation layers.