Reloading textures with GLES2 Rendersystem

Problems building or running the engine, queries about how to use features etc.
Post Reply
lscandolo
Gnoblar
Posts: 17
Joined: Tue Apr 30, 2013 5:17 pm
x 1

Reloading textures with GLES2 Rendersystem

Post by lscandolo »

I want to be able to reload textures for a model that will use different color schemes. To do this, I have several textures with the same name distributed in folders according to the scheme. When I want to change the color scheme I remove the current scheme folder from the resource manager, add the new folder, and reload all textures in the model.

The code looks something like this:

Code: Select all

void reloadTextures(Ogre::Entity* ent)
{

    Ogre::TextureManager* tMgr = Ogre::TextureManager::getSingletonPtr();

    Ogre::Entity* oent = ent->getEntity();

    for (int i = 0; i < oent->getNumSubEntities(); ++i) {
        Ogre::SubEntity* ose = oent->getSubEntity(i);
        const Ogre::MaterialPtr& mat = ose->getMaterial();

        for (int t = 0; t < mat->getNumTechniques(); t++) {
            Ogre::Technique* tec = mat->getTechnique(t);
            for (int p = 0; p < tec->getNumPasses(); p++) {
                Ogre::Pass* pass = tec->getPass(p);
                for (int u = 0; u < pass->getNumTextureUnitStates(); ++u) {
                    Ogre::TextureUnitState* tu = pass->getTextureUnitState(u);
                    Ogre::TexturePtr tex = Ogre::TextureManager::getSingletonPtr()->getByName(tu->getTextureName());

                     tMgr->unload(tex->getName());
                     tex->reload();
                }
            }
        }
    }
}
This is done for all entities in the model. I've noticed the following behavior:

- This code works, it does reload the textures, both for the GL and GLES2 rendersystems.
- Using GL it works flawlessly. Using GLES2 it leaks memory, big time.
- If I reload 3 512x512 textures, I get a 5MB memory rise in GLES2. In GL memory consumption stays exactly the same.
- If I just do the tex->reload(), and I don't add the tMgr->unload() call, memory rise is bigger still (nearing 15 MB).

Since the snippet works for regular OpenGL, I'm guessing it's not wrong, but maybe there's some other way to do it? I don't want to have all the textures in the resource manager because that would mean keeping track of all the the different file names, this way I can just throw in a new folder with modified textures and it will work.

Has anyone come into something like this before using GLES2? I'll be trying to debug this, but I wanted to know if the code should work as it is right now.
lscandolo
Gnoblar
Posts: 17
Joined: Tue Apr 30, 2013 5:17 pm
x 1

Re: Reloading textures with GLES2 Rendersystem

Post by lscandolo »

Just some more info I forgot to add: this is done using Ogre 1.9, updated from the end of may (28 I think). It does the exact same thing with the v1-9-0 tag.
lscandolo
Gnoblar
Posts: 17
Joined: Tue Apr 30, 2013 5:17 pm
x 1

Re: Reloading textures with GLES2 Rendersystem

Post by lscandolo »

Well, I finally got it working, but I didn't really find the error, so I'll just write what I did in case it is useful for someone.

Whatever the leak was, it was in the image loading code, since I could modify the contents of the texture by locking/unlocking the hardwarepixelbuffer with no increase in memory usage.

I decided to recompile Ogre with the standard allocator option instead of nedmalloc/pooling and that made it work fine, so if anyone encounters this problem, give this solution a try.
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: Reloading textures with GLES2 Rendersystem

Post by c6burns »

Good catch, still would be nice to be able to use nedmalloc's pooling, but good to know there's an easy fix.
User avatar
Wolfmanfx
OGRE Team Member
OGRE Team Member
Posts: 1525
Joined: Fri Feb 03, 2006 10:37 pm
Location: Austria - Leoben
x 99
Contact:

Re: Reloading textures with GLES2 Rendersystem

Post by Wolfmanfx »

I have fixed a mem leak in 1.9 / 1.10 which leaked the whole image on loading on gles2.0. Maybe you checked out my fix in the meantime.
lscandolo
Gnoblar
Posts: 17
Joined: Tue Apr 30, 2013 5:17 pm
x 1

Re: Reloading textures with GLES2 Rendersystem

Post by lscandolo »

Yes, I saw that commit, and I thought I had tested the bug with a build past that commit, but maybe I screwed up and recompiling with the repo's latest commit is really what made it work.
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: Reloading textures with GLES2 Rendersystem

Post by c6burns »

I can confirm that this commit has fixed the noticeable leakage in android and I continue to use nedmalloc for pooling :) Thanks wolfman!
Post Reply