SimpleGL  1.1.0
A framework for platform independent rendering
debug_report.cpp
Go to the documentation of this file.
1 
9 
10 #include <stdexcept>
11 
12 #include <SimpleGL/logging.hpp>
13 
16 
17 using namespace std;
18 
19 namespace sgl {
20  DebugReportCallback* DebugReportCallback::current = nullptr;
21 
22  DebugReportCallback* DebugReportCallback::getCurrent() {
23  return current;
24  }
25 
26  void DebugReportCallback::setCurrent(DebugReportCallback* callback) {
27  /* Only set if callback is not a nullptr */
28  if (callback != nullptr) {
29  current = callback;
30  }
31  }
32 
33  VkBool32 DebugReportCallback::dispatch(VkDebugReportFlagsEXT flags,
34  VkDebugReportObjectTypeEXT objectType,
35  uint64_t object,
36  size_t location,
37  int32_t messageCode,
38  const char* pLayerPrefix,
39  const char* pMessage,
40  void* pUserData) {
41  /* Instance shouldn't be null when calling this function */
42  if (current) {
43  string messageType = "DEBUG REPORT";
44  if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
45  messageType = "ERROR";
46  } else if (flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
47  messageType = "WARNING";
48  } else if (flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
49  messageType = "PERFORMANCE WARNING";
50  } else if (flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
51  messageType = "INFO";
52  } else if (flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
53  messageType = "DEBUG";
54  }
55  current->invoke(messageType, messageCode, string(pMessage));
56  }
57 
58  return VK_FALSE;
59  }
60 
61  DebugReportCallback::DebugReportCallback(void) {
62  Instance* instance = VulkanContext::getCurrent()->getInstance();
63 
64  /* Initialize the create info */
65  VkDebugReportCallbackCreateInfoEXT createInfo = {};
66  createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
67  createInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
68  createInfo.pfnCallback = DebugReportCallback::dispatch;
69 
70  /* Create the Vulkan debug report callback */
71  auto vkCreateDebugReportCallbackEXT = (PFN_vkCreateDebugReportCallbackEXT) instance->getProcAddr("vkCreateDebugReportCallbackEXT");
72  if (vkCreateDebugReportCallbackEXT(instance->getHandle(), &createInfo, nullptr, &handle) != VK_SUCCESS) {
73  throw runtime_error("Could not create debug report callback!");
74  }
75  }
76 
77  DebugReportCallback::~DebugReportCallback(void) {
78  Instance* instance = VulkanContext::getCurrent()->getInstance();
79 
80  /* Destroy the debug report callback */
81  auto vkDestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT) instance->getProcAddr("vkDestroyDebugReportCallbackEXT");
82  vkDestroyDebugReportCallbackEXT(instance->getHandle(), handle, nullptr);
83 
84  /* Set instance to nullptr if this was the current debug report callback */
85  if (DebugReportCallback::current == this) {
86  DebugReportCallback::current = nullptr;
87  }
88  }
89 
90  VkDebugReportCallbackEXT DebugReportCallback::getHandle() {
91  return handle;
92  }
93 
95  void DebugReportCallback::invoke(std::string messageType, int messageCode, std::string message) {
96  Logger::logError("Vulkan " + messageType + " Code " + to_string(messageCode) + ": " + message);
97  }
98 }
This class wraps a Vulkan instance.
VkInstance getHandle()
Returns the handle of the Vulkan instance.
Generic namespace for the SimpleGL framework.
Definition: application.hpp:18
This class wraps a Vulkan debug report callback.
virtual void invoke(std::string messageType, int messageCode, std::string message)
This function gets called whenever a Vulkan debug report occurs.
PFN_vkVoidFunction getProcAddr(std::string name)
Returns a function pointer for an instance function.