From 197b54370af0bb890d7747849bc52bbff4e2990f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Wed, 31 May 2023 12:50:27 +0200 Subject: [PATCH] Fix materials' shaders not updated if loaded on a non-main thread --- scene/resources/material.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp index 5752f4cc1a8..8bd61c75270 100644 --- a/scene/resources/material.cpp +++ b/scene/resources/material.cpp @@ -88,9 +88,13 @@ void Material::_mark_initialized(const Callable &p_queue_shader_change_callable) if (ResourceLoader::is_within_load() && Thread::get_caller_id() != Thread::get_main_id()) { DEV_ASSERT(init_state != INIT_STATE_READY); if (init_state == INIT_STATE_UNINITIALIZED) { // Prevent queueing twice. - // Queue an individual update of this material (the ResourceLoader knows how to handle deferred calls safely). - p_queue_shader_change_callable.call_deferred(); + // Let's mark this material as being initialized. init_state = INIT_STATE_INITIALIZING; + // Knowing that the ResourceLoader will eventually feed deferred calls into the main message queue, let's do these: + // 1. Queue setting the init state to INIT_STATE_READY finally. + callable_mp(this, &Material::_mark_initialized).bind(p_queue_shader_change_callable).call_deferred(); + // 2. Queue an individual update of this material. + p_queue_shader_change_callable.call_deferred(); } } else { // Straightforward conditions.