From bcecfe29645ab7a16b1bc330a1b040a89965b91b Mon Sep 17 00:00:00 2001 From: maj00r Date: Mon, 16 Nov 2026 15:59:57 +0100 Subject: [PATCH] refactor(renderer): a material's textures let go of in one place, for every backend Unload_Material walked the textures of a material and of its variants the same way in both opengl renderers, and Sonar counted its declarations, set into the long run of overrides the two headers share, as duplicated: 5.2% of the new code, 3% allowed. The walk is gfx_renderer's now, over IMaterial::GetAllTextures and the backend's Unload_Texture. Materials that don't tell their textures, the Vulkan renderer's, give none, and are left alone as before. Co-Authored-By: Claude Opus 5.5 --- global_include/interfaces/IMaterial.h | 6 ++++++ model/material.h | 7 +++++++ rendering/opengl33renderer.cpp | 12 ------------ rendering/opengl33renderer.h | 2 -- rendering/openglrenderer.cpp | 15 --------------- rendering/openglrenderer.h | 2 -- rendering/renderer.cpp | 10 ++++++++++ rendering/renderer.h | 2 +- 8 files changed, 24 insertions(+), 32 deletions(-) diff --git a/global_include/interfaces/IMaterial.h b/global_include/interfaces/IMaterial.h index c6f3d1237..2d16a43b0 100644 --- a/global_include/interfaces/IMaterial.h +++ b/global_include/interfaces/IMaterial.h @@ -3,6 +3,7 @@ #include #include #include +#include #include "ITexture.h" @@ -17,6 +18,11 @@ struct IMaterial virtual std::optional GetSelfillum() const = 0; virtual int GetShadowRank() const = 0; virtual texture_handle GetTexture(int slot) const = 0; + // every texture the material draws with, those of all its variants included. materials which don't tell give none + virtual std::vector GetAllTextures() const + { + return {}; + } static IMaterial *null_material() { struct NullMaterial : public IMaterial diff --git a/model/material.h b/model/material.h index 82d14557c..4b7323f76 100644 --- a/model/material.h +++ b/model/material.h @@ -66,6 +66,13 @@ struct opengl_material : public IMaterial { { return textures[slot]; } + virtual std::vector GetAllTextures() const override + { + std::vector all(textures.begin(), textures.end()); + for (auto const &variant : texture_variants) + all.insert(all.end(), variant.begin(), variant.end()); + return all; + } // returns texture set to use for model instance identified by provided variant id texture_set const &GetTextures(std::uint32_t const Variant) const { diff --git a/rendering/opengl33renderer.cpp b/rendering/opengl33renderer.cpp index d3dfef6e9..800f33dcf 100644 --- a/rendering/opengl33renderer.cpp +++ b/rendering/opengl33renderer.cpp @@ -2575,18 +2575,6 @@ void opengl33_renderer::Unload_Texture(texture_handle const Texture) m_textures.unload(Texture); } -void opengl33_renderer::Unload_Material(material_handle const Material) -{ - if (Material == null_handle) - return; - auto const &material{m_materials.material(Material)}; - for (auto const texture : material.textures) - m_textures.unload(texture); - for (auto const &variant : material.texture_variants) - for (auto const texture : variant) - m_textures.unload(texture); -} - void opengl33_renderer::Update_AnimModel(TAnimModel *model) { model->RaAnimate(m_framestamp); diff --git a/rendering/opengl33renderer.h b/rendering/opengl33renderer.h index c4af00c4f..4322cc2db 100644 --- a/rendering/opengl33renderer.h +++ b/rendering/opengl33renderer.h @@ -112,8 +112,6 @@ class opengl33_renderer : public gfx_renderer { Texture( texture_handle const Texture ) const override; void Unload_Texture( texture_handle const Texture ) override; - void - Unload_Material( material_handle const Material ) override; // utility methods void Pick_Control_Callback( std::function Callback ) override; diff --git a/rendering/openglrenderer.cpp b/rendering/openglrenderer.cpp index 9f48c40b5..737c4b379 100644 --- a/rendering/openglrenderer.cpp +++ b/rendering/openglrenderer.cpp @@ -1928,21 +1928,6 @@ opengl_renderer::Unload_Texture( texture_handle const Texture ) { m_textures.unload( Texture ); } -void -opengl_renderer::Unload_Material( material_handle const Material ) { - - if( Material == null_handle ) { return; } - auto const &material { m_materials.material( Material ) }; - for( auto const texture : material.textures ) { - m_textures.unload( texture ); - } - for( auto const &variant : material.texture_variants ) { - for( auto const texture : variant ) { - m_textures.unload( texture ); - } - } -} - void opengl_renderer::Pick_Control_Callback( std::function Callback ) { diff --git a/rendering/openglrenderer.h b/rendering/openglrenderer.h index 768acc19a..ef3a9f0f3 100644 --- a/rendering/openglrenderer.h +++ b/rendering/openglrenderer.h @@ -97,8 +97,6 @@ public: Texture( texture_handle const Texture ) const override; void Unload_Texture( texture_handle const Texture ) override; - void - Unload_Material( material_handle const Material ) override; // utility methods void Pick_Control_Callback( std::function Callback ) override; diff --git a/rendering/renderer.cpp b/rendering/renderer.cpp index d9d4b5df0..7e7d93e94 100644 --- a/rendering/renderer.cpp +++ b/rendering/renderer.cpp @@ -39,6 +39,16 @@ gfx_renderer_factory *gfx_renderer_factory::get_instance() gfx_renderer_factory *gfx_renderer_factory::instance; +void gfx_renderer::Unload_Material( material_handle const Material ) +{ + if( Material == null_handle ) { + return; + } + for( auto const texture : this->Material( Material )->GetAllTextures() ) { + Unload_Texture( texture ); + } +} + material_handle gfx_renderer::Terrain_Material( material_handle const Reuse, std::vector const &Layers, int const Samples, std::uint8_t const *Weights, glm::vec3 const &Placement ) { // without blending the chunk gets the material which covers the most of it diff --git a/rendering/renderer.h b/rendering/renderer.h index 7e3ef2eb2..7edf135da 100644 --- a/rendering/renderer.h +++ b/rendering/renderer.h @@ -106,7 +106,7 @@ public: // texture picker scrolled past. backends which keep their textures otherwise leave it virtual void Unload_Texture( texture_handle const Texture ) {} // the same for all the textures of a material, those of its variants with them - virtual void Unload_Material( material_handle const Material ) {} + void Unload_Material( material_handle const Material ); // utility methods virtual void Pick_Control_Callback( std::function Callback ) = 0; virtual void Pick_Node_Callback( std::function Callback ) = 0;