Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


what do think of this ? (opengl states)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
19 replies to this topic

#1 burnseh   Members   -  Reputation: 122

Like
Likes
Like

Posted 30 December 2001 - 11:43 AM

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

Sponsor:

#2 zedzeek   Members   -  Reputation: 528

Like
Likes
Like

Posted 30 December 2001 - 02:43 PM

>>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

#3 v71   Banned   -  Reputation: 100

Like
Likes
Like

Posted 30 December 2001 - 11:24 PM

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.



#4 GalaxyQuest   Members   -  Reputation: 122

Like
Likes
Like

Posted 02 January 2002 - 01:30 PM

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

#5 Doener   Members   -  Reputation: 134

Like
Likes
Like

Posted 02 January 2002 - 01:56 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.

#6 zedzeek   Members   -  Reputation: 528

Like
Likes
Like

Posted 02 January 2002 - 02:10 PM

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

#7 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 02 January 2002 - 03:01 PM

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

#8 zedzeek   Members   -  Reputation: 528

Like
Likes
Like

Posted 02 January 2002 - 07:42 PM

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

#9 GalaxyQuest   Members   -  Reputation: 122

Like
Likes
Like

Posted 02 January 2002 - 08:55 PM

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??

#10 Vko   Members   -  Reputation: 122

Like
Likes
Like

Posted 02 January 2002 - 11:54 PM

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

#11 merlin9x9   Members   -  Reputation: 174

Like
Likes
Like

Posted 03 January 2002 - 12:06 AM

Shag, even if certain drivers did actually ignore redundancy, there''s no guarantee of such a thing in the OpenGL specification. Therefore, if you were to make that assumption, your app might break somewhere. Never make assumptions.

#12 v71   Banned   -  Reputation: 100

Like
Likes
Like

Posted 03 January 2002 - 12:21 AM

To Galaxy Quest , i ''ve heard too this kind of tree for opengl state switching but i think it has to deal with a lot of precomputing, in fact before rnedering you should check which of you actual calls to the opengl api are more expensive than other and thus put in the tree, then render it, i think its a good idea coupling with a sort of list of the slowest state change
done during initialization , but after some struggle i preferred to write an opengl state manager with inlined function and the speed gain is neat at the cost of some if , moreover i don''t think that some driver does a redundant check, at least at my actual knowledge



#13 Kippesoep   Members   -  Reputation: 892

Like
Likes
Like

Posted 03 January 2002 - 12:55 AM

If you actually inline the mglEnable and mglDisable functions, the compiler should optimise away the entire switch/case block, provided you use only constants as parameters.

#14 Shag   Members   -  Reputation: 122

Like
Likes
Like

Posted 03 January 2002 - 04:37 AM

I should know by now never to post when pissed!!! I was bound to get it the wrong way round!

*quietly deleting above post*

Sorry for the misinformation!

Edited by - Shag on January 3, 2002 11:38:37 AM

#15 Gelmir   Members   -  Reputation: 122

Like
Likes
Like

Posted 03 January 2002 - 04:53 AM

The faster way is to take the shader description from a file (Quake3 uses text plain files). While interpreting the shader, make an OpenGL Display List. To activate the shader you must call the glCallList function. I thought that''s the faster way.



"If you''''re gonna die, die with your boots on"

#16 GalaxyQuest   Members   -  Reputation: 122

Like
Likes
Like

Posted 03 January 2002 - 09:21 AM

Im sorta new to 3d, so I am unfamiliar with this term "SHADER" thrown around??

I also notice it referenced to quake(3). Did carmack coin this or is it specific to quake? I dont understand how this "shader" refers to states....

#17 Vko   Members   -  Reputation: 122

Like
Likes
Like

Posted 03 January 2002 - 09:28 PM

Shaders allow to define a lot of triangles properties such as if the depth test is enble, the culling, textures stage (envmode, gentex...), rgb color ...

So all of that use states in OpenGL so to set up a shader system, you must have a good states switching manager.

For the glList it is a excellent methods to improve the speed !

Vko

#18 Dredge-Master   Members   -  Reputation: 175

Like
Likes
Like

Posted 05 January 2002 - 04:40 AM

I haven't read any of the above replies yet so this may have already been said, but you can enumerate the names or place them in an array of some sort

instead of calls like

glEnable(GL_TEXTURE_2D);
bState_Texture2D = true;

use stuff something like

Sorry, had to edit this. the source call doesn't understand the define statement or the // comments very well, or breaks either.

// following line not really necessary but makes it neat
#define Statements_Number 3
// texturing on by default
#define Statements_Default_1 TRUE
// depthtest on by default
#define Statements_Default_2 TRUE
// blend off by default
#define Statements_Default_3 FALSE

typedef struct __mglState
{
long int enable;
char flag;
}_mglState;

_mglState mglState[Statements_Number]={
{ GL_TEXTURE_2D,Statements_Default_1 },
{ GL_DEPTH_TEST,Statements_Default_2 },
{ GL_BLEND,Statements_Default_3 }
};

#define mglEnable(x)\
{\
if(mglState[x].flag==FALSE)\
{\
glEnable(mglState[x].enable);\
mglState[x].flag=TRUE;\
}\
}

#define mglDisable(x)\
{\
if(mglState[x].flag==TRUE)\
{\
glDisable(mglState[x].enable);\
mglState[x].flag=FALSE;\
}\
}




Beer - the love catalyst
good ol' homepage



PS - check if it is long int or what for glEnable/glDisable. I forgot. Make sure it is the same (pososibly glint) as it will save on converting the number format.

Edited by - Dredge-Master on January 5, 2002 11:59:44 AM

#19 smitty1276   Members   -  Reputation: 560

Like
Likes
Like

Posted 05 January 2002 - 06:48 PM

Guys... I have a question.

What is the benefit to doing all of that work instead of calling glIsEnabled( stateToCheck );

Is that function slow or something?


#20 Lev Povalahev   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 January 2002 - 05:35 AM

quote:
Original post by smitty1276
Is that function slow or something?



Yes, very slow in some cases.

-Lev





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS