Problem with difference textures which have the same texture name

Started by kiffa, September 15, 2012, 08:25:14 AM

Previous topic - Next topic

kiffa

I have a dog, in scene-1, the dog will be set to texture1 which named "dog".

In scene-2,  i first removeAndUnload("dog"), then new texture2 which is different from texture1 but has the same name "dog", then set the texture2 to the dog.

But the dog in scene-2 looks like with another texture which is not texture2.

Codes:

//scene1
texture1 = new texture(in);
textManager.addTexture("dog", texture1);
dog.setTexture("dog");

//do something
...

//switch to scene2
removeAndUnload("dog");

//in scene2
//add some other texture first
textureManager.addTexture("some other textures")

//new texture2 with the same name "dog"
texture2 = new texture(in2);
textureManager.addTexture("dog", texture2);

dog.setTexture("dog");

// the dog seems not to be set to  the texture2, but one of "some other textures"


I guess that was caused by some caching mechanism?











EgonOlsen

Try to use replaceTexture in the TextureManager instead to create a new texture of the same name.

kiffa

Should i unload the texture which will be replaced?

new texture1;
tm.addTexture("dog", texture1);
dog.setTexture("dog");

new texture2;

//Then
tm.unloadTexture("dog");
tm.replace("dog", texture2);

//Or just need
tm.replace("dog", texture2);  // the new texture wiil be auto upload to gpu, and the old one will be auto unloaded





kiffa

I don't want to hold the texture of the dog in memory(both vm and gpu) all the time because of the limited memory. I want to free the texture of the dog when switching to some scenes which don't need the dog.

But i need hold the object of dogModel(Animated3D) in memory all the time(by static reference)  because of the long model-loading time(So i want to just loading once).

So, if the texture name of my dog is always the same, and if i use TextureManager.replaceTextures(), which means i need hold the texture of the dog in memory all the time.

And if i free the texture in some situation,  then create it again later, dog.setTexture(theSameName) will cause an problem which i described above.

Any suggestion? Could i clear the texture cache of the dog by some methods?

EgonOlsen

There's no cache or such thing. If you remove and unload a texture, it's gone. If it's still shown on the model, the model references some texture in gpu memory that is only still there because the memory hasn't been reused yet. I'm not sure what happens if you remove one and add another texture with same name later. I suggest to use replace instead. If you are worried about the memory, replace it with some very small dummy instead. I'll have a look why it behaves the way it does in your case.

kiffa

Thanks, i will try small dummy texture. One question: Do all Object3D(s) with the dummy texture share the same texture in memory(means there is only one dummy texture in vm and gpu)?

And the "cache" what i mean is that(i guess):

Object3D.setTexture(name)  -> Objcet3D.setTexture(getTextureIdByName(name)) ->try to getCachedId()  first -> if not fonud, then find and return the real id mapped to the name.

So, if i use the textures which are diffenent but have the same name, getCachedId() may return the last cached but wrong id.

EgonOlsen

Yes, a texture used multiple times will be stored only once in vm and gpu memory.
About the caching: There is no cache. There might by some structure to speed up texture access and it might not be able to handle this case, but i can't remember it ATM. I'll look at it, when i'm back home.