Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/openxr/extensions/openxr_composition_layer_extension.cpp
11353 views
1
/**************************************************************************/
2
/* openxr_composition_layer_extension.cpp */
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
#include "openxr_composition_layer_extension.h"
32
33
#ifdef ANDROID_ENABLED
34
#include <openxr/openxr.h>
35
#include <openxr/openxr_platform.h>
36
#endif
37
38
#include "openxr_fb_update_swapchain_extension.h"
39
#include "platform/android/api/java_class_wrapper.h"
40
#include "servers/rendering/rendering_server_globals.h"
41
42
////////////////////////////////////////////////////////////////////////////
43
// OpenXRCompositionLayerExtension
44
45
OpenXRCompositionLayerExtension *OpenXRCompositionLayerExtension::singleton = nullptr;
46
47
OpenXRCompositionLayerExtension *OpenXRCompositionLayerExtension::get_singleton() {
48
return singleton;
49
}
50
51
OpenXRCompositionLayerExtension::OpenXRCompositionLayerExtension() {
52
singleton = this;
53
}
54
55
OpenXRCompositionLayerExtension::~OpenXRCompositionLayerExtension() {
56
singleton = nullptr;
57
}
58
59
HashMap<String, bool *> OpenXRCompositionLayerExtension::get_requested_extensions() {
60
HashMap<String, bool *> request_extensions;
61
62
request_extensions[XR_KHR_COMPOSITION_LAYER_CYLINDER_EXTENSION_NAME] = &cylinder_ext_available;
63
request_extensions[XR_KHR_COMPOSITION_LAYER_EQUIRECT2_EXTENSION_NAME] = &equirect_ext_available;
64
65
#ifdef ANDROID_ENABLED
66
request_extensions[XR_KHR_ANDROID_SURFACE_SWAPCHAIN_EXTENSION_NAME] = &android_surface_ext_available;
67
#endif
68
69
return request_extensions;
70
}
71
72
void OpenXRCompositionLayerExtension::on_instance_created(const XrInstance p_instance) {
73
#ifdef ANDROID_ENABLED
74
EXT_INIT_XR_FUNC(xrDestroySwapchain);
75
EXT_INIT_XR_FUNC(xrCreateSwapchainAndroidSurfaceKHR);
76
#endif
77
}
78
79
void OpenXRCompositionLayerExtension::on_session_created(const XrSession p_session) {
80
OpenXRAPI::get_singleton()->register_composition_layer_provider(this);
81
}
82
83
void OpenXRCompositionLayerExtension::on_session_destroyed() {
84
OpenXRAPI::get_singleton()->unregister_composition_layer_provider(this);
85
86
#ifdef ANDROID_ENABLED
87
free_queued_android_surface_swapchains();
88
#endif
89
}
90
91
void OpenXRCompositionLayerExtension::on_pre_render() {
92
#ifdef ANDROID_ENABLED
93
free_queued_android_surface_swapchains();
94
#endif
95
96
for (OpenXRViewportCompositionLayerProvider *composition_layer : composition_layers) {
97
composition_layer->on_pre_render();
98
}
99
}
100
101
int OpenXRCompositionLayerExtension::get_composition_layer_count() {
102
return composition_layers.size();
103
}
104
105
XrCompositionLayerBaseHeader *OpenXRCompositionLayerExtension::get_composition_layer(int p_index) {
106
ERR_FAIL_INDEX_V(p_index, composition_layers.size(), nullptr);
107
return composition_layers[p_index]->get_composition_layer();
108
}
109
110
int OpenXRCompositionLayerExtension::get_composition_layer_order(int p_index) {
111
ERR_FAIL_INDEX_V(p_index, composition_layers.size(), 1);
112
return composition_layers[p_index]->get_sort_order();
113
}
114
115
void OpenXRCompositionLayerExtension::register_viewport_composition_layer_provider(OpenXRViewportCompositionLayerProvider *p_composition_layer) {
116
composition_layers.push_back(p_composition_layer);
117
}
118
119
void OpenXRCompositionLayerExtension::unregister_viewport_composition_layer_provider(OpenXRViewportCompositionLayerProvider *p_composition_layer) {
120
composition_layers.erase(p_composition_layer);
121
}
122
123
bool OpenXRCompositionLayerExtension::is_available(XrStructureType p_which) {
124
switch (p_which) {
125
case XR_TYPE_COMPOSITION_LAYER_QUAD: {
126
// Doesn't require an extension.
127
return true;
128
} break;
129
case XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR: {
130
return cylinder_ext_available;
131
} break;
132
case XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR: {
133
return equirect_ext_available;
134
} break;
135
default: {
136
ERR_PRINT(vformat("Unsupported composition layer type: %s", p_which));
137
return false;
138
}
139
}
140
}
141
142
#ifdef ANDROID_ENABLED
143
bool OpenXRCompositionLayerExtension::create_android_surface_swapchain(XrSwapchainCreateInfo *p_info, XrSwapchain *r_swapchain, jobject *r_surface) {
144
if (android_surface_ext_available) {
145
OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
146
ERR_FAIL_NULL_V(openxr_api, false);
147
148
XrResult result = xrCreateSwapchainAndroidSurfaceKHR(openxr_api->get_session(), p_info, r_swapchain, r_surface);
149
if (XR_FAILED(result)) {
150
print_line("OpenXR: Failed to create Android surface swapchain [", openxr_api->get_error_string(result), "]");
151
return false;
152
}
153
154
return true;
155
}
156
157
return false;
158
}
159
160
void OpenXRCompositionLayerExtension::free_android_surface_swapchain(XrSwapchain p_swapchain) {
161
android_surface_swapchain_free_queue.push_back(p_swapchain);
162
}
163
164
void OpenXRCompositionLayerExtension::free_queued_android_surface_swapchains() {
165
for (XrSwapchain swapchain : android_surface_swapchain_free_queue) {
166
xrDestroySwapchain(swapchain);
167
}
168
android_surface_swapchain_free_queue.clear();
169
}
170
#endif
171
172
////////////////////////////////////////////////////////////////////////////
173
// OpenXRViewportCompositionLayerProvider
174
175
OpenXRViewportCompositionLayerProvider::OpenXRViewportCompositionLayerProvider(XrCompositionLayerBaseHeader *p_composition_layer) {
176
composition_layer = p_composition_layer;
177
openxr_api = OpenXRAPI::get_singleton();
178
composition_layer_extension = OpenXRCompositionLayerExtension::get_singleton();
179
}
180
181
OpenXRViewportCompositionLayerProvider::~OpenXRViewportCompositionLayerProvider() {
182
for (OpenXRExtensionWrapper *extension : OpenXRAPI::get_registered_extension_wrappers()) {
183
extension->on_viewport_composition_layer_destroyed(composition_layer);
184
}
185
186
if (use_android_surface) {
187
free_swapchain();
188
} else {
189
// This will reset the viewport and free the swapchain too.
190
set_viewport(RID(), Size2i());
191
}
192
}
193
194
void OpenXRViewportCompositionLayerProvider::set_alpha_blend(bool p_alpha_blend) {
195
if (alpha_blend != p_alpha_blend) {
196
alpha_blend = p_alpha_blend;
197
if (alpha_blend) {
198
composition_layer->layerFlags |= XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT;
199
} else {
200
composition_layer->layerFlags &= ~XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT;
201
}
202
}
203
}
204
205
void OpenXRViewportCompositionLayerProvider::set_viewport(RID p_viewport, Size2i p_size) {
206
ERR_FAIL_COND(use_android_surface);
207
208
RenderingServer *rs = RenderingServer::get_singleton();
209
ERR_FAIL_NULL(rs);
210
211
if (subviewport.viewport != p_viewport) {
212
if (subviewport.viewport.is_valid()) {
213
RID rt = rs->viewport_get_render_target(subviewport.viewport);
214
RSG::texture_storage->render_target_set_override(rt, RID(), RID(), RID(), RID());
215
}
216
217
subviewport.viewport = p_viewport;
218
219
if (subviewport.viewport.is_valid()) {
220
subviewport.viewport_size = p_size;
221
} else {
222
free_swapchain();
223
subviewport.viewport_size = Size2i();
224
}
225
}
226
}
227
228
void OpenXRViewportCompositionLayerProvider::set_use_android_surface(bool p_use_android_surface, Size2i p_size) {
229
#ifdef ANDROID_ENABLED
230
if (p_use_android_surface == use_android_surface) {
231
if (use_android_surface && swapchain_size != p_size) {
232
OpenXRFBUpdateSwapchainExtension *fb_update_swapchain_ext = OpenXRFBUpdateSwapchainExtension::get_singleton();
233
if (fb_update_swapchain_ext && fb_update_swapchain_ext->is_android_ext_enabled()) {
234
swapchain_size = p_size;
235
fb_update_swapchain_ext->update_swapchain_surface_size(android_surface.swapchain, swapchain_size);
236
}
237
}
238
return;
239
}
240
241
use_android_surface = p_use_android_surface;
242
243
if (use_android_surface) {
244
if (!composition_layer_extension->is_android_surface_swapchain_available()) {
245
ERR_PRINT_ONCE("OpenXR: Cannot use Android surface for composition layer because the extension isn't available");
246
}
247
248
if (subviewport.viewport.is_valid()) {
249
set_viewport(RID(), Size2i());
250
}
251
252
swapchain_size = p_size;
253
} else {
254
free_swapchain();
255
}
256
#endif
257
}
258
259
#ifdef ANDROID_ENABLED
260
void OpenXRViewportCompositionLayerProvider::create_android_surface() {
261
ERR_FAIL_COND(android_surface.swapchain != XR_NULL_HANDLE || android_surface.surface.is_valid());
262
ERR_FAIL_COND(!openxr_api || !openxr_api->is_running());
263
264
void *next_pointer = nullptr;
265
for (OpenXRExtensionWrapper *wrapper : openxr_api->get_registered_extension_wrappers()) {
266
void *np = wrapper->set_android_surface_swapchain_create_info_and_get_next_pointer(extension_property_values, next_pointer);
267
if (np != nullptr) {
268
next_pointer = np;
269
}
270
}
271
272
// Check to see if content should be protected.
273
XrSwapchainCreateFlags create_flags = 0;
274
275
if (protected_content) {
276
create_flags = XR_SWAPCHAIN_CREATE_PROTECTED_CONTENT_BIT;
277
}
278
279
// The XR_FB_android_surface_swapchain_create extension mandates that format, sampleCount,
280
// faceCount, arraySize, and mipCount must be zero.
281
XrSwapchainCreateInfo info = {
282
XR_TYPE_SWAPCHAIN_CREATE_INFO, // type
283
next_pointer, // next
284
create_flags, // createFlags
285
XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT | XR_SWAPCHAIN_USAGE_MUTABLE_FORMAT_BIT, // usageFlags
286
0, // format
287
0, // sampleCount
288
(uint32_t)swapchain_size.x, // width
289
(uint32_t)swapchain_size.y, // height
290
0, // faceCount
291
0, // arraySize
292
0, // mipCount
293
};
294
295
jobject surface;
296
composition_layer_extension->create_android_surface_swapchain(&info, &android_surface.swapchain, &surface);
297
298
swapchain_state.dirty = true;
299
300
if (surface) {
301
android_surface.surface.instantiate(JavaClassWrapper::get_singleton()->wrap("android.view.Surface"), surface);
302
}
303
}
304
#endif
305
306
Ref<JavaObject> OpenXRViewportCompositionLayerProvider::get_android_surface() {
307
#ifdef ANDROID_ENABLED
308
if (use_android_surface) {
309
if (android_surface.surface.is_null()) {
310
create_android_surface();
311
}
312
return android_surface.surface;
313
}
314
#endif
315
return Ref<JavaObject>();
316
}
317
318
void OpenXRViewportCompositionLayerProvider::set_extension_property_values(const Dictionary &p_extension_property_values) {
319
extension_property_values = p_extension_property_values;
320
extension_property_values_changed = true;
321
}
322
323
void OpenXRViewportCompositionLayerProvider::on_pre_render() {
324
#ifdef ANDROID_ENABLED
325
if (use_android_surface) {
326
if (android_surface.surface.is_null()) {
327
create_android_surface();
328
}
329
return;
330
}
331
#endif
332
333
RenderingServer *rs = RenderingServer::get_singleton();
334
ERR_FAIL_NULL(rs);
335
336
if (subviewport.viewport.is_valid() && openxr_api && openxr_api->is_running()) {
337
RS::ViewportUpdateMode update_mode = rs->viewport_get_update_mode(subviewport.viewport);
338
if (update_mode == RS::VIEWPORT_UPDATE_ONCE || update_mode == RS::VIEWPORT_UPDATE_ALWAYS) {
339
// Update our XR swapchain
340
if (update_and_acquire_swapchain(update_mode == RS::VIEWPORT_UPDATE_ONCE)) {
341
// Render to our XR swapchain image.
342
RID rt = rs->viewport_get_render_target(subviewport.viewport);
343
RSG::texture_storage->render_target_set_override(rt, get_current_swapchain_texture(), RID(), RID(), RID());
344
}
345
}
346
}
347
348
if (swapchain_state.dirty) {
349
update_swapchain_state();
350
swapchain_state.dirty = false;
351
}
352
}
353
354
XrCompositionLayerBaseHeader *OpenXRViewportCompositionLayerProvider::get_composition_layer() {
355
if (openxr_api == nullptr || composition_layer_extension == nullptr) {
356
// OpenXR not initialized or we're in the editor?
357
return nullptr;
358
}
359
360
if (!composition_layer_extension->is_available(composition_layer->type)) {
361
// Selected type is not supported, ignore our layer.
362
return nullptr;
363
}
364
365
XrSwapchainSubImage subimage = {
366
0, // swapchain // NOLINT(modernize-use-nullptr) - 32-bit uses non-pointer uint64
367
{ { 0, 0 }, { 0, 0 } }, // imageRect
368
0, // imageArrayIndex
369
};
370
update_swapchain_sub_image(subimage);
371
372
if (subimage.swapchain == XR_NULL_HANDLE) {
373
// Don't have a swapchain to display? Ignore our layer.
374
return nullptr;
375
}
376
377
// Update the layer struct for the swapchain.
378
switch (composition_layer->type) {
379
case XR_TYPE_COMPOSITION_LAYER_QUAD: {
380
XrCompositionLayerQuad *quad_layer = (XrCompositionLayerQuad *)composition_layer;
381
quad_layer->space = openxr_api->get_play_space();
382
quad_layer->subImage = subimage;
383
} break;
384
385
case XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR: {
386
XrCompositionLayerCylinderKHR *cylinder_layer = (XrCompositionLayerCylinderKHR *)composition_layer;
387
cylinder_layer->space = openxr_api->get_play_space();
388
cylinder_layer->subImage = subimage;
389
} break;
390
391
case XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR: {
392
XrCompositionLayerEquirect2KHR *equirect_layer = (XrCompositionLayerEquirect2KHR *)composition_layer;
393
equirect_layer->space = openxr_api->get_play_space();
394
equirect_layer->subImage = subimage;
395
} break;
396
397
default: {
398
return nullptr;
399
} break;
400
}
401
402
if (extension_property_values_changed) {
403
extension_property_values_changed = false;
404
405
void *next_pointer = nullptr;
406
for (OpenXRExtensionWrapper *extension : OpenXRAPI::get_registered_extension_wrappers()) {
407
void *np = extension->set_viewport_composition_layer_and_get_next_pointer(composition_layer, extension_property_values, next_pointer);
408
if (np) {
409
next_pointer = np;
410
}
411
}
412
composition_layer->next = next_pointer;
413
}
414
415
return composition_layer;
416
}
417
418
void OpenXRViewportCompositionLayerProvider::update_swapchain_state() {
419
OpenXRFBUpdateSwapchainExtension *fb_update_swapchain_ext = OpenXRFBUpdateSwapchainExtension::get_singleton();
420
if (!fb_update_swapchain_ext) {
421
return;
422
}
423
424
#ifdef ANDROID_ENABLED
425
if (use_android_surface) {
426
if (android_surface.swapchain == XR_NULL_HANDLE) {
427
return;
428
}
429
430
fb_update_swapchain_ext->update_swapchain_state(android_surface.swapchain, &swapchain_state);
431
} else
432
#endif
433
{
434
if (subviewport.swapchain_info.get_swapchain() == XR_NULL_HANDLE) {
435
return;
436
}
437
438
fb_update_swapchain_ext->update_swapchain_state(subviewport.swapchain_info.get_swapchain(), &swapchain_state);
439
}
440
}
441
442
OpenXRViewportCompositionLayerProvider::SwapchainState *OpenXRViewportCompositionLayerProvider::get_swapchain_state() {
443
return &swapchain_state;
444
}
445
446
void OpenXRViewportCompositionLayerProvider::update_swapchain_sub_image(XrSwapchainSubImage &r_subimage) {
447
#ifdef ANDROID_ENABLED
448
if (use_android_surface) {
449
r_subimage.swapchain = android_surface.swapchain;
450
} else
451
#endif
452
{
453
XrSwapchain swapchain = subviewport.swapchain_info.get_swapchain();
454
455
if (swapchain && subviewport.swapchain_info.is_image_acquired()) {
456
subviewport.swapchain_info.release();
457
}
458
459
r_subimage.swapchain = swapchain;
460
}
461
462
r_subimage.imageRect.extent.width = swapchain_size.width;
463
r_subimage.imageRect.extent.height = swapchain_size.height;
464
}
465
466
bool OpenXRViewportCompositionLayerProvider::update_and_acquire_swapchain(bool p_static_image) {
467
ERR_FAIL_COND_V(use_android_surface, false);
468
469
if (openxr_api == nullptr || composition_layer_extension == nullptr) {
470
// OpenXR not initialized or we're in the editor?
471
return false;
472
}
473
if (!composition_layer_extension->is_available(get_openxr_type())) {
474
// Selected type is not supported?
475
return false;
476
}
477
478
// See if our current swapchain is outdated.
479
if (subviewport.swapchain_info.get_swapchain() != XR_NULL_HANDLE) {
480
// If this swap chain, or the previous one, were static, then we can't reuse it.
481
if (swapchain_size == subviewport.viewport_size && !p_static_image && !subviewport.static_image && protected_content == subviewport.swapchain_protected_content) {
482
// We're all good! Just acquire it.
483
// We can ignore should_render here, return will be false.
484
bool should_render = true;
485
return subviewport.swapchain_info.acquire(should_render);
486
}
487
488
subviewport.swapchain_info.queue_free();
489
}
490
491
// Create our new swap chain
492
int64_t swapchain_format = openxr_api->get_color_swapchain_format();
493
const uint32_t sample_count = 1;
494
const uint32_t array_size = 1;
495
XrSwapchainCreateFlags create_flags = 0;
496
if (p_static_image) {
497
create_flags |= XR_SWAPCHAIN_CREATE_STATIC_IMAGE_BIT;
498
}
499
if (protected_content) {
500
create_flags |= XR_SWAPCHAIN_CREATE_PROTECTED_CONTENT_BIT;
501
}
502
if (!subviewport.swapchain_info.create(create_flags, XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT | XR_SWAPCHAIN_USAGE_MUTABLE_FORMAT_BIT, swapchain_format, subviewport.viewport_size.width, subviewport.viewport_size.height, sample_count, array_size)) {
503
swapchain_size = Size2i();
504
return false;
505
}
506
507
swapchain_state.dirty = true;
508
509
// Acquire our image so we can start rendering into it,
510
// we can ignore should_render here, ret will be false.
511
bool should_render = true;
512
bool ret = subviewport.swapchain_info.acquire(should_render);
513
514
swapchain_size = subviewport.viewport_size;
515
subviewport.static_image = p_static_image;
516
subviewport.swapchain_protected_content = protected_content;
517
return ret;
518
}
519
520
void OpenXRViewportCompositionLayerProvider::free_swapchain() {
521
#ifdef ANDROID_ENABLED
522
if (use_android_surface) {
523
if (android_surface.swapchain != XR_NULL_HANDLE) {
524
composition_layer_extension->free_android_surface_swapchain(android_surface.swapchain);
525
526
android_surface.swapchain = XR_NULL_HANDLE;
527
android_surface.surface.unref();
528
}
529
} else
530
#endif
531
{
532
if (subviewport.swapchain_info.get_swapchain() != XR_NULL_HANDLE) {
533
subviewport.swapchain_info.queue_free();
534
}
535
subviewport.static_image = false;
536
}
537
538
swapchain_size = Size2i();
539
}
540
541
RID OpenXRViewportCompositionLayerProvider::get_current_swapchain_texture() {
542
ERR_FAIL_COND_V(use_android_surface, RID());
543
544
if (openxr_api == nullptr) {
545
return RID();
546
}
547
548
return subviewport.swapchain_info.get_image();
549
}
550
551