openxr_extension_wrapper.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**************************************************************************/
  2. /* openxr_extension_wrapper.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_extension_wrapper.h"
  31. #include "../openxr_api.h"
  32. #include "../openxr_api_extension.h"
  33. void OpenXRExtensionWrapper::_bind_methods() {
  34. GDVIRTUAL_BIND(_get_requested_extensions);
  35. GDVIRTUAL_BIND(_set_system_properties_and_get_next_pointer, "next_pointer");
  36. GDVIRTUAL_BIND(_set_instance_create_info_and_get_next_pointer, "next_pointer");
  37. GDVIRTUAL_BIND(_set_session_create_and_get_next_pointer, "next_pointer");
  38. GDVIRTUAL_BIND(_set_swapchain_create_info_and_get_next_pointer, "next_pointer");
  39. GDVIRTUAL_BIND(_set_hand_joint_locations_and_get_next_pointer, "hand_index", "next_pointer");
  40. GDVIRTUAL_BIND(_set_projection_views_and_get_next_pointer, "view_index", "next_pointer");
  41. GDVIRTUAL_BIND(_get_composition_layer_count);
  42. GDVIRTUAL_BIND(_get_composition_layer, "index");
  43. GDVIRTUAL_BIND(_get_composition_layer_order, "index");
  44. GDVIRTUAL_BIND(_get_suggested_tracker_names);
  45. GDVIRTUAL_BIND(_on_register_metadata);
  46. GDVIRTUAL_BIND(_on_before_instance_created);
  47. GDVIRTUAL_BIND(_on_instance_created, "instance");
  48. GDVIRTUAL_BIND(_on_instance_destroyed);
  49. GDVIRTUAL_BIND(_on_session_created, "session");
  50. GDVIRTUAL_BIND(_on_process);
  51. GDVIRTUAL_BIND(_on_pre_render);
  52. GDVIRTUAL_BIND(_on_main_swapchains_created);
  53. GDVIRTUAL_BIND(_on_pre_draw_viewport, "viewport");
  54. GDVIRTUAL_BIND(_on_post_draw_viewport, "viewport");
  55. GDVIRTUAL_BIND(_on_session_destroyed);
  56. GDVIRTUAL_BIND(_on_state_idle);
  57. GDVIRTUAL_BIND(_on_state_ready);
  58. GDVIRTUAL_BIND(_on_state_synchronized);
  59. GDVIRTUAL_BIND(_on_state_visible);
  60. GDVIRTUAL_BIND(_on_state_focused);
  61. GDVIRTUAL_BIND(_on_state_stopping);
  62. GDVIRTUAL_BIND(_on_state_loss_pending);
  63. GDVIRTUAL_BIND(_on_state_exiting);
  64. GDVIRTUAL_BIND(_on_event_polled, "event");
  65. GDVIRTUAL_BIND(_set_viewport_composition_layer_and_get_next_pointer, "layer", "property_values", "next_pointer");
  66. GDVIRTUAL_BIND(_get_viewport_composition_layer_extension_properties);
  67. GDVIRTUAL_BIND(_get_viewport_composition_layer_extension_property_defaults);
  68. GDVIRTUAL_BIND(_on_viewport_composition_layer_destroyed, "layer");
  69. GDVIRTUAL_BIND(_set_android_surface_swapchain_create_info_and_get_next_pointer, "property_values", "next_pointer");
  70. ClassDB::bind_method(D_METHOD("get_openxr_api"), &OpenXRExtensionWrapper::_gdextension_get_openxr_api);
  71. ClassDB::bind_method(D_METHOD("register_extension_wrapper"), &OpenXRExtensionWrapper::_gdextension_register_extension_wrapper);
  72. }
  73. HashMap<String, bool *> OpenXRExtensionWrapper::get_requested_extensions() {
  74. Dictionary request_extension;
  75. if (GDVIRTUAL_CALL(_get_requested_extensions, request_extension)) {
  76. HashMap<String, bool *> result;
  77. Array keys = request_extension.keys();
  78. for (int i = 0; i < keys.size(); i++) {
  79. String key = keys.get(i);
  80. GDExtensionPtr<bool> value = VariantCaster<GDExtensionPtr<bool>>::cast(request_extension.get(key, GDExtensionPtr<bool>(nullptr)));
  81. result.insert(key, value);
  82. }
  83. return result;
  84. }
  85. return HashMap<String, bool *>();
  86. }
  87. void *OpenXRExtensionWrapper::set_system_properties_and_get_next_pointer(void *p_next_pointer) {
  88. uint64_t pointer;
  89. if (GDVIRTUAL_CALL(_set_system_properties_and_get_next_pointer, GDExtensionPtr<void>(p_next_pointer), pointer)) {
  90. return reinterpret_cast<void *>(pointer);
  91. }
  92. return nullptr;
  93. }
  94. void *OpenXRExtensionWrapper::set_instance_create_info_and_get_next_pointer(void *p_next_pointer) {
  95. uint64_t pointer;
  96. if (GDVIRTUAL_CALL(_set_instance_create_info_and_get_next_pointer, GDExtensionPtr<void>(p_next_pointer), pointer)) {
  97. return reinterpret_cast<void *>(pointer);
  98. }
  99. return nullptr;
  100. }
  101. void *OpenXRExtensionWrapper::set_session_create_and_get_next_pointer(void *p_next_pointer) {
  102. uint64_t pointer;
  103. if (GDVIRTUAL_CALL(_set_session_create_and_get_next_pointer, GDExtensionPtr<void>(p_next_pointer), pointer)) {
  104. return reinterpret_cast<void *>(pointer);
  105. }
  106. return nullptr;
  107. }
  108. void *OpenXRExtensionWrapper::set_swapchain_create_info_and_get_next_pointer(void *p_next_pointer) {
  109. uint64_t pointer;
  110. if (GDVIRTUAL_CALL(_set_swapchain_create_info_and_get_next_pointer, GDExtensionPtr<void>(p_next_pointer), pointer)) {
  111. return reinterpret_cast<void *>(pointer);
  112. }
  113. return nullptr;
  114. }
  115. void *OpenXRExtensionWrapper::set_hand_joint_locations_and_get_next_pointer(int p_hand_index, void *p_next_pointer) {
  116. uint64_t pointer;
  117. if (GDVIRTUAL_CALL(_set_hand_joint_locations_and_get_next_pointer, p_hand_index, GDExtensionPtr<void>(p_next_pointer), pointer)) {
  118. return reinterpret_cast<void *>(pointer);
  119. }
  120. return nullptr;
  121. }
  122. void *OpenXRExtensionWrapper::set_projection_views_and_get_next_pointer(int p_view_index, void *p_next_pointer) {
  123. uint64_t pointer = 0;
  124. if (GDVIRTUAL_CALL(_set_projection_views_and_get_next_pointer, p_view_index, GDExtensionPtr<void>(p_next_pointer), pointer)) {
  125. return reinterpret_cast<void *>(pointer);
  126. }
  127. return nullptr;
  128. }
  129. PackedStringArray OpenXRExtensionWrapper::get_suggested_tracker_names() {
  130. PackedStringArray ret;
  131. if (GDVIRTUAL_CALL(_get_suggested_tracker_names, ret)) {
  132. return ret;
  133. }
  134. return PackedStringArray();
  135. }
  136. int OpenXRExtensionWrapper::get_composition_layer_count() {
  137. int count = 0;
  138. GDVIRTUAL_CALL(_get_composition_layer_count, count);
  139. return count;
  140. }
  141. XrCompositionLayerBaseHeader *OpenXRExtensionWrapper::get_composition_layer(int p_index) {
  142. uint64_t pointer;
  143. if (GDVIRTUAL_CALL(_get_composition_layer, p_index, pointer)) {
  144. return reinterpret_cast<XrCompositionLayerBaseHeader *>(pointer);
  145. }
  146. return nullptr;
  147. }
  148. int OpenXRExtensionWrapper::get_composition_layer_order(int p_index) {
  149. int order = 0;
  150. GDVIRTUAL_CALL(_get_composition_layer_order, p_index, order);
  151. return order;
  152. }
  153. void OpenXRExtensionWrapper::on_register_metadata() {
  154. GDVIRTUAL_CALL(_on_register_metadata);
  155. }
  156. void OpenXRExtensionWrapper::on_before_instance_created() {
  157. GDVIRTUAL_CALL(_on_before_instance_created);
  158. }
  159. void OpenXRExtensionWrapper::on_instance_created(const XrInstance p_instance) {
  160. uint64_t instance = (uint64_t)p_instance;
  161. GDVIRTUAL_CALL(_on_instance_created, instance);
  162. }
  163. void OpenXRExtensionWrapper::on_instance_destroyed() {
  164. GDVIRTUAL_CALL(_on_instance_destroyed);
  165. }
  166. void OpenXRExtensionWrapper::on_session_created(const XrSession p_session) {
  167. uint64_t session = (uint64_t)p_session;
  168. GDVIRTUAL_CALL(_on_session_created, session);
  169. }
  170. void OpenXRExtensionWrapper::on_process() {
  171. GDVIRTUAL_CALL(_on_process);
  172. }
  173. void OpenXRExtensionWrapper::on_pre_render() {
  174. GDVIRTUAL_CALL(_on_pre_render);
  175. }
  176. void OpenXRExtensionWrapper::on_main_swapchains_created() {
  177. GDVIRTUAL_CALL(_on_main_swapchains_created);
  178. }
  179. void OpenXRExtensionWrapper::on_session_destroyed() {
  180. GDVIRTUAL_CALL(_on_session_destroyed);
  181. }
  182. void OpenXRExtensionWrapper::on_pre_draw_viewport(RID p_render_target) {
  183. GDVIRTUAL_CALL(_on_pre_draw_viewport, p_render_target);
  184. }
  185. void OpenXRExtensionWrapper::on_post_draw_viewport(RID p_render_target) {
  186. GDVIRTUAL_CALL(_on_post_draw_viewport, p_render_target);
  187. }
  188. void OpenXRExtensionWrapper::on_state_idle() {
  189. GDVIRTUAL_CALL(_on_state_idle);
  190. }
  191. void OpenXRExtensionWrapper::on_state_ready() {
  192. GDVIRTUAL_CALL(_on_state_ready);
  193. }
  194. void OpenXRExtensionWrapper::on_state_synchronized() {
  195. GDVIRTUAL_CALL(_on_state_synchronized);
  196. }
  197. void OpenXRExtensionWrapper::on_state_visible() {
  198. GDVIRTUAL_CALL(_on_state_visible);
  199. }
  200. void OpenXRExtensionWrapper::on_state_focused() {
  201. GDVIRTUAL_CALL(_on_state_focused);
  202. }
  203. void OpenXRExtensionWrapper::on_state_stopping() {
  204. GDVIRTUAL_CALL(_on_state_stopping);
  205. }
  206. void OpenXRExtensionWrapper::on_state_loss_pending() {
  207. GDVIRTUAL_CALL(_on_state_loss_pending);
  208. }
  209. void OpenXRExtensionWrapper::on_state_exiting() {
  210. GDVIRTUAL_CALL(_on_state_exiting);
  211. }
  212. bool OpenXRExtensionWrapper::on_event_polled(const XrEventDataBuffer &p_event) {
  213. bool event_polled;
  214. if (GDVIRTUAL_CALL(_on_event_polled, GDExtensionConstPtr<void>(&p_event), event_polled)) {
  215. return event_polled;
  216. }
  217. return false;
  218. }
  219. void *OpenXRExtensionWrapper::set_viewport_composition_layer_and_get_next_pointer(const XrCompositionLayerBaseHeader *p_layer, const Dictionary &p_property_values, void *p_next_pointer) {
  220. uint64_t pointer = 0;
  221. if (GDVIRTUAL_CALL(_set_viewport_composition_layer_and_get_next_pointer, GDExtensionConstPtr<void>(p_layer), p_property_values, GDExtensionPtr<void>(p_next_pointer), pointer)) {
  222. return reinterpret_cast<void *>(pointer);
  223. }
  224. return p_next_pointer;
  225. }
  226. void OpenXRExtensionWrapper::on_viewport_composition_layer_destroyed(const XrCompositionLayerBaseHeader *p_layer) {
  227. GDVIRTUAL_CALL(_on_viewport_composition_layer_destroyed, GDExtensionConstPtr<void>(p_layer));
  228. }
  229. void OpenXRExtensionWrapper::get_viewport_composition_layer_extension_properties(List<PropertyInfo> *p_property_list) {
  230. TypedArray<Dictionary> properties;
  231. if (GDVIRTUAL_CALL(_get_viewport_composition_layer_extension_properties, properties)) {
  232. for (int i = 0; i < properties.size(); i++) {
  233. p_property_list->push_back(PropertyInfo::from_dict(properties[i]));
  234. }
  235. }
  236. }
  237. Dictionary OpenXRExtensionWrapper::get_viewport_composition_layer_extension_property_defaults() {
  238. Dictionary property_defaults;
  239. GDVIRTUAL_CALL(_get_viewport_composition_layer_extension_property_defaults, property_defaults);
  240. return property_defaults;
  241. }
  242. void *OpenXRExtensionWrapper::set_android_surface_swapchain_create_info_and_get_next_pointer(const Dictionary &p_property_values, void *p_next_pointer) {
  243. uint64_t pointer = 0;
  244. if (GDVIRTUAL_CALL(_set_android_surface_swapchain_create_info_and_get_next_pointer, p_property_values, GDExtensionPtr<void>(p_next_pointer), pointer)) {
  245. return reinterpret_cast<void *>(pointer);
  246. }
  247. return p_next_pointer;
  248. }
  249. Ref<OpenXRAPIExtension> OpenXRExtensionWrapper::_gdextension_get_openxr_api() {
  250. static Ref<OpenXRAPIExtension> openxr_api_extension;
  251. if (unlikely(openxr_api_extension.is_null())) {
  252. openxr_api_extension.instantiate();
  253. }
  254. return openxr_api_extension;
  255. }
  256. void OpenXRExtensionWrapper::_gdextension_register_extension_wrapper() {
  257. OpenXRAPI::register_extension_wrapper(this);
  258. }