Author Topic: Problem with difference textures which have the same texture name  (Read 2946 times)

Offline kiffa

  • long
  • ***
  • Posts: 199
    • View Profile
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:

Code: [Select]
//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?










« Last Edit: September 15, 2012, 08:27:26 am by kiffa »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Problem with difference textures which have the same texture name
« Reply #1 on: September 15, 2012, 09:33:10 am »
Try to use replaceTexture in the TextureManager instead to create a new texture of the same name.

Offline kiffa

  • long
  • ***
  • Posts: 199
    • View Profile
Re: Problem with difference textures which have the same texture name
« Reply #2 on: September 15, 2012, 10:04:23 am »
Should i unload the texture which will be replaced?

Code: [Select]
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



« Last Edit: September 15, 2012, 10:51:52 am by kiffa »

Offline kiffa

  • long
  • ***
  • Posts: 199
    • View Profile
Re: Problem with difference textures which have the same texture name
« Reply #3 on: September 15, 2012, 10:49:23 am »
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?
« Last Edit: September 15, 2012, 10:50:56 am by kiffa »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Problem with difference textures which have the same texture name
« Reply #4 on: September 15, 2012, 09:16:49 pm »
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.

Offline kiffa

  • long
  • ***
  • Posts: 199
    • View Profile
Re: Problem with difference textures which have the same texture name
« Reply #5 on: September 17, 2012, 05:43:50 am »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Problem with difference textures which have the same texture name
« Reply #6 on: September 17, 2012, 08:56:58 pm »
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.