mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-20 11:34:57 +01:00
Merge pull request #386 from MaSzyna-EU07/fix/editor-trackbed-texture-crash
fix(editor): the texture pickers let go of the images scrolled past, the memory no longer runs out
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
@@ -745,13 +745,23 @@ 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<std::string, int> thumbnails; // texture shown for the item of the name, 0: none
|
||||
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;
|
||||
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
|
||||
|
||||
@@ -93,25 +93,53 @@ 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).first;
|
||||
thumbnail.texture = GfxRenderer->Fetch_Texture(Item.name, true);
|
||||
lookup = browser.thumbnails.emplace(Item.name, thumbnail).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<std::uint64_t>(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
|
||||
// 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 &browser{m_textures};
|
||||
for (auto entry{browser.thumbnails.begin()}; entry != browser.thumbnails.end();)
|
||||
{
|
||||
auto const &thumbnail{entry->second};
|
||||
if (false == browser.refresh && ((thumbnail.texture == null_handle && thumbnail.material == 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
|
||||
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()
|
||||
{
|
||||
auto &browser{m_textures};
|
||||
@@ -173,7 +201,8 @@ void editor_mode::render_texture_browser()
|
||||
if (ImGui::Button(STR_C("Refresh")))
|
||||
{
|
||||
browser.listed = false;
|
||||
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());
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user