register_types.cpp 9.4 KB

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