| 1 | /**************************************************************************/ |
| 2 | /* openxr_spatial_entity_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_spatial_entity_extension.h" |
| 32 | |
| 33 | #include "../../openxr_api.h" |
| 34 | #include "core/config/project_settings.h" |
| 35 | #include "servers/xr/xr_server.h" |
| 36 | |
| 37 | //////////////////////////////////////////////////////////////////////////// |
| 38 | // OpenXRSpatialEntityExtension |
| 39 | |
| 40 | OpenXRSpatialEntityExtension *OpenXRSpatialEntityExtension::singleton = nullptr; |
| 41 | |
| 42 | OpenXRSpatialEntityExtension *OpenXRSpatialEntityExtension::get_singleton() { |
| 43 | return singleton; |
| 44 | } |
| 45 | |
| 46 | void OpenXRSpatialEntityExtension::_bind_methods() { |
| 47 | ClassDB::bind_method(D_METHOD("supports_capability", "capability"), &OpenXRSpatialEntityExtension::_supports_capability); |
| 48 | ClassDB::bind_method(D_METHOD("supports_component_type", "capability", "component_type"), &OpenXRSpatialEntityExtension::_supports_component_type); |
| 49 | |
| 50 | ClassDB::bind_method(D_METHOD("create_spatial_context", "capability_configurations", "next", "user_callback"), &OpenXRSpatialEntityExtension::create_spatial_context, DEFVAL(Variant()), DEFVAL(Callable())); |
| 51 | ClassDB::bind_method(D_METHOD("get_spatial_context_ready", "spatial_context"), &OpenXRSpatialEntityExtension::get_spatial_context_ready); |
| 52 | ClassDB::bind_method(D_METHOD("free_spatial_context", "spatial_context"), &OpenXRSpatialEntityExtension::free_spatial_context); |
| 53 | ClassDB::bind_method(D_METHOD("get_spatial_context_handle", "spatial_context"), &OpenXRSpatialEntityExtension::_get_spatial_context_handle); |
| 54 | |
| 55 | ADD_SIGNAL(MethodInfo("spatial_discovery_recommended", PropertyInfo(Variant::RID, "spatial_context"))); |
| 56 | |
| 57 | // Component_types should be an int array typed to ComponentType(XrSpatialComponentTypeEXT), but we currently don't support that. |
| 58 | ClassDB::bind_method(D_METHOD("discover_spatial_entities", "spatial_context", "component_types", "next", "user_callback"), &OpenXRSpatialEntityExtension::_discover_spatial_entities, DEFVAL(Variant()), DEFVAL(Callable())); |
| 59 | ClassDB::bind_method(D_METHOD("update_spatial_entities", "spatial_context", "entities", "component_types", "next"), &OpenXRSpatialEntityExtension::_update_spatial_entities, DEFVAL(Variant())); |
| 60 | |
| 61 | ClassDB::bind_method(D_METHOD("free_spatial_snapshot", "spatial_snapshot"), &OpenXRSpatialEntityExtension::free_spatial_snapshot); |
| 62 | ClassDB::bind_method(D_METHOD("get_spatial_snapshot_handle", "spatial_snapshot"), &OpenXRSpatialEntityExtension::_get_spatial_snapshot_handle); |
| 63 | ClassDB::bind_method(D_METHOD("get_spatial_snapshot_context", "spatial_snapshot"), &OpenXRSpatialEntityExtension::get_spatial_snapshot_context); |
| 64 | ClassDB::bind_method(D_METHOD("query_snapshot", "spatial_snapshot", "component_data", "next"), &OpenXRSpatialEntityExtension::query_snapshot, DEFVAL(Variant())); |
| 65 | |
| 66 | ClassDB::bind_method(D_METHOD("get_string", "spatial_snapshot", "buffer_id"), &OpenXRSpatialEntityExtension::_get_string); |
| 67 | ClassDB::bind_method(D_METHOD("get_uint8_buffer", "spatial_snapshot", "buffer_id"), &OpenXRSpatialEntityExtension::_get_uint8_buffer); |
| 68 | ClassDB::bind_method(D_METHOD("get_uint16_buffer", "spatial_snapshot", "buffer_id"), &OpenXRSpatialEntityExtension::_get_uint16_buffer); |
| 69 | ClassDB::bind_method(D_METHOD("get_uint32_buffer", "spatial_snapshot", "buffer_id"), &OpenXRSpatialEntityExtension::_get_uint32_buffer); |
| 70 | ClassDB::bind_method(D_METHOD("get_float_buffer", "spatial_snapshot", "buffer_id"), &OpenXRSpatialEntityExtension::_get_float_buffer); |
| 71 | ClassDB::bind_method(D_METHOD("get_vector2_buffer", "spatial_snapshot", "buffer_id"), &OpenXRSpatialEntityExtension::_get_vector2_buffer); |
| 72 | ClassDB::bind_method(D_METHOD("get_vector3_buffer", "spatial_snapshot", "buffer_id"), &OpenXRSpatialEntityExtension::_get_vector3_buffer); |
| 73 | |
| 74 | ClassDB::bind_method(D_METHOD("find_spatial_entity", "entity_id"), &OpenXRSpatialEntityExtension::_find_entity); |
| 75 | ClassDB::bind_method(D_METHOD("add_spatial_entity", "spatial_context", "entity_id", "entity"), &OpenXRSpatialEntityExtension::_add_entity); |
| 76 | ClassDB::bind_method(D_METHOD("make_spatial_entity", "spatial_context", "entity_id"), &OpenXRSpatialEntityExtension::_make_entity); |
| 77 | ClassDB::bind_method(D_METHOD("get_spatial_entity_id", "entity"), &OpenXRSpatialEntityExtension::_get_entity_id); |
| 78 | ClassDB::bind_method(D_METHOD("get_spatial_entity_context", "entity"), &OpenXRSpatialEntityExtension::get_spatial_entity_context); |
| 79 | ClassDB::bind_method(D_METHOD("free_spatial_entity", "entity"), &OpenXRSpatialEntityExtension::free_spatial_entity); |
| 80 | |
| 81 | BIND_ENUM_CONSTANT(CAPABILITY_PLANE_TRACKING); |
| 82 | BIND_ENUM_CONSTANT(CAPABILITY_MARKER_TRACKING_QR_CODE); |
| 83 | BIND_ENUM_CONSTANT(CAPABILITY_MARKER_TRACKING_MICRO_QR_CODE); |
| 84 | BIND_ENUM_CONSTANT(CAPABILITY_MARKER_TRACKING_ARUCO_MARKER); |
| 85 | BIND_ENUM_CONSTANT(CAPABILITY_MARKER_TRACKING_APRIL_TAG); |
| 86 | BIND_ENUM_CONSTANT(CAPABILITY_ANCHOR); |
| 87 | |
| 88 | BIND_ENUM_CONSTANT(COMPONENT_TYPE_BOUNDED_2D); |
| 89 | BIND_ENUM_CONSTANT(COMPONENT_TYPE_BOUNDED_3D); |
| 90 | BIND_ENUM_CONSTANT(COMPONENT_TYPE_PARENT); |
| 91 | BIND_ENUM_CONSTANT(COMPONENT_TYPE_MESH_3D); |
| 92 | BIND_ENUM_CONSTANT(COMPONENT_TYPE_PLANE_ALIGNMENT); |
| 93 | BIND_ENUM_CONSTANT(COMPONENT_TYPE_MESH_2D); |
| 94 | BIND_ENUM_CONSTANT(COMPONENT_TYPE_POLYGON_2D); |
| 95 | BIND_ENUM_CONSTANT(COMPONENT_TYPE_PLANE_SEMANTIC_LABEL); |
| 96 | BIND_ENUM_CONSTANT(COMPONENT_TYPE_MARKER); |
| 97 | BIND_ENUM_CONSTANT(COMPONENT_TYPE_ANCHOR); |
| 98 | BIND_ENUM_CONSTANT(COMPONENT_TYPE_PERSISTENCE); |
| 99 | } |
| 100 | |
| 101 | OpenXRSpatialEntityExtension::OpenXRSpatialEntityExtension() { |
| 102 | singleton = this; |
| 103 | } |
| 104 | |
| 105 | OpenXRSpatialEntityExtension::~OpenXRSpatialEntityExtension() { |
| 106 | singleton = nullptr; |
| 107 | } |
| 108 | |
| 109 | HashMap<String, bool *> OpenXRSpatialEntityExtension::get_requested_extensions(XrVersion p_version) { |
| 110 | HashMap<String, bool *> request_extensions; |
| 111 | |
| 112 | if (GLOBAL_GET_CACHED(bool, "xr/openxr/extensions/spatial_entity/enabled")) { |
| 113 | request_extensions[XR_EXT_SPATIAL_ENTITY_EXTENSION_NAME] = &spatial_entity_ext; |
| 114 | } |
| 115 | |
| 116 | return request_extensions; |
| 117 | } |
| 118 | |
| 119 | void OpenXRSpatialEntityExtension::on_instance_created(const XrInstance p_instance) { |
| 120 | if (spatial_entity_ext) { |
| 121 | EXT_INIT_XR_FUNC(xrEnumerateSpatialCapabilitiesEXT); |
| 122 | EXT_INIT_XR_FUNC(xrEnumerateSpatialCapabilityComponentTypesEXT); |
| 123 | EXT_INIT_XR_FUNC(xrEnumerateSpatialCapabilityFeaturesEXT); |
| 124 | EXT_INIT_XR_FUNC(xrCreateSpatialContextAsyncEXT); |
| 125 | EXT_INIT_XR_FUNC(xrCreateSpatialContextCompleteEXT); |
| 126 | EXT_INIT_XR_FUNC(xrDestroySpatialContextEXT); |
| 127 | EXT_INIT_XR_FUNC(xrCreateSpatialDiscoverySnapshotAsyncEXT); |
| 128 | EXT_INIT_XR_FUNC(xrCreateSpatialDiscoverySnapshotCompleteEXT); |
| 129 | EXT_INIT_XR_FUNC(xrQuerySpatialComponentDataEXT); |
| 130 | EXT_INIT_XR_FUNC(xrDestroySpatialSnapshotEXT); |
| 131 | EXT_INIT_XR_FUNC(xrCreateSpatialEntityFromIdEXT); |
| 132 | EXT_INIT_XR_FUNC(xrDestroySpatialEntityEXT); |
| 133 | EXT_INIT_XR_FUNC(xrCreateSpatialUpdateSnapshotEXT); |
| 134 | EXT_INIT_XR_FUNC(xrGetSpatialBufferStringEXT); |
| 135 | EXT_INIT_XR_FUNC(xrGetSpatialBufferUint8EXT); |
| 136 | EXT_INIT_XR_FUNC(xrGetSpatialBufferUint16EXT); |
| 137 | EXT_INIT_XR_FUNC(xrGetSpatialBufferUint32EXT); |
| 138 | EXT_INIT_XR_FUNC(xrGetSpatialBufferFloatEXT); |
| 139 | EXT_INIT_XR_FUNC(xrGetSpatialBufferVector2fEXT); |
| 140 | EXT_INIT_XR_FUNC(xrGetSpatialBufferVector3fEXT); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void OpenXRSpatialEntityExtension::on_instance_destroyed() { |
| 145 | supported_capabilities.clear(); |
| 146 | capabilities_load_state = 0; |
| 147 | |
| 148 | xrEnumerateSpatialCapabilitiesEXT_ptr = nullptr; |
| 149 | xrEnumerateSpatialCapabilityComponentTypesEXT_ptr = nullptr; |
| 150 | xrEnumerateSpatialCapabilityFeaturesEXT_ptr = nullptr; |
| 151 | xrCreateSpatialContextAsyncEXT_ptr = nullptr; |
| 152 | xrCreateSpatialContextCompleteEXT_ptr = nullptr; |
| 153 | xrDestroySpatialContextEXT_ptr = nullptr; |
| 154 | xrCreateSpatialDiscoverySnapshotAsyncEXT_ptr = nullptr; |
| 155 | xrCreateSpatialDiscoverySnapshotCompleteEXT_ptr = nullptr; |
| 156 | xrQuerySpatialComponentDataEXT_ptr = nullptr; |
| 157 | xrDestroySpatialSnapshotEXT_ptr = nullptr; |
| 158 | xrCreateSpatialEntityFromIdEXT_ptr = nullptr; |
| 159 | xrDestroySpatialEntityEXT_ptr = nullptr; |
| 160 | xrCreateSpatialUpdateSnapshotEXT_ptr = nullptr; |
| 161 | xrGetSpatialBufferStringEXT_ptr = nullptr; |
| 162 | xrGetSpatialBufferUint8EXT_ptr = nullptr; |
| 163 | xrGetSpatialBufferUint16EXT_ptr = nullptr; |
| 164 | xrGetSpatialBufferUint32EXT_ptr = nullptr; |
| 165 | xrGetSpatialBufferFloatEXT_ptr = nullptr; |
| 166 | xrGetSpatialBufferVector2fEXT_ptr = nullptr; |
| 167 | xrGetSpatialBufferVector3fEXT_ptr = nullptr; |
| 168 | } |
| 169 | |
| 170 | void OpenXRSpatialEntityExtension::on_session_destroyed() { |
| 171 | if (!get_active()) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 176 | ERR_FAIL_NULL(openxr_api); |
| 177 | |
| 178 | // Cleanup remaining entity RIDs. |
| 179 | LocalVector<RID> spatial_entity_rids = spatial_entity_owner.get_owned_list(); |
| 180 | for (const RID &rid : spatial_entity_rids) { |
| 181 | if (is_print_verbose_enabled()) { |
| 182 | SpatialEntityData *spatial_entity_data = spatial_entity_owner.get_or_null(rid); |
| 183 | if (spatial_entity_data) { // Should never be nullptr seeing we called get_owned_list just now, but just in case. |
| 184 | print_line("OpenXR: Found orphaned spatial entity with ID ", String::num_int64(spatial_entity_data->entity_id)); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | free_spatial_entity(rid); |
| 189 | } |
| 190 | |
| 191 | // Cleanup remaining snapshot RIDs. |
| 192 | LocalVector<RID> spatial_snapshot_rids = spatial_snapshot_owner.get_owned_list(); |
| 193 | if (!spatial_snapshot_rids.is_empty()) { |
| 194 | print_verbose("OpenXR: Found " + String::num_int64(spatial_snapshot_rids.size()) + " orphaned spatial snapshots"); // Don't have useful data to report here so just report count. |
| 195 | for (const RID &rid : spatial_snapshot_rids) { |
| 196 | free_spatial_snapshot(rid); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // Clean up all remaining spatial context RIDs. |
| 201 | LocalVector<RID> spatial_context_rids = spatial_context_owner.get_owned_list(); |
| 202 | if (!spatial_context_rids.is_empty()) { |
| 203 | print_verbose("OpenXR: Found " + String::num_int64(spatial_context_rids.size()) + " orphaned spatial contexts"); // Don't have useful data to report here so just report count. |
| 204 | for (const RID &rid : spatial_context_rids) { |
| 205 | free_spatial_context(rid); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | bool OpenXRSpatialEntityExtension::get_active() const { |
| 211 | return spatial_entity_ext; |
| 212 | } |
| 213 | |
| 214 | bool OpenXRSpatialEntityExtension::_load_capabilities() { |
| 215 | if (capabilities_load_state == 0) { |
| 216 | if (!spatial_entity_ext) { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 221 | ERR_FAIL_NULL_V(openxr_api, false); |
| 222 | |
| 223 | XrInstance instance = openxr_api->get_instance(); |
| 224 | ERR_FAIL_COND_V(instance == XR_NULL_HANDLE, false); |
| 225 | XrSystemId system_id = openxr_api->get_system_id(); |
| 226 | ERR_FAIL_COND_V(system_id == 0, false); |
| 227 | |
| 228 | // If we fail before this point, this may be called too early. |
| 229 | // Assume failure so we don't keep trying this unless we succeed. |
| 230 | capabilities_load_state = 2; |
| 231 | |
| 232 | // Check our capabilities. |
| 233 | Vector<XrSpatialCapabilityEXT> capabilities; |
| 234 | uint32_t capability_size = 0; |
| 235 | XrResult result = xrEnumerateSpatialCapabilitiesEXT(instance, system_id, 0, &capability_size, nullptr); |
| 236 | if (XR_FAILED(result)) { |
| 237 | // Not successful? then exit. |
| 238 | ERR_FAIL_V_MSG(false, "OpenXR: Failed to get spatial entity capability count [" + openxr_api->get_error_string(result) + "]"); |
| 239 | } |
| 240 | |
| 241 | if (capability_size > 0) { |
| 242 | capabilities.resize(capability_size); |
| 243 | result = xrEnumerateSpatialCapabilitiesEXT(instance, system_id, capabilities.size(), &capability_size, capabilities.ptrw()); |
| 244 | if (XR_FAILED(result)) { |
| 245 | // Not successful? then exit. |
| 246 | ERR_FAIL_V_MSG(false, "OpenXR: Failed to get spatial entity capabilities [" + openxr_api->get_error_string(result) + "]"); |
| 247 | } |
| 248 | |
| 249 | // Loop through capabilities |
| 250 | for (const XrSpatialCapabilityEXT &capability : capabilities) { |
| 251 | print_verbose("OpenXR: Found spatial entity capability " + get_spatial_capability_name(capability) + "."); |
| 252 | |
| 253 | SpatialEntityCapabality &spatial_entity_capability = supported_capabilities[capability]; |
| 254 | |
| 255 | // retrieve component types for this capability |
| 256 | XrSpatialCapabilityComponentTypesEXT component_types = { |
| 257 | XR_TYPE_SPATIAL_CAPABILITY_COMPONENT_TYPES_EXT, // type |
| 258 | nullptr, // next |
| 259 | 0, // componentTypeCapacityInput |
| 260 | 0, // componentTypeCountOutput |
| 261 | nullptr // componentTypes |
| 262 | }; |
| 263 | result = xrEnumerateSpatialCapabilityComponentTypesEXT(instance, system_id, capability, &component_types); |
| 264 | if (XR_FAILED(result)) { |
| 265 | // Not successful? just keep going. |
| 266 | ERR_PRINT("OpenXR: Failed to get spatial entity component type count [" + openxr_api->get_error_string(result) + "]"); |
| 267 | } else if (component_types.componentTypeCountOutput > 0) { |
| 268 | spatial_entity_capability.component_types.resize(component_types.componentTypeCountOutput); |
| 269 | component_types.componentTypeCapacityInput = spatial_entity_capability.component_types.size(); |
| 270 | component_types.componentTypeCountOutput = 0; |
| 271 | component_types.componentTypes = spatial_entity_capability.component_types.ptrw(); |
| 272 | result = xrEnumerateSpatialCapabilityComponentTypesEXT(instance, system_id, capability, &component_types); |
| 273 | if (XR_FAILED(result)) { |
| 274 | // Not successful? just keep going. |
| 275 | ERR_PRINT("OpenXR: Failed to get spatial entity component types [" + openxr_api->get_error_string(result) + "]"); |
| 276 | } else if (is_print_verbose_enabled()) { |
| 277 | for (const XrSpatialComponentTypeEXT &component_type : spatial_entity_capability.component_types) { |
| 278 | print_verbose("- component type " + get_spatial_component_type_name(component_type)); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // Retrieve features for this capability |
| 284 | result = xrEnumerateSpatialCapabilityFeaturesEXT(instance, system_id, capability, 0, &capability_size, nullptr); |
| 285 | if (XR_FAILED(result)) { |
| 286 | // Not successful? just keep going. |
| 287 | ERR_PRINT("OpenXR: Failed to get spatial entity feature count [" + openxr_api->get_error_string(result) + "]"); |
| 288 | } else if (capability_size > 0) { |
| 289 | spatial_entity_capability.features.resize(capability_size); |
| 290 | result = xrEnumerateSpatialCapabilityFeaturesEXT(instance, system_id, capability, spatial_entity_capability.features.size(), &capability_size, spatial_entity_capability.features.ptrw()); |
| 291 | if (XR_FAILED(result)) { |
| 292 | // Not successful? just keep going. |
| 293 | ERR_PRINT("OpenXR: Failed to get spatial entity features [" + openxr_api->get_error_string(result) + "]"); |
| 294 | } else if (is_print_verbose_enabled()) { |
| 295 | for (const XrSpatialCapabilityFeatureEXT &feature : spatial_entity_capability.features) { |
| 296 | print_verbose("- feature " + get_spatial_feature_name(feature)); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | capabilities_load_state = 1; // success! |
| 304 | } |
| 305 | |
| 306 | return capabilities_load_state == 1; |
| 307 | } |
| 308 | |
| 309 | bool OpenXRSpatialEntityExtension::supports_capability(XrSpatialCapabilityEXT p_capability) { |
| 310 | if (!_load_capabilities()) { |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | return supported_capabilities.has(p_capability); |
| 315 | } |
| 316 | |
| 317 | bool OpenXRSpatialEntityExtension::_supports_capability(Capability p_capability) { |
| 318 | return supports_capability((XrSpatialCapabilityEXT)p_capability); |
| 319 | } |
| 320 | |
| 321 | bool OpenXRSpatialEntityExtension::supports_component_type(XrSpatialCapabilityEXT p_capability, XrSpatialComponentTypeEXT p_component_type) { |
| 322 | if (!_load_capabilities()) { |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | if (supported_capabilities.has(p_capability)) { |
| 327 | return supported_capabilities[p_capability].component_types.has(p_component_type); |
| 328 | } |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | bool OpenXRSpatialEntityExtension::_supports_component_type(Capability p_capability, ComponentType p_component_type) { |
| 333 | return supports_component_type((XrSpatialCapabilityEXT)p_capability, (XrSpatialComponentTypeEXT)p_component_type); |
| 334 | } |
| 335 | |
| 336 | bool OpenXRSpatialEntityExtension::on_event_polled(const XrEventDataBuffer &event) { |
| 337 | if (!get_active()) { |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 342 | ERR_FAIL_NULL_V(openxr_api, false); |
| 343 | |
| 344 | switch (event.type) { |
| 345 | case XR_TYPE_EVENT_DATA_SPATIAL_DISCOVERY_RECOMMENDED_EXT: { |
| 346 | const XrEventDataSpatialDiscoveryRecommendedEXT *eventdata = (const XrEventDataSpatialDiscoveryRecommendedEXT *)&event; |
| 347 | |
| 348 | // TODO: Should maybe keep a HashMap for a reverse lookup. |
| 349 | |
| 350 | LocalVector<RID> spatial_context_rids = spatial_context_owner.get_owned_list(); |
| 351 | for (const RID &rid : spatial_context_rids) { |
| 352 | if (get_spatial_context_handle(rid) == eventdata->spatialContext) { |
| 353 | emit_signal(SNAME("spatial_discovery_recommended"), rid); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | return true; |
| 358 | } break; |
| 359 | default: { |
| 360 | return false; |
| 361 | } break; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | //////////////////////////////////////////////////////////////////////////// |
| 366 | // Spatial contexts |
| 367 | |
| 368 | Ref<OpenXRFutureResult> OpenXRSpatialEntityExtension::create_spatial_context(const TypedArray<OpenXRSpatialCapabilityConfigurationBaseHeader> &p_capability_configurations, Ref<OpenXRStructureBase> p_next, const Callable &p_user_callback) { |
| 369 | if (!get_active()) { |
| 370 | return nullptr; |
| 371 | } |
| 372 | |
| 373 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 374 | ERR_FAIL_NULL_V(openxr_api, nullptr); |
| 375 | |
| 376 | OpenXRFutureExtension *future_api = OpenXRFutureExtension::get_singleton(); |
| 377 | ERR_FAIL_NULL_V(future_api, nullptr); |
| 378 | |
| 379 | // Parse our configuration. |
| 380 | Vector<XrSpatialCapabilityConfigurationBaseHeaderEXT *> configuration; |
| 381 | for (Ref<OpenXRSpatialCapabilityConfigurationBaseHeader> capability_configuration : p_capability_configurations) { |
| 382 | ERR_FAIL_COND_V(capability_configuration.is_null(), nullptr); |
| 383 | |
| 384 | XrSpatialCapabilityConfigurationBaseHeaderEXT *config = capability_configuration->get_configuration(); |
| 385 | if (config != nullptr) { |
| 386 | configuration.push_back(config); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | void *next = nullptr; |
| 391 | if (p_next.is_valid()) { |
| 392 | next = p_next->get_header(next); |
| 393 | } |
| 394 | |
| 395 | XrSpatialContextCreateInfoEXT create_info = { |
| 396 | XR_TYPE_SPATIAL_CONTEXT_CREATE_INFO_EXT, // type |
| 397 | next, // next |
| 398 | uint32_t(configuration.size()), // capabilityConfigCount |
| 399 | configuration.is_empty() ? nullptr : configuration.ptr(), // capabilityConfigs |
| 400 | }; |
| 401 | XrFutureEXT future = XR_NULL_HANDLE; |
| 402 | XrResult xr_result = xrCreateSpatialContextAsyncEXT(openxr_api->get_session(), &create_info, &future); |
| 403 | if (XR_FAILED(xr_result)) { |
| 404 | // Not successful? then exit. |
| 405 | ERR_FAIL_V_MSG(Ref<OpenXRFutureResult>(), "OpenXR: Failed to create spatial context [" + openxr_api->get_error_string(xr_result) + "]"); |
| 406 | } |
| 407 | |
| 408 | // Create our future result |
| 409 | Ref<OpenXRFutureResult> future_result = future_api->register_future(future, callable_mp(this, &OpenXRSpatialEntityExtension::_on_context_creation_ready).bind(p_user_callback)); |
| 410 | |
| 411 | return future_result; |
| 412 | } |
| 413 | |
| 414 | void OpenXRSpatialEntityExtension::_on_context_creation_ready(Ref<OpenXRFutureResult> p_future_result, const Callable &p_user_callback) { |
| 415 | // Complete context creation... |
| 416 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 417 | ERR_FAIL_NULL(openxr_api); |
| 418 | |
| 419 | XrCreateSpatialContextCompletionEXT completion = { |
| 420 | XR_TYPE_CREATE_SPATIAL_CONTEXT_COMPLETION_EXT, // type |
| 421 | nullptr, // next |
| 422 | XR_RESULT_MAX_ENUM, // futureResult |
| 423 | XR_NULL_HANDLE // spatialContext |
| 424 | }; |
| 425 | XrResult result = xrCreateSpatialContextCompleteEXT(openxr_api->get_session(), p_future_result->get_future(), &completion); |
| 426 | if (XR_FAILED(result)) { // Did our xrCreateSpatialContextCompleteEXT call fail? |
| 427 | // Log issue and fail. |
| 428 | ERR_FAIL_MSG("OpenXR: Failed to complete spatial context create future [" + openxr_api->get_error_string(result) + "]"); |
| 429 | } |
| 430 | if (XR_FAILED(completion.futureResult)) { // Did our completion fail? |
| 431 | // Log issue and fail. |
| 432 | ERR_FAIL_MSG("OpenXR: Failed to complete spatial context creation [" + openxr_api->get_error_string(completion.futureResult) + "]"); |
| 433 | } |
| 434 | |
| 435 | // Wrap our spatial context |
| 436 | SpatialContextData spatial_context_data; |
| 437 | spatial_context_data.spatial_context = completion.spatialContext; |
| 438 | |
| 439 | // Store this as an RID so we keep track of it. |
| 440 | RID context_rid = spatial_context_owner.make_rid(spatial_context_data); |
| 441 | |
| 442 | // Set our RID as our result value on our future. |
| 443 | p_future_result->set_result_value(context_rid); |
| 444 | |
| 445 | // And perform our callback if we have one. |
| 446 | if (p_user_callback.is_valid()) { |
| 447 | p_user_callback.call(context_rid); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | bool OpenXRSpatialEntityExtension::get_spatial_context_ready(RID p_spatial_context) const { |
| 452 | SpatialContextData *context_data = spatial_context_owner.get_or_null(p_spatial_context); |
| 453 | ERR_FAIL_NULL_V(context_data, false); |
| 454 | |
| 455 | return context_data->spatial_context != XR_NULL_HANDLE; |
| 456 | } |
| 457 | |
| 458 | void OpenXRSpatialEntityExtension::free_spatial_context(RID p_spatial_context) { |
| 459 | SpatialContextData *context_data = spatial_context_owner.get_or_null(p_spatial_context); |
| 460 | ERR_FAIL_NULL(context_data); |
| 461 | |
| 462 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 463 | ERR_FAIL_NULL(openxr_api); |
| 464 | |
| 465 | if (context_data->spatial_context != XR_NULL_HANDLE) { |
| 466 | // Destroy our spatial context |
| 467 | XrResult result = xrDestroySpatialContextEXT(context_data->spatial_context); |
| 468 | if (XR_FAILED(result)) { |
| 469 | WARN_PRINT("OpenXR: Failed to destroy the spatial context [" + openxr_api->get_error_string(result) + "]"); |
| 470 | } |
| 471 | context_data->spatial_context = XR_NULL_HANDLE; |
| 472 | |
| 473 | // And remove our RID. |
| 474 | spatial_context_owner.free(p_spatial_context); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | XrSpatialContextEXT OpenXRSpatialEntityExtension::get_spatial_context_handle(RID p_spatial_context) const { |
| 479 | SpatialContextData *context_data = spatial_context_owner.get_or_null(p_spatial_context); |
| 480 | ERR_FAIL_NULL_V(context_data, XR_NULL_HANDLE); |
| 481 | |
| 482 | return context_data->spatial_context; |
| 483 | } |
| 484 | |
| 485 | // For exposing this to GDExtension |
| 486 | uint64_t OpenXRSpatialEntityExtension::_get_spatial_context_handle(RID p_spatial_context) const { |
| 487 | return (uint64_t)get_spatial_context_handle(p_spatial_context); |
| 488 | } |
| 489 | |
| 490 | //////////////////////////////////////////////////////////////////////////// |
| 491 | // Discovery queries |
| 492 | |
| 493 | Ref<OpenXRFutureResult> OpenXRSpatialEntityExtension::discover_spatial_entities(RID p_spatial_context, const Vector<XrSpatialComponentTypeEXT> &p_component_types, Ref<OpenXRStructureBase> p_next, const Callable &p_user_callback) { |
| 494 | if (!get_active()) { |
| 495 | return nullptr; |
| 496 | } |
| 497 | |
| 498 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 499 | ERR_FAIL_NULL_V(openxr_api, nullptr); |
| 500 | |
| 501 | OpenXRFutureExtension *future_api = OpenXRFutureExtension::get_singleton(); |
| 502 | ERR_FAIL_NULL_V(future_api, nullptr); |
| 503 | |
| 504 | void *next = nullptr; |
| 505 | if (p_next.is_valid()) { |
| 506 | next = p_next->get_header(next); |
| 507 | } |
| 508 | |
| 509 | // Start our discovery snapshot. |
| 510 | XrSpatialDiscoverySnapshotCreateInfoEXT create_info = { |
| 511 | XR_TYPE_SPATIAL_DISCOVERY_SNAPSHOT_CREATE_INFO_EXT, // type |
| 512 | next, // next |
| 513 | (uint32_t)p_component_types.size(), // componentTypeCount |
| 514 | p_component_types.is_empty() ? nullptr : p_component_types.ptr() // componentTypes |
| 515 | }; |
| 516 | |
| 517 | XrFutureEXT future; |
| 518 | XrResult result = xrCreateSpatialDiscoverySnapshotAsyncEXT(get_spatial_context_handle(p_spatial_context), &create_info, &future); |
| 519 | if (XR_FAILED(result)) { |
| 520 | ERR_FAIL_V_MSG(nullptr, "OpenXR: Failed to initiate snapshot discovery [" + openxr_api->get_error_string(result) + "]"); |
| 521 | } |
| 522 | |
| 523 | // Create our future result |
| 524 | Ref<OpenXRFutureResult> future_result = future_api->register_future(future, callable_mp(this, &OpenXRSpatialEntityExtension::_on_discovered_spatial_entities).bind(p_spatial_context, p_user_callback)); |
| 525 | |
| 526 | return future_result; |
| 527 | } |
| 528 | |
| 529 | // For calls from GDExtension |
| 530 | Ref<OpenXRFutureResult> OpenXRSpatialEntityExtension::_discover_spatial_entities(RID p_spatial_context, const PackedInt64Array &p_component_types, Ref<OpenXRStructureBase> p_next, const Callable &p_callback) { |
| 531 | Vector<XrSpatialComponentTypeEXT> component_types; |
| 532 | component_types.resize(p_component_types.size()); |
| 533 | XrSpatialComponentTypeEXT *ptr = component_types.ptrw(); |
| 534 | for (const int64_t &component_type : p_component_types) { |
| 535 | *ptr = (XrSpatialComponentTypeEXT)component_type; |
| 536 | ptr++; |
| 537 | } |
| 538 | |
| 539 | return discover_spatial_entities(p_spatial_context, component_types, p_next, p_callback); |
| 540 | } |
| 541 | |
| 542 | void OpenXRSpatialEntityExtension::_on_discovered_spatial_entities(Ref<OpenXRFutureResult> p_future_result, RID p_discovery_spatial_context, const Callable &p_user_callback) { |
| 543 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 544 | ERR_FAIL_NULL(openxr_api); |
| 545 | |
| 546 | XrSpatialContextEXT xr_spatial_context = get_spatial_context_handle(p_discovery_spatial_context); |
| 547 | ERR_FAIL_COND(xr_spatial_context == XR_NULL_HANDLE); |
| 548 | |
| 549 | XrCreateSpatialDiscoverySnapshotCompletionInfoEXT completion_info = { |
| 550 | XR_TYPE_CREATE_SPATIAL_DISCOVERY_SNAPSHOT_COMPLETION_INFO_EXT, // type |
| 551 | nullptr, // next |
| 552 | openxr_api->get_play_space(), // baseSpace |
| 553 | openxr_api->get_predicted_display_time(), // time |
| 554 | p_future_result->get_future() // future |
| 555 | }; |
| 556 | |
| 557 | XrCreateSpatialDiscoverySnapshotCompletionEXT completion = { |
| 558 | XR_TYPE_CREATE_SPATIAL_DISCOVERY_SNAPSHOT_COMPLETION_EXT, // type |
| 559 | nullptr, // next |
| 560 | XR_SUCCESS, // futureResult |
| 561 | XR_NULL_HANDLE // snapshot |
| 562 | }; |
| 563 | XrResult result = xrCreateSpatialDiscoverySnapshotCompleteEXT(xr_spatial_context, &completion_info, &completion); |
| 564 | |
| 565 | if (XR_FAILED(result)) { // Did our xrCreateSpatialContextCompleteEXT call fail? |
| 566 | // And log issue. |
| 567 | ERR_FAIL_MSG("OpenXR: Failed to complete discovery query future [" + openxr_api->get_error_string(result) + "]"); |
| 568 | } |
| 569 | if (XR_FAILED(completion.futureResult)) { // Did our completion fail? |
| 570 | // And log issue. |
| 571 | ERR_FAIL_MSG("OpenXR: Failed to complete discovery query [" + openxr_api->get_error_string(completion.futureResult) + "]"); |
| 572 | } |
| 573 | |
| 574 | // Wrap our spatial snapshot |
| 575 | SpatialSnapshotData snapshot_data; |
| 576 | snapshot_data.spatial_context = p_discovery_spatial_context; |
| 577 | snapshot_data.spatial_snapshot = completion.snapshot; |
| 578 | |
| 579 | // Store this as an RID so we keep track of it. |
| 580 | RID snapshot_rid = spatial_snapshot_owner.make_rid(snapshot_data); |
| 581 | |
| 582 | // Set our RID as our result value on our future. |
| 583 | p_future_result->set_result_value(snapshot_rid); |
| 584 | |
| 585 | // And perform our callback if we have one. |
| 586 | if (p_user_callback.is_valid()) { |
| 587 | p_user_callback.call(snapshot_rid); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | //////////////////////////////////////////////////////////////////////////// |
| 592 | // Update query |
| 593 | |
| 594 | RID OpenXRSpatialEntityExtension::update_spatial_entities(RID p_spatial_context, const LocalVector<RID> &p_entities, const LocalVector<XrSpatialComponentTypeEXT> &p_component_types, Ref<OpenXRStructureBase> p_next) { |
| 595 | if (!get_active()) { |
| 596 | return RID(); |
| 597 | } |
| 598 | |
| 599 | ERR_FAIL_COND_V(p_entities.is_empty(), RID()); |
| 600 | |
| 601 | SpatialContextData *context_data = spatial_context_owner.get_or_null(p_spatial_context); |
| 602 | ERR_FAIL_NULL_V(context_data, RID()); |
| 603 | |
| 604 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 605 | ERR_FAIL_NULL_V(openxr_api, RID()); |
| 606 | |
| 607 | // Convert our entity RIDs to XrSpatialEntityEXT |
| 608 | thread_local LocalVector<XrSpatialEntityEXT> entities; |
| 609 | |
| 610 | entities.resize(p_entities.size()); |
| 611 | XrSpatialEntityEXT *ptr = entities.ptr(); |
| 612 | for (const RID &rid : p_entities) { |
| 613 | SpatialEntityData *entity_data = spatial_entity_owner.get_or_null(rid); |
| 614 | *ptr = entity_data ? entity_data->entity : XR_NULL_HANDLE; |
| 615 | ptr++; |
| 616 | } |
| 617 | |
| 618 | void *next = nullptr; |
| 619 | if (p_next.is_valid()) { |
| 620 | next = p_next->get_header(next); |
| 621 | } |
| 622 | |
| 623 | SpatialSnapshotData spatial_snapshot_data; |
| 624 | |
| 625 | // Store the context we used for this discovery query |
| 626 | spatial_snapshot_data.spatial_context = p_spatial_context; |
| 627 | |
| 628 | // Do update |
| 629 | XrSpatialUpdateSnapshotCreateInfoEXT create_info = { |
| 630 | XR_TYPE_SPATIAL_UPDATE_SNAPSHOT_CREATE_INFO_EXT, // type |
| 631 | next, // next |
| 632 | (uint32_t)entities.size(), // entityCount, |
| 633 | entities.ptr(), // entities |
| 634 | (uint32_t)p_component_types.size(), // componentTypeCount |
| 635 | p_component_types.is_empty() ? nullptr : p_component_types.ptr(), // componentTypes |
| 636 | openxr_api->get_play_space(), // baseSpace |
| 637 | openxr_api->get_predicted_display_time() // time |
| 638 | }; |
| 639 | XrResult result = xrCreateSpatialUpdateSnapshotEXT(context_data->spatial_context, &create_info, &spatial_snapshot_data.spatial_snapshot); |
| 640 | if (XR_FAILED(result)) { |
| 641 | ERR_FAIL_V_MSG(RID(), "OpenXR: Failed to create update snapshot [" + openxr_api->get_error_string(result) + "]"); |
| 642 | } |
| 643 | |
| 644 | // Store our snapshot in an RID and return. |
| 645 | return spatial_snapshot_owner.make_rid(spatial_snapshot_data); |
| 646 | } |
| 647 | |
| 648 | RID OpenXRSpatialEntityExtension::_update_spatial_entities(RID p_spatial_context, const TypedArray<RID> &p_entities, const PackedInt64Array &p_component_types, Ref<OpenXRStructureBase> p_next) { |
| 649 | thread_local LocalVector<RID> entities; |
| 650 | entities.resize(p_entities.size()); |
| 651 | RID *rids = entities.ptr(); |
| 652 | for (const RID rid : p_entities) { |
| 653 | *rids = rid; |
| 654 | rids++; |
| 655 | } |
| 656 | |
| 657 | thread_local LocalVector<XrSpatialComponentTypeEXT> component_types; |
| 658 | component_types.resize(p_component_types.size()); |
| 659 | XrSpatialComponentTypeEXT *ptr = component_types.ptr(); |
| 660 | for (const int64_t &component_type : p_component_types) { |
| 661 | *ptr = (XrSpatialComponentTypeEXT)component_type; |
| 662 | ptr++; |
| 663 | } |
| 664 | |
| 665 | return update_spatial_entities(p_spatial_context, entities, component_types, p_next); |
| 666 | } |
| 667 | |
| 668 | //////////////////////////////////////////////////////////////////////////// |
| 669 | // Snapshot data |
| 670 | |
| 671 | void OpenXRSpatialEntityExtension::free_spatial_snapshot(RID p_spatial_snapshot) { |
| 672 | SpatialSnapshotData *snapshot_data = spatial_snapshot_owner.get_or_null(p_spatial_snapshot); |
| 673 | ERR_FAIL_NULL(snapshot_data); |
| 674 | |
| 675 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 676 | ERR_FAIL_NULL(openxr_api); |
| 677 | |
| 678 | if (snapshot_data->spatial_snapshot != XR_NULL_HANDLE) { |
| 679 | // Destroy our spatial context |
| 680 | XrResult result = xrDestroySpatialSnapshotEXT(snapshot_data->spatial_snapshot); |
| 681 | if (XR_FAILED(result)) { |
| 682 | WARN_PRINT("OpenXR: Failed to destroy the spatial snapshot [" + openxr_api->get_error_string(result) + "]"); |
| 683 | } |
| 684 | snapshot_data->spatial_snapshot = XR_NULL_HANDLE; |
| 685 | } |
| 686 | |
| 687 | // And remove our RID. |
| 688 | spatial_snapshot_owner.free(p_spatial_snapshot); |
| 689 | } |
| 690 | |
| 691 | XrSpatialSnapshotEXT OpenXRSpatialEntityExtension::get_spatial_snapshot_handle(RID p_spatial_snapshot) const { |
| 692 | SpatialSnapshotData *snapshot_data = spatial_snapshot_owner.get_or_null(p_spatial_snapshot); |
| 693 | ERR_FAIL_NULL_V(snapshot_data, XR_NULL_HANDLE); |
| 694 | |
| 695 | return snapshot_data->spatial_snapshot; |
| 696 | } |
| 697 | |
| 698 | RID OpenXRSpatialEntityExtension::get_spatial_snapshot_context(RID p_spatial_snapshot) const { |
| 699 | SpatialSnapshotData *snapshot_data = spatial_snapshot_owner.get_or_null(p_spatial_snapshot); |
| 700 | ERR_FAIL_NULL_V(snapshot_data, RID()); |
| 701 | |
| 702 | return snapshot_data->spatial_context; |
| 703 | } |
| 704 | |
| 705 | // For exposing this to GDExtension |
| 706 | uint64_t OpenXRSpatialEntityExtension::_get_spatial_snapshot_handle(RID p_spatial_snapshot) const { |
| 707 | return (uint64_t)get_spatial_snapshot_handle(p_spatial_snapshot); |
| 708 | } |
| 709 | |
| 710 | bool OpenXRSpatialEntityExtension::query_snapshot(RID p_spatial_snapshot, const TypedArray<OpenXRSpatialComponentData> &p_component_data, Ref<OpenXRStructureBase> p_next) { |
| 711 | SpatialSnapshotData *snapshot_data = spatial_snapshot_owner.get_or_null(p_spatial_snapshot); |
| 712 | ERR_FAIL_NULL_V(snapshot_data, false); |
| 713 | |
| 714 | ERR_FAIL_COND_V(p_component_data.is_empty(), false); |
| 715 | |
| 716 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 717 | ERR_FAIL_NULL_V(openxr_api, false); |
| 718 | |
| 719 | Ref<OpenXRSpatialQueryResultData> query_result_data = p_component_data[0]; |
| 720 | ERR_FAIL_COND_V_MSG(query_result_data.is_null(), false, "OpenXR: The first component must be of type OpenXRSpatialQueryResultData"); |
| 721 | |
| 722 | // Gather component types we need to query. |
| 723 | Vector<XrSpatialComponentTypeEXT> component_types; |
| 724 | for (Ref<OpenXRSpatialComponentData> component_data : p_component_data) { |
| 725 | if (component_data.is_valid()) { |
| 726 | XrSpatialComponentTypeEXT component_type = component_data->get_component_type(); |
| 727 | if (component_type != XR_SPATIAL_COMPONENT_TYPE_MAX_ENUM_EXT) { |
| 728 | component_types.push_back(component_type); |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | void *next = nullptr; |
| 734 | if (p_next.is_valid()) { |
| 735 | next = p_next->get_header(next); |
| 736 | } |
| 737 | |
| 738 | XrSpatialComponentDataQueryConditionEXT query_condition = { |
| 739 | XR_TYPE_SPATIAL_COMPONENT_DATA_QUERY_CONDITION_EXT, // type |
| 740 | next, // next |
| 741 | 0, // componentTypeCount |
| 742 | nullptr // componentTypes |
| 743 | }; |
| 744 | |
| 745 | query_condition.componentTypeCount = component_types.size(); |
| 746 | query_condition.componentTypes = component_types.ptr(); |
| 747 | |
| 748 | XrSpatialComponentDataQueryResultEXT *query_result = (XrSpatialComponentDataQueryResultEXT *)query_result_data->get_structure_data(nullptr); |
| 749 | XrResult result = xrQuerySpatialComponentDataEXT(snapshot_data->spatial_snapshot, &query_condition, query_result); |
| 750 | if (XR_FAILED(result)) { |
| 751 | ERR_FAIL_V_MSG(false, "OpenXR: Failed to query snapshot count [" + openxr_api->get_error_string(result) + "]"); |
| 752 | } |
| 753 | |
| 754 | // Nothing to do? |
| 755 | if (query_result->entityIdCountOutput == 0) { |
| 756 | return true; |
| 757 | } |
| 758 | |
| 759 | // This indicates an issue in the XR runtime, we should have a state for every entity so these counts must match. |
| 760 | ERR_FAIL_COND_V_MSG(query_result->entityIdCountOutput != query_result->entityStateCountOutput, false, "OpenXR: Entity ID count and entity state count don't match!"); |
| 761 | |
| 762 | // Allocate our memory and parse our next structure |
| 763 | next = nullptr; |
| 764 | for (Ref<OpenXRSpatialComponentData> component_data : p_component_data) { |
| 765 | if (component_data.is_valid()) { |
| 766 | component_data->set_capacity(query_result->entityIdCountOutput); |
| 767 | XrSpatialComponentTypeEXT component_type = component_data->get_component_type(); |
| 768 | if (component_type != XR_SPATIAL_COMPONENT_TYPE_MAX_ENUM_EXT) { |
| 769 | next = component_data->get_structure_data(next); |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | query_result = (XrSpatialComponentDataQueryResultEXT *)query_result_data->get_structure_data(next); |
| 775 | result = xrQuerySpatialComponentDataEXT(snapshot_data->spatial_snapshot, &query_condition, query_result); |
| 776 | if (XR_FAILED(result)) { |
| 777 | ERR_FAIL_V_MSG(false, "OpenXR: Failed to query snapshot data [" + openxr_api->get_error_string(result) + "]"); |
| 778 | } |
| 779 | |
| 780 | return true; |
| 781 | } |
| 782 | |
| 783 | //////////////////////////////////////////////////////////////////////////// |
| 784 | // Buffers from snapshot |
| 785 | |
| 786 | String OpenXRSpatialEntityExtension::get_string(RID p_spatial_snapshot, XrSpatialBufferIdEXT p_buffer_id) const { |
| 787 | String ret; |
| 788 | |
| 789 | SpatialSnapshotData *snapshot_data = spatial_snapshot_owner.get_or_null(p_spatial_snapshot); |
| 790 | ERR_FAIL_NULL_V(snapshot_data, ret); |
| 791 | |
| 792 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 793 | ERR_FAIL_NULL_V(openxr_api, ret); |
| 794 | |
| 795 | XrSpatialBufferGetInfoEXT info = { |
| 796 | XR_TYPE_SPATIAL_BUFFER_GET_INFO_EXT, // type |
| 797 | nullptr, // next |
| 798 | p_buffer_id, // bufferId |
| 799 | }; |
| 800 | |
| 801 | uint32_t count = 0; |
| 802 | XrResult result = xrGetSpatialBufferStringEXT(snapshot_data->spatial_snapshot, &info, 0, &count, nullptr); |
| 803 | if (XR_FAILED(result)) { |
| 804 | ERR_FAIL_V_MSG(ret, "OpenXR: Failed to get buffer size [" + openxr_api->get_error_string(result) + "]"); |
| 805 | } |
| 806 | |
| 807 | LocalVector<char> buffer; |
| 808 | buffer.resize(count + 1); |
| 809 | buffer[count] = '\0'; // + 1 and setting a zero terminator just in case runtime is not including this. |
| 810 | |
| 811 | result = xrGetSpatialBufferStringEXT(snapshot_data->spatial_snapshot, &info, buffer.size(), &count, buffer.ptr()); |
| 812 | if (XR_FAILED(result)) { |
| 813 | ERR_FAIL_V_MSG(ret, "OpenXR: Failed to get buffer [" + openxr_api->get_error_string(result) + "]"); |
| 814 | } |
| 815 | |
| 816 | ret = String::utf8(buffer.ptr()); |
| 817 | return ret; |
| 818 | } |
| 819 | |
| 820 | PackedByteArray OpenXRSpatialEntityExtension::get_uint8_buffer(RID p_spatial_snapshot, XrSpatialBufferIdEXT p_buffer_id) const { |
| 821 | PackedByteArray ret; |
| 822 | |
| 823 | SpatialSnapshotData *snapshot_data = spatial_snapshot_owner.get_or_null(p_spatial_snapshot); |
| 824 | ERR_FAIL_NULL_V(snapshot_data, ret); |
| 825 | |
| 826 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 827 | ERR_FAIL_NULL_V(openxr_api, ret); |
| 828 | |
| 829 | XrSpatialBufferGetInfoEXT info = { |
| 830 | XR_TYPE_SPATIAL_BUFFER_GET_INFO_EXT, // type |
| 831 | nullptr, // next |
| 832 | p_buffer_id, // bufferId |
| 833 | }; |
| 834 | |
| 835 | uint32_t count = 0; |
| 836 | XrResult result = xrGetSpatialBufferUint8EXT(snapshot_data->spatial_snapshot, &info, 0, &count, nullptr); |
| 837 | if (XR_FAILED(result)) { |
| 838 | ERR_FAIL_V_MSG(ret, "OpenXR: Failed to get buffer size [" + openxr_api->get_error_string(result) + "]"); |
| 839 | } |
| 840 | |
| 841 | ret.resize(count); |
| 842 | |
| 843 | result = xrGetSpatialBufferUint8EXT(snapshot_data->spatial_snapshot, &info, ret.size(), &count, (uint8_t *)ret.ptrw()); |
| 844 | if (XR_FAILED(result)) { |
| 845 | ERR_FAIL_V_MSG(PackedByteArray(), "OpenXR: Failed to get buffer [" + openxr_api->get_error_string(result) + "]"); |
| 846 | } |
| 847 | |
| 848 | return ret; |
| 849 | } |
| 850 | |
| 851 | Vector<uint16_t> OpenXRSpatialEntityExtension::get_uint16_buffer(RID p_spatial_snapshot, XrSpatialBufferIdEXT p_buffer_id) const { |
| 852 | Vector<uint16_t> ret; |
| 853 | |
| 854 | SpatialSnapshotData *snapshot_data = spatial_snapshot_owner.get_or_null(p_spatial_snapshot); |
| 855 | ERR_FAIL_NULL_V(snapshot_data, ret); |
| 856 | |
| 857 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 858 | ERR_FAIL_NULL_V(openxr_api, ret); |
| 859 | |
| 860 | XrSpatialBufferGetInfoEXT info = { |
| 861 | XR_TYPE_SPATIAL_BUFFER_GET_INFO_EXT, // type |
| 862 | nullptr, // next |
| 863 | p_buffer_id, // bufferId |
| 864 | }; |
| 865 | |
| 866 | uint32_t count = 0; |
| 867 | XrResult result = xrGetSpatialBufferUint16EXT(snapshot_data->spatial_snapshot, &info, 0, &count, nullptr); |
| 868 | if (XR_FAILED(result)) { |
| 869 | ERR_FAIL_V_MSG(ret, "OpenXR: Failed to get buffer size [" + openxr_api->get_error_string(result) + "]"); |
| 870 | } |
| 871 | |
| 872 | ret.resize(count); |
| 873 | |
| 874 | result = xrGetSpatialBufferUint16EXT(snapshot_data->spatial_snapshot, &info, ret.size(), &count, ret.ptrw()); |
| 875 | if (XR_FAILED(result)) { |
| 876 | ERR_FAIL_V_MSG(Vector<uint16_t>(), "OpenXR: Failed to get buffer [" + openxr_api->get_error_string(result) + "]"); |
| 877 | } |
| 878 | |
| 879 | return ret; |
| 880 | } |
| 881 | |
| 882 | Vector<uint32_t> OpenXRSpatialEntityExtension::get_uint32_buffer(RID p_spatial_snapshot, XrSpatialBufferIdEXT p_buffer_id) const { |
| 883 | Vector<uint32_t> ret; |
| 884 | |
| 885 | SpatialSnapshotData *snapshot_data = spatial_snapshot_owner.get_or_null(p_spatial_snapshot); |
| 886 | ERR_FAIL_NULL_V(snapshot_data, ret); |
| 887 | |
| 888 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 889 | ERR_FAIL_NULL_V(openxr_api, ret); |
| 890 | |
| 891 | XrSpatialBufferGetInfoEXT info = { |
| 892 | XR_TYPE_SPATIAL_BUFFER_GET_INFO_EXT, // type |
| 893 | nullptr, // next |
| 894 | p_buffer_id, // bufferId |
| 895 | }; |
| 896 | |
| 897 | uint32_t count = 0; |
| 898 | XrResult result = xrGetSpatialBufferUint32EXT(snapshot_data->spatial_snapshot, &info, 0, &count, nullptr); |
| 899 | if (XR_FAILED(result)) { |
| 900 | ERR_FAIL_V_MSG(ret, "OpenXR: Failed to get buffer size [" + openxr_api->get_error_string(result) + "]"); |
| 901 | } |
| 902 | |
| 903 | ret.resize(count); |
| 904 | |
| 905 | result = xrGetSpatialBufferUint32EXT(snapshot_data->spatial_snapshot, &info, ret.size(), &count, ret.ptrw()); |
| 906 | if (XR_FAILED(result)) { |
| 907 | ERR_FAIL_V_MSG(Vector<uint32_t>(), "OpenXR: Failed to get buffer [" + openxr_api->get_error_string(result) + "]"); |
| 908 | } |
| 909 | |
| 910 | return ret; |
| 911 | } |
| 912 | |
| 913 | PackedFloat32Array OpenXRSpatialEntityExtension::get_float_buffer(RID p_spatial_snapshot, XrSpatialBufferIdEXT p_buffer_id) const { |
| 914 | PackedFloat32Array ret; |
| 915 | |
| 916 | SpatialSnapshotData *snapshot_data = spatial_snapshot_owner.get_or_null(p_spatial_snapshot); |
| 917 | ERR_FAIL_NULL_V(snapshot_data, ret); |
| 918 | |
| 919 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 920 | ERR_FAIL_NULL_V(openxr_api, ret); |
| 921 | |
| 922 | XrSpatialBufferGetInfoEXT info = { |
| 923 | XR_TYPE_SPATIAL_BUFFER_GET_INFO_EXT, // type |
| 924 | nullptr, // next |
| 925 | p_buffer_id, // bufferId |
| 926 | }; |
| 927 | |
| 928 | uint32_t count = 0; |
| 929 | XrResult result = xrGetSpatialBufferFloatEXT(snapshot_data->spatial_snapshot, &info, 0, &count, nullptr); |
| 930 | if (XR_FAILED(result)) { |
| 931 | ERR_FAIL_V_MSG(ret, "OpenXR: Failed to get buffer size [" + openxr_api->get_error_string(result) + "]"); |
| 932 | } |
| 933 | |
| 934 | ret.resize(count); |
| 935 | |
| 936 | result = xrGetSpatialBufferFloatEXT(snapshot_data->spatial_snapshot, &info, ret.size(), &count, ret.ptrw()); |
| 937 | if (XR_FAILED(result)) { |
| 938 | ERR_FAIL_V_MSG(PackedFloat32Array(), "OpenXR: Failed to get buffer [" + openxr_api->get_error_string(result) + "]"); |
| 939 | } |
| 940 | |
| 941 | return ret; |
| 942 | } |
| 943 | |
| 944 | PackedVector2Array OpenXRSpatialEntityExtension::get_vector2_buffer(RID p_spatial_snapshot, XrSpatialBufferIdEXT p_buffer_id) const { |
| 945 | PackedVector2Array ret; |
| 946 | |
| 947 | SpatialSnapshotData *snapshot_data = spatial_snapshot_owner.get_or_null(p_spatial_snapshot); |
| 948 | ERR_FAIL_NULL_V(snapshot_data, ret); |
| 949 | |
| 950 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 951 | ERR_FAIL_NULL_V(openxr_api, ret); |
| 952 | |
| 953 | XrSpatialBufferGetInfoEXT info = { |
| 954 | XR_TYPE_SPATIAL_BUFFER_GET_INFO_EXT, // type |
| 955 | nullptr, // next |
| 956 | p_buffer_id, // bufferId |
| 957 | }; |
| 958 | |
| 959 | uint32_t count = 0; |
| 960 | XrResult result = xrGetSpatialBufferVector2fEXT(snapshot_data->spatial_snapshot, &info, 0, &count, nullptr); |
| 961 | if (XR_FAILED(result)) { |
| 962 | ERR_FAIL_V_MSG(ret, "OpenXR: Failed to get buffer size [" + openxr_api->get_error_string(result) + "]"); |
| 963 | } |
| 964 | |
| 965 | #ifdef REAL_T_IS_DOUBLE |
| 966 | // OpenXR XrVector2f is using floats, Godot Vector2 is using double, so we need to do a copy. |
| 967 | LocalVector<XrVector2f> buffer; |
| 968 | buffer.resize(count); |
| 969 | |
| 970 | result = xrGetSpatialBufferVector2fEXT(snapshot_data->spatial_snapshot, &info, buffer.size(), &count, buffer.ptr()); |
| 971 | if (XR_FAILED(result)) { |
| 972 | ERR_FAIL_V_MSG(ret, "OpenXR: Failed to get buffer [" + openxr_api->get_error_string(result) + "]"); |
| 973 | } |
| 974 | |
| 975 | ret.resize(count); |
| 976 | Vector2 *ptr = ret.ptrw(); |
| 977 | for (uint32_t i = 0; i < count; i++) { |
| 978 | ptr[i].x = buffer[i].x; |
| 979 | ptr[i].y = buffer[i].y; |
| 980 | } |
| 981 | #else |
| 982 | // OpenXR's XrVector2f and Godots Vector2 should be interchangeable. |
| 983 | ret.resize(count); |
| 984 | |
| 985 | result = xrGetSpatialBufferVector2fEXT(snapshot_data->spatial_snapshot, &info, ret.size(), &count, (XrVector2f *)ret.ptrw()); |
| 986 | if (XR_FAILED(result)) { |
| 987 | ERR_FAIL_V_MSG(PackedVector2Array(), "OpenXR: Failed to get buffer [" + openxr_api->get_error_string(result) + "]"); |
| 988 | } |
| 989 | #endif |
| 990 | |
| 991 | return ret; |
| 992 | } |
| 993 | |
| 994 | PackedVector3Array OpenXRSpatialEntityExtension::get_vector3_buffer(RID p_spatial_snapshot, XrSpatialBufferIdEXT p_buffer_id) const { |
| 995 | PackedVector3Array ret; |
| 996 | |
| 997 | SpatialSnapshotData *snapshot_data = spatial_snapshot_owner.get_or_null(p_spatial_snapshot); |
| 998 | ERR_FAIL_NULL_V(snapshot_data, ret); |
| 999 | |
| 1000 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 1001 | ERR_FAIL_NULL_V(openxr_api, ret); |
| 1002 | |
| 1003 | XrSpatialBufferGetInfoEXT info = { |
| 1004 | XR_TYPE_SPATIAL_BUFFER_GET_INFO_EXT, // type |
| 1005 | nullptr, // next |
| 1006 | p_buffer_id, // bufferId |
| 1007 | }; |
| 1008 | |
| 1009 | uint32_t count = 0; |
| 1010 | XrResult result = xrGetSpatialBufferVector3fEXT(snapshot_data->spatial_snapshot, &info, 0, &count, nullptr); |
| 1011 | if (XR_FAILED(result)) { |
| 1012 | ERR_FAIL_V_MSG(ret, "OpenXR: Failed to get buffer size [" + openxr_api->get_error_string(result) + "]"); |
| 1013 | } |
| 1014 | |
| 1015 | #ifdef REAL_T_IS_DOUBLE |
| 1016 | // OpenXR XrVector3f is using floats, Godot Vector3 is using double, so we need to do a copy. |
| 1017 | LocalVector<XrVector3f> buffer; |
| 1018 | buffer.resize(count); |
| 1019 | |
| 1020 | result = xrGetSpatialBufferVector3fEXT(snapshot_data->spatial_snapshot, &info, buffer.size(), &count, buffer.ptr()); |
| 1021 | if (XR_FAILED(result)) { |
| 1022 | ERR_FAIL_V_MSG(ret, "OpenXR: Failed to get buffer [" + openxr_api->get_error_string(result) + "]"); |
| 1023 | } |
| 1024 | |
| 1025 | ret.resize(count); |
| 1026 | Vector3 *ptr = ret.ptrw(); |
| 1027 | for (uint32_t i = 0; i < count; i++) { |
| 1028 | ptr[i].x = buffer[i].x; |
| 1029 | ptr[i].y = buffer[i].y; |
| 1030 | ptr[i].z = buffer[i].z; |
| 1031 | } |
| 1032 | #else |
| 1033 | // OpenXR's XrVector3f and Godots Vector3 should be interchangeable. |
| 1034 | ret.resize(count); |
| 1035 | |
| 1036 | result = xrGetSpatialBufferVector3fEXT(snapshot_data->spatial_snapshot, &info, ret.size(), &count, (XrVector3f *)ret.ptrw()); |
| 1037 | if (XR_FAILED(result)) { |
| 1038 | ERR_FAIL_V_MSG(PackedVector3Array(), "OpenXR: Failed to get buffer [" + openxr_api->get_error_string(result) + "]"); |
| 1039 | } |
| 1040 | #endif |
| 1041 | |
| 1042 | return ret; |
| 1043 | } |
| 1044 | |
| 1045 | String OpenXRSpatialEntityExtension::_get_string(RID p_spatial_snapshot, uint64_t p_buffer_id) const { |
| 1046 | return get_string(p_spatial_snapshot, (XrSpatialBufferIdEXT)p_buffer_id); |
| 1047 | } |
| 1048 | |
| 1049 | PackedByteArray OpenXRSpatialEntityExtension::_get_uint8_buffer(RID p_spatial_snapshot, uint64_t p_buffer_id) const { |
| 1050 | return get_uint8_buffer(p_spatial_snapshot, (XrSpatialBufferIdEXT)p_buffer_id); |
| 1051 | } |
| 1052 | |
| 1053 | PackedInt32Array OpenXRSpatialEntityExtension::_get_uint16_buffer(RID p_spatial_snapshot, uint64_t p_buffer_id) const { |
| 1054 | PackedInt32Array ret; |
| 1055 | Vector<uint16_t> buffer = get_uint16_buffer(p_spatial_snapshot, (XrSpatialBufferIdEXT)p_buffer_id); |
| 1056 | |
| 1057 | if (!buffer.is_empty()) { |
| 1058 | // We don't have PackedInt16Array so we convert to PackedInt32Array |
| 1059 | |
| 1060 | ret.resize(buffer.size()); |
| 1061 | |
| 1062 | int size = ret.size(); |
| 1063 | int32_t *ptr = ret.ptrw(); |
| 1064 | for (int i = 0; i < size; i++) { |
| 1065 | ptr[i] = buffer[i]; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | return ret; |
| 1070 | } |
| 1071 | |
| 1072 | PackedInt32Array OpenXRSpatialEntityExtension::_get_uint32_buffer(RID p_spatial_snapshot, uint64_t p_buffer_id) const { |
| 1073 | PackedInt32Array ret; |
| 1074 | Vector<uint32_t> buffer = get_uint32_buffer(p_spatial_snapshot, (XrSpatialBufferIdEXT)p_buffer_id); |
| 1075 | |
| 1076 | if (!buffer.is_empty()) { |
| 1077 | // Note, we don't have a UINT32 array that we can use with GDScript and using an INT64 array is overkill. |
| 1078 | // Bit wasteful this but... |
| 1079 | |
| 1080 | ret.resize(buffer.size()); |
| 1081 | |
| 1082 | int size = ret.size(); |
| 1083 | int32_t *ptr = ret.ptrw(); |
| 1084 | for (int i = 0; i < size; i++) { |
| 1085 | ptr[i] = buffer[i]; |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | return ret; |
| 1090 | } |
| 1091 | |
| 1092 | PackedFloat32Array OpenXRSpatialEntityExtension::_get_float_buffer(RID p_spatial_snapshot, uint64_t p_buffer_id) const { |
| 1093 | return get_float_buffer(p_spatial_snapshot, (XrSpatialBufferIdEXT)p_buffer_id); |
| 1094 | } |
| 1095 | |
| 1096 | PackedVector2Array OpenXRSpatialEntityExtension::_get_vector2_buffer(RID p_spatial_snapshot, uint64_t p_buffer_id) const { |
| 1097 | return get_vector2_buffer(p_spatial_snapshot, (XrSpatialBufferIdEXT)p_buffer_id); |
| 1098 | } |
| 1099 | |
| 1100 | PackedVector3Array OpenXRSpatialEntityExtension::_get_vector3_buffer(RID p_spatial_snapshot, uint64_t p_buffer_id) const { |
| 1101 | return get_vector3_buffer(p_spatial_snapshot, (XrSpatialBufferIdEXT)p_buffer_id); |
| 1102 | } |
| 1103 | |
| 1104 | //////////////////////////////////////////////////////////////////////////// |
| 1105 | // Entities |
| 1106 | |
| 1107 | RID OpenXRSpatialEntityExtension::find_spatial_entity(XrSpatialEntityIdEXT p_entity_id) const { |
| 1108 | ERR_FAIL_COND_V(!get_active(), RID()); |
| 1109 | |
| 1110 | LocalVector<RID> entities = spatial_entity_owner.get_owned_list(); |
| 1111 | for (const RID &entity : entities) { |
| 1112 | SpatialEntityData *entity_data = spatial_entity_owner.get_or_null(entity); |
| 1113 | ERR_FAIL_NULL_V(entity_data, RID()); |
| 1114 | |
| 1115 | if (entity_data->entity_id == p_entity_id) { |
| 1116 | return entity; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | return RID(); |
| 1121 | } |
| 1122 | |
| 1123 | RID OpenXRSpatialEntityExtension::_find_entity(uint64_t p_entity_id) { |
| 1124 | return find_spatial_entity((XrSpatialEntityIdEXT)p_entity_id); |
| 1125 | } |
| 1126 | |
| 1127 | RID OpenXRSpatialEntityExtension::add_spatial_entity(RID p_spatial_context, XrSpatialEntityIdEXT p_entity_id, XrSpatialEntityEXT p_entity) { |
| 1128 | ERR_FAIL_COND_V(!get_active(), RID()); |
| 1129 | |
| 1130 | // Entity has been created elsewhere, we just register it |
| 1131 | SpatialEntityData spatial_entity_data; |
| 1132 | |
| 1133 | spatial_entity_data.spatial_context = p_spatial_context; |
| 1134 | spatial_entity_data.entity_id = p_entity_id; |
| 1135 | spatial_entity_data.entity = p_entity; |
| 1136 | |
| 1137 | return spatial_entity_owner.make_rid(spatial_entity_data); |
| 1138 | } |
| 1139 | |
| 1140 | RID OpenXRSpatialEntityExtension::_add_entity(RID p_spatial_context, uint64_t p_entity_id, uint64_t p_entity) { |
| 1141 | return add_spatial_entity(p_spatial_context, (XrSpatialEntityIdEXT)p_entity_id, (XrSpatialEntityEXT)p_entity); |
| 1142 | } |
| 1143 | |
| 1144 | RID OpenXRSpatialEntityExtension::make_spatial_entity(RID p_spatial_context, XrSpatialEntityIdEXT p_entity_id) { |
| 1145 | ERR_FAIL_COND_V(!get_active(), RID()); |
| 1146 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 1147 | ERR_FAIL_NULL_V(openxr_api, RID()); |
| 1148 | |
| 1149 | SpatialEntityData spatial_entity_data; |
| 1150 | |
| 1151 | spatial_entity_data.spatial_context = p_spatial_context; |
| 1152 | spatial_entity_data.entity_id = p_entity_id; |
| 1153 | XrSpatialEntityFromIdCreateInfoEXT create_info = { |
| 1154 | XR_TYPE_SPATIAL_ENTITY_FROM_ID_CREATE_INFO_EXT, // type |
| 1155 | nullptr, // next |
| 1156 | p_entity_id //entityId |
| 1157 | }; |
| 1158 | XrResult result = xrCreateSpatialEntityFromIdEXT(get_spatial_context_handle(p_spatial_context), &create_info, &spatial_entity_data.entity); |
| 1159 | if (XR_FAILED(result)) { |
| 1160 | ERR_FAIL_V_MSG(RID(), "OpenXR: Failed to create spatial entity [" + openxr_api->get_error_string(result) + "]"); |
| 1161 | } |
| 1162 | |
| 1163 | return spatial_entity_owner.make_rid(spatial_entity_data); |
| 1164 | } |
| 1165 | |
| 1166 | RID OpenXRSpatialEntityExtension::_make_entity(RID p_spatial_context, uint64_t p_entity_id) { |
| 1167 | return make_spatial_entity(p_spatial_context, (XrSpatialEntityIdEXT)p_entity_id); |
| 1168 | } |
| 1169 | |
| 1170 | XrSpatialEntityIdEXT OpenXRSpatialEntityExtension::get_spatial_entity_id(RID p_entity) const { |
| 1171 | SpatialEntityData *entity_data = spatial_entity_owner.get_or_null(p_entity); |
| 1172 | ERR_FAIL_NULL_V(entity_data, XR_NULL_ENTITY); |
| 1173 | |
| 1174 | return entity_data->entity_id; |
| 1175 | } |
| 1176 | |
| 1177 | uint64_t OpenXRSpatialEntityExtension::_get_entity_id(RID p_entity) const { |
| 1178 | return (uint64_t)get_spatial_entity_id(p_entity); |
| 1179 | } |
| 1180 | |
| 1181 | RID OpenXRSpatialEntityExtension::get_spatial_entity_context(RID p_entity) const { |
| 1182 | SpatialEntityData *entity_data = spatial_entity_owner.get_or_null(p_entity); |
| 1183 | ERR_FAIL_NULL_V(entity_data, RID()); |
| 1184 | |
| 1185 | return entity_data->spatial_context; |
| 1186 | } |
| 1187 | |
| 1188 | void OpenXRSpatialEntityExtension::free_spatial_entity(RID p_entity) { |
| 1189 | SpatialEntityData *entity_data = spatial_entity_owner.get_or_null(p_entity); |
| 1190 | ERR_FAIL_NULL(entity_data); |
| 1191 | ERR_FAIL_COND(entity_data->entity == XR_NULL_HANDLE); |
| 1192 | |
| 1193 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 1194 | ERR_FAIL_NULL(openxr_api); |
| 1195 | |
| 1196 | XrResult result = xrDestroySpatialEntityEXT_ptr(entity_data->entity); |
| 1197 | if (XR_FAILED(result)) { |
| 1198 | WARN_PRINT("OpenXR: Failed to destroy spatial entity [" + openxr_api->get_error_string(result) + "]"); |
| 1199 | } |
| 1200 | |
| 1201 | // And remove our RID. |
| 1202 | spatial_entity_owner.free(p_entity); |
| 1203 | } |
| 1204 | |
| 1205 | String OpenXRSpatialEntityExtension::get_spatial_capability_name(XrSpatialCapabilityEXT p_capability){ |
| 1206 | XR_ENUM_SWITCH(XrSpatialCapabilityEXT, p_capability) |
| 1207 | } |
| 1208 | |
| 1209 | String OpenXRSpatialEntityExtension::get_spatial_component_type_name(XrSpatialComponentTypeEXT p_component_type){ |
| 1210 | XR_ENUM_SWITCH(XrSpatialComponentTypeEXT, p_component_type) |
| 1211 | } |
| 1212 | |
| 1213 | String OpenXRSpatialEntityExtension::get_spatial_feature_name(XrSpatialCapabilityFeatureEXT p_feature) { |
| 1214 | XR_ENUM_SWITCH(XrSpatialCapabilityFeatureEXT, p_feature) |
| 1215 | } |