Jump to content
  • Advertisement

Archived

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

Structural

State changes: avoid as much as possible?

This topic is 5804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve read that to speed up an application as that one should avoid as many state changes as possible. Ofcourse, this is logical, but what if you call glDisable on a state that already was disabled? For example, if I have GL_TEXTURE_2D disabled, and then call glDisable(GL_TEXTURE_2D), will OpenGL then only change a boolean, or will it go deeper down and make many more calls? Would it be wiser to check a state with glIsEnabled, and act accordingly? Thank you!

Share this post


Link to post
Share on other sites
Advertisement
I''m curious too. How big of a deal is it to make a call like: glEnable (GL_LIGHTING)?

Or: glEnable (DEPTH_TEST)?

Or any of the other ones. Sometimes, I feel that the call is only setting a switch, and the expense of the call is pretty much nonexistent.

Share this post


Link to post
Share on other sites
>>Ofcourse, this is logical, but what if you call glDisable on a state that already was disabled?<<

even if its already disabled dont call it, it will hurt performance do the checking yourself
eg

void setglLighting( const bool on )
{

if ( !on && gl_lighting_currently_enabled )
{
glDisable( GL_LIGHTING );
gl_lighting_currently_enabled=false;
return;
}
if ( on && !gl_lighting_currently_enabled )
{
glEnable( GL_LIGHTING );
gl_lighting_currently_enabled=true;
}

http://uk.geocities.com/sloppyturds/kea/kea.html
http://uk.geocities.com/sloppyturds/gotterdammerung.html

Share this post


Link to post
Share on other sites
Working for 4 months at my engine, I can safely tell you that state changes are totally inexpensive. I enable/disable lighting (and backface cooling, z buffer, etc. at LEAST 3 times/frame (sometimes even 20 times) and there is no penalty whatsoever.
And this is on a Vanta TNT2 8 MB

Share this post


Link to post
Share on other sites
Well, that''s good to hear. But 3 or 20 times isn''t that much. Let''s say you have lots of things, say, hundreds of billboarded trees, many many objects, etc. Let''s say each object has it''s own set of rendering parameters. Short of sorting the objects according to state criteria, each object needs to undergo a set of state changes before being rendered. Let''s just say that you''ve got 1,000 objects. Let''s say each object has 10 state parameters associated with it. That would be 10,000 state changes per frame.

Share this post


Link to post
Share on other sites
Think of what the call is doing (or might be doing if you aren''t familiar with the details of a typical rendering pipeline) and you can guess what is going to be expensive and what isn''t.

Binding a different texture will be expensive because it involves moving data around internally.

Turning texturing/lighting/depth r/w/rasterization on/off isn''t going to be that expensive because it just involves skipping over a section of the pipeline.

Changing one of the 3 main matrices will be a little more expensive because the data has to be sent across the bus, you don''t want to repeat that unecessarily too often.

And so on and so forth...

------------
- outRider -

Share this post


Link to post
Share on other sites
quote:
Original post by bishop_pass
Well, that''s good to hear. But 3 or 20 times isn''t that much. Let''s say you have lots of things, say, hundreds of billboarded trees, many many objects, etc. Let''s say each object has it''s own set of rendering parameters. Short of sorting the objects according to state criteria, each object needs to undergo a set of state changes before being rendered. Let''s just say that you''ve got 1,000 objects. Let''s say each object has 10 state parameters associated with it. That would be 10,000 state changes per frame.


That''s exactly what I am doing
My 3D objects have this flags (on or off):

Alpha blended
Have transparency
Recive light (or have the same light no matter what, like for light sources)

The objects that have transparency need alpha testing, and the backface culling is disabled.
I usually have about 10-20 objects in the scene (it is an isometric engine), but they can be drawn 3 times, when the shadows are enabled, and they are near the water (reflections).
Sure, not all the state changes take place when drawing the shadows and reflections, but, still...
So, I can have about 200 state changes per frame, and there is no difference.



Height Map Editor | Eternal lands

Share this post


Link to post
Share on other sites
quote:

Turning texturing/lighting/depth r/w/rasterization on/off isn''t going to be that expensive because it just involves skipping over a section of the pipeline.


If it only were that easy. It''s not what the new state itself means to the pipe, it''s about how expensive the actual state change is. Turning, say, texturing on and off is not just to set/reset a bit and then skip/not skip that part of the pipe. Say I have a certain state setup which fully uses the hardware, and then I enable something that was previously disabled. The driver must now perform an analysis of the new state setup and determine whether it can perform it in hardware or must fall back on a software solution. It''s this analysis you should avoid by not doing redundant state changes.

Share this post


Link to post
Share on other sites
I had the same problem.. I decided that the best thing to do was to find out how time consuming each state change call was. Them in order from most expensive, I made my engine so that it relatively cheaply renders the object in order according to the state changes... Now this definately isn''t the way to go when you end up with large overdraw, but if you have overdraw, you have other things to worry about...

The way I have my engine set up, Everything is drawn in groups based on its texture. The way I have my textures set, up this also indirectly sorts the objects by if they need shadowing/lighting/ect. Just a thought....



"The only thing worse than not having that new _______ , is when some rich kid has it, but can''t and/or doesn''t appreciate it."-me
Tazzel3d ~ Dwiel

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!