Skip to content

Commit

Permalink
Update unload/release material/textures
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Aug 22, 2024
1 parent 0957b80 commit f25fee8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Projects/Skylicht/Engine/Source/Material/CMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,18 @@ namespace Skylicht
}
}

void CMaterial::unloadUniformTexture(const char* name)
{
SUniformTexture* p = getUniformTexture(name);
if (p != NULL && p->Texture)
{
CTextureManager* textureManager = CTextureManager::getInstance();
textureManager->removeTexture(p->Texture);
p->Texture = NULL;
m_textures[p->TextureSlot] = NULL;
}
}

void CMaterial::initMaterial()
{
deleteAllParams();
Expand Down
2 changes: 2 additions & 0 deletions Projects/Skylicht/Engine/Source/Material/CMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ namespace Skylicht

void unloadUniformTexture();

void unloadUniformTexture(const char *name);

void setOverrideResource(ITexture* texture, CShader::EResourceType type)
{
m_overrideTextures[type] = texture;
Expand Down
21 changes: 21 additions & 0 deletions Projects/Skylicht/Engine/Source/Material/CMaterialManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ namespace Skylicht
m_listGenerateMaterials.clear();
}

void CMaterialManager::unloadMaterial(const char* filename)
{
std::map<std::string, ArrayMaterial>::iterator it = m_materials.find(filename);
if (it == m_materials.end())
{
char log[512];
sprintf(log, "[CMaterialManager] %s not found", filename);
os::Printer::log(log);
return;
}

ArrayMaterial& list = (*it).second;
for (int j = 0, n = (int)list.size(); j < n; j++)
{
CMaterial* m = list[j];
m->drop();
}

m_materials.erase(it);
}

ArrayMaterial& CMaterialManager::loadMaterial(const char* filename, bool loadTexture, const std::vector<std::string>& textureFolders)
{
// find in cached
Expand Down
2 changes: 2 additions & 0 deletions Projects/Skylicht/Engine/Source/Material/CMaterialManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ namespace Skylicht

ArrayMaterial& loadMaterial(const char* filename, bool loadTexture, const std::vector<std::string>& textureFolders);

void unloadMaterial(const char* filename);

CMaterial* createMaterial(ArrayMaterial& materials);

void deleteMaterial(ArrayMaterial& materials, CMaterial* material);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace Skylicht
while (i != end)
{
char log[512];
sprintf(log, "Remove Texture: %s", (*i)->texture->getName().getPath().c_str());
sprintf(log, "Remove texture: %s", (*i)->texture->getName().getPath().c_str());
os::Printer::log(log);

driver->removeTexture((*i)->texture);
Expand All @@ -87,6 +87,10 @@ namespace Skylicht
{
if ((*i)->texture == tex)
{
char log[512];
sprintf(log, "Remove texture: %s", (*i)->texture->getName().getPath().c_str());
os::Printer::log(log);

driver->removeTexture((*i)->texture);
delete (*i);

Expand Down

0 comments on commit f25fee8

Please sign in to comment.