Sign in to follow this  
andrew111

OpenGL Speed question with calling change state methods.

Recommended Posts

Hi, I was wondering if there would be any significant speed issues when calling state change methods to set a state that is already set? e.g.
glEnable(GL_CULL_FACE); //set state
renderMyObject1();

glEnable(GL_CULL_FACE); //same state value as before
renderMyObject2();

glEnable(GL_CULL_FACE); //same state value as before
renderMyObject2();

..

//and so on to like 500 more times.

I ask because I am creating an effects system, and I am sorting objects by their states, and I was wondering if it would be necessary to introduce a layer between the OpenGL api and my program that would discard state change calls if the state values were already set. e.g.
void StateManager::enableCullFace() {
	if(!cullFace) {
		cullFace = true;
		glEnable(GL_CULL_FACE);
	}
}
void StateManager::disableCullFace() {
	if(cullFace) {
		cullFace = false;
		glDisable(GL_CULL_FACE);
	}
}

Thanks.

Share this post


Link to post
Share on other sites
I don't think the potential for a failure in branch prediction is worth the minimal at best reduction in the command pipeline. If you did do this state check thing, it wouldn't give you much at all since you would have to query the opengl state anyway.

Share this post


Link to post
Share on other sites
short answer is yes, you should track state and dont alter state if you dont have to.

longer answer is, there are some state change which are more expensive than others, e.g. texture binds, shader binds, setting uniforms, but there are a few more. Also it depends alot on the quality of the GL drivers and the underlying implementation, for example some drivers build a list of commands and include redundant state change filtering, some don't.

Research "state sorting", it is very common (and necessary) for rendering performance.

For a definitive answer, test.

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