what do think of this ? (opengl states)

Started by
18 comments, last by burnseh 22 years, 3 months ago
hello, right, from what I understand using glEnable()/glDisable() is expensive (right ?) so I started writing a few "state manager" functions, well 2 actually...:

// all states a false by default 
bool bState_Texture2D=false;
bool bState_DepthTest=false;
bool bState_Blend=false;

// enables a state unless its already enabled 
void mglEnable(int state)
{
	switch(state)
	{
		case GL_TEXTURE_2D: if(!bState_Texture2D) 
							{
								glEnable(GL_TEXTURE_2D);
								bState_Texture2D = true;
							}
							break;

		case GL_DEPTH_TEST:	if(!bState_DepthTest)
							{
								glEnable(GL_DEPTH_TEST);
								bState_DepthTest = true;
							}
							break;

		case GL_BLEND:		if(!bState_Blend)
							{
								glEnable(GL_BLEND);
								bState_Blend = true;
							}
							break;
	};
}

// disables a state unless already disabled
void mglDisable(int state)
{
	switch(state)
	{
		case GL_TEXTURE_2D: if(bState_Texture2D)
							{
								glDisable(GL_TEXTURE_2D);
								bState_Texture2D = false;
							}
							break;

		case GL_DEPTH_TEST:	if(bState_DepthTest)
							{
								glDisable(GL_DEPTH_TEST);
								bState_DepthTest = false;
							}
							break;

		case GL_BLEND:		if(bState_Blend)
							{
								glDisable(GL_BLEND);
								bState_Blend = false;
							}
							break;
	};
}
  
Would this result in a speed increase at all or is this a complete waste of time ? or is there a different/better way of doing this ? cheers, rich Edit: code formattings a bit wonky, ah well Edited by - burnseh on December 30, 2001 6:44:09 PM Edited by - burnseh on December 30, 2001 6:44:55 PM
Advertisement
>>Would this result in a speed increase at all<<

yes, i do it (though a lot more complicated)

http://uk.geocities.com/sloppyturds/gotterdammerung.html
its the same thing i did and the speed increase is a real thing since i''m using a sort of ''shader'' for each object before rendering.

zedzeek,

Would you mind elaborating on what you do, I am interested in building my own state manager.

Also, would a rendering manager by similar to a state manager? For example, to reduce state changes, I WOULD build a tree. Each node would be a state change and a linked list of data to render under that state. Then, rendering would traverse this tree of state nodes with data associated with each. Would this idea be the same as what is being discussed here??

Edited by - GalaxyQuest on January 2, 2002 8:31:52 PM
It''s faster than just calling glEnable everytime, but that switch is a bottleneck, especially if you have a lot of cases. An easy way to avoid this is just writing some functions like mglEnableTexture2D and so on. Or define them as macros what''ll save you the function''s overhead. I use some trick of mine to optimize my code where I would need a switch, making the code as fast as seperate functions, but it needs special values for all cases and a lot of assembler, making it only usable if you may define all constants by yourself.
my opengl statemanager
*handles sorting of states (for optimal performance).
*it will also LOG out all current states (so i can see if something is enabled which shouldnt be)
*a lot of error checking etc

btw its 3000+ LOC so its not a short piece of code

http://uk.geocities.com/sloppyturds/gotterdammerung.html
There was an interesting discussion about redundant state changed on opengl.org''s forums and the thing that came out was that the redundant state changes should be avoided at any cost(well, almost at any cost). NVIDIA driver does not ignore them, while the driver _could_ check for redundant state changes they do not do it to favor well-written applications over badly-written(this was one of the points, search the advanced forum for more details).

While with many cases the state-manager could be a little slow, its still faster than a redundant state change in 99%. To speed up one could use macros, or, which I prefer, inline functions.

-Lev
as lev saiz its a bad think if the driver checked. if it checked its gonna be slower so no drivers will do this for statechanges

shag - i havent been working much on gotterdammerung lately (been staying in a tent 5 days out of seven 4 the last couple of months whilst im away picking fruit thus no access to a computer) though the bright side is ive saved up enuf cash to buy me a geforce2mx if i can just make it to a decent sized town ill grab one (the nearest is 300km distance



http://uk.geocities.com/sloppyturds/gotterdammerung.html
I posted my idea of using a tree with state "nodes" where at each node has a linked list of data to render. At render time the tree is traversed, therby it should minimize state changes to what is needed....but no one really had any comments on whether this "sounds" good. Ive heard of this idea elsewhere. Sound good??
Hi,

I think that the switch method is "too slow" because it costs a lot of "if" instructions.
I had implement the state management through flag.

- to enable a state you wrote :
Enable (CF_CULL_FACE);
- CF_CULL_FACE is a flag define like below
#define CF_CULL_FACE 0 //cull is the 0-th bit
- a flag with the value of the state is declared:
long states;
- All render state are in an array :
GLenum allGLstates[] = { GL_CULL_FACE, ... };
- and enable code is like this :
void Enable (const ulong flag)
{
if ( GetFlagValue (states, (1< {
glEnable ( *(allGLstates+flag) );
SetFlag (states, (1< }
}
I think that this method could be faster that the switch. But there are a lot of methods to do it.

Vko
Vko

This topic is closed to new replies.

Advertisement