register_types.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**************************************************************************/
  2. /* register_types.cpp */
  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. #include "register_types.h"
  31. #include "action_map/openxr_action.h"
  32. #include "action_map/openxr_action_map.h"
  33. #include "action_map/openxr_action_set.h"
  34. #include "action_map/openxr_interaction_profile.h"
  35. #include "action_map/openxr_interaction_profile_metadata.h"
  36. #include "openxr_interface.h"
  37. #include "extensions/openxr_extension_wrapper_extension.h"
  38. #include "scene/openxr_hand.h"
  39. #include "extensions/openxr_composition_layer_depth_extension.h"
  40. #include "extensions/openxr_eye_gaze_interaction.h"
  41. #include "extensions/openxr_fb_display_refresh_rate_extension.h"
  42. #include "extensions/openxr_fb_passthrough_extension_wrapper.h"
  43. #include "extensions/openxr_hand_tracking_extension.h"
  44. #include "extensions/openxr_htc_controller_extension.h"
  45. #include "extensions/openxr_htc_vive_tracker_extension.h"
  46. #include "extensions/openxr_huawei_controller_extension.h"
  47. #include "extensions/openxr_ml2_controller_extension.h"
  48. #include "extensions/openxr_palm_pose_extension.h"
  49. #include "extensions/openxr_pico_controller_extension.h"
  50. #include "extensions/openxr_wmr_controller_extension.h"
  51. #ifdef TOOLS_ENABLED
  52. #include "editor/openxr_editor_plugin.h"
  53. #endif
  54. #ifdef ANDROID_ENABLED
  55. #include "extensions/openxr_android_extension.h"
  56. #endif
  57. #include "core/config/project_settings.h"
  58. #include "main/main.h"
  59. #ifdef TOOLS_ENABLED
  60. #include "editor/editor_node.h"
  61. #endif
  62. static OpenXRAPI *openxr_api = nullptr;
  63. static OpenXRInteractionProfileMetadata *openxr_interaction_profile_metadata = nullptr;
  64. static Ref<OpenXRInterface> openxr_interface;
  65. #ifdef TOOLS_ENABLED
  66. static void _editor_init() {
  67. if (OpenXRAPI::openxr_is_enabled(false)) {
  68. // Only add our OpenXR action map editor if OpenXR is enabled for our project
  69. if (openxr_interaction_profile_metadata == nullptr) {
  70. // If we didn't initialize our actionmap metadata at startup, we initialize it now.
  71. openxr_interaction_profile_metadata = memnew(OpenXRInteractionProfileMetadata);
  72. ERR_FAIL_NULL(openxr_interaction_profile_metadata);
  73. }
  74. OpenXREditorPlugin *openxr_plugin = memnew(OpenXREditorPlugin());
  75. EditorNode::get_singleton()->add_editor_plugin(openxr_plugin);
  76. }
  77. }
  78. #endif
  79. void initialize_openxr_module(ModuleInitializationLevel p_level) {
  80. if (p_level == MODULE_INITIALIZATION_LEVEL_CORE) {
  81. GDREGISTER_CLASS(OpenXRExtensionWrapperExtension);
  82. GDREGISTER_CLASS(OpenXRAPIExtension);
  83. }
  84. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  85. if (OpenXRAPI::openxr_is_enabled(false)) {
  86. // Always register our extension wrappers even if we don't initialize OpenXR.
  87. // Some of these wrappers will add functionality to our editor.
  88. #ifdef ANDROID_ENABLED
  89. OpenXRAPI::register_extension_wrapper(memnew(OpenXRAndroidExtension));
  90. #endif
  91. // register our other extensions
  92. OpenXRAPI::register_extension_wrapper(memnew(OpenXRPalmPoseExtension));
  93. OpenXRAPI::register_extension_wrapper(memnew(OpenXRPicoControllerExtension));
  94. OpenXRAPI::register_extension_wrapper(memnew(OpenXRCompositionLayerDepthExtension));
  95. OpenXRAPI::register_extension_wrapper(memnew(OpenXRHTCControllerExtension));
  96. OpenXRAPI::register_extension_wrapper(memnew(OpenXRHTCViveTrackerExtension));
  97. OpenXRAPI::register_extension_wrapper(memnew(OpenXRHuaweiControllerExtension));
  98. OpenXRAPI::register_extension_wrapper(memnew(OpenXRFbPassthroughExtensionWrapper));
  99. OpenXRAPI::register_extension_wrapper(memnew(OpenXRDisplayRefreshRateExtension));
  100. OpenXRAPI::register_extension_wrapper(memnew(OpenXRWMRControllerExtension));
  101. OpenXRAPI::register_extension_wrapper(memnew(OpenXRML2ControllerExtension));
  102. // register gated extensions
  103. if (GLOBAL_GET("xr/openxr/extensions/eye_gaze_interaction") && (!OS::get_singleton()->has_feature("mobile") || OS::get_singleton()->has_feature(XR_EXT_EYE_GAZE_INTERACTION_EXTENSION_NAME))) {
  104. OpenXRAPI::register_extension_wrapper(memnew(OpenXREyeGazeInteractionExtension));
  105. }
  106. if (GLOBAL_GET("xr/openxr/extensions/hand_tracking")) {
  107. OpenXRAPI::register_extension_wrapper(memnew(OpenXRHandTrackingExtension));
  108. }
  109. }
  110. if (OpenXRAPI::openxr_is_enabled()) {
  111. openxr_interaction_profile_metadata = memnew(OpenXRInteractionProfileMetadata);
  112. ERR_FAIL_NULL(openxr_interaction_profile_metadata);
  113. openxr_api = memnew(OpenXRAPI);
  114. ERR_FAIL_NULL(openxr_api);
  115. if (!openxr_api->initialize(Main::get_rendering_driver_name())) {
  116. const char *init_error_message =
  117. "OpenXR was requested but failed to start.\n"
  118. "Please check if your HMD is connected.\n"
  119. "When using Windows MR please note that WMR only has DirectX support, make sure SteamVR is your default OpenXR runtime.\n"
  120. "Godot will start in normal mode.\n";
  121. WARN_PRINT(init_error_message);
  122. bool init_show_startup_alert = GLOBAL_GET("xr/openxr/startup_alert");
  123. if (init_show_startup_alert) {
  124. OS::get_singleton()->alert(init_error_message);
  125. }
  126. memdelete(openxr_api);
  127. openxr_api = nullptr;
  128. return;
  129. }
  130. }
  131. }
  132. if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) {
  133. GDREGISTER_CLASS(OpenXRInterface);
  134. GDREGISTER_CLASS(OpenXRAction);
  135. GDREGISTER_CLASS(OpenXRActionSet);
  136. GDREGISTER_CLASS(OpenXRActionMap);
  137. GDREGISTER_CLASS(OpenXRInteractionProfileMetadata);
  138. GDREGISTER_CLASS(OpenXRIPBinding);
  139. GDREGISTER_CLASS(OpenXRInteractionProfile);
  140. GDREGISTER_CLASS(OpenXRHand);
  141. XRServer *xr_server = XRServer::get_singleton();
  142. if (xr_server) {
  143. openxr_interface.instantiate();
  144. xr_server->add_interface(openxr_interface);
  145. if (openxr_interface->initialize_on_startup()) {
  146. openxr_interface->initialize();
  147. }
  148. }
  149. #ifdef TOOLS_ENABLED
  150. EditorNode::add_init_callback(_editor_init);
  151. #endif
  152. }
  153. }
  154. void uninitialize_openxr_module(ModuleInitializationLevel p_level) {
  155. if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
  156. return;
  157. }
  158. if (openxr_interface.is_valid()) {
  159. // uninitialize just in case
  160. if (openxr_interface->is_initialized()) {
  161. openxr_interface->uninitialize();
  162. }
  163. // unregister our interface from the XR server
  164. XRServer *xr_server = XRServer::get_singleton();
  165. if (xr_server) {
  166. if (xr_server->get_primary_interface() == openxr_interface) {
  167. xr_server->set_primary_interface(Ref<XRInterface>());
  168. }
  169. xr_server->remove_interface(openxr_interface);
  170. }
  171. // and release
  172. openxr_interface.unref();
  173. }
  174. if (openxr_api) {
  175. openxr_api->finish();
  176. memdelete(openxr_api);
  177. openxr_api = nullptr;
  178. }
  179. if (openxr_interaction_profile_metadata) {
  180. memdelete(openxr_interaction_profile_metadata);
  181. openxr_interaction_profile_metadata = nullptr;
  182. }
  183. // cleanup our extension wrappers
  184. OpenXRAPI::cleanup_extension_wrappers();
  185. }