Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/openxr/extensions/openxr_composition_layer_extension.h
21658 views
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
// Must be identical to EyeVisibility enum definition in OpenXRCompositionLayer.
111
enum EyeVisibility {
112
EYE_VISIBILITY_BOTH,
113
EYE_VISIBILITY_LEFT,
114
EYE_VISIBILITY_RIGHT,
115
};
116
117
enum PoseSpace {
118
POSE_WORLD_LOCKED,
119
POSE_HEAD_LOCKED,
120
};
121
122
struct SwapchainState {
123
Filter min_filter = Filter::FILTER_LINEAR;
124
Filter mag_filter = Filter::FILTER_LINEAR;
125
MipmapMode mipmap_mode = MipmapMode::MIPMAP_MODE_LINEAR;
126
Wrap horizontal_wrap = Wrap::WRAP_CLAMP_TO_BORDER;
127
Wrap vertical_wrap = Wrap::WRAP_CLAMP_TO_BORDER;
128
Swizzle red_swizzle = Swizzle::SWIZZLE_RED;
129
Swizzle green_swizzle = Swizzle::SWIZZLE_GREEN;
130
Swizzle blue_swizzle = Swizzle::SWIZZLE_BLUE;
131
Swizzle alpha_swizzle = Swizzle::SWIZZLE_ALPHA;
132
float max_anisotropy = 1.0;
133
Color border_color = { 0.0, 0.0, 0.0, 0.0 };
134
};
135
136
static OpenXRCompositionLayerExtension *get_singleton();
137
138
OpenXRCompositionLayerExtension();
139
virtual ~OpenXRCompositionLayerExtension() override;
140
141
virtual HashMap<String, bool *> get_requested_extensions(XrVersion p_version) override;
142
virtual void on_instance_created(const XrInstance p_instance) override;
143
virtual void on_session_created(const XrSession p_session) override;
144
virtual void on_session_destroyed() override;
145
virtual void on_pre_render() override;
146
147
virtual int get_composition_layer_count() override;
148
virtual XrCompositionLayerBaseHeader *get_composition_layer(int p_index) override;
149
virtual int get_composition_layer_order(int p_index) override;
150
151
// The data on p_openxr_layer will be copied - there is no need to keep it valid after this call.
152
RID composition_layer_create(XrCompositionLayerBaseHeader *p_openxr_layer);
153
void composition_layer_free(RID p_layer);
154
155
void composition_layer_register(RID p_layer);
156
void composition_layer_unregister(RID p_layer);
157
158
OPENXR_LAYER_FUNC2(set_viewport, RID, const Size2i &);
159
OPENXR_LAYER_FUNC2(set_use_android_surface, bool, const Size2i &);
160
OPENXR_LAYER_FUNC1(set_sort_order, int);
161
OPENXR_LAYER_FUNC1(set_alpha_blend, bool);
162
OPENXR_LAYER_FUNC1(set_transform, const Transform3D &);
163
OPENXR_LAYER_FUNC1(set_protected_content, bool);
164
OPENXR_LAYER_FUNC1(set_extension_property_values, Dictionary);
165
166
OPENXR_LAYER_FUNC1(set_min_filter, Filter);
167
OPENXR_LAYER_FUNC1(set_mag_filter, Filter);
168
OPENXR_LAYER_FUNC1(set_mipmap_mode, MipmapMode);
169
OPENXR_LAYER_FUNC1(set_horizontal_wrap, Wrap);
170
OPENXR_LAYER_FUNC1(set_vertical_wrap, Wrap);
171
OPENXR_LAYER_FUNC1(set_red_swizzle, Swizzle);
172
OPENXR_LAYER_FUNC1(set_blue_swizzle, Swizzle);
173
OPENXR_LAYER_FUNC1(set_green_swizzle, Swizzle);
174
OPENXR_LAYER_FUNC1(set_alpha_swizzle, Swizzle);
175
OPENXR_LAYER_FUNC1(set_max_anisotropy, float);
176
OPENXR_LAYER_FUNC1(set_border_color, const Color &);
177
OPENXR_LAYER_FUNC1(set_pose_space, PoseSpace);
178
OPENXR_LAYER_FUNC1(set_eye_visibility, EyeVisibility);
179
180
OPENXR_LAYER_FUNC1(set_quad_size, const Size2 &);
181
182
OPENXR_LAYER_FUNC1(set_cylinder_radius, float);
183
OPENXR_LAYER_FUNC1(set_cylinder_aspect_ratio, float);
184
OPENXR_LAYER_FUNC1(set_cylinder_central_angle, float);
185
186
OPENXR_LAYER_FUNC1(set_equirect_radius, float);
187
OPENXR_LAYER_FUNC1(set_equirect_central_horizontal_angle, float);
188
OPENXR_LAYER_FUNC1(set_equirect_upper_vertical_angle, float);
189
OPENXR_LAYER_FUNC1(set_equirect_lower_vertical_angle, float);
190
191
Ref<JavaObject> composition_layer_get_android_surface(RID p_layer);
192
193
bool is_available(XrStructureType p_which);
194
bool is_android_surface_swapchain_available() { return android_surface_ext_available; }
195
196
private:
197
static OpenXRCompositionLayerExtension *singleton;
198
199
bool cylinder_ext_available = false;
200
bool equirect_ext_available = false;
201
bool android_surface_ext_available = false;
202
203
void _composition_layer_free_rt(RID p_layer);
204
void _composition_layer_register_rt(RID p_layer);
205
void _composition_layer_unregister_rt(RID p_layer);
206
207
#ifdef ANDROID_ENABLED
208
bool create_android_surface_swapchain(XrSwapchainCreateInfo *p_info, XrSwapchain *r_swapchain, jobject *r_surface);
209
210
EXT_PROTO_XRRESULT_FUNC1(xrDestroySwapchain, (XrSwapchain), swapchain)
211
EXT_PROTO_XRRESULT_FUNC4(xrCreateSwapchainAndroidSurfaceKHR, (XrSession), session, (const XrSwapchainCreateInfo *), info, (XrSwapchain *), swapchain, (jobject *), surface)
212
#endif
213
214
struct CompositionLayer {
215
union {
216
XrCompositionLayerBaseHeader composition_layer;
217
XrCompositionLayerQuad composition_layer_quad;
218
XrCompositionLayerCylinderKHR composition_layer_cylinder;
219
XrCompositionLayerEquirect2KHR composition_layer_equirect;
220
};
221
222
int sort_order = 1;
223
bool alpha_blend = false;
224
Dictionary extension_property_values;
225
bool extension_property_values_changed = true;
226
227
struct {
228
RID viewport;
229
Size2i viewport_size;
230
OpenXRAPI::OpenXRSwapChainInfo swapchain_info;
231
bool static_image = false;
232
bool swapchain_protected_content = false;
233
} subviewport;
234
235
#ifdef ANDROID_ENABLED
236
struct {
237
XrSwapchain swapchain = XR_NULL_HANDLE;
238
Ref<JavaObject> surface;
239
} android_surface;
240
#endif
241
242
PoseSpace pose_space = POSE_WORLD_LOCKED;
243
XrSpace layer_reference_space = XR_NULL_HANDLE;
244
245
bool use_android_surface = false;
246
bool protected_content = false;
247
Size2i swapchain_size;
248
249
SwapchainState swapchain_state;
250
bool swapchain_state_is_dirty = false;
251
252
void set_viewport(RID p_viewport, const Size2i &p_size);
253
void set_use_android_surface(bool p_use_android_surface, const Size2i &p_size);
254
255
void set_sort_order(int p_sort_order) { sort_order = p_sort_order; }
256
void set_alpha_blend(bool p_alpha_blend);
257
void set_protected_content(bool p_protected_content) { protected_content = p_protected_content; }
258
void set_transform(const Transform3D &p_transform);
259
void set_extension_property_values(const Dictionary &p_extension_property_values);
260
261
void set_min_filter(Filter p_mode);
262
void set_mag_filter(Filter p_mode);
263
void set_mipmap_mode(MipmapMode p_mode);
264
void set_horizontal_wrap(Wrap p_mode);
265
void set_vertical_wrap(Wrap p_mode);
266
void set_red_swizzle(Swizzle p_mode);
267
void set_green_swizzle(Swizzle p_mode);
268
void set_blue_swizzle(Swizzle p_mode);
269
void set_alpha_swizzle(Swizzle p_mode);
270
void set_max_anisotropy(float p_value);
271
void set_border_color(const Color &p_color);
272
void set_pose_space(PoseSpace p_pose_space);
273
void set_eye_visibility(EyeVisibility p_eye_visibility);
274
275
void set_quad_size(const Size2 &p_size);
276
277
void set_cylinder_radius(float p_radius);
278
void set_cylinder_aspect_ratio(float p_aspect_ratio);
279
void set_cylinder_central_angle(float p_central_angle);
280
281
void set_equirect_radius(float p_radius);
282
void set_equirect_central_horizontal_angle(float p_angle);
283
void set_equirect_upper_vertical_angle(float p_angle);
284
void set_equirect_lower_vertical_angle(float p_angle);
285
286
Ref<JavaObject> get_android_surface();
287
void on_pre_render();
288
XrCompositionLayerBaseHeader *get_composition_layer();
289
void free();
290
291
private:
292
void update_swapchain_state();
293
void update_swapchain_sub_image(XrSwapchainSubImage &r_subimage);
294
bool update_and_acquire_swapchain(bool p_static_image);
295
RID get_current_swapchain_texture();
296
void free_swapchain();
297
298
#ifdef ANDROID_ENABLED
299
void create_android_surface();
300
#endif
301
};
302
303
Mutex composition_layer_mutex;
304
RID_Owner<CompositionLayer, true> composition_layer_owner;
305
LocalVector<CompositionLayer *> registered_composition_layers;
306
};
307
308
VARIANT_ENUM_CAST(OpenXRCompositionLayerExtension::Filter);
309
VARIANT_ENUM_CAST(OpenXRCompositionLayerExtension::MipmapMode);
310
VARIANT_ENUM_CAST(OpenXRCompositionLayerExtension::Wrap);
311
VARIANT_ENUM_CAST(OpenXRCompositionLayerExtension::Swizzle);
312
VARIANT_ENUM_CAST(OpenXRCompositionLayerExtension::PoseSpace);
313
VARIANT_ENUM_CAST(OpenXRCompositionLayerExtension::EyeVisibility);
314
315
#undef OPENXR_LAYER_FUNC1
316
#undef OPENXR_LAYER_FUNC2
317
318