| 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 reference_frame = XRServer::get_singleton()->get_reference_frame(); |
| 286 | Transform3D transform = reference_frame.inverse() * p_transform; |
| 287 | Quaternion quat(transform.basis.orthonormalized()); |
| 288 | |
| 289 | XrPosef pose = { |
| 290 | { (float)quat.x, (float)quat.y, (float)quat.z, (float)quat.w }, |
| 291 | { (float)transform.origin.x, (float)transform.origin.y, (float)transform.origin.z } |
| 292 | }; |
| 293 | |
| 294 | switch (composition_layer.type) { |
| 295 | case XR_TYPE_COMPOSITION_LAYER_QUAD: { |
| 296 | composition_layer_quad.pose = pose; |
| 297 | } break; |
| 298 | case XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR: { |
| 299 | composition_layer_cylinder.pose = pose; |
| 300 | } break; |
| 301 | case XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR: { |
| 302 | composition_layer_equirect.pose = pose; |
| 303 | } break; |
| 304 | default: { |
| 305 | ERR_PRINT(vformat("Cannot set transform on unsupported composition layer type: %s", composition_layer.type)); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | void OpenXRCompositionLayerExtension::CompositionLayer::set_extension_property_values(const Dictionary &p_property_values) { |
| 311 | extension_property_values = p_property_values; |
| 312 | extension_property_values_changed = true; |
| 313 | } |
| 314 | |
| 315 | void OpenXRCompositionLayerExtension::CompositionLayer::set_min_filter(Filter p_mode) { |
| 316 | swapchain_state.min_filter = p_mode; |
| 317 | swapchain_state_is_dirty = true; |
| 318 | } |
| 319 | |
| 320 | void OpenXRCompositionLayerExtension::CompositionLayer::set_mag_filter(Filter p_mode) { |
| 321 | swapchain_state.mag_filter = p_mode; |
| 322 | swapchain_state_is_dirty = true; |
| 323 | } |
| 324 | |
| 325 | void OpenXRCompositionLayerExtension::CompositionLayer::set_mipmap_mode(MipmapMode p_mode) { |
| 326 | swapchain_state.mipmap_mode = p_mode; |
| 327 | swapchain_state_is_dirty = true; |
| 328 | } |
| 329 | |
| 330 | void OpenXRCompositionLayerExtension::CompositionLayer::set_horizontal_wrap(Wrap p_mode) { |
| 331 | swapchain_state.horizontal_wrap = p_mode; |
| 332 | swapchain_state_is_dirty = true; |
| 333 | } |
| 334 | |
| 335 | void OpenXRCompositionLayerExtension::CompositionLayer::set_vertical_wrap(Wrap p_mode) { |
| 336 | swapchain_state.vertical_wrap = p_mode; |
| 337 | swapchain_state_is_dirty = true; |
| 338 | } |
| 339 | |
| 340 | void OpenXRCompositionLayerExtension::CompositionLayer::set_red_swizzle(Swizzle p_mode) { |
| 341 | swapchain_state.red_swizzle = p_mode; |
| 342 | swapchain_state_is_dirty = true; |
| 343 | } |
| 344 | |
| 345 | void OpenXRCompositionLayerExtension::CompositionLayer::set_green_swizzle(Swizzle p_mode) { |
| 346 | swapchain_state.green_swizzle = p_mode; |
| 347 | swapchain_state_is_dirty = true; |
| 348 | } |
| 349 | |
| 350 | void OpenXRCompositionLayerExtension::CompositionLayer::set_blue_swizzle(Swizzle p_mode) { |
| 351 | swapchain_state.blue_swizzle = p_mode; |
| 352 | swapchain_state_is_dirty = true; |
| 353 | } |
| 354 | |
| 355 | void OpenXRCompositionLayerExtension::CompositionLayer::set_alpha_swizzle(Swizzle p_mode) { |
| 356 | swapchain_state.alpha_swizzle = p_mode; |
| 357 | swapchain_state_is_dirty = true; |
| 358 | } |
| 359 | |
| 360 | void OpenXRCompositionLayerExtension::CompositionLayer::set_max_anisotropy(float p_value) { |
| 361 | swapchain_state.max_anisotropy = p_value; |
| 362 | swapchain_state_is_dirty = true; |
| 363 | } |
| 364 | |
| 365 | void OpenXRCompositionLayerExtension::CompositionLayer::set_border_color(const Color &p_color) { |
| 366 | swapchain_state.border_color = p_color; |
| 367 | swapchain_state_is_dirty = true; |
| 368 | } |
| 369 | |
| 370 | void OpenXRCompositionLayerExtension::CompositionLayer::set_quad_size(const Size2 &p_size) { |
| 371 | ERR_FAIL_COND(composition_layer.type != XR_TYPE_COMPOSITION_LAYER_QUAD); |
| 372 | composition_layer_quad.size = { (float)p_size.x, (float)p_size.y }; |
| 373 | } |
| 374 | |
| 375 | void OpenXRCompositionLayerExtension::CompositionLayer::set_cylinder_radius(float p_radius) { |
| 376 | ERR_FAIL_COND(composition_layer.type != XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR); |
| 377 | composition_layer_cylinder.radius = p_radius; |
| 378 | } |
| 379 | |
| 380 | void OpenXRCompositionLayerExtension::CompositionLayer::set_cylinder_aspect_ratio(float p_aspect_ratio) { |
| 381 | ERR_FAIL_COND(composition_layer.type != XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR); |
| 382 | composition_layer_cylinder.aspectRatio = p_aspect_ratio; |
| 383 | } |
| 384 | |
| 385 | void OpenXRCompositionLayerExtension::CompositionLayer::set_cylinder_central_angle(float p_central_angle) { |
| 386 | ERR_FAIL_COND(composition_layer.type != XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR); |
| 387 | composition_layer_cylinder.centralAngle = p_central_angle; |
| 388 | } |
| 389 | |
| 390 | void OpenXRCompositionLayerExtension::CompositionLayer::set_equirect_radius(float p_radius) { |
| 391 | ERR_FAIL_COND(composition_layer.type != XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR); |
| 392 | composition_layer_equirect.radius = p_radius; |
| 393 | } |
| 394 | |
| 395 | void OpenXRCompositionLayerExtension::CompositionLayer::set_equirect_central_horizontal_angle(float p_angle) { |
| 396 | ERR_FAIL_COND(composition_layer.type != XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR); |
| 397 | composition_layer_equirect.centralHorizontalAngle = p_angle; |
| 398 | } |
| 399 | |
| 400 | void OpenXRCompositionLayerExtension::CompositionLayer::set_equirect_upper_vertical_angle(float p_angle) { |
| 401 | ERR_FAIL_COND(composition_layer.type != XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR); |
| 402 | composition_layer_equirect.upperVerticalAngle = p_angle; |
| 403 | } |
| 404 | |
| 405 | void OpenXRCompositionLayerExtension::CompositionLayer::set_equirect_lower_vertical_angle(float p_angle) { |
| 406 | ERR_FAIL_COND(composition_layer.type != XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR); |
| 407 | composition_layer_equirect.lowerVerticalAngle = p_angle; |
| 408 | } |
| 409 | |
| 410 | Ref<JavaObject> OpenXRCompositionLayerExtension::CompositionLayer::get_android_surface() { |
| 411 | #ifdef ANDROID_ENABLED |
| 412 | if (use_android_surface) { |
| 413 | MutexLock lock(OpenXRCompositionLayerExtension::get_singleton()->composition_layer_mutex); |
| 414 | if (android_surface.surface.is_null()) { |
| 415 | create_android_surface(); |
| 416 | } |
| 417 | return android_surface.surface; |
| 418 | } |
| 419 | #endif |
| 420 | return Ref<JavaObject>(); |
| 421 | } |
| 422 | |
| 423 | void OpenXRCompositionLayerExtension::CompositionLayer::on_pre_render() { |
| 424 | #ifdef ANDROID_ENABLED |
| 425 | if (use_android_surface) { |
| 426 | MutexLock lock(OpenXRCompositionLayerExtension::get_singleton()->composition_layer_mutex); |
| 427 | if (android_surface.surface.is_null()) { |
| 428 | create_android_surface(); |
| 429 | } |
| 430 | return; |
| 431 | } |
| 432 | #endif |
| 433 | |
| 434 | RenderingServer *rs = RenderingServer::get_singleton(); |
| 435 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 436 | |
| 437 | if (subviewport.viewport.is_valid() && openxr_api && openxr_api->is_running()) { |
| 438 | RS::ViewportUpdateMode update_mode = rs->viewport_get_update_mode(subviewport.viewport); |
| 439 | if (update_mode == RS::VIEWPORT_UPDATE_ONCE || update_mode == RS::VIEWPORT_UPDATE_ALWAYS) { |
| 440 | // Update our XR swapchain |
| 441 | if (update_and_acquire_swapchain(update_mode == RS::VIEWPORT_UPDATE_ONCE)) { |
| 442 | // Render to our XR swapchain image. |
| 443 | RID rt = rs->viewport_get_render_target(subviewport.viewport); |
| 444 | RSG::texture_storage->render_target_set_override(rt, get_current_swapchain_texture(), RID(), RID(), RID()); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | if (swapchain_state_is_dirty) { |
| 450 | update_swapchain_state(); |
| 451 | swapchain_state_is_dirty = false; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | XrCompositionLayerBaseHeader *OpenXRCompositionLayerExtension::CompositionLayer::get_composition_layer() { |
| 456 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 457 | OpenXRCompositionLayerExtension *composition_layer_extension = OpenXRCompositionLayerExtension::get_singleton(); |
| 458 | |
| 459 | if (openxr_api == nullptr || composition_layer_extension == nullptr) { |
| 460 | // OpenXR not initialized or we're in the editor? |
| 461 | return nullptr; |
| 462 | } |
| 463 | |
| 464 | if (!composition_layer_extension->is_available(composition_layer.type)) { |
| 465 | // Selected type is not supported, ignore our layer. |
| 466 | return nullptr; |
| 467 | } |
| 468 | |
| 469 | XrSwapchainSubImage subimage = { |
| 470 | 0, // swapchain // NOLINT(modernize-use-nullptr) - 32-bit uses non-pointer uint64 |
| 471 | { { 0, 0 }, { 0, 0 } }, // imageRect |
| 472 | 0, // imageArrayIndex |
| 473 | }; |
| 474 | update_swapchain_sub_image(subimage); |
| 475 | |
| 476 | if (subimage.swapchain == XR_NULL_HANDLE) { |
| 477 | // Don't have a swapchain to display? Ignore our layer. |
| 478 | return nullptr; |
| 479 | } |
| 480 | |
| 481 | // Update the layer struct for the swapchain. |
| 482 | switch (composition_layer.type) { |
| 483 | case XR_TYPE_COMPOSITION_LAYER_QUAD: { |
| 484 | composition_layer_quad.space = openxr_api->get_play_space(); |
| 485 | composition_layer_quad.subImage = subimage; |
| 486 | } break; |
| 487 | |
| 488 | case XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR: { |
| 489 | composition_layer_cylinder.space = openxr_api->get_play_space(); |
| 490 | composition_layer_cylinder.subImage = subimage; |
| 491 | } break; |
| 492 | |
| 493 | case XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR: { |
| 494 | composition_layer_equirect.space = openxr_api->get_play_space(); |
| 495 | composition_layer_equirect.subImage = subimage; |
| 496 | } break; |
| 497 | |
| 498 | default: { |
| 499 | return nullptr; |
| 500 | } break; |
| 501 | } |
| 502 | |
| 503 | if (extension_property_values_changed) { |
| 504 | extension_property_values_changed = false; |
| 505 | |
| 506 | void *next_pointer = nullptr; |
| 507 | for (OpenXRExtensionWrapper *extension : OpenXRAPI::get_registered_extension_wrappers()) { |
| 508 | void *np = extension->set_viewport_composition_layer_and_get_next_pointer(&composition_layer, extension_property_values, next_pointer); |
| 509 | if (np) { |
| 510 | next_pointer = np; |
| 511 | } |
| 512 | } |
| 513 | composition_layer.next = next_pointer; |
| 514 | } |
| 515 | |
| 516 | return &composition_layer; |
| 517 | } |
| 518 | |
| 519 | void OpenXRCompositionLayerExtension::CompositionLayer::free() { |
| 520 | if (use_android_surface) { |
| 521 | free_swapchain(); |
| 522 | } else { |
| 523 | // This will reset the viewport and free the swapchain too. |
| 524 | set_viewport(RID(), Size2i()); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | void OpenXRCompositionLayerExtension::CompositionLayer::update_swapchain_state() { |
| 529 | OpenXRFBUpdateSwapchainExtension *fb_update_swapchain_ext = OpenXRFBUpdateSwapchainExtension::get_singleton(); |
| 530 | if (!fb_update_swapchain_ext) { |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | #ifdef ANDROID_ENABLED |
| 535 | if (use_android_surface) { |
| 536 | if (android_surface.swapchain == XR_NULL_HANDLE) { |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | fb_update_swapchain_ext->update_swapchain_state(android_surface.swapchain, &swapchain_state); |
| 541 | } else |
| 542 | #endif |
| 543 | { |
| 544 | if (subviewport.swapchain_info.get_swapchain() == XR_NULL_HANDLE) { |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | fb_update_swapchain_ext->update_swapchain_state(subviewport.swapchain_info.get_swapchain(), &swapchain_state); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | void OpenXRCompositionLayerExtension::CompositionLayer::update_swapchain_sub_image(XrSwapchainSubImage &r_subimage) { |
| 553 | #ifdef ANDROID_ENABLED |
| 554 | if (use_android_surface) { |
| 555 | r_subimage.swapchain = android_surface.swapchain; |
| 556 | } else |
| 557 | #endif |
| 558 | { |
| 559 | XrSwapchain swapchain = subviewport.swapchain_info.get_swapchain(); |
| 560 | |
| 561 | if (swapchain && subviewport.swapchain_info.is_image_acquired()) { |
| 562 | subviewport.swapchain_info.release(); |
| 563 | } |
| 564 | |
| 565 | r_subimage.swapchain = swapchain; |
| 566 | } |
| 567 | |
| 568 | r_subimage.imageRect.extent.width = swapchain_size.width; |
| 569 | r_subimage.imageRect.extent.height = swapchain_size.height; |
| 570 | } |
| 571 | |
| 572 | bool OpenXRCompositionLayerExtension::CompositionLayer::update_and_acquire_swapchain(bool p_static_image) { |
| 573 | ERR_FAIL_COND_V(use_android_surface, false); |
| 574 | |
| 575 | OpenXRCompositionLayerExtension *composition_layer_extension = OpenXRCompositionLayerExtension::get_singleton(); |
| 576 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 577 | |
| 578 | if (openxr_api == nullptr || composition_layer_extension == nullptr) { |
| 579 | // OpenXR not initialized or we're in the editor? |
| 580 | return false; |
| 581 | } |
| 582 | if (!composition_layer_extension->is_available(composition_layer.type)) { |
| 583 | // Selected type is not supported? |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | // See if our current swapchain is outdated. |
| 588 | if (subviewport.swapchain_info.get_swapchain() != XR_NULL_HANDLE) { |
| 589 | // If this swap chain, or the previous one, were static, then we can't reuse it. |
| 590 | if (swapchain_size == subviewport.viewport_size && !p_static_image && !subviewport.static_image && protected_content == subviewport.swapchain_protected_content) { |
| 591 | // We're all good! Just acquire it. |
| 592 | // We can ignore should_render here, return will be false. |
| 593 | bool should_render = true; |
| 594 | return subviewport.swapchain_info.acquire(should_render); |
| 595 | } |
| 596 | |
| 597 | subviewport.swapchain_info.queue_free(); |
| 598 | } |
| 599 | |
| 600 | // Create our new swap chain |
| 601 | int64_t swapchain_format = openxr_api->get_color_swapchain_format(); |
| 602 | const uint32_t sample_count = 1; |
| 603 | const uint32_t array_size = 1; |
| 604 | XrSwapchainCreateFlags create_flags = 0; |
| 605 | if (p_static_image) { |
| 606 | create_flags |= XR_SWAPCHAIN_CREATE_STATIC_IMAGE_BIT; |
| 607 | } |
| 608 | if (protected_content) { |
| 609 | create_flags |= XR_SWAPCHAIN_CREATE_PROTECTED_CONTENT_BIT; |
| 610 | } |
| 611 | 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)) { |
| 612 | swapchain_size = Size2i(); |
| 613 | return false; |
| 614 | } |
| 615 | |
| 616 | swapchain_state_is_dirty = true; |
| 617 | |
| 618 | // Acquire our image so we can start rendering into it, |
| 619 | // we can ignore should_render here, ret will be false. |
| 620 | bool should_render = true; |
| 621 | bool ret = subviewport.swapchain_info.acquire(should_render); |
| 622 | |
| 623 | swapchain_size = subviewport.viewport_size; |
| 624 | subviewport.static_image = p_static_image; |
| 625 | subviewport.swapchain_protected_content = protected_content; |
| 626 | return ret; |
| 627 | } |
| 628 | |
| 629 | RID OpenXRCompositionLayerExtension::CompositionLayer::get_current_swapchain_texture() { |
| 630 | ERR_FAIL_COND_V(use_android_surface, RID()); |
| 631 | |
| 632 | if (OpenXRAPI::get_singleton() == nullptr) { |
| 633 | return RID(); |
| 634 | } |
| 635 | |
| 636 | return subviewport.swapchain_info.get_image(); |
| 637 | } |
| 638 | |
| 639 | void OpenXRCompositionLayerExtension::CompositionLayer::free_swapchain() { |
| 640 | #ifdef ANDROID_ENABLED |
| 641 | if (use_android_surface) { |
| 642 | if (android_surface.swapchain != XR_NULL_HANDLE) { |
| 643 | OpenXRCompositionLayerExtension::get_singleton()->xrDestroySwapchain(android_surface.swapchain); |
| 644 | android_surface.swapchain = XR_NULL_HANDLE; |
| 645 | android_surface.surface.unref(); |
| 646 | } |
| 647 | } else |
| 648 | #endif |
| 649 | { |
| 650 | if (subviewport.swapchain_info.get_swapchain() != XR_NULL_HANDLE) { |
| 651 | subviewport.swapchain_info.queue_free(); |
| 652 | } |
| 653 | subviewport.static_image = false; |
| 654 | } |
| 655 | |
| 656 | swapchain_size = Size2i(); |
| 657 | } |
| 658 | |
| 659 | #ifdef ANDROID_ENABLED |
| 660 | void OpenXRCompositionLayerExtension::CompositionLayer::create_android_surface() { |
| 661 | ERR_FAIL_COND(android_surface.swapchain != XR_NULL_HANDLE || android_surface.surface.is_valid()); |
| 662 | |
| 663 | void *next_pointer = nullptr; |
| 664 | for (OpenXRExtensionWrapper *wrapper : OpenXRAPI::get_registered_extension_wrappers()) { |
| 665 | void *np = wrapper->set_android_surface_swapchain_create_info_and_get_next_pointer(extension_property_values, next_pointer); |
| 666 | if (np != nullptr) { |
| 667 | next_pointer = np; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | // Check to see if content should be protected. |
| 672 | XrSwapchainCreateFlags create_flags = 0; |
| 673 | |
| 674 | if (protected_content) { |
| 675 | create_flags = XR_SWAPCHAIN_CREATE_PROTECTED_CONTENT_BIT; |
| 676 | } |
| 677 | |
| 678 | // The XR_FB_android_surface_swapchain_create extension mandates that format, sampleCount, |
| 679 | // faceCount, arraySize, and mipCount must be zero. |
| 680 | XrSwapchainCreateInfo info = { |
| 681 | XR_TYPE_SWAPCHAIN_CREATE_INFO, // type |
| 682 | next_pointer, // next |
| 683 | create_flags, // createFlags |
| 684 | XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT | XR_SWAPCHAIN_USAGE_MUTABLE_FORMAT_BIT, // usageFlags |
| 685 | 0, // format |
| 686 | 0, // sampleCount |
| 687 | (uint32_t)swapchain_size.x, // width |
| 688 | (uint32_t)swapchain_size.y, // height |
| 689 | 0, // faceCount |
| 690 | 0, // arraySize |
| 691 | 0, // mipCount |
| 692 | }; |
| 693 | |
| 694 | jobject surface; |
| 695 | OpenXRCompositionLayerExtension::get_singleton()->create_android_surface_swapchain(&info, &android_surface.swapchain, &surface); |
| 696 | |
| 697 | swapchain_state_is_dirty = true; |
| 698 | |
| 699 | if (surface) { |
| 700 | android_surface.surface.instantiate(JavaClassWrapper::get_singleton()->wrap("android.view.Surface"), surface); |
| 701 | } |
| 702 | } |
| 703 | #endif |