From 69f2bc27517a0a352f0a7859de16c2fafa7a0264 Mon Sep 17 00:00:00 2001 From: RedworkDE <10944644+RedworkDE@users.noreply.github.com> Date: Mon, 22 May 2023 13:07:56 +0200 Subject: [PATCH] Improve `SelfList` and fix error in `BaseMaterial3D` when running doctool --- core/templates/self_list.h | 11 ++++++++++- scene/resources/material.cpp | 16 +++++++--------- scene/resources/material.h | 2 +- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/core/templates/self_list.h b/core/templates/self_list.h index c3d7391d6c5..ff6fa953ae0 100644 --- a/core/templates/self_list.h +++ b/core/templates/self_list.h @@ -99,11 +99,20 @@ public: p_elem->_root = nullptr; } + void clear() { + while (_first) { + remove(_first); + } + } + _FORCE_INLINE_ SelfList *first() { return _first; } _FORCE_INLINE_ const SelfList *first() const { return _first; } _FORCE_INLINE_ List() {} - _FORCE_INLINE_ ~List() { ERR_FAIL_COND(_first != nullptr); } + _FORCE_INLINE_ ~List() { + // A self list must be empty on destruction. + DEV_ASSERT(_first == nullptr); + } }; private: diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp index d35c49b2663..5752f4cc1a8 100644 --- a/scene/resources/material.cpp +++ b/scene/resources/material.cpp @@ -475,13 +475,11 @@ ShaderMaterial::~ShaderMaterial() { ///////////////////////////////// Mutex BaseMaterial3D::material_mutex; -SelfList::List *BaseMaterial3D::dirty_materials = nullptr; +SelfList::List BaseMaterial3D::dirty_materials; HashMap BaseMaterial3D::shader_map; BaseMaterial3D::ShaderNames *BaseMaterial3D::shader_names = nullptr; void BaseMaterial3D::init_shaders() { - dirty_materials = memnew(SelfList::List); - shader_names = memnew(ShaderNames); shader_names->albedo = "albedo"; @@ -568,14 +566,14 @@ HashMap> BaseMaterial3D::materials_for_2d; void BaseMaterial3D::finish_shaders() { materials_for_2d.clear(); - memdelete(dirty_materials); - dirty_materials = nullptr; + dirty_materials.clear(); memdelete(shader_names); + shader_names = nullptr; } void BaseMaterial3D::_update_shader() { - dirty_materials->remove(&element); + dirty_materials.remove(&element); MaterialKey mk = _compute_key(); if (mk == current_key) { @@ -1494,8 +1492,8 @@ void BaseMaterial3D::_update_shader() { void BaseMaterial3D::flush_changes() { MutexLock lock(material_mutex); - while (dirty_materials->first()) { - dirty_materials->first()->self()->_update_shader(); + while (dirty_materials.first()) { + dirty_materials.first()->self()->_update_shader(); } } @@ -1503,7 +1501,7 @@ void BaseMaterial3D::_queue_shader_change() { MutexLock lock(material_mutex); if (_is_initialized() && !element.in_list()) { - dirty_materials->add(&element); + dirty_materials.add(&element); } } diff --git a/scene/resources/material.h b/scene/resources/material.h index b70522dda1e..677ddf30d88 100644 --- a/scene/resources/material.h +++ b/scene/resources/material.h @@ -452,7 +452,7 @@ private: }; static Mutex material_mutex; - static SelfList::List *dirty_materials; + static SelfList::List dirty_materials; static ShaderNames *shader_names; SelfList element;