openxr_spatial_anchor.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**************************************************************************/
  2. /* openxr_spatial_anchor.h */
  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. #pragma once
  31. #include "../../openxr_util.h"
  32. #include "openxr_spatial_entities.h"
  33. #include "openxr_spatial_entity_extension.h"
  34. // Anchor capability configuration
  35. class OpenXRSpatialCapabilityConfigurationAnchor : public OpenXRSpatialCapabilityConfigurationBaseHeader {
  36. GDCLASS(OpenXRSpatialCapabilityConfigurationAnchor, OpenXRSpatialCapabilityConfigurationBaseHeader);
  37. public:
  38. virtual bool has_valid_configuration() const override;
  39. virtual XrSpatialCapabilityConfigurationBaseHeaderEXT *get_configuration() override;
  40. Vector<XrSpatialComponentTypeEXT> get_enabled_components() const { return anchor_enabled_components; }
  41. protected:
  42. static void _bind_methods();
  43. private:
  44. Vector<XrSpatialComponentTypeEXT> anchor_enabled_components;
  45. XrSpatialCapabilityConfigurationAnchorEXT anchor_config = { XR_TYPE_SPATIAL_CAPABILITY_CONFIGURATION_ANCHOR_EXT, nullptr, XR_SPATIAL_CAPABILITY_ANCHOR_EXT, 0, nullptr };
  46. PackedInt64Array _get_enabled_components() const;
  47. };
  48. // Anchor component anchor list
  49. class OpenXRSpatialComponentAnchorList : public OpenXRSpatialComponentData {
  50. GDCLASS(OpenXRSpatialComponentAnchorList, OpenXRSpatialComponentData);
  51. protected:
  52. static void _bind_methods();
  53. public:
  54. virtual void set_capacity(uint32_t p_capacity) override;
  55. virtual XrSpatialComponentTypeEXT get_component_type() const override;
  56. virtual void *get_structure_data(void *p_next) override;
  57. Transform3D get_entity_pose(int64_t p_index) const;
  58. private:
  59. Vector<XrPosef> entity_poses;
  60. XrSpatialComponentAnchorListEXT anchor_list = { XR_TYPE_SPATIAL_COMPONENT_ANCHOR_LIST_EXT, nullptr, 0, nullptr };
  61. };
  62. // Persistence configuration
  63. class OpenXRSpatialContextPersistenceConfig : public OpenXRStructureBase {
  64. GDCLASS(OpenXRSpatialContextPersistenceConfig, OpenXRStructureBase);
  65. public:
  66. bool has_valid_configuration() const;
  67. virtual void *get_header(void *p_next) override;
  68. virtual XrStructureType get_structure_type() override;
  69. void add_persistence_context(RID p_persistence_context);
  70. void remove_persistence_context(RID p_persistence_context);
  71. protected:
  72. static void _bind_methods();
  73. private:
  74. Vector<RID> persistence_contexts;
  75. Vector<XrSpatialPersistenceContextEXT> context_handles;
  76. XrSpatialContextPersistenceConfigEXT persistence_config = { XR_TYPE_SPATIAL_CONTEXT_PERSISTENCE_CONFIG_EXT, nullptr, 0, nullptr };
  77. };
  78. // Component persistence list
  79. class OpenXRSpatialComponentPersistenceList : public OpenXRSpatialComponentData {
  80. GDCLASS(OpenXRSpatialComponentPersistenceList, OpenXRSpatialComponentData);
  81. protected:
  82. static void _bind_methods();
  83. public:
  84. virtual void set_capacity(uint32_t p_capacity) override;
  85. virtual XrSpatialComponentTypeEXT get_component_type() const override;
  86. virtual void *get_structure_data(void *p_next) override;
  87. XrUuid get_persistent_uuid(int64_t p_index) const;
  88. XrSpatialPersistenceStateEXT get_persistent_state(int64_t p_index) const;
  89. static String get_persistence_state_name(XrSpatialPersistenceStateEXT p_state);
  90. private:
  91. Vector<XrSpatialPersistenceDataEXT> persist_data;
  92. XrSpatialComponentPersistenceListEXT persistence_list = { XR_TYPE_SPATIAL_COMPONENT_PERSISTENCE_LIST_EXT, nullptr, 0, nullptr };
  93. String _get_persistent_uuid(int64_t p_index) const;
  94. uint64_t _get_persistent_state(int64_t p_index) const;
  95. };
  96. // Anchor tracker, this adds no new logic, it's purely for typing!
  97. class OpenXRAnchorTracker : public OpenXRSpatialEntityTracker {
  98. GDCLASS(OpenXRAnchorTracker, OpenXRSpatialEntityTracker);
  99. protected:
  100. static void _bind_methods();
  101. public:
  102. bool has_uuid() const;
  103. XrUuid get_uuid() const;
  104. void set_uuid(const XrUuid &p_uuid);
  105. private:
  106. XrUuid uuid = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  107. String _get_uuid() const;
  108. void _set_uuid(const String &p_uuid);
  109. bool uuid_is_equal(const XrUuid &p_a, const XrUuid &p_b);
  110. };
  111. // (Persistent) anchor logic
  112. class OpenXRSpatialAnchorCapability : public OpenXRExtensionWrapper {
  113. GDCLASS(OpenXRSpatialAnchorCapability, OpenXRExtensionWrapper);
  114. public:
  115. enum PersistenceScope {
  116. PERSISTENCE_SCOPE_SYSTEM_MANAGED = XR_SPATIAL_PERSISTENCE_SCOPE_SYSTEM_MANAGED_EXT,
  117. PERSISTENCE_SCOPE_LOCAL_ANCHORS = XR_SPATIAL_PERSISTENCE_SCOPE_LOCAL_ANCHORS_EXT,
  118. };
  119. static OpenXRSpatialAnchorCapability *get_singleton();
  120. OpenXRSpatialAnchorCapability();
  121. virtual ~OpenXRSpatialAnchorCapability() override;
  122. virtual HashMap<String, bool *> get_requested_extensions(XrVersion p_version) override;
  123. virtual void on_instance_created(const XrInstance p_instance) override;
  124. virtual void on_instance_destroyed() override;
  125. virtual void on_session_created(const XrSession p_session) override;
  126. virtual void on_session_destroyed() override;
  127. virtual void on_process() override;
  128. bool is_spatial_anchor_supported();
  129. bool is_spatial_persistence_supported();
  130. // Persistence scopes
  131. bool is_persistence_scope_supported(XrSpatialPersistenceScopeEXT p_scope);
  132. Ref<OpenXRFutureResult> create_persistence_context(XrSpatialPersistenceScopeEXT p_scope, const Callable &p_user_callback = Callable());
  133. XrSpatialPersistenceContextEXT get_persistence_context_handle(RID p_persistence_context) const;
  134. void free_persistence_context(RID p_persistence_context);
  135. Ref<OpenXRAnchorTracker> create_new_anchor(const Transform3D &p_transform, RID p_spatial_context = RID());
  136. void remove_anchor(Ref<OpenXRAnchorTracker> p_anchor_tracker);
  137. Ref<OpenXRFutureResult> persist_anchor(Ref<OpenXRAnchorTracker> p_anchor_tracker, RID p_persistence_context = RID(), const Callable &p_user_callback = Callable());
  138. Ref<OpenXRFutureResult> unpersist_anchor(Ref<OpenXRAnchorTracker> p_anchor_tracker, RID p_persistence_context = RID(), const Callable &p_user_callback = Callable());
  139. static String get_spatial_persistence_scope_name(XrSpatialPersistenceScopeEXT p_scope);
  140. static String get_spatial_persistence_context_result_name(XrSpatialPersistenceContextResultEXT p_result);
  141. protected:
  142. static void _bind_methods();
  143. private:
  144. static OpenXRSpatialAnchorCapability *singleton;
  145. bool spatial_anchor_ext = false;
  146. bool spatial_persistence_ext = false;
  147. bool spatial_persistence_operations_ext = false;
  148. bool spatial_anchor_supported = false;
  149. RID spatial_context;
  150. RID persistence_context;
  151. bool need_discovery = false;
  152. int discovery_cooldown = 0;
  153. Ref<OpenXRFutureResult> discovery_query_result;
  154. Ref<OpenXRSpatialCapabilityConfigurationAnchor> anchor_configuration;
  155. Ref<OpenXRSpatialContextPersistenceConfig> persistence_configuration;
  156. Vector<XrSpatialPersistenceScopeEXT> supported_persistence_scopes;
  157. bool _load_supported_persistence_scopes();
  158. // Persistence scopes
  159. struct PersistenceContextData {
  160. XrSpatialPersistenceScopeEXT scope;
  161. XrSpatialPersistenceContextEXT persistence_context = XR_NULL_HANDLE;
  162. };
  163. mutable RID_Owner<PersistenceContextData> persistence_context_owner;
  164. bool _is_persistence_scope_supported(PersistenceScope p_scope);
  165. Ref<OpenXRFutureResult> _create_persistence_context(PersistenceScope p_scope, Callable p_user_callback = Callable());
  166. uint64_t _get_persistence_context_handle(RID p_persistence_context) const;
  167. void _on_persistence_context_ready(Ref<OpenXRFutureResult> p_future_result, uint64_t p_scope, Callable p_user_callback = Callable());
  168. // Discovery logic
  169. void _on_persistence_context_completed(RID p_persistence_context);
  170. Ref<OpenXRFutureResult> _create_spatial_context();
  171. void _on_spatial_context_created(RID p_spatial_context);
  172. void _on_spatial_discovery_recommended(RID p_spatial_context);
  173. Ref<OpenXRFutureResult> _start_entity_discovery();
  174. void _process_discovery_snapshot(RID p_snapshot);
  175. void _process_update_snapshot(RID p_snapshot);
  176. // Entities
  177. void _on_made_anchor_persistent(Ref<OpenXRFutureResult> p_future_result, RID p_persistence_context, Ref<OpenXRAnchorTracker> p_anchor_tracker, const Callable &p_callback);
  178. void _on_made_anchor_unpersistent(Ref<OpenXRFutureResult> p_future_result, RID p_persistence_context, Ref<OpenXRAnchorTracker> p_anchor_tracker, const Callable &p_callback);
  179. // Trackers
  180. HashMap<XrSpatialEntityIdEXT, Ref<OpenXRAnchorTracker>> anchors;
  181. // OpenXR API call wrappers
  182. EXT_PROTO_XRRESULT_FUNC4(xrCreateSpatialAnchorEXT, (XrSpatialContextEXT), spatialContext, (const XrSpatialAnchorCreateInfoEXT *), create_info, (XrSpatialEntityIdEXT *), anchor_entity_id, (XrSpatialEntityEXT *), anchor_entity);
  183. EXT_PROTO_XRRESULT_FUNC5(xrEnumerateSpatialPersistenceScopesEXT, (XrInstance), instance, (XrSystemId), system_id, (uint32_t), persistence_scope_capacity_input, (uint32_t *), persistence_scope_count_output, (XrSpatialPersistenceScopeEXT *), persistence_scopes);
  184. EXT_PROTO_XRRESULT_FUNC3(xrCreateSpatialPersistenceContextAsyncEXT, (XrSession), session, (const XrSpatialPersistenceContextCreateInfoEXT *), create_info, (XrFutureEXT *), future);
  185. EXT_PROTO_XRRESULT_FUNC3(xrCreateSpatialPersistenceContextCompleteEXT, (XrSession), session, (XrFutureEXT), future, (XrCreateSpatialPersistenceContextCompletionEXT *), completion);
  186. EXT_PROTO_XRRESULT_FUNC1(xrDestroySpatialPersistenceContextEXT, (XrSpatialPersistenceContextEXT), persistence_context);
  187. EXT_PROTO_XRRESULT_FUNC3(xrPersistSpatialEntityAsyncEXT, (XrSpatialPersistenceContextEXT), persistence_context, (const XrSpatialEntityPersistInfoEXT *), persist_info, (XrFutureEXT *), future);
  188. EXT_PROTO_XRRESULT_FUNC3(xrPersistSpatialEntityCompleteEXT, (XrSpatialPersistenceContextEXT), persistence_context, (XrFutureEXT), future, (XrPersistSpatialEntityCompletionEXT *), completion);
  189. EXT_PROTO_XRRESULT_FUNC3(xrUnpersistSpatialEntityAsyncEXT, (XrSpatialPersistenceContextEXT), persistence_context, (const XrSpatialEntityUnpersistInfoEXT *), unpersist_info, (XrFutureEXT *), future);
  190. EXT_PROTO_XRRESULT_FUNC3(xrUnpersistSpatialEntityCompleteEXT, (XrSpatialPersistenceContextEXT), persistence_context, (XrFutureEXT), future, (XrUnpersistSpatialEntityCompletionEXT *), completion);
  191. };
  192. VARIANT_ENUM_CAST(OpenXRSpatialAnchorCapability::PersistenceScope);