Archived

This topic is now archived and is closed to further replies.

burnseh

OpenGL what do think of this ? (opengl states)

Recommended Posts

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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

  • Similar Content

    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
    • By Picpenguin
      Hi
      I'm new to learning OpenGL and still learning C. I'm using SDL2, glew, OpenGL 3.3, linmath and stb_image.
      I started following through learnopengl.com and got through it until I had to load models. The problem is, it uses Assimp for loading models. Assimp is C++ and uses things I don't want in my program (boost for example) and C support doesn't seem that good.
      Things like glVertexAttribPointer and shaders are still confusing to me, but I have to start somewhere right?
      I can't seem to find any good loading/rendering tutorials or source code that is simple to use and easy to understand.
      I have tried this for over a week by myself, searching for solutions but so far no luck. With tinyobjloader-c and project that uses it, FantasyGolfSimulator, I was able to actually load the model with plain color (always the same color no matter what I do) on screen and move it around, but cannot figure out how to use textures or use its multiple textures with it.
      I don't ask much: I just want to load models with textures in them, maybe have lights affect them (directional spotlight etc). Also, some models have multiple parts and multiple textures in them, how can I handle those?
      Are there solutions anywhere?
      Thank you for your time. Sorry if this is a bit confusing, English isn't my native language
    • By dpadam450
      FINALLY, upgrading my engine to openGL 4. I was having some trouble so I started with a stripped down application and was wondering if VAO's are required, because I have a sample working, but if I remove the VAO then it doesn't seem to like drawing my triangle.
  • Popular Now