Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding textures in OpenGL

So I'm rendering my scene in batches, to try and minimize state changes. Since my shaders usually need multiple textures, I have a few of them bound at the same time to different texture units. Some of these textures are used in multiple batches, so could even stay bound.

Now my question is, is it fine to just re-bind all of the textures that I need during a batch, even though some of them may already be bound? Or should I check which ones are bound, and only bind new ones? How expensive is glBindTexture? I'm using shaders, is it bad to have unused textures bound to texture units that the shader won't sample from, or should I unbind them?

I'm mostly asking "how to make it fast", not "how to".

EDIT: Would it help if I supplied code?

like image 229
Hannesh Avatar asked Mar 10 '11 20:03

Hannesh


2 Answers

The holy rule of GPU programming is that less you talk to GPU, the more she loves you. glBindTexture is much more expensive than single condition check on CPU, so for frequent state changes, I think you should see if there is a need to transfer new texture index to GPU.

like image 97
Pythagoras of Samos Avatar answered Sep 16 '22 14:09

Pythagoras of Samos


The answer is difficult, because any strong GL implementation should make binding a already bound texture a no-op as an optimization. You should try benchmarking to see if putting a condition makes a difference or not.

The same applies to unused textures in texture units, since the GL implementation does know what texture units are finally used, it should not affect performance (as an optimization) to have unused texture units.

like image 43
Dr. Snoopy Avatar answered Sep 20 '22 14:09

Dr. Snoopy