register_types.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 "modules/modules_enabled.gen.h"
  32. #include "action_map/openxr_action.h"
  33. #include "action_map/openxr_action_map.h"
  34. #include "action_map/openxr_action_set.h"
  35. #include "action_map/openxr_haptic_feedback.h"
  36. #include "action_map/openxr_interaction_profile.h"
  37. #include "action_map/openxr_interaction_profile_metadata.h"
  38. #include "openxr_api_extension.h"
  39. #include "openxr_interface.h"
  40. #ifndef DISABLE_DEPRECATED
  41. #include "extensions/openxr_extension_wrapper_extension.h"
  42. #include "scene/openxr_hand.h"
  43. #endif // DISABLE_DEPRECATED
  44. #include "scene/openxr_composition_layer.h"
  45. #include "scene/openxr_composition_layer_cylinder.h"
  46. #include "scene/openxr_composition_layer_equirect.h"
  47. #include "scene/openxr_composition_layer_quad.h"
  48. #ifdef MODULE_GLTF_ENABLED
  49. #include "scene/openxr_render_model.h"
  50. #include "scene/openxr_render_model_manager.h"
  51. #endif
  52. #include "scene/openxr_visibility_mask.h"
  53. #include "extensions/openxr_android_thread_settings_extension.h"
  54. #include "extensions/openxr_composition_layer_depth_extension.h"
  55. #include "extensions/openxr_composition_layer_extension.h"
  56. #include "extensions/openxr_debug_utils_extension.h"
  57. #include "extensions/openxr_dpad_binding_extension.h"
  58. #include "extensions/openxr_eye_gaze_interaction.h"
  59. #include "extensions/openxr_fb_display_refresh_rate_extension.h"
  60. #include "extensions/openxr_frame_synthesis_extension.h"
  61. #include "extensions/openxr_future_extension.h"
  62. #include "extensions/openxr_hand_interaction_extension.h"
  63. #include "extensions/openxr_hand_tracking_extension.h"
  64. #include "extensions/openxr_htc_controller_extension.h"
  65. #include "extensions/openxr_htc_vive_tracker_extension.h"
  66. #include "extensions/openxr_huawei_controller_extension.h"
  67. #include "extensions/openxr_local_floor_extension.h"
  68. #include "extensions/openxr_meta_controller_extension.h"
  69. #include "extensions/openxr_ml2_controller_extension.h"
  70. #include "extensions/openxr_mxink_extension.h"
  71. #include "extensions/openxr_palm_pose_extension.h"
  72. #include "extensions/openxr_performance_settings_extension.h"
  73. #include "extensions/openxr_pico_controller_extension.h"
  74. #ifdef MODULE_GLTF_ENABLED
  75. #include "extensions/openxr_render_model_extension.h"
  76. #endif
  77. #include "extensions/openxr_valve_analog_threshold_extension.h"
  78. #include "extensions/openxr_valve_controller_extension.h"
  79. #include "extensions/openxr_visibility_mask_extension.h"
  80. #include "extensions/openxr_wmr_controller_extension.h"
  81. #include "extensions/spatial_entities/openxr_spatial_entity_extension.h"
  82. #include "extensions/spatial_entities/openxr_spatial_anchor.h"
  83. #include "extensions/spatial_entities/openxr_spatial_marker_tracking.h"
  84. #include "extensions/spatial_entities/openxr_spatial_plane_tracking.h"
  85. #ifdef TOOLS_ENABLED
  86. #include "editor/openxr_editor_plugin.h"
  87. #endif
  88. #ifdef ANDROID_ENABLED
  89. #include "extensions/platform/openxr_android_extension.h"
  90. #endif
  91. #include "core/config/project_settings.h"
  92. #include "main/main.h"
  93. #ifdef TOOLS_ENABLED
  94. #include "editor/editor_node.h"
  95. #include "editor/openxr_binding_modifier_editor.h"
  96. #include "editor/openxr_interaction_profile_editor.h"
  97. #endif
  98. static OpenXRAPI *openxr_api = nullptr;
  99. static OpenXRInteractionProfileMetadata *openxr_interaction_profile_metadata = nullptr;
  100. static Ref<OpenXRInterface> openxr_interface;
  101. #ifdef TOOLS_ENABLED
  102. static void _editor_init() {
  103. if (OpenXRAPI::openxr_is_enabled(false)) {
  104. if (openxr_interaction_profile_metadata == nullptr) {
  105. // If we didn't initialize our actionmap metadata at startup, we initialize it now.
  106. openxr_interaction_profile_metadata = memnew(OpenXRInteractionProfileMetadata);
  107. ERR_FAIL_NULL(openxr_interaction_profile_metadata);
  108. }
  109. }
  110. OpenXREditorPlugin *openxr_plugin = memnew(OpenXREditorPlugin());
  111. EditorNode::get_singleton()->add_editor_plugin(openxr_plugin);
  112. }
  113. #endif
  114. void initialize_openxr_module(ModuleInitializationLevel p_level) {
  115. if (p_level == MODULE_INITIALIZATION_LEVEL_CORE) {
  116. GDREGISTER_VIRTUAL_CLASS(OpenXRExtensionWrapper);
  117. #ifndef DISABLE_DEPRECATED
  118. GDREGISTER_VIRTUAL_CLASS(OpenXRExtensionWrapperExtension);
  119. #endif // DISABLE_DEPRECATED
  120. GDREGISTER_ABSTRACT_CLASS(OpenXRFutureResult); // Declared abstract, should never be instantiated by a user (Q or should this be internal?)
  121. GDREGISTER_CLASS(OpenXRFrameSynthesisExtension);
  122. GDREGISTER_CLASS(OpenXRFutureExtension);
  123. GDREGISTER_CLASS(OpenXRAPIExtension);
  124. #ifdef MODULE_GLTF_ENABLED
  125. GDREGISTER_CLASS(OpenXRRenderModelExtension);
  126. #endif
  127. GDREGISTER_CLASS(OpenXRAndroidThreadSettingsExtension);
  128. // Note, we're not registering all wrapper classes here, there is no point in exposing them
  129. // if there isn't specific logic to expose.
  130. }
  131. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  132. if (OpenXRAPI::openxr_is_enabled(false)) {
  133. // Always register our extension wrappers even if we don't initialize OpenXR.
  134. // Some of these wrappers will add functionality to our editor.
  135. #ifdef ANDROID_ENABLED
  136. OpenXRAPI::register_extension_wrapper(memnew(OpenXRAndroidExtension));
  137. #endif
  138. // register our other extensions
  139. OpenXRAPI::register_extension_wrapper(memnew(OpenXRPalmPoseExtension));
  140. OpenXRAPI::register_extension_wrapper(memnew(OpenXRLocalFloorExtension));
  141. OpenXRAPI::register_extension_wrapper(memnew(OpenXRPicoControllerExtension));
  142. OpenXRAPI::register_extension_wrapper(memnew(OpenXRCompositionLayerDepthExtension));
  143. OpenXRAPI::register_extension_wrapper(memnew(OpenXRCompositionLayerExtension));
  144. OpenXRAPI::register_extension_wrapper(memnew(OpenXRHTCControllerExtension));
  145. OpenXRAPI::register_extension_wrapper(memnew(OpenXRHTCViveTrackerExtension));
  146. OpenXRAPI::register_extension_wrapper(memnew(OpenXRHuaweiControllerExtension));
  147. OpenXRAPI::register_extension_wrapper(memnew(OpenXRDisplayRefreshRateExtension));
  148. OpenXRAPI::register_extension_wrapper(memnew(OpenXRWMRControllerExtension));
  149. OpenXRAPI::register_extension_wrapper(memnew(OpenXRML2ControllerExtension));
  150. OpenXRAPI::register_extension_wrapper(memnew(OpenXRMetaControllerExtension));
  151. OpenXRAPI::register_extension_wrapper(memnew(OpenXREyeGazeInteractionExtension));
  152. OpenXRAPI::register_extension_wrapper(memnew(OpenXRHandInteractionExtension));
  153. OpenXRAPI::register_extension_wrapper(memnew(OpenXRMxInkExtension));
  154. OpenXRAPI::register_extension_wrapper(memnew(OpenXRVisibilityMaskExtension));
  155. OpenXRAPI::register_extension_wrapper(memnew(OpenXRPerformanceSettingsExtension));
  156. OpenXRAPI::register_extension_wrapper(memnew(OpenXRValveControllerExtension));
  157. // Futures extension has to be registered as a singleton so extensions can access it.
  158. OpenXRFutureExtension *future_extension = memnew(OpenXRFutureExtension);
  159. OpenXRAPI::register_extension_wrapper(future_extension);
  160. Engine::get_singleton()->add_singleton(Engine::Singleton("OpenXRFutureExtension", future_extension));
  161. // Register render model extension as a singleton.
  162. #ifdef MODULE_GLTF_ENABLED
  163. OpenXRRenderModelExtension *render_model_extension = memnew(OpenXRRenderModelExtension);
  164. OpenXRAPI::register_extension_wrapper(render_model_extension);
  165. Engine::get_singleton()->add_singleton(Engine::Singleton("OpenXRRenderModelExtension", render_model_extension));
  166. #endif
  167. // Register spatial entity extensions
  168. OpenXRSpatialEntityExtension *spatial_entity_extension = memnew(OpenXRSpatialEntityExtension);
  169. OpenXRAPI::register_extension_wrapper(spatial_entity_extension);
  170. Engine::get_singleton()->add_singleton(Engine::Singleton("OpenXRSpatialEntityExtension", spatial_entity_extension));
  171. OpenXRSpatialAnchorCapability *anchor_capability = memnew(OpenXRSpatialAnchorCapability);
  172. OpenXRAPI::register_extension_wrapper(anchor_capability);
  173. Engine::get_singleton()->add_singleton(Engine::Singleton("OpenXRSpatialAnchorCapability", anchor_capability));
  174. OpenXRSpatialPlaneTrackingCapability *plane_tracking_capability = memnew(OpenXRSpatialPlaneTrackingCapability);
  175. OpenXRAPI::register_extension_wrapper(plane_tracking_capability);
  176. Engine::get_singleton()->add_singleton(Engine::Singleton("OpenXRSpatialPlaneTrackingCapability", plane_tracking_capability));
  177. OpenXRSpatialMarkerTrackingCapability *marker_tracking_capability = memnew(OpenXRSpatialMarkerTrackingCapability);
  178. OpenXRAPI::register_extension_wrapper(marker_tracking_capability);
  179. Engine::get_singleton()->add_singleton(Engine::Singleton("OpenXRSpatialMarkerTrackingCapability", marker_tracking_capability));
  180. // Register frame synthesis extension as a singleton.
  181. OpenXRFrameSynthesisExtension *frame_synthesis_extension = memnew(OpenXRFrameSynthesisExtension);
  182. OpenXRAPI::register_extension_wrapper(frame_synthesis_extension);
  183. Engine::get_singleton()->add_singleton(Engine::Singleton("OpenXRFrameSynthesisExtension", frame_synthesis_extension));
  184. // Register android thread settings extension as a singleton.
  185. OpenXRAndroidThreadSettingsExtension *android_thread_settings = memnew(OpenXRAndroidThreadSettingsExtension);
  186. OpenXRAPI::register_extension_wrapper(android_thread_settings);
  187. Engine::get_singleton()->add_singleton(Engine::Singleton("OpenXRAndroidThreadSettingsExtension", android_thread_settings));
  188. // register gated extensions
  189. if (int(GLOBAL_GET("xr/openxr/extensions/debug_utils")) > 0) {
  190. OpenXRAPI::register_extension_wrapper(memnew(OpenXRDebugUtilsExtension));
  191. }
  192. if (GLOBAL_GET("xr/openxr/extensions/hand_tracking")) {
  193. OpenXRAPI::register_extension_wrapper(memnew(OpenXRHandTrackingExtension));
  194. }
  195. // register gated binding modifiers
  196. if (GLOBAL_GET("xr/openxr/binding_modifiers/analog_threshold")) {
  197. OpenXRAPI::register_extension_wrapper(memnew(OpenXRValveAnalogThresholdExtension));
  198. }
  199. if (GLOBAL_GET("xr/openxr/binding_modifiers/dpad_binding")) {
  200. OpenXRAPI::register_extension_wrapper(memnew(OpenXRDPadBindingExtension));
  201. }
  202. }
  203. if (OpenXRAPI::openxr_is_enabled()) {
  204. openxr_interaction_profile_metadata = memnew(OpenXRInteractionProfileMetadata);
  205. ERR_FAIL_NULL(openxr_interaction_profile_metadata);
  206. openxr_api = memnew(OpenXRAPI);
  207. ERR_FAIL_NULL(openxr_api);
  208. if (!openxr_api->initialize(Main::get_rendering_driver_name())) {
  209. const char *init_error_message =
  210. "OpenXR was requested but failed to start.\n"
  211. "Please check if your HMD is connected.\n"
  212. #ifdef WINDOWS_ENABLED
  213. "When using Windows Mixed Reality, note that WMR only has DirectX support. Make sure SteamVR is your default OpenXR runtime.\n"
  214. #endif
  215. "Godot will start in normal mode.\n";
  216. WARN_PRINT(init_error_message);
  217. bool init_show_startup_alert = GLOBAL_GET("xr/openxr/startup_alert");
  218. if (init_show_startup_alert) {
  219. OS::get_singleton()->alert(init_error_message);
  220. }
  221. memdelete(openxr_api);
  222. openxr_api = nullptr;
  223. return;
  224. }
  225. }
  226. }
  227. if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) {
  228. GDREGISTER_CLASS(OpenXRInterface);
  229. GDREGISTER_CLASS(OpenXRAction);
  230. GDREGISTER_CLASS(OpenXRActionSet);
  231. GDREGISTER_CLASS(OpenXRActionMap);
  232. GDREGISTER_CLASS(OpenXRInteractionProfileMetadata);
  233. GDREGISTER_CLASS(OpenXRIPBinding);
  234. GDREGISTER_CLASS(OpenXRInteractionProfile);
  235. GDREGISTER_ABSTRACT_CLASS(OpenXRBindingModifier);
  236. GDREGISTER_VIRTUAL_CLASS(OpenXRIPBindingModifier);
  237. GDREGISTER_VIRTUAL_CLASS(OpenXRActionBindingModifier);
  238. GDREGISTER_CLASS(OpenXRAnalogThresholdModifier);
  239. GDREGISTER_CLASS(OpenXRDpadBindingModifier);
  240. GDREGISTER_ABSTRACT_CLASS(OpenXRHapticBase);
  241. GDREGISTER_CLASS(OpenXRHapticVibration);
  242. GDREGISTER_ABSTRACT_CLASS(OpenXRCompositionLayer);
  243. GDREGISTER_CLASS(OpenXRCompositionLayerEquirect);
  244. GDREGISTER_CLASS(OpenXRCompositionLayerCylinder);
  245. GDREGISTER_CLASS(OpenXRCompositionLayerQuad);
  246. #ifndef DISABLE_DEPRECATED
  247. GDREGISTER_CLASS(OpenXRHand);
  248. #endif
  249. GDREGISTER_CLASS(OpenXRVisibilityMask);
  250. #ifdef MODULE_GLTF_ENABLED
  251. GDREGISTER_CLASS(OpenXRRenderModel);
  252. GDREGISTER_CLASS(OpenXRRenderModelManager);
  253. #endif
  254. GDREGISTER_CLASS(OpenXRSpatialEntityExtension);
  255. GDREGISTER_VIRTUAL_CLASS(OpenXRSpatialEntityTracker);
  256. GDREGISTER_CLASS(OpenXRAnchorTracker);
  257. GDREGISTER_CLASS(OpenXRPlaneTracker);
  258. GDREGISTER_CLASS(OpenXRMarkerTracker);
  259. GDREGISTER_VIRTUAL_CLASS(OpenXRStructureBase);
  260. GDREGISTER_VIRTUAL_CLASS(OpenXRSpatialCapabilityConfigurationBaseHeader);
  261. GDREGISTER_CLASS(OpenXRSpatialCapabilityConfigurationAnchor);
  262. GDREGISTER_CLASS(OpenXRSpatialCapabilityConfigurationQrCode);
  263. GDREGISTER_CLASS(OpenXRSpatialCapabilityConfigurationMicroQrCode);
  264. GDREGISTER_CLASS(OpenXRSpatialCapabilityConfigurationAruco);
  265. GDREGISTER_CLASS(OpenXRSpatialCapabilityConfigurationAprilTag);
  266. GDREGISTER_CLASS(OpenXRSpatialContextPersistenceConfig);
  267. GDREGISTER_CLASS(OpenXRSpatialCapabilityConfigurationPlaneTracking);
  268. GDREGISTER_VIRTUAL_CLASS(OpenXRSpatialComponentData);
  269. GDREGISTER_CLASS(OpenXRSpatialComponentBounded2DList);
  270. GDREGISTER_CLASS(OpenXRSpatialComponentBounded3DList);
  271. GDREGISTER_CLASS(OpenXRSpatialComponentParentList);
  272. GDREGISTER_CLASS(OpenXRSpatialComponentMesh2DList);
  273. GDREGISTER_CLASS(OpenXRSpatialComponentMesh3DList);
  274. GDREGISTER_CLASS(OpenXRSpatialComponentPlaneAlignmentList);
  275. GDREGISTER_CLASS(OpenXRSpatialComponentPolygon2DList);
  276. GDREGISTER_CLASS(OpenXRSpatialComponentPlaneSemanticLabelList);
  277. GDREGISTER_CLASS(OpenXRSpatialComponentMarkerList);
  278. GDREGISTER_CLASS(OpenXRSpatialQueryResultData);
  279. GDREGISTER_CLASS(OpenXRSpatialComponentAnchorList);
  280. GDREGISTER_CLASS(OpenXRSpatialComponentPersistenceList);
  281. GDREGISTER_CLASS(OpenXRSpatialAnchorCapability);
  282. GDREGISTER_CLASS(OpenXRSpatialPlaneTrackingCapability);
  283. GDREGISTER_CLASS(OpenXRSpatialMarkerTrackingCapability);
  284. XRServer *xr_server = XRServer::get_singleton();
  285. if (xr_server) {
  286. openxr_interface.instantiate();
  287. xr_server->add_interface(openxr_interface);
  288. if (openxr_interface->initialize_on_startup()) {
  289. openxr_interface->initialize();
  290. }
  291. }
  292. }
  293. #ifdef TOOLS_ENABLED
  294. if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
  295. GDREGISTER_ABSTRACT_CLASS(OpenXRInteractionProfileEditorBase);
  296. GDREGISTER_CLASS(OpenXRInteractionProfileEditor);
  297. GDREGISTER_CLASS(OpenXRBindingModifierEditor);
  298. EditorNode::add_init_callback(_editor_init);
  299. }
  300. #endif
  301. }
  302. void uninitialize_openxr_module(ModuleInitializationLevel p_level) {
  303. if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
  304. return;
  305. }
  306. if (openxr_interface.is_valid()) {
  307. // uninitialize just in case
  308. if (openxr_interface->is_initialized()) {
  309. openxr_interface->uninitialize();
  310. }
  311. // unregister our interface from the XR server
  312. XRServer *xr_server = XRServer::get_singleton();
  313. if (xr_server) {
  314. if (xr_server->get_primary_interface() == openxr_interface) {
  315. xr_server->set_primary_interface(Ref<XRInterface>());
  316. }
  317. xr_server->remove_interface(openxr_interface);
  318. }
  319. // and release
  320. openxr_interface.unref();
  321. }
  322. if (openxr_api) {
  323. openxr_api->finish();
  324. memdelete(openxr_api);
  325. openxr_api = nullptr;
  326. }
  327. if (openxr_interaction_profile_metadata) {
  328. memdelete(openxr_interaction_profile_metadata);
  329. openxr_interaction_profile_metadata = nullptr;
  330. }
  331. // cleanup our extension wrappers
  332. OpenXRAPI::cleanup_extension_wrappers();
  333. }