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
10278 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
class OpenXRViewportCompositionLayerProvider;
47
48
// This extension provides access to composition layers for displaying 2D content through the XR compositor.
49
50
// OpenXRCompositionLayerExtension enables the extensions related to this functionality
51
class OpenXRCompositionLayerExtension : public OpenXRExtensionWrapper {
52
GDCLASS(OpenXRCompositionLayerExtension, OpenXRExtensionWrapper);
53
54
protected:
55
static void _bind_methods() {}
56
57
public:
58
static OpenXRCompositionLayerExtension *get_singleton();
59
60
OpenXRCompositionLayerExtension();
61
virtual ~OpenXRCompositionLayerExtension() override;
62
63
virtual HashMap<String, bool *> get_requested_extensions() override;
64
virtual void on_instance_created(const XrInstance p_instance) override;
65
virtual void on_session_created(const XrSession p_session) override;
66
virtual void on_session_destroyed() override;
67
virtual void on_pre_render() override;
68
69
virtual int get_composition_layer_count() override;
70
virtual XrCompositionLayerBaseHeader *get_composition_layer(int p_index) override;
71
virtual int get_composition_layer_order(int p_index) override;
72
73
void register_viewport_composition_layer_provider(OpenXRViewportCompositionLayerProvider *p_composition_layer);
74
void unregister_viewport_composition_layer_provider(OpenXRViewportCompositionLayerProvider *p_composition_layer);
75
76
bool is_available(XrStructureType p_which);
77
bool is_android_surface_swapchain_available() { return android_surface_ext_available; }
78
79
#ifdef ANDROID_ENABLED
80
bool create_android_surface_swapchain(XrSwapchainCreateInfo *p_info, XrSwapchain *r_swapchain, jobject *r_surface);
81
void free_android_surface_swapchain(XrSwapchain p_swapchain);
82
#endif
83
84
private:
85
static OpenXRCompositionLayerExtension *singleton;
86
87
Vector<OpenXRViewportCompositionLayerProvider *> composition_layers;
88
89
bool cylinder_ext_available = false;
90
bool equirect_ext_available = false;
91
bool android_surface_ext_available = false;
92
93
#ifdef ANDROID_ENABLED
94
Vector<XrSwapchain> android_surface_swapchain_free_queue;
95
void free_queued_android_surface_swapchains();
96
97
EXT_PROTO_XRRESULT_FUNC1(xrDestroySwapchain, (XrSwapchain), swapchain)
98
EXT_PROTO_XRRESULT_FUNC4(xrCreateSwapchainAndroidSurfaceKHR, (XrSession), session, (const XrSwapchainCreateInfo *), info, (XrSwapchain *), swapchain, (jobject *), surface)
99
#endif
100
};
101
102
class OpenXRViewportCompositionLayerProvider {
103
public:
104
// Must be identical to Filter enum definition in OpenXRCompositionLayer.
105
enum Filter {
106
FILTER_NEAREST,
107
FILTER_LINEAR,
108
FILTER_CUBIC,
109
};
110
111
// Must be identical to MipmapMode enum definition in OpenXRCompositionLayer.
112
enum MipmapMode {
113
MIPMAP_MODE_DISABLED,
114
MIPMAP_MODE_NEAREST,
115
MIPMAP_MODE_LINEAR,
116
};
117
118
// Must be identical to Wrap enum definition in OpenXRCompositionLayer.
119
enum Wrap {
120
WRAP_CLAMP_TO_BORDER,
121
WRAP_CLAMP_TO_EDGE,
122
WRAP_REPEAT,
123
WRAP_MIRRORED_REPEAT,
124
WRAP_MIRROR_CLAMP_TO_EDGE,
125
};
126
127
// Must be identical to Swizzle enum definition in OpenXRCompositionLayer.
128
enum Swizzle {
129
SWIZZLE_RED,
130
SWIZZLE_GREEN,
131
SWIZZLE_BLUE,
132
SWIZZLE_ALPHA,
133
SWIZZLE_ZERO,
134
SWIZZLE_ONE,
135
};
136
137
struct SwapchainState {
138
Filter min_filter = Filter::FILTER_LINEAR;
139
Filter mag_filter = Filter::FILTER_LINEAR;
140
MipmapMode mipmap_mode = MipmapMode::MIPMAP_MODE_LINEAR;
141
Wrap horizontal_wrap = Wrap::WRAP_CLAMP_TO_BORDER;
142
Wrap vertical_wrap = Wrap::WRAP_CLAMP_TO_BORDER;
143
Swizzle red_swizzle = Swizzle::SWIZZLE_RED;
144
Swizzle green_swizzle = Swizzle::SWIZZLE_GREEN;
145
Swizzle blue_swizzle = Swizzle::SWIZZLE_BLUE;
146
Swizzle alpha_swizzle = Swizzle::SWIZZLE_ALPHA;
147
float max_anisotropy = 1.0;
148
Color border_color = { 0.0, 0.0, 0.0, 0.0 };
149
bool dirty = false;
150
};
151
152
private:
153
XrCompositionLayerBaseHeader *composition_layer = nullptr;
154
int sort_order = 1;
155
bool alpha_blend = false;
156
Dictionary extension_property_values;
157
bool extension_property_values_changed = true;
158
159
struct {
160
RID viewport;
161
Size2i viewport_size;
162
OpenXRAPI::OpenXRSwapChainInfo swapchain_info;
163
bool static_image = false;
164
} subviewport;
165
166
#ifdef ANDROID_ENABLED
167
struct {
168
XrSwapchain swapchain = XR_NULL_HANDLE;
169
Ref<JavaObject> surface;
170
} android_surface;
171
#endif
172
173
bool use_android_surface = false;
174
Size2i swapchain_size;
175
176
OpenXRAPI *openxr_api = nullptr;
177
OpenXRCompositionLayerExtension *composition_layer_extension = nullptr;
178
179
// Only for SubViewports.
180
bool update_and_acquire_swapchain(bool p_static_image);
181
RID get_current_swapchain_texture();
182
183
void update_swapchain_sub_image(XrSwapchainSubImage &r_swapchain_sub_image);
184
void free_swapchain();
185
186
SwapchainState swapchain_state;
187
188
#ifdef ANDROID_ENABLED
189
void create_android_surface();
190
#endif
191
192
public:
193
XrStructureType get_openxr_type() { return composition_layer->type; }
194
195
void set_sort_order(int p_sort_order) { sort_order = p_sort_order; }
196
int get_sort_order() const { return sort_order; }
197
198
void set_alpha_blend(bool p_alpha_blend);
199
bool get_alpha_blend() const { return alpha_blend; }
200
201
void set_viewport(RID p_viewport, Size2i p_size);
202
RID get_viewport() const { return subviewport.viewport; }
203
204
void set_use_android_surface(bool p_enable, Size2i p_size);
205
bool get_use_android_surface() const { return use_android_surface; }
206
207
Ref<JavaObject> get_android_surface();
208
209
void set_extension_property_values(const Dictionary &p_property_values);
210
211
void on_pre_render();
212
XrCompositionLayerBaseHeader *get_composition_layer();
213
214
void update_swapchain_state();
215
SwapchainState *get_swapchain_state();
216
217
OpenXRViewportCompositionLayerProvider(XrCompositionLayerBaseHeader *p_composition_layer);
218
~OpenXRViewportCompositionLayerProvider();
219
};
220
221