From 030eee10f3aa1c5d2ca349683b6252b18f0c2f14 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 23 Feb 2022 08:23:16 +0100 Subject: [PATCH] Removed multiple materials from the PropCache singleton. --- material_cache/prop_2d_material_cache.cpp | 11 +- singleton/prop_2d_cache.cpp | 120 +++++++--------------- singleton/prop_2d_cache.h | 23 ++--- 3 files changed, 52 insertions(+), 102 deletions(-) diff --git a/material_cache/prop_2d_material_cache.cpp b/material_cache/prop_2d_material_cache.cpp index af8e180..4e7b567 100644 --- a/material_cache/prop_2d_material_cache.cpp +++ b/material_cache/prop_2d_material_cache.cpp @@ -238,17 +238,16 @@ void Prop2DMaterialCache::refresh_rects() { void Prop2DMaterialCache::initial_setup_default() { //Note: call only on the main thread! Shader->duplicate() can crash if done from an another thread! + //Also shader duplication is synchronized with the main thread. So you can cause daedlocks if you hold up the main thread + //Somwhere else. Prop2DCache *pc = Prop2DCache::get_singleton(); - pc->ensure_materials_loaded(); + pc->ensure_material_loaded(); - int matc = pc->material_get_num(); - for (int i = 0; i < matc; ++i) { - Ref m = pc->material_get(i); - - ERR_CONTINUE(!m.is_valid()); + Ref m = pc->material_get(); + if (m.is_valid()) { Ref md = m->duplicate(); _material = md; diff --git a/singleton/prop_2d_cache.cpp b/singleton/prop_2d_cache.cpp index 08d7c49..9dea661 100644 --- a/singleton/prop_2d_cache.cpp +++ b/singleton/prop_2d_cache.cpp @@ -116,77 +116,41 @@ void Prop2DCache::set_margin(const int margin) { } #endif -PoolStringArray Prop2DCache::material_paths_get() const { - return _material_paths; +String Prop2DCache::material_path_get() const { + return _material_path; } -void Prop2DCache::material_paths_set(const PoolStringArray &value) { - _material_paths = value; +void Prop2DCache::material_path_set(const String &value) { + _material_path = value; } -void Prop2DCache::material_add(const Ref &value) { - ERR_FAIL_COND(!value.is_valid()); - - _materials.push_back(value); +Ref Prop2DCache::material_get() { + return _material; } -Ref Prop2DCache::material_get(const int index) { - ERR_FAIL_INDEX_V(index, _materials.size(), Ref()); - - return _materials[index]; +void Prop2DCache::material_set(const Ref &value) { + _material = value; } -void Prop2DCache::material_set(const int index, const Ref &value) { - ERR_FAIL_INDEX(index, _materials.size()); +void Prop2DCache::material_load() { + _material.unref(); - _materials.set(index, value); -} - -void Prop2DCache::material_remove(const int index) { - _materials.remove(index); -} - -int Prop2DCache::material_get_num() const { - return _materials.size(); -} - -void Prop2DCache::materials_clear() { - _materials.clear(); -} - -void Prop2DCache::materials_load() { - _materials.clear(); - - for (int i = 0; i < _material_paths.size(); ++i) { - StringName path = _material_paths[i]; - - ERR_CONTINUE(path == ""); - - Ref d = load_resource(path, "Material"); - - ERR_CONTINUE(!d.is_valid()); - - _materials.push_back(d); + if (_material_path == "") { + return; } + + Ref d = load_resource(_material_path, "Material"); + + ERR_FAIL_COND(!d.is_valid()); + + _material = d; } -void Prop2DCache::ensure_materials_loaded() { - if (_materials.size() != _material_paths.size()) { - materials_load(); +void Prop2DCache::ensure_material_loaded() { + if (_material_path == "" || _material.is_valid()) { + return; } -} -Vector Prop2DCache::materials_get() { - VARIANT_ARRAY_GET(_materials); -} - -void Prop2DCache::materials_set(const Vector &materials) { - _materials.clear(); - - for (int i = 0; i < materials.size(); i++) { - Ref material = Ref(materials[i]); - - _materials.push_back(material); - } + material_load(); } Ref Prop2DCache::material_cache_get(const Ref &prop) { @@ -369,25 +333,25 @@ Prop2DCache::Prop2DCache() { _instance = this; #if TEXTURE_PACKER_PRESENT - _default_prop_material_cache_class = GLOBAL_DEF("props/default_prop_material_cache_class", "Prop2DMaterialCachePCM"); + _default_prop_material_cache_class = GLOBAL_DEF("props_2d/default_prop_material_cache_class", "Prop2DMaterialCachePCM"); #else - _default_prop_material_cache_class = GLOBAL_DEF("props/default_prop_material_cache_class", "Prop2DMaterialCache"); + _default_prop_material_cache_class = GLOBAL_DEF("props_2d/default_prop_material_cache_class", "Prop2DMaterialCache"); #endif #ifdef TEXTURE_PACKER_PRESENT #if VERSION_MAJOR < 4 - _texture_flags = GLOBAL_DEF("props/texture_flags", Texture::FLAG_MIPMAPS | Texture::FLAG_FILTER); + _texture_flags = GLOBAL_DEF("props_2d/texture_flags", Texture::FLAG_MIPMAPS | Texture::FLAG_FILTER); #else - _texture_flags = GLOBAL_DEF("props/texture_flags", 0); + _texture_flags = GLOBAL_DEF("props_2d/texture_flags", 0); #endif - _max_atlas_size = GLOBAL_DEF("props/max_atlas_size", 1024); - _keep_original_atlases = GLOBAL_DEF("props/keep_original_atlases", false); - _background_color = GLOBAL_DEF("props/background_color", Color()); - _margin = GLOBAL_DEF("props/margin", 0); + _max_atlas_size = GLOBAL_DEF("props_2d/max_atlas_size", 1024); + _keep_original_atlases = GLOBAL_DEF("props_2d/keep_original_atlases", false); + _background_color = GLOBAL_DEF("props_2d/background_color", Color()); + _margin = GLOBAL_DEF("props_2d/margin", 0); #endif - _material_paths = GLOBAL_DEF("props/material_paths", PoolStringArray()); + _material_path = GLOBAL_DEF("props_2d/material_path", ""); } Prop2DCache::~Prop2DCache() { @@ -421,22 +385,16 @@ void Prop2DCache::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::INT, "margin"), "set_margin", "get_margin"); #endif - ClassDB::bind_method(D_METHOD("material_paths_get"), &Prop2DCache::material_paths_get); - ClassDB::bind_method(D_METHOD("material_paths_set", "value"), &Prop2DCache::material_paths_set); - ADD_PROPERTY(PropertyInfo(Variant::POOL_STRING_ARRAY, "material_paths"), "material_paths_set", "material_paths_get"); + ClassDB::bind_method(D_METHOD("material_path_get"), &Prop2DCache::material_path_get); + ClassDB::bind_method(D_METHOD("material_path_set", "value"), &Prop2DCache::material_path_set); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "material_path"), "material_path_set", "material_path_get"); - ClassDB::bind_method(D_METHOD("material_add", "value"), &Prop2DCache::material_add); - ClassDB::bind_method(D_METHOD("material_get", "index"), &Prop2DCache::material_get); - ClassDB::bind_method(D_METHOD("material_set", "index", "value"), &Prop2DCache::material_set); - ClassDB::bind_method(D_METHOD("material_remove", "index"), &Prop2DCache::material_remove); - ClassDB::bind_method(D_METHOD("material_get_num"), &Prop2DCache::material_get_num); - ClassDB::bind_method(D_METHOD("materials_clear"), &Prop2DCache::materials_clear); - ClassDB::bind_method(D_METHOD("materials_load"), &Prop2DCache::materials_load); - ClassDB::bind_method(D_METHOD("ensure_materials_loaded"), &Prop2DCache::ensure_materials_loaded); + ClassDB::bind_method(D_METHOD("material_get"), &Prop2DCache::material_get); + ClassDB::bind_method(D_METHOD("material_set", "value"), &Prop2DCache::material_set); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "material_set", "material_get"); - ClassDB::bind_method(D_METHOD("materials_get"), &Prop2DCache::materials_get); - ClassDB::bind_method(D_METHOD("materials_set"), &Prop2DCache::materials_set); - ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "materials", PROPERTY_HINT_NONE, "17/17:Material", PROPERTY_USAGE_DEFAULT, "Material"), "materials_set", "materials_get"); + ClassDB::bind_method(D_METHOD("material_load"), &Prop2DCache::material_load); + ClassDB::bind_method(D_METHOD("ensure_material_loaded"), &Prop2DCache::ensure_material_loaded); ClassDB::bind_method(D_METHOD("material_cache_get", "prop"), &Prop2DCache::material_cache_get); ClassDB::bind_method(D_METHOD("material_cache_unref", "prop"), &Prop2DCache::material_cache_unref); diff --git a/singleton/prop_2d_cache.h b/singleton/prop_2d_cache.h index de66680..770af5a 100644 --- a/singleton/prop_2d_cache.h +++ b/singleton/prop_2d_cache.h @@ -76,20 +76,13 @@ public: void set_margin(const int margin); #endif - PoolStringArray material_paths_get() const; - void material_paths_set(const PoolStringArray &array); + String material_path_get() const; + void material_path_set(const String &array); - void material_add(const Ref &value); - Ref material_get(const int index); - void material_set(const int index, const Ref &value); - void material_remove(const int index); - int material_get_num() const; - void materials_clear(); - void materials_load(); - void ensure_materials_loaded(); - - Vector materials_get(); - void materials_set(const Vector &materials); + Ref material_get(); + void material_set(const Ref &value); + void material_load(); + void ensure_material_loaded(); Ref material_cache_get(const Ref &prop); void material_cache_unref(const Ref &prop); @@ -130,8 +123,8 @@ protected: int _margin; #endif - PoolStringArray _material_paths; - Vector> _materials; + String _material_path; + Ref _material; }; #endif