openxr_api.h 30 KB

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