openxr_debug_utils_extension.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**************************************************************************/
  2. /* openxr_debug_utils_extension.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "openxr_debug_utils_extension.h"
  31. #include "../openxr_api.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/string/print_string.h"
  34. #include <openxr/openxr.h>
  35. OpenXRDebugUtilsExtension *OpenXRDebugUtilsExtension::singleton = nullptr;
  36. OpenXRDebugUtilsExtension *OpenXRDebugUtilsExtension::get_singleton() {
  37. return singleton;
  38. }
  39. OpenXRDebugUtilsExtension::OpenXRDebugUtilsExtension() {
  40. singleton = this;
  41. }
  42. OpenXRDebugUtilsExtension::~OpenXRDebugUtilsExtension() {
  43. singleton = nullptr;
  44. }
  45. HashMap<String, bool *> OpenXRDebugUtilsExtension::get_requested_extensions() {
  46. HashMap<String, bool *> request_extensions;
  47. request_extensions[XR_EXT_DEBUG_UTILS_EXTENSION_NAME] = &debug_utils_ext;
  48. return request_extensions;
  49. }
  50. void OpenXRDebugUtilsExtension::on_instance_created(const XrInstance p_instance) {
  51. if (debug_utils_ext) {
  52. EXT_INIT_XR_FUNC(xrCreateDebugUtilsMessengerEXT);
  53. EXT_INIT_XR_FUNC(xrDestroyDebugUtilsMessengerEXT);
  54. EXT_INIT_XR_FUNC(xrSetDebugUtilsObjectNameEXT);
  55. EXT_INIT_XR_FUNC(xrSessionBeginDebugUtilsLabelRegionEXT);
  56. EXT_INIT_XR_FUNC(xrSessionEndDebugUtilsLabelRegionEXT);
  57. EXT_INIT_XR_FUNC(xrSessionInsertDebugUtilsLabelEXT);
  58. debug_utils_ext = xrCreateDebugUtilsMessengerEXT_ptr && xrDestroyDebugUtilsMessengerEXT_ptr && xrSetDebugUtilsObjectNameEXT_ptr && xrSessionBeginDebugUtilsLabelRegionEXT_ptr && xrSessionEndDebugUtilsLabelRegionEXT_ptr && xrSessionInsertDebugUtilsLabelEXT_ptr;
  59. } else {
  60. WARN_PRINT("OpenXR: The debug utils extension is not available on this runtime. Debug logging is not enabled!");
  61. }
  62. // On successful init, setup our default messenger.
  63. if (debug_utils_ext) {
  64. int max_severity = GLOBAL_GET("xr/openxr/extensions/debug_utils");
  65. int types = GLOBAL_GET("xr/openxr/extensions/debug_message_types");
  66. XrDebugUtilsMessageSeverityFlagsEXT message_severities = 0;
  67. if (max_severity >= 1) {
  68. message_severities |= XR_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
  69. }
  70. if (max_severity >= 2) {
  71. message_severities |= XR_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
  72. }
  73. if (max_severity >= 3) {
  74. message_severities |= XR_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
  75. }
  76. if (max_severity >= 4) {
  77. message_severities |= XR_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
  78. }
  79. XrDebugUtilsMessageTypeFlagsEXT message_types = 0;
  80. // These should match up but just to be safe and future proof...
  81. if (types & 1) {
  82. message_types |= XR_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT;
  83. }
  84. if (types & 2) {
  85. message_types |= XR_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
  86. }
  87. if (types & 4) {
  88. message_types |= XR_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
  89. }
  90. if (types & 8) {
  91. message_types |= XR_DEBUG_UTILS_MESSAGE_TYPE_CONFORMANCE_BIT_EXT;
  92. }
  93. XrDebugUtilsMessengerCreateInfoEXT callback_info = {
  94. XR_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT, // type
  95. nullptr, // next
  96. message_severities, // messageSeverities
  97. message_types, // messageTypes
  98. &OpenXRDebugUtilsExtension::_debug_callback, // userCallback
  99. nullptr, // userData
  100. };
  101. XrResult result = xrCreateDebugUtilsMessengerEXT(p_instance, &callback_info, &default_messenger);
  102. if (XR_FAILED(result)) {
  103. ERR_PRINT("OpenXR: Failed to create debug callback [" + OpenXRAPI::get_singleton()->get_error_string(result) + "]");
  104. }
  105. set_object_name(XR_OBJECT_TYPE_INSTANCE, uint64_t(p_instance), "Main Godot OpenXR Instance");
  106. }
  107. }
  108. void OpenXRDebugUtilsExtension::on_instance_destroyed() {
  109. if (default_messenger != XR_NULL_HANDLE) {
  110. XrResult result = xrDestroyDebugUtilsMessengerEXT(default_messenger);
  111. if (XR_FAILED(result)) {
  112. ERR_PRINT("OpenXR: Failed to destroy debug callback [" + OpenXRAPI::get_singleton()->get_error_string(result) + "]");
  113. }
  114. default_messenger = XR_NULL_HANDLE;
  115. }
  116. xrCreateDebugUtilsMessengerEXT_ptr = nullptr;
  117. xrDestroyDebugUtilsMessengerEXT_ptr = nullptr;
  118. xrSetDebugUtilsObjectNameEXT_ptr = nullptr;
  119. xrSessionBeginDebugUtilsLabelRegionEXT_ptr = nullptr;
  120. xrSessionEndDebugUtilsLabelRegionEXT_ptr = nullptr;
  121. xrSessionInsertDebugUtilsLabelEXT_ptr = nullptr;
  122. debug_utils_ext = false;
  123. }
  124. bool OpenXRDebugUtilsExtension::get_active() {
  125. return debug_utils_ext;
  126. }
  127. void OpenXRDebugUtilsExtension::set_object_name(XrObjectType p_object_type, uint64_t p_object_handle, const char *p_object_name) {
  128. ERR_FAIL_COND(!debug_utils_ext);
  129. ERR_FAIL_NULL(xrSetDebugUtilsObjectNameEXT_ptr);
  130. const XrDebugUtilsObjectNameInfoEXT space_name_info = {
  131. XR_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT, // type
  132. nullptr, // next
  133. p_object_type, // objectType
  134. p_object_handle, // objectHandle
  135. p_object_name, // objectName
  136. };
  137. XrResult result = xrSetDebugUtilsObjectNameEXT_ptr(OpenXRAPI::get_singleton()->get_instance(), &space_name_info);
  138. if (XR_FAILED(result)) {
  139. ERR_PRINT("OpenXR: Failed to set object name [" + OpenXRAPI::get_singleton()->get_error_string(result) + "]");
  140. }
  141. }
  142. void OpenXRDebugUtilsExtension::begin_debug_label_region(const char *p_label_name) {
  143. ERR_FAIL_COND(!debug_utils_ext);
  144. ERR_FAIL_NULL(xrSessionBeginDebugUtilsLabelRegionEXT_ptr);
  145. const XrDebugUtilsLabelEXT session_active_region_label = {
  146. XR_TYPE_DEBUG_UTILS_LABEL_EXT, // type
  147. nullptr, // next
  148. p_label_name, // labelName
  149. };
  150. XrResult result = xrSessionBeginDebugUtilsLabelRegionEXT_ptr(OpenXRAPI::get_singleton()->get_session(), &session_active_region_label);
  151. if (XR_FAILED(result)) {
  152. ERR_PRINT("OpenXR: Failed to begin label region [" + OpenXRAPI::get_singleton()->get_error_string(result) + "]");
  153. }
  154. }
  155. void OpenXRDebugUtilsExtension::end_debug_label_region() {
  156. ERR_FAIL_COND(!debug_utils_ext);
  157. ERR_FAIL_NULL(xrSessionEndDebugUtilsLabelRegionEXT_ptr);
  158. XrResult result = xrSessionEndDebugUtilsLabelRegionEXT_ptr(OpenXRAPI::get_singleton()->get_session());
  159. if (XR_FAILED(result)) {
  160. ERR_PRINT("OpenXR: Failed to end label region [" + OpenXRAPI::get_singleton()->get_error_string(result) + "]");
  161. }
  162. }
  163. void OpenXRDebugUtilsExtension::insert_debug_label(const char *p_label_name) {
  164. ERR_FAIL_COND(!debug_utils_ext);
  165. ERR_FAIL_NULL(xrSessionInsertDebugUtilsLabelEXT_ptr);
  166. const XrDebugUtilsLabelEXT session_active_region_label = {
  167. XR_TYPE_DEBUG_UTILS_LABEL_EXT, // type
  168. nullptr, // next
  169. p_label_name, // labelName
  170. };
  171. XrResult result = xrSessionInsertDebugUtilsLabelEXT_ptr(OpenXRAPI::get_singleton()->get_session(), &session_active_region_label);
  172. if (XR_FAILED(result)) {
  173. ERR_PRINT("OpenXR: Failed to insert label [" + OpenXRAPI::get_singleton()->get_error_string(result) + "]");
  174. }
  175. }
  176. XrBool32 XRAPI_PTR OpenXRDebugUtilsExtension::_debug_callback(XrDebugUtilsMessageSeverityFlagsEXT p_message_severity, XrDebugUtilsMessageTypeFlagsEXT p_message_types, const XrDebugUtilsMessengerCallbackDataEXT *p_callback_data, void *p_user_data) {
  177. OpenXRDebugUtilsExtension *debug_utils = OpenXRDebugUtilsExtension::get_singleton();
  178. if (debug_utils) {
  179. return debug_utils->debug_callback(p_message_severity, p_message_types, p_callback_data, p_user_data);
  180. }
  181. return XR_FALSE;
  182. }
  183. XrBool32 OpenXRDebugUtilsExtension::debug_callback(XrDebugUtilsMessageSeverityFlagsEXT p_message_severity, XrDebugUtilsMessageTypeFlagsEXT p_message_types, const XrDebugUtilsMessengerCallbackDataEXT *p_callback_data, void *p_user_data) {
  184. String msg;
  185. ERR_FAIL_NULL_V(p_callback_data, XR_FALSE);
  186. if (p_message_types == XR_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) {
  187. msg = ", type: General";
  188. } else if (p_message_types == XR_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
  189. msg = ", type: Validation";
  190. } else if (p_message_types == XR_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) {
  191. msg = ", type: Performance";
  192. } else if (p_message_types == XR_DEBUG_UTILS_MESSAGE_TYPE_CONFORMANCE_BIT_EXT) {
  193. msg = ", type: Conformance";
  194. } else {
  195. msg = ", type: Unknown (" + String::num_uint64(p_message_types) + ")";
  196. }
  197. if (p_callback_data->functionName) {
  198. msg += ", function Name: " + String(p_callback_data->functionName);
  199. }
  200. if (p_callback_data->messageId) {
  201. msg += "\nMessage ID: " + String(p_callback_data->messageId);
  202. }
  203. if (p_callback_data->message) {
  204. msg += "\nMessage: " + String(p_callback_data->message);
  205. }
  206. if (p_callback_data->objectCount > 0) {
  207. String objects;
  208. for (uint32_t i = 0; i < p_callback_data->objectCount; i++) {
  209. if (!objects.is_empty()) {
  210. objects += ", ";
  211. }
  212. objects += p_callback_data->objects[i].objectName;
  213. }
  214. msg += "\nObjects: " + objects;
  215. }
  216. if (p_callback_data->sessionLabelCount > 0) {
  217. String labels;
  218. for (uint32_t i = 0; i < p_callback_data->sessionLabelCount; i++) {
  219. if (!labels.is_empty()) {
  220. labels += ", ";
  221. }
  222. labels += p_callback_data->sessionLabels[i].labelName;
  223. }
  224. msg += "\nLabels: " + labels;
  225. }
  226. if (p_message_severity == XR_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
  227. ERR_PRINT("OpenXR: Severity: Error" + msg);
  228. } else if (p_message_severity == XR_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
  229. WARN_PRINT("OpenXR: Severity: Warning" + msg);
  230. } else if (p_message_severity == XR_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
  231. print_line("OpenXR: Severity: Info" + msg);
  232. } else if (p_message_severity == XR_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
  233. // This is a bit double because we won't output this unless verbose messaging in Godot is on.
  234. print_verbose("OpenXR: Severity: Verbose" + msg);
  235. }
  236. return XR_FALSE;
  237. }