openxr_extension_wrapper.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**************************************************************************/
  2. /* openxr_extension_wrapper.h */
  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. #pragma once
  31. #include "core/error/error_macros.h"
  32. #include "core/math/projection.h"
  33. #include "core/object/class_db.h"
  34. #include "core/object/gdvirtual.gen.inc"
  35. #include "core/templates/hash_map.h"
  36. #include "core/templates/rid.h"
  37. #include "core/variant/native_ptr.h"
  38. #include "core/variant/typed_array.h"
  39. #include "core/variant/variant.h"
  40. #include <openxr/openxr.h>
  41. class OpenXRAPI;
  42. class OpenXRAPIExtension;
  43. class OpenXRActionMap;
  44. // `OpenXRExtensionWrapper` allows us to implement OpenXR extensions.
  45. class OpenXRExtensionWrapper : public Object {
  46. GDCLASS(OpenXRExtensionWrapper, Object);
  47. Ref<OpenXRAPIExtension> _gdextension_get_openxr_api();
  48. void _gdextension_register_extension_wrapper();
  49. protected:
  50. static void _bind_methods();
  51. Ref<OpenXRAPIExtension> openxr_api_extension;
  52. public:
  53. // `get_requested_extensions` should return a list of OpenXR extensions related to this extension.
  54. // This function can be called multiple times.
  55. // p_xr_version is the OpenXR version which we're attempting to initialize.
  56. // Note: when called from the editor, p_xr_version will be 0 and we should return any extension we may initialize.
  57. // If the bool * is a nullptr this extension is mandatory
  58. // If the bool * points to a boolean, the boolean will be updated
  59. // to true if the extension is enabled.
  60. virtual HashMap<String, bool *> get_requested_extensions(XrVersion p_xr_version);
  61. GDVIRTUAL1R(Dictionary, _get_requested_extensions, uint64_t);
  62. // These functions allow an extension to add entries to a struct chain.
  63. // `p_next_pointer` points to the last struct that was created for this chain
  64. // and should be used as the value for the `pNext` pointer in the first struct you add.
  65. // You should return the pointer to the last struct you define as your result.
  66. // If you are not adding any structs, just return `p_next_pointer`.
  67. // See existing extensions for examples of this implementation.
  68. virtual void *set_system_properties_and_get_next_pointer(void *p_next_pointer); // Add additional data structures when we interrogate OpenXR's system abilities.
  69. virtual void *set_instance_create_info_and_get_next_pointer(XrVersion p_xr_version, void *p_next_pointer); // Add additional data structures when we create our OpenXR instance.
  70. virtual void *set_session_create_and_get_next_pointer(void *p_next_pointer); // Add additional data structures when we create our OpenXR session.
  71. virtual void *set_swapchain_create_info_and_get_next_pointer(void *p_next_pointer); // Add additional data structures when creating OpenXR swap chains.
  72. virtual void *set_hand_joint_locations_and_get_next_pointer(int p_hand_index, void *p_next_pointer);
  73. virtual void *set_projection_views_and_get_next_pointer(int p_view_index, void *p_next_pointer);
  74. virtual void *set_reference_space_create_info_and_get_next_pointer(int p_reference_space_type, void *p_next_pointer);
  75. // These will only be called for extensions registered via OpenXRApi::register_frame_info_extension().
  76. virtual void *set_frame_wait_info_and_get_next_pointer(void *p_next_pointer); // Add additional data structures when calling xrWaitFrame
  77. virtual void *set_view_locate_info_and_get_next_pointer(void *p_next_pointer); // Add additional data structures when calling xrLocateViews
  78. virtual void *set_frame_end_info_and_get_next_pointer(void *p_next_pointer); // Add additional data structures when calling xrEndFrame
  79. virtual void prepare_view_configuration(uint32_t p_view_count);
  80. virtual void *set_view_configuration_and_get_next_pointer(uint32_t p_view, void *p_next_pointer); // Add additional data structures when calling xrEnumerateViewConfiguration
  81. virtual void print_view_configuration_info(uint32_t p_view) const;
  82. //TODO workaround as GDExtensionPtr<void> return type results in build error in godot-cpp
  83. GDVIRTUAL1R(uint64_t, _set_system_properties_and_get_next_pointer, GDExtensionPtr<void>);
  84. GDVIRTUAL2R(uint64_t, _set_instance_create_info_and_get_next_pointer, uint64_t, GDExtensionPtr<void>);
  85. GDVIRTUAL1R(uint64_t, _set_session_create_and_get_next_pointer, GDExtensionPtr<void>);
  86. GDVIRTUAL1R(uint64_t, _set_swapchain_create_info_and_get_next_pointer, GDExtensionPtr<void>);
  87. GDVIRTUAL2R(uint64_t, _set_hand_joint_locations_and_get_next_pointer, int, GDExtensionPtr<void>);
  88. GDVIRTUAL2R(uint64_t, _set_projection_views_and_get_next_pointer, int, GDExtensionPtr<void>);
  89. GDVIRTUAL1R(uint64_t, _set_frame_wait_info_and_get_next_pointer, GDExtensionPtr<void>);
  90. GDVIRTUAL1R(uint64_t, _set_frame_end_info_and_get_next_pointer, GDExtensionPtr<void>);
  91. GDVIRTUAL1R(uint64_t, _set_view_locate_info_and_get_next_pointer, GDExtensionPtr<void>);
  92. GDVIRTUAL2R(uint64_t, _set_reference_space_create_info_and_get_next_pointer, int, GDExtensionPtr<void>);
  93. GDVIRTUAL0R(int, _get_composition_layer_count);
  94. GDVIRTUAL1R(uint64_t, _get_composition_layer, int);
  95. GDVIRTUAL1R(int, _get_composition_layer_order, int);
  96. GDVIRTUAL1(_prepare_view_configuration, int);
  97. GDVIRTUAL2R(uint64_t, _set_view_configuration_and_get_next_pointer, uint32_t, GDExtensionPtr<void>);
  98. GDVIRTUAL1C(_print_view_configuration_info, int);
  99. #ifndef DISABLE_DEPRECATED
  100. GDVIRTUAL0R_COMPAT(_get_requested_extensions_bind_compat_109302, Dictionary, _get_requested_extensions);
  101. GDVIRTUAL1R_COMPAT(_set_instance_create_info_and_get_next_pointer_bind_compat_109302, uint64_t, _set_instance_create_info_and_get_next_pointer, GDExtensionPtr<void>);
  102. #endif
  103. virtual PackedStringArray get_suggested_tracker_names();
  104. GDVIRTUAL0R(PackedStringArray, _get_suggested_tracker_names);
  105. // `on_register_metadata` allows extensions to register additional controller metadata.
  106. // This function is called even when OpenXRApi is not constructured as the metadata
  107. // needs to be available to the editor.
  108. // Also extensions should provide metadata regardless of whether they are supported
  109. // on the host system as the controller data is used to setup action maps for users
  110. // who may have access to the relevant hardware.
  111. virtual void on_register_metadata();
  112. virtual void on_before_instance_created(); // `on_before_instance_created` is called before we create our OpenXR instance.
  113. virtual void on_instance_created(const XrInstance p_instance); // `on_instance_created` is called right after we've successfully created our OpenXR instance.
  114. virtual void on_instance_destroyed(); // `on_instance_destroyed` is called right before we destroy our OpenXR instance.
  115. virtual void on_session_created(const XrSession p_session); // `on_session_created` is called right after we've successfully created our OpenXR session.
  116. virtual void on_session_destroyed(); // `on_session_destroyed` is called right before we destroy our OpenXR session.
  117. // `on_process` is called as part of our OpenXR process handling,
  118. // this happens right before physics process and normal processing is run.
  119. // This is when controller data is queried and made available to game logic.
  120. virtual void on_process();
  121. virtual void on_sync_actions(); // `on_sync_actions` is called right after we sync our action sets.
  122. virtual void on_pre_render(); // `on_pre_render` is called right before we start rendering our XR viewports.
  123. virtual void on_main_swapchains_created(); // `on_main_swapchains_created` is called right after our main swapchains are (re)created.
  124. virtual void on_pre_draw_viewport(RID p_render_target); // `on_pre_draw_viewport` is called right before we start rendering this viewport
  125. virtual void on_post_draw_viewport(RID p_render_target); // `on_port_draw_viewport` is called right after we start rendering this viewport (note that on Vulkan draw commands may only be queued)
  126. GDVIRTUAL0(_on_register_metadata);
  127. GDVIRTUAL0(_on_before_instance_created);
  128. GDVIRTUAL1(_on_instance_created, uint64_t);
  129. GDVIRTUAL0(_on_instance_destroyed);
  130. GDVIRTUAL1(_on_session_created, uint64_t);
  131. GDVIRTUAL0(_on_process);
  132. GDVIRTUAL0(_on_sync_actions);
  133. GDVIRTUAL0(_on_pre_render);
  134. GDVIRTUAL0(_on_main_swapchains_created);
  135. GDVIRTUAL0(_on_session_destroyed);
  136. GDVIRTUAL1(_on_pre_draw_viewport, RID);
  137. GDVIRTUAL1(_on_post_draw_viewport, RID);
  138. virtual void on_state_idle(); // `on_state_idle` is called when the OpenXR session state is changed to idle.
  139. virtual void on_state_ready(); // `on_state_ready` is called when the OpenXR session state is changed to ready, this means OpenXR is ready to setup our session.
  140. virtual void on_state_synchronized(); // `on_state_synchronized` is called when the OpenXR session state is changed to synchronized, note that OpenXR also returns to this state when our application looses focus.
  141. virtual void on_state_visible(); // `on_state_visible` is called when the OpenXR session state is changed to visible, OpenXR is now ready to receive frames.
  142. virtual void on_state_focused(); // `on_state_focused` is called when the OpenXR session state is changed to focused, this state is the active state when our game runs.
  143. virtual void on_state_stopping(); // `on_state_stopping` is called when the OpenXR session state is changed to stopping.
  144. virtual void on_state_loss_pending(); // `on_state_loss_pending` is called when the OpenXR session state is changed to loss pending.
  145. virtual void on_state_exiting(); // `on_state_exiting` is called when the OpenXR session state is changed to exiting.
  146. GDVIRTUAL0(_on_state_idle);
  147. GDVIRTUAL0(_on_state_ready);
  148. GDVIRTUAL0(_on_state_synchronized);
  149. GDVIRTUAL0(_on_state_visible);
  150. GDVIRTUAL0(_on_state_focused);
  151. GDVIRTUAL0(_on_state_stopping);
  152. GDVIRTUAL0(_on_state_loss_pending);
  153. GDVIRTUAL0(_on_state_exiting);
  154. // These will only be called on extensions registered via OpenXRAPI::register_composition_layer_provider().
  155. virtual int get_composition_layer_count();
  156. virtual XrCompositionLayerBaseHeader *get_composition_layer(int p_index);
  157. virtual int get_composition_layer_order(int p_index);
  158. virtual void *set_viewport_composition_layer_and_get_next_pointer(const XrCompositionLayerBaseHeader *p_layer, const Dictionary &p_property_values, void *p_next_pointer); // Add additional data structures to composition layers created via OpenXRCompositionLayer.
  159. virtual void on_viewport_composition_layer_destroyed(const XrCompositionLayerBaseHeader *p_layer); // `on_viewport_composition_layer_destroyed` is called when a composition layer created via OpenXRCompositionLayer is destroyed.
  160. virtual void get_viewport_composition_layer_extension_properties(List<PropertyInfo> *p_property_list); // Get additional property definitions for OpenXRCompositionLayer.
  161. virtual Dictionary get_viewport_composition_layer_extension_property_defaults(); // Get the default values for the additional property definitions for OpenXRCompositionLayer.
  162. virtual void *set_android_surface_swapchain_create_info_and_get_next_pointer(const Dictionary &p_property_values, void *p_next_pointer);
  163. GDVIRTUAL3R(uint64_t, _set_viewport_composition_layer_and_get_next_pointer, GDExtensionConstPtr<void>, Dictionary, GDExtensionPtr<void>);
  164. GDVIRTUAL1(_on_viewport_composition_layer_destroyed, GDExtensionConstPtr<void>);
  165. GDVIRTUAL0R(TypedArray<Dictionary>, _get_viewport_composition_layer_extension_properties);
  166. GDVIRTUAL0R(Dictionary, _get_viewport_composition_layer_extension_property_defaults);
  167. GDVIRTUAL2R(uint64_t, _set_android_surface_swapchain_create_info_and_get_next_pointer, Dictionary, GDExtensionPtr<void>);
  168. // `on_event_polled` is called when there is an OpenXR event to process.
  169. // Should return true if the event was handled, false otherwise.
  170. virtual bool on_event_polled(const XrEventDataBuffer &event);
  171. GDVIRTUAL1R(bool, _on_event_polled, GDExtensionConstPtr<void>);
  172. OpenXRExtensionWrapper();
  173. virtual ~OpenXRExtensionWrapper() override;
  174. };
  175. // `OpenXRGraphicsExtensionWrapper` implements specific logic for each supported graphics API.
  176. class OpenXRGraphicsExtensionWrapper : public OpenXRExtensionWrapper {
  177. public:
  178. virtual void get_usable_swapchain_formats(Vector<int64_t> &p_usable_swap_chains) = 0; // `get_usable_swapchain_formats` should return a list of usable color formats.
  179. virtual void get_usable_depth_formats(Vector<int64_t> &p_usable_swap_chains) = 0; // `get_usable_depth_formats` should return a list of usable depth formats.
  180. virtual String get_swapchain_format_name(int64_t p_swapchain_format) const = 0; // `get_swapchain_format_name` should return the constant name of a given format.
  181. virtual bool get_swapchain_image_data(XrSwapchain p_swapchain, int64_t p_swapchain_format, uint32_t p_width, uint32_t p_height, uint32_t p_sample_count, uint32_t p_array_size, void **r_swapchain_graphics_data) = 0; // `get_swapchain_image_data` extracts image IDs for the swapchain images and stores there in an implementation dependent data structure.
  182. virtual void cleanup_swapchain_graphics_data(void **p_swapchain_graphics_data) = 0; // `cleanup_swapchain_graphics_data` cleans up the data held in our implementation dependent data structure and should free up its memory.
  183. virtual bool create_projection_fov(const XrFovf p_fov, double p_z_near, double p_z_far, Projection &r_camera_matrix) = 0; // `create_projection_fov` creates a proper projection matrix based on asymmetric FOV data provided by OpenXR.
  184. virtual RID get_texture(void *p_swapchain_graphics_data, int p_image_index) = 0; // `get_texture` returns a Godot texture RID for the current active texture in our swapchain.
  185. virtual RID get_density_map(void *p_swapchain_graphics_data, int p_image_index) = 0; // `get_density_map` returns a Godot texture RID for the current active density map in our swapchain (if any).
  186. };