From a23fc126eb0ea93e0ca8e622586649404e6f668e Mon Sep 17 00:00:00 2001 From: David Snopek Date: Wed, 27 Jan 2021 14:16:00 -0600 Subject: [PATCH] Prevent fatal error in WebXR when 'immersize-ar' loses and regains tracking --- modules/webxr/native/library_godot_webxr.js | 14 ++++++++++- modules/webxr/webxr_interface_js.cpp | 26 ++++++++++++++------- modules/webxr/webxr_interface_js.h | 2 +- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/modules/webxr/native/library_godot_webxr.js b/modules/webxr/native/library_godot_webxr.js index 764656712de..8e9ef8a73c8 100644 --- a/modules/webxr/native/library_godot_webxr.js +++ b/modules/webxr/native/library_godot_webxr.js @@ -380,6 +380,11 @@ const GodotWebXR = { gl.deleteTexture(texture); } GodotWebXR.textures[i] = null; + + const texture_id = GodotWebXR.texture_ids[i]; + if (texture_id !== null) { + GL.textures[texture_id] = null; + } GodotWebXR.texture_ids[i] = null; } @@ -460,7 +465,7 @@ const GodotWebXR = { godot_webxr_get_external_texture_for_eye__proxy: 'sync', godot_webxr_get_external_texture_for_eye__sig: 'ii', godot_webxr_get_external_texture_for_eye: function (p_eye) { - if (!GodotWebXR.session || !GodotWebXR.pose) { + if (!GodotWebXR.session) { return 0; } @@ -469,6 +474,13 @@ const GodotWebXR = { return GodotWebXR.texture_ids[view_index]; } + // Check pose separately and after returning the cached texture id, + // because we won't get a pose in some cases if we lose tracking, and + // we don't want to return 0 just because tracking was lost. + if (!GodotWebXR.pose) { + return 0; + } + const glLayer = GodotWebXR.session.renderState.baseLayer; const view = GodotWebXR.pose.views[view_index]; const viewport = glLayer.getViewport(view); diff --git a/modules/webxr/webxr_interface_js.cpp b/modules/webxr/webxr_interface_js.cpp index 7cfaf31495b..74789fc98e6 100644 --- a/modules/webxr/webxr_interface_js.cpp +++ b/modules/webxr/webxr_interface_js.cpp @@ -78,6 +78,8 @@ void _emwebxr_on_session_failed(char *p_message) { Ref interface = xr_server->find_interface("WebXR"); ERR_FAIL_COND(interface.is_null()); + interface->uninitialize(); + String message = String(p_message); interface->emit_signal("session_failed", message); } @@ -224,6 +226,12 @@ bool WebXRInterfaceJS::initialize() { // make this our primary interface xr_server->set_primary_interface(this); + // Clear render_targetsize to make sure it gets reset to the new size. + // Clearing in uninitialize() doesn't work because a frame can still be + // rendered after it's called, which will fill render_targetsize again. + render_targetsize.width = 0; + render_targetsize.height = 0; + initialized = true; godot_webxr_initialize( @@ -277,22 +285,24 @@ Transform WebXRInterfaceJS::_js_matrix_to_transform(float *p_js_matrix) { } Size2 WebXRInterfaceJS::get_render_targetsize() { - Size2 target_size; + if (render_targetsize.width != 0 && render_targetsize.height != 0) { + return render_targetsize; + } int *js_size = godot_webxr_get_render_targetsize(); if (!initialized || js_size == nullptr) { - // As a default, use half the window size. - target_size = DisplayServer::get_singleton()->window_get_size(); - target_size.width /= 2.0; - return target_size; + // As a temporary default (until WebXR is fully initialized), use half the window size. + Size2 temp = DisplayServer::get_singleton()->window_get_size(); + temp.width /= 2.0; + return temp; } - target_size.width = js_size[0]; - target_size.height = js_size[1]; + render_targetsize.width = js_size[0]; + render_targetsize.height = js_size[1]; free(js_size); - return target_size; + return render_targetsize; }; Transform WebXRInterfaceJS::get_transform_for_eye(XRInterface::Eyes p_eye, const Transform &p_cam_transform) { diff --git a/modules/webxr/webxr_interface_js.h b/modules/webxr/webxr_interface_js.h index 93da9a6d12e..49299b252f9 100644 --- a/modules/webxr/webxr_interface_js.h +++ b/modules/webxr/webxr_interface_js.h @@ -47,7 +47,6 @@ class WebXRInterfaceJS : public WebXRInterface { private: bool initialized; - // @todo Should these really use enums instead of strings? String session_mode; String required_features; String optional_features; @@ -55,6 +54,7 @@ private: String reference_space_type; bool controllers_state[2]; + Size2 render_targetsize; Transform _js_matrix_to_transform(float *p_js_matrix); void _update_tracker(int p_controller_id);