How to avoid state changes?

Started by
5 comments, last by LackOfGrace 16 years, 8 months ago
From what I've understood, too many state changes can result in slowdown. In my case, the relevant changes would be binding new textures and changing the blendmode. Is it a good idea to sort my render list according to texture/blendmode or could I simply put a query in the texture-drawing method like so:

if (currentBoundTexture != newTexture)
{
glBindTexture(GL_TEXTURE_2D, newTexture);
}

thnx /Mekanikles
Advertisement
well, engines tend to batch objects with the same state together , so its a good idea.

you could also sort these batches according to state change penality ( maybe one batch is using the same texture but another blending mode )
Thnx for the quick reply.

I don't know what I was thinking when I compared the query "solution" to sorting, as it would not reduce the amount of state changes at all :)
What I think I meant however is:
Would the query help speed up things? When you try to bind a texture that is already bound, does OpenGL detect this and discard the change or does it cost as much as binding a new texture?

thnx again
/Mekanikles
Minimising state changes is a good idea. You'd be best to sort your render-list, if you simply used that code without sorting you could still end up with this scenario:
TextureA
TextureB
TextureA
TextureB

Which results in 4 texture binds.
With sorting you'd have:
TextureA
TextureA
TextureB
TextureB

So now theres only 2 texture binds, of course you will still need to query whether you actually need to change state. Whether or not the cost of re-binding an already bound state is reduced or not depends on the specific implementation and the state itself, but it will still cost you something.

Once you introduce more states then you can sort them in priority so that the most expensive ones are minimised the most and the least expensive ones are changed the most frequently.

There is however a certain point where the cost of sorting by another state is actually greater than the cost of needlessly changing that state, in this case its not worth minimising this state change.

Of course if you have a simple enough scene then its not worth sorting by state anyway, only when you have many expensive states (which usually goes hand in hand with a complicated scene) is it worth state sorting.
@dmatter

I don't know what counts as a complicated scene, but I would think that I would bind new textures alot since Im doing a sprite-based shootemup (lots and lots of things on the screen at the same time). I will try and do some sorting and using a query on both blendmode and bound texture.

Thank you
/Mekanikles
Lots and lots of things on the scene at one time comprises a complicated scene in my books [smile]

You might want to consider packing multiple sprites into a single texture too, that way a singly bound texture can be used for many different sprites.
and dont forget

dont render what you cant see

if you render everything this will probably be a good optmization

This topic is closed to new replies.

Advertisement