Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/openxr/openxr_interface.h
10277 views
1
/**************************************************************************/
2
/* openxr_interface.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
// A note on multithreading and thread safety in OpenXR.
34
//
35
// Most entry points will be called from the main thread in Godot
36
// however a number of entry points will be called from the
37
// rendering thread, potentially while we're already processing
38
// the next frame on the main thread.
39
//
40
// OpenXR itself has been designed with threading in mind including
41
// a high likelihood that the XR runtime runs in separate threads
42
// as well.
43
// Hence all the frame timing information, use of swapchains and
44
// sync functions.
45
// Do note that repeated calls to tracking APIs will provide
46
// increasingly more accurate data for the same timestamp as
47
// tracking data is continuously updated.
48
//
49
// For our code we mostly implement this in our OpenXRAPI class.
50
// We store data accessed from the rendering thread in a separate
51
// struct, setting values through our renderer command queue.
52
//
53
// As some data is setup before we start rendering, and cleaned up
54
// after we've stopped, that is accessed directly from both threads.
55
56
#include "action_map/openxr_action_map.h"
57
#include "extensions/openxr_hand_tracking_extension.h"
58
#include "openxr_api.h"
59
60
#include "servers/xr/xr_controller_tracker.h"
61
#include "servers/xr/xr_interface.h"
62
63
// declare some default strings
64
#define INTERACTION_PROFILE_NONE "/interaction_profiles/none"
65
66
class OpenXRInterface : public XRInterface {
67
GDCLASS(OpenXRInterface, XRInterface);
68
69
private:
70
OpenXRAPI *openxr_api = nullptr;
71
bool initialized = false;
72
bool reference_stage_changing = false;
73
XRInterface::TrackingStatus tracking_state;
74
75
// At a minimum we need a tracker for our head
76
Ref<XRPositionalTracker> head;
77
Transform3D head_transform;
78
Vector3 head_linear_velocity;
79
Vector3 head_angular_velocity;
80
XRPose::TrackingConfidence head_confidence;
81
Transform3D transform_for_view[2]; // We currently assume 2, but could be 4 for VARJO which we do not support yet
82
83
XRVRS xr_vrs;
84
85
void _load_action_map();
86
87
struct Action { // An action we've registered with OpenXR
88
String action_name; // Name of our action as presented to Godot (can be altered from the action map)
89
OpenXRAction::ActionType action_type; // The action type of this action
90
RID action_rid; // RID of the action registered with our OpenXR API
91
};
92
struct ActionSet { // An action set we've registered with OpenXR
93
String action_set_name; // Name of our action set
94
bool is_active; // If true this action set is active and we will sync it
95
Vector<Action *> actions; // List of actions in this action set
96
RID action_set_rid; // RID of the action registered with our OpenXR API
97
};
98
struct Tracker { // A tracker we've registered with OpenXR
99
String tracker_name; // Name of our tracker (can be altered from the action map)
100
Vector<Action *> actions; // Actions related to this tracker
101
Ref<XRControllerTracker> controller_tracker; // Our positional tracker object that holds our tracker state
102
RID tracker_rid; // RID of the tracker registered with our OpenXR API
103
RID interaction_profile; // RID of the interaction profile bound to this tracker (can be null)
104
};
105
106
Vector<ActionSet *> action_sets;
107
Vector<RID> interaction_profiles;
108
Vector<Tracker *> trackers;
109
110
ActionSet *create_action_set(const String &p_action_set_name, const String &p_localized_name, const int p_priority);
111
void free_action_sets();
112
113
Action *create_action(ActionSet *p_action_set, const String &p_action_name, const String &p_localized_name, OpenXRAction::ActionType p_action_type, const Vector<Tracker *> p_trackers);
114
Action *find_action(const String &p_action_name);
115
void free_actions(ActionSet *p_action_set);
116
117
Tracker *find_tracker(const String &p_tracker_name, bool p_create = false);
118
void handle_tracker(Tracker *p_tracker);
119
void free_trackers();
120
121
void free_interaction_profiles();
122
123
void _set_default_pos(Transform3D &p_transform, double p_world_scale, uint64_t p_eye);
124
125
void handle_hand_tracking(const String &p_path, OpenXRHandTrackingExtension::HandTrackedHands p_hand);
126
127
protected:
128
static void _bind_methods();
129
130
public:
131
virtual StringName get_name() const override;
132
virtual uint32_t get_capabilities() const override;
133
134
virtual PackedStringArray get_suggested_tracker_names() const override;
135
virtual TrackingStatus get_tracking_status() const override;
136
137
bool is_hand_tracking_supported();
138
bool is_hand_interaction_supported() const;
139
bool is_eye_gaze_interaction_supported();
140
141
bool initialize_on_startup() const;
142
virtual bool is_initialized() const override;
143
virtual bool initialize() override;
144
virtual void uninitialize() override;
145
virtual Dictionary get_system_info() override;
146
147
virtual void trigger_haptic_pulse(const String &p_action_name, const StringName &p_tracker_name, double p_frequency, double p_amplitude, double p_duration_sec, double p_delay_sec = 0) override;
148
149
virtual bool supports_play_area_mode(XRInterface::PlayAreaMode p_mode) override;
150
virtual XRInterface::PlayAreaMode get_play_area_mode() const override;
151
virtual bool set_play_area_mode(XRInterface::PlayAreaMode p_mode) override;
152
virtual PackedVector3Array get_play_area() const override;
153
154
float get_display_refresh_rate() const;
155
void set_display_refresh_rate(float p_refresh_rate);
156
Array get_available_display_refresh_rates() const;
157
158
bool is_action_set_active(const String &p_action_set) const;
159
void set_action_set_active(const String &p_action_set, bool p_active);
160
Array get_action_sets() const;
161
162
double get_render_target_size_multiplier() const;
163
void set_render_target_size_multiplier(double multiplier);
164
165
bool is_foveation_supported() const;
166
167
int get_foveation_level() const;
168
void set_foveation_level(int p_foveation_level);
169
170
bool get_foveation_dynamic() const;
171
void set_foveation_dynamic(bool p_foveation_dynamic);
172
173
float get_vrs_min_radius() const;
174
void set_vrs_min_radius(float p_vrs_min_radius);
175
176
float get_vrs_strength() const;
177
void set_vrs_strength(float p_vrs_strength);
178
179
virtual Size2 get_render_target_size() override;
180
virtual uint32_t get_view_count() override;
181
virtual Transform3D get_camera_transform() override;
182
virtual Transform3D get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) override;
183
virtual Projection get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) override;
184
185
virtual Rect2i get_render_region() override;
186
187
virtual RID get_color_texture() override;
188
virtual RID get_depth_texture() override;
189
virtual RID get_velocity_texture() override;
190
virtual RID get_velocity_depth_texture() override;
191
virtual Size2i get_velocity_target_size() override;
192
193
virtual void process() override;
194
virtual void pre_render() override;
195
bool pre_draw_viewport(RID p_render_target) override;
196
virtual Vector<BlitToScreen> post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) override;
197
virtual void end_frame() override;
198
199
virtual bool is_passthrough_supported() override;
200
virtual bool is_passthrough_enabled() override;
201
virtual bool start_passthrough() override;
202
virtual void stop_passthrough() override;
203
204
/** environment blend mode. */
205
virtual Array get_supported_environment_blend_modes() override;
206
virtual XRInterface::EnvironmentBlendMode get_environment_blend_mode() const override;
207
virtual bool set_environment_blend_mode(XRInterface::EnvironmentBlendMode mode) override;
208
209
void on_state_ready();
210
void on_state_visible();
211
void on_state_focused();
212
void on_state_synchronized();
213
void on_state_stopping();
214
void on_state_loss_pending();
215
void on_state_exiting();
216
void on_reference_space_change_pending();
217
void on_refresh_rate_changes(float p_new_rate);
218
void tracker_profile_changed(RID p_tracker, RID p_interaction_profile);
219
220
/** Session */
221
enum SessionState { // Should mirror XrSessionState
222
SESSION_STATE_UNKNOWN = 0,
223
SESSION_STATE_IDLE = 1,
224
SESSION_STATE_READY = 2,
225
SESSION_STATE_SYNCHRONIZED = 3,
226
SESSION_STATE_VISIBLE = 4,
227
SESSION_STATE_FOCUSED = 5,
228
SESSION_STATE_STOPPING = 6,
229
SESSION_STATE_LOSS_PENDING = 7,
230
SESSION_STATE_EXITING = 8,
231
};
232
233
SessionState get_session_state();
234
235
/** Hand tracking. */
236
enum Hand {
237
HAND_LEFT,
238
HAND_RIGHT,
239
HAND_MAX,
240
};
241
242
enum HandMotionRange {
243
HAND_MOTION_RANGE_UNOBSTRUCTED,
244
HAND_MOTION_RANGE_CONFORM_TO_CONTROLLER,
245
HAND_MOTION_RANGE_MAX
246
};
247
248
void set_motion_range(const Hand p_hand, const HandMotionRange p_motion_range);
249
HandMotionRange get_motion_range(const Hand p_hand) const;
250
251
enum HandTrackedSource {
252
HAND_TRACKED_SOURCE_UNKNOWN,
253
HAND_TRACKED_SOURCE_UNOBSTRUCTED,
254
HAND_TRACKED_SOURCE_CONTROLLER,
255
HAND_TRACKED_SOURCE_MAX
256
};
257
258
HandTrackedSource get_hand_tracking_source(const Hand p_hand) const;
259
260
enum HandJoints {
261
HAND_JOINT_PALM = 0,
262
HAND_JOINT_WRIST = 1,
263
HAND_JOINT_THUMB_METACARPAL = 2,
264
HAND_JOINT_THUMB_PROXIMAL = 3,
265
HAND_JOINT_THUMB_DISTAL = 4,
266
HAND_JOINT_THUMB_TIP = 5,
267
HAND_JOINT_INDEX_METACARPAL = 6,
268
HAND_JOINT_INDEX_PROXIMAL = 7,
269
HAND_JOINT_INDEX_INTERMEDIATE = 8,
270
HAND_JOINT_INDEX_DISTAL = 9,
271
HAND_JOINT_INDEX_TIP = 10,
272
HAND_JOINT_MIDDLE_METACARPAL = 11,
273
HAND_JOINT_MIDDLE_PROXIMAL = 12,
274
HAND_JOINT_MIDDLE_INTERMEDIATE = 13,
275
HAND_JOINT_MIDDLE_DISTAL = 14,
276
HAND_JOINT_MIDDLE_TIP = 15,
277
HAND_JOINT_RING_METACARPAL = 16,
278
HAND_JOINT_RING_PROXIMAL = 17,
279
HAND_JOINT_RING_INTERMEDIATE = 18,
280
HAND_JOINT_RING_DISTAL = 19,
281
HAND_JOINT_RING_TIP = 20,
282
HAND_JOINT_LITTLE_METACARPAL = 21,
283
HAND_JOINT_LITTLE_PROXIMAL = 22,
284
HAND_JOINT_LITTLE_INTERMEDIATE = 23,
285
HAND_JOINT_LITTLE_DISTAL = 24,
286
HAND_JOINT_LITTLE_TIP = 25,
287
HAND_JOINT_MAX = 26,
288
};
289
290
enum HandJointFlags {
291
HAND_JOINT_NONE = 0,
292
HAND_JOINT_ORIENTATION_VALID = 1,
293
HAND_JOINT_ORIENTATION_TRACKED = 2,
294
HAND_JOINT_POSITION_VALID = 4,
295
HAND_JOINT_POSITION_TRACKED = 8,
296
HAND_JOINT_LINEAR_VELOCITY_VALID = 16,
297
HAND_JOINT_ANGULAR_VELOCITY_VALID = 32,
298
};
299
300
BitField<HandJointFlags> get_hand_joint_flags(Hand p_hand, HandJoints p_joint) const;
301
Quaternion get_hand_joint_rotation(Hand p_hand, HandJoints p_joint) const;
302
Vector3 get_hand_joint_position(Hand p_hand, HandJoints p_joint) const;
303
float get_hand_joint_radius(Hand p_hand, HandJoints p_joint) const;
304
305
Vector3 get_hand_joint_linear_velocity(Hand p_hand, HandJoints p_joint) const;
306
Vector3 get_hand_joint_angular_velocity(Hand p_hand, HandJoints p_joint) const;
307
308
virtual RID get_vrs_texture() override;
309
virtual VRSTextureFormat get_vrs_texture_format() override;
310
311
// Performance settings.
312
enum PerfSettingsLevel {
313
PERF_SETTINGS_LEVEL_POWER_SAVINGS,
314
PERF_SETTINGS_LEVEL_SUSTAINED_LOW,
315
PERF_SETTINGS_LEVEL_SUSTAINED_HIGH,
316
PERF_SETTINGS_LEVEL_BOOST,
317
};
318
319
enum PerfSettingsSubDomain {
320
PERF_SETTINGS_SUB_DOMAIN_COMPOSITING,
321
PERF_SETTINGS_SUB_DOMAIN_RENDERING,
322
PERF_SETTINGS_SUB_DOMAIN_THERMAL,
323
};
324
325
enum PerfSettingsNotificationLevel {
326
PERF_SETTINGS_NOTIF_LEVEL_NORMAL,
327
PERF_SETTINGS_NOTIF_LEVEL_WARNING,
328
PERF_SETTINGS_NOTIF_LEVEL_IMPAIRED,
329
};
330
331
void set_cpu_level(PerfSettingsLevel p_level);
332
void set_gpu_level(PerfSettingsLevel p_level);
333
void on_cpu_level_changed(PerfSettingsSubDomain p_sub_domain, PerfSettingsNotificationLevel p_from_level, PerfSettingsNotificationLevel p_to_level);
334
void on_gpu_level_changed(PerfSettingsSubDomain p_sub_domain, PerfSettingsNotificationLevel p_from_level, PerfSettingsNotificationLevel p_to_level);
335
336
OpenXRInterface();
337
~OpenXRInterface();
338
};
339
340
VARIANT_ENUM_CAST(OpenXRInterface::SessionState)
341
VARIANT_ENUM_CAST(OpenXRInterface::Hand)
342
VARIANT_ENUM_CAST(OpenXRInterface::HandMotionRange)
343
VARIANT_ENUM_CAST(OpenXRInterface::HandTrackedSource)
344
VARIANT_ENUM_CAST(OpenXRInterface::HandJoints)
345
VARIANT_ENUM_CAST(OpenXRInterface::PerfSettingsLevel)
346
VARIANT_ENUM_CAST(OpenXRInterface::PerfSettingsSubDomain)
347
VARIANT_ENUM_CAST(OpenXRInterface::PerfSettingsNotificationLevel)
348
VARIANT_BITFIELD_CAST(OpenXRInterface::HandJointFlags)
349
350