openxr_interface.h 14 KB

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