openxr_api_extension.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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("set_object_name", "object_type", "object_handle", "object_name"), &OpenXRAPIExtension::set_object_name);
  43. ClassDB::bind_method(D_METHOD("begin_debug_label_region", "label_name"), &OpenXRAPIExtension::begin_debug_label_region);
  44. ClassDB::bind_method(D_METHOD("end_debug_label_region"), &OpenXRAPIExtension::end_debug_label_region);
  45. ClassDB::bind_method(D_METHOD("insert_debug_label", "label_name"), &OpenXRAPIExtension::insert_debug_label);
  46. ClassDB::bind_method(D_METHOD("is_initialized"), &OpenXRAPIExtension::is_initialized);
  47. ClassDB::bind_method(D_METHOD("is_running"), &OpenXRAPIExtension::is_running);
  48. ClassDB::bind_method(D_METHOD("get_play_space"), &OpenXRAPIExtension::get_play_space);
  49. ClassDB::bind_method(D_METHOD("get_predicted_display_time"), &OpenXRAPIExtension::get_predicted_display_time);
  50. ClassDB::bind_method(D_METHOD("get_next_frame_time"), &OpenXRAPIExtension::get_next_frame_time);
  51. ClassDB::bind_method(D_METHOD("can_render"), &OpenXRAPIExtension::can_render);
  52. ClassDB::bind_method(D_METHOD("get_hand_tracker", "hand_index"), &OpenXRAPIExtension::get_hand_tracker);
  53. ClassDB::bind_method(D_METHOD("register_composition_layer_provider", "extension"), &OpenXRAPIExtension::register_composition_layer_provider);
  54. ClassDB::bind_method(D_METHOD("unregister_composition_layer_provider", "extension"), &OpenXRAPIExtension::unregister_composition_layer_provider);
  55. ClassDB::bind_method(D_METHOD("set_emulate_environment_blend_mode_alpha_blend", "enabled"), &OpenXRAPIExtension::set_emulate_environment_blend_mode_alpha_blend);
  56. ClassDB::bind_method(D_METHOD("is_environment_blend_mode_alpha_supported"), &OpenXRAPIExtension::is_environment_blend_mode_alpha_blend_supported);
  57. BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE);
  58. BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_REAL);
  59. BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_EMULATING);
  60. }
  61. uint64_t OpenXRAPIExtension::get_instance() {
  62. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  63. return (uint64_t)OpenXRAPI::get_singleton()->get_instance();
  64. }
  65. uint64_t OpenXRAPIExtension::get_system_id() {
  66. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  67. return (uint64_t)OpenXRAPI::get_singleton()->get_system_id();
  68. }
  69. uint64_t OpenXRAPIExtension::get_session() {
  70. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  71. return (uint64_t)OpenXRAPI::get_singleton()->get_session();
  72. }
  73. Transform3D OpenXRAPIExtension::transform_from_pose(GDExtensionConstPtr<const void> p_pose) {
  74. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), Transform3D());
  75. return OpenXRAPI::get_singleton()->transform_from_pose(*(XrPosef *)p_pose.data);
  76. }
  77. bool OpenXRAPIExtension::xr_result(uint64_t result, String format, Array args) {
  78. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
  79. return OpenXRAPI::get_singleton()->xr_result((XrResult)result, format.utf8().get_data(), args);
  80. }
  81. bool OpenXRAPIExtension::openxr_is_enabled(bool p_check_run_in_editor) {
  82. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
  83. return OpenXRAPI::openxr_is_enabled(p_check_run_in_editor);
  84. }
  85. uint64_t OpenXRAPIExtension::get_instance_proc_addr(String p_name) {
  86. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  87. CharString str = p_name.utf8();
  88. PFN_xrVoidFunction addr = nullptr;
  89. XrResult result = OpenXRAPI::get_singleton()->get_instance_proc_addr(str.get_data(), &addr);
  90. if (result != XR_SUCCESS) {
  91. return 0;
  92. }
  93. return reinterpret_cast<uint64_t>(addr);
  94. }
  95. String OpenXRAPIExtension::get_error_string(uint64_t result) {
  96. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), String());
  97. return OpenXRAPI::get_singleton()->get_error_string((XrResult)result);
  98. }
  99. String OpenXRAPIExtension::get_swapchain_format_name(int64_t p_swapchain_format) {
  100. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), String());
  101. return OpenXRAPI::get_singleton()->get_swapchain_format_name(p_swapchain_format);
  102. }
  103. void OpenXRAPIExtension::set_object_name(int64_t p_object_type, uint64_t p_object_handle, const String &p_object_name) {
  104. ERR_FAIL_NULL(OpenXRAPI::get_singleton());
  105. OpenXRAPI::get_singleton()->set_object_name(XrObjectType(p_object_type), p_object_handle, p_object_name);
  106. }
  107. void OpenXRAPIExtension::begin_debug_label_region(const String &p_label_name) {
  108. ERR_FAIL_NULL(OpenXRAPI::get_singleton());
  109. OpenXRAPI::get_singleton()->begin_debug_label_region(p_label_name);
  110. }
  111. void OpenXRAPIExtension::end_debug_label_region() {
  112. ERR_FAIL_NULL(OpenXRAPI::get_singleton());
  113. OpenXRAPI::get_singleton()->end_debug_label_region();
  114. }
  115. void OpenXRAPIExtension::insert_debug_label(const String &p_label_name) {
  116. ERR_FAIL_NULL(OpenXRAPI::get_singleton());
  117. OpenXRAPI::get_singleton()->insert_debug_label(p_label_name);
  118. }
  119. bool OpenXRAPIExtension::is_initialized() {
  120. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
  121. return OpenXRAPI::get_singleton()->is_initialized();
  122. }
  123. bool OpenXRAPIExtension::is_running() {
  124. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
  125. return OpenXRAPI::get_singleton()->is_running();
  126. }
  127. uint64_t OpenXRAPIExtension::get_play_space() {
  128. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  129. return (uint64_t)OpenXRAPI::get_singleton()->get_play_space();
  130. }
  131. int64_t OpenXRAPIExtension::get_predicted_display_time() {
  132. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  133. return (XrTime)OpenXRAPI::get_singleton()->get_predicted_display_time();
  134. }
  135. int64_t OpenXRAPIExtension::get_next_frame_time() {
  136. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  137. // In the past we needed to look a frame ahead, may be calling this unintentionally so lets warn the dev.
  138. WARN_PRINT_ONCE("OpenXR: Next frame timing called, verify this is intended.");
  139. return (XrTime)OpenXRAPI::get_singleton()->get_next_frame_time();
  140. }
  141. bool OpenXRAPIExtension::can_render() {
  142. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
  143. return OpenXRAPI::get_singleton()->can_render();
  144. }
  145. uint64_t OpenXRAPIExtension::get_hand_tracker(int p_hand_index) {
  146. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  147. return (uint64_t)OpenXRAPI::get_singleton()->get_hand_tracker(p_hand_index);
  148. }
  149. void OpenXRAPIExtension::register_composition_layer_provider(OpenXRExtensionWrapperExtension *p_extension) {
  150. ERR_FAIL_NULL(OpenXRAPI::get_singleton());
  151. OpenXRAPI::get_singleton()->register_composition_layer_provider(p_extension);
  152. }
  153. void OpenXRAPIExtension::unregister_composition_layer_provider(OpenXRExtensionWrapperExtension *p_extension) {
  154. ERR_FAIL_NULL(OpenXRAPI::get_singleton());
  155. OpenXRAPI::get_singleton()->unregister_composition_layer_provider(p_extension);
  156. }
  157. void OpenXRAPIExtension::set_emulate_environment_blend_mode_alpha_blend(bool p_enabled) {
  158. ERR_FAIL_NULL(OpenXRAPI::get_singleton());
  159. OpenXRAPI::get_singleton()->set_emulate_environment_blend_mode_alpha_blend(p_enabled);
  160. }
  161. OpenXRAPIExtension::OpenXRAlphaBlendModeSupport OpenXRAPIExtension::is_environment_blend_mode_alpha_blend_supported() {
  162. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE);
  163. return (OpenXRAPIExtension::OpenXRAlphaBlendModeSupport)OpenXRAPI::get_singleton()->is_environment_blend_mode_alpha_blend_supported();
  164. }
  165. OpenXRAPIExtension::OpenXRAPIExtension() {
  166. }