openxr_api_extension.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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_next_frame_time"), &OpenXRAPIExtension::get_next_frame_time);
  46. ClassDB::bind_method(D_METHOD("can_render"), &OpenXRAPIExtension::can_render);
  47. ClassDB::bind_method(D_METHOD("get_hand_tracker", "hand_index"), &OpenXRAPIExtension::get_hand_tracker);
  48. ClassDB::bind_method(D_METHOD("register_composition_layer_provider", "extension"), &OpenXRAPIExtension::register_composition_layer_provider);
  49. ClassDB::bind_method(D_METHOD("unregister_composition_layer_provider", "extension"), &OpenXRAPIExtension::unregister_composition_layer_provider);
  50. ClassDB::bind_method(D_METHOD("set_emulate_environment_blend_mode_alpha_blend", "enabled"), &OpenXRAPIExtension::set_emulate_environment_blend_mode_alpha_blend);
  51. ClassDB::bind_method(D_METHOD("is_environment_blend_mode_alpha_supported"), &OpenXRAPIExtension::is_environment_blend_mode_alpha_blend_supported);
  52. BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE);
  53. BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_REAL);
  54. BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_EMULATING);
  55. }
  56. uint64_t OpenXRAPIExtension::get_instance() {
  57. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  58. return (uint64_t)OpenXRAPI::get_singleton()->get_instance();
  59. }
  60. uint64_t OpenXRAPIExtension::get_system_id() {
  61. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  62. return (uint64_t)OpenXRAPI::get_singleton()->get_system_id();
  63. }
  64. uint64_t OpenXRAPIExtension::get_session() {
  65. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  66. return (uint64_t)OpenXRAPI::get_singleton()->get_session();
  67. }
  68. Transform3D OpenXRAPIExtension::transform_from_pose(GDExtensionConstPtr<const void> p_pose) {
  69. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), Transform3D());
  70. return OpenXRAPI::get_singleton()->transform_from_pose(*(XrPosef *)p_pose.data);
  71. }
  72. bool OpenXRAPIExtension::xr_result(uint64_t result, String format, Array args) {
  73. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
  74. return OpenXRAPI::get_singleton()->xr_result((XrResult)result, format.utf8().get_data(), args);
  75. }
  76. bool OpenXRAPIExtension::openxr_is_enabled(bool p_check_run_in_editor) {
  77. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
  78. return OpenXRAPI::openxr_is_enabled(p_check_run_in_editor);
  79. }
  80. uint64_t OpenXRAPIExtension::get_instance_proc_addr(String p_name) {
  81. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  82. CharString str = p_name.utf8();
  83. PFN_xrVoidFunction addr = nullptr;
  84. XrResult result = OpenXRAPI::get_singleton()->get_instance_proc_addr(str.get_data(), &addr);
  85. if (result != XR_SUCCESS) {
  86. return 0;
  87. }
  88. return reinterpret_cast<uint64_t>(addr);
  89. }
  90. String OpenXRAPIExtension::get_error_string(uint64_t result) {
  91. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), String());
  92. return OpenXRAPI::get_singleton()->get_error_string((XrResult)result);
  93. }
  94. String OpenXRAPIExtension::get_swapchain_format_name(int64_t p_swapchain_format) {
  95. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), String());
  96. return OpenXRAPI::get_singleton()->get_swapchain_format_name(p_swapchain_format);
  97. }
  98. bool OpenXRAPIExtension::is_initialized() {
  99. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
  100. return OpenXRAPI::get_singleton()->is_initialized();
  101. }
  102. bool OpenXRAPIExtension::is_running() {
  103. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
  104. return OpenXRAPI::get_singleton()->is_running();
  105. }
  106. uint64_t OpenXRAPIExtension::get_play_space() {
  107. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  108. return (uint64_t)OpenXRAPI::get_singleton()->get_play_space();
  109. }
  110. int64_t OpenXRAPIExtension::get_next_frame_time() {
  111. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  112. return (XrTime)OpenXRAPI::get_singleton()->get_next_frame_time();
  113. }
  114. bool OpenXRAPIExtension::can_render() {
  115. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);
  116. return OpenXRAPI::get_singleton()->can_render();
  117. }
  118. uint64_t OpenXRAPIExtension::get_hand_tracker(int p_hand_index) {
  119. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);
  120. return (uint64_t)OpenXRAPI::get_singleton()->get_hand_tracker(p_hand_index);
  121. }
  122. void OpenXRAPIExtension::register_composition_layer_provider(OpenXRExtensionWrapperExtension *p_extension) {
  123. ERR_FAIL_NULL(OpenXRAPI::get_singleton());
  124. OpenXRAPI::get_singleton()->register_composition_layer_provider(p_extension);
  125. }
  126. void OpenXRAPIExtension::unregister_composition_layer_provider(OpenXRExtensionWrapperExtension *p_extension) {
  127. ERR_FAIL_NULL(OpenXRAPI::get_singleton());
  128. OpenXRAPI::get_singleton()->unregister_composition_layer_provider(p_extension);
  129. }
  130. void OpenXRAPIExtension::set_emulate_environment_blend_mode_alpha_blend(bool p_enabled) {
  131. ERR_FAIL_NULL(OpenXRAPI::get_singleton());
  132. OpenXRAPI::get_singleton()->set_emulate_environment_blend_mode_alpha_blend(p_enabled);
  133. }
  134. OpenXRAPIExtension::OpenXRAlphaBlendModeSupport OpenXRAPIExtension::is_environment_blend_mode_alpha_blend_supported() {
  135. ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE);
  136. return (OpenXRAPIExtension::OpenXRAlphaBlendModeSupport)OpenXRAPI::get_singleton()->is_environment_blend_mode_alpha_blend_supported();
  137. }
  138. OpenXRAPIExtension::OpenXRAPIExtension() {
  139. }