Debian Codegraph @

modules/openxr/extensions/openxr_composition_layer_extension.h

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