| 1 | /**************************************************************************/ |
| 2 | /* openxr_composition_layer.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.h" |
| 32 | |
| 33 | #include "../extensions/openxr_composition_layer_extension.h" |
| 34 | #include "../openxr_api.h" |
| 35 | #include "../openxr_interface.h" |
| 36 | |
| 37 | #include "scene/3d/mesh_instance_3d.h" |
| 38 | #include "scene/3d/xr/xr_nodes.h" |
| 39 | #include "scene/main/viewport.h" |
| 40 | |
| 41 | #include "platform/android/api/java_class_wrapper.h" |
| 42 | |
| 43 | Vector<OpenXRCompositionLayer *> OpenXRCompositionLayer::composition_layer_nodes; |
| 44 | |
| 45 | static const char *HOLE_PUNCH_SHADER_CODE = |
| 46 | "shader_type spatial;\n" |
| 47 | "render_mode blend_mix, depth_draw_opaque, cull_back, shadow_to_opacity, shadows_disabled;\n" |
| 48 | "void fragment() {\n" |
| 49 | "\tALBEDO = vec3(0.0, 0.0, 0.0);\n" |
| 50 | "}\n"; |
| 51 | |
| 52 | OpenXRCompositionLayer::OpenXRCompositionLayer() { |
| 53 | openxr_api = OpenXRAPI::get_singleton(); |
| 54 | composition_layer_extension = OpenXRCompositionLayerExtension::get_singleton(); |
| 55 | |
| 56 | if (openxr_api) { |
| 57 | openxr_session_running = openxr_api->is_running(); |
| 58 | } |
| 59 | |
| 60 | Ref<OpenXRInterface> openxr_interface = XRServer::get_singleton()->find_interface("OpenXR"); |
| 61 | if (openxr_interface.is_valid()) { |
| 62 | openxr_interface->connect("session_begun", callable_mp(this, &OpenXRCompositionLayer::_on_openxr_session_begun)); |
| 63 | openxr_interface->connect("session_stopping", callable_mp(this, &OpenXRCompositionLayer::_on_openxr_session_stopping)); |
| 64 | } |
| 65 | |
| 66 | XRServer::get_singleton()->connect("reference_frame_changed", callable_mp(this, &OpenXRCompositionLayer::update_transform)); |
| 67 | |
| 68 | set_process_internal(true); |
| 69 | set_notify_local_transform(true); |
| 70 | |
| 71 | if (Engine::get_singleton()->is_editor_hint()) { |
| 72 | // In the editor, create the fallback right away. |
| 73 | _create_fallback_node(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | OpenXRCompositionLayer::~OpenXRCompositionLayer() { |
| 78 | Ref<OpenXRInterface> openxr_interface = XRServer::get_singleton()->find_interface("OpenXR"); |
| 79 | if (openxr_interface.is_valid()) { |
| 80 | openxr_interface->disconnect("session_begun", callable_mp(this, &OpenXRCompositionLayer::_on_openxr_session_begun)); |
| 81 | openxr_interface->disconnect("session_stopping", callable_mp(this, &OpenXRCompositionLayer::_on_openxr_session_stopping)); |
| 82 | } |
| 83 | |
| 84 | composition_layer_nodes.erase(this); |
| 85 | |
| 86 | if (composition_layer_extension && composition_layer.is_valid()) { |
| 87 | composition_layer_extension->composition_layer_free(composition_layer); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void OpenXRCompositionLayer::_bind_methods() { |
| 92 | ClassDB::bind_method(D_METHOD("set_layer_viewport", "viewport"), &OpenXRCompositionLayer::set_layer_viewport); |
| 93 | ClassDB::bind_method(D_METHOD("get_layer_viewport"), &OpenXRCompositionLayer::get_layer_viewport); |
| 94 | |
| 95 | ClassDB::bind_method(D_METHOD("set_use_android_surface", "enable"), &OpenXRCompositionLayer::set_use_android_surface); |
| 96 | ClassDB::bind_method(D_METHOD("get_use_android_surface"), &OpenXRCompositionLayer::get_use_android_surface); |
| 97 | |
| 98 | ClassDB::bind_method(D_METHOD("set_android_surface_size", "size"), &OpenXRCompositionLayer::set_android_surface_size); |
| 99 | ClassDB::bind_method(D_METHOD("get_android_surface_size"), &OpenXRCompositionLayer::get_android_surface_size); |
| 100 | |
| 101 | ClassDB::bind_method(D_METHOD("set_enable_hole_punch", "enable"), &OpenXRCompositionLayer::set_enable_hole_punch); |
| 102 | ClassDB::bind_method(D_METHOD("get_enable_hole_punch"), &OpenXRCompositionLayer::get_enable_hole_punch); |
| 103 | |
| 104 | ClassDB::bind_method(D_METHOD("set_sort_order", "order"), &OpenXRCompositionLayer::set_sort_order); |
| 105 | ClassDB::bind_method(D_METHOD("get_sort_order"), &OpenXRCompositionLayer::get_sort_order); |
| 106 | |
| 107 | ClassDB::bind_method(D_METHOD("set_alpha_blend", "enabled"), &OpenXRCompositionLayer::set_alpha_blend); |
| 108 | ClassDB::bind_method(D_METHOD("get_alpha_blend"), &OpenXRCompositionLayer::get_alpha_blend); |
| 109 | |
| 110 | ClassDB::bind_method(D_METHOD("get_android_surface"), &OpenXRCompositionLayer::get_android_surface); |
| 111 | ClassDB::bind_method(D_METHOD("is_natively_supported"), &OpenXRCompositionLayer::is_natively_supported); |
| 112 | |
| 113 | ClassDB::bind_method(D_METHOD("is_protected_content"), &OpenXRCompositionLayer::is_protected_content); |
| 114 | ClassDB::bind_method(D_METHOD("set_protected_content", "protected_content"), &OpenXRCompositionLayer::set_protected_content); |
| 115 | |
| 116 | ClassDB::bind_method(D_METHOD("set_min_filter", "mode"), &OpenXRCompositionLayer::set_min_filter); |
| 117 | ClassDB::bind_method(D_METHOD("get_min_filter"), &OpenXRCompositionLayer::get_min_filter); |
| 118 | |
| 119 | ClassDB::bind_method(D_METHOD("set_mag_filter", "mode"), &OpenXRCompositionLayer::set_mag_filter); |
| 120 | ClassDB::bind_method(D_METHOD("get_mag_filter"), &OpenXRCompositionLayer::get_mag_filter); |
| 121 | |
| 122 | ClassDB::bind_method(D_METHOD("set_mipmap_mode", "mode"), &OpenXRCompositionLayer::set_mipmap_mode); |
| 123 | ClassDB::bind_method(D_METHOD("get_mipmap_mode"), &OpenXRCompositionLayer::get_mipmap_mode); |
| 124 | |
| 125 | ClassDB::bind_method(D_METHOD("set_horizontal_wrap", "mode"), &OpenXRCompositionLayer::set_horizontal_wrap); |
| 126 | ClassDB::bind_method(D_METHOD("get_horizontal_wrap"), &OpenXRCompositionLayer::get_horizontal_wrap); |
| 127 | |
| 128 | ClassDB::bind_method(D_METHOD("set_vertical_wrap", "mode"), &OpenXRCompositionLayer::set_vertical_wrap); |
| 129 | ClassDB::bind_method(D_METHOD("get_vertical_wrap"), &OpenXRCompositionLayer::get_vertical_wrap); |
| 130 | |
| 131 | ClassDB::bind_method(D_METHOD("set_red_swizzle", "mode"), &OpenXRCompositionLayer::set_red_swizzle); |
| 132 | ClassDB::bind_method(D_METHOD("get_red_swizzle"), &OpenXRCompositionLayer::get_red_swizzle); |
| 133 | |
| 134 | ClassDB::bind_method(D_METHOD("set_green_swizzle", "mode"), &OpenXRCompositionLayer::set_green_swizzle); |
| 135 | ClassDB::bind_method(D_METHOD("get_green_swizzle"), &OpenXRCompositionLayer::get_green_swizzle); |
| 136 | |
| 137 | ClassDB::bind_method(D_METHOD("set_blue_swizzle", "mode"), &OpenXRCompositionLayer::set_blue_swizzle); |
| 138 | ClassDB::bind_method(D_METHOD("get_blue_swizzle"), &OpenXRCompositionLayer::get_blue_swizzle); |
| 139 | |
| 140 | ClassDB::bind_method(D_METHOD("set_alpha_swizzle", "mode"), &OpenXRCompositionLayer::set_alpha_swizzle); |
| 141 | ClassDB::bind_method(D_METHOD("get_alpha_swizzle"), &OpenXRCompositionLayer::get_alpha_swizzle); |
| 142 | |
| 143 | ClassDB::bind_method(D_METHOD("set_max_anisotropy", "value"), &OpenXRCompositionLayer::set_max_anisotropy); |
| 144 | ClassDB::bind_method(D_METHOD("get_max_anisotropy"), &OpenXRCompositionLayer::get_max_anisotropy); |
| 145 | |
| 146 | ClassDB::bind_method(D_METHOD("set_border_color", "color"), &OpenXRCompositionLayer::set_border_color); |
| 147 | ClassDB::bind_method(D_METHOD("get_border_color"), &OpenXRCompositionLayer::get_border_color); |
| 148 | |
| 149 | ClassDB::bind_method(D_METHOD("intersects_ray", "origin", "direction"), &OpenXRCompositionLayer::intersects_ray); |
| 150 | |
| 151 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "layer_viewport", PROPERTY_HINT_NODE_TYPE, "SubViewport"), "set_layer_viewport", "get_layer_viewport"); |
| 152 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_android_surface", PROPERTY_HINT_NONE, ""), "set_use_android_surface", "get_use_android_surface"); |
| 153 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "protected_content", PROPERTY_HINT_NONE, ""), "set_protected_content", "is_protected_content"); |
| 154 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "android_surface_size", PROPERTY_HINT_NONE, ""), "set_android_surface_size", "get_android_surface_size"); |
| 155 | ADD_PROPERTY(PropertyInfo(Variant::INT, "sort_order", PROPERTY_HINT_NONE, ""), "set_sort_order", "get_sort_order"); |
| 156 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "alpha_blend", PROPERTY_HINT_NONE, ""), "set_alpha_blend", "get_alpha_blend"); |
| 157 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enable_hole_punch", PROPERTY_HINT_NONE, ""), "set_enable_hole_punch", "get_enable_hole_punch"); |
| 158 | |
| 159 | ADD_GROUP("Swapchain State", "swapchain_state_"); |
| 160 | ADD_PROPERTY(PropertyInfo(Variant::INT, "swapchain_state_min_filter", PROPERTY_HINT_ENUM, "Nearest,Linear,Cubic"), "set_min_filter", "get_min_filter"); |
| 161 | ADD_PROPERTY(PropertyInfo(Variant::INT, "swapchain_state_mag_filter", PROPERTY_HINT_ENUM, "Nearest,Linear,Cubic"), "set_mag_filter", "get_mag_filter"); |
| 162 | ADD_PROPERTY(PropertyInfo(Variant::INT, "swapchain_state_mipmap_mode", PROPERTY_HINT_ENUM, "Disabled,Nearest,Linear"), "set_mipmap_mode", "get_mipmap_mode"); |
| 163 | ADD_PROPERTY(PropertyInfo(Variant::INT, "swapchain_state_horizontal_wrap", PROPERTY_HINT_ENUM, "Clamp to Border,Clamp to Edge,Repeat,Mirrored Repeat,Mirror Clamp to Edge"), "set_horizontal_wrap", "get_horizontal_wrap"); |
| 164 | ADD_PROPERTY(PropertyInfo(Variant::INT, "swapchain_state_vertical_wrap", PROPERTY_HINT_ENUM, "Clamp to Border,Clamp to Edge,Repeat,Mirrored Repeat,Mirror Clamp to Edge"), "set_vertical_wrap", "get_vertical_wrap"); |
| 165 | ADD_PROPERTY(PropertyInfo(Variant::INT, "swapchain_state_red_swizzle", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Zero,One"), "set_red_swizzle", "get_red_swizzle"); |
| 166 | ADD_PROPERTY(PropertyInfo(Variant::INT, "swapchain_state_green_swizzle", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Zero,One"), "set_green_swizzle", "get_green_swizzle"); |
| 167 | ADD_PROPERTY(PropertyInfo(Variant::INT, "swapchain_state_blue_swizzle", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Zero,One"), "set_blue_swizzle", "get_blue_swizzle"); |
| 168 | ADD_PROPERTY(PropertyInfo(Variant::INT, "swapchain_state_alpha_swizzle", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Zero,One"), "set_alpha_swizzle", "get_alpha_swizzle"); |
| 169 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "swapchain_state_max_anisotropy", PROPERTY_HINT_RANGE, "1.0,16.0,0.001"), "set_max_anisotropy", "get_max_anisotropy"); |
| 170 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "swapchain_state_border_color"), "set_border_color", "get_border_color"); |
| 171 | ADD_GROUP("", ""); |
| 172 | |
| 173 | BIND_ENUM_CONSTANT(FILTER_NEAREST); |
| 174 | BIND_ENUM_CONSTANT(FILTER_LINEAR); |
| 175 | BIND_ENUM_CONSTANT(FILTER_CUBIC); |
| 176 | |
| 177 | BIND_ENUM_CONSTANT(MIPMAP_MODE_DISABLED); |
| 178 | BIND_ENUM_CONSTANT(MIPMAP_MODE_NEAREST); |
| 179 | BIND_ENUM_CONSTANT(MIPMAP_MODE_LINEAR); |
| 180 | |
| 181 | BIND_ENUM_CONSTANT(WRAP_CLAMP_TO_BORDER); |
| 182 | BIND_ENUM_CONSTANT(WRAP_CLAMP_TO_EDGE); |
| 183 | BIND_ENUM_CONSTANT(WRAP_REPEAT); |
| 184 | BIND_ENUM_CONSTANT(WRAP_MIRRORED_REPEAT); |
| 185 | BIND_ENUM_CONSTANT(WRAP_MIRROR_CLAMP_TO_EDGE); |
| 186 | |
| 187 | BIND_ENUM_CONSTANT(SWIZZLE_RED); |
| 188 | BIND_ENUM_CONSTANT(SWIZZLE_GREEN); |
| 189 | BIND_ENUM_CONSTANT(SWIZZLE_BLUE); |
| 190 | BIND_ENUM_CONSTANT(SWIZZLE_ALPHA); |
| 191 | BIND_ENUM_CONSTANT(SWIZZLE_ZERO); |
| 192 | BIND_ENUM_CONSTANT(SWIZZLE_ONE); |
| 193 | } |
| 194 | |
| 195 | bool OpenXRCompositionLayer::_should_use_fallback_node() { |
| 196 | if (Engine::get_singleton()->is_editor_hint() || openxr_api == nullptr) { |
| 197 | return true; |
| 198 | } else if (openxr_session_running) { |
| 199 | return enable_hole_punch || (!is_natively_supported() && !use_android_surface); |
| 200 | } |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | void OpenXRCompositionLayer::_create_fallback_node() { |
| 205 | ERR_FAIL_COND(fallback); |
| 206 | fallback = memnew(MeshInstance3D); |
| 207 | fallback->set_cast_shadows_setting(GeometryInstance3D::SHADOW_CASTING_SETTING_OFF); |
| 208 | add_child(fallback, false, INTERNAL_MODE_FRONT); |
| 209 | should_update_fallback_mesh = true; |
| 210 | } |
| 211 | |
| 212 | void OpenXRCompositionLayer::_remove_fallback_node() { |
| 213 | ERR_FAIL_COND(fallback != nullptr); |
| 214 | remove_child(fallback); |
| 215 | fallback->queue_free(); |
| 216 | fallback = nullptr; |
| 217 | } |
| 218 | |
| 219 | void OpenXRCompositionLayer::_setup_composition_layer() { |
| 220 | if (use_android_surface || layer_viewport) { |
| 221 | if (composition_layer_extension) { |
| 222 | composition_layer_extension->composition_layer_register(composition_layer); |
| 223 | registered = true; |
| 224 | |
| 225 | // NOTE: We don't setup/clear when using Android surfaces, so we don't destroy the surface unexpectedly. |
| 226 | if (layer_viewport) { |
| 227 | // Set our properties on the layer provider, which will create all the necessary resources (ex swap chains). |
| 228 | composition_layer_extension->composition_layer_set_viewport(composition_layer, layer_viewport->get_viewport_rid(), layer_viewport->get_size()); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void OpenXRCompositionLayer::_clear_composition_layer() { |
| 235 | if (composition_layer_extension) { |
| 236 | composition_layer_extension->composition_layer_unregister(composition_layer); |
| 237 | registered = false; |
| 238 | |
| 239 | // NOTE: We don't setup/clear when using Android surfaces, so we don't destroy the surface unexpectedly. |
| 240 | if (!use_android_surface) { |
| 241 | // This will reset the viewport and free all the resources (ex swap chains) used by the layer. |
| 242 | composition_layer_extension->composition_layer_set_viewport(composition_layer, RID(), Size2i()); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void OpenXRCompositionLayer::_viewport_size_changed() { |
| 248 | if (layer_viewport && openxr_session_running && composition_layer_extension && is_natively_supported() && is_visible() && is_inside_tree()) { |
| 249 | composition_layer_extension->composition_layer_set_viewport(composition_layer, layer_viewport->get_viewport_rid(), layer_viewport->get_size()); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void OpenXRCompositionLayer::_on_openxr_session_begun() { |
| 254 | openxr_session_running = true; |
| 255 | if (_should_register()) { |
| 256 | _setup_composition_layer(); |
| 257 | } |
| 258 | if (!fallback && _should_use_fallback_node()) { |
| 259 | _create_fallback_node(); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | void OpenXRCompositionLayer::_on_openxr_session_stopping() { |
| 264 | openxr_session_running = false; |
| 265 | if (fallback && !_should_use_fallback_node()) { |
| 266 | _remove_fallback_node(); |
| 267 | } |
| 268 | _clear_composition_layer(); |
| 269 | } |
| 270 | |
| 271 | void OpenXRCompositionLayer::update_transform() { |
| 272 | if (composition_layer_extension) { |
| 273 | composition_layer_extension->composition_layer_set_transform(composition_layer, get_transform()); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void OpenXRCompositionLayer::update_fallback_mesh() { |
| 278 | should_update_fallback_mesh = true; |
| 279 | } |
| 280 | |
| 281 | bool OpenXRCompositionLayer::_should_register() { |
| 282 | return !registered && openxr_session_running && is_inside_tree() && is_visible() && is_natively_supported(); |
| 283 | } |
| 284 | |
| 285 | bool OpenXRCompositionLayer::is_viewport_in_use(SubViewport *p_viewport) { |
| 286 | ERR_FAIL_NULL_V(p_viewport, false); |
| 287 | for (const OpenXRCompositionLayer *other_composition_layer : composition_layer_nodes) { |
| 288 | if (other_composition_layer != this && other_composition_layer->is_inside_tree() && other_composition_layer->get_layer_viewport() == p_viewport) { |
| 289 | return true; |
| 290 | } |
| 291 | } |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | void OpenXRCompositionLayer::set_layer_viewport(SubViewport *p_viewport) { |
| 296 | if (layer_viewport == p_viewport) { |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | if (p_viewport != nullptr) { |
| 301 | ERR_FAIL_COND_EDMSG(is_viewport_in_use(p_viewport), RTR("Cannot use the same SubViewport with multiple OpenXR composition layers. Clear it from its current layer first.")); |
| 302 | } |
| 303 | if (use_android_surface) { |
| 304 | ERR_FAIL_COND_MSG(p_viewport != nullptr, RTR("Cannot set SubViewport on an OpenXR composition layer when using an Android surface.")); |
| 305 | } |
| 306 | |
| 307 | if (layer_viewport) { |
| 308 | layer_viewport->disconnect("size_changed", callable_mp(this, &OpenXRCompositionLayer::_viewport_size_changed)); |
| 309 | } |
| 310 | |
| 311 | layer_viewport = p_viewport; |
| 312 | if (_should_register()) { |
| 313 | _setup_composition_layer(); |
| 314 | } |
| 315 | |
| 316 | if (layer_viewport) { |
| 317 | layer_viewport->connect("size_changed", callable_mp(this, &OpenXRCompositionLayer::_viewport_size_changed)); |
| 318 | |
| 319 | SubViewport::UpdateMode update_mode = layer_viewport->get_update_mode(); |
| 320 | if (update_mode == SubViewport::UPDATE_WHEN_VISIBLE || update_mode == SubViewport::UPDATE_WHEN_PARENT_VISIBLE) { |
| 321 | WARN_PRINT_ONCE("OpenXR composition layers cannot use SubViewports with UPDATE_WHEN_VISIBLE or UPDATE_WHEN_PARENT_VISIBLE. Switching to UPDATE_ALWAYS."); |
| 322 | layer_viewport->set_update_mode(SubViewport::UPDATE_ALWAYS); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | if (fallback) { |
| 327 | _reset_fallback_material(); |
| 328 | } else if (openxr_session_running && composition_layer_extension && is_visible() && is_inside_tree()) { |
| 329 | if (layer_viewport) { |
| 330 | composition_layer_extension->composition_layer_set_viewport(composition_layer, layer_viewport->get_viewport_rid(), layer_viewport->get_size()); |
| 331 | } else { |
| 332 | composition_layer_extension->composition_layer_set_viewport(composition_layer, RID(), Size2i()); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | void OpenXRCompositionLayer::set_use_android_surface(bool p_use_android_surface) { |
| 338 | if (use_android_surface == p_use_android_surface) { |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | use_android_surface = p_use_android_surface; |
| 343 | if (use_android_surface) { |
| 344 | // It's possible that the layer provider is unregistered here (if previously invisible) |
| 345 | set_layer_viewport(nullptr); |
| 346 | if (composition_layer_extension) { |
| 347 | composition_layer_extension->composition_layer_set_use_android_surface(composition_layer, true, android_surface_size); |
| 348 | } |
| 349 | // ...and it may not be set up above because of viewport = null, android surface is false, so set it up again: |
| 350 | if (_should_register()) { |
| 351 | _setup_composition_layer(); |
| 352 | } |
| 353 | } else { |
| 354 | if (composition_layer_extension) { |
| 355 | composition_layer_extension->composition_layer_set_use_android_surface(composition_layer, false, Size2i()); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | notify_property_list_changed(); |
| 360 | } |
| 361 | |
| 362 | bool OpenXRCompositionLayer::get_use_android_surface() const { |
| 363 | return use_android_surface; |
| 364 | } |
| 365 | |
| 366 | void OpenXRCompositionLayer::set_android_surface_size(Size2i p_size) { |
| 367 | if (android_surface_size == p_size) { |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | android_surface_size = p_size; |
| 372 | if (use_android_surface && composition_layer_extension) { |
| 373 | composition_layer_extension->composition_layer_set_use_android_surface(composition_layer, true, android_surface_size); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | Size2i OpenXRCompositionLayer::get_android_surface_size() const { |
| 378 | return android_surface_size; |
| 379 | } |
| 380 | |
| 381 | SubViewport *OpenXRCompositionLayer::get_layer_viewport() const { |
| 382 | return layer_viewport; |
| 383 | } |
| 384 | |
| 385 | void OpenXRCompositionLayer::set_enable_hole_punch(bool p_enable) { |
| 386 | if (enable_hole_punch == p_enable) { |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | enable_hole_punch = p_enable; |
| 391 | if (_should_use_fallback_node()) { |
| 392 | if (fallback) { |
| 393 | _reset_fallback_material(); |
| 394 | } else { |
| 395 | _create_fallback_node(); |
| 396 | } |
| 397 | } else if (fallback) { |
| 398 | _remove_fallback_node(); |
| 399 | } |
| 400 | |
| 401 | update_configuration_warnings(); |
| 402 | } |
| 403 | |
| 404 | bool OpenXRCompositionLayer::get_enable_hole_punch() const { |
| 405 | return enable_hole_punch; |
| 406 | } |
| 407 | |
| 408 | void OpenXRCompositionLayer::set_sort_order(int p_order) { |
| 409 | sort_order = p_order; |
| 410 | if (composition_layer_extension) { |
| 411 | composition_layer_extension->composition_layer_set_sort_order(composition_layer, p_order); |
| 412 | } |
| 413 | update_configuration_warnings(); |
| 414 | } |
| 415 | |
| 416 | int OpenXRCompositionLayer::get_sort_order() const { |
| 417 | return sort_order; |
| 418 | } |
| 419 | |
| 420 | void OpenXRCompositionLayer::set_alpha_blend(bool p_alpha_blend) { |
| 421 | alpha_blend = p_alpha_blend; |
| 422 | if (composition_layer_extension) { |
| 423 | composition_layer_extension->composition_layer_set_alpha_blend(composition_layer, p_alpha_blend); |
| 424 | } |
| 425 | if (fallback) { |
| 426 | _reset_fallback_material(); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | bool OpenXRCompositionLayer::get_alpha_blend() const { |
| 431 | return alpha_blend; |
| 432 | } |
| 433 | |
| 434 | bool OpenXRCompositionLayer::is_natively_supported() const { |
| 435 | if (composition_layer_extension && openxr_api) { |
| 436 | return composition_layer_extension->is_available(_get_openxr_type()); |
| 437 | } |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | void OpenXRCompositionLayer::set_protected_content(bool p_protected_content) { |
| 442 | if (protected_content == p_protected_content) { |
| 443 | return; |
| 444 | } |
| 445 | protected_content = p_protected_content; |
| 446 | if (composition_layer_extension) { |
| 447 | composition_layer_extension->composition_layer_set_protected_content(composition_layer, p_protected_content); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | bool OpenXRCompositionLayer::is_protected_content() const { |
| 452 | return protected_content; |
| 453 | } |
| 454 | |
| 455 | void OpenXRCompositionLayer::set_min_filter(Filter p_mode) { |
| 456 | if (min_filter == p_mode) { |
| 457 | return; |
| 458 | } |
| 459 | min_filter = p_mode; |
| 460 | if (composition_layer_extension) { |
| 461 | composition_layer_extension->composition_layer_set_min_filter(composition_layer, (OpenXRCompositionLayerExtension::Filter)p_mode); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | OpenXRCompositionLayer::Filter OpenXRCompositionLayer::get_min_filter() const { |
| 466 | return min_filter; |
| 467 | } |
| 468 | |
| 469 | void OpenXRCompositionLayer::set_mag_filter(Filter p_mode) { |
| 470 | if (mag_filter == p_mode) { |
| 471 | return; |
| 472 | } |
| 473 | mag_filter = p_mode; |
| 474 | if (composition_layer_extension) { |
| 475 | composition_layer_extension->composition_layer_set_mag_filter(composition_layer, (OpenXRCompositionLayerExtension::Filter)p_mode); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | OpenXRCompositionLayer::Filter OpenXRCompositionLayer::get_mag_filter() const { |
| 480 | return mag_filter; |
| 481 | } |
| 482 | |
| 483 | void OpenXRCompositionLayer::set_mipmap_mode(MipmapMode p_mode) { |
| 484 | if (mipmap_mode == p_mode) { |
| 485 | return; |
| 486 | } |
| 487 | mipmap_mode = p_mode; |
| 488 | if (composition_layer_extension) { |
| 489 | composition_layer_extension->composition_layer_set_mipmap_mode(composition_layer, (OpenXRCompositionLayerExtension::MipmapMode)p_mode); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | OpenXRCompositionLayer::MipmapMode OpenXRCompositionLayer::get_mipmap_mode() const { |
| 494 | return mipmap_mode; |
| 495 | } |
| 496 | |
| 497 | void OpenXRCompositionLayer::set_horizontal_wrap(Wrap p_mode) { |
| 498 | if (horizontal_wrap == p_mode) { |
| 499 | return; |
| 500 | } |
| 501 | horizontal_wrap = p_mode; |
| 502 | if (composition_layer_extension) { |
| 503 | composition_layer_extension->composition_layer_set_horizontal_wrap(composition_layer, (OpenXRCompositionLayerExtension::Wrap)p_mode); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | OpenXRCompositionLayer::Wrap OpenXRCompositionLayer::get_horizontal_wrap() const { |
| 508 | return horizontal_wrap; |
| 509 | } |
| 510 | |
| 511 | void OpenXRCompositionLayer::set_vertical_wrap(Wrap p_mode) { |
| 512 | if (vertical_wrap == p_mode) { |
| 513 | return; |
| 514 | } |
| 515 | vertical_wrap = p_mode; |
| 516 | if (composition_layer_extension) { |
| 517 | composition_layer_extension->composition_layer_set_vertical_wrap(composition_layer, (OpenXRCompositionLayerExtension::Wrap)p_mode); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | OpenXRCompositionLayer::Wrap OpenXRCompositionLayer::get_vertical_wrap() const { |
| 522 | return vertical_wrap; |
| 523 | } |
| 524 | |
| 525 | void OpenXRCompositionLayer::set_red_swizzle(Swizzle p_mode) { |
| 526 | if (red_swizzle == p_mode) { |
| 527 | return; |
| 528 | } |
| 529 | red_swizzle = p_mode; |
| 530 | if (composition_layer_extension) { |
| 531 | composition_layer_extension->composition_layer_set_red_swizzle(composition_layer, (OpenXRCompositionLayerExtension::Swizzle)p_mode); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | OpenXRCompositionLayer::Swizzle OpenXRCompositionLayer::get_red_swizzle() const { |
| 536 | return red_swizzle; |
| 537 | } |
| 538 | |
| 539 | void OpenXRCompositionLayer::set_green_swizzle(Swizzle p_mode) { |
| 540 | if (green_swizzle == p_mode) { |
| 541 | return; |
| 542 | } |
| 543 | green_swizzle = p_mode; |
| 544 | if (composition_layer_extension) { |
| 545 | composition_layer_extension->composition_layer_set_green_swizzle(composition_layer, (OpenXRCompositionLayerExtension::Swizzle)p_mode); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | OpenXRCompositionLayer::Swizzle OpenXRCompositionLayer::get_green_swizzle() const { |
| 550 | return green_swizzle; |
| 551 | } |
| 552 | |
| 553 | void OpenXRCompositionLayer::set_blue_swizzle(Swizzle p_mode) { |
| 554 | if (blue_swizzle == p_mode) { |
| 555 | return; |
| 556 | } |
| 557 | blue_swizzle = p_mode; |
| 558 | if (composition_layer_extension) { |
| 559 | composition_layer_extension->composition_layer_set_blue_swizzle(composition_layer, (OpenXRCompositionLayerExtension::Swizzle)p_mode); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | OpenXRCompositionLayer::Swizzle OpenXRCompositionLayer::get_blue_swizzle() const { |
| 564 | return blue_swizzle; |
| 565 | } |
| 566 | |
| 567 | void OpenXRCompositionLayer::set_alpha_swizzle(Swizzle p_mode) { |
| 568 | if (alpha_swizzle == p_mode) { |
| 569 | return; |
| 570 | } |
| 571 | alpha_swizzle = p_mode; |
| 572 | if (composition_layer_extension) { |
| 573 | composition_layer_extension->composition_layer_set_alpha_swizzle(composition_layer, (OpenXRCompositionLayerExtension::Swizzle)p_mode); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | OpenXRCompositionLayer::Swizzle OpenXRCompositionLayer::get_alpha_swizzle() const { |
| 578 | return alpha_swizzle; |
| 579 | } |
| 580 | |
| 581 | void OpenXRCompositionLayer::set_max_anisotropy(float p_value) { |
| 582 | if (max_anisotropy == p_value) { |
| 583 | return; |
| 584 | } |
| 585 | max_anisotropy = p_value; |
| 586 | if (composition_layer_extension) { |
| 587 | composition_layer_extension->composition_layer_set_max_anisotropy(composition_layer, p_value); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | float OpenXRCompositionLayer::get_max_anisotropy() const { |
| 592 | return max_anisotropy; |
| 593 | } |
| 594 | |
| 595 | void OpenXRCompositionLayer::set_border_color(const Color &p_color) { |
| 596 | if (border_color == p_color) { |
| 597 | return; |
| 598 | } |
| 599 | border_color = p_color; |
| 600 | if (composition_layer_extension) { |
| 601 | composition_layer_extension->composition_layer_set_border_color(composition_layer, p_color); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | Color OpenXRCompositionLayer::get_border_color() const { |
| 606 | return border_color; |
| 607 | } |
| 608 | |
| 609 | Ref<JavaObject> OpenXRCompositionLayer::get_android_surface() { |
| 610 | if (composition_layer_extension) { |
| 611 | return composition_layer_extension->composition_layer_get_android_surface(composition_layer); |
| 612 | } |
| 613 | return Ref<JavaObject>(); |
| 614 | } |
| 615 | |
| 616 | Vector2 OpenXRCompositionLayer::intersects_ray(const Vector3 &p_origin, const Vector3 &p_direction) const { |
| 617 | return Vector2(-1.0, -1.0); |
| 618 | } |
| 619 | |
| 620 | void OpenXRCompositionLayer::_reset_fallback_material() { |
| 621 | ERR_FAIL_NULL(fallback); |
| 622 | |
| 623 | if (fallback->get_mesh().is_null()) { |
| 624 | return; |
| 625 | } |
| 626 | |
| 627 | if (enable_hole_punch && !Engine::get_singleton()->is_editor_hint() && is_natively_supported()) { |
| 628 | Ref<ShaderMaterial> material = fallback->get_surface_override_material(0); |
| 629 | if (material.is_null()) { |
| 630 | Ref<Shader> shader; |
| 631 | shader.instantiate(); |
| 632 | shader->set_code(HOLE_PUNCH_SHADER_CODE); |
| 633 | |
| 634 | material.instantiate(); |
| 635 | material->set_shader(shader); |
| 636 | |
| 637 | fallback->set_surface_override_material(0, material); |
| 638 | } |
| 639 | } else if (layer_viewport) { |
| 640 | Ref<StandardMaterial3D> material = fallback->get_surface_override_material(0); |
| 641 | if (material.is_null()) { |
| 642 | material.instantiate(); |
| 643 | material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
| 644 | material->set_local_to_scene(true); |
| 645 | fallback->set_surface_override_material(0, material); |
| 646 | } |
| 647 | |
| 648 | material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, !enable_hole_punch); |
| 649 | material->set_transparency(get_alpha_blend() ? StandardMaterial3D::TRANSPARENCY_ALPHA : StandardMaterial3D::TRANSPARENCY_DISABLED); |
| 650 | material->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, layer_viewport->get_texture()); |
| 651 | } else { |
| 652 | fallback->set_surface_override_material(0, Ref<Material>()); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | void OpenXRCompositionLayer::_notification(int p_what) { |
| 657 | switch (p_what) { |
| 658 | case NOTIFICATION_POSTINITIALIZE: { |
| 659 | composition_layer_nodes.push_back(this); |
| 660 | |
| 661 | for (OpenXRExtensionWrapper *extension : OpenXRAPI::get_registered_extension_wrappers()) { |
| 662 | extension_property_values.merge(extension->get_viewport_composition_layer_extension_property_defaults()); |
| 663 | } |
| 664 | if (composition_layer_extension) { |
| 665 | composition_layer_extension->composition_layer_set_extension_property_values(composition_layer, extension_property_values); |
| 666 | } |
| 667 | } break; |
| 668 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 669 | if (fallback) { |
| 670 | if (should_update_fallback_mesh) { |
| 671 | fallback->set_mesh(_create_fallback_mesh()); |
| 672 | _reset_fallback_material(); |
| 673 | should_update_fallback_mesh = false; |
| 674 | } |
| 675 | } |
| 676 | } break; |
| 677 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 678 | if (is_natively_supported() && openxr_session_running && is_inside_tree()) { |
| 679 | if (is_visible()) { |
| 680 | _setup_composition_layer(); |
| 681 | } else { |
| 682 | _clear_composition_layer(); |
| 683 | } |
| 684 | } |
| 685 | update_configuration_warnings(); |
| 686 | } break; |
| 687 | case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: { |
| 688 | update_transform(); |
| 689 | update_configuration_warnings(); |
| 690 | } break; |
| 691 | case NOTIFICATION_ENTER_TREE: { |
| 692 | if (layer_viewport && is_viewport_in_use(layer_viewport)) { |
| 693 | _clear_composition_layer(); |
| 694 | } else if (openxr_session_running && is_visible()) { |
| 695 | _setup_composition_layer(); |
| 696 | } |
| 697 | } break; |
| 698 | case NOTIFICATION_EXIT_TREE: { |
| 699 | // This will clean up existing resources. |
| 700 | _clear_composition_layer(); |
| 701 | } break; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | void OpenXRCompositionLayer::_get_property_list(List<PropertyInfo> *p_property_list) const { |
| 706 | List<PropertyInfo> extension_properties; |
| 707 | for (OpenXRExtensionWrapper *extension : OpenXRAPI::get_registered_extension_wrappers()) { |
| 708 | extension->get_viewport_composition_layer_extension_properties(&extension_properties); |
| 709 | } |
| 710 | |
| 711 | for (const PropertyInfo &pinfo : extension_properties) { |
| 712 | StringName prop_name = pinfo.name; |
| 713 | if (!String(prop_name).contains_char('/')) { |
| 714 | WARN_PRINT_ONCE(vformat("Discarding OpenXRCompositionLayer property name '%s' from extension because it doesn't contain a '/'.")); |
| 715 | continue; |
| 716 | } |
| 717 | p_property_list->push_back(pinfo); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | bool OpenXRCompositionLayer::_get(const StringName &p_property, Variant &r_value) const { |
| 722 | if (extension_property_values.has(p_property)) { |
| 723 | r_value = extension_property_values[p_property]; |
| 724 | return true; |
| 725 | } |
| 726 | |
| 727 | return false; |
| 728 | } |
| 729 | |
| 730 | bool OpenXRCompositionLayer::_set(const StringName &p_property, const Variant &p_value) { |
| 731 | extension_property_values[p_property] = p_value; |
| 732 | |
| 733 | if (composition_layer_extension) { |
| 734 | composition_layer_extension->composition_layer_set_extension_property_values(composition_layer, extension_property_values); |
| 735 | } |
| 736 | |
| 737 | return true; |
| 738 | } |
| 739 | |
| 740 | void OpenXRCompositionLayer::_validate_property(PropertyInfo &p_property) const { |
| 741 | if (!Engine::get_singleton()->is_editor_hint()) { |
| 742 | return; |
| 743 | } |
| 744 | if (p_property.name == "layer_viewport") { |
| 745 | if (use_android_surface) { |
| 746 | p_property.usage &= ~PROPERTY_USAGE_EDITOR; |
| 747 | } else { |
| 748 | p_property.usage |= PROPERTY_USAGE_EDITOR; |
| 749 | } |
| 750 | } else if (p_property.name == "android_surface_size") { |
| 751 | if (use_android_surface) { |
| 752 | p_property.usage |= PROPERTY_USAGE_EDITOR; |
| 753 | } else { |
| 754 | p_property.usage &= ~PROPERTY_USAGE_EDITOR; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | PackedStringArray OpenXRCompositionLayer::get_configuration_warnings() const { |
| 760 | PackedStringArray warnings = Node3D::get_configuration_warnings(); |
| 761 | |
| 762 | if (is_visible() && is_inside_tree()) { |
| 763 | XROrigin3D *origin = Object::cast_to<XROrigin3D>(get_parent()); |
| 764 | if (origin == nullptr) { |
| 765 | warnings.push_back(RTR("OpenXR composition layers must have an XROrigin3D node as their parent.")); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | if (!get_transform().basis.is_orthonormal()) { |
| 770 | warnings.push_back(RTR("OpenXR composition layers must have orthonormalized transforms (ie. no scale or shearing).")); |
| 771 | } |
| 772 | |
| 773 | if (enable_hole_punch && get_sort_order() >= 0) { |
| 774 | warnings.push_back(RTR("Hole punching won't work as expected unless the sort order is less than zero.")); |
| 775 | } |
| 776 | |
| 777 | return warnings; |
| 778 | } |