summaryrefslogtreecommitdiffstats
path: root/scene/resources/material.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/resources/material.cpp')
-rw-r--r--scene/resources/material.cpp22
1 files changed, 19 insertions, 3 deletions
diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp
index d0aa224773..d35c49b266 100644
--- a/scene/resources/material.cpp
+++ b/scene/resources/material.cpp
@@ -82,6 +82,23 @@ void Material::_validate_property(PropertyInfo &p_property) const {
}
}
+void Material::_mark_initialized(const Callable &p_queue_shader_change_callable) {
+ // If this is happening as part of resource loading, it is not safe to queue the update
+ // as an addition to the dirty list, unless the load is happening on the main thread.
+ 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();
+ init_state = INIT_STATE_INITIALIZING;
+ }
+ } else {
+ // Straightforward conditions.
+ init_state = INIT_STATE_READY;
+ p_queue_shader_change_callable.callv(Array());
+ }
+}
+
void Material::inspect_native_shader_code() {
SceneTree *st = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop());
RID shader = get_shader_rid();
@@ -1485,7 +1502,7 @@ void BaseMaterial3D::flush_changes() {
void BaseMaterial3D::_queue_shader_change() {
MutexLock lock(material_mutex);
- if (is_initialized && !element.in_list()) {
+ if (_is_initialized() && !element.in_list()) {
dirty_materials->add(&element);
}
}
@@ -3028,8 +3045,7 @@ BaseMaterial3D::BaseMaterial3D(bool p_orm) :
flags[FLAG_ALBEDO_TEXTURE_MSDF] = false;
flags[FLAG_USE_TEXTURE_REPEAT] = true;
- is_initialized = true;
- _queue_shader_change();
+ _mark_initialized(callable_mp(this, &BaseMaterial3D::_queue_shader_change));
}
BaseMaterial3D::~BaseMaterial3D() {