From 4525023777a7c007603ab21b131dc158866d8b46 Mon Sep 17 00:00:00 2001 From: maj00r Date: Mon, 16 Nov 2026 01:54:40 +0100 Subject: [PATCH 1/4] fix(editor): the texture pickers let go of the images scrolled past, the memory no longer runs out The picker of the textures of a track offers every texture of the textures folder, and each tile that came into sight read its texture full size, into the graphics card and the memory, and kept it there for good. Scrolling through the list for a trackbed texture took about 2.5 MB a tile: some 900 tiles made 3.5 GB, the whole folder 28 GB, and the game went down once the memory ran out. An image out of sight for 3 seconds is now taken out of both again (texture_manager::unload, Unload_Texture of the renderers) and read anew when it's shown once more, a few a frame as before. Only a texture the scene has never drawn is let go; those of the scene stay to the resource sweep. The texture browser lets go of its images on Refresh too. The picker no longer lists textures/previews: the 4100 previews of the node bank are images of models, not textures of anything. Checked on td.scn by a scripted run: 8000 images of the picker read one batch a few frames after another held the game at 2-3 GB, where it went to 28 GB and fell before; images shown again after they were let go come back; a trackbed texture picked from the folder is drawn on the track after its image was let go. --- application/editormode.cpp | 1 + application/editormode.h | 10 +++++++++- application/editormodetextures.cpp | 28 +++++++++++++++++++++++++--- application/editormodetrackprops.cpp | 6 ++++-- model/Texture.cpp | 28 ++++++++++++++++++++++++++++ model/Texture.h | 4 ++++ rendering/opengl33renderer.cpp | 5 +++++ rendering/opengl33renderer.h | 2 ++ rendering/openglrenderer.cpp | 6 ++++++ rendering/openglrenderer.h | 2 ++ rendering/renderer.h | 3 +++ 11 files changed, 89 insertions(+), 6 deletions(-) diff --git a/application/editormode.cpp b/application/editormode.cpp index e08e1f0e7..ab150cb7b 100644 --- a/application/editormode.cpp +++ b/application/editormode.cpp @@ -1398,6 +1398,7 @@ bool editor_mode::update() Global.editor_tracks = (current_work_area() == work_area::tracks); selftest_step(); steps.step("selftest"); + thumbnails_trim(); // the drives of the switches go to the tips of their blades once, as the templates of the scenery are known if (false == m_drives_checked) { diff --git a/application/editormode.h b/application/editormode.h index bcf22592c..68746cdf4 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -745,13 +745,21 @@ class editor_mode : public application_mode, private editor_track::observer, pri char filter[64]{}; float size{96.f}; // of the images, pixels std::string selected; - std::map thumbnails; // texture shown for the item of the name, 0: none + struct thumbnail + { + int texture{0}; // 0: none + double shown{0.0}; // when it was last asked for, ImGui time + }; + std::map thumbnails; // texture shown for the item of the name int loads{0}; // textures read in the frame, a few at most so the browser doesn't stall the editor int loadsframe{-1}; } m_textures; void render_texture_browser(); // texture of the image of the item of the browser, 0 while there's none std::uint64_t texture_thumbnail(texture_browser::item const &Item); + // the textures of the images out of sight for a while taken out of the memory: a picker of thousands of them scrolled + // through would keep them all, full size, until the memory ran out + void thumbnails_trim(); // takes a material dragged from the texture browser onto the item drawn last. returns: true if one was dropped, its name in Name static bool material_drop(std::string &Name); // the same, for a text field diff --git a/application/editormodetextures.cpp b/application/editormodetextures.cpp index 155210a87..8d5fcd3be 100644 --- a/application/editormodetextures.cpp +++ b/application/editormodetextures.cpp @@ -102,16 +102,36 @@ std::uint64_t editor_mode::texture_thumbnail(texture_browser::item const &Item) } else texture = GfxRenderer->Fetch_Texture(Item.name, true); - lookup = browser.thumbnails.emplace(Item.name, texture).first; + lookup = browser.thumbnails.emplace(Item.name, texture_browser::thumbnail{texture}).first; } - if (lookup->second == null_handle) + lookup->second.shown = ImGui::GetTime(); + if (lookup->second.texture == null_handle) return 0; - auto &texture{GfxRenderer->Texture(lookup->second)}; + auto &texture{GfxRenderer->Texture(lookup->second.texture)}; if (false == texture.get_is_ready()) texture.create(); return texture.get_is_ready() ? static_cast(texture.get_id()) : 0; } +void editor_mode::thumbnails_trim() +{ + // a few seconds out of sight, so scrolling back and forth doesn't read them over and over + auto constexpr kept{3.0}; + auto const now{ImGui::GetTime()}; + auto &thumbnails{m_textures.thumbnails}; + for (auto entry{thumbnails.begin()}; entry != thumbnails.end();) + { + if (entry->second.texture == null_handle || now - entry->second.shown < kept) + { + ++entry; + continue; + } + // the scene's own textures stay; shown again, the image is read anew within the few a frame + GfxRenderer->Unload_Texture(entry->second.texture); + entry = thumbnails.erase(entry); + } +} + void editor_mode::render_texture_browser() { auto &browser{m_textures}; @@ -173,6 +193,8 @@ void editor_mode::render_texture_browser() if (ImGui::Button(STR_C("Refresh"))) { browser.listed = false; + for (auto const &[name, thumbnail] : browser.thumbnails) + GfxRenderer->Unload_Texture(thumbnail.texture); browser.thumbnails.clear(); } ImGui::SameLine(); diff --git a/application/editormodetrackprops.cpp b/application/editormodetrackprops.cpp index 13a9d2a49..4542d0225 100644 --- a/application/editormodetrackprops.cpp +++ b/application/editormodetrackprops.cpp @@ -1043,8 +1043,10 @@ void editor_mode::textures_index() { if (entry->is_directory(ignored)) { - // the normal maps go with their textures, not on a track by themselves - if (lowered(entry->path().filename().string()) == "normals") + // the normal maps go with their textures, not on a track by themselves; the previews of the node bank, thousands of + // them, are images of models and not textures of anything + auto const folder{lowered(entry->path().filename().string())}; + if (folder == "normals" || (folder == "previews" && entry.depth() == 0)) entry.disable_recursion_pending(); continue; } diff --git a/model/Texture.cpp b/model/Texture.cpp index cd275c9e0..36757bd08 100644 --- a/model/Texture.cpp +++ b/model/Texture.cpp @@ -1886,6 +1886,34 @@ texture_manager::mark_as_needed( texture_handle const Texture, bool const Near ) } } +void +texture_manager::unload( texture_handle const Texture ) { + + if( ( Texture <= 0 ) + || ( Texture >= static_cast( m_textures.size() ) ) ) { + return; + } + auto const &record { m_textures[ Texture ] }; + auto &texture { *record.first }; + if( ( record.second != resource_timestamp() ) + || ( texture.id == opengl_texture::invalid_id ) + || ( texture.id == 0 ) + || ( true == texture.is_static ) + || ( true == texture.is_rendertarget ) + || ( texture.type == "make:" ) + || ( texture.type == "internalsrc:" ) ) { + return; + } + texture.release(); + texture.data = std::vector(); + texture.data_state = resource_state::none; + texture.reload_on_use = true; + // the released gl name can be given to a new texture, the unit cache can't trust it + for( auto &unit : opengl_texture::units ) { + unit = -1; + } +} + // releases gl textures of vehicles not needed nor drawn for a while void texture_manager::release_unneeded() { diff --git a/model/Texture.h b/model/Texture.h index 313d0f6f4..d177592cc 100644 --- a/model/Texture.h +++ b/model/Texture.h @@ -181,6 +181,10 @@ public: // releases gl textures of vehicles not needed nor drawn for a while void release_unneeded(); + // takes a texture the scene has never drawn out of both the gfx card and the memory, e.g. the image of a picker scrolled + // past; it's read again when it's used. textures the scene draws are left to the resource sweep + void + unload( texture_handle const Texture ); // debug performance string std::string info() const; diff --git a/rendering/opengl33renderer.cpp b/rendering/opengl33renderer.cpp index 5eb98222b..800f33dcf 100644 --- a/rendering/opengl33renderer.cpp +++ b/rendering/opengl33renderer.cpp @@ -2570,6 +2570,11 @@ ITexture const &opengl33_renderer::Texture(texture_handle const Texture) const return m_textures.texture(Texture); } +void opengl33_renderer::Unload_Texture(texture_handle const Texture) +{ + 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 807563c40..4322cc2db 100644 --- a/rendering/opengl33renderer.h +++ b/rendering/opengl33renderer.h @@ -110,6 +110,8 @@ class opengl33_renderer : public gfx_renderer { Texture( texture_handle const Texture ) override; ITexture const & Texture( texture_handle const Texture ) const override; + void + Unload_Texture( texture_handle const Texture ) override; // utility methods void Pick_Control_Callback( std::function Callback ) override; diff --git a/rendering/openglrenderer.cpp b/rendering/openglrenderer.cpp index 71bd7d0ef..737c4b379 100644 --- a/rendering/openglrenderer.cpp +++ b/rendering/openglrenderer.cpp @@ -1922,6 +1922,12 @@ opengl_renderer::Texture( texture_handle const Texture ) const { return m_textures.texture( Texture ); } +void +opengl_renderer::Unload_Texture( texture_handle const Texture ) { + + m_textures.unload( Texture ); +} + void opengl_renderer::Pick_Control_Callback( std::function Callback ) { diff --git a/rendering/openglrenderer.h b/rendering/openglrenderer.h index ecb8a3c50..ef3a9f0f3 100644 --- a/rendering/openglrenderer.h +++ b/rendering/openglrenderer.h @@ -95,6 +95,8 @@ public: Texture( texture_handle const Texture ) override; ITexture const & Texture( texture_handle const Texture ) const override; + void + Unload_Texture( texture_handle const Texture ) override; // utility methods void Pick_Control_Callback( std::function Callback ) override; diff --git a/rendering/renderer.h b/rendering/renderer.h index 5ad805c4a..337d63925 100644 --- a/rendering/renderer.h +++ b/rendering/renderer.h @@ -102,6 +102,9 @@ public: virtual void Bind_Texture( std::size_t const Unit, texture_handle const Texture ) = 0; virtual auto Texture( texture_handle const Texture ) -> ITexture & = 0; virtual auto Texture( texture_handle const Texture ) const -> ITexture const & = 0; + // a texture the scene has never drawn taken out of the gfx card and the memory, read again when it's used: the image of a + // texture picker scrolled past. backends which keep their textures otherwise leave it + virtual void Unload_Texture( texture_handle const Texture ) {} // utility methods virtual void Pick_Control_Callback( std::function Callback ) = 0; virtual void Pick_Node_Callback( std::function Callback ) = 0; From 0bbf07bf1191fb430640b21ef0d7e1ffa60b2926 Mon Sep 17 00:00:00 2001 From: maj00r Date: Mon, 16 Nov 2026 02:10:59 +0100 Subject: [PATCH 2/4] fix(editor): the images of the materials let go of with all their textures, the browser's Refresh a frame later An image of a material read the material with all its textures, the normal and specular maps and the variants, and only the first one was let go of: those stayed in the memory for every material scrolled past. They are let go of together now (Unload_Material). texture_manager::unload also takes the textures read and not made yet, waiting for a worker still reading one, and deletes the gl texture itself: release() would read it back from the card with gfx.resource.move only for it to be dropped. The Refresh of the texture browser let go of the images in the middle of the frame, while panels drawn before it could still refer to them; it now marks them, and they go at the start of the next frame. Checked on td.scn by a scripted run: 8040 images of the picker read a batch every few frames end at 2.0 GB, against 2.3-2.6 GB before this and 1.1 GB at the start; a trackbed material picked from the folder is drawn on the track after its image was let go. --- application/editormode.h | 2 ++ application/editormodetextures.cpp | 35 ++++++++++++++++++------------ model/Texture.cpp | 32 +++++++++++++++++++++------ model/Texture.h | 2 +- rendering/opengl33renderer.cpp | 12 ++++++++++ rendering/opengl33renderer.h | 2 ++ rendering/openglrenderer.cpp | 15 +++++++++++++ rendering/openglrenderer.h | 2 ++ rendering/renderer.h | 2 ++ 9 files changed, 82 insertions(+), 22 deletions(-) diff --git a/application/editormode.h b/application/editormode.h index 68746cdf4..81347c950 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -748,9 +748,11 @@ class editor_mode : public application_mode, private editor_track::observer, pri struct thumbnail { int texture{0}; // 0: none + int material{0}; // the material the texture is the first of, 0: a texture alone double shown{0.0}; // when it was last asked for, ImGui time }; std::map thumbnails; // texture shown for the item of the name + bool refresh{false}; // all the images to be let go of and read again int loads{0}; // textures read in the frame, a few at most so the browser doesn't stall the editor int loadsframe{-1}; } m_textures; diff --git a/application/editormodetextures.cpp b/application/editormodetextures.cpp index 8d5fcd3be..ecf846431 100644 --- a/application/editormodetextures.cpp +++ b/application/editormodetextures.cpp @@ -93,16 +93,17 @@ std::uint64_t editor_mode::texture_thumbnail(texture_browser::item const &Item) if (browser.loads >= 2) return 0; ++browser.loads; - texture_handle texture{null_handle}; + texture_browser::thumbnail thumbnail; if (Item.material) { - auto const material{GfxRenderer->Fetch_Material(Item.name)}; - if (auto const *data{GfxRenderer->Material(material)}; material != null_handle && data != nullptr) - texture = data->GetTexture(0); + // a material reads all its textures, the normal maps and the rest with the one shown + thumbnail.material = GfxRenderer->Fetch_Material(Item.name); + if (auto const *data{GfxRenderer->Material(thumbnail.material)}; thumbnail.material != null_handle && data != nullptr) + thumbnail.texture = data->GetTexture(0); } else - texture = GfxRenderer->Fetch_Texture(Item.name, true); - lookup = browser.thumbnails.emplace(Item.name, texture_browser::thumbnail{texture}).first; + thumbnail.texture = GfxRenderer->Fetch_Texture(Item.name, true); + lookup = browser.thumbnails.emplace(Item.name, thumbnail).first; } lookup->second.shown = ImGui::GetTime(); if (lookup->second.texture == null_handle) @@ -116,20 +117,27 @@ std::uint64_t editor_mode::texture_thumbnail(texture_browser::item const &Item) void editor_mode::thumbnails_trim() { // a few seconds out of sight, so scrolling back and forth doesn't read them over and over + // all of them after the browser's Refresh, here before any image is drawn: not while the panels drawn earlier in the + // frame still refer to them auto constexpr kept{3.0}; auto const now{ImGui::GetTime()}; - auto &thumbnails{m_textures.thumbnails}; - for (auto entry{thumbnails.begin()}; entry != thumbnails.end();) + auto &browser{m_textures}; + for (auto entry{browser.thumbnails.begin()}; entry != browser.thumbnails.end();) { - if (entry->second.texture == null_handle || now - entry->second.shown < kept) + auto const &thumbnail{entry->second}; + if (false == browser.refresh && (thumbnail.texture == null_handle || now - thumbnail.shown < kept)) { ++entry; continue; } // the scene's own textures stay; shown again, the image is read anew within the few a frame - GfxRenderer->Unload_Texture(entry->second.texture); - entry = thumbnails.erase(entry); + if (thumbnail.material != null_handle) + GfxRenderer->Unload_Material(thumbnail.material); + else + GfxRenderer->Unload_Texture(thumbnail.texture); + entry = browser.thumbnails.erase(entry); } + browser.refresh = false; } void editor_mode::render_texture_browser() @@ -193,9 +201,8 @@ void editor_mode::render_texture_browser() if (ImGui::Button(STR_C("Refresh"))) { browser.listed = false; - for (auto const &[name, thumbnail] : browser.thumbnails) - GfxRenderer->Unload_Texture(thumbnail.texture); - browser.thumbnails.clear(); + // the images let go of at the start of the next frame, see thumbnails_trim() + browser.refresh = true; } ImGui::SameLine(); ImGui::TextUnformatted((std::string{paths::textures} + browser.folder).c_str()); diff --git a/model/Texture.cpp b/model/Texture.cpp index 36757bd08..eb24a0de6 100644 --- a/model/Texture.cpp +++ b/model/Texture.cpp @@ -1896,22 +1896,40 @@ texture_manager::unload( texture_handle const Texture ) { auto const &record { m_textures[ Texture ] }; auto &texture { *record.first }; if( ( record.second != resource_timestamp() ) - || ( texture.id == opengl_texture::invalid_id ) - || ( texture.id == 0 ) + || ( texture.id == 0 ) // failed to load || ( true == texture.is_static ) || ( true == texture.is_rendertarget ) || ( texture.type == "make:" ) || ( texture.type == "internalsrc:" ) ) { return; } - texture.release(); + if( ( texture.data_state == resource_state::loading ) + && ( texture.request != nullptr ) ) { + // read by a worker thread: taken over first, to be dropped with the rest + texture.complete_data(); + if( texture.data_state == resource_state::failed ) { + return; + } + } + auto const created { texture.id != opengl_texture::invalid_id }; + if( ( false == created ) + && ( texture.data_state != resource_state::good ) ) { + // nothing read nor made, e.g. the textures of a material asked for without loading + return; + } + if( true == created ) { + // deleted outright: release() would read the texture back from the card with gfx.resource.move, only for it to be dropped + ::glDeleteTextures( 1, &texture.id ); + texture.id = opengl_texture::invalid_id; + texture.is_ready = false; + // the released gl name can be given to a new texture, the unit cache can't trust it + for( auto &unit : opengl_texture::units ) { + unit = -1; + } + } texture.data = std::vector(); texture.data_state = resource_state::none; texture.reload_on_use = true; - // the released gl name can be given to a new texture, the unit cache can't trust it - for( auto &unit : opengl_texture::units ) { - unit = -1; - } } // releases gl textures of vehicles not needed nor drawn for a while diff --git a/model/Texture.h b/model/Texture.h index d177592cc..20221fcea 100644 --- a/model/Texture.h +++ b/model/Texture.h @@ -182,7 +182,7 @@ public: void release_unneeded(); // takes a texture the scene has never drawn out of both the gfx card and the memory, e.g. the image of a picker scrolled - // past; it's read again when it's used. textures the scene draws are left to the resource sweep + // past, or one read and not made yet; it's read again when it's used. textures the scene draws are left to the resource sweep void unload( texture_handle const Texture ); // debug performance string diff --git a/rendering/opengl33renderer.cpp b/rendering/opengl33renderer.cpp index 800f33dcf..d3dfef6e9 100644 --- a/rendering/opengl33renderer.cpp +++ b/rendering/opengl33renderer.cpp @@ -2575,6 +2575,18 @@ 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 4322cc2db..c4af00c4f 100644 --- a/rendering/opengl33renderer.h +++ b/rendering/opengl33renderer.h @@ -112,6 +112,8 @@ 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 737c4b379..9f48c40b5 100644 --- a/rendering/openglrenderer.cpp +++ b/rendering/openglrenderer.cpp @@ -1928,6 +1928,21 @@ 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 ef3a9f0f3..768acc19a 100644 --- a/rendering/openglrenderer.h +++ b/rendering/openglrenderer.h @@ -97,6 +97,8 @@ 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.h b/rendering/renderer.h index 337d63925..7e3ef2eb2 100644 --- a/rendering/renderer.h +++ b/rendering/renderer.h @@ -105,6 +105,8 @@ public: // a texture the scene has never drawn taken out of the gfx card and the memory, read again when it's used: the image of a // 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 ) {} // utility methods virtual void Pick_Control_Callback( std::function Callback ) = 0; virtual void Pick_Node_Callback( std::function Callback ) = 0; From b832d2fab957f87772f12a7a79454f647d20f879 Mon Sep 17 00:00:00 2001 From: maj00r Date: Mon, 16 Nov 2026 02:13:09 +0100 Subject: [PATCH 3/4] fix(editor): the image of a material with no texture in its first slot is let go of too Its normal and specular maps were read with it and stayed in the memory: the trim passed over every image with no texture of its own, the material along with it. --- application/editormodetextures.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/editormodetextures.cpp b/application/editormodetextures.cpp index ecf846431..e3423de5e 100644 --- a/application/editormodetextures.cpp +++ b/application/editormodetextures.cpp @@ -125,7 +125,7 @@ void editor_mode::thumbnails_trim() for (auto entry{browser.thumbnails.begin()}; entry != browser.thumbnails.end();) { auto const &thumbnail{entry->second}; - if (false == browser.refresh && (thumbnail.texture == null_handle || now - thumbnail.shown < kept)) + if (false == browser.refresh && ((thumbnail.texture == null_handle && thumbnail.material == null_handle) || now - thumbnail.shown < kept)) { ++entry; continue; From bcecfe29645ab7a16b1bc330a1b040a89965b91b Mon Sep 17 00:00:00 2001 From: maj00r Date: Mon, 16 Nov 2026 15:59:57 +0100 Subject: [PATCH 4/4] 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;