About batching and GL states.

Hi all, I have implemented a simple batching code. The goal of my batching is to group each VAO by material pass, where each material pass is related with a unique shader program, then I minimized the calls to glUseProgram, doing only one per registered pass type.

In each pass ( one per batch entry ) I have custom variables which are linked as uniforms to the shader, apart of this I have custom GL states, like “GL_DEPTH_TEST” or “glPolygonOffSet” among others.

Here a pseudo code of how it works:


for( BatchGroup *group : this->groups )
{
	glUseProgram( group->GetShader()->GetId( ) );

	for( BatchEntry *entry : group->entries )
	{
		// get the material pass, which have custom shader values and GL states
		Pass *pass = entry->GetPass( );

		// set custom GL states ( glCullMode, glEnable, glDisable ... )
		pass->SetGLStates( )
		// bind custom variables to shader program 
		pass->BindUniforms( group->GetShader( ) )
		// call glDrawElements ... 
		entry->Draw( ) 
	}
}

My question is about the line “pass->SetGLStates( )”, is it correct to have differents GL states in the same batch group? or all entries which are drawn in the same group, that is, are using the same shader program, should have the same GL state.

Thanks in advance.

[QUOTE=Alberto García;1286565]Hi all, I have implemented a simple batching code. …
My question is about the line “pass->SetGLStates( )”, is it correct to have differents GL states in the same batch group? or all entries wich are drawn in the same group, that is, are using the same shader program, should have the same GL state.[/QUOTE]

What you’re doing seems pretty reasonable, depending on what GL states “SetGLStates” encompasses.

Check out pg. 48 in this presentation (video and presentation PDF link):

This seems to mesh preetty well with what you’re doing, and gives you suggestions for how you can layer your GL state settings by cost.

Thanks for your response Dark Photon.

I will read the presentation.