18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-21 22:34: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 struct thumbnail
{ {
int texture{0}; // 0: none 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 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 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 loads{0}; // textures read in the frame, a few at most so the browser doesn't stall the editor
int loadsframe{-1}; int loadsframe{-1};
} m_textures; } m_textures;

View File

@@ -93,16 +93,17 @@ std::uint64_t editor_mode::texture_thumbnail(texture_browser::item const &Item)
if (browser.loads >= 2) if (browser.loads >= 2)
return 0; return 0;
++browser.loads; ++browser.loads;
texture_handle texture{null_handle}; texture_browser::thumbnail thumbnail;
if (Item.material) if (Item.material)
{ {
auto const material{GfxRenderer->Fetch_Material(Item.name)}; // a material reads all its textures, the normal maps and the rest with the one shown
if (auto const *data{GfxRenderer->Material(material)}; material != null_handle && data != nullptr) thumbnail.material = GfxRenderer->Fetch_Material(Item.name);
texture = data->GetTexture(0); if (auto const *data{GfxRenderer->Material(thumbnail.material)}; thumbnail.material != null_handle && data != nullptr)
thumbnail.texture = data->GetTexture(0);
} }
else else
texture = GfxRenderer->Fetch_Texture(Item.name, true); thumbnail.texture = GfxRenderer->Fetch_Texture(Item.name, true);
lookup = browser.thumbnails.emplace(Item.name, texture_browser::thumbnail{texture}).first; lookup = browser.thumbnails.emplace(Item.name, thumbnail).first;
} }
lookup->second.shown = ImGui::GetTime(); lookup->second.shown = ImGui::GetTime();
if (lookup->second.texture == null_handle) 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() void editor_mode::thumbnails_trim()
{ {
// a few seconds out of sight, so scrolling back and forth doesn't read them over and over // 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 constexpr kept{3.0};
auto const now{ImGui::GetTime()}; auto const now{ImGui::GetTime()};
auto &thumbnails{m_textures.thumbnails}; auto &browser{m_textures};
for (auto entry{thumbnails.begin()}; entry != thumbnails.end();) 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; ++entry;
continue; continue;
} }
// the scene's own textures stay; shown again, the image is read anew within the few a frame // the scene's own textures stay; shown again, the image is read anew within the few a frame
GfxRenderer->Unload_Texture(entry->second.texture); if (thumbnail.material != null_handle)
entry = thumbnails.erase(entry); 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() void editor_mode::render_texture_browser()
@@ -193,9 +201,8 @@ void editor_mode::render_texture_browser()
if (ImGui::Button(STR_C("Refresh"))) if (ImGui::Button(STR_C("Refresh")))
{ {
browser.listed = false; browser.listed = false;
for (auto const &[name, thumbnail] : browser.thumbnails) // the images let go of at the start of the next frame, see thumbnails_trim()
GfxRenderer->Unload_Texture(thumbnail.texture); browser.refresh = true;
browser.thumbnails.clear();
} }
ImGui::SameLine(); ImGui::SameLine();
ImGui::TextUnformatted((std::string{paths::textures} + browser.folder).c_str()); ImGui::TextUnformatted((std::string{paths::textures} + browser.folder).c_str());

View File

@@ -1896,23 +1896,41 @@ texture_manager::unload( texture_handle const Texture ) {
auto const &record { m_textures[ Texture ] }; auto const &record { m_textures[ Texture ] };
auto &texture { *record.first }; auto &texture { *record.first };
if( ( record.second != resource_timestamp() ) if( ( record.second != resource_timestamp() )
|| ( texture.id == opengl_texture::invalid_id ) || ( texture.id == 0 ) // failed to load
|| ( texture.id == 0 )
|| ( true == texture.is_static ) || ( true == texture.is_static )
|| ( true == texture.is_rendertarget ) || ( true == texture.is_rendertarget )
|| ( texture.type == "make:" ) || ( texture.type == "make:" )
|| ( texture.type == "internalsrc:" ) ) { || ( texture.type == "internalsrc:" ) ) {
return; return;
} }
texture.release(); if( ( texture.data_state == resource_state::loading )
texture.data = std::vector<unsigned char>(); && ( texture.request != nullptr ) ) {
texture.data_state = resource_state::none; // read by a worker thread: taken over first, to be dropped with the rest
texture.reload_on_use = true; 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 // the released gl name can be given to a new texture, the unit cache can't trust it
for( auto &unit : opengl_texture::units ) { for( auto &unit : opengl_texture::units ) {
unit = -1; unit = -1;
} }
} }
texture.data = std::vector<unsigned char>();
texture.data_state = resource_state::none;
texture.reload_on_use = true;
}
// releases gl textures of vehicles not needed nor drawn for a while // releases gl textures of vehicles not needed nor drawn for a while
void void

View File

@@ -182,7 +182,7 @@ public:
void void
release_unneeded(); 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 // 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 void
unload( texture_handle const Texture ); unload( texture_handle const Texture );
// debug performance string // debug performance string

View File

@@ -2575,6 +2575,18 @@ void opengl33_renderer::Unload_Texture(texture_handle const Texture)
m_textures.unload(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) void opengl33_renderer::Update_AnimModel(TAnimModel *model)
{ {
model->RaAnimate(m_framestamp); model->RaAnimate(m_framestamp);

View File

@@ -112,6 +112,8 @@ class opengl33_renderer : public gfx_renderer {
Texture( texture_handle const Texture ) const override; Texture( texture_handle const Texture ) const override;
void void
Unload_Texture( texture_handle const Texture ) override; Unload_Texture( texture_handle const Texture ) override;
void
Unload_Material( material_handle const Material ) override;
// utility methods // utility methods
void void
Pick_Control_Callback( std::function<void( TSubModel const *, const glm::vec2 )> Callback ) override; Pick_Control_Callback( std::function<void( TSubModel const *, const glm::vec2 )> Callback ) override;

View File

@@ -1928,6 +1928,21 @@ opengl_renderer::Unload_Texture( texture_handle const Texture ) {
m_textures.unload( 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 void
opengl_renderer::Pick_Control_Callback( std::function<void( TSubModel const *, const glm::vec2 )> Callback ) { opengl_renderer::Pick_Control_Callback( std::function<void( TSubModel const *, const glm::vec2 )> Callback ) {

View File

@@ -97,6 +97,8 @@ public:
Texture( texture_handle const Texture ) const override; Texture( texture_handle const Texture ) const override;
void void
Unload_Texture( texture_handle const Texture ) override; Unload_Texture( texture_handle const Texture ) override;
void
Unload_Material( material_handle const Material ) override;
// utility methods // utility methods
void void
Pick_Control_Callback( std::function<void( TSubModel const *, const glm::vec2 )> Callback ) override; Pick_Control_Callback( std::function<void( TSubModel const *, const glm::vec2 )> Callback ) override;

View File

@@ -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 // 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 // texture picker scrolled past. backends which keep their textures otherwise leave it
virtual void Unload_Texture( texture_handle const Texture ) {} 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 // utility methods
virtual void Pick_Control_Callback( std::function<void( TSubModel const *, const glm::vec2 )> Callback ) = 0; virtual void Pick_Control_Callback( std::function<void( TSubModel const *, const glm::vec2 )> Callback ) = 0;
virtual void Pick_Node_Callback( std::function<void( scene::basic_node * )> Callback ) = 0; virtual void Pick_Node_Callback( std::function<void( scene::basic_node * )> Callback ) = 0;