openxr_fb_passthrough_extension_wrapper.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*************************************************************************/
  2. /* openxr_fb_passthrough_extension_wrapper.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "openxr_fb_passthrough_extension_wrapper.h"
  31. #include "core/os/os.h"
  32. #include "scene/main/viewport.h"
  33. #include "scene/main/window.h"
  34. using namespace godot;
  35. OpenXRFbPassthroughExtensionWrapper *OpenXRFbPassthroughExtensionWrapper::singleton = nullptr;
  36. OpenXRFbPassthroughExtensionWrapper *OpenXRFbPassthroughExtensionWrapper::get_singleton() {
  37. return singleton;
  38. }
  39. OpenXRFbPassthroughExtensionWrapper::OpenXRFbPassthroughExtensionWrapper(OpenXRAPI *p_openxr_api) :
  40. OpenXRExtensionWrapper(p_openxr_api) {
  41. request_extensions[XR_FB_PASSTHROUGH_EXTENSION_NAME] = &fb_passthrough_ext;
  42. request_extensions[XR_FB_TRIANGLE_MESH_EXTENSION_NAME] = &fb_triangle_mesh_ext;
  43. singleton = this;
  44. }
  45. OpenXRFbPassthroughExtensionWrapper::~OpenXRFbPassthroughExtensionWrapper() {
  46. cleanup();
  47. }
  48. void OpenXRFbPassthroughExtensionWrapper::cleanup() {
  49. fb_passthrough_ext = false;
  50. fb_triangle_mesh_ext = false;
  51. }
  52. Viewport *OpenXRFbPassthroughExtensionWrapper::get_main_viewport() {
  53. MainLoop *main_loop = OS::get_singleton()->get_main_loop();
  54. if (!main_loop) {
  55. print_error("Unable to retrieve main loop");
  56. return nullptr;
  57. }
  58. auto *scene_tree = Object::cast_to<SceneTree>(main_loop);
  59. if (!scene_tree) {
  60. print_error("Unable to retrieve scene tree");
  61. return nullptr;
  62. }
  63. Viewport *viewport = scene_tree->get_root()->get_viewport();
  64. return viewport;
  65. }
  66. void OpenXRFbPassthroughExtensionWrapper::on_instance_created(const XrInstance instance) {
  67. if (fb_passthrough_ext) {
  68. bool result = initialize_fb_passthrough_extension(instance);
  69. if (!result) {
  70. print_error("Failed to initialize fb_passthrough extension");
  71. fb_passthrough_ext = false;
  72. }
  73. }
  74. if (fb_triangle_mesh_ext) {
  75. bool result = initialize_fb_triangle_mesh_extension(instance);
  76. if (!result) {
  77. print_error("Failed to initialize fb_triangle_mesh extension");
  78. fb_triangle_mesh_ext = false;
  79. }
  80. }
  81. if (fb_passthrough_ext) {
  82. openxr_api->register_composition_layer_provider(this);
  83. }
  84. }
  85. bool OpenXRFbPassthroughExtensionWrapper::is_passthrough_enabled() {
  86. return fb_passthrough_ext && passthrough_handle != XR_NULL_HANDLE && passthrough_layer != XR_NULL_HANDLE;
  87. }
  88. bool OpenXRFbPassthroughExtensionWrapper::is_composition_passthrough_layer_ready() {
  89. return fb_passthrough_ext && passthrough_handle != XR_NULL_HANDLE && composition_passthrough_layer.layerHandle != XR_NULL_HANDLE;
  90. }
  91. bool OpenXRFbPassthroughExtensionWrapper::start_passthrough() {
  92. if (passthrough_handle == XR_NULL_HANDLE) {
  93. return false;
  94. }
  95. if (is_passthrough_enabled()) {
  96. return true;
  97. }
  98. // Start the passthrough feature
  99. XrResult result = xrPassthroughStartFB(passthrough_handle);
  100. if (!is_valid_passthrough_result(result, "Failed to start passthrough")) {
  101. stop_passthrough();
  102. return false;
  103. }
  104. // Create the passthrough layer
  105. result = xrCreatePassthroughLayerFB(openxr_api->get_session(), &passthrough_layer_config, &passthrough_layer);
  106. if (!is_valid_passthrough_result(result, "Failed to create the passthrough layer")) {
  107. stop_passthrough();
  108. return false;
  109. }
  110. // Check if the the viewport has transparent background
  111. Viewport *viewport = get_main_viewport();
  112. if (viewport && !viewport->has_transparent_background()) {
  113. print_error("Main viewport doesn't have transparent background! Passthrough may not properly render.");
  114. }
  115. composition_passthrough_layer.layerHandle = passthrough_layer;
  116. return true;
  117. }
  118. void OpenXRFbPassthroughExtensionWrapper::on_session_created(const XrSession session) {
  119. if (fb_passthrough_ext) {
  120. // Create the passthrough feature and start it.
  121. XrResult result = xrCreatePassthroughFB(openxr_api->get_session(), &passthrough_create_info, &passthrough_handle);
  122. if (!openxr_api->xr_result(result, "Failed to create passthrough")) {
  123. passthrough_handle = XR_NULL_HANDLE;
  124. return;
  125. }
  126. }
  127. }
  128. XrCompositionLayerBaseHeader *OpenXRFbPassthroughExtensionWrapper::get_composition_layer() {
  129. if (is_composition_passthrough_layer_ready()) {
  130. return (XrCompositionLayerBaseHeader *)&composition_passthrough_layer;
  131. } else {
  132. return nullptr;
  133. }
  134. }
  135. void OpenXRFbPassthroughExtensionWrapper::stop_passthrough() {
  136. if (!fb_passthrough_ext) {
  137. return;
  138. }
  139. composition_passthrough_layer.layerHandle = XR_NULL_HANDLE;
  140. XrResult result;
  141. if (passthrough_layer != XR_NULL_HANDLE) {
  142. // Destroy the layer
  143. result = xrDestroyPassthroughLayerFB(passthrough_layer);
  144. openxr_api->xr_result(result, "Unable to destroy passthrough layer");
  145. passthrough_layer = XR_NULL_HANDLE;
  146. }
  147. if (passthrough_handle != XR_NULL_HANDLE) {
  148. result = xrPassthroughPauseFB(passthrough_handle);
  149. openxr_api->xr_result(result, "Unable to stop passthrough feature");
  150. }
  151. }
  152. void OpenXRFbPassthroughExtensionWrapper::on_session_destroyed() {
  153. if (fb_passthrough_ext) {
  154. stop_passthrough();
  155. XrResult result;
  156. if (passthrough_handle != XR_NULL_HANDLE) {
  157. result = xrDestroyPassthroughFB(passthrough_handle);
  158. openxr_api->xr_result(result, "Unable to destroy passthrough feature");
  159. passthrough_handle = XR_NULL_HANDLE;
  160. }
  161. }
  162. }
  163. void OpenXRFbPassthroughExtensionWrapper::on_instance_destroyed() {
  164. if (fb_passthrough_ext) {
  165. openxr_api->unregister_composition_layer_provider(this);
  166. }
  167. cleanup();
  168. }
  169. bool OpenXRFbPassthroughExtensionWrapper::initialize_fb_passthrough_extension(const XrInstance p_instance) {
  170. ERR_FAIL_NULL_V(openxr_api, false);
  171. EXT_INIT_XR_FUNC_V(xrCreatePassthroughFB);
  172. EXT_INIT_XR_FUNC_V(xrDestroyPassthroughFB);
  173. EXT_INIT_XR_FUNC_V(xrPassthroughStartFB);
  174. EXT_INIT_XR_FUNC_V(xrPassthroughPauseFB);
  175. EXT_INIT_XR_FUNC_V(xrCreatePassthroughLayerFB);
  176. EXT_INIT_XR_FUNC_V(xrDestroyPassthroughLayerFB);
  177. EXT_INIT_XR_FUNC_V(xrPassthroughLayerPauseFB);
  178. EXT_INIT_XR_FUNC_V(xrPassthroughLayerResumeFB);
  179. EXT_INIT_XR_FUNC_V(xrPassthroughLayerSetStyleFB);
  180. EXT_INIT_XR_FUNC_V(xrCreateGeometryInstanceFB);
  181. EXT_INIT_XR_FUNC_V(xrDestroyGeometryInstanceFB);
  182. EXT_INIT_XR_FUNC_V(xrGeometryInstanceSetTransformFB);
  183. return true;
  184. }
  185. bool OpenXRFbPassthroughExtensionWrapper::initialize_fb_triangle_mesh_extension(const XrInstance p_instance) {
  186. ERR_FAIL_NULL_V(openxr_api, false);
  187. EXT_INIT_XR_FUNC_V(xrCreateTriangleMeshFB);
  188. EXT_INIT_XR_FUNC_V(xrDestroyTriangleMeshFB);
  189. EXT_INIT_XR_FUNC_V(xrTriangleMeshGetVertexBufferFB);
  190. EXT_INIT_XR_FUNC_V(xrTriangleMeshGetIndexBufferFB);
  191. EXT_INIT_XR_FUNC_V(xrTriangleMeshBeginUpdateFB);
  192. EXT_INIT_XR_FUNC_V(xrTriangleMeshEndUpdateFB);
  193. EXT_INIT_XR_FUNC_V(xrTriangleMeshBeginVertexBufferUpdateFB);
  194. EXT_INIT_XR_FUNC_V(xrTriangleMeshEndVertexBufferUpdateFB);
  195. return true;
  196. }