openxr_api.h 28 KB

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