openxr_composition_layer_extension.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**************************************************************************/
  2. /* openxr_composition_layer_extension.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 "openxr_extension_wrapper.h"
  32. #include "../openxr_api.h"
  33. #ifdef ANDROID_ENABLED
  34. #include <jni.h>
  35. // Copied here from openxr_platform.h, in order to avoid including that whole header,
  36. // which can cause compilation issues on some platforms.
  37. typedef XrResult(XRAPI_PTR *PFN_xrCreateSwapchainAndroidSurfaceKHR)(XrSession session, const XrSwapchainCreateInfo *info, XrSwapchain *swapchain, jobject *surface);
  38. #endif
  39. class JavaObject;
  40. // This extension provides access to composition layers for displaying 2D content through the XR compositor.
  41. #define OPENXR_LAYER_FUNC1(m_name, m_arg1) \
  42. void _composition_layer_##m_name##_rt(RID p_layer, m_arg1 p1) { \
  43. CompositionLayer *layer = composition_layer_owner.get_or_null(p_layer); \
  44. ERR_FAIL_NULL(layer); \
  45. layer->m_name(p1); \
  46. } \
  47. void composition_layer_##m_name(RID p_layer, m_arg1 p1) { \
  48. RenderingServer::get_singleton()->call_on_render_thread(callable_mp(this, &OpenXRCompositionLayerExtension::_composition_layer_##m_name##_rt).bind(p_layer, p1)); \
  49. }
  50. #define OPENXR_LAYER_FUNC2(m_name, m_arg1, m_arg2) \
  51. void _composition_layer_##m_name##_rt(RID p_layer, m_arg1 p1, m_arg2 p2) { \
  52. CompositionLayer *layer = composition_layer_owner.get_or_null(p_layer); \
  53. ERR_FAIL_NULL(layer); \
  54. layer->m_name(p1, p2); \
  55. } \
  56. void composition_layer_##m_name(RID p_layer, m_arg1 p1, m_arg2 p2) { \
  57. RenderingServer::get_singleton()->call_on_render_thread(callable_mp(this, &OpenXRCompositionLayerExtension::_composition_layer_##m_name##_rt).bind(p_layer, p1, p2)); \
  58. }
  59. // OpenXRCompositionLayerExtension enables the extensions related to this functionality
  60. class OpenXRCompositionLayerExtension : public OpenXRExtensionWrapper {
  61. GDCLASS(OpenXRCompositionLayerExtension, OpenXRExtensionWrapper);
  62. protected:
  63. static void _bind_methods() {}
  64. public:
  65. // Must be identical to Filter enum definition in OpenXRCompositionLayer.
  66. enum Filter {
  67. FILTER_NEAREST,
  68. FILTER_LINEAR,
  69. FILTER_CUBIC,
  70. };
  71. // Must be identical to MipmapMode enum definition in OpenXRCompositionLayer.
  72. enum MipmapMode {
  73. MIPMAP_MODE_DISABLED,
  74. MIPMAP_MODE_NEAREST,
  75. MIPMAP_MODE_LINEAR,
  76. };
  77. // Must be identical to Wrap enum definition in OpenXRCompositionLayer.
  78. enum Wrap {
  79. WRAP_CLAMP_TO_BORDER,
  80. WRAP_CLAMP_TO_EDGE,
  81. WRAP_REPEAT,
  82. WRAP_MIRRORED_REPEAT,
  83. WRAP_MIRROR_CLAMP_TO_EDGE,
  84. };
  85. // Must be identical to Swizzle enum definition in OpenXRCompositionLayer.
  86. enum Swizzle {
  87. SWIZZLE_RED,
  88. SWIZZLE_GREEN,
  89. SWIZZLE_BLUE,
  90. SWIZZLE_ALPHA,
  91. SWIZZLE_ZERO,
  92. SWIZZLE_ONE,
  93. };
  94. struct SwapchainState {
  95. Filter min_filter = Filter::FILTER_LINEAR;
  96. Filter mag_filter = Filter::FILTER_LINEAR;
  97. MipmapMode mipmap_mode = MipmapMode::MIPMAP_MODE_LINEAR;
  98. Wrap horizontal_wrap = Wrap::WRAP_CLAMP_TO_BORDER;
  99. Wrap vertical_wrap = Wrap::WRAP_CLAMP_TO_BORDER;
  100. Swizzle red_swizzle = Swizzle::SWIZZLE_RED;
  101. Swizzle green_swizzle = Swizzle::SWIZZLE_GREEN;
  102. Swizzle blue_swizzle = Swizzle::SWIZZLE_BLUE;
  103. Swizzle alpha_swizzle = Swizzle::SWIZZLE_ALPHA;
  104. float max_anisotropy = 1.0;
  105. Color border_color = { 0.0, 0.0, 0.0, 0.0 };
  106. };
  107. static OpenXRCompositionLayerExtension *get_singleton();
  108. OpenXRCompositionLayerExtension();
  109. virtual ~OpenXRCompositionLayerExtension() override;
  110. virtual HashMap<String, bool *> get_requested_extensions(XrVersion p_version) override;
  111. virtual void on_instance_created(const XrInstance p_instance) override;
  112. virtual void on_session_created(const XrSession p_session) override;
  113. virtual void on_session_destroyed() override;
  114. virtual void on_pre_render() override;
  115. virtual int get_composition_layer_count() override;
  116. virtual XrCompositionLayerBaseHeader *get_composition_layer(int p_index) override;
  117. virtual int get_composition_layer_order(int p_index) override;
  118. // The data on p_openxr_layer will be copied - there is no need to keep it valid after this call.
  119. RID composition_layer_create(XrCompositionLayerBaseHeader *p_openxr_layer);
  120. void composition_layer_free(RID p_layer);
  121. void composition_layer_register(RID p_layer);
  122. void composition_layer_unregister(RID p_layer);
  123. OPENXR_LAYER_FUNC2(set_viewport, RID, const Size2i &);
  124. OPENXR_LAYER_FUNC2(set_use_android_surface, bool, const Size2i &);
  125. OPENXR_LAYER_FUNC1(set_sort_order, int);
  126. OPENXR_LAYER_FUNC1(set_alpha_blend, bool);
  127. OPENXR_LAYER_FUNC1(set_transform, const Transform3D &);
  128. OPENXR_LAYER_FUNC1(set_protected_content, bool);
  129. OPENXR_LAYER_FUNC1(set_extension_property_values, Dictionary);
  130. OPENXR_LAYER_FUNC1(set_min_filter, Filter);
  131. OPENXR_LAYER_FUNC1(set_mag_filter, Filter);
  132. OPENXR_LAYER_FUNC1(set_mipmap_mode, MipmapMode);
  133. OPENXR_LAYER_FUNC1(set_horizontal_wrap, Wrap);
  134. OPENXR_LAYER_FUNC1(set_vertical_wrap, Wrap);
  135. OPENXR_LAYER_FUNC1(set_red_swizzle, Swizzle);
  136. OPENXR_LAYER_FUNC1(set_blue_swizzle, Swizzle);
  137. OPENXR_LAYER_FUNC1(set_green_swizzle, Swizzle);
  138. OPENXR_LAYER_FUNC1(set_alpha_swizzle, Swizzle);
  139. OPENXR_LAYER_FUNC1(set_max_anisotropy, float);
  140. OPENXR_LAYER_FUNC1(set_border_color, const Color &);
  141. OPENXR_LAYER_FUNC1(set_quad_size, const Size2 &);
  142. OPENXR_LAYER_FUNC1(set_cylinder_radius, float);
  143. OPENXR_LAYER_FUNC1(set_cylinder_aspect_ratio, float);
  144. OPENXR_LAYER_FUNC1(set_cylinder_central_angle, float);
  145. OPENXR_LAYER_FUNC1(set_equirect_radius, float);
  146. OPENXR_LAYER_FUNC1(set_equirect_central_horizontal_angle, float);
  147. OPENXR_LAYER_FUNC1(set_equirect_upper_vertical_angle, float);
  148. OPENXR_LAYER_FUNC1(set_equirect_lower_vertical_angle, float);
  149. Ref<JavaObject> composition_layer_get_android_surface(RID p_layer);
  150. bool is_available(XrStructureType p_which);
  151. bool is_android_surface_swapchain_available() { return android_surface_ext_available; }
  152. private:
  153. static OpenXRCompositionLayerExtension *singleton;
  154. bool cylinder_ext_available = false;
  155. bool equirect_ext_available = false;
  156. bool android_surface_ext_available = false;
  157. void _composition_layer_free_rt(RID p_layer);
  158. void _composition_layer_register_rt(RID p_layer);
  159. void _composition_layer_unregister_rt(RID p_layer);
  160. #ifdef ANDROID_ENABLED
  161. bool create_android_surface_swapchain(XrSwapchainCreateInfo *p_info, XrSwapchain *r_swapchain, jobject *r_surface);
  162. EXT_PROTO_XRRESULT_FUNC1(xrDestroySwapchain, (XrSwapchain), swapchain)
  163. EXT_PROTO_XRRESULT_FUNC4(xrCreateSwapchainAndroidSurfaceKHR, (XrSession), session, (const XrSwapchainCreateInfo *), info, (XrSwapchain *), swapchain, (jobject *), surface)
  164. #endif
  165. struct CompositionLayer {
  166. union {
  167. XrCompositionLayerBaseHeader composition_layer;
  168. XrCompositionLayerQuad composition_layer_quad;
  169. XrCompositionLayerCylinderKHR composition_layer_cylinder;
  170. XrCompositionLayerEquirect2KHR composition_layer_equirect;
  171. };
  172. int sort_order = 1;
  173. bool alpha_blend = false;
  174. Dictionary extension_property_values;
  175. bool extension_property_values_changed = true;
  176. struct {
  177. RID viewport;
  178. Size2i viewport_size;
  179. OpenXRAPI::OpenXRSwapChainInfo swapchain_info;
  180. bool static_image = false;
  181. bool swapchain_protected_content = false;
  182. } subviewport;
  183. #ifdef ANDROID_ENABLED
  184. struct {
  185. XrSwapchain swapchain = XR_NULL_HANDLE;
  186. Ref<JavaObject> surface;
  187. } android_surface;
  188. #endif
  189. bool use_android_surface = false;
  190. bool protected_content = false;
  191. Size2i swapchain_size;
  192. SwapchainState swapchain_state;
  193. bool swapchain_state_is_dirty = false;
  194. void set_viewport(RID p_viewport, const Size2i &p_size);
  195. void set_use_android_surface(bool p_use_android_surface, const Size2i &p_size);
  196. void set_sort_order(int p_sort_order) { sort_order = p_sort_order; }
  197. void set_alpha_blend(bool p_alpha_blend);
  198. void set_protected_content(bool p_protected_content) { protected_content = p_protected_content; }
  199. void set_transform(const Transform3D &p_transform);
  200. void set_extension_property_values(const Dictionary &p_extension_property_values);
  201. void set_min_filter(Filter p_mode);
  202. void set_mag_filter(Filter p_mode);
  203. void set_mipmap_mode(MipmapMode p_mode);
  204. void set_horizontal_wrap(Wrap p_mode);
  205. void set_vertical_wrap(Wrap p_mode);
  206. void set_red_swizzle(Swizzle p_mode);
  207. void set_green_swizzle(Swizzle p_mode);
  208. void set_blue_swizzle(Swizzle p_mode);
  209. void set_alpha_swizzle(Swizzle p_mode);
  210. void set_max_anisotropy(float p_value);
  211. void set_border_color(const Color &p_color);
  212. void set_quad_size(const Size2 &p_size);
  213. void set_cylinder_radius(float p_radius);
  214. void set_cylinder_aspect_ratio(float p_aspect_ratio);
  215. void set_cylinder_central_angle(float p_central_angle);
  216. void set_equirect_radius(float p_radius);
  217. void set_equirect_central_horizontal_angle(float p_angle);
  218. void set_equirect_upper_vertical_angle(float p_angle);
  219. void set_equirect_lower_vertical_angle(float p_angle);
  220. Ref<JavaObject> get_android_surface();
  221. void on_pre_render();
  222. XrCompositionLayerBaseHeader *get_composition_layer();
  223. void free();
  224. private:
  225. void update_swapchain_state();
  226. void update_swapchain_sub_image(XrSwapchainSubImage &r_subimage);
  227. bool update_and_acquire_swapchain(bool p_static_image);
  228. RID get_current_swapchain_texture();
  229. void free_swapchain();
  230. #ifdef ANDROID_ENABLED
  231. void create_android_surface();
  232. #endif
  233. };
  234. Mutex composition_layer_mutex;
  235. RID_Owner<CompositionLayer, true> composition_layer_owner;
  236. LocalVector<CompositionLayer *> registered_composition_layers;
  237. };
  238. VARIANT_ENUM_CAST(OpenXRCompositionLayerExtension::Filter);
  239. VARIANT_ENUM_CAST(OpenXRCompositionLayerExtension::MipmapMode);
  240. VARIANT_ENUM_CAST(OpenXRCompositionLayerExtension::Wrap);
  241. VARIANT_ENUM_CAST(OpenXRCompositionLayerExtension::Swizzle);
  242. #undef OPENXR_LAYER_FUNC1
  243. #undef OPENXR_LAYER_FUNC2