| 1 | /**************************************************************************/ |
| 2 | /* openxr_spatial_anchor.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_spatial_anchor.h" |
| 32 | |
| 33 | #include "../../openxr_api.h" |
| 34 | #include "../../openxr_util.h" |
| 35 | #include "../openxr_future_extension.h" |
| 36 | #include "core/config/project_settings.h" |
| 37 | #include "servers/xr/xr_server.h" |
| 38 | |
| 39 | //////////////////////////////////////////////////////////////////////////// |
| 40 | // OpenXRSpatialCapabilityConfigurationAnchor |
| 41 | |
| 42 | void OpenXRSpatialCapabilityConfigurationAnchor::_bind_methods() { |
| 43 | ClassDB::bind_method(D_METHOD("get_enabled_components"), &OpenXRSpatialCapabilityConfigurationAnchor::_get_enabled_components); |
| 44 | } |
| 45 | |
| 46 | bool OpenXRSpatialCapabilityConfigurationAnchor::has_valid_configuration() const { |
| 47 | OpenXRSpatialAnchorCapability *capability = OpenXRSpatialAnchorCapability::get_singleton(); |
| 48 | ERR_FAIL_NULL_V(capability, false); |
| 49 | |
| 50 | return capability->is_spatial_anchor_supported(); |
| 51 | } |
| 52 | |
| 53 | XrSpatialCapabilityConfigurationBaseHeaderEXT *OpenXRSpatialCapabilityConfigurationAnchor::get_configuration() { |
| 54 | OpenXRSpatialAnchorCapability *capability = OpenXRSpatialAnchorCapability::get_singleton(); |
| 55 | ERR_FAIL_NULL_V(capability, nullptr); |
| 56 | |
| 57 | if (capability->is_spatial_anchor_supported()) { |
| 58 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 59 | ERR_FAIL_NULL_V(se_extension, nullptr); |
| 60 | |
| 61 | anchor_enabled_components.clear(); |
| 62 | |
| 63 | // Guaranteed components: |
| 64 | anchor_enabled_components.push_back(XR_SPATIAL_COMPONENT_TYPE_ANCHOR_EXT); |
| 65 | |
| 66 | // enable optional components |
| 67 | if (capability->is_spatial_persistence_supported()) { |
| 68 | anchor_enabled_components.push_back(XR_SPATIAL_COMPONENT_TYPE_PERSISTENCE_EXT); |
| 69 | } |
| 70 | |
| 71 | anchor_config.enabledComponentCount = anchor_enabled_components.size(); |
| 72 | anchor_config.enabledComponents = anchor_enabled_components.ptr(); |
| 73 | |
| 74 | // and return this. |
| 75 | return (XrSpatialCapabilityConfigurationBaseHeaderEXT *)&anchor_config; |
| 76 | } |
| 77 | |
| 78 | return nullptr; |
| 79 | } |
| 80 | |
| 81 | PackedInt64Array OpenXRSpatialCapabilityConfigurationAnchor::_get_enabled_components() const { |
| 82 | PackedInt64Array components; |
| 83 | |
| 84 | for (const XrSpatialComponentTypeEXT &component_type : anchor_enabled_components) { |
| 85 | components.push_back((int64_t)component_type); |
| 86 | } |
| 87 | |
| 88 | return components; |
| 89 | } |
| 90 | |
| 91 | //////////////////////////////////////////////////////////////////////////// |
| 92 | // OpenXRSpatialComponentAnchorList |
| 93 | |
| 94 | void OpenXRSpatialComponentAnchorList::_bind_methods() { |
| 95 | ClassDB::bind_method(D_METHOD("get_entity_pose", "index"), &OpenXRSpatialComponentAnchorList::get_entity_pose); |
| 96 | } |
| 97 | |
| 98 | void OpenXRSpatialComponentAnchorList::set_capacity(uint32_t p_capacity) { |
| 99 | entity_poses.resize(p_capacity); |
| 100 | |
| 101 | anchor_list.locationCount = uint32_t(entity_poses.size()); |
| 102 | anchor_list.locations = entity_poses.ptrw(); |
| 103 | } |
| 104 | |
| 105 | XrSpatialComponentTypeEXT OpenXRSpatialComponentAnchorList::get_component_type() const { |
| 106 | return XR_SPATIAL_COMPONENT_TYPE_ANCHOR_EXT; |
| 107 | } |
| 108 | |
| 109 | void *OpenXRSpatialComponentAnchorList::get_structure_data(void *p_next) { |
| 110 | anchor_list.next = p_next; |
| 111 | return &anchor_list; |
| 112 | } |
| 113 | |
| 114 | Transform3D OpenXRSpatialComponentAnchorList::get_entity_pose(int64_t p_index) const { |
| 115 | ERR_FAIL_INDEX_V(p_index, entity_poses.size(), Transform3D()); |
| 116 | |
| 117 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 118 | ERR_FAIL_NULL_V(openxr_api, Transform3D()); |
| 119 | |
| 120 | return openxr_api->transform_from_pose(entity_poses[p_index]); |
| 121 | } |
| 122 | |
| 123 | //////////////////////////////////////////////////////////////////////////// |
| 124 | // OpenXRSpatialContextPersistenceConfig |
| 125 | |
| 126 | void OpenXRSpatialContextPersistenceConfig::_bind_methods() { |
| 127 | ClassDB::bind_method(D_METHOD("add_persistence_context", "persistence_context"), &OpenXRSpatialContextPersistenceConfig::add_persistence_context); |
| 128 | ClassDB::bind_method(D_METHOD("remove_persistence_context", "persistence_context"), &OpenXRSpatialContextPersistenceConfig::remove_persistence_context); |
| 129 | } |
| 130 | |
| 131 | bool OpenXRSpatialContextPersistenceConfig::has_valid_configuration() const { |
| 132 | OpenXRSpatialAnchorCapability *capability = OpenXRSpatialAnchorCapability::get_singleton(); |
| 133 | ERR_FAIL_NULL_V(capability, false); |
| 134 | |
| 135 | if (!capability->is_spatial_persistence_supported()) { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | // Check if we have a valid config. |
| 140 | if (persistence_contexts.is_empty()) { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | void *OpenXRSpatialContextPersistenceConfig::get_header(void *p_next) { |
| 148 | void *n = p_next; |
| 149 | if (get_next().is_valid()) { |
| 150 | n = get_next()->get_header(n); |
| 151 | } |
| 152 | |
| 153 | if (has_valid_configuration()) { |
| 154 | OpenXRSpatialAnchorCapability *anchor_capability = OpenXRSpatialAnchorCapability::get_singleton(); |
| 155 | ERR_FAIL_NULL_V(anchor_capability, nullptr); |
| 156 | |
| 157 | // Prepare our buffer. |
| 158 | context_handles.resize(persistence_contexts.size()); |
| 159 | |
| 160 | // Copy our handles. |
| 161 | XrSpatialPersistenceContextEXT *ptr = context_handles.ptrw(); |
| 162 | int i = 0; |
| 163 | for (const RID &rid : persistence_contexts) { |
| 164 | ptr[i++] = anchor_capability->get_persistence_context_handle(rid); |
| 165 | } |
| 166 | |
| 167 | persistence_config.next = n; |
| 168 | persistence_config.persistenceContextCount = (uint32_t)context_handles.size(); |
| 169 | persistence_config.persistenceContexts = context_handles.ptr(); |
| 170 | |
| 171 | // and return this. |
| 172 | return (XrSpatialCapabilityConfigurationBaseHeaderEXT *)&persistence_config; |
| 173 | } |
| 174 | |
| 175 | return n; |
| 176 | } |
| 177 | |
| 178 | XrStructureType OpenXRSpatialContextPersistenceConfig::get_structure_type() { |
| 179 | return XR_TYPE_SPATIAL_CONTEXT_PERSISTENCE_CONFIG_EXT; |
| 180 | } |
| 181 | |
| 182 | void OpenXRSpatialContextPersistenceConfig::add_persistence_context(RID p_persistence_context) { |
| 183 | ERR_FAIL_COND(persistence_contexts.has(p_persistence_context)); |
| 184 | |
| 185 | persistence_contexts.push_back(p_persistence_context); |
| 186 | } |
| 187 | |
| 188 | void OpenXRSpatialContextPersistenceConfig::remove_persistence_context(RID p_persistence_context) { |
| 189 | ERR_FAIL_COND(!persistence_contexts.has(p_persistence_context)); |
| 190 | |
| 191 | persistence_contexts.erase(p_persistence_context); |
| 192 | } |
| 193 | |
| 194 | //////////////////////////////////////////////////////////////////////////// |
| 195 | // OpenXRSpatialComponentPersistenceList |
| 196 | |
| 197 | void OpenXRSpatialComponentPersistenceList::_bind_methods() { |
| 198 | ClassDB::bind_method(D_METHOD("get_persistent_uuid", "index"), &OpenXRSpatialComponentPersistenceList::_get_persistent_uuid); |
| 199 | ClassDB::bind_method(D_METHOD("get_persistent_state", "index"), &OpenXRSpatialComponentPersistenceList::_get_persistent_state); |
| 200 | } |
| 201 | |
| 202 | void OpenXRSpatialComponentPersistenceList::set_capacity(uint32_t p_capacity) { |
| 203 | persist_data.resize(p_capacity); |
| 204 | |
| 205 | persistence_list.persistDataCount = uint32_t(persist_data.size()); |
| 206 | persistence_list.persistData = persist_data.ptrw(); |
| 207 | } |
| 208 | |
| 209 | XrSpatialComponentTypeEXT OpenXRSpatialComponentPersistenceList::get_component_type() const { |
| 210 | return XR_SPATIAL_COMPONENT_TYPE_PERSISTENCE_EXT; |
| 211 | } |
| 212 | |
| 213 | void *OpenXRSpatialComponentPersistenceList::get_structure_data(void *p_next) { |
| 214 | persistence_list.next = p_next; |
| 215 | return &persistence_list; |
| 216 | } |
| 217 | |
| 218 | XrUuid OpenXRSpatialComponentPersistenceList::get_persistent_uuid(int64_t p_index) const { |
| 219 | XrUuid null_uuid = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 220 | |
| 221 | ERR_FAIL_INDEX_V(p_index, persist_data.size(), null_uuid); |
| 222 | |
| 223 | return persist_data[p_index].persistUuid; |
| 224 | } |
| 225 | |
| 226 | String OpenXRSpatialComponentPersistenceList::_get_persistent_uuid(int64_t p_index) const { |
| 227 | return OpenXRUtil::string_from_xruuid(get_persistent_uuid(p_index)); |
| 228 | } |
| 229 | |
| 230 | XrSpatialPersistenceStateEXT OpenXRSpatialComponentPersistenceList::get_persistent_state(int64_t p_index) const { |
| 231 | ERR_FAIL_INDEX_V(p_index, persist_data.size(), XR_SPATIAL_PERSISTENCE_STATE_MAX_ENUM_EXT); |
| 232 | |
| 233 | return persist_data[p_index].persistState; |
| 234 | } |
| 235 | |
| 236 | uint64_t OpenXRSpatialComponentPersistenceList::_get_persistent_state(int64_t p_index) const { |
| 237 | // TODO make a Godot constant that mirrors XrSpatialPersistenceStateEXT and return that |
| 238 | return (uint64_t)get_persistent_state(p_index); |
| 239 | } |
| 240 | |
| 241 | String OpenXRSpatialComponentPersistenceList::get_persistence_state_name(XrSpatialPersistenceStateEXT p_state) { |
| 242 | XR_ENUM_SWITCH(XrSpatialPersistenceStateEXT, p_state) |
| 243 | } |
| 244 | |
| 245 | //////////////////////////////////////////////////////////////////////////// |
| 246 | // OpenXRAnchorTracker |
| 247 | |
| 248 | void OpenXRAnchorTracker::_bind_methods() { |
| 249 | ClassDB::bind_method(D_METHOD("has_uuid"), &OpenXRAnchorTracker::has_uuid); |
| 250 | ClassDB::bind_method(D_METHOD("set_uuid", "uuid"), &OpenXRAnchorTracker::_set_uuid); |
| 251 | ClassDB::bind_method(D_METHOD("get_uuid"), &OpenXRAnchorTracker::_get_uuid); |
| 252 | |
| 253 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "uuid"), "set_uuid", "get_uuid"); |
| 254 | |
| 255 | ADD_SIGNAL(MethodInfo("uuid_changed")); |
| 256 | } |
| 257 | |
| 258 | bool OpenXRAnchorTracker::has_uuid() const { |
| 259 | for (int i = 0; i < XR_UUID_SIZE; i++) { |
| 260 | if (uuid.data[i] != 0) { |
| 261 | return true; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | XrUuid OpenXRAnchorTracker::get_uuid() const { |
| 269 | return uuid; |
| 270 | } |
| 271 | |
| 272 | void OpenXRAnchorTracker::set_uuid(const XrUuid &p_uuid) { |
| 273 | if (uuid_is_equal(uuid, p_uuid)) { |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | uuid = p_uuid; |
| 278 | |
| 279 | emit_signal(SNAME("uuid_changed")); |
| 280 | } |
| 281 | |
| 282 | String OpenXRAnchorTracker::_get_uuid() const { |
| 283 | return OpenXRUtil::string_from_xruuid(uuid); |
| 284 | } |
| 285 | |
| 286 | void OpenXRAnchorTracker::_set_uuid(const String &p_uuid) { |
| 287 | set_uuid(OpenXRUtil::xruuid_from_string(p_uuid)); |
| 288 | } |
| 289 | |
| 290 | bool OpenXRAnchorTracker::uuid_is_equal(const XrUuid &p_a, const XrUuid &p_b) { |
| 291 | for (int i = 0; i < XR_UUID_SIZE; i++) { |
| 292 | if (p_a.data[i] != p_b.data[i]) { |
| 293 | return false; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | //////////////////////////////////////////////////////////////////////////// |
| 301 | // OpenXRSpatialAnchorCapability |
| 302 | |
| 303 | OpenXRSpatialAnchorCapability *OpenXRSpatialAnchorCapability::singleton = nullptr; |
| 304 | |
| 305 | OpenXRSpatialAnchorCapability *OpenXRSpatialAnchorCapability::get_singleton() { |
| 306 | return singleton; |
| 307 | } |
| 308 | |
| 309 | OpenXRSpatialAnchorCapability::OpenXRSpatialAnchorCapability() { |
| 310 | singleton = this; |
| 311 | } |
| 312 | |
| 313 | OpenXRSpatialAnchorCapability::~OpenXRSpatialAnchorCapability() { |
| 314 | singleton = nullptr; |
| 315 | } |
| 316 | |
| 317 | void OpenXRSpatialAnchorCapability::_bind_methods() { |
| 318 | ClassDB::bind_method(D_METHOD("is_spatial_anchor_supported"), &OpenXRSpatialAnchorCapability::is_spatial_anchor_supported); |
| 319 | ClassDB::bind_method(D_METHOD("is_spatial_persistence_supported"), &OpenXRSpatialAnchorCapability::is_spatial_persistence_supported); |
| 320 | |
| 321 | ClassDB::bind_method(D_METHOD("is_persistence_scope_supported", "scope"), &OpenXRSpatialAnchorCapability::_is_persistence_scope_supported); |
| 322 | ClassDB::bind_method(D_METHOD("create_persistence_context", "scope", "user_callback"), &OpenXRSpatialAnchorCapability::_create_persistence_context, DEFVAL(Callable())); |
| 323 | ClassDB::bind_method(D_METHOD("get_persistence_context_handle", "persistence_context"), &OpenXRSpatialAnchorCapability::_get_persistence_context_handle); |
| 324 | ClassDB::bind_method(D_METHOD("free_persistence_context", "persistence_context"), &OpenXRSpatialAnchorCapability::free_persistence_context); |
| 325 | |
| 326 | ClassDB::bind_method(D_METHOD("create_new_anchor", "transform", "spatial_context"), &OpenXRSpatialAnchorCapability::create_new_anchor, DEFVAL(RID())); |
| 327 | ClassDB::bind_method(D_METHOD("remove_anchor", "anchor_tracker"), &OpenXRSpatialAnchorCapability::remove_anchor); |
| 328 | ClassDB::bind_method(D_METHOD("persist_anchor", "anchor_tracker", "persistence_context", "user_callback"), &OpenXRSpatialAnchorCapability::persist_anchor, DEFVAL(RID()), DEFVAL(Callable())); |
| 329 | ClassDB::bind_method(D_METHOD("unpersist_anchor", "anchor_tracker", "persistence_context", "user_callback"), &OpenXRSpatialAnchorCapability::unpersist_anchor, DEFVAL(RID()), DEFVAL(Callable())); |
| 330 | |
| 331 | BIND_ENUM_CONSTANT(PERSISTENCE_SCOPE_SYSTEM_MANAGED); |
| 332 | BIND_ENUM_CONSTANT(PERSISTENCE_SCOPE_LOCAL_ANCHORS); |
| 333 | } |
| 334 | |
| 335 | HashMap<String, bool *> OpenXRSpatialAnchorCapability::get_requested_extensions(XrVersion p_version) { |
| 336 | HashMap<String, bool *> request_extensions; |
| 337 | |
| 338 | if (GLOBAL_GET_CACHED(bool, "xr/openxr/extensions/spatial_entity/enabled") && GLOBAL_GET_CACHED(bool, "xr/openxr/extensions/spatial_entity/enable_spatial_anchors")) { |
| 339 | request_extensions[XR_EXT_SPATIAL_ANCHOR_EXTENSION_NAME] = &spatial_anchor_ext; |
| 340 | if (GLOBAL_GET_CACHED(bool, "xr/openxr/extensions/spatial_entity/enable_persistent_anchors")) { |
| 341 | request_extensions[XR_EXT_SPATIAL_PERSISTENCE_EXTENSION_NAME] = &spatial_persistence_ext; |
| 342 | request_extensions[XR_EXT_SPATIAL_PERSISTENCE_OPERATIONS_EXTENSION_NAME] = &spatial_persistence_operations_ext; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | return request_extensions; |
| 347 | } |
| 348 | |
| 349 | void OpenXRSpatialAnchorCapability::on_instance_created(const XrInstance p_instance) { |
| 350 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 351 | ERR_FAIL_NULL(se_extension); |
| 352 | |
| 353 | if (spatial_anchor_ext) { |
| 354 | EXT_INIT_XR_FUNC(xrCreateSpatialAnchorEXT); |
| 355 | } |
| 356 | |
| 357 | if (spatial_persistence_ext) { |
| 358 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 359 | ERR_FAIL_NULL(openxr_api); |
| 360 | |
| 361 | // TODO REMOVE THIS WORKAROUND ONCE POSSIBLE |
| 362 | // There has been some back and forth between stores and scopes. Scopes won out. |
| 363 | XrResult xr_result = openxr_api->get_instance_proc_addr("xrEnumerateSpatialPersistenceScopesEXT", (PFN_xrVoidFunction *)&xrEnumerateSpatialPersistenceScopesEXT_ptr); |
| 364 | if (xr_result != XR_SUCCESS) { |
| 365 | // Check for stores for compatibility with beta runtimes. |
| 366 | // Lucky for us, related structs and enums are compatible. |
| 367 | print_verbose("OpenXR: xrEnumerateSpatialPersistenceScopesEXT is not supported, falling back to xrEnumerateSpatialPersistenceStoresEXT!"); |
| 368 | xr_result = openxr_api->get_instance_proc_addr("xrEnumerateSpatialPersistenceStoresEXT", (PFN_xrVoidFunction *)&xrEnumerateSpatialPersistenceScopesEXT_ptr); |
| 369 | } |
| 370 | ERR_FAIL_COND(XR_FAILED(xr_result)); |
| 371 | //EXT_INIT_XR_FUNC(xrEnumerateSpatialPersistenceScopesEXT); |
| 372 | |
| 373 | EXT_INIT_XR_FUNC(xrCreateSpatialPersistenceContextAsyncEXT); |
| 374 | EXT_INIT_XR_FUNC(xrCreateSpatialPersistenceContextCompleteEXT); |
| 375 | EXT_INIT_XR_FUNC(xrDestroySpatialPersistenceContextEXT); |
| 376 | } |
| 377 | |
| 378 | if (spatial_persistence_operations_ext) { |
| 379 | EXT_INIT_XR_FUNC(xrPersistSpatialEntityAsyncEXT); |
| 380 | EXT_INIT_XR_FUNC(xrPersistSpatialEntityCompleteEXT); |
| 381 | EXT_INIT_XR_FUNC(xrUnpersistSpatialEntityAsyncEXT); |
| 382 | EXT_INIT_XR_FUNC(xrUnpersistSpatialEntityCompleteEXT); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | void OpenXRSpatialAnchorCapability::on_instance_destroyed() { |
| 387 | xrCreateSpatialAnchorEXT_ptr = nullptr; |
| 388 | |
| 389 | xrEnumerateSpatialPersistenceScopesEXT_ptr = nullptr; |
| 390 | xrCreateSpatialPersistenceContextAsyncEXT_ptr = nullptr; |
| 391 | xrCreateSpatialPersistenceContextCompleteEXT_ptr = nullptr; |
| 392 | xrDestroySpatialPersistenceContextEXT_ptr = nullptr; |
| 393 | } |
| 394 | |
| 395 | void OpenXRSpatialAnchorCapability::on_session_created(const XrSession p_session) { |
| 396 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 397 | ERR_FAIL_NULL(se_extension); |
| 398 | |
| 399 | if (!spatial_anchor_ext) { |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | spatial_anchor_supported = se_extension->supports_component_type(XR_SPATIAL_CAPABILITY_ANCHOR_EXT, XR_SPATIAL_COMPONENT_TYPE_ANCHOR_EXT); |
| 404 | if (!spatial_anchor_supported) { |
| 405 | // Supported by XR runtime but not by device? We're done. |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | _load_supported_persistence_scopes(); |
| 410 | |
| 411 | se_extension->connect(SNAME("spatial_discovery_recommended"), callable_mp(this, &OpenXRSpatialAnchorCapability::_on_spatial_discovery_recommended)); |
| 412 | |
| 413 | if (GLOBAL_GET_CACHED(bool, "xr/openxr/extensions/spatial_entity/enable_builtin_anchor_detection")) { |
| 414 | if (spatial_persistence_ext && !supported_persistence_scopes.is_empty()) { |
| 415 | // TODO make something nicer to select the persistence scope we want. |
| 416 | // We may even want to create multiple here so we get access to all |
| 417 | // but then mark one that we use to create our persistent anchors on. |
| 418 | |
| 419 | XrSpatialPersistenceScopeEXT scope = XR_SPATIAL_PERSISTENCE_SCOPE_MAX_ENUM_EXT; |
| 420 | |
| 421 | // Lets check these in order of importance to us and find the best applicable scope. |
| 422 | if (supported_persistence_scopes.has(XR_SPATIAL_PERSISTENCE_SCOPE_LOCAL_ANCHORS_EXT)) { |
| 423 | // This scope allows for local storage and is required if we want to create our own anchors. |
| 424 | scope = XR_SPATIAL_PERSISTENCE_SCOPE_LOCAL_ANCHORS_EXT; |
| 425 | } else if (supported_persistence_scopes.has(XR_SPATIAL_PERSISTENCE_SCOPE_SYSTEM_MANAGED_EXT)) { |
| 426 | // The system managed scope is a read only scope with system managed anchors. |
| 427 | scope = XR_SPATIAL_PERSISTENCE_SCOPE_SYSTEM_MANAGED_EXT; |
| 428 | } else { |
| 429 | // Just use the first supported scope, but this will be an unknown type. |
| 430 | scope = supported_persistence_scopes[0]; |
| 431 | } |
| 432 | |
| 433 | // Output what we're using: |
| 434 | print_verbose("OpenXR: Using persistence scope " + get_spatial_persistence_scope_name(scope)); |
| 435 | |
| 436 | // Start by creating our persistence context. |
| 437 | create_persistence_context(scope, callable_mp(this, &OpenXRSpatialAnchorCapability::_on_persistence_context_completed)); |
| 438 | } else { |
| 439 | // Start by creating our spatial context |
| 440 | _create_spatial_context(); |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | void OpenXRSpatialAnchorCapability::on_session_destroyed() { |
| 446 | if (!spatial_anchor_supported) { |
| 447 | return; |
| 448 | } |
| 449 | spatial_anchor_supported = false; |
| 450 | |
| 451 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 452 | ERR_FAIL_NULL(se_extension); |
| 453 | XRServer *xr_server = XRServer::get_singleton(); |
| 454 | ERR_FAIL_NULL(xr_server); |
| 455 | |
| 456 | // Free and unregister our anchors |
| 457 | for (const KeyValue<XrSpatialEntityIdEXT, Ref<OpenXRAnchorTracker>> &anchor : anchors) { |
| 458 | xr_server->remove_tracker(anchor.value); |
| 459 | } |
| 460 | anchors.clear(); |
| 461 | |
| 462 | // Free our configurations |
| 463 | anchor_configuration.unref(); |
| 464 | |
| 465 | // Free our spatial context |
| 466 | if (spatial_context.is_valid()) { |
| 467 | se_extension->free_spatial_context(spatial_context); |
| 468 | spatial_context = RID(); |
| 469 | } |
| 470 | |
| 471 | // Free our persistence context |
| 472 | if (persistence_context.is_valid()) { |
| 473 | free_persistence_context(persistence_context); |
| 474 | persistence_context = RID(); |
| 475 | } |
| 476 | |
| 477 | se_extension->disconnect(SNAME("spatial_discovery_recommended"), callable_mp(this, &OpenXRSpatialAnchorCapability::_on_spatial_discovery_recommended)); |
| 478 | |
| 479 | supported_persistence_scopes.clear(); |
| 480 | |
| 481 | // Clean up all remaining persistence context RIDs. |
| 482 | LocalVector<RID> persistence_context_rids = persistence_context_owner.get_owned_list(); |
| 483 | for (const RID &rid : persistence_context_rids) { |
| 484 | if (is_print_verbose_enabled()) { |
| 485 | PersistenceContextData *persistence_context_data = persistence_context_owner.get_or_null(rid); |
| 486 | if (persistence_context_data) { // Should never be nullptr seeing we called get_owned_list just now, but just in case. |
| 487 | print_line("OpenXR: Found orphaned persistence context for scope ", get_spatial_persistence_scope_name(persistence_context_data->scope)); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | free_persistence_context(rid); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | void OpenXRSpatialAnchorCapability::on_process() { |
| 496 | if (!spatial_context.is_valid()) { |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | // Protection against plane discovery happening too often. |
| 501 | if (discovery_cooldown > 0) { |
| 502 | discovery_cooldown--; |
| 503 | } |
| 504 | |
| 505 | // Check if we need to start our discovery. |
| 506 | if (need_discovery && discovery_cooldown == 0 && !discovery_query_result.is_valid()) { |
| 507 | need_discovery = false; |
| 508 | discovery_cooldown = 60; // Set our cooldown to 60 frames, it doesn't need to be an exact science. |
| 509 | |
| 510 | _start_entity_discovery(); |
| 511 | } |
| 512 | |
| 513 | // If we have a valid spatial context, and we have anchors, we want updates! |
| 514 | if (spatial_context.is_valid() && !anchors.is_empty()) { |
| 515 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 516 | ERR_FAIL_NULL(se_extension); |
| 517 | |
| 518 | // We want updates for all anchors |
| 519 | thread_local LocalVector<RID> entities; |
| 520 | entities.resize(anchors.size()); |
| 521 | RID *entity = entities.ptr(); |
| 522 | for (const KeyValue<XrSpatialEntityIdEXT, Ref<OpenXRAnchorTracker>> &e : anchors) { |
| 523 | *entity = e.value->get_entity(); |
| 524 | entity++; |
| 525 | } |
| 526 | |
| 527 | // We just want our anchor component |
| 528 | thread_local LocalVector<XrSpatialComponentTypeEXT> component_types; |
| 529 | component_types.push_back(XR_SPATIAL_COMPONENT_TYPE_ANCHOR_EXT); |
| 530 | |
| 531 | // And we get our update snapshot, this is NOT async! |
| 532 | RID snapshot = se_extension->update_spatial_entities(spatial_context, entities, component_types, nullptr); |
| 533 | if (snapshot.is_valid()) { |
| 534 | _process_update_snapshot(snapshot); |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | bool OpenXRSpatialAnchorCapability::is_spatial_anchor_supported() { |
| 540 | return spatial_anchor_supported; |
| 541 | } |
| 542 | |
| 543 | bool OpenXRSpatialAnchorCapability::is_spatial_persistence_supported() { |
| 544 | // Need anchor support for persistence to be usable |
| 545 | if (!is_spatial_anchor_supported()) { |
| 546 | return false; |
| 547 | } |
| 548 | |
| 549 | return spatial_persistence_ext; |
| 550 | } |
| 551 | |
| 552 | //////////////////////////////////////////////////////////////////////////// |
| 553 | // Persistence scopes |
| 554 | |
| 555 | bool OpenXRSpatialAnchorCapability::_load_supported_persistence_scopes() { |
| 556 | ERR_FAIL_COND_V(!is_spatial_persistence_supported(), false); |
| 557 | |
| 558 | if (supported_persistence_scopes.is_empty()) { |
| 559 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 560 | ERR_FAIL_NULL_V(openxr_api, false); |
| 561 | |
| 562 | uint32_t size; |
| 563 | XrInstance instance = openxr_api->get_instance(); |
| 564 | XrSystemId system_id = openxr_api->get_system_id(); |
| 565 | |
| 566 | ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, false); |
| 567 | |
| 568 | XrResult result = xrEnumerateSpatialPersistenceScopesEXT(instance, system_id, 0, &size, nullptr); |
| 569 | if (XR_FAILED(result)) { |
| 570 | ERR_FAIL_V_MSG(false, "OpenXR: Failed to query persistence scope count [" + openxr_api->get_error_string(result) + "]"); |
| 571 | } |
| 572 | |
| 573 | if (size > 0) { |
| 574 | supported_persistence_scopes.resize(size); |
| 575 | result = xrEnumerateSpatialPersistenceScopesEXT(instance, system_id, supported_persistence_scopes.size(), &size, supported_persistence_scopes.ptrw()); |
| 576 | if (XR_FAILED(result)) { |
| 577 | ERR_FAIL_V_MSG(false, "OpenXR: Failed to query persistence scopes [" + openxr_api->get_error_string(result) + "]"); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | if (is_print_verbose_enabled()) { |
| 582 | if (!supported_persistence_scopes.is_empty()) { |
| 583 | print_verbose("OpenXR: Supported spatial persistence scopes:"); |
| 584 | for (const XrSpatialPersistenceScopeEXT &scope : supported_persistence_scopes) { |
| 585 | print_verbose(" - " + get_spatial_persistence_scope_name(scope)); |
| 586 | } |
| 587 | } else { |
| 588 | WARN_PRINT("OpenXR: No persistence scopes found!"); |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | return true; |
| 594 | } |
| 595 | |
| 596 | bool OpenXRSpatialAnchorCapability::is_persistence_scope_supported(XrSpatialPersistenceScopeEXT p_scope) { |
| 597 | if (!is_spatial_persistence_supported()) { |
| 598 | return false; |
| 599 | } |
| 600 | |
| 601 | if (!_load_supported_persistence_scopes()) { |
| 602 | return false; |
| 603 | } |
| 604 | |
| 605 | return supported_persistence_scopes.has(p_scope); |
| 606 | } |
| 607 | |
| 608 | bool OpenXRSpatialAnchorCapability::_is_persistence_scope_supported(PersistenceScope p_scope) { |
| 609 | return is_persistence_scope_supported((XrSpatialPersistenceScopeEXT)p_scope); |
| 610 | } |
| 611 | |
| 612 | Ref<OpenXRFutureResult> OpenXRSpatialAnchorCapability::create_persistence_context(XrSpatialPersistenceScopeEXT p_scope, const Callable &p_user_callback) { |
| 613 | if (!is_spatial_persistence_supported()) { |
| 614 | return nullptr; |
| 615 | } |
| 616 | |
| 617 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 618 | ERR_FAIL_NULL_V(openxr_api, nullptr); |
| 619 | |
| 620 | OpenXRFutureExtension *future_api = OpenXRFutureExtension::get_singleton(); |
| 621 | ERR_FAIL_NULL_V(future_api, nullptr); |
| 622 | |
| 623 | XrSpatialPersistenceContextCreateInfoEXT create_info = { |
| 624 | XR_TYPE_SPATIAL_PERSISTENCE_CONTEXT_CREATE_INFO_EXT, // type |
| 625 | nullptr, // next |
| 626 | p_scope // scope |
| 627 | }; |
| 628 | XrFutureEXT future = XR_NULL_HANDLE; |
| 629 | XrResult result = xrCreateSpatialPersistenceContextAsyncEXT(openxr_api->get_session(), &create_info, &future); |
| 630 | if (XR_FAILED(result)) { |
| 631 | // Not successful? then exit. |
| 632 | ERR_FAIL_V_MSG(Ref<OpenXRFutureResult>(), "OpenXR: Failed to create persistence scope [" + openxr_api->get_error_string(result) + "]"); |
| 633 | } |
| 634 | |
| 635 | // Create our future result |
| 636 | Ref<OpenXRFutureResult> future_result = future_api->register_future(future, callable_mp(this, &OpenXRSpatialAnchorCapability::_on_persistence_context_ready).bind((uint64_t)p_scope, p_user_callback)); |
| 637 | |
| 638 | return future_result; |
| 639 | } |
| 640 | |
| 641 | Ref<OpenXRFutureResult> OpenXRSpatialAnchorCapability::_create_persistence_context(PersistenceScope p_scope, Callable p_user_callback) { |
| 642 | return create_persistence_context((XrSpatialPersistenceScopeEXT)p_scope, p_user_callback); |
| 643 | } |
| 644 | |
| 645 | void OpenXRSpatialAnchorCapability::_on_persistence_context_ready(Ref<OpenXRFutureResult> p_future_result, uint64_t p_scope, Callable p_user_callback) { |
| 646 | // Complete context creation... |
| 647 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 648 | ERR_FAIL_NULL(openxr_api); |
| 649 | |
| 650 | XrCreateSpatialPersistenceContextCompletionEXT completion = { |
| 651 | XR_TYPE_CREATE_SPATIAL_PERSISTENCE_CONTEXT_COMPLETION_EXT, // type |
| 652 | nullptr, // next |
| 653 | XR_RESULT_MAX_ENUM, // futureResult |
| 654 | XR_SPATIAL_PERSISTENCE_CONTEXT_RESULT_MAX_ENUM_EXT, // createResult |
| 655 | XR_NULL_HANDLE // persistenceContext |
| 656 | }; |
| 657 | XrResult result = xrCreateSpatialPersistenceContextCompleteEXT(openxr_api->get_session(), p_future_result->get_future(), &completion); |
| 658 | if (XR_FAILED(result)) { // Did our xrCreateSpatialContextCompleteEXT call fail? |
| 659 | // Log issue and fail. |
| 660 | ERR_FAIL_MSG("OpenXR: Failed to complete persistence context create future [" + openxr_api->get_error_string(result) + "]"); |
| 661 | } |
| 662 | if (XR_FAILED(completion.futureResult)) { // Did our completion fail? |
| 663 | // Log issue and fail. |
| 664 | ERR_FAIL_MSG("OpenXR: Failed to complete persistence context creation [" + openxr_api->get_error_string(completion.futureResult) + "]"); |
| 665 | } |
| 666 | if (completion.createResult != XR_SPATIAL_PERSISTENCE_CONTEXT_RESULT_SUCCESS_EXT) { // Did our persist fail? |
| 667 | // Log issue and fail. |
| 668 | ERR_FAIL_MSG("OpenXR: Failed to complete persistence context creation [" + get_spatial_persistence_context_result_name(completion.createResult) + "]"); |
| 669 | } |
| 670 | |
| 671 | // Wrap our persistence context |
| 672 | PersistenceContextData persistence_context_data; |
| 673 | |
| 674 | // Update our spatial context data |
| 675 | persistence_context_data.scope = (XrSpatialPersistenceScopeEXT)p_scope; |
| 676 | persistence_context_data.persistence_context = completion.persistenceContext; |
| 677 | |
| 678 | // Store this as an RID so we keep track of it. |
| 679 | RID context_rid = persistence_context_owner.make_rid(persistence_context_data); |
| 680 | |
| 681 | // Set our RID as our result value on our future. |
| 682 | p_future_result->set_result_value(context_rid); |
| 683 | |
| 684 | // And perform our callback if we have one. |
| 685 | if (p_user_callback.is_valid()) { |
| 686 | p_user_callback.call(context_rid); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | XrSpatialPersistenceContextEXT OpenXRSpatialAnchorCapability::get_persistence_context_handle(RID p_persistence_context) const { |
| 691 | PersistenceContextData *persistence_context_data = persistence_context_owner.get_or_null(p_persistence_context); |
| 692 | ERR_FAIL_NULL_V(persistence_context_data, XR_NULL_HANDLE); |
| 693 | |
| 694 | return persistence_context_data->persistence_context; |
| 695 | } |
| 696 | |
| 697 | uint64_t OpenXRSpatialAnchorCapability::_get_persistence_context_handle(RID p_persistence_context) const { |
| 698 | return (uint64_t)get_persistence_context_handle(p_persistence_context); |
| 699 | } |
| 700 | |
| 701 | void OpenXRSpatialAnchorCapability::free_persistence_context(RID p_persistence_context) { |
| 702 | PersistenceContextData *persistence_context_data = persistence_context_owner.get_or_null(p_persistence_context); |
| 703 | ERR_FAIL_NULL(persistence_context_data); |
| 704 | |
| 705 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 706 | ERR_FAIL_NULL(openxr_api); |
| 707 | |
| 708 | if (persistence_context_data->persistence_context != XR_NULL_HANDLE) { |
| 709 | // Destroy our spatial context |
| 710 | XrResult result = xrDestroySpatialPersistenceContextEXT(persistence_context_data->persistence_context); |
| 711 | if (XR_FAILED(result)) { |
| 712 | WARN_PRINT("OpenXR: Failed to destroy the persistence context [" + openxr_api->get_error_string(result) + "]"); |
| 713 | } |
| 714 | persistence_context_data->persistence_context = XR_NULL_HANDLE; |
| 715 | } |
| 716 | |
| 717 | // And remove our RID. |
| 718 | persistence_context_owner.free(p_persistence_context); |
| 719 | } |
| 720 | |
| 721 | //////////////////////////////////////////////////////////////////////////// |
| 722 | // Discovery logic |
| 723 | |
| 724 | void OpenXRSpatialAnchorCapability::_on_persistence_context_completed(RID p_persistence_context) { |
| 725 | persistence_context = p_persistence_context; |
| 726 | |
| 727 | _create_spatial_context(); |
| 728 | } |
| 729 | |
| 730 | Ref<OpenXRFutureResult> OpenXRSpatialAnchorCapability::_create_spatial_context() { |
| 731 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 732 | ERR_FAIL_NULL_V(se_extension, nullptr); |
| 733 | |
| 734 | TypedArray<OpenXRSpatialCapabilityConfigurationBaseHeader> capability_configurations; |
| 735 | |
| 736 | // Create our configuration objects. |
| 737 | anchor_configuration.instantiate(); |
| 738 | capability_configurations.push_back(anchor_configuration); |
| 739 | |
| 740 | if (persistence_context.is_valid()) { |
| 741 | persistence_configuration.instantiate(); |
| 742 | persistence_configuration->add_persistence_context(persistence_context); |
| 743 | } else { |
| 744 | // Shouldn't be instantiated in the first place but JIC |
| 745 | persistence_configuration.unref(); |
| 746 | } |
| 747 | |
| 748 | return se_extension->create_spatial_context(capability_configurations, persistence_configuration, callable_mp(this, &OpenXRSpatialAnchorCapability::_on_spatial_context_created)); |
| 749 | } |
| 750 | |
| 751 | void OpenXRSpatialAnchorCapability::_on_spatial_context_created(RID p_spatial_context) { |
| 752 | spatial_context = p_spatial_context; |
| 753 | need_discovery = true; |
| 754 | } |
| 755 | |
| 756 | void OpenXRSpatialAnchorCapability::_on_spatial_discovery_recommended(RID p_spatial_context) { |
| 757 | if (p_spatial_context == spatial_context) { |
| 758 | // Trigger new discovery. |
| 759 | need_discovery = true; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | Ref<OpenXRFutureResult> OpenXRSpatialAnchorCapability::_start_entity_discovery() { |
| 764 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 765 | ERR_FAIL_NULL_V(se_extension, nullptr); |
| 766 | |
| 767 | // It makes no sense to discover non persistent anchors as we'd have created them during this session. |
| 768 | if (!persistence_context.is_valid()) { |
| 769 | return nullptr; |
| 770 | } |
| 771 | |
| 772 | // Already running or ran discovery, cancel/clean up. |
| 773 | if (discovery_query_result.is_valid()) { |
| 774 | discovery_query_result->cancel_future(); |
| 775 | discovery_query_result.unref(); |
| 776 | } |
| 777 | |
| 778 | // We want both our anchor and persistence component. |
| 779 | Vector<XrSpatialComponentTypeEXT> component_types; |
| 780 | component_types.push_back(XR_SPATIAL_COMPONENT_TYPE_ANCHOR_EXT); |
| 781 | component_types.push_back(XR_SPATIAL_COMPONENT_TYPE_PERSISTENCE_EXT); |
| 782 | |
| 783 | // Start our new snapshot. |
| 784 | discovery_query_result = se_extension->discover_spatial_entities(spatial_context, component_types, nullptr, callable_mp(this, &OpenXRSpatialAnchorCapability::_process_discovery_snapshot)); |
| 785 | |
| 786 | return discovery_query_result; |
| 787 | } |
| 788 | |
| 789 | void OpenXRSpatialAnchorCapability::_process_discovery_snapshot(RID p_snapshot) { |
| 790 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 791 | ERR_FAIL_NULL(se_extension); |
| 792 | XRServer *xr_server = XRServer::get_singleton(); |
| 793 | ERR_FAIL_NULL(xr_server); |
| 794 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 795 | ERR_FAIL_NULL(openxr_api); |
| 796 | |
| 797 | // Build our component data. |
| 798 | TypedArray<OpenXRSpatialComponentData> component_data; |
| 799 | |
| 800 | // We always need a query result data object. |
| 801 | Ref<OpenXRSpatialQueryResultData> query_result_data; |
| 802 | query_result_data.instantiate(); |
| 803 | component_data.push_back(query_result_data); |
| 804 | |
| 805 | // And an anchor list object. |
| 806 | Ref<OpenXRSpatialComponentAnchorList> anchor_list_data; |
| 807 | anchor_list_data.instantiate(); |
| 808 | component_data.push_back(anchor_list_data); |
| 809 | |
| 810 | // Note that adding this data object means our snapshot will only return persistent anchors! |
| 811 | Ref<OpenXRSpatialComponentPersistenceList> persistence_list_data; |
| 812 | persistence_list_data.instantiate(); |
| 813 | component_data.push_back(persistence_list_data); |
| 814 | |
| 815 | if (se_extension->query_snapshot(p_snapshot, component_data, nullptr)) { |
| 816 | // Now loop through our data and update our anchors. |
| 817 | int64_t size = query_result_data->get_capacity(); |
| 818 | for (int64_t i = 0; i < size; i++) { |
| 819 | XrSpatialEntityIdEXT entity_id = query_result_data->get_entity_id(i); |
| 820 | XrSpatialEntityTrackingStateEXT entity_state = query_result_data->get_entity_state(i); |
| 821 | |
| 822 | if (entity_state == XR_SPATIAL_ENTITY_TRACKING_STATE_STOPPED_EXT) { |
| 823 | // We shouldn't get stopped anchors for discovery queries, but JIC. |
| 824 | if (anchors.has(entity_id)) { |
| 825 | Ref<OpenXRAnchorTracker> anchor = anchors[entity_id]; |
| 826 | anchor->invalidate_pose(SNAME("default")); |
| 827 | anchor->set_spatial_tracking_state(XR_SPATIAL_ENTITY_TRACKING_STATE_STOPPED_EXT); |
| 828 | } |
| 829 | } else { |
| 830 | // Process our entity. |
| 831 | bool add_to_xr_server = false; |
| 832 | Ref<OpenXRAnchorTracker> anchor; |
| 833 | |
| 834 | if (anchors.has(entity_id)) { |
| 835 | // We know about this one already. |
| 836 | anchor = anchors[entity_id]; |
| 837 | } else { |
| 838 | // Create a new anchor. |
| 839 | anchor.instantiate(); |
| 840 | anchor->set_entity(se_extension->make_spatial_entity(se_extension->get_spatial_snapshot_context(p_snapshot), entity_id)); |
| 841 | anchors[entity_id] = anchor; |
| 842 | |
| 843 | add_to_xr_server = true; |
| 844 | } |
| 845 | |
| 846 | // Handle component data. |
| 847 | if (entity_state == XR_SPATIAL_ENTITY_TRACKING_STATE_PAUSED_EXT) { |
| 848 | anchor->invalidate_pose(SNAME("default")); |
| 849 | anchor->set_spatial_tracking_state(XR_SPATIAL_ENTITY_TRACKING_STATE_PAUSED_EXT); |
| 850 | |
| 851 | // No further component data will be valid in this state, we need to ignore it! |
| 852 | } else if (entity_state == XR_SPATIAL_ENTITY_TRACKING_STATE_TRACKING_EXT) { |
| 853 | Transform3D transform = anchor_list_data->get_entity_pose(i); |
| 854 | anchor->set_pose(SNAME("default"), transform, Vector3(), Vector3()); |
| 855 | anchor->set_spatial_tracking_state(XR_SPATIAL_ENTITY_TRACKING_STATE_TRACKING_EXT); |
| 856 | } |
| 857 | |
| 858 | // Persistence is the only component that will contain valid data if entity_state == XR_SPATIAL_ENTITY_TRACKING_STATE_PAUSED_EXT. |
| 859 | const XrSpatialPersistenceStateEXT persistent_state = persistence_list_data->get_persistent_state(i); |
| 860 | if (persistent_state == XR_SPATIAL_PERSISTENCE_STATE_LOADED_EXT) { |
| 861 | anchor->set_uuid(persistence_list_data->get_persistent_uuid(i)); |
| 862 | } |
| 863 | |
| 864 | if (add_to_xr_server) { |
| 865 | // Register with XR server. |
| 866 | xr_server->add_tracker(anchor); |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | // We don't remove trackers here, users will be removing anchors. |
| 872 | // Maybe at some point when shared anchors between headsets result |
| 873 | // in another device removing the shared anchor we need to deal with this. |
| 874 | } |
| 875 | |
| 876 | // Now that we're done, clean up our snapshot! |
| 877 | se_extension->free_spatial_snapshot(p_snapshot); |
| 878 | |
| 879 | // And if this was our discovery snapshot, let's reset it. |
| 880 | if (discovery_query_result.is_valid() && discovery_query_result->get_result_value() == p_snapshot) { |
| 881 | discovery_query_result.unref(); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | void OpenXRSpatialAnchorCapability::_process_update_snapshot(RID p_snapshot) { |
| 886 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 887 | ERR_FAIL_NULL(se_extension); |
| 888 | XRServer *xr_server = XRServer::get_singleton(); |
| 889 | ERR_FAIL_NULL(xr_server); |
| 890 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 891 | ERR_FAIL_NULL(openxr_api); |
| 892 | |
| 893 | // Build our component data. |
| 894 | TypedArray<OpenXRSpatialComponentData> component_data; |
| 895 | |
| 896 | // We always need a query result data object. |
| 897 | Ref<OpenXRSpatialQueryResultData> query_result_data; |
| 898 | query_result_data.instantiate(); |
| 899 | component_data.push_back(query_result_data); |
| 900 | |
| 901 | // And an anchor list object. |
| 902 | Ref<OpenXRSpatialComponentAnchorList> anchor_list_data; |
| 903 | anchor_list_data.instantiate(); |
| 904 | component_data.push_back(anchor_list_data); |
| 905 | |
| 906 | if (se_extension->query_snapshot(p_snapshot, component_data, nullptr)) { |
| 907 | // Now loop through our data and update our anchors. |
| 908 | int64_t size = query_result_data->get_capacity(); |
| 909 | for (int64_t i = 0; i < size; i++) { |
| 910 | XrSpatialEntityIdEXT entity_id = query_result_data->get_entity_id(i); |
| 911 | XrSpatialEntityTrackingStateEXT entity_state = query_result_data->get_entity_state(i); |
| 912 | |
| 913 | if (anchors.has(entity_id)) { |
| 914 | // Process our entity. |
| 915 | Ref<OpenXRAnchorTracker> anchor = anchors[entity_id]; |
| 916 | |
| 917 | if (entity_state == XR_SPATIAL_ENTITY_TRACKING_STATE_STOPPED_EXT) { |
| 918 | anchor->invalidate_pose(SNAME("default")); |
| 919 | anchor->set_spatial_tracking_state(XR_SPATIAL_ENTITY_TRACKING_STATE_STOPPED_EXT); |
| 920 | } else if (entity_state == XR_SPATIAL_ENTITY_TRACKING_STATE_PAUSED_EXT) { |
| 921 | anchor->invalidate_pose(SNAME("default")); |
| 922 | anchor->set_spatial_tracking_state(XR_SPATIAL_ENTITY_TRACKING_STATE_PAUSED_EXT); |
| 923 | } else if (entity_state == XR_SPATIAL_ENTITY_TRACKING_STATE_TRACKING_EXT) { |
| 924 | Transform3D transform = anchor_list_data->get_entity_pose(i); |
| 925 | anchor->set_pose(SNAME("default"), transform, Vector3(), Vector3()); |
| 926 | anchor->set_spatial_tracking_state(XR_SPATIAL_ENTITY_TRACKING_STATE_TRACKING_EXT); |
| 927 | } |
| 928 | } else { |
| 929 | WARN_PRINT("OpenXR: Anchor update query returned unknown anchor with entity ID: " + String::num_int64(entity_id)); |
| 930 | } |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | // Now that we're done, clean up our snapshot! |
| 935 | se_extension->free_spatial_snapshot(p_snapshot); |
| 936 | } |
| 937 | |
| 938 | //////////////////////////////////////////////////////////////////////////// |
| 939 | // Anchor creation |
| 940 | |
| 941 | Ref<OpenXRAnchorTracker> OpenXRSpatialAnchorCapability::create_new_anchor(const Transform3D &p_transform, RID p_spatial_context) { |
| 942 | Ref<OpenXRAnchorTracker> tracker; |
| 943 | |
| 944 | ERR_FAIL_COND_V_MSG(!is_spatial_anchor_supported(), tracker, "OpenXR: Spatial entity anchor capability is not supported on this hardware!"); |
| 945 | |
| 946 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 947 | ERR_FAIL_NULL_V(openxr_api, tracker); |
| 948 | XRServer *xr_server = XRServer::get_singleton(); |
| 949 | ERR_FAIL_NULL_V(xr_server, tracker); |
| 950 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 951 | ERR_FAIL_NULL_V(se_extension, tracker); |
| 952 | |
| 953 | // TODO reverse apply world scale and reference frame to transform. |
| 954 | |
| 955 | XrPosef pose = openxr_api->pose_from_transform(p_transform); |
| 956 | |
| 957 | RID sc = p_spatial_context.is_valid() ? p_spatial_context : spatial_context; |
| 958 | ERR_FAIL_COND_V(sc.is_null(), tracker); |
| 959 | |
| 960 | XrSpatialAnchorCreateInfoEXT create_info = { |
| 961 | XR_TYPE_SPATIAL_ANCHOR_CREATE_INFO_EXT, // type |
| 962 | nullptr, // next |
| 963 | openxr_api->get_play_space(), // baseSpace |
| 964 | openxr_api->get_predicted_display_time(), // time |
| 965 | pose // pose |
| 966 | }; |
| 967 | XrSpatialEntityIdEXT entity_id; |
| 968 | XrSpatialEntityEXT entity; |
| 969 | XrResult result = xrCreateSpatialAnchorEXT(se_extension->get_spatial_context_handle(sc), &create_info, &entity_id, &entity); |
| 970 | if (XR_FAILED(result)) { // Did our xrCreateSpatialContextCompleteEXT call fail? |
| 971 | ERR_FAIL_V_MSG(tracker, "OpenXR: Failed to create anchor [" + openxr_api->get_error_string(result) + "]"); |
| 972 | } |
| 973 | |
| 974 | tracker.instantiate(); |
| 975 | tracker->set_entity(se_extension->add_spatial_entity(sc, entity_id, entity)); |
| 976 | tracker->set_tracker_desc("Anchor"); |
| 977 | tracker->set_pose(SNAME("default"), p_transform, Vector3(), Vector3()); |
| 978 | |
| 979 | // Remember our tracker. |
| 980 | anchors[entity_id] = tracker; |
| 981 | xr_server->add_tracker(tracker); |
| 982 | |
| 983 | return tracker; |
| 984 | } |
| 985 | |
| 986 | void OpenXRSpatialAnchorCapability::remove_anchor(Ref<OpenXRAnchorTracker> p_anchor_tracker) { |
| 987 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 988 | ERR_FAIL_NULL(openxr_api); |
| 989 | XRServer *xr_server = XRServer::get_singleton(); |
| 990 | ERR_FAIL_NULL(xr_server); |
| 991 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 992 | ERR_FAIL_NULL(se_extension); |
| 993 | |
| 994 | // We check for this here. We could do this asynchronous but the caller may than wrongly expect this method to be instant. |
| 995 | ERR_FAIL_COND_MSG(p_anchor_tracker->has_uuid(), "OpenXR: This anchor is persistent. It must first be made unpersistent."); |
| 996 | |
| 997 | // Attempt to unregister it from our xr_server. |
| 998 | xr_server->remove_tracker(p_anchor_tracker); |
| 999 | |
| 1000 | // Get our entity. |
| 1001 | RID entity = p_anchor_tracker->get_entity(); |
| 1002 | ERR_FAIL_COND(entity.is_null()); |
| 1003 | |
| 1004 | // Get our entity id. |
| 1005 | XrSpatialEntityIdEXT entity_id = se_extension->get_spatial_entity_id(entity); |
| 1006 | ERR_FAIL_COND(entity_id == XR_NULL_ENTITY); |
| 1007 | |
| 1008 | // Remove it from our entity list. |
| 1009 | if (anchors.has(entity_id)) { |
| 1010 | anchors.erase(entity_id); |
| 1011 | } |
| 1012 | |
| 1013 | // Clear our entity, this will free it as well. |
| 1014 | p_anchor_tracker->set_entity(RID()); |
| 1015 | |
| 1016 | // The anchor tracker will be cleaned up once its fully dereferenced. |
| 1017 | } |
| 1018 | |
| 1019 | Ref<OpenXRFutureResult> OpenXRSpatialAnchorCapability::persist_anchor(Ref<OpenXRAnchorTracker> p_anchor_tracker, RID p_persistence_context, const Callable &p_user_callback) { |
| 1020 | ERR_FAIL_COND_V(!is_spatial_persistence_supported(), nullptr); |
| 1021 | |
| 1022 | RID pc = p_persistence_context.is_valid() ? p_persistence_context : persistence_context; |
| 1023 | ERR_FAIL_COND_V(pc.is_null(), nullptr); |
| 1024 | ERR_FAIL_COND_V(p_anchor_tracker.is_null(), nullptr); |
| 1025 | PersistenceContextData *persistence_context_data = persistence_context_owner.get_or_null(pc); |
| 1026 | ERR_FAIL_NULL_V(persistence_context_data, nullptr); |
| 1027 | const XrSpatialPersistenceContextEXT persistence_context_handle = persistence_context_data->persistence_context; |
| 1028 | |
| 1029 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 1030 | ERR_FAIL_NULL_V(openxr_api, nullptr); |
| 1031 | OpenXRFutureExtension *future_api = OpenXRFutureExtension::get_singleton(); |
| 1032 | ERR_FAIL_NULL_V(future_api, nullptr); |
| 1033 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 1034 | ERR_FAIL_NULL_V(se_extension, nullptr); |
| 1035 | |
| 1036 | RID entity = p_anchor_tracker->get_entity(); |
| 1037 | ERR_FAIL_COND_V(entity.is_null(), nullptr); |
| 1038 | |
| 1039 | XrSpatialEntityIdEXT entity_id = se_extension->get_spatial_entity_id(entity); |
| 1040 | ERR_FAIL_COND_V(entity_id == XR_NULL_ENTITY, nullptr); |
| 1041 | |
| 1042 | RID spatial_context_rid = se_extension->get_spatial_entity_context(entity); |
| 1043 | const XrSpatialContextEXT spatial_context_handle = se_extension->get_spatial_context_handle(spatial_context_rid); |
| 1044 | |
| 1045 | XrFutureEXT future = XR_NULL_HANDLE; |
| 1046 | |
| 1047 | XrSpatialEntityPersistInfoEXT persist_info = { |
| 1048 | XR_TYPE_SPATIAL_ENTITY_PERSIST_INFO_EXT, // type |
| 1049 | nullptr, // next |
| 1050 | spatial_context_handle, // spatialContext |
| 1051 | entity_id // entityId |
| 1052 | }; |
| 1053 | XrResult result = xrPersistSpatialEntityAsyncEXT(persistence_context_handle, &persist_info, &future); |
| 1054 | if (XR_FAILED(result)) { |
| 1055 | ERR_FAIL_V_MSG(nullptr, "OpenXR: Failed to start making anchor persistent [" + openxr_api->get_error_string(result) + "]"); |
| 1056 | } |
| 1057 | |
| 1058 | // Create our future result. |
| 1059 | Ref<OpenXRFutureResult> future_result = future_api->register_future(future, callable_mp(this, &OpenXRSpatialAnchorCapability::_on_made_anchor_persistent).bind(pc, p_anchor_tracker, p_user_callback)); |
| 1060 | |
| 1061 | return future_result; |
| 1062 | } |
| 1063 | |
| 1064 | void OpenXRSpatialAnchorCapability::_on_made_anchor_persistent(Ref<OpenXRFutureResult> p_future_result, RID p_persistence_context, Ref<OpenXRAnchorTracker> p_anchor_tracker, const Callable &p_user_callback) { |
| 1065 | ERR_FAIL_COND(p_anchor_tracker.is_null()); |
| 1066 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 1067 | ERR_FAIL_NULL(openxr_api); |
| 1068 | PersistenceContextData *persistence_context_data = persistence_context_owner.get_or_null(p_persistence_context); |
| 1069 | ERR_FAIL_NULL(persistence_context_data); |
| 1070 | |
| 1071 | XrFutureEXT future = p_future_result->get_future(); |
| 1072 | |
| 1073 | XrPersistSpatialEntityCompletionEXT completion = { |
| 1074 | XR_TYPE_PERSIST_SPATIAL_ENTITY_COMPLETION_EXT, // type |
| 1075 | nullptr, // next |
| 1076 | XR_RESULT_MAX_ENUM, // futureResult |
| 1077 | XR_SPATIAL_PERSISTENCE_CONTEXT_RESULT_MAX_ENUM_EXT, // persistResult |
| 1078 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } // persistUuid |
| 1079 | }; |
| 1080 | XrResult result = xrPersistSpatialEntityCompleteEXT(persistence_context_data->persistence_context, future, &completion); |
| 1081 | if (XR_FAILED(result)) { // Did our xrCreateSpatialContextCompleteEXT call fail? |
| 1082 | p_future_result->set_result_value(false); |
| 1083 | ERR_FAIL_MSG("OpenXR: Failed to complete anchor persistent future [" + openxr_api->get_error_string(result) + "]"); |
| 1084 | } |
| 1085 | if (XR_FAILED(completion.futureResult)) { // Did our completion fail? |
| 1086 | p_future_result->set_result_value(false); |
| 1087 | ERR_FAIL_MSG("OpenXR: Failed to complete making anchor persistent [" + openxr_api->get_error_string(completion.futureResult) + "]"); |
| 1088 | } |
| 1089 | if (completion.persistResult != XR_SPATIAL_PERSISTENCE_CONTEXT_RESULT_SUCCESS_EXT) { // Did our process fail? |
| 1090 | p_future_result->set_result_value(false); |
| 1091 | ERR_FAIL_MSG("OpenXR: Failed to make anchor persistent [" + get_spatial_persistence_context_result_name(completion.persistResult) + "]"); |
| 1092 | } |
| 1093 | |
| 1094 | // Set our new UUID. |
| 1095 | p_anchor_tracker->set_uuid(completion.persistUuid); |
| 1096 | |
| 1097 | // Set true our result value on our future. |
| 1098 | p_future_result->set_result_value(true); |
| 1099 | |
| 1100 | // Do our callback. |
| 1101 | p_user_callback.call(p_anchor_tracker); |
| 1102 | } |
| 1103 | |
| 1104 | Ref<OpenXRFutureResult> OpenXRSpatialAnchorCapability::unpersist_anchor(Ref<OpenXRAnchorTracker> p_anchor_tracker, RID p_persistence_context, const Callable &p_user_callback) { |
| 1105 | ERR_FAIL_COND_V(!is_spatial_persistence_supported(), nullptr); |
| 1106 | |
| 1107 | RID pc = p_persistence_context.is_valid() ? p_persistence_context : persistence_context; |
| 1108 | ERR_FAIL_COND_V(pc.is_null(), nullptr); |
| 1109 | ERR_FAIL_COND_V(p_anchor_tracker.is_null(), nullptr); |
| 1110 | PersistenceContextData *persistence_context_data = persistence_context_owner.get_or_null(pc); |
| 1111 | ERR_FAIL_NULL_V(persistence_context_data, nullptr); |
| 1112 | |
| 1113 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 1114 | ERR_FAIL_NULL_V(openxr_api, nullptr); |
| 1115 | OpenXRFutureExtension *future_api = OpenXRFutureExtension::get_singleton(); |
| 1116 | ERR_FAIL_NULL_V(future_api, nullptr); |
| 1117 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 1118 | ERR_FAIL_NULL_V(se_extension, nullptr); |
| 1119 | |
| 1120 | XrFutureEXT future; |
| 1121 | |
| 1122 | XrSpatialEntityUnpersistInfoEXT unpersist_info = { |
| 1123 | XR_TYPE_SPATIAL_ENTITY_UNPERSIST_INFO_EXT, // type |
| 1124 | nullptr, // next |
| 1125 | p_anchor_tracker->get_uuid() // persistUuid |
| 1126 | }; |
| 1127 | XrResult result = xrUnpersistSpatialEntityAsyncEXT(persistence_context_data->persistence_context, &unpersist_info, &future); |
| 1128 | if (XR_FAILED(result)) { |
| 1129 | ERR_FAIL_V_MSG(nullptr, "OpenXR: Failed to make anchor unpersistent [" + openxr_api->get_error_string(result) + "]"); |
| 1130 | } |
| 1131 | |
| 1132 | // Create our future result. |
| 1133 | Ref<OpenXRFutureResult> future_result = future_api->register_future(future, callable_mp(this, &OpenXRSpatialAnchorCapability::_on_made_anchor_unpersistent).bind(pc, p_anchor_tracker, p_user_callback)); |
| 1134 | |
| 1135 | return future_result; |
| 1136 | } |
| 1137 | |
| 1138 | void OpenXRSpatialAnchorCapability::_on_made_anchor_unpersistent(Ref<OpenXRFutureResult> p_future_result, RID p_persistence_context, Ref<OpenXRAnchorTracker> p_anchor_tracker, const Callable &p_user_callback) { |
| 1139 | ERR_FAIL_COND(p_anchor_tracker.is_null()); |
| 1140 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 1141 | ERR_FAIL_NULL(openxr_api); |
| 1142 | PersistenceContextData *persistence_context_data = persistence_context_owner.get_or_null(p_persistence_context); |
| 1143 | ERR_FAIL_NULL(persistence_context_data); |
| 1144 | |
| 1145 | XrFutureEXT future = p_future_result->get_future(); |
| 1146 | |
| 1147 | XrUnpersistSpatialEntityCompletionEXT completion = { |
| 1148 | XR_TYPE_UNPERSIST_SPATIAL_ENTITY_COMPLETION_EXT, // type |
| 1149 | nullptr, // next |
| 1150 | XR_RESULT_MAX_ENUM, // futureResult |
| 1151 | XR_SPATIAL_PERSISTENCE_CONTEXT_RESULT_MAX_ENUM_EXT, // unpersistResult |
| 1152 | }; |
| 1153 | XrResult result = xrUnpersistSpatialEntityCompleteEXT(persistence_context_data->persistence_context, future, &completion); |
| 1154 | if (XR_FAILED(result)) { // Did our xrCreateSpatialContextCompleteEXT call fail? |
| 1155 | p_future_result->set_result_value(false); |
| 1156 | ERR_FAIL_MSG("OpenXR: Failed to complete anchor unpersistent future [" + openxr_api->get_error_string(result) + "]"); |
| 1157 | } |
| 1158 | if (XR_FAILED(completion.futureResult)) { // Did our completion fail? |
| 1159 | p_future_result->set_result_value(false); |
| 1160 | ERR_FAIL_MSG("OpenXR: Failed to complete making anchor unpersistent [" + openxr_api->get_error_string(completion.futureResult) + "]"); |
| 1161 | } |
| 1162 | if (completion.unpersistResult != XR_SPATIAL_PERSISTENCE_CONTEXT_RESULT_SUCCESS_EXT) { // Did our process fail? |
| 1163 | p_future_result->set_result_value(false); |
| 1164 | ERR_FAIL_MSG("OpenXR: Failed to make anchor unpersistent [" + get_spatial_persistence_context_result_name(completion.unpersistResult) + "]"); |
| 1165 | } |
| 1166 | |
| 1167 | // Unset our UUID. |
| 1168 | XrUuid empty_uid = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 1169 | p_anchor_tracker->set_uuid(empty_uid); |
| 1170 | |
| 1171 | // Set true our result value on our future. |
| 1172 | p_future_result->set_result_value(true); |
| 1173 | |
| 1174 | // Do our callback. |
| 1175 | p_user_callback.call(p_anchor_tracker); |
| 1176 | } |
| 1177 | |
| 1178 | String OpenXRSpatialAnchorCapability::get_spatial_persistence_scope_name(XrSpatialPersistenceScopeEXT p_scope){ |
| 1179 | XR_ENUM_SWITCH(XrSpatialPersistenceScopeEXT, p_scope) |
| 1180 | } |
| 1181 | |
| 1182 | String OpenXRSpatialAnchorCapability::get_spatial_persistence_context_result_name(XrSpatialPersistenceContextResultEXT p_result) { |
| 1183 | XR_ENUM_SWITCH(XrSpatialPersistenceContextResultEXT, p_result) |
| 1184 | } |