openxr_interface.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**************************************************************************/
  2. /* openxr_interface.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_INTERFACE_H
  31. #define OPENXR_INTERFACE_H
  32. // A note on multithreading and thread safety in OpenXR.
  33. //
  34. // Most entry points will be called from the main thread in Godot
  35. // however a number of entry points will be called from the
  36. // rendering thread, potentially while we're already processing
  37. // the next frame on the main thread.
  38. //
  39. // OpenXR itself has been designed with threading in mind including
  40. // a high likelihood that the XR runtime runs in separate threads
  41. // as well.
  42. // Hence all the frame timing information, use of swapchains and
  43. // sync functions.
  44. // Do note that repeated calls to tracking APIs will provide
  45. // increasingly more accurate data for the same timestamp as
  46. // tracking data is continuously updated.
  47. //
  48. // For our code we mostly implement this in our OpenXRAPI class.
  49. // We store data accessed from the rendering thread in a separate
  50. // struct, setting values through our renderer command queue.
  51. //
  52. // As some data is setup before we start rendering, and cleaned up
  53. // after we've stopped, that is accessed directly from both threads.
  54. #include "action_map/openxr_action_map.h"
  55. #include "extensions/openxr_hand_tracking_extension.h"
  56. #include "openxr_api.h"
  57. #include "servers/xr/xr_controller_tracker.h"
  58. #include "servers/xr/xr_interface.h"
  59. // declare some default strings
  60. #define INTERACTION_PROFILE_NONE "/interaction_profiles/none"
  61. class OpenXRInterface : public XRInterface {
  62. GDCLASS(OpenXRInterface, XRInterface);
  63. private:
  64. OpenXRAPI *openxr_api = nullptr;
  65. bool initialized = false;
  66. bool reference_stage_changing = false;
  67. XRInterface::TrackingStatus tracking_state;
  68. // At a minimum we need a tracker for our head
  69. Ref<XRPositionalTracker> head;
  70. Transform3D head_transform;
  71. Vector3 head_linear_velocity;
  72. Vector3 head_angular_velocity;
  73. XRPose::TrackingConfidence head_confidence;
  74. Transform3D transform_for_view[2]; // We currently assume 2, but could be 4 for VARJO which we do not support yet
  75. XRVRS xr_vrs;
  76. void _load_action_map();
  77. struct Action { // An action we've registered with OpenXR
  78. String action_name; // Name of our action as presented to Godot (can be altered from the action map)
  79. OpenXRAction::ActionType action_type; // The action type of this action
  80. RID action_rid; // RID of the action registered with our OpenXR API
  81. };
  82. struct ActionSet { // An action set we've registered with OpenXR
  83. String action_set_name; // Name of our action set
  84. bool is_active; // If true this action set is active and we will sync it
  85. Vector<Action *> actions; // List of actions in this action set
  86. RID action_set_rid; // RID of the action registered with our OpenXR API
  87. };
  88. struct Tracker { // A tracker we've registered with OpenXR
  89. String tracker_name; // Name of our tracker (can be altered from the action map)
  90. Vector<Action *> actions; // Actions related to this tracker
  91. Ref<XRControllerTracker> controller_tracker; // Our positional tracker object that holds our tracker state
  92. RID tracker_rid; // RID of the tracker registered with our OpenXR API
  93. RID interaction_profile; // RID of the interaction profile bound to this tracker (can be null)
  94. };
  95. Vector<ActionSet *> action_sets;
  96. Vector<RID> interaction_profiles;
  97. Vector<Tracker *> trackers;
  98. ActionSet *create_action_set(const String &p_action_set_name, const String &p_localized_name, const int p_priority);
  99. void free_action_sets();
  100. Action *create_action(ActionSet *p_action_set, const String &p_action_name, const String &p_localized_name, OpenXRAction::ActionType p_action_type, const Vector<Tracker *> p_trackers);
  101. Action *find_action(const String &p_action_name);
  102. void free_actions(ActionSet *p_action_set);
  103. Tracker *find_tracker(const String &p_tracker_name, bool p_create = false);
  104. void handle_tracker(Tracker *p_tracker);
  105. void free_trackers();
  106. void free_interaction_profiles();
  107. void _set_default_pos(Transform3D &p_transform, double p_world_scale, uint64_t p_eye);
  108. void handle_hand_tracking(const String &p_path, OpenXRHandTrackingExtension::HandTrackedHands p_hand);
  109. protected:
  110. static void _bind_methods();
  111. public:
  112. virtual StringName get_name() const override;
  113. virtual uint32_t get_capabilities() const override;
  114. virtual PackedStringArray get_suggested_tracker_names() const override;
  115. virtual TrackingStatus get_tracking_status() const override;
  116. bool is_hand_tracking_supported();
  117. bool is_hand_interaction_supported() const;
  118. bool is_eye_gaze_interaction_supported();
  119. bool initialize_on_startup() const;
  120. virtual bool is_initialized() const override;
  121. virtual bool initialize() override;
  122. virtual void uninitialize() override;
  123. virtual Dictionary get_system_info() override;
  124. virtual void trigger_haptic_pulse(const String &p_action_name, const StringName &p_tracker_name, double p_frequency, double p_amplitude, double p_duration_sec, double p_delay_sec = 0) override;
  125. virtual bool supports_play_area_mode(XRInterface::PlayAreaMode p_mode) override;
  126. virtual XRInterface::PlayAreaMode get_play_area_mode() const override;
  127. virtual bool set_play_area_mode(XRInterface::PlayAreaMode p_mode) override;
  128. virtual PackedVector3Array get_play_area() const override;
  129. float get_display_refresh_rate() const;
  130. void set_display_refresh_rate(float p_refresh_rate);
  131. Array get_available_display_refresh_rates() const;
  132. bool is_action_set_active(const String &p_action_set) const;
  133. void set_action_set_active(const String &p_action_set, bool p_active);
  134. Array get_action_sets() const;
  135. double get_render_target_size_multiplier() const;
  136. void set_render_target_size_multiplier(double multiplier);
  137. bool is_foveation_supported() const;
  138. int get_foveation_level() const;
  139. void set_foveation_level(int p_foveation_level);
  140. bool get_foveation_dynamic() const;
  141. void set_foveation_dynamic(bool p_foveation_dynamic);
  142. float get_vrs_min_radius() const;
  143. void set_vrs_min_radius(float p_vrs_min_radius);
  144. float get_vrs_strength() const;
  145. void set_vrs_strength(float p_vrs_strength);
  146. virtual Size2 get_render_target_size() override;
  147. virtual uint32_t get_view_count() override;
  148. virtual Transform3D get_camera_transform() override;
  149. virtual Transform3D get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) override;
  150. virtual Projection get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) override;
  151. virtual RID get_color_texture() override;
  152. virtual RID get_depth_texture() override;
  153. virtual void process() override;
  154. virtual void pre_render() override;
  155. bool pre_draw_viewport(RID p_render_target) override;
  156. virtual Vector<BlitToScreen> post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) override;
  157. virtual void end_frame() override;
  158. virtual bool is_passthrough_supported() override;
  159. virtual bool is_passthrough_enabled() override;
  160. virtual bool start_passthrough() override;
  161. virtual void stop_passthrough() override;
  162. /** environment blend mode. */
  163. virtual Array get_supported_environment_blend_modes() override;
  164. virtual XRInterface::EnvironmentBlendMode get_environment_blend_mode() const override;
  165. virtual bool set_environment_blend_mode(XRInterface::EnvironmentBlendMode mode) override;
  166. void on_state_ready();
  167. void on_state_visible();
  168. void on_state_focused();
  169. void on_state_stopping();
  170. void on_state_loss_pending();
  171. void on_state_exiting();
  172. void on_reference_space_change_pending();
  173. void on_refresh_rate_changes(float p_new_rate);
  174. void tracker_profile_changed(RID p_tracker, RID p_interaction_profile);
  175. /** Hand tracking. */
  176. enum Hand {
  177. HAND_LEFT,
  178. HAND_RIGHT,
  179. HAND_MAX,
  180. };
  181. enum HandMotionRange {
  182. HAND_MOTION_RANGE_UNOBSTRUCTED,
  183. HAND_MOTION_RANGE_CONFORM_TO_CONTROLLER,
  184. HAND_MOTION_RANGE_MAX
  185. };
  186. void set_motion_range(const Hand p_hand, const HandMotionRange p_motion_range);
  187. HandMotionRange get_motion_range(const Hand p_hand) const;
  188. enum HandTrackedSource {
  189. HAND_TRACKED_SOURCE_UNKNOWN,
  190. HAND_TRACKED_SOURCE_UNOBSTRUCTED,
  191. HAND_TRACKED_SOURCE_CONTROLLER,
  192. HAND_TRACKED_SOURCE_MAX
  193. };
  194. HandTrackedSource get_hand_tracking_source(const Hand p_hand) const;
  195. enum HandJoints {
  196. HAND_JOINT_PALM = 0,
  197. HAND_JOINT_WRIST = 1,
  198. HAND_JOINT_THUMB_METACARPAL = 2,
  199. HAND_JOINT_THUMB_PROXIMAL = 3,
  200. HAND_JOINT_THUMB_DISTAL = 4,
  201. HAND_JOINT_THUMB_TIP = 5,
  202. HAND_JOINT_INDEX_METACARPAL = 6,
  203. HAND_JOINT_INDEX_PROXIMAL = 7,
  204. HAND_JOINT_INDEX_INTERMEDIATE = 8,
  205. HAND_JOINT_INDEX_DISTAL = 9,
  206. HAND_JOINT_INDEX_TIP = 10,
  207. HAND_JOINT_MIDDLE_METACARPAL = 11,
  208. HAND_JOINT_MIDDLE_PROXIMAL = 12,
  209. HAND_JOINT_MIDDLE_INTERMEDIATE = 13,
  210. HAND_JOINT_MIDDLE_DISTAL = 14,
  211. HAND_JOINT_MIDDLE_TIP = 15,
  212. HAND_JOINT_RING_METACARPAL = 16,
  213. HAND_JOINT_RING_PROXIMAL = 17,
  214. HAND_JOINT_RING_INTERMEDIATE = 18,
  215. HAND_JOINT_RING_DISTAL = 19,
  216. HAND_JOINT_RING_TIP = 20,
  217. HAND_JOINT_LITTLE_METACARPAL = 21,
  218. HAND_JOINT_LITTLE_PROXIMAL = 22,
  219. HAND_JOINT_LITTLE_INTERMEDIATE = 23,
  220. HAND_JOINT_LITTLE_DISTAL = 24,
  221. HAND_JOINT_LITTLE_TIP = 25,
  222. HAND_JOINT_MAX = 26,
  223. };
  224. enum HandJointFlags {
  225. HAND_JOINT_NONE = 0,
  226. HAND_JOINT_ORIENTATION_VALID = 1,
  227. HAND_JOINT_ORIENTATION_TRACKED = 2,
  228. HAND_JOINT_POSITION_VALID = 4,
  229. HAND_JOINT_POSITION_TRACKED = 8,
  230. HAND_JOINT_LINEAR_VELOCITY_VALID = 16,
  231. HAND_JOINT_ANGULAR_VELOCITY_VALID = 32,
  232. };
  233. BitField<HandJointFlags> get_hand_joint_flags(Hand p_hand, HandJoints p_joint) const;
  234. Quaternion get_hand_joint_rotation(Hand p_hand, HandJoints p_joint) const;
  235. Vector3 get_hand_joint_position(Hand p_hand, HandJoints p_joint) const;
  236. float get_hand_joint_radius(Hand p_hand, HandJoints p_joint) const;
  237. Vector3 get_hand_joint_linear_velocity(Hand p_hand, HandJoints p_joint) const;
  238. Vector3 get_hand_joint_angular_velocity(Hand p_hand, HandJoints p_joint) const;
  239. virtual RID get_vrs_texture() override;
  240. OpenXRInterface();
  241. ~OpenXRInterface();
  242. };
  243. VARIANT_ENUM_CAST(OpenXRInterface::Hand)
  244. VARIANT_ENUM_CAST(OpenXRInterface::HandMotionRange)
  245. VARIANT_ENUM_CAST(OpenXRInterface::HandTrackedSource)
  246. VARIANT_ENUM_CAST(OpenXRInterface::HandJoints)
  247. VARIANT_BITFIELD_CAST(OpenXRInterface::HandJointFlags)
  248. #endif // OPENXR_INTERFACE_H