Binding textures

I have a question. If I create N texture objects and I run out of texture memory, what happens? Does the texImage2 fail? I am confused because my docs say that opengl will automatically delete older textures and there is even an API to set that delete priority.

So if it does delete an older texture object how does my app know it has to reload it? Will the subsequent bind fail?

OpenGL will never delete your textures without you doing it explicitly. That said, your docs probably meant resident textures, and the functions to prioritize and query for residency. When a texture is readily available to the renderer, it is considered resident. If it isn’t, it will have to be copied, and other resident textures might have to be kicked out.

Practically, residency means textures currently in video memory (usually 32, 64 MB on the common cards), and nonresidency means system memory. When binding a nonresident texture, the OpenGL driver will have to upload it to resident memory.

That also means that you can always create new textures, to the system memory limit, but performance will suffer if textures are swapped in and out of resident memory all the time.

  • elias

And that’s also the reason texture binding are considering expensive - there’s a risk that a texture will have to be uploaded at each bind.

  • elias

I am still confused. If you use glGenTexture to get a texture object name, then use glBindTexture, should you not call glTextImage if it is already bound? or does opengl just ignore the following glTexImage call if it already has a copy?

And is it safe to change the texture between draws? Do you have to delete the texture to repplace it with a new one?

If you delete a texture is it’s name still valid?

If I get the OpenGL manual chapter 9 correct, it’s legal do it the following way. In the OpenGL init() method you generate all your Texture objects by doing something like:


int[] tmpids = new int[1];
gl.glGenTextures(1, tmpids);
int id = tmpids[0]; // and store this id
gl.glBindTexture(o.GL_TEXTURE_2D, id);
gl.glTexImage2D(o.GL_TEXTURE_2D, ...);

Later on in the display() method you then activate the wanted texture object before drawing a polygon


gl.glEnable(o.GL_TEXTURE_2D);
gl.glBindTexture(o.GL_TEXTURE_2D, id);
// draw the polygon 

When the wanted texture object is already in the graphic card’s V-RAM (resident), it’s used, otherwhise its loaded from slow system RAM. For the latter to happen some other resident textures are being kicked (not deleted from the logical part, so their object id keeps intact). The manual mentioned that for this process typical implementations of OpenGL apply a least recently used (LRU) strategy to decide which texture objects to move out of the working set.

If the V-RAM is too small to hold all your textures residently, the game will go slow. You can’t do anything expect to decrease the texture sizes (a typical option in game menues).

Of course, I could have mis-understood all what the manual said. :slight_smile:

Excellent!

So it would be my assumption that for scenes that have more textures than texture memory, but where the textures needed in any one frame are less than texture memory it would be most efficient to delete the texture objects explictly and manage a texture cache within your own code?

In other words are there advantages to doing texture caching management explicitely, or should we just rely on OpenGL to manage that?

Also, if you make a change to the texture, can we re-issue the gl.glTexImage2D(o.GL_TEXTURE_2D, …) command to have the texture update on the card? Is this the method used for “video textures” where the texture is changing every frame, or is there a better way to do it?