openxr_api_extension.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**************************************************************************/
  2. /* openxr_api_extension.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 "openxr_api_extension.h"
  31. #include "extensions/openxr_extension_wrapper_extension.h"
  32. void OpenXRAPIExtension::_bind_methods() {
  33. ClassDB::bind_method(D_METHOD("get_instance"), &OpenXRAPIExtension::get_instance);
  34. ClassDB::bind_method(D_METHOD("get_system_id"), &OpenXRAPIExtension::get_system_id);
  35. ClassDB::bind_method(D_METHOD("get_session"), &OpenXRAPIExtension::get_session);
  36. ClassDB::bind_method(D_METHOD("transform_from_pose", "pose"), &OpenXRAPIExtension::transform_from_pose);
  37. ClassDB::bind_method(D_METHOD("xr_result", "result", "format", "args"), &OpenXRAPIExtension::xr_result);
  38. ClassDB::bind_static_method("OpenXRAPIExtension", D_METHOD("openxr_is_enabled", "check_run_in_editor"), &OpenXRAPIExtension::openxr_is_enabled);
  39. ClassDB::bind_method(D_METHOD("get_instance_proc_addr", "name"), &OpenXRAPIExtension::get_instance_proc_addr);
  40. ClassDB::bind_method(D_METHOD("get_error_string", "result"), &OpenXRAPIExtension::get_error_string);
  41. ClassDB::bind_method(D_METHOD("get_swapchain_format_name", "swapchain_format"), &OpenXRAPIExtension::get_swapchain_format_name);
  42. ClassDB::bind_method(D_METHOD("is_initialized"), &OpenXRAPIExtension::is_initialized);
  43. ClassDB::bind_method(D_METHOD("is_running"), &OpenXRAPIExtension::is_running);
  44. ClassDB::bind_method(D_METHOD("get_play_space"), &OpenXRAPIExtension::get_play_space);
  45. ClassDB::bind_method(D_METHOD("get_predicted_display_time"), &OpenXRAPIExtension::get_predicted_display_time);
  46. ClassDB::bind_method(D_METHOD("get_next_frame_time"), &OpenXRAPIExtension::get_next_frame_time);
  47. ClassDB::bind_method(D_METHOD("can_render"), &OpenXRAPIExtension::can_render);
  48. ClassDB::bind_method(D_METHOD("get_hand_tracker", "hand_index"), &OpenXRAPIExtension::get_hand_tracker);
  49. ClassDB::bind_method(D_METHOD("register_composition_layer_provider", "extension"), &OpenXRAPIExtension::register_composition_layer_provider);
  50. ClassDB::bind_method(D_METHOD("unregister_composition_layer_provider", "extension"), &OpenXRAPIExtension::unregister_composition_layer_provider);
  51. ClassDB::bind_method(D_METHOD("set_emulate_environment_blend_mode_alpha_blend", "enabled"), &OpenXRAPIExtension::set_emulate_environment_blend_mode_alpha_blend);
  52. ClassDB::bind_method(D_METHOD("is_environment_blend_mode_alpha_supported"), &OpenXRAPIExtension::is_environment_blend_mode_alpha_blend_supported);
  53. BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE);
  54. BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_REAL);
  55. BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_EMULATING);
  56. }
  57. uint64_t OpenXRAPIExtension::get_instance() {
  58. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  59. return (uint64_t)OpenXRAPI::get_singleton()->get_instance();
  60. }
  61. uint64_t OpenXRAPIExtension::get_system_id() {
  62. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  63. return (uint64_t)OpenXRAPI::get_singleton()->get_system_id();
  64. }
  65. uint64_t OpenXRAPIExtension::get_session() {
  66. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  67. return (uint64_t)OpenXRAPI::get_singleton()->get_session();
  68. }
  69. Transform3D OpenXRAPIExtension::transform_from_pose(GDExtensionConstPtr<const void> p_pose) {
  70. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), Transform3D());
  71. return OpenXRAPI::get_singleton()->transform_from_pose(*(XrPosef *)p_pose.data);
  72. }
  73. bool OpenXRAPIExtension::xr_result(uint64_t result, String format, Array args) {
  74. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
  75. return OpenXRAPI::get_singleton()->xr_result((XrResult)result, format.utf8().get_data(), args);
  76. }
  77. bool OpenXRAPIExtension::openxr_is_enabled(bool p_check_run_in_editor) {
  78. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
  79. return OpenXRAPI::openxr_is_enabled(p_check_run_in_editor);
  80. }
  81. uint64_t OpenXRAPIExtension::get_instance_proc_addr(String p_name) {
  82. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  83. CharString str = p_name.utf8();
  84. PFN_xrVoidFunction addr = nullptr;
  85. XrResult result = OpenXRAPI::get_singleton()->get_instance_proc_addr(str.get_data(), &addr);
  86. if (result != XR_SUCCESS) {
  87. return 0;
  88. }
  89. return reinterpret_cast<uint64_t>(addr);
  90. }
  91. String OpenXRAPIExtension::get_error_string(uint64_t result) {
  92. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), String());
  93. return OpenXRAPI::get_singleton()->get_error_string((XrResult)result);
  94. }
  95. String OpenXRAPIExtension::get_swapchain_format_name(int64_t p_swapchain_format) {
  96. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), String());
  97. return OpenXRAPI::get_singleton()->get_swapchain_format_name(p_swapchain_format);
  98. }
  99. bool OpenXRAPIExtension::is_initialized() {
  100. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
  101. return OpenXRAPI::get_singleton()->is_initialized();
  102. }
  103. bool OpenXRAPIExtension::is_running() {
  104. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
  105. return OpenXRAPI::get_singleton()->is_running();
  106. }
  107. uint64_t OpenXRAPIExtension::get_play_space() {
  108. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  109. return (uint64_t)OpenXRAPI::get_singleton()->get_play_space();
  110. }
  111. int64_t OpenXRAPIExtension::get_predicted_display_time() {
  112. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  113. return (XrTime)OpenXRAPI::get_singleton()->get_predicted_display_time();
  114. }
  115. int64_t OpenXRAPIExtension::get_next_frame_time() {
  116. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  117. // In the past we needed to look a frame ahead, may be calling this unintentionally so lets warn the dev.
  118. WARN_PRINT_ONCE("OpenXR: Next frame timing called, verify this is intended.");
  119. return (XrTime)OpenXRAPI::get_singleton()->get_next_frame_time();
  120. }
  121. bool OpenXRAPIExtension::can_render() {
  122. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
  123. return OpenXRAPI::get_singleton()->can_render();
  124. }
  125. uint64_t OpenXRAPIExtension::get_hand_tracker(int p_hand_index) {
  126. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  127. return (uint64_t)OpenXRAPI::get_singleton()->get_hand_tracker(p_hand_index);
  128. }
  129. void OpenXRAPIExtension::register_composition_layer_provider(OpenXRExtensionWrapperExtension *p_extension) {
  130. ERR_FAIL_NULL(OpenXRAPI::get_singleton());
  131. OpenXRAPI::get_singleton()->register_composition_layer_provider(p_extension);
  132. }
  133. void OpenXRAPIExtension::unregister_composition_layer_provider(OpenXRExtensionWrapperExtension *p_extension) {
  134. ERR_FAIL_NULL(OpenXRAPI::get_singleton());
  135. OpenXRAPI::get_singleton()->unregister_composition_layer_provider(p_extension);
  136. }
  137. void OpenXRAPIExtension::set_emulate_environment_blend_mode_alpha_blend(bool p_enabled) {
  138. ERR_FAIL_NULL(OpenXRAPI::get_singleton());
  139. OpenXRAPI::get_singleton()->set_emulate_environment_blend_mode_alpha_blend(p_enabled);
  140. }
  141. OpenXRAPIExtension::OpenXRAlphaBlendModeSupport OpenXRAPIExtension::is_environment_blend_mode_alpha_blend_supported() {
  142. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE);
  143. return (OpenXRAPIExtension::OpenXRAlphaBlendModeSupport)OpenXRAPI::get_singleton()->is_environment_blend_mode_alpha_blend_supported();
  144. }
  145. OpenXRAPIExtension::OpenXRAPIExtension() {
  146. }