Prevent shaders from generating code before the constructor finishes.

Fixes #43733: "creating SpatialMaterial in a separate thread creates invalid
shaders (temporarily)."

The bug occurred because various setters called in materials' constructors add
materials to queues that are processed on the main thread. This means that
when the materials are created in another thread, they can be processed on the
main thread before the constructor has finished.

The fix adds a flag to affected materials that prevents them from being added
to the queue until their constructors have finished initialising all the
members.

(cherry picked from commit 9e9bac1549)
This commit is contained in:
Tom Coxon 2021-09-06 13:32:59 +01:00 committed by Rémi Verschelde
parent 0ac9c9906b
commit a2f3decb88
No known key found for this signature in database
GPG key ID: C3336907360768E1
6 changed files with 9 additions and 3 deletions

View file

@ -162,7 +162,7 @@ void CanvasItemMaterial::_queue_shader_change() {
material_mutex.lock();
if (!element.in_list()) {
if (is_initialized && !element.in_list()) {
dirty_materials->add(&element);
}
@ -313,6 +313,7 @@ CanvasItemMaterial::CanvasItemMaterial() :
current_key.key = 0;
current_key.invalid_key = 1;
is_initialized = true;
_queue_shader_change();
}

View file

@ -116,6 +116,7 @@ private:
_FORCE_INLINE_ void _queue_shader_change();
_FORCE_INLINE_ bool _is_shader_dirty() const;
bool is_initialized = false;
BlendMode blend_mode;
LightMode light_mode;
bool particles_animation;

View file

@ -1071,7 +1071,7 @@ void SpatialMaterial::_queue_shader_change() {
material_mutex.lock();
if (!element.in_list()) {
if (is_initialized && !element.in_list()) {
dirty_materials->add(&element);
}
@ -2409,6 +2409,7 @@ SpatialMaterial::SpatialMaterial() :
current_key.key = 0;
current_key.invalid_key = 1;
is_initialized = true;
_queue_shader_change();
}

View file

@ -371,6 +371,7 @@ private:
_FORCE_INLINE_ void _queue_shader_change();
_FORCE_INLINE_ bool _is_shader_dirty() const;
bool is_initialized = false;
Color albedo;
float specular;
float metallic;

View file

@ -621,7 +621,7 @@ void ParticlesMaterial::_queue_shader_change() {
material_mutex.lock();
if (!element.in_list()) {
if (is_initialized && !element.in_list()) {
dirty_materials->add(&element);
}
@ -1282,6 +1282,7 @@ ParticlesMaterial::ParticlesMaterial() :
current_key.key = 0;
current_key.invalid_key = 1;
is_initialized = true;
_queue_shader_change();
}

View file

@ -198,6 +198,7 @@ private:
_FORCE_INLINE_ void _queue_shader_change();
_FORCE_INLINE_ bool _is_shader_dirty() const;
bool is_initialized = false;
Vector3 direction;
float spread;
float flatness;