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
20969 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(XrVersion p_version) {
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
if (android_surface_ext_available) {
76
EXT_INIT_XR_FUNC(xrCreateSwapchainAndroidSurfaceKHR);
77
}
78
#endif
79
}
80
81
void OpenXRCompositionLayerExtension::on_session_created(const XrSession p_session) {
82
OpenXRAPI::get_singleton()->register_composition_layer_provider(this);
83
}
84
85
void OpenXRCompositionLayerExtension::on_session_destroyed() {
86
OpenXRAPI::get_singleton()->unregister_composition_layer_provider(this);
87
}
88
89
void OpenXRCompositionLayerExtension::on_pre_render() {
90
for (CompositionLayer *composition_layer : registered_composition_layers) {
91
composition_layer->on_pre_render();
92
}
93
}
94
95
int OpenXRCompositionLayerExtension::get_composition_layer_count() {
96
return registered_composition_layers.size();
97
}
98
99
XrCompositionLayerBaseHeader *OpenXRCompositionLayerExtension::get_composition_layer(int p_index) {
100
ERR_FAIL_UNSIGNED_INDEX_V((unsigned int)p_index, registered_composition_layers.size(), nullptr);
101
return registered_composition_layers[p_index]->get_composition_layer();
102
}
103
104
int OpenXRCompositionLayerExtension::get_composition_layer_order(int p_index) {
105
ERR_FAIL_UNSIGNED_INDEX_V((unsigned int)p_index, registered_composition_layers.size(), 1);
106
return registered_composition_layers[p_index]->sort_order;
107
}
108
109
RID OpenXRCompositionLayerExtension::composition_layer_create(XrCompositionLayerBaseHeader *p_openxr_layer) {
110
RID rid = composition_layer_owner.make_rid();
111
CompositionLayer *layer = composition_layer_owner.get_or_null(rid);
112
113
switch (p_openxr_layer->type) {
114
case XR_TYPE_COMPOSITION_LAYER_QUAD: {
115
layer->composition_layer_quad = *(XrCompositionLayerQuad *)p_openxr_layer;
116
} break;
117
case XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR: {
118
layer->composition_layer_cylinder = *(XrCompositionLayerCylinderKHR *)p_openxr_layer;
119
} break;
120
case XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR: {
121
layer->composition_layer_equirect = *(XrCompositionLayerEquirect2KHR *)p_openxr_layer;
122
} break;
123
default: {
124
ERR_PRINT(vformat("Invalid OpenXR composition layer type: %s", p_openxr_layer->type));
125
composition_layer_owner.free(rid);
126
return RID();
127
}
128
}
129
130
return rid;
131
}
132
133
void OpenXRCompositionLayerExtension::composition_layer_free(RID p_layer) {
134
RenderingServer::get_singleton()->call_on_render_thread(callable_mp(this, &OpenXRCompositionLayerExtension::_composition_layer_free_rt).bind(p_layer));
135
}
136
137
void OpenXRCompositionLayerExtension::composition_layer_register(RID p_layer) {
138
RenderingServer::get_singleton()->call_on_render_thread(callable_mp(this, &OpenXRCompositionLayerExtension::_composition_layer_register_rt).bind(p_layer));
139
}
140
141
void OpenXRCompositionLayerExtension::composition_layer_unregister(RID p_layer) {
142
RenderingServer::get_singleton()->call_on_render_thread(callable_mp(this, &OpenXRCompositionLayerExtension::_composition_layer_unregister_rt).bind(p_layer));
143
}
144
145
Ref<JavaObject> OpenXRCompositionLayerExtension::composition_layer_get_android_surface(RID p_layer) {
146
MutexLock lock(composition_layer_mutex);
147
CompositionLayer *layer = composition_layer_owner.get_or_null(p_layer);
148
ERR_FAIL_NULL_V(layer, Ref<JavaObject>());
149
return layer->get_android_surface();
150
}
151
152
void OpenXRCompositionLayerExtension::_composition_layer_free_rt(RID p_layer) {
153
_composition_layer_unregister_rt(p_layer);
154
155
MutexLock lock(composition_layer_mutex);
156
CompositionLayer *layer = composition_layer_owner.get_or_null(p_layer);
157
if (layer) {
158
for (OpenXRExtensionWrapper *extension : OpenXRAPI::get_registered_extension_wrappers()) {
159
extension->on_viewport_composition_layer_destroyed(&layer->composition_layer);
160
}
161
layer->free();
162
}
163
164
composition_layer_owner.free(p_layer);
165
}
166
167
void OpenXRCompositionLayerExtension::_composition_layer_register_rt(RID p_layer) {
168
CompositionLayer *layer = composition_layer_owner.get_or_null(p_layer);
169
ERR_FAIL_NULL(layer);
170
registered_composition_layers.push_back(layer);
171
}
172
173
void OpenXRCompositionLayerExtension::_composition_layer_unregister_rt(RID p_layer) {
174
CompositionLayer *layer = composition_layer_owner.get_or_null(p_layer);
175
ERR_FAIL_NULL(layer);
176
registered_composition_layers.erase(layer);
177
}
178
179
bool OpenXRCompositionLayerExtension::is_available(XrStructureType p_which) {
180
switch (p_which) {
181
case XR_TYPE_COMPOSITION_LAYER_QUAD: {
182
// Doesn't require an extension.
183
return true;
184
} break;
185
case XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR: {
186
return cylinder_ext_available;
187
} break;
188
case XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR: {
189
return equirect_ext_available;
190
} break;
191
default: {
192
ERR_PRINT(vformat("Unsupported composition layer type: %s", p_which));
193
return false;
194
}
195
}
196
}
197
198
#ifdef ANDROID_ENABLED
199
bool OpenXRCompositionLayerExtension::create_android_surface_swapchain(XrSwapchainCreateInfo *p_info, XrSwapchain *r_swapchain, jobject *r_surface) {
200
if (android_surface_ext_available) {
201
OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
202
ERR_FAIL_NULL_V(openxr_api, false);
203
204
XrResult result = xrCreateSwapchainAndroidSurfaceKHR(openxr_api->get_session(), p_info, r_swapchain, r_surface);
205
if (XR_FAILED(result)) {
206
print_line("OpenXR: Failed to create Android surface swapchain [", openxr_api->get_error_string(result), "]");
207
return false;
208
}
209
210
return true;
211
}
212
213
return false;
214
}
215
#endif
216
217
////////////////////////////////////////////////////////////////////////////
218
// OpenXRCompositionLayerExtension::CompositionLayer
219
220
void OpenXRCompositionLayerExtension::CompositionLayer::set_viewport(RID p_viewport, const Size2i &p_size) {
221
ERR_FAIL_COND(use_android_surface);
222
223
if (subviewport.viewport != p_viewport) {
224
if (subviewport.viewport.is_valid()) {
225
RID rt = RenderingServer::get_singleton()->viewport_get_render_target(subviewport.viewport);
226
RSG::texture_storage->render_target_set_override(rt, RID(), RID(), RID(), RID());
227
}
228
229
subviewport.viewport = p_viewport;
230
231
if (subviewport.viewport.is_valid()) {
232
subviewport.viewport_size = p_size;
233
} else {
234
free_swapchain();
235
subviewport.viewport_size = Size2i();
236
}
237
} else if (subviewport.viewport_size != p_size) {
238
subviewport.viewport_size = p_size;
239
}
240
}
241
242
void OpenXRCompositionLayerExtension::CompositionLayer::set_use_android_surface(bool p_use_android_surface, const Size2i &p_size) {
243
#ifdef ANDROID_ENABLED
244
if (p_use_android_surface == use_android_surface) {
245
if (use_android_surface && swapchain_size != p_size) {
246
OpenXRFBUpdateSwapchainExtension *fb_update_swapchain_ext = OpenXRFBUpdateSwapchainExtension::get_singleton();
247
if (fb_update_swapchain_ext && fb_update_swapchain_ext->is_android_ext_enabled()) {
248
swapchain_size = p_size;
249
fb_update_swapchain_ext->update_swapchain_surface_size(android_surface.swapchain, swapchain_size);
250
}
251
}
252
return;
253
}
254
255
use_android_surface = p_use_android_surface;
256
257
if (use_android_surface) {
258
if (!OpenXRCompositionLayerExtension::get_singleton()->is_android_surface_swapchain_available()) {
259
ERR_PRINT_ONCE("OpenXR: Cannot use Android surface for composition layer because the extension isn't available");
260
}
261
262
if (subviewport.viewport.is_valid()) {
263
set_viewport(RID(), Size2i());
264
}
265
266
swapchain_size = p_size;
267
} else {
268
free_swapchain();
269
}
270
#endif
271
}
272
273
void OpenXRCompositionLayerExtension::CompositionLayer::set_alpha_blend(bool p_alpha_blend) {
274
if (alpha_blend != p_alpha_blend) {
275
alpha_blend = p_alpha_blend;
276
if (alpha_blend) {
277
composition_layer.layerFlags |= XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT;
278
} else {
279
composition_layer.layerFlags &= ~XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT;
280
}
281
}
282
}
283
284
void OpenXRCompositionLayerExtension::CompositionLayer::set_transform(const Transform3D &p_transform) {
285
Transform3D xf;
286
287
if (pose_space == POSE_HEAD_LOCKED) {
288
// Local transform relative to the head/camera.
289
xf = p_transform;
290
} else {
291
// Relative to the XROrigin3D, so we need to apply the reference frame.
292
Transform3D reference_frame = XRServer::get_singleton()->get_reference_frame();
293
xf = reference_frame.inverse() * p_transform;
294
}
295
296
Quaternion quat(xf.basis.orthonormalized());
297
298
// Prevent invalid quaternion
299
if (Math::is_zero_approx(quat.length())) {
300
quat = Quaternion(); // identity quaternion
301
}
302
303
XrPosef pose = {
304
{ (float)quat.x, (float)quat.y, (float)quat.z, (float)quat.w },
305
{ (float)xf.origin.x, (float)xf.origin.y, (float)xf.origin.z }
306
};
307
308
switch (composition_layer.type) {
309
case XR_TYPE_COMPOSITION_LAYER_QUAD: {
310
composition_layer_quad.pose = pose;
311
} break;
312
313
case XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR: {
314
composition_layer_cylinder.pose = pose;
315
} break;
316
317
case XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR: {
318
composition_layer_equirect.pose = pose;
319
} break;
320
321
default: {
322
ERR_PRINT(vformat("Cannot set transform on unsupported composition layer type: %s", composition_layer.type));
323
}
324
}
325
}
326
327
void OpenXRCompositionLayerExtension::CompositionLayer::set_extension_property_values(const Dictionary &p_property_values) {
328
extension_property_values = p_property_values;
329
extension_property_values_changed = true;
330
}
331
332
void OpenXRCompositionLayerExtension::CompositionLayer::set_min_filter(Filter p_mode) {
333
swapchain_state.min_filter = p_mode;
334
swapchain_state_is_dirty = true;
335
}
336
337
void OpenXRCompositionLayerExtension::CompositionLayer::set_mag_filter(Filter p_mode) {
338
swapchain_state.mag_filter = p_mode;
339
swapchain_state_is_dirty = true;
340
}
341
342
void OpenXRCompositionLayerExtension::CompositionLayer::set_mipmap_mode(MipmapMode p_mode) {
343
swapchain_state.mipmap_mode = p_mode;
344
swapchain_state_is_dirty = true;
345
}
346
347
void OpenXRCompositionLayerExtension::CompositionLayer::set_horizontal_wrap(Wrap p_mode) {
348
swapchain_state.horizontal_wrap = p_mode;
349
swapchain_state_is_dirty = true;
350
}
351
352
void OpenXRCompositionLayerExtension::CompositionLayer::set_vertical_wrap(Wrap p_mode) {
353
swapchain_state.vertical_wrap = p_mode;
354
swapchain_state_is_dirty = true;
355
}
356
357
void OpenXRCompositionLayerExtension::CompositionLayer::set_red_swizzle(Swizzle p_mode) {
358
swapchain_state.red_swizzle = p_mode;
359
swapchain_state_is_dirty = true;
360
}
361
362
void OpenXRCompositionLayerExtension::CompositionLayer::set_green_swizzle(Swizzle p_mode) {
363
swapchain_state.green_swizzle = p_mode;
364
swapchain_state_is_dirty = true;
365
}
366
367
void OpenXRCompositionLayerExtension::CompositionLayer::set_blue_swizzle(Swizzle p_mode) {
368
swapchain_state.blue_swizzle = p_mode;
369
swapchain_state_is_dirty = true;
370
}
371
372
void OpenXRCompositionLayerExtension::CompositionLayer::set_alpha_swizzle(Swizzle p_mode) {
373
swapchain_state.alpha_swizzle = p_mode;
374
swapchain_state_is_dirty = true;
375
}
376
377
void OpenXRCompositionLayerExtension::CompositionLayer::set_max_anisotropy(float p_value) {
378
swapchain_state.max_anisotropy = p_value;
379
swapchain_state_is_dirty = true;
380
}
381
382
void OpenXRCompositionLayerExtension::CompositionLayer::set_border_color(const Color &p_color) {
383
swapchain_state.border_color = p_color;
384
swapchain_state_is_dirty = true;
385
}
386
387
void OpenXRCompositionLayerExtension::CompositionLayer::set_pose_space(PoseSpace p_pose_space) {
388
pose_space = p_pose_space;
389
}
390
391
void OpenXRCompositionLayerExtension::CompositionLayer::set_eye_visibility(EyeVisibility p_eye_visibility) {
392
XrEyeVisibility eye_visibility;
393
394
switch (p_eye_visibility) {
395
case EYE_VISIBILITY_BOTH: {
396
eye_visibility = XR_EYE_VISIBILITY_BOTH;
397
} break;
398
399
case EYE_VISIBILITY_LEFT: {
400
eye_visibility = XR_EYE_VISIBILITY_LEFT;
401
} break;
402
403
case EYE_VISIBILITY_RIGHT: {
404
eye_visibility = XR_EYE_VISIBILITY_RIGHT;
405
} break;
406
407
default: {
408
eye_visibility = XR_EYE_VISIBILITY_BOTH;
409
}
410
}
411
412
switch (composition_layer.type) {
413
case XR_TYPE_COMPOSITION_LAYER_QUAD: {
414
composition_layer_quad.eyeVisibility = eye_visibility;
415
} break;
416
417
case XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR: {
418
composition_layer_cylinder.eyeVisibility = eye_visibility;
419
} break;
420
421
case XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR: {
422
composition_layer_equirect.eyeVisibility = eye_visibility;
423
} break;
424
425
default: {
426
ERR_PRINT(vformat("%s does not support setting eye visibility.", composition_layer.type));
427
}
428
}
429
}
430
431
void OpenXRCompositionLayerExtension::CompositionLayer::set_quad_size(const Size2 &p_size) {
432
ERR_FAIL_COND(composition_layer.type != XR_TYPE_COMPOSITION_LAYER_QUAD);
433
composition_layer_quad.size = { (float)p_size.x, (float)p_size.y };
434
}
435
436
void OpenXRCompositionLayerExtension::CompositionLayer::set_cylinder_radius(float p_radius) {
437
ERR_FAIL_COND(composition_layer.type != XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR);
438
composition_layer_cylinder.radius = p_radius;
439
}
440
441
void OpenXRCompositionLayerExtension::CompositionLayer::set_cylinder_aspect_ratio(float p_aspect_ratio) {
442
ERR_FAIL_COND(composition_layer.type != XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR);
443
composition_layer_cylinder.aspectRatio = p_aspect_ratio;
444
}
445
446
void OpenXRCompositionLayerExtension::CompositionLayer::set_cylinder_central_angle(float p_central_angle) {
447
ERR_FAIL_COND(composition_layer.type != XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR);
448
composition_layer_cylinder.centralAngle = p_central_angle;
449
}
450
451
void OpenXRCompositionLayerExtension::CompositionLayer::set_equirect_radius(float p_radius) {
452
ERR_FAIL_COND(composition_layer.type != XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR);
453
composition_layer_equirect.radius = p_radius;
454
}
455
456
void OpenXRCompositionLayerExtension::CompositionLayer::set_equirect_central_horizontal_angle(float p_angle) {
457
ERR_FAIL_COND(composition_layer.type != XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR);
458
composition_layer_equirect.centralHorizontalAngle = p_angle;
459
}
460
461
void OpenXRCompositionLayerExtension::CompositionLayer::set_equirect_upper_vertical_angle(float p_angle) {
462
ERR_FAIL_COND(composition_layer.type != XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR);
463
composition_layer_equirect.upperVerticalAngle = p_angle;
464
}
465
466
void OpenXRCompositionLayerExtension::CompositionLayer::set_equirect_lower_vertical_angle(float p_angle) {
467
ERR_FAIL_COND(composition_layer.type != XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR);
468
composition_layer_equirect.lowerVerticalAngle = p_angle;
469
}
470
471
Ref<JavaObject> OpenXRCompositionLayerExtension::CompositionLayer::get_android_surface() {
472
#ifdef ANDROID_ENABLED
473
if (use_android_surface) {
474
MutexLock lock(OpenXRCompositionLayerExtension::get_singleton()->composition_layer_mutex);
475
if (android_surface.surface.is_null()) {
476
create_android_surface();
477
}
478
return android_surface.surface;
479
}
480
#endif
481
return Ref<JavaObject>();
482
}
483
484
void OpenXRCompositionLayerExtension::CompositionLayer::on_pre_render() {
485
#ifdef ANDROID_ENABLED
486
if (use_android_surface) {
487
MutexLock lock(OpenXRCompositionLayerExtension::get_singleton()->composition_layer_mutex);
488
if (android_surface.surface.is_null()) {
489
create_android_surface();
490
}
491
return;
492
}
493
#endif
494
495
RenderingServer *rs = RenderingServer::get_singleton();
496
OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
497
498
if (subviewport.viewport.is_valid() && openxr_api && openxr_api->is_running()) {
499
RS::ViewportUpdateMode update_mode = rs->viewport_get_update_mode(subviewport.viewport);
500
if (update_mode == RS::VIEWPORT_UPDATE_ONCE || update_mode == RS::VIEWPORT_UPDATE_ALWAYS) {
501
// Update our XR swapchain
502
if (update_and_acquire_swapchain(update_mode == RS::VIEWPORT_UPDATE_ONCE)) {
503
// Render to our XR swapchain image.
504
RID rt = rs->viewport_get_render_target(subviewport.viewport);
505
RSG::texture_storage->render_target_set_override(rt, get_current_swapchain_texture(), RID(), RID(), RID());
506
}
507
}
508
}
509
510
if (swapchain_state_is_dirty) {
511
update_swapchain_state();
512
swapchain_state_is_dirty = false;
513
}
514
}
515
516
XrCompositionLayerBaseHeader *OpenXRCompositionLayerExtension::CompositionLayer::get_composition_layer() {
517
OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
518
OpenXRCompositionLayerExtension *composition_layer_extension = OpenXRCompositionLayerExtension::get_singleton();
519
520
if (openxr_api == nullptr || composition_layer_extension == nullptr) {
521
// OpenXR not initialized or we're in the editor?
522
return nullptr;
523
}
524
525
if (!composition_layer_extension->is_available(composition_layer.type)) {
526
// Selected type is not supported, ignore our layer.
527
return nullptr;
528
}
529
530
XrSwapchainSubImage subimage = {
531
0, // swapchain // NOLINT(modernize-use-nullptr) - 32-bit uses non-pointer uint64
532
{ { 0, 0 }, { 0, 0 } }, // imageRect
533
0, // imageArrayIndex
534
};
535
update_swapchain_sub_image(subimage);
536
537
if (subimage.swapchain == XR_NULL_HANDLE) {
538
// Don't have a swapchain to display? Ignore our layer.
539
return nullptr;
540
}
541
542
// Update the layer's reference space
543
switch (pose_space) {
544
case POSE_WORLD_LOCKED: {
545
layer_reference_space = openxr_api->get_play_space();
546
break;
547
}
548
549
case POSE_HEAD_LOCKED: {
550
layer_reference_space = openxr_api->get_view_space();
551
break;
552
}
553
default: {
554
return nullptr;
555
}
556
}
557
558
// Update the layer struct for the swapchain.
559
switch (composition_layer.type) {
560
case XR_TYPE_COMPOSITION_LAYER_QUAD: {
561
composition_layer_quad.space = layer_reference_space;
562
composition_layer_quad.subImage = subimage;
563
} break;
564
565
case XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR: {
566
composition_layer_cylinder.space = layer_reference_space;
567
composition_layer_cylinder.subImage = subimage;
568
} break;
569
570
case XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR: {
571
composition_layer_equirect.space = layer_reference_space;
572
composition_layer_equirect.subImage = subimage;
573
} break;
574
575
default: {
576
return nullptr;
577
}
578
}
579
580
if (extension_property_values_changed) {
581
extension_property_values_changed = false;
582
583
void *next_pointer = nullptr;
584
for (OpenXRExtensionWrapper *extension : OpenXRAPI::get_registered_extension_wrappers()) {
585
void *np = extension->set_viewport_composition_layer_and_get_next_pointer(&composition_layer, extension_property_values, next_pointer);
586
if (np) {
587
next_pointer = np;
588
}
589
}
590
composition_layer.next = next_pointer;
591
}
592
593
return &composition_layer;
594
}
595
596
void OpenXRCompositionLayerExtension::CompositionLayer::free() {
597
if (use_android_surface) {
598
free_swapchain();
599
} else {
600
// This will reset the viewport and free the swapchain too.
601
set_viewport(RID(), Size2i());
602
}
603
}
604
605
void OpenXRCompositionLayerExtension::CompositionLayer::update_swapchain_state() {
606
OpenXRFBUpdateSwapchainExtension *fb_update_swapchain_ext = OpenXRFBUpdateSwapchainExtension::get_singleton();
607
if (!fb_update_swapchain_ext) {
608
return;
609
}
610
611
#ifdef ANDROID_ENABLED
612
if (use_android_surface) {
613
if (android_surface.swapchain == XR_NULL_HANDLE) {
614
return;
615
}
616
617
fb_update_swapchain_ext->update_swapchain_state(android_surface.swapchain, &swapchain_state);
618
} else
619
#endif
620
{
621
if (subviewport.swapchain_info.get_swapchain() == XR_NULL_HANDLE) {
622
return;
623
}
624
625
fb_update_swapchain_ext->update_swapchain_state(subviewport.swapchain_info.get_swapchain(), &swapchain_state);
626
}
627
}
628
629
void OpenXRCompositionLayerExtension::CompositionLayer::update_swapchain_sub_image(XrSwapchainSubImage &r_subimage) {
630
#ifdef ANDROID_ENABLED
631
if (use_android_surface) {
632
r_subimage.swapchain = android_surface.swapchain;
633
} else
634
#endif
635
{
636
XrSwapchain swapchain = subviewport.swapchain_info.get_swapchain();
637
638
if (swapchain && subviewport.swapchain_info.is_image_acquired()) {
639
subviewport.swapchain_info.release();
640
}
641
642
r_subimage.swapchain = swapchain;
643
}
644
645
r_subimage.imageRect.extent.width = swapchain_size.width;
646
r_subimage.imageRect.extent.height = swapchain_size.height;
647
}
648
649
bool OpenXRCompositionLayerExtension::CompositionLayer::update_and_acquire_swapchain(bool p_static_image) {
650
ERR_FAIL_COND_V(use_android_surface, false);
651
652
OpenXRCompositionLayerExtension *composition_layer_extension = OpenXRCompositionLayerExtension::get_singleton();
653
OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
654
655
if (openxr_api == nullptr || composition_layer_extension == nullptr) {
656
// OpenXR not initialized or we're in the editor?
657
return false;
658
}
659
if (!composition_layer_extension->is_available(composition_layer.type)) {
660
// Selected type is not supported?
661
return false;
662
}
663
664
// See if our current swapchain is outdated.
665
if (subviewport.swapchain_info.get_swapchain() != XR_NULL_HANDLE) {
666
// If this swap chain, or the previous one, were static, then we can't reuse it.
667
if (swapchain_size == subviewport.viewport_size && !p_static_image && !subviewport.static_image && protected_content == subviewport.swapchain_protected_content) {
668
// We're all good! Just acquire it.
669
// We can ignore should_render here, return will be false.
670
bool should_render = true;
671
return subviewport.swapchain_info.acquire(should_render);
672
}
673
674
subviewport.swapchain_info.queue_free();
675
}
676
677
// Create our new swap chain
678
int64_t swapchain_format = openxr_api->get_color_swapchain_format();
679
const uint32_t sample_count = 1;
680
const uint32_t array_size = 1;
681
XrSwapchainCreateFlags create_flags = 0;
682
if (p_static_image) {
683
create_flags |= XR_SWAPCHAIN_CREATE_STATIC_IMAGE_BIT;
684
}
685
if (protected_content) {
686
create_flags |= XR_SWAPCHAIN_CREATE_PROTECTED_CONTENT_BIT;
687
}
688
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)) {
689
swapchain_size = Size2i();
690
return false;
691
}
692
693
swapchain_state_is_dirty = true;
694
695
// Acquire our image so we can start rendering into it,
696
// we can ignore should_render here, ret will be false.
697
bool should_render = true;
698
bool ret = subviewport.swapchain_info.acquire(should_render);
699
700
swapchain_size = subviewport.viewport_size;
701
subviewport.static_image = p_static_image;
702
subviewport.swapchain_protected_content = protected_content;
703
return ret;
704
}
705
706
RID OpenXRCompositionLayerExtension::CompositionLayer::get_current_swapchain_texture() {
707
ERR_FAIL_COND_V(use_android_surface, RID());
708
709
if (OpenXRAPI::get_singleton() == nullptr) {
710
return RID();
711
}
712
713
return subviewport.swapchain_info.get_image();
714
}
715
716
void OpenXRCompositionLayerExtension::CompositionLayer::free_swapchain() {
717
#ifdef ANDROID_ENABLED
718
if (use_android_surface) {
719
if (android_surface.swapchain != XR_NULL_HANDLE) {
720
OpenXRCompositionLayerExtension::get_singleton()->xrDestroySwapchain(android_surface.swapchain);
721
android_surface.swapchain = XR_NULL_HANDLE;
722
android_surface.surface.unref();
723
}
724
} else
725
#endif
726
{
727
if (subviewport.swapchain_info.get_swapchain() != XR_NULL_HANDLE) {
728
subviewport.swapchain_info.queue_free();
729
}
730
subviewport.static_image = false;
731
}
732
733
swapchain_size = Size2i();
734
}
735
736
#ifdef ANDROID_ENABLED
737
void OpenXRCompositionLayerExtension::CompositionLayer::create_android_surface() {
738
ERR_FAIL_COND(android_surface.swapchain != XR_NULL_HANDLE || android_surface.surface.is_valid());
739
740
void *next_pointer = nullptr;
741
for (OpenXRExtensionWrapper *wrapper : OpenXRAPI::get_registered_extension_wrappers()) {
742
void *np = wrapper->set_android_surface_swapchain_create_info_and_get_next_pointer(extension_property_values, next_pointer);
743
if (np != nullptr) {
744
next_pointer = np;
745
}
746
}
747
748
// Check to see if content should be protected.
749
XrSwapchainCreateFlags create_flags = 0;
750
751
if (protected_content) {
752
create_flags = XR_SWAPCHAIN_CREATE_PROTECTED_CONTENT_BIT;
753
}
754
755
// The XR_FB_android_surface_swapchain_create extension mandates that format, sampleCount,
756
// faceCount, arraySize, and mipCount must be zero.
757
XrSwapchainCreateInfo info = {
758
XR_TYPE_SWAPCHAIN_CREATE_INFO, // type
759
next_pointer, // next
760
create_flags, // createFlags
761
XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT | XR_SWAPCHAIN_USAGE_MUTABLE_FORMAT_BIT, // usageFlags
762
0, // format
763
0, // sampleCount
764
(uint32_t)swapchain_size.x, // width
765
(uint32_t)swapchain_size.y, // height
766
0, // faceCount
767
0, // arraySize
768
0, // mipCount
769
};
770
771
jobject surface;
772
OpenXRCompositionLayerExtension::get_singleton()->create_android_surface_swapchain(&info, &android_surface.swapchain, &surface);
773
774
swapchain_state_is_dirty = true;
775
776
if (surface) {
777
android_surface.surface.instantiate(JavaClassWrapper::get_singleton()->wrap("android.view.Surface"), surface);
778
}
779
}
780
#endif
781
782