| 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 | |
| 31 | #include "openxr_debug_utils_extension.h" |
| 32 | |
| 33 | #include "../openxr_api.h" |
| 34 | #include "core/config/project_settings.h" |
| 35 | #include "core/string/print_string.h" |
| 36 | |
| 37 | #include <openxr/openxr.h> |
| 38 | |
| 39 | OpenXRDebugUtilsExtension *OpenXRDebugUtilsExtension::singleton = nullptr; |
| 40 | |
| 41 | OpenXRDebugUtilsExtension *OpenXRDebugUtilsExtension::get_singleton() { |
| 42 | return singleton; |
| 43 | } |
| 44 | |
| 45 | OpenXRDebugUtilsExtension::OpenXRDebugUtilsExtension() { |
| 46 | singleton = this; |
| 47 | } |
| 48 | |
| 49 | OpenXRDebugUtilsExtension::~OpenXRDebugUtilsExtension() { |
| 50 | singleton = nullptr; |
| 51 | } |
| 52 | |
| 53 | HashMap<String, bool *> OpenXRDebugUtilsExtension::get_requested_extensions(XrVersion p_version) { |
| 54 | HashMap<String, bool *> request_extensions; |
| 55 | |
| 56 | request_extensions[XR_EXT_DEBUG_UTILS_EXTENSION_NAME] = &debug_utils_ext; |
| 57 | |
| 58 | return request_extensions; |
| 59 | } |
| 60 | |
| 61 | void OpenXRDebugUtilsExtension::on_instance_created(const XrInstance p_instance) { |
| 62 | if (debug_utils_ext) { |
| 63 | EXT_INIT_XR_FUNC(xrCreateDebugUtilsMessengerEXT); |
| 64 | EXT_INIT_XR_FUNC(xrDestroyDebugUtilsMessengerEXT); |
| 65 | EXT_INIT_XR_FUNC(xrSetDebugUtilsObjectNameEXT); |
| 66 | EXT_INIT_XR_FUNC(xrSessionBeginDebugUtilsLabelRegionEXT); |
| 67 | EXT_INIT_XR_FUNC(xrSessionEndDebugUtilsLabelRegionEXT); |
| 68 | EXT_INIT_XR_FUNC(xrSessionInsertDebugUtilsLabelEXT); |
| 69 | |
| 70 | debug_utils_ext = xrCreateDebugUtilsMessengerEXT_ptr && xrDestroyDebugUtilsMessengerEXT_ptr && xrSetDebugUtilsObjectNameEXT_ptr && xrSessionBeginDebugUtilsLabelRegionEXT_ptr && xrSessionEndDebugUtilsLabelRegionEXT_ptr && xrSessionInsertDebugUtilsLabelEXT_ptr; |
| 71 | } else { |
| 72 | WARN_PRINT("OpenXR: The debug utils extension is not available on this runtime. Debug logging is not enabled!"); |
| 73 | } |
| 74 | |
| 75 | // On successful init, setup our default messenger. |
| 76 | if (debug_utils_ext) { |
| 77 | int max_severity = GLOBAL_GET("xr/openxr/extensions/debug_utils"); |
| 78 | int types = GLOBAL_GET("xr/openxr/extensions/debug_message_types"); |
| 79 | |
| 80 | XrDebugUtilsMessageSeverityFlagsEXT message_severities = 0; |
| 81 | |
| 82 | if (max_severity >= 1) { |
| 83 | message_severities |= XR_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; |
| 84 | } |
| 85 | if (max_severity >= 2) { |
| 86 | message_severities |= XR_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT; |
| 87 | } |
| 88 | if (max_severity >= 3) { |
| 89 | message_severities |= XR_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT; |
| 90 | } |
| 91 | if (max_severity >= 4) { |
| 92 | message_severities |= XR_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT; |
| 93 | } |
| 94 | |
| 95 | XrDebugUtilsMessageTypeFlagsEXT message_types = 0; |
| 96 | |
| 97 | // These should match up but just to be safe and future proof... |
| 98 | if (types & 1) { |
| 99 | message_types |= XR_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT; |
| 100 | } |
| 101 | if (types & 2) { |
| 102 | message_types |= XR_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT; |
| 103 | } |
| 104 | if (types & 4) { |
| 105 | message_types |= XR_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT; |
| 106 | } |
| 107 | if (types & 8) { |
| 108 | message_types |= XR_DEBUG_UTILS_MESSAGE_TYPE_CONFORMANCE_BIT_EXT; |
| 109 | } |
| 110 | |
| 111 | XrDebugUtilsMessengerCreateInfoEXT callback_info = { |
| 112 | XR_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT, // type |
| 113 | nullptr, // next |
| 114 | message_severities, // messageSeverities |
| 115 | message_types, // messageTypes |
| 116 | &OpenXRDebugUtilsExtension::_debug_callback, // userCallback |
| 117 | nullptr, // userData |
| 118 | }; |
| 119 | |
| 120 | XrResult result = xrCreateDebugUtilsMessengerEXT(p_instance, &callback_info, &default_messenger); |
| 121 | if (XR_FAILED(result)) { |
| 122 | ERR_PRINT("OpenXR: Failed to create debug callback [" + OpenXRAPI::get_singleton()->get_error_string(result) + "]"); |
| 123 | } |
| 124 | |
| 125 | set_object_name(XR_OBJECT_TYPE_INSTANCE, uint64_t(p_instance), "Main Godot OpenXR Instance"); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void OpenXRDebugUtilsExtension::on_instance_destroyed() { |
| 130 | if (default_messenger != XR_NULL_HANDLE) { |
| 131 | XrResult result = xrDestroyDebugUtilsMessengerEXT(default_messenger); |
| 132 | if (XR_FAILED(result)) { |
| 133 | ERR_PRINT("OpenXR: Failed to destroy debug callback [" + OpenXRAPI::get_singleton()->get_error_string(result) + "]"); |
| 134 | } |
| 135 | |
| 136 | default_messenger = XR_NULL_HANDLE; |
| 137 | } |
| 138 | |
| 139 | xrCreateDebugUtilsMessengerEXT_ptr = nullptr; |
| 140 | xrDestroyDebugUtilsMessengerEXT_ptr = nullptr; |
| 141 | xrSetDebugUtilsObjectNameEXT_ptr = nullptr; |
| 142 | xrSessionBeginDebugUtilsLabelRegionEXT_ptr = nullptr; |
| 143 | xrSessionEndDebugUtilsLabelRegionEXT_ptr = nullptr; |
| 144 | xrSessionInsertDebugUtilsLabelEXT_ptr = nullptr; |
| 145 | debug_utils_ext = false; |
| 146 | } |
| 147 | |
| 148 | bool OpenXRDebugUtilsExtension::get_active() { |
| 149 | return debug_utils_ext; |
| 150 | } |
| 151 | |
| 152 | void OpenXRDebugUtilsExtension::set_object_name(XrObjectType p_object_type, uint64_t p_object_handle, const char *p_object_name) { |
| 153 | ERR_FAIL_COND(!debug_utils_ext); |
| 154 | ERR_FAIL_NULL(xrSetDebugUtilsObjectNameEXT_ptr); |
| 155 | |
| 156 | const XrDebugUtilsObjectNameInfoEXT space_name_info = { |
| 157 | XR_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT, // type |
| 158 | nullptr, // next |
| 159 | p_object_type, // objectType |
| 160 | p_object_handle, // objectHandle |
| 161 | p_object_name, // objectName |
| 162 | }; |
| 163 | |
| 164 | XrResult result = xrSetDebugUtilsObjectNameEXT_ptr(OpenXRAPI::get_singleton()->get_instance(), &space_name_info); |
| 165 | if (XR_FAILED(result)) { |
| 166 | ERR_PRINT("OpenXR: Failed to set object name [" + OpenXRAPI::get_singleton()->get_error_string(result) + "]"); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void OpenXRDebugUtilsExtension::begin_debug_label_region(const char *p_label_name) { |
| 171 | ERR_FAIL_COND(!debug_utils_ext); |
| 172 | ERR_FAIL_NULL(xrSessionBeginDebugUtilsLabelRegionEXT_ptr); |
| 173 | |
| 174 | const XrDebugUtilsLabelEXT session_active_region_label = { |
| 175 | XR_TYPE_DEBUG_UTILS_LABEL_EXT, // type |
| 176 | nullptr, // next |
| 177 | p_label_name, // labelName |
| 178 | }; |
| 179 | |
| 180 | XrResult result = xrSessionBeginDebugUtilsLabelRegionEXT_ptr(OpenXRAPI::get_singleton()->get_session(), &session_active_region_label); |
| 181 | if (XR_FAILED(result)) { |
| 182 | ERR_PRINT("OpenXR: Failed to begin label region [" + OpenXRAPI::get_singleton()->get_error_string(result) + "]"); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | void OpenXRDebugUtilsExtension::end_debug_label_region() { |
| 187 | ERR_FAIL_COND(!debug_utils_ext); |
| 188 | ERR_FAIL_NULL(xrSessionEndDebugUtilsLabelRegionEXT_ptr); |
| 189 | |
| 190 | XrResult result = xrSessionEndDebugUtilsLabelRegionEXT_ptr(OpenXRAPI::get_singleton()->get_session()); |
| 191 | if (XR_FAILED(result)) { |
| 192 | ERR_PRINT("OpenXR: Failed to end label region [" + OpenXRAPI::get_singleton()->get_error_string(result) + "]"); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void OpenXRDebugUtilsExtension::insert_debug_label(const char *p_label_name) { |
| 197 | ERR_FAIL_COND(!debug_utils_ext); |
| 198 | ERR_FAIL_NULL(xrSessionInsertDebugUtilsLabelEXT_ptr); |
| 199 | |
| 200 | const XrDebugUtilsLabelEXT session_active_region_label = { |
| 201 | XR_TYPE_DEBUG_UTILS_LABEL_EXT, // type |
| 202 | nullptr, // next |
| 203 | p_label_name, // labelName |
| 204 | }; |
| 205 | |
| 206 | XrResult result = xrSessionInsertDebugUtilsLabelEXT_ptr(OpenXRAPI::get_singleton()->get_session(), &session_active_region_label); |
| 207 | if (XR_FAILED(result)) { |
| 208 | ERR_PRINT("OpenXR: Failed to insert label [" + OpenXRAPI::get_singleton()->get_error_string(result) + "]"); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | XrBool32 XRAPI_PTR OpenXRDebugUtilsExtension::_debug_callback(XrDebugUtilsMessageSeverityFlagsEXT p_message_severity, XrDebugUtilsMessageTypeFlagsEXT p_message_types, const XrDebugUtilsMessengerCallbackDataEXT *p_callback_data, void *p_user_data) { |
| 213 | OpenXRDebugUtilsExtension *debug_utils = OpenXRDebugUtilsExtension::get_singleton(); |
| 214 | |
| 215 | if (debug_utils) { |
| 216 | return debug_utils->debug_callback(p_message_severity, p_message_types, p_callback_data, p_user_data); |
| 217 | } |
| 218 | |
| 219 | return XR_FALSE; |
| 220 | } |
| 221 | |
| 222 | XrBool32 OpenXRDebugUtilsExtension::debug_callback(XrDebugUtilsMessageSeverityFlagsEXT p_message_severity, XrDebugUtilsMessageTypeFlagsEXT p_message_types, const XrDebugUtilsMessengerCallbackDataEXT *p_callback_data, void *p_user_data) { |
| 223 | String msg; |
| 224 | |
| 225 | ERR_FAIL_NULL_V(p_callback_data, XR_FALSE); |
| 226 | |
| 227 | if (p_message_types == XR_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) { |
| 228 | msg = ", type: General"; |
| 229 | } else if (p_message_types == XR_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) { |
| 230 | msg = ", type: Validation"; |
| 231 | } else if (p_message_types == XR_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) { |
| 232 | msg = ", type: Performance"; |
| 233 | } else if (p_message_types == XR_DEBUG_UTILS_MESSAGE_TYPE_CONFORMANCE_BIT_EXT) { |
| 234 | msg = ", type: Conformance"; |
| 235 | } else { |
| 236 | msg = ", type: Unknown (" + String::num_uint64(p_message_types) + ")"; |
| 237 | } |
| 238 | |
| 239 | if (p_callback_data->functionName) { |
| 240 | msg += ", function Name: " + String(p_callback_data->functionName); |
| 241 | } |
| 242 | if (p_callback_data->messageId) { |
| 243 | msg += "\nMessage ID: " + String(p_callback_data->messageId); |
| 244 | } |
| 245 | if (p_callback_data->message) { |
| 246 | msg += "\nMessage: " + String(p_callback_data->message); |
| 247 | } |
| 248 | |
| 249 | if (p_callback_data->objectCount > 0) { |
| 250 | String objects; |
| 251 | |
| 252 | for (uint32_t i = 0; i < p_callback_data->objectCount; i++) { |
| 253 | if (!objects.is_empty()) { |
| 254 | objects += ", "; |
| 255 | } |
| 256 | objects += p_callback_data->objects[i].objectName; |
| 257 | } |
| 258 | |
| 259 | msg += "\nObjects: " + objects; |
| 260 | } |
| 261 | |
| 262 | if (p_callback_data->sessionLabelCount > 0) { |
| 263 | String labels; |
| 264 | |
| 265 | for (uint32_t i = 0; i < p_callback_data->sessionLabelCount; i++) { |
| 266 | if (!labels.is_empty()) { |
| 267 | labels += ", "; |
| 268 | } |
| 269 | labels += p_callback_data->sessionLabels[i].labelName; |
| 270 | } |
| 271 | |
| 272 | msg += "\nLabels: " + labels; |
| 273 | } |
| 274 | |
| 275 | if (p_message_severity == XR_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) { |
| 276 | ERR_PRINT("OpenXR: Severity: Error" + msg); |
| 277 | } else if (p_message_severity == XR_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) { |
| 278 | WARN_PRINT("OpenXR: Severity: Warning" + msg); |
| 279 | } else if (p_message_severity == XR_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) { |
| 280 | print_line("OpenXR: Severity: Info" + msg); |
| 281 | } else if (p_message_severity == XR_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) { |
| 282 | // This is a bit double because we won't output this unless verbose messaging in Godot is on. |
| 283 | print_verbose("OpenXR: Severity: Verbose" + msg); |
| 284 | } |
| 285 | |
| 286 | return XR_FALSE; |
| 287 | } |