Debian Codegraph @

modules/openxr/extensions/spatial_entities/openxr_spatial_anchor.h

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