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