18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 10:24:58 +01:00

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.
This commit is contained in:
maj00r
2026-11-16 02:10:59 +01:00
parent 4525023777
commit 0bbf07bf11
9 changed files with 82 additions and 22 deletions

View File

@@ -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<std::string, thumbnail> 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;

View File

@@ -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());