Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/openxr/openxr_api.h
21344 views
1
/**************************************************************************/
2
/* openxr_api.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 "action_map/openxr_action.h"
34
#include "extensions/openxr_extension_wrapper.h"
35
#include "util.h"
36
37
#include "core/error/error_macros.h"
38
#include "core/math/projection.h"
39
#include "core/math/transform_3d.h"
40
#include "core/math/vector2.h"
41
#include "core/string/ustring.h"
42
#include "core/templates/rid_owner.h"
43
#include "core/templates/vector.h"
44
#include "servers/rendering/rendering_server.h"
45
#include "servers/xr/xr_pose.h"
46
47
#include <openxr/openxr.h>
48
49
// Name we add to our extensions if OpenXR 1.1 context is available.
50
#define XR_OPENXR_1_1_NAME "OPENXR_1_1"
51
52
#define XR_API_VERSION_1_1_0 XR_MAKE_VERSION(1, 1, 0)
53
54
// forward declarations, we don't want to include these fully
55
class OpenXRInterface;
56
57
class OpenXRAPI {
58
public:
59
class OpenXRSwapChainInfo {
60
private:
61
XrSwapchain swapchain = XR_NULL_HANDLE;
62
void *swapchain_graphics_data = nullptr;
63
uint32_t image_index = 0;
64
bool image_acquired = false;
65
bool skip_acquire_swapchain = false;
66
67
static Vector<OpenXRSwapChainInfo> free_queue;
68
69
public:
70
_FORCE_INLINE_ XrSwapchain get_swapchain() const { return swapchain; }
71
_FORCE_INLINE_ bool is_image_acquired() const { return image_acquired; }
72
73
bool create(XrSwapchainCreateFlags p_create_flags, XrSwapchainUsageFlags p_usage_flags, int64_t p_swapchain_format, uint32_t p_width, uint32_t p_height, uint32_t p_sample_count, uint32_t p_array_size);
74
void queue_free();
75
static void free_queued();
76
void free();
77
78
bool acquire(bool &p_should_render);
79
bool release();
80
RID get_image();
81
RID get_density_map();
82
};
83
84
private:
85
// our singleton
86
static OpenXRAPI *singleton;
87
88
// Registered extension wrappers
89
static Vector<OpenXRExtensionWrapper *> registered_extension_wrappers;
90
91
// linked XR interface
92
OpenXRInterface *xr_interface = nullptr;
93
94
// layers
95
LocalVector<XrApiLayerProperties> layer_properties;
96
97
// extensions
98
LocalVector<XrExtensionProperties> supported_extensions;
99
LocalVector<CharString> enabled_extensions;
100
101
// composition layer providers
102
Vector<OpenXRExtensionWrapper *> composition_layer_providers;
103
104
// projection views extensions
105
Vector<OpenXRExtensionWrapper *> projection_views_extensions;
106
107
// frame info extensions
108
Vector<OpenXRExtensionWrapper *> frame_info_extensions;
109
110
// view configuration
111
LocalVector<XrViewConfigurationType> supported_view_configuration_types;
112
113
// reference spaces
114
LocalVector<XrReferenceSpaceType> supported_reference_spaces;
115
116
// swapchains (note these are platform dependent)
117
PackedInt64Array supported_swapchain_formats;
118
119
// system info
120
XrVersion openxr_version;
121
String runtime_name;
122
String runtime_version;
123
124
// configuration
125
XrFormFactor form_factor = XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY;
126
XrViewConfigurationType view_configuration = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO;
127
XrReferenceSpaceType requested_reference_space = XR_REFERENCE_SPACE_TYPE_STAGE;
128
XrReferenceSpaceType reference_space = XR_REFERENCE_SPACE_TYPE_LOCAL;
129
bool submit_depth_buffer = false; // if set to true we submit depth buffers to OpenXR if a suitable extension is enabled.
130
131
// blend mode
132
XrEnvironmentBlendMode environment_blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE;
133
XrEnvironmentBlendMode requested_environment_blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE;
134
Vector<XrEnvironmentBlendMode> supported_environment_blend_modes;
135
bool emulate_environment_blend_mode_alpha_blend = false;
136
137
// state
138
XrInstance instance = XR_NULL_HANDLE;
139
XrSystemId system_id = 0;
140
String system_name;
141
uint32_t vendor_id = 0;
142
XrSystemTrackingProperties tracking_properties;
143
XrSession session = XR_NULL_HANDLE;
144
XrSessionState session_state = XR_SESSION_STATE_UNKNOWN;
145
bool running = false;
146
XrFrameState frame_state = { XR_TYPE_FRAME_STATE, nullptr, 0, 0, false };
147
double render_target_size_multiplier = 1.0;
148
Rect2i render_region;
149
150
OpenXRGraphicsExtensionWrapper *graphics_extension = nullptr;
151
XrSystemGraphicsProperties graphics_properties;
152
153
LocalVector<XrViewConfigurationView> view_configuration_views;
154
155
enum OpenXRSwapChainTypes {
156
OPENXR_SWAPCHAIN_COLOR,
157
OPENXR_SWAPCHAIN_DEPTH,
158
OPENXR_SWAPCHAIN_MAX
159
};
160
161
int64_t color_swapchain_format = 0;
162
int64_t depth_swapchain_format = 0;
163
164
bool play_space_is_dirty = true;
165
XrSpace play_space = XR_NULL_HANDLE;
166
XrSpace custom_play_space = XR_NULL_HANDLE;
167
XrSpace view_space = XR_NULL_HANDLE;
168
XRPose::TrackingConfidence head_pose_confidence = XRPose::XR_TRACKING_CONFIDENCE_NONE;
169
170
RID velocity_texture;
171
RID velocity_depth_texture;
172
Size2i velocity_target_size;
173
174
// When LOCAL_FLOOR isn't supported, we use an approach based on the example code in the
175
// OpenXR spec in order to emulate it.
176
// See: https://registry.khronos.org/OpenXR/specs/1.0/html/xrspec.html#XR_EXT_local_floor
177
struct LocalFloorEmulation {
178
bool enabled = false;
179
XrSpace local_space = XR_NULL_HANDLE;
180
XrSpace stage_space = XR_NULL_HANDLE;
181
bool should_reset_floor_height = false;
182
} local_floor_emulation;
183
184
bool reset_emulated_floor_height();
185
186
bool load_layer_properties();
187
bool load_supported_extensions();
188
bool is_extension_supported(const String &p_extension) const;
189
bool is_any_extension_enabled(const String &p_extensions) const;
190
191
struct RequestExtension {
192
String name;
193
bool *enabled;
194
};
195
XrResult attempt_create_instance(XrVersion p_version);
196
197
bool openxr_loader_init();
198
bool resolve_instance_openxr_symbols();
199
200
#ifdef ANDROID_ENABLED
201
// On Android we keep tracker of our external OpenXR loader
202
void *openxr_loader_library_handle = nullptr;
203
#endif
204
205
// function pointers
206
#ifdef ANDROID_ENABLED
207
// On non-Android platforms we use the OpenXR symbol linked into the engine binary.
208
PFN_xrGetInstanceProcAddr xrGetInstanceProcAddr = nullptr;
209
#endif
210
EXT_PROTO_XRRESULT_FUNC3(xrAcquireSwapchainImage, (XrSwapchain), swapchain, (const XrSwapchainImageAcquireInfo *), acquireInfo, (uint32_t *), index)
211
EXT_PROTO_XRRESULT_FUNC3(xrApplyHapticFeedback, (XrSession), session, (const XrHapticActionInfo *), hapticActionInfo, (const XrHapticBaseHeader *), hapticFeedback)
212
EXT_PROTO_XRRESULT_FUNC2(xrAttachSessionActionSets, (XrSession), session, (const XrSessionActionSetsAttachInfo *), attachInfo)
213
EXT_PROTO_XRRESULT_FUNC2(xrBeginFrame, (XrSession), session, (const XrFrameBeginInfo *), frameBeginInfo)
214
EXT_PROTO_XRRESULT_FUNC2(xrBeginSession, (XrSession), session, (const XrSessionBeginInfo *), beginInfo)
215
EXT_PROTO_XRRESULT_FUNC3(xrCreateAction, (XrActionSet), actionSet, (const XrActionCreateInfo *), createInfo, (XrAction *), action)
216
EXT_PROTO_XRRESULT_FUNC3(xrCreateActionSet, (XrInstance), instance, (const XrActionSetCreateInfo *), createInfo, (XrActionSet *), actionSet)
217
EXT_PROTO_XRRESULT_FUNC3(xrCreateActionSpace, (XrSession), session, (const XrActionSpaceCreateInfo *), createInfo, (XrSpace *), space)
218
EXT_PROTO_XRRESULT_FUNC2(xrCreateInstance, (const XrInstanceCreateInfo *), createInfo, (XrInstance *), instance)
219
EXT_PROTO_XRRESULT_FUNC3(xrCreateReferenceSpace, (XrSession), session, (const XrReferenceSpaceCreateInfo *), createInfo, (XrSpace *), space)
220
EXT_PROTO_XRRESULT_FUNC3(xrCreateSession, (XrInstance), instance, (const XrSessionCreateInfo *), createInfo, (XrSession *), session)
221
EXT_PROTO_XRRESULT_FUNC3(xrCreateSwapchain, (XrSession), session, (const XrSwapchainCreateInfo *), createInfo, (XrSwapchain *), swapchain)
222
EXT_PROTO_XRRESULT_FUNC1(xrDestroyAction, (XrAction), action)
223
EXT_PROTO_XRRESULT_FUNC1(xrDestroyActionSet, (XrActionSet), actionSet)
224
EXT_PROTO_XRRESULT_FUNC1(xrDestroyInstance, (XrInstance), instance)
225
EXT_PROTO_XRRESULT_FUNC1(xrDestroySession, (XrSession), session)
226
EXT_PROTO_XRRESULT_FUNC1(xrDestroySpace, (XrSpace), space)
227
EXT_PROTO_XRRESULT_FUNC1(xrDestroySwapchain, (XrSwapchain), swapchain)
228
EXT_PROTO_XRRESULT_FUNC2(xrEndFrame, (XrSession), session, (const XrFrameEndInfo *), frameEndInfo)
229
EXT_PROTO_XRRESULT_FUNC1(xrEndSession, (XrSession), session)
230
EXT_PROTO_XRRESULT_FUNC3(xrEnumerateApiLayerProperties, (uint32_t), propertyCapacityInput, (uint32_t *), propertyCountOutput, (XrApiLayerProperties *), properties)
231
EXT_PROTO_XRRESULT_FUNC6(xrEnumerateEnvironmentBlendModes, (XrInstance), instance, (XrSystemId), systemId, (XrViewConfigurationType), viewConfigurationType, (uint32_t), environmentBlendModeCapacityInput, (uint32_t *), environmentBlendModeCountOutput, (XrEnvironmentBlendMode *), environmentBlendModes)
232
EXT_PROTO_XRRESULT_FUNC4(xrEnumerateInstanceExtensionProperties, (const char *), layerName, (uint32_t), propertyCapacityInput, (uint32_t *), propertyCountOutput, (XrExtensionProperties *), properties)
233
EXT_PROTO_XRRESULT_FUNC4(xrEnumerateReferenceSpaces, (XrSession), session, (uint32_t), spaceCapacityInput, (uint32_t *), spaceCountOutput, (XrReferenceSpaceType *), spaces)
234
EXT_PROTO_XRRESULT_FUNC4(xrEnumerateSwapchainFormats, (XrSession), session, (uint32_t), formatCapacityInput, (uint32_t *), formatCountOutput, (int64_t *), formats)
235
EXT_PROTO_XRRESULT_FUNC5(xrEnumerateViewConfigurations, (XrInstance), instance, (XrSystemId), systemId, (uint32_t), viewConfigurationTypeCapacityInput, (uint32_t *), viewConfigurationTypeCountOutput, (XrViewConfigurationType *), viewConfigurationTypes)
236
EXT_PROTO_XRRESULT_FUNC6(xrEnumerateViewConfigurationViews, (XrInstance), instance, (XrSystemId), systemId, (XrViewConfigurationType), viewConfigurationType, (uint32_t), viewCapacityInput, (uint32_t *), viewCountOutput, (XrViewConfigurationView *), views)
237
EXT_PROTO_XRRESULT_FUNC3(xrGetActionStateBoolean, (XrSession), session, (const XrActionStateGetInfo *), getInfo, (XrActionStateBoolean *), state)
238
EXT_PROTO_XRRESULT_FUNC3(xrGetActionStateFloat, (XrSession), session, (const XrActionStateGetInfo *), getInfo, (XrActionStateFloat *), state)
239
EXT_PROTO_XRRESULT_FUNC3(xrGetActionStateVector2f, (XrSession), session, (const XrActionStateGetInfo *), getInfo, (XrActionStateVector2f *), state)
240
EXT_PROTO_XRRESULT_FUNC3(xrGetCurrentInteractionProfile, (XrSession), session, (XrPath), topLevelUserPath, (XrInteractionProfileState *), interactionProfile)
241
EXT_PROTO_XRRESULT_FUNC2(xrGetInstanceProperties, (XrInstance), instance, (XrInstanceProperties *), instanceProperties)
242
EXT_PROTO_XRRESULT_FUNC3(xrGetReferenceSpaceBoundsRect, (XrSession), session, (XrReferenceSpaceType), referenceSpaceType, (XrExtent2Df *), bounds)
243
EXT_PROTO_XRRESULT_FUNC3(xrGetSystem, (XrInstance), instance, (const XrSystemGetInfo *), getInfo, (XrSystemId *), systemId)
244
EXT_PROTO_XRRESULT_FUNC3(xrGetSystemProperties, (XrInstance), instance, (XrSystemId), systemId, (XrSystemProperties *), properties)
245
EXT_PROTO_XRRESULT_FUNC4(xrLocateSpace, (XrSpace), space, (XrSpace), baseSpace, (XrTime), time, (XrSpaceLocation *), location)
246
EXT_PROTO_XRRESULT_FUNC6(xrLocateViews, (XrSession), session, (const XrViewLocateInfo *), viewLocateInfo, (XrViewState *), viewState, (uint32_t), viewCapacityInput, (uint32_t *), viewCountOutput, (XrView *), views)
247
EXT_PROTO_XRRESULT_FUNC5(xrPathToString, (XrInstance), instance, (XrPath), path, (uint32_t), bufferCapacityInput, (uint32_t *), bufferCountOutput, (char *), buffer)
248
EXT_PROTO_XRRESULT_FUNC2(xrPollEvent, (XrInstance), instance, (XrEventDataBuffer *), eventData)
249
EXT_PROTO_XRRESULT_FUNC2(xrReleaseSwapchainImage, (XrSwapchain), swapchain, (const XrSwapchainImageReleaseInfo *), releaseInfo)
250
EXT_PROTO_XRRESULT_FUNC3(xrResultToString, (XrInstance), instance, (XrResult), value, (char *), buffer)
251
EXT_PROTO_XRRESULT_FUNC3(xrStringToPath, (XrInstance), instance, (const char *), pathString, (XrPath *), path)
252
EXT_PROTO_XRRESULT_FUNC2(xrSuggestInteractionProfileBindings, (XrInstance), instance, (const XrInteractionProfileSuggestedBinding *), suggestedBindings)
253
EXT_PROTO_XRRESULT_FUNC2(xrSyncActions, (XrSession), session, (const XrActionsSyncInfo *), syncInfo)
254
EXT_PROTO_XRRESULT_FUNC3(xrWaitFrame, (XrSession), session, (const XrFrameWaitInfo *), frameWaitInfo, (XrFrameState *), frameState)
255
EXT_PROTO_XRRESULT_FUNC2(xrWaitSwapchainImage, (XrSwapchain), swapchain, (const XrSwapchainImageWaitInfo *), waitInfo)
256
257
// instance
258
bool create_instance();
259
bool get_system_info();
260
bool load_supported_view_configuration_types();
261
bool load_supported_environmental_blend_modes();
262
bool is_view_configuration_supported(XrViewConfigurationType p_configuration_type) const;
263
bool load_supported_view_configuration_views(XrViewConfigurationType p_configuration_type);
264
void destroy_instance();
265
266
// session
267
bool create_session();
268
bool load_supported_reference_spaces();
269
bool is_reference_space_supported(XrReferenceSpaceType p_reference_space);
270
bool setup_play_space();
271
bool setup_view_space();
272
bool load_supported_swapchain_formats();
273
bool is_swapchain_format_supported(int64_t p_swapchain_format);
274
bool obtain_swapchain_formats();
275
bool create_main_swapchains(const Size2i &p_size);
276
void free_main_swapchains();
277
void destroy_session();
278
279
// action map
280
struct Tracker { // Trackers represent tracked physical objects such as controllers, pucks, etc.
281
String name; // Name for this tracker (i.e. "/user/hand/left")
282
XrPath toplevel_path; // OpenXR XrPath for this tracker
283
RID active_profile_rid; // RID of the active profile for this tracker
284
};
285
RID_Owner<Tracker, true> tracker_owner;
286
RID get_tracker_rid(XrPath p_path);
287
bool interaction_profile_changed = true; // If true we need to check for updates to our active_profile_rid.
288
289
struct ActionSet { // Action sets define a set of actions that can be enabled together
290
String name; // Name for this action set (i.e. "godot_action_set")
291
bool is_attached; // If true our action set has been attached to the session and can no longer be modified
292
XrActionSet handle; // OpenXR handle for this action set
293
};
294
RID_Owner<ActionSet, true> action_set_owner;
295
296
struct ActionTracker { // Links and action to a tracker
297
RID tracker_rid; // RID of the tracker
298
XrSpace space; // Optional space for pose actions
299
bool was_location_valid; // If true the last position we obtained was valid
300
};
301
302
struct Action { // Actions define the inputs and outputs in OpenXR
303
RID action_set_rid; // RID of the action set this action belongs to
304
String name; // Name for this action (i.e. "aim_pose")
305
XrActionType action_type; // Type of action (bool, float, etc.)
306
Vector<ActionTracker> trackers; // The trackers this action can be used with
307
XrAction handle; // OpenXR handle for this action
308
};
309
RID_Owner<Action, true> action_owner;
310
RID get_action_rid(XrAction p_action);
311
312
struct InteractionProfile { // Interaction profiles define suggested bindings between the physical inputs on controller types and our actions
313
String name; // Name of the interaction profile (i.e. "/interaction_profiles/valve/index_controller")
314
CharString internal_name; // Internal name of the interaction profile (translated if required)
315
XrPath path; // OpenXR path for this profile
316
Vector<XrActionSuggestedBinding> bindings; // OpenXR action bindings
317
Vector<PackedByteArray> modifiers; // Array of modifiers we'll add into XrBindingModificationsKHR
318
};
319
RID_Owner<InteractionProfile, true> interaction_profile_owner;
320
RID get_interaction_profile_rid(XrPath p_path);
321
XrPath get_interaction_profile_path(RID p_interaction_profile);
322
323
CharString get_interaction_profile_internal_name(const String &p_interaction_profile_name) const;
324
const char *check_profile_path(const CharString &p_interaction_profile_name, const char *p_path) const;
325
326
struct OrderedCompositionLayer {
327
const XrCompositionLayerBaseHeader *composition_layer;
328
int sort_order;
329
330
_FORCE_INLINE_ bool operator()(const OrderedCompositionLayer &a, const OrderedCompositionLayer &b) const {
331
return a.sort_order < b.sort_order || (a.sort_order == b.sort_order && uint64_t(a.composition_layer) < uint64_t(b.composition_layer));
332
}
333
};
334
335
// state changes
336
bool poll_events();
337
bool on_state_idle();
338
bool on_state_ready();
339
bool on_state_synchronized();
340
bool on_state_visible();
341
bool on_state_focused();
342
bool on_state_stopping();
343
bool on_state_loss_pending();
344
bool on_state_exiting();
345
346
// convenience
347
void copy_string_to_char_buffer(const String &p_string, char *p_buffer, int p_buffer_len);
348
349
// Render state, Only accessible in rendering thread
350
struct RenderState {
351
bool running = false;
352
bool should_render = false;
353
bool has_xr_viewport = false;
354
XrTime predicted_display_time = 0;
355
XrSpace play_space = XR_NULL_HANDLE;
356
XrEnvironmentBlendMode environment_blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE;
357
double render_target_size_multiplier = 1.0;
358
uint64_t frame = 0;
359
Rect2i render_region;
360
361
LocalVector<XrView> views;
362
LocalVector<XrCompositionLayerProjectionView> projection_views;
363
LocalVector<XrCompositionLayerDepthInfoKHR> depth_views; // Only used by Composition Layer Depth Extension if available
364
bool submit_depth_buffer = false; // if set to true we submit depth buffers to OpenXR if a suitable extension is enabled.
365
bool view_pose_valid = false;
366
367
double z_near = 0.0;
368
double z_far = 0.0;
369
370
XrCompositionLayerProjection projection_layer = {
371
XR_TYPE_COMPOSITION_LAYER_PROJECTION, // type
372
nullptr, // next
373
0, // layerFlags
374
XR_NULL_HANDLE, // space
375
0, // viewCount
376
nullptr // views
377
};
378
379
Size2i main_swapchain_size;
380
OpenXRSwapChainInfo main_swapchains[OPENXR_SWAPCHAIN_MAX];
381
} render_state;
382
383
static void _allocate_view_buffers_rt(uint32_t p_view_count, bool p_submit_depth_buffer);
384
static void _set_render_session_running_rt(bool p_is_running);
385
static void _set_render_display_info_rt(XrTime p_predicted_display_time, bool p_should_render);
386
static void _set_render_play_space_rt(uint64_t p_play_space);
387
static void _set_render_environment_blend_mode_rt(int32_t p_environment_blend_mode);
388
static void _set_render_state_multiplier_rt(double p_render_target_size_multiplier);
389
static void _set_render_state_render_region_rt(const Rect2i &p_render_region);
390
static void _update_main_swapchain_size_rt();
391
392
_FORCE_INLINE_ void allocate_view_buffers(uint32_t p_view_count, bool p_submit_depth_buffer) {
393
// If we're rendering on a separate thread, we may still be processing the last frame, don't communicate this till we're ready...
394
RenderingServer *rendering_server = RenderingServer::get_singleton();
395
ERR_FAIL_NULL(rendering_server);
396
397
rendering_server->call_on_render_thread(callable_mp_static(&OpenXRAPI::_allocate_view_buffers_rt).bind(p_view_count, p_submit_depth_buffer));
398
}
399
400
_FORCE_INLINE_ void set_render_session_running(bool p_is_running) {
401
// If we're rendering on a separate thread, we may still be processing the last frame, don't communicate this till we're ready...
402
RenderingServer *rendering_server = RenderingServer::get_singleton();
403
ERR_FAIL_NULL(rendering_server);
404
405
rendering_server->call_on_render_thread(callable_mp_static(&OpenXRAPI::_set_render_session_running_rt).bind(p_is_running));
406
}
407
408
_FORCE_INLINE_ void set_render_display_info(XrTime p_predicted_display_time, bool p_should_render) {
409
// If we're rendering on a separate thread, we may still be processing the last frame, don't communicate this till we're ready...
410
RenderingServer *rendering_server = RenderingServer::get_singleton();
411
ERR_FAIL_NULL(rendering_server);
412
413
rendering_server->call_on_render_thread(callable_mp_static(&OpenXRAPI::_set_render_display_info_rt).bind(p_predicted_display_time, p_should_render));
414
}
415
416
_FORCE_INLINE_ void set_render_play_space(XrSpace p_play_space) {
417
// If we're rendering on a separate thread, we may still be processing the last frame, don't communicate this till we're ready...
418
RenderingServer *rendering_server = RenderingServer::get_singleton();
419
ERR_FAIL_NULL(rendering_server);
420
421
rendering_server->call_on_render_thread(callable_mp_static(&OpenXRAPI::_set_render_play_space_rt).bind(uint64_t(p_play_space)));
422
}
423
424
_FORCE_INLINE_ void set_render_environment_blend_mode(XrEnvironmentBlendMode p_mode) {
425
// If we're rendering on a separate thread, we may still be processing the last frame, don't communicate this till we're ready...
426
RenderingServer *rendering_server = RenderingServer::get_singleton();
427
ERR_FAIL_NULL(rendering_server);
428
429
rendering_server->call_on_render_thread(callable_mp_static(&OpenXRAPI::_set_render_environment_blend_mode_rt).bind((int32_t)p_mode));
430
}
431
432
_FORCE_INLINE_ void set_render_state_multiplier(double p_render_target_size_multiplier) {
433
// If we're rendering on a separate thread, we may still be processing the last frame, don't communicate this till we're ready...
434
RenderingServer *rendering_server = RenderingServer::get_singleton();
435
ERR_FAIL_NULL(rendering_server);
436
437
rendering_server->call_on_render_thread(callable_mp_static(&OpenXRAPI::_set_render_state_multiplier_rt).bind(p_render_target_size_multiplier));
438
}
439
440
_FORCE_INLINE_ void set_render_state_render_region(const Rect2i &p_render_region) {
441
RenderingServer *rendering_server = RenderingServer::get_singleton();
442
ERR_FAIL_NULL(rendering_server);
443
444
rendering_server->call_on_render_thread(callable_mp_static(&OpenXRAPI::_set_render_state_render_region_rt).bind(p_render_region));
445
}
446
447
public:
448
_FORCE_INLINE_ void update_main_swapchain_size() {
449
RenderingServer *rendering_server = RenderingServer::get_singleton();
450
ERR_FAIL_NULL(rendering_server);
451
452
rendering_server->call_on_render_thread(callable_mp_static(&OpenXRAPI::_update_main_swapchain_size_rt));
453
}
454
455
XrVersion get_openxr_version() const { return openxr_version; }
456
XrInstance get_instance() const { return instance; }
457
XrSystemId get_system_id() const { return system_id; }
458
XrSession get_session() const { return session; }
459
XrSessionState get_session_state() const { return session_state; }
460
OpenXRGraphicsExtensionWrapper *get_graphics_extension() const { return graphics_extension; }
461
String get_runtime_name() const { return runtime_name; }
462
String get_runtime_version() const { return runtime_version; }
463
String get_system_name() const { return system_name; }
464
uint32_t get_vendor_id() const { return vendor_id; }
465
466
// helper method to convert an XrPosef to a Transform3D
467
Transform3D transform_from_pose(const XrPosef &p_pose);
468
XrPosef pose_from_transform(const Transform3D &p_transform);
469
470
// helper method to get a valid Transform3D from an openxr space location
471
XRPose::TrackingConfidence transform_from_location(const XrSpaceLocation &p_location, Transform3D &r_transform);
472
XRPose::TrackingConfidence transform_from_location(const XrHandJointLocationEXT &p_location, Transform3D &r_transform);
473
void parse_velocities(const XrSpaceVelocity &p_velocity, Vector3 &r_linear_velocity, Vector3 &r_angular_velocity);
474
bool xr_result(XrResult p_result, const char *p_format, const Array &p_args = Array()) const;
475
XrPath get_xr_path(const String &p_path);
476
String get_xr_path_name(const XrPath &p_path);
477
bool is_top_level_path_supported(const String &p_toplevel_path);
478
bool is_interaction_profile_supported(const String &p_ip_path);
479
bool interaction_profile_supports_io_path(const String &p_ip_path, const String &p_io_path);
480
481
static bool openxr_is_enabled(bool p_check_run_in_editor = true);
482
_FORCE_INLINE_ static OpenXRAPI *get_singleton() { return singleton; }
483
484
XrResult try_get_instance_proc_addr(const char *p_name, PFN_xrVoidFunction *p_addr);
485
XrResult get_instance_proc_addr(const char *p_name, PFN_xrVoidFunction *p_addr);
486
String get_error_string(XrResult result) const;
487
String get_swapchain_format_name(int64_t p_swapchain_format) const;
488
void set_object_name(XrObjectType p_object_type, uint64_t p_object_handle, const String &p_object_name);
489
void begin_debug_label_region(const String &p_label_name);
490
void end_debug_label_region();
491
void insert_debug_label(const String &p_label_name);
492
493
OpenXRInterface *get_xr_interface() const { return xr_interface; }
494
void set_xr_interface(OpenXRInterface *p_xr_interface);
495
static void register_extension_wrapper(OpenXRExtensionWrapper *p_extension_wrapper);
496
static void unregister_extension_wrapper(OpenXRExtensionWrapper *p_extension_wrapper);
497
static const Vector<OpenXRExtensionWrapper *> &get_registered_extension_wrappers();
498
static void register_extension_metadata();
499
static void cleanup_extension_wrappers();
500
static PackedStringArray get_all_requested_extensions(XrVersion p_xr_version);
501
502
void set_form_factor(XrFormFactor p_form_factor);
503
XrFormFactor get_form_factor() const { return form_factor; }
504
505
uint32_t get_view_count();
506
void set_view_configuration(XrViewConfigurationType p_view_configuration);
507
XrViewConfigurationType get_view_configuration() const { return view_configuration; }
508
509
bool set_requested_reference_space(XrReferenceSpaceType p_requested_reference_space);
510
XrReferenceSpaceType get_requested_reference_space() const { return requested_reference_space; }
511
XrReferenceSpaceType get_reference_space() const { return reference_space; }
512
void set_custom_play_space(XrSpace p_custom_space);
513
514
void set_submit_depth_buffer(bool p_submit_depth_buffer);
515
bool get_submit_depth_buffer() const { return submit_depth_buffer; }
516
517
bool is_initialized();
518
bool is_running();
519
bool initialize(const String &p_rendering_driver);
520
bool initialize_session();
521
void finish();
522
523
_FORCE_INLINE_ XrSpace get_play_space() const { return play_space; }
524
_FORCE_INLINE_ XrSpace get_view_space() const { return view_space; }
525
_FORCE_INLINE_ XrTime get_predicted_display_time() { return frame_state.predictedDisplayTime; }
526
_FORCE_INLINE_ XrTime get_next_frame_time() { return frame_state.predictedDisplayTime + frame_state.predictedDisplayPeriod; }
527
_FORCE_INLINE_ bool can_render() {
528
return instance != XR_NULL_HANDLE && session != XR_NULL_HANDLE && running && frame_state.shouldRender;
529
}
530
531
XrHandTrackerEXT get_hand_tracker(int p_hand_index);
532
533
Size2 get_recommended_target_size();
534
XRPose::TrackingConfidence get_head_center(Transform3D &r_transform, Vector3 &r_linear_velocity, Vector3 &r_angular_velocity);
535
bool get_view_transform(uint32_t p_view, Transform3D &r_transform);
536
bool get_view_projection(uint32_t p_view, double p_z_near, double p_z_far, Projection &p_camera_matrix);
537
Vector2 get_eye_focus(uint32_t p_view, float p_aspect);
538
bool process();
539
540
void pre_render();
541
bool pre_draw_viewport(RID p_render_target);
542
XrSwapchain get_color_swapchain();
543
RID get_color_texture();
544
RID get_depth_texture();
545
RID get_density_map_texture();
546
void set_velocity_texture(RID p_render_target);
547
RID get_velocity_texture();
548
void set_velocity_depth_texture(RID p_render_target);
549
RID get_velocity_depth_texture();
550
void set_velocity_target_size(const Size2i &p_target_size);
551
Size2i get_velocity_target_size();
552
const XrCompositionLayerProjection *get_projection_layer() const;
553
void post_draw_viewport(RID p_render_target);
554
void end_frame();
555
556
// Display refresh rate
557
float get_display_refresh_rate() const;
558
void set_display_refresh_rate(float p_refresh_rate);
559
Array get_available_display_refresh_rates() const;
560
561
// Render Target size multiplier
562
double get_render_target_size_multiplier() const;
563
void set_render_target_size_multiplier(double multiplier);
564
565
Rect2i get_render_region() const;
566
void set_render_region(const Rect2i &p_render_region);
567
568
// Foveation settings
569
bool is_foveation_supported() const;
570
571
int get_foveation_level() const;
572
void set_foveation_level(int p_foveation_level);
573
574
bool get_foveation_dynamic() const;
575
void set_foveation_dynamic(bool p_foveation_dynamic);
576
577
// Play space.
578
Size2 get_play_space_bounds() const;
579
580
// swapchains
581
PackedInt64Array get_supported_swapchain_formats();
582
int64_t get_color_swapchain_format() const { return color_swapchain_format; }
583
int64_t get_depth_swapchain_format() const { return depth_swapchain_format; }
584
585
double get_render_state_z_near() const { return render_state.z_near; }
586
double get_render_state_z_far() const { return render_state.z_far; }
587
588
// action map
589
String get_default_action_map_resource_name();
590
591
RID tracker_create(const String &p_name);
592
String tracker_get_name(RID p_tracker);
593
void tracker_check_profile(RID p_tracker, XrSession p_session = XR_NULL_HANDLE);
594
void tracker_free(RID p_tracker);
595
596
RID action_set_create(const String &p_name, const String &p_localized_name, const int p_priority);
597
String action_set_get_name(RID p_action_set);
598
XrActionSet action_set_get_handle(RID p_action_set);
599
bool attach_action_sets(const Vector<RID> &p_action_sets);
600
void action_set_free(RID p_action_set);
601
602
RID action_create(RID p_action_set, const String &p_name, const String &p_localized_name, OpenXRAction::ActionType p_action_type, const Vector<RID> &p_trackers);
603
String action_get_name(RID p_action);
604
XrAction action_get_handle(RID p_action);
605
void action_free(RID p_action);
606
607
RID interaction_profile_create(const String &p_name);
608
String interaction_profile_get_name(RID p_interaction_profile);
609
void interaction_profile_clear_bindings(RID p_interaction_profile);
610
int interaction_profile_add_binding(RID p_interaction_profile, RID p_action, const String &p_path);
611
bool interaction_profile_add_modifier(RID p_interaction_profile, const PackedByteArray &p_modifier);
612
bool interaction_profile_suggest_bindings(RID p_interaction_profile);
613
void interaction_profile_free(RID p_interaction_profile);
614
615
RID find_tracker(const String &p_name);
616
RID find_action_set(const String &p_name);
617
RID find_action(const String &p_name, const RID &p_action_set = RID());
618
619
bool sync_action_sets(const Vector<RID> &p_active_sets);
620
bool get_action_bool(RID p_action, RID p_tracker);
621
float get_action_float(RID p_action, RID p_tracker);
622
Vector2 get_action_vector2(RID p_action, RID p_tracker);
623
XRPose::TrackingConfidence get_action_pose(RID p_action, RID p_tracker, Transform3D &r_transform, Vector3 &r_linear_velocity, Vector3 &r_angular_velocity);
624
bool trigger_haptic_pulse(RID p_action, RID p_tracker, float p_frequency, float p_amplitude, XrDuration p_duration_ns);
625
626
void register_composition_layer_provider(OpenXRExtensionWrapper *p_extension);
627
void unregister_composition_layer_provider(OpenXRExtensionWrapper *p_extension);
628
629
void register_projection_views_extension(OpenXRExtensionWrapper *p_extension);
630
void unregister_projection_views_extension(OpenXRExtensionWrapper *p_extension);
631
632
void register_frame_info_extension(OpenXRExtensionWrapper *p_extension);
633
void unregister_frame_info_extension(OpenXRExtensionWrapper *p_extension);
634
635
const Vector<XrEnvironmentBlendMode> get_supported_environment_blend_modes();
636
637
bool is_environment_blend_mode_supported(XrEnvironmentBlendMode p_blend_mode) const;
638
bool set_environment_blend_mode(XrEnvironmentBlendMode p_blend_mode);
639
XrEnvironmentBlendMode get_environment_blend_mode() const { return requested_environment_blend_mode; }
640
641
enum OpenXRAlphaBlendModeSupport {
642
OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE = 0,
643
OPENXR_ALPHA_BLEND_MODE_SUPPORT_REAL = 1,
644
OPENXR_ALPHA_BLEND_MODE_SUPPORT_EMULATING = 2,
645
};
646
647
void set_emulate_environment_blend_mode_alpha_blend(bool p_enabled);
648
OpenXRAlphaBlendModeSupport is_environment_blend_mode_alpha_blend_supported();
649
650
OpenXRAPI();
651
~OpenXRAPI();
652
};
653
654