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