openxr_api.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*************************************************************************/
  2. /* openxr_api.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_DRIVER_H
  31. #define OPENXR_DRIVER_H
  32. #include "core/error/error_macros.h"
  33. #include "core/math/camera_matrix.h"
  34. #include "core/math/transform_3d.h"
  35. #include "core/math/vector2.h"
  36. #include "core/os/memory.h"
  37. #include "core/string/ustring.h"
  38. #include "core/templates/map.h"
  39. #include "core/templates/rid_owner.h"
  40. #include "core/templates/vector.h"
  41. #include "servers/xr/xr_pose.h"
  42. #include "thirdparty/openxr/src/common/xr_linear.h"
  43. #include <openxr/openxr.h>
  44. #include "action_map/openxr_action.h"
  45. #include "extensions/openxr_composition_layer_provider.h"
  46. #include "extensions/openxr_extension_wrapper.h"
  47. // Note, OpenXR code that we wrote for our plugin makes use of C++20 notation for initialising structs which ensures zeroing out unspecified members.
  48. // Godot is currently restricted to C++17 which doesn't allow this notation. Make sure critical fields are set.
  49. // forward declarations, we don't want to include these fully
  50. class OpenXRVulkanExtension;
  51. class OpenXRInterface;
  52. class OpenXRAPI {
  53. private:
  54. // our singleton
  55. static OpenXRAPI *singleton;
  56. // linked XR interface
  57. OpenXRInterface *xr_interface = nullptr;
  58. // layers
  59. uint32_t num_layer_properties = 0;
  60. XrApiLayerProperties *layer_properties = nullptr;
  61. // extensions
  62. uint32_t num_supported_extensions = 0;
  63. XrExtensionProperties *supported_extensions = nullptr;
  64. Vector<OpenXRExtensionWrapper *> registered_extension_wrappers;
  65. Vector<const char *> enabled_extensions;
  66. bool ext_hp_mixed_reality_available = false;
  67. bool ext_samsung_odyssey_available = false;
  68. bool ext_vive_cosmos_available = false;
  69. bool ext_vive_focus3_available = false;
  70. bool ext_huawei_controller_available = false;
  71. // composition layer providers
  72. Vector<OpenXRCompositionLayerProvider *> composition_layer_providers;
  73. // view configuration
  74. uint32_t num_view_configuration_types = 0;
  75. XrViewConfigurationType *supported_view_configuration_types = nullptr;
  76. // reference spaces
  77. uint32_t num_reference_spaces = 0;
  78. XrReferenceSpaceType *supported_reference_spaces = nullptr;
  79. // swapchains (note these are platform dependent)
  80. uint32_t num_swapchain_formats = 0;
  81. int64_t *supported_swapchain_formats = nullptr;
  82. // configuration
  83. XrFormFactor form_factor = XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY;
  84. XrViewConfigurationType view_configuration = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO;
  85. XrReferenceSpaceType reference_space = XR_REFERENCE_SPACE_TYPE_STAGE;
  86. XrEnvironmentBlendMode environment_blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE;
  87. // state
  88. XrInstance instance = XR_NULL_HANDLE;
  89. XrSystemId system_id = 0;
  90. String system_name;
  91. uint32_t vendor_id = 0;
  92. XrSystemTrackingProperties tracking_properties;
  93. XrSession session = XR_NULL_HANDLE;
  94. XrSessionState session_state = XR_SESSION_STATE_UNKNOWN;
  95. bool running = false;
  96. XrFrameState frame_state = { XR_TYPE_FRAME_STATE, NULL, 0, 0, false };
  97. OpenXRGraphicsExtensionWrapper *graphics_extension = nullptr;
  98. XrSystemGraphicsProperties graphics_properties;
  99. void *swapchain_graphics_data = nullptr;
  100. uint32_t image_index = 0;
  101. bool image_acquired = false;
  102. uint32_t view_count = 0;
  103. XrViewConfigurationView *view_configuration_views = nullptr;
  104. XrView *views = nullptr;
  105. XrCompositionLayerProjectionView *projection_views = nullptr;
  106. XrSwapchain swapchain = XR_NULL_HANDLE;
  107. XrSpace play_space = XR_NULL_HANDLE;
  108. XrSpace view_space = XR_NULL_HANDLE;
  109. bool view_pose_valid = false;
  110. XRPose::TrackingConfidence head_pose_confidence = XRPose::XR_TRACKING_CONFIDENCE_NONE;
  111. bool load_layer_properties();
  112. bool load_supported_extensions();
  113. bool is_extension_supported(const char *p_extension) const;
  114. // instance
  115. bool create_instance();
  116. bool get_system_info();
  117. bool load_supported_view_configuration_types();
  118. bool is_view_configuration_supported(XrViewConfigurationType p_configuration_type) const;
  119. bool load_supported_view_configuration_views(XrViewConfigurationType p_configuration_type);
  120. void destroy_instance();
  121. // session
  122. bool create_session();
  123. bool load_supported_reference_spaces();
  124. bool is_reference_space_supported(XrReferenceSpaceType p_reference_space);
  125. bool setup_spaces();
  126. bool load_supported_swapchain_formats();
  127. bool is_swapchain_format_supported(int64_t p_swapchain_format);
  128. bool create_main_swapchain();
  129. void destroy_session();
  130. // swapchains
  131. bool create_swapchain(int64_t p_swapchain_format, uint32_t p_width, uint32_t p_height, uint32_t p_sample_count, uint32_t p_array_size, XrSwapchain &r_swapchain, void **r_swapchain_graphics_data);
  132. bool acquire_image(XrSwapchain p_swapchain, uint32_t &r_image_index);
  133. bool release_image(XrSwapchain p_swapchain);
  134. // action map
  135. struct Tracker { // Trackers represent tracked physical objects such as controllers, pucks, etc.
  136. String name; // Name for this tracker (i.e. "/user/hand/left")
  137. XrPath toplevel_path; // OpenXR XrPath for this tracker
  138. RID active_profile_rid; // RID of the active profile for this tracker
  139. };
  140. RID_Owner<Tracker, true> tracker_owner;
  141. RID get_tracker_rid(XrPath p_path);
  142. struct ActionSet { // Action sets define a set of actions that can be enabled together
  143. String name; // Name for this action set (i.e. "godot_action_set")
  144. bool is_attached; // If true our action set has been attached to the session and can no longer be modified
  145. XrActionSet handle; // OpenXR handle for this action set
  146. };
  147. RID_Owner<ActionSet, true> action_set_owner;
  148. struct ActionTracker { // Links and action to a tracker
  149. RID tracker_rid; // RID of the tracker
  150. XrSpace space; // Optional space for pose actions
  151. bool was_location_valid; // If true the last position we obtained was valid
  152. };
  153. struct Action { // Actions define the inputs and outputs in OpenXR
  154. RID action_set_rid; // RID of the action set this action belongs to
  155. String name; // Name for this action (i.e. "aim_pose")
  156. XrActionType action_type; // Type of action (bool, float, etc.)
  157. Vector<ActionTracker> trackers; // The trackers this action can be used with
  158. XrAction handle; // OpenXR handle for this action
  159. };
  160. RID_Owner<Action, true> action_owner;
  161. RID get_action_rid(XrAction p_action);
  162. struct InteractionProfile { // Interaction profiles define suggested bindings between the physical inputs on controller types and our actions
  163. String name; // Name of the interaction profile (i.e. "/interaction_profiles/valve/index_controller")
  164. XrPath path; // OpenXR path for this profile
  165. Vector<XrActionSuggestedBinding> bindings; // OpenXR action bindings
  166. };
  167. RID_Owner<InteractionProfile, true> interaction_profile_owner;
  168. RID get_interaction_profile_rid(XrPath p_path);
  169. XrPath get_interaction_profile_path(RID p_interaction_profile);
  170. // state changes
  171. bool poll_events();
  172. bool on_state_idle();
  173. bool on_state_ready();
  174. bool on_state_synchronized();
  175. bool on_state_visible();
  176. bool on_state_focused();
  177. bool on_state_stopping();
  178. bool on_state_loss_pending();
  179. bool on_state_exiting();
  180. // convencience
  181. void copy_string_to_char_buffer(const String p_string, char *p_buffer, int p_buffer_len);
  182. protected:
  183. friend class OpenXRVulkanExtension;
  184. XrInstance get_instance() const { return instance; };
  185. XrSystemId get_system_id() const { return system_id; };
  186. XrSession get_session() const { return session; };
  187. // helper method to convert an XrPosef to a Transform3D
  188. Transform3D transform_from_pose(const XrPosef &p_pose);
  189. // helper method to get a valid Transform3D from an openxr space location
  190. XRPose::TrackingConfidence transform_from_location(const XrSpaceLocation &p_location, Transform3D &r_transform);
  191. XRPose::TrackingConfidence transform_from_location(const XrHandJointLocationEXT &p_location, Transform3D &r_transform);
  192. void parse_velocities(const XrSpaceVelocity &p_velocity, Vector3 &r_linear_velocity, Vector3 r_angular_velocity);
  193. public:
  194. static bool openxr_is_enabled(bool p_check_run_in_editor = true);
  195. static OpenXRAPI *get_singleton();
  196. String get_error_string(XrResult result);
  197. String get_swapchain_format_name(int64_t p_swapchain_format) const;
  198. void set_xr_interface(OpenXRInterface *p_xr_interface);
  199. void register_extension_wrapper(OpenXRExtensionWrapper *p_extension_wrapper);
  200. bool is_initialized();
  201. bool is_running();
  202. bool initialize(const String &p_rendering_driver);
  203. bool initialize_session();
  204. void finish();
  205. XrTime get_next_frame_time() { return frame_state.predictedDisplayTime + frame_state.predictedDisplayPeriod; };
  206. bool can_render() { return instance != XR_NULL_HANDLE && session != XR_NULL_HANDLE && running && view_pose_valid && frame_state.shouldRender; };
  207. Size2 get_recommended_target_size();
  208. XRPose::TrackingConfidence get_head_center(Transform3D &r_transform, Vector3 &r_linear_velocity, const Vector3 &r_angular_velocity);
  209. bool get_view_transform(uint32_t p_view, Transform3D &r_transform);
  210. bool get_view_projection(uint32_t p_view, double p_z_near, double p_z_far, CameraMatrix &p_camera_matrix);
  211. bool process();
  212. void pre_render();
  213. bool pre_draw_viewport(RID p_render_target);
  214. void post_draw_viewport(RID p_render_target);
  215. void end_frame();
  216. // action map
  217. String get_default_action_map_resource_name();
  218. RID tracker_create(const String p_name);
  219. String tracker_get_name(RID p_tracker);
  220. void tracker_check_profile(RID p_tracker, XrSession p_session = XR_NULL_HANDLE);
  221. void tracker_free(RID p_tracker);
  222. RID action_set_create(const String p_name, const String p_localized_name, const int p_priority);
  223. String action_set_get_name(RID p_action_set);
  224. bool action_set_attach(RID p_action_set);
  225. void action_set_free(RID p_action_set);
  226. 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);
  227. String action_get_name(RID p_action);
  228. void action_free(RID p_action);
  229. RID interaction_profile_create(const String p_name);
  230. String interaction_profile_get_name(RID p_interaction_profile);
  231. void interaction_profile_clear_bindings(RID p_interaction_profile);
  232. bool interaction_profile_add_binding(RID p_interaction_profile, RID p_action, const String p_path);
  233. bool interaction_profile_suggest_bindings(RID p_interaction_profile);
  234. void interaction_profile_free(RID p_interaction_profile);
  235. bool sync_action_sets(const Vector<RID> p_active_sets);
  236. bool get_action_bool(RID p_action, RID p_tracker);
  237. float get_action_float(RID p_action, RID p_tracker);
  238. Vector2 get_action_vector2(RID p_action, RID p_tracker);
  239. XRPose::TrackingConfidence get_action_pose(RID p_action, RID p_tracker, Transform3D &r_transform, Vector3 &r_linear_velocity, const Vector3 &r_angular_velocity);
  240. bool trigger_haptic_pulse(RID p_action, RID p_tracker, float p_frequency, float p_amplitude, XrDuration p_duration_ns);
  241. OpenXRAPI();
  242. ~OpenXRAPI();
  243. };
  244. #endif // !OPENXR_DRIVER_H