openxr_composition_layer.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. #include "openxr_composition_layer.h"
  31. #include "../extensions/openxr_composition_layer_extension.h"
  32. #include "../openxr_api.h"
  33. #include "../openxr_interface.h"
  34. #include "scene/3d/mesh_instance_3d.h"
  35. #include "scene/3d/xr_nodes.h"
  36. #include "scene/main/viewport.h"
  37. #include "platform/android/api/java_class_wrapper.h"
  38. Vector<OpenXRCompositionLayer *> OpenXRCompositionLayer::composition_layer_nodes;
  39. static const char *HOLE_PUNCH_SHADER_CODE =
  40. "shader_type spatial;\n"
  41. "render_mode blend_mix, depth_draw_opaque, cull_back, shadow_to_opacity, shadows_disabled;\n"
  42. "void fragment() {\n"
  43. "\tALBEDO = vec3(0.0, 0.0, 0.0);\n"
  44. "}\n";
  45. OpenXRCompositionLayer::OpenXRCompositionLayer(XrCompositionLayerBaseHeader *p_composition_layer) {
  46. composition_layer_base_header = p_composition_layer;
  47. openxr_layer_provider = memnew(OpenXRViewportCompositionLayerProvider(composition_layer_base_header));
  48. openxr_api = OpenXRAPI::get_singleton();
  49. composition_layer_extension = OpenXRCompositionLayerExtension::get_singleton();
  50. if (openxr_api) {
  51. openxr_session_running = openxr_api->is_running();
  52. }
  53. Ref<OpenXRInterface> openxr_interface = XRServer::get_singleton()->find_interface("OpenXR");
  54. if (openxr_interface.is_valid()) {
  55. openxr_interface->connect("session_begun", callable_mp(this, &OpenXRCompositionLayer::_on_openxr_session_begun));
  56. openxr_interface->connect("session_stopping", callable_mp(this, &OpenXRCompositionLayer::_on_openxr_session_stopping));
  57. }
  58. set_process_internal(true);
  59. set_notify_local_transform(true);
  60. if (Engine::get_singleton()->is_editor_hint()) {
  61. // In the editor, create the fallback right away.
  62. _create_fallback_node();
  63. }
  64. }
  65. OpenXRCompositionLayer::~OpenXRCompositionLayer() {
  66. Ref<OpenXRInterface> openxr_interface = XRServer::get_singleton()->find_interface("OpenXR");
  67. if (openxr_interface.is_valid()) {
  68. openxr_interface->disconnect("session_begun", callable_mp(this, &OpenXRCompositionLayer::_on_openxr_session_begun));
  69. openxr_interface->disconnect("session_stopping", callable_mp(this, &OpenXRCompositionLayer::_on_openxr_session_stopping));
  70. }
  71. composition_layer_nodes.erase(this);
  72. if (openxr_layer_provider != nullptr) {
  73. memdelete(openxr_layer_provider);
  74. openxr_layer_provider = nullptr;
  75. }
  76. }
  77. void OpenXRCompositionLayer::_bind_methods() {
  78. ClassDB::bind_method(D_METHOD("set_layer_viewport", "viewport"), &OpenXRCompositionLayer::set_layer_viewport);
  79. ClassDB::bind_method(D_METHOD("get_layer_viewport"), &OpenXRCompositionLayer::get_layer_viewport);
  80. ClassDB::bind_method(D_METHOD("set_use_android_surface", "enable"), &OpenXRCompositionLayer::set_use_android_surface);
  81. ClassDB::bind_method(D_METHOD("get_use_android_surface"), &OpenXRCompositionLayer::get_use_android_surface);
  82. ClassDB::bind_method(D_METHOD("set_android_surface_size", "size"), &OpenXRCompositionLayer::set_android_surface_size);
  83. ClassDB::bind_method(D_METHOD("get_android_surface_size"), &OpenXRCompositionLayer::get_android_surface_size);
  84. ClassDB::bind_method(D_METHOD("set_enable_hole_punch", "enable"), &OpenXRCompositionLayer::set_enable_hole_punch);
  85. ClassDB::bind_method(D_METHOD("get_enable_hole_punch"), &OpenXRCompositionLayer::get_enable_hole_punch);
  86. ClassDB::bind_method(D_METHOD("set_sort_order", "order"), &OpenXRCompositionLayer::set_sort_order);
  87. ClassDB::bind_method(D_METHOD("get_sort_order"), &OpenXRCompositionLayer::get_sort_order);
  88. ClassDB::bind_method(D_METHOD("set_alpha_blend", "enabled"), &OpenXRCompositionLayer::set_alpha_blend);
  89. ClassDB::bind_method(D_METHOD("get_alpha_blend"), &OpenXRCompositionLayer::get_alpha_blend);
  90. ClassDB::bind_method(D_METHOD("get_android_surface"), &OpenXRCompositionLayer::get_android_surface);
  91. ClassDB::bind_method(D_METHOD("is_natively_supported"), &OpenXRCompositionLayer::is_natively_supported);
  92. ClassDB::bind_method(D_METHOD("intersects_ray", "origin", "direction"), &OpenXRCompositionLayer::intersects_ray);
  93. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "layer_viewport", PROPERTY_HINT_NODE_TYPE, "SubViewport"), "set_layer_viewport", "get_layer_viewport");
  94. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_android_surface", PROPERTY_HINT_NONE, ""), "set_use_android_surface", "get_use_android_surface");
  95. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "android_surface_size", PROPERTY_HINT_NONE, ""), "set_android_surface_size", "get_android_surface_size");
  96. ADD_PROPERTY(PropertyInfo(Variant::INT, "sort_order", PROPERTY_HINT_NONE, ""), "set_sort_order", "get_sort_order");
  97. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "alpha_blend", PROPERTY_HINT_NONE, ""), "set_alpha_blend", "get_alpha_blend");
  98. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enable_hole_punch", PROPERTY_HINT_NONE, ""), "set_enable_hole_punch", "get_enable_hole_punch");
  99. }
  100. bool OpenXRCompositionLayer::_should_use_fallback_node() {
  101. if (Engine::get_singleton()->is_editor_hint()) {
  102. return true;
  103. } else if (openxr_session_running) {
  104. return enable_hole_punch || (!is_natively_supported() && !use_android_surface);
  105. }
  106. return false;
  107. }
  108. void OpenXRCompositionLayer::_create_fallback_node() {
  109. ERR_FAIL_COND(fallback);
  110. fallback = memnew(MeshInstance3D);
  111. fallback->set_cast_shadows_setting(GeometryInstance3D::SHADOW_CASTING_SETTING_OFF);
  112. add_child(fallback, false, INTERNAL_MODE_FRONT);
  113. should_update_fallback_mesh = true;
  114. }
  115. void OpenXRCompositionLayer::_remove_fallback_node() {
  116. ERR_FAIL_COND(fallback != nullptr);
  117. remove_child(fallback);
  118. fallback->queue_free();
  119. fallback = nullptr;
  120. }
  121. void OpenXRCompositionLayer::_setup_composition_layer_provider() {
  122. if (use_android_surface || layer_viewport) {
  123. if (composition_layer_extension) {
  124. composition_layer_extension->register_viewport_composition_layer_provider(openxr_layer_provider);
  125. }
  126. // NOTE: We don't setup/clear when using Android surfaces, so we don't destroy the surface unexpectedly.
  127. if (layer_viewport) {
  128. // Set our properties on the layer provider, which will create all the necessary resources (ex swap chains).
  129. openxr_layer_provider->set_viewport(layer_viewport->get_viewport_rid(), layer_viewport->get_size());
  130. }
  131. }
  132. }
  133. void OpenXRCompositionLayer::_clear_composition_layer_provider() {
  134. if (composition_layer_extension) {
  135. composition_layer_extension->unregister_viewport_composition_layer_provider(openxr_layer_provider);
  136. }
  137. // NOTE: We don't setup/clear when using Android surfaces, so we don't destroy the surface unexpectedly.
  138. if (!use_android_surface) {
  139. // This will reset the viewport and free all the resources (ex swap chains) used by the layer.
  140. openxr_layer_provider->set_viewport(RID(), Size2i());
  141. }
  142. }
  143. void OpenXRCompositionLayer::_on_openxr_session_begun() {
  144. openxr_session_running = true;
  145. if (is_natively_supported() && is_visible() && is_inside_tree()) {
  146. _setup_composition_layer_provider();
  147. }
  148. if (!fallback && _should_use_fallback_node()) {
  149. _create_fallback_node();
  150. }
  151. }
  152. void OpenXRCompositionLayer::_on_openxr_session_stopping() {
  153. openxr_session_running = false;
  154. if (fallback && !_should_use_fallback_node()) {
  155. _remove_fallback_node();
  156. }
  157. _clear_composition_layer_provider();
  158. }
  159. void OpenXRCompositionLayer::update_fallback_mesh() {
  160. should_update_fallback_mesh = true;
  161. }
  162. XrPosef OpenXRCompositionLayer::get_openxr_pose() {
  163. Transform3D reference_frame = XRServer::get_singleton()->get_reference_frame();
  164. Transform3D transform = reference_frame.inverse() * get_transform();
  165. Quaternion quat(transform.basis.orthonormalized());
  166. return {
  167. { (float)quat.x, (float)quat.y, (float)quat.z, (float)quat.w },
  168. { (float)transform.origin.x, (float)transform.origin.y, (float)transform.origin.z }
  169. };
  170. }
  171. bool OpenXRCompositionLayer::is_viewport_in_use(SubViewport *p_viewport) {
  172. ERR_FAIL_NULL_V(p_viewport, false);
  173. for (const OpenXRCompositionLayer *other_composition_layer : composition_layer_nodes) {
  174. if (other_composition_layer != this && other_composition_layer->is_inside_tree() && other_composition_layer->get_layer_viewport() == p_viewport) {
  175. return true;
  176. }
  177. }
  178. return false;
  179. }
  180. void OpenXRCompositionLayer::set_layer_viewport(SubViewport *p_viewport) {
  181. if (layer_viewport == p_viewport) {
  182. return;
  183. }
  184. if (p_viewport != nullptr) {
  185. 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."));
  186. }
  187. if (use_android_surface) {
  188. ERR_FAIL_COND_MSG(p_viewport != nullptr, RTR("Cannot set SubViewport on an OpenXR composition layer when using an Android surface."));
  189. }
  190. layer_viewport = p_viewport;
  191. if (layer_viewport) {
  192. SubViewport::UpdateMode update_mode = layer_viewport->get_update_mode();
  193. if (update_mode == SubViewport::UPDATE_WHEN_VISIBLE || update_mode == SubViewport::UPDATE_WHEN_PARENT_VISIBLE) {
  194. WARN_PRINT_ONCE("OpenXR composition layers cannot use SubViewports with UPDATE_WHEN_VISIBLE or UPDATE_WHEN_PARENT_VISIBLE. Switching to UPDATE_ALWAYS.");
  195. layer_viewport->set_update_mode(SubViewport::UPDATE_ALWAYS);
  196. }
  197. }
  198. if (fallback) {
  199. _reset_fallback_material();
  200. } else if (openxr_session_running && is_visible() && is_inside_tree()) {
  201. if (layer_viewport) {
  202. openxr_layer_provider->set_viewport(layer_viewport->get_viewport_rid(), layer_viewport->get_size());
  203. } else {
  204. openxr_layer_provider->set_viewport(RID(), Size2i());
  205. }
  206. }
  207. }
  208. void OpenXRCompositionLayer::set_use_android_surface(bool p_use_android_surface) {
  209. if (use_android_surface == p_use_android_surface) {
  210. return;
  211. }
  212. use_android_surface = p_use_android_surface;
  213. if (use_android_surface) {
  214. set_layer_viewport(nullptr);
  215. openxr_layer_provider->set_use_android_surface(true, android_surface_size);
  216. } else {
  217. openxr_layer_provider->set_use_android_surface(false, Size2i());
  218. }
  219. notify_property_list_changed();
  220. }
  221. bool OpenXRCompositionLayer::get_use_android_surface() const {
  222. return use_android_surface;
  223. }
  224. void OpenXRCompositionLayer::set_android_surface_size(Size2i p_size) {
  225. if (android_surface_size == p_size) {
  226. return;
  227. }
  228. android_surface_size = p_size;
  229. if (use_android_surface) {
  230. openxr_layer_provider->set_use_android_surface(true, android_surface_size);
  231. }
  232. }
  233. Size2i OpenXRCompositionLayer::get_android_surface_size() const {
  234. return android_surface_size;
  235. }
  236. SubViewport *OpenXRCompositionLayer::get_layer_viewport() const {
  237. return layer_viewport;
  238. }
  239. void OpenXRCompositionLayer::set_enable_hole_punch(bool p_enable) {
  240. if (enable_hole_punch == p_enable) {
  241. return;
  242. }
  243. enable_hole_punch = p_enable;
  244. if (_should_use_fallback_node()) {
  245. if (fallback) {
  246. _reset_fallback_material();
  247. } else {
  248. _create_fallback_node();
  249. }
  250. } else if (fallback) {
  251. _remove_fallback_node();
  252. }
  253. update_configuration_warnings();
  254. }
  255. bool OpenXRCompositionLayer::get_enable_hole_punch() const {
  256. return enable_hole_punch;
  257. }
  258. void OpenXRCompositionLayer::set_sort_order(int p_order) {
  259. openxr_layer_provider->set_sort_order(p_order);
  260. update_configuration_warnings();
  261. }
  262. int OpenXRCompositionLayer::get_sort_order() const {
  263. return openxr_layer_provider->get_sort_order();
  264. }
  265. void OpenXRCompositionLayer::set_alpha_blend(bool p_alpha_blend) {
  266. openxr_layer_provider->set_alpha_blend(p_alpha_blend);
  267. if (fallback) {
  268. _reset_fallback_material();
  269. }
  270. }
  271. bool OpenXRCompositionLayer::get_alpha_blend() const {
  272. return openxr_layer_provider->get_alpha_blend();
  273. }
  274. bool OpenXRCompositionLayer::is_natively_supported() const {
  275. if (composition_layer_extension) {
  276. return composition_layer_extension->is_available(openxr_layer_provider->get_openxr_type());
  277. }
  278. return false;
  279. }
  280. Ref<JavaObject> OpenXRCompositionLayer::get_android_surface() {
  281. return openxr_layer_provider->get_android_surface();
  282. }
  283. Vector2 OpenXRCompositionLayer::intersects_ray(const Vector3 &p_origin, const Vector3 &p_direction) const {
  284. return Vector2(-1.0, -1.0);
  285. }
  286. void OpenXRCompositionLayer::_reset_fallback_material() {
  287. ERR_FAIL_NULL(fallback);
  288. if (fallback->get_mesh().is_null()) {
  289. return;
  290. }
  291. if (enable_hole_punch && !Engine::get_singleton()->is_editor_hint() && is_natively_supported()) {
  292. Ref<ShaderMaterial> material = fallback->get_surface_override_material(0);
  293. if (material.is_null()) {
  294. Ref<Shader> shader;
  295. shader.instantiate();
  296. shader->set_code(HOLE_PUNCH_SHADER_CODE);
  297. material.instantiate();
  298. material->set_shader(shader);
  299. fallback->set_surface_override_material(0, material);
  300. }
  301. } else if (layer_viewport) {
  302. Ref<StandardMaterial3D> material = fallback->get_surface_override_material(0);
  303. if (material.is_null()) {
  304. material.instantiate();
  305. material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  306. material->set_local_to_scene(true);
  307. fallback->set_surface_override_material(0, material);
  308. }
  309. material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, !enable_hole_punch);
  310. material->set_transparency(get_alpha_blend() ? StandardMaterial3D::TRANSPARENCY_ALPHA : StandardMaterial3D::TRANSPARENCY_DISABLED);
  311. Ref<ViewportTexture> texture = material->get_texture(StandardMaterial3D::TEXTURE_ALBEDO);
  312. if (texture.is_null()) {
  313. texture = layer_viewport->get_texture();
  314. }
  315. Node *loc_scene = texture->get_local_scene();
  316. NodePath viewport_path = loc_scene->get_path_to(layer_viewport);
  317. texture->set_viewport_path_in_scene(viewport_path);
  318. material->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, texture);
  319. } else {
  320. fallback->set_surface_override_material(0, Ref<Material>());
  321. }
  322. }
  323. void OpenXRCompositionLayer::_notification(int p_what) {
  324. switch (p_what) {
  325. case NOTIFICATION_POSTINITIALIZE: {
  326. composition_layer_nodes.push_back(this);
  327. for (OpenXRExtensionWrapper *extension : OpenXRAPI::get_registered_extension_wrappers()) {
  328. extension_property_values.merge(extension->get_viewport_composition_layer_extension_property_defaults());
  329. }
  330. openxr_layer_provider->set_extension_property_values(extension_property_values);
  331. } break;
  332. case NOTIFICATION_INTERNAL_PROCESS: {
  333. if (fallback) {
  334. if (should_update_fallback_mesh) {
  335. fallback->set_mesh(_create_fallback_mesh());
  336. _reset_fallback_material();
  337. should_update_fallback_mesh = false;
  338. }
  339. }
  340. } break;
  341. case NOTIFICATION_VISIBILITY_CHANGED: {
  342. if (!fallback && openxr_session_running && is_inside_tree()) {
  343. if (is_visible()) {
  344. _setup_composition_layer_provider();
  345. } else {
  346. _clear_composition_layer_provider();
  347. }
  348. }
  349. update_configuration_warnings();
  350. } break;
  351. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  352. update_configuration_warnings();
  353. } break;
  354. case NOTIFICATION_ENTER_TREE: {
  355. if (layer_viewport && is_viewport_in_use(layer_viewport)) {
  356. _clear_composition_layer_provider();
  357. } else if (openxr_session_running && is_visible()) {
  358. _setup_composition_layer_provider();
  359. }
  360. } break;
  361. case NOTIFICATION_EXIT_TREE: {
  362. // This will clean up existing resources.
  363. _clear_composition_layer_provider();
  364. } break;
  365. }
  366. }
  367. void OpenXRCompositionLayer::_get_property_list(List<PropertyInfo> *p_property_list) const {
  368. List<PropertyInfo> extension_properties;
  369. for (OpenXRExtensionWrapper *extension : OpenXRAPI::get_registered_extension_wrappers()) {
  370. extension->get_viewport_composition_layer_extension_properties(&extension_properties);
  371. }
  372. for (const PropertyInfo &pinfo : extension_properties) {
  373. StringName prop_name = pinfo.name;
  374. if (!String(prop_name).contains("/")) {
  375. WARN_PRINT_ONCE(vformat("Discarding OpenXRCompositionLayer property name '%s' from extension because it doesn't contain a '/'."));
  376. continue;
  377. }
  378. p_property_list->push_back(pinfo);
  379. }
  380. }
  381. bool OpenXRCompositionLayer::_get(const StringName &p_property, Variant &r_value) const {
  382. if (extension_property_values.has(p_property)) {
  383. r_value = extension_property_values[p_property];
  384. }
  385. return true;
  386. }
  387. bool OpenXRCompositionLayer::_set(const StringName &p_property, const Variant &p_value) {
  388. extension_property_values[p_property] = p_value;
  389. openxr_layer_provider->set_extension_property_values(extension_property_values);
  390. return true;
  391. }
  392. void OpenXRCompositionLayer::_validate_property(PropertyInfo &p_property) const {
  393. if (p_property.name == "layer_viewport") {
  394. if (use_android_surface) {
  395. p_property.usage &= ~PROPERTY_USAGE_EDITOR;
  396. } else {
  397. p_property.usage |= PROPERTY_USAGE_EDITOR;
  398. }
  399. } else if (p_property.name == "android_surface_size") {
  400. if (use_android_surface) {
  401. p_property.usage |= PROPERTY_USAGE_EDITOR;
  402. } else {
  403. p_property.usage &= ~PROPERTY_USAGE_EDITOR;
  404. }
  405. }
  406. }
  407. PackedStringArray OpenXRCompositionLayer::get_configuration_warnings() const {
  408. PackedStringArray warnings = Node3D::get_configuration_warnings();
  409. if (is_visible() && is_inside_tree()) {
  410. XROrigin3D *origin = Object::cast_to<XROrigin3D>(get_parent());
  411. if (origin == nullptr) {
  412. warnings.push_back(RTR("OpenXR composition layers must have an XROrigin3D node as their parent."));
  413. }
  414. }
  415. if (!get_transform().basis.is_orthonormal()) {
  416. warnings.push_back(RTR("OpenXR composition layers must have orthonormalized transforms (ie. no scale or shearing)."));
  417. }
  418. if (enable_hole_punch && get_sort_order() >= 0) {
  419. warnings.push_back(RTR("Hole punching won't work as expected unless the sort order is less than zero."));
  420. }
  421. return warnings;
  422. }