openxr_spatial_entity_extension.cpp 49 KB

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