register_types.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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_hand_tracking_extension.h"
  43. #include "extensions/openxr_htc_controller_extension.h"
  44. #include "extensions/openxr_htc_vive_tracker_extension.h"
  45. #include "extensions/openxr_huawei_controller_extension.h"
  46. #include "extensions/openxr_local_floor_extension.h"
  47. #include "extensions/openxr_meta_controller_extension.h"
  48. #include "extensions/openxr_ml2_controller_extension.h"
  49. #include "extensions/openxr_palm_pose_extension.h"
  50. #include "extensions/openxr_pico_controller_extension.h"
  51. #include "extensions/openxr_wmr_controller_extension.h"
  52. #ifdef TOOLS_ENABLED
  53. #include "editor/openxr_editor_plugin.h"
  54. #endif
  55. #ifdef ANDROID_ENABLED
  56. #include "extensions/platform/openxr_android_extension.h"
  57. #endif
  58. #include "core/config/project_settings.h"
  59. #include "main/main.h"
  60. #ifdef TOOLS_ENABLED
  61. #include "editor/editor_node.h"
  62. #endif
  63. static OpenXRAPI *openxr_api = nullptr;
  64. static OpenXRInteractionProfileMetadata *openxr_interaction_profile_metadata = nullptr;
  65. static Ref<OpenXRInterface> openxr_interface;
  66. #ifdef TOOLS_ENABLED
  67. static void _editor_init() {
  68. if (OpenXRAPI::openxr_is_enabled(false)) {
  69. // Only add our OpenXR action map editor if OpenXR is enabled for our project
  70. if (openxr_interaction_profile_metadata == nullptr) {
  71. // If we didn't initialize our actionmap metadata at startup, we initialize it now.
  72. openxr_interaction_profile_metadata = memnew(OpenXRInteractionProfileMetadata);
  73. ERR_FAIL_NULL(openxr_interaction_profile_metadata);
  74. }
  75. OpenXREditorPlugin *openxr_plugin = memnew(OpenXREditorPlugin());
  76. EditorNode::get_singleton()->add_editor_plugin(openxr_plugin);
  77. }
  78. }
  79. #endif
  80. void initialize_openxr_module(ModuleInitializationLevel p_level) {
  81. if (p_level == MODULE_INITIALIZATION_LEVEL_CORE) {
  82. GDREGISTER_CLASS(OpenXRExtensionWrapperExtension);
  83. GDREGISTER_CLASS(OpenXRAPIExtension);
  84. }
  85. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  86. if (OpenXRAPI::openxr_is_enabled(false)) {
  87. // Always register our extension wrappers even if we don't initialize OpenXR.
  88. // Some of these wrappers will add functionality to our editor.
  89. #ifdef ANDROID_ENABLED
  90. OpenXRAPI::register_extension_wrapper(memnew(OpenXRAndroidExtension));
  91. #endif
  92. // register our other extensions
  93. OpenXRAPI::register_extension_wrapper(memnew(OpenXRPalmPoseExtension));
  94. OpenXRAPI::register_extension_wrapper(memnew(OpenXRLocalFloorExtension));
  95. OpenXRAPI::register_extension_wrapper(memnew(OpenXRPicoControllerExtension));
  96. OpenXRAPI::register_extension_wrapper(memnew(OpenXRCompositionLayerDepthExtension));
  97. OpenXRAPI::register_extension_wrapper(memnew(OpenXRHTCControllerExtension));
  98. OpenXRAPI::register_extension_wrapper(memnew(OpenXRHTCViveTrackerExtension));
  99. OpenXRAPI::register_extension_wrapper(memnew(OpenXRHuaweiControllerExtension));
  100. OpenXRAPI::register_extension_wrapper(memnew(OpenXRDisplayRefreshRateExtension));
  101. OpenXRAPI::register_extension_wrapper(memnew(OpenXRWMRControllerExtension));
  102. OpenXRAPI::register_extension_wrapper(memnew(OpenXRML2ControllerExtension));
  103. OpenXRAPI::register_extension_wrapper(memnew(OpenXRMetaControllerExtension));
  104. OpenXRAPI::register_extension_wrapper(memnew(OpenXREyeGazeInteractionExtension));
  105. // register gated extensions
  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. #ifdef WINDOWS_ENABLED
  120. "When using Windows Mixed Reality, note that WMR only has DirectX support. Make sure SteamVR is your default OpenXR runtime.\n"
  121. #endif
  122. "Godot will start in normal mode.\n";
  123. WARN_PRINT(init_error_message);
  124. bool init_show_startup_alert = GLOBAL_GET("xr/openxr/startup_alert");
  125. if (init_show_startup_alert) {
  126. OS::get_singleton()->alert(init_error_message);
  127. }
  128. memdelete(openxr_api);
  129. openxr_api = nullptr;
  130. return;
  131. }
  132. }
  133. }
  134. if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) {
  135. GDREGISTER_CLASS(OpenXRInterface);
  136. GDREGISTER_CLASS(OpenXRAction);
  137. GDREGISTER_CLASS(OpenXRActionSet);
  138. GDREGISTER_CLASS(OpenXRActionMap);
  139. GDREGISTER_CLASS(OpenXRInteractionProfileMetadata);
  140. GDREGISTER_CLASS(OpenXRIPBinding);
  141. GDREGISTER_CLASS(OpenXRInteractionProfile);
  142. GDREGISTER_CLASS(OpenXRHand);
  143. XRServer *xr_server = XRServer::get_singleton();
  144. if (xr_server) {
  145. openxr_interface.instantiate();
  146. xr_server->add_interface(openxr_interface);
  147. if (openxr_interface->initialize_on_startup()) {
  148. openxr_interface->initialize();
  149. }
  150. }
  151. #ifdef TOOLS_ENABLED
  152. EditorNode::add_init_callback(_editor_init);
  153. #endif
  154. }
  155. }
  156. void uninitialize_openxr_module(ModuleInitializationLevel p_level) {
  157. if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
  158. return;
  159. }
  160. if (openxr_interface.is_valid()) {
  161. // uninitialize just in case
  162. if (openxr_interface->is_initialized()) {
  163. openxr_interface->uninitialize();
  164. }
  165. // unregister our interface from the XR server
  166. XRServer *xr_server = XRServer::get_singleton();
  167. if (xr_server) {
  168. if (xr_server->get_primary_interface() == openxr_interface) {
  169. xr_server->set_primary_interface(Ref<XRInterface>());
  170. }
  171. xr_server->remove_interface(openxr_interface);
  172. }
  173. // and release
  174. openxr_interface.unref();
  175. }
  176. if (openxr_api) {
  177. openxr_api->finish();
  178. memdelete(openxr_api);
  179. openxr_api = nullptr;
  180. }
  181. if (openxr_interaction_profile_metadata) {
  182. memdelete(openxr_interaction_profile_metadata);
  183. openxr_interaction_profile_metadata = nullptr;
  184. }
  185. // cleanup our extension wrappers
  186. OpenXRAPI::cleanup_extension_wrappers();
  187. }