openxr_composition_layer.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**************************************************************************/
  2. /* openxr_composition_layer.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/openxr.h>
  32. #include "../extensions/openxr_composition_layer_extension.h"
  33. #include "scene/3d/node_3d.h"
  34. class JavaObject;
  35. class MeshInstance3D;
  36. class Mesh;
  37. class OpenXRAPI;
  38. class OpenXRCompositionLayerExtension;
  39. class OpenXRViewportCompositionLayerProvider;
  40. class SubViewport;
  41. class OpenXRCompositionLayer : public Node3D {
  42. GDCLASS(OpenXRCompositionLayer, Node3D);
  43. public:
  44. // Must be identical to Filter enum definition in OpenXRViewportCompositionLayerProvider.
  45. enum Filter {
  46. FILTER_NEAREST,
  47. FILTER_LINEAR,
  48. FILTER_CUBIC,
  49. };
  50. // Must be identical to MipmapMode enum definition in OpenXRViewportCompositionLayerProvider.
  51. enum MipmapMode {
  52. MIPMAP_MODE_DISABLED,
  53. MIPMAP_MODE_NEAREST,
  54. MIPMAP_MODE_LINEAR,
  55. };
  56. // Must be identical to Wrap enum definition in OpenXRViewportCompositionLayerProvider.
  57. enum Wrap {
  58. WRAP_CLAMP_TO_BORDER,
  59. WRAP_CLAMP_TO_EDGE,
  60. WRAP_REPEAT,
  61. WRAP_MIRRORED_REPEAT,
  62. WRAP_MIRROR_CLAMP_TO_EDGE,
  63. };
  64. // Must be identical to Swizzle enum definition in OpenXRViewportCompositionLayerProvider.
  65. enum Swizzle {
  66. SWIZZLE_RED,
  67. SWIZZLE_GREEN,
  68. SWIZZLE_BLUE,
  69. SWIZZLE_ALPHA,
  70. SWIZZLE_ZERO,
  71. SWIZZLE_ONE,
  72. };
  73. private:
  74. XrCompositionLayerBaseHeader *composition_layer_base_header = nullptr;
  75. OpenXRViewportCompositionLayerProvider *openxr_layer_provider = nullptr;
  76. SubViewport *layer_viewport = nullptr;
  77. bool use_android_surface = false;
  78. Size2i android_surface_size = Size2i(1024, 1024);
  79. bool enable_hole_punch = false;
  80. MeshInstance3D *fallback = nullptr;
  81. bool should_update_fallback_mesh = false;
  82. bool openxr_session_running = false;
  83. bool registered = false;
  84. OpenXRViewportCompositionLayerProvider::SwapchainState *swapchain_state = nullptr;
  85. Dictionary extension_property_values;
  86. bool _should_use_fallback_node();
  87. void _create_fallback_node();
  88. void _reset_fallback_material();
  89. void _remove_fallback_node();
  90. void _setup_composition_layer_provider();
  91. void _clear_composition_layer_provider();
  92. protected:
  93. OpenXRAPI *openxr_api = nullptr;
  94. OpenXRCompositionLayerExtension *composition_layer_extension = nullptr;
  95. static void _bind_methods();
  96. void _notification(int p_what);
  97. void _get_property_list(List<PropertyInfo> *p_property_list) const;
  98. bool _get(const StringName &p_property, Variant &r_value) const;
  99. bool _set(const StringName &p_property, const Variant &p_value);
  100. void _validate_property(PropertyInfo &p_property) const;
  101. virtual void _on_openxr_session_begun();
  102. virtual void _on_openxr_session_stopping();
  103. virtual Ref<Mesh> _create_fallback_mesh() = 0;
  104. void update_fallback_mesh();
  105. XrPosef get_openxr_pose();
  106. static Vector<OpenXRCompositionLayer *> composition_layer_nodes;
  107. bool is_viewport_in_use(SubViewport *p_viewport);
  108. OpenXRCompositionLayer(XrCompositionLayerBaseHeader *p_composition_layer);
  109. public:
  110. void set_layer_viewport(SubViewport *p_viewport);
  111. SubViewport *get_layer_viewport() const;
  112. void set_use_android_surface(bool p_use_android_surface);
  113. bool get_use_android_surface() const;
  114. void set_android_surface_size(Size2i p_size);
  115. Size2i get_android_surface_size() const;
  116. void set_enable_hole_punch(bool p_enable);
  117. bool get_enable_hole_punch() const;
  118. void set_sort_order(int p_order);
  119. int get_sort_order() const;
  120. void set_alpha_blend(bool p_alpha_blend);
  121. bool get_alpha_blend() const;
  122. Ref<JavaObject> get_android_surface();
  123. bool is_natively_supported() const;
  124. void set_min_filter(Filter p_mode);
  125. Filter get_min_filter() const;
  126. void set_mag_filter(Filter p_mode);
  127. Filter get_mag_filter() const;
  128. void set_mipmap_mode(MipmapMode p_mode);
  129. MipmapMode get_mipmap_mode() const;
  130. void set_horizontal_wrap(Wrap p_mode);
  131. Wrap get_horizontal_wrap() const;
  132. void set_vertical_wrap(Wrap p_mode);
  133. Wrap get_vertical_wrap() const;
  134. void set_red_swizzle(Swizzle p_mode);
  135. Swizzle get_red_swizzle() const;
  136. void set_green_swizzle(Swizzle p_mode);
  137. Swizzle get_green_swizzle() const;
  138. void set_blue_swizzle(Swizzle p_mode);
  139. Swizzle get_blue_swizzle() const;
  140. void set_alpha_swizzle(Swizzle p_mode);
  141. Swizzle get_alpha_swizzle() const;
  142. void set_max_anisotropy(float p_value);
  143. float get_max_anisotropy() const;
  144. void set_border_color(Color p_color);
  145. Color get_border_color() const;
  146. virtual PackedStringArray get_configuration_warnings() const override;
  147. virtual Vector2 intersects_ray(const Vector3 &p_origin, const Vector3 &p_direction) const;
  148. ~OpenXRCompositionLayer();
  149. };
  150. VARIANT_ENUM_CAST(OpenXRCompositionLayer::Filter)
  151. VARIANT_ENUM_CAST(OpenXRCompositionLayer::MipmapMode)
  152. VARIANT_ENUM_CAST(OpenXRCompositionLayer::Wrap)
  153. VARIANT_ENUM_CAST(OpenXRCompositionLayer::Swizzle)