Sign in to follow this  
Mekanikles

How to avoid state changes?

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 )

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
@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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this