From 78ba8b28041fa366e2263f8ad7d7070f640828be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Mon, 22 Jul 2024 14:44:14 +0200 Subject: [PATCH] Fix certain resource previews empty with GL compat As an additional fix, the preview render request now ignores the main viewport hierarchy so previews don't pay the cost of re-rendering the whole editor and descendant viewports. --- editor/editor_resource_preview.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/editor/editor_resource_preview.cpp b/editor/editor_resource_preview.cpp index 742d29fef030..71865f7e8cc0 100644 --- a/editor/editor_resource_preview.cpp +++ b/editor/editor_resource_preview.cpp @@ -40,6 +40,7 @@ #include "editor/editor_settings.h" #include "editor/editor_string_names.h" #include "editor/themes/editor_scale.h" +#include "scene/main/window.h" #include "scene/resources/image_texture.h" #include "servers/rendering/rendering_server_default.h" @@ -96,14 +97,25 @@ EditorResourcePreviewGenerator::EditorResourcePreviewGenerator() { } void EditorResourcePreviewGenerator::DrawRequester::request_and_wait(RID p_viewport) const { + Callable request_vp_update_once = callable_mp(RS::get_singleton(), &RS::viewport_set_update_mode).bind(p_viewport, RS::VIEWPORT_UPDATE_ONCE); + if (EditorResourcePreview::get_singleton()->is_threaded()) { - Callable request_vp_update_once = callable_mp(RS::get_singleton(), &RS::viewport_set_update_mode).bind(p_viewport, RS::VIEWPORT_UPDATE_ONCE); RS::get_singleton()->connect(SNAME("frame_pre_draw"), request_vp_update_once, Object::CONNECT_ONE_SHOT); RS::get_singleton()->request_frame_drawn_callback(callable_mp(const_cast(this), &EditorResourcePreviewGenerator::DrawRequester::_post_semaphore)); semaphore.wait(); } else { + // Avoid the main viewport and children being redrawn. + SceneTree *st = Object::cast_to(OS::get_singleton()->get_main_loop()); + ERR_FAIL_NULL_MSG(st, "Editor's MainLoop is not a SceneTree. This is a bug."); + RID root_vp = st->get_root()->get_viewport_rid(); + RenderingServer::get_singleton()->viewport_set_active(root_vp, false); + + request_vp_update_once.call(); RS::get_singleton()->draw(false); + + // Let main viewport and children be drawn again. + RenderingServer::get_singleton()->viewport_set_active(root_vp, true); } }