Added an api to get MaterialCaches from the PropCache singleton. Also changed toe default_prop_material_cache_class property's type to stringname.

This commit is contained in:
Relintai 2021-08-09 16:56:11 +02:00
parent e0db2189be
commit b15ac01225
2 changed files with 96 additions and 5 deletions

View File

@ -45,19 +45,94 @@ SOFTWARE.
#include "../../texture_packer/texture_packer.h"
#endif
#include "../material_cache/prop_material_cache.h"
#include "core/hashfuncs.h"
PropCache *PropCache::_instance;
PropCache *PropCache::get_singleton() {
return _instance;
}
String PropCache::get_default_prop_material_cache_class() {
StringName PropCache::get_default_prop_material_cache_class() {
return _default_prop_material_cache_class;
}
void PropCache::set_default_prop_material_cache_class(const String &cls_name) {
void PropCache::set_default_prop_material_cache_class(const StringName &cls_name) {
_default_prop_material_cache_class = cls_name;
}
Ref<PropMaterialCache> PropCache::material_cache_get(const Ref<PropData> &prop) {
ERR_FAIL_COND_V(!prop.is_valid(), Ref<PropMaterialCache>());
//get pointer's value as uint64
uint64_t k = make_uint64_t<const PropData *>(*prop);
_material_cache_mutex.lock();
if (_material_cache.has(k)) {
Ref<PropMaterialCache> m = _material_cache[k];
m->inc_ref_count();
_material_cache_mutex.unlock();
return m;
}
PropMaterialCache *p = Object::cast_to<PropMaterialCache>(ClassDB::instance(_default_prop_material_cache_class));
if (!p) {
ERR_PRINT("Can't instance the given PropMaterialCache! class_name: " + String(_default_prop_material_cache_class));
}
Ref<PropMaterialCache> m(p);
_material_cache[k] = m;
_material_cache_mutex.unlock();
return m;
}
void PropCache::material_cache_unref(const Ref<PropData> &prop) {
_material_cache_mutex.lock();
_material_cache_mutex.unlock();
}
Ref<PropMaterialCache> PropCache::material_cache_custom_key_get(const uint64_t key) {
_custom_keyed_material_cache_mutex.lock();
if (_custom_keyed_material_cache.has(key)) {
Ref<PropMaterialCache> m = _custom_keyed_material_cache[key];
m->inc_ref_count();
_custom_keyed_material_cache_mutex.unlock();
return m;
}
PropMaterialCache *p = Object::cast_to<PropMaterialCache>(ClassDB::instance(_default_prop_material_cache_class));
if (!p) {
ERR_PRINT("Can't instance the given PropMaterialCache! class_name: " + String(_default_prop_material_cache_class));
}
Ref<PropMaterialCache> m(p);
_custom_keyed_material_cache[key] = m;
_custom_keyed_material_cache_mutex.unlock();
return m;
}
void PropCache::material_cache_custom_key_unref(const uint64_t key) {
_custom_keyed_material_cache_mutex.lock();
_custom_keyed_material_cache_mutex.unlock();
}
#if TEXTURE_PACKER_PRESENT
bool PropCache::has_texture(const Ref<PropData> &prop) {
for (int i = 0; i < _entries.size(); ++i) {

View File

@ -37,12 +37,16 @@ SOFTWARE.
#include "core/vector.h"
#endif
#include "core/os/mutex.h"
#include "../props/prop_data.h"
#if TEXTURE_PACKER_PRESENT
class TexturePacker;
#endif
class PropMaterialCache;
class PropCache : public Object {
GDCLASS(PropCache, Object);
@ -57,8 +61,14 @@ public:
public:
static PropCache *get_singleton();
String get_default_prop_material_cache_class();
void set_default_prop_material_cache_class(const String& cls_name);
StringName get_default_prop_material_cache_class();
void set_default_prop_material_cache_class(const StringName &cls_name);
Ref<PropMaterialCache> material_cache_get(const Ref<PropData> &prop);
void material_cache_unref(const Ref<PropData> &prop);
Ref<PropMaterialCache> material_cache_custom_key_get(const uint64_t key);
void material_cache_custom_key_unref(const uint64_t key);
bool has_texture(const Ref<PropData> &prop);
void set_texture(const Ref<PropData> &prop, const Ref<TexturePacker> &merger);
@ -86,7 +96,13 @@ public:
protected:
static void _bind_methods();
String _default_prop_material_cache_class;
StringName _default_prop_material_cache_class;
Map<uint64_t, Ref<PropMaterialCache>> _material_cache;
Map<uint64_t, Ref<PropMaterialCache>> _custom_keyed_material_cache;
Mutex _material_cache_mutex;
Mutex _custom_keyed_material_cache_mutex;
};
#endif