When to unbind textures in OpenGL?

Hi. This is the Java 3d subforum, not the Java3d one, isn’t it? :slight_smile: So the place to ask questions about OpenGL and Java, too? :slight_smile:

For a 2d game in OpenGL (yeah it’s 3d in 2d view) I’ve loads of textures to bind during game play. Many graphics appear on screen for some time and then disappear. Some of them re-appear some time later, some won’t come again.
Currently I’ve got some usage counter per texture. When a sprite uses one, the texture is bound and the usage counter incs. When a sprites disapears forever, its texture’s usage counter decs and when it’s zero the texture is unbound.
While this works, it’s fussily. Also, when new sprites appear and use the same texture an old disappeared sprite used to use, I’ve to rebind the texture. Since they’re being compressed on the fly this is an overhead which I’d like to avoid.

However, maybe this isn’t necessary at all. That’s the question. Would it be sufficient to unbind all textures just at the end of the level? And let the OpenGL driver do the rest? With rest I mean: when new textures are being bound, old ones which doesn’t fit into the gfx card are moved to the PC’s RAM anyway, isn’t it?
Any experiences?

How do high level 3d APIs using OpenGL solve this issue? When, for example, a user loads many 3d models which tons of textures and all of those models could appear in the scene graph? When do they bind the textures, and when unbind?

PS: With “bind” I mean something like this:
gl.glGenTextures(…) to get a new texture ID
then
gl.glBindTexture(GL.GL_TEXTURE_2D, texture’s ID);

With “unbind” I mean this:
gl.glDeleteTextures(texture’s ID)

I keep everything bound and let the page file handle it :wink: Cheeky but effective.

I’ve been experimenting with the garbage collector automatically unbinding textures for me when my GLTexture objects are collected.

Cas :slight_smile:

[quote]I keep everything bound and let the page file handle it :wink: Cheeky but effective.
[/quote]
Thanks for your answer. If the smart Alienflux creator does handle it this way, I’ll do so, too. :slight_smile:

With “keep bound” you mean during the entire game or for one level?

Does keeping also mean you can “delete” (=null) your BufferedImages (or whatever type you load texture files into from disc) once you’ve bound the BufferedImages to Ogl’s texture (and got an Ogl tex ID) ?
Because currently I keep those BufferedImage in my Java app and they’re doubled on the gfx card (and even copied again in case the Ogl driver pages them out…)

Sounds nice: has it been worth the effort? And also: how do you get notified when the GC kicks your texture?

Entire game.

As soon as you’ve loaded your image into GL with glTexImage2D, forget about it! GL makes a copy of the image and manages it from then on. You no longer need it.

The garbage collection idea is much more complicated than it sounds. You can’t use a finalizer because it’s a) unreliable and b) occurs in the wrong thread so you’ve got to figure out some way of using reference queues. I say you, coz I haven’t quite got it right yet :wink:

Cas :slight_smile:

[quote]Entire game.

As soon as you’ve loaded your image into GL with glTexImage2D, forget about it! GL makes a copy of the image and manages it from then on. You no longer need it.
[/quote]
Now this is news I love to hear!

[quote]The garbage collection idea is much more complicated than it sounds. You can’t use a finalizer because it’s a) unreliable and b) occurs in the wrong thread so you’ve got to figure out some way of using reference queues. I say you, coz I haven’t quite got it right yet :wink:
[/quote]
Yes sounds … complicated. I’ll stick to the simple® things. :wink: