From 2568178b426a1c0b22551e9c4cf338a4f8456de0 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 25 Aug 2021 17:24:25 +0200 Subject: [PATCH] Only call initial_setup_default on the main thread, and document that shader duplication can crash if done from a different thread. Removed the material mutex from the prop cache as it's not needed anymore. --- material_cache/prop_material_cache.cpp | 2 ++ prop_instance_merger.cpp | 16 +++++++++++++++- prop_instance_prop_job.cpp | 4 +++- singleton/prop_cache.cpp | 4 ---- singleton/prop_cache.h | 2 -- tiled_wall/tiled_wall.cpp | 1 + 6 files changed, 21 insertions(+), 8 deletions(-) diff --git a/material_cache/prop_material_cache.cpp b/material_cache/prop_material_cache.cpp index a428d20..270beff 100644 --- a/material_cache/prop_material_cache.cpp +++ b/material_cache/prop_material_cache.cpp @@ -282,6 +282,8 @@ void PropMaterialCache::refresh_rects() { } void PropMaterialCache::initial_setup_default() { + //Note: call only on the main thread! Shader->duplicate() can crash if done from an another thread! + PropCache *pc = PropCache::get_singleton(); pc->ensure_materials_loaded(); diff --git a/prop_instance_merger.cpp b/prop_instance_merger.cpp index c503803..03a1aea 100644 --- a/prop_instance_merger.cpp +++ b/prop_instance_merger.cpp @@ -533,6 +533,21 @@ void PropInstanceMerger::_build() { Ref cache = PropCache::get_singleton()->material_cache_get(_prop_data); + if (!cache->get_initialized()) { + //lock it! + cache->mutex_lock(); + + //check again, this thread might have gotten here after an another one already did the initialization! + //this check might not be needed here + if (!cache->get_initialized()) { + //this will set up materials, and settings + //needs to be called from the main thread! + cache->initial_setup_default(); + } + + cache->mutex_unlock(); + } + _job->set_material_cache(cache); prop_preprocess(Transform(), _prop_data); @@ -588,7 +603,6 @@ void PropInstanceMerger::_prop_preprocess(Transform transform, const Ref tiled_wall_data = e; if (tiled_wall_data.is_valid()) { - _job->add_tiled_wall(tiled_wall_data, t); if (tiled_wall_data->get_collision()) { diff --git a/prop_instance_prop_job.cpp b/prop_instance_prop_job.cpp index 7165fc3..9ab9512 100644 --- a/prop_instance_prop_job.cpp +++ b/prop_instance_prop_job.cpp @@ -267,7 +267,9 @@ void PropInstancePropJob::phase_setup_cache() { //check again, this thread might have gotten here after an another one already did the initialization! if (!_material_cache->get_initialized()) { //this will set up materials, and settings - _material_cache->initial_setup_default(); + //Can only be called from the main thread! + //Merger calls this + //_material_cache->initial_setup_default(); _material_cache->prop_add_textures(_prop_instace->get_prop_data()); diff --git a/singleton/prop_cache.cpp b/singleton/prop_cache.cpp index 06166e9..482559a 100644 --- a/singleton/prop_cache.cpp +++ b/singleton/prop_cache.cpp @@ -170,13 +170,9 @@ void PropCache::materials_load() { } void PropCache::ensure_materials_loaded() { - _material_mutex.lock(); - if (_materials.size() != _material_paths.size()) { materials_load(); } - - _material_mutex.unlock(); } Vector PropCache::materials_get() { diff --git a/singleton/prop_cache.h b/singleton/prop_cache.h index 9a1b685..e588c91 100644 --- a/singleton/prop_cache.h +++ b/singleton/prop_cache.h @@ -122,8 +122,6 @@ protected: Mutex _tiled_wall_material_cache_mutex; Mutex _custom_keyed_material_cache_mutex; - Mutex _material_mutex; - #ifdef TEXTURE_PACKER_PRESENT int _texture_flags; int _max_atlas_size; diff --git a/tiled_wall/tiled_wall.cpp b/tiled_wall/tiled_wall.cpp index 765d96b..223bcc1 100644 --- a/tiled_wall/tiled_wall.cpp +++ b/tiled_wall/tiled_wall.cpp @@ -155,6 +155,7 @@ void TiledWall::refresh() { //An anouther thread could have initialized it before wo got the mutex! if (!_cache->get_initialized()) { + //can only be called from the main thread! _cache->initial_setup_default(); _data->setup_cache(_cache);