Jump to content
  • Advertisement
Sign in to follow this  
Mari_p

A simple question about performance

This topic is 4657 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

Hi there. Suppose I need to set many render states and lights, but they will not be changed when the application is running. I would like to know if there is a performance difference among: 1) Setting all the render states and lights before the game loop. 2) Setting all the render states and lights in the game loop, only once per frame. Thanks in advance Edited: Sorry... I was thinking in Direct3D, but I see now that it would be better to post this in the Game Programming forum.

Share this post


Link to post
Share on other sites
Advertisement
The first option is definitely better. Each state set requires a read and write to memory to set a state variable and the less of these you have the faster the application will run. If it isn't going to change, better to set it once rather than constantly resetting a value in a loop.

Share this post


Link to post
Share on other sites
Actually, I don't think the performance loss would be large at all.

The main problem with setting renderstates, is that in order to change them, the GPU has to stop everything it is currently doing, set the state, and start over. If this happens many times per frame, it could really slow you down.

Now, if you set everything up at the begining of the frame, the GPU is idle, so it wouldn't have to stop. In addition, if the states are already set to the correct values, the calls might be skipped by the driver.

I'd recommend you Do what is easier for you in your app. The difference is so small it isn't worth the effort to build your application around this limitation. If it doesn't matter, go with the first one.

Share this post


Link to post
Share on other sites
Quote:
Original post by Aurvandil
The first option is definitely better. Each state set requires a read and write to memory to set a state variable and the less of these you have the faster the application will run. If it isn't going to change, better to set it once rather than constantly resetting a value in a loop.

Thanks Aurvandil!

Doesn't Direct3D internally configure the render states in the rendering process, to each frame? Wouldn't this be like the option 2?

Share this post


Link to post
Share on other sites
Quote:
Original post by sirob
Actually, I don't think the performance loss would be large at all.

The main problem with setting renderstates, is that in order to change them, the GPU has to stop everything it is currently doing, set the state, and start over. If this happens many times per frame, it could really slow you down.

Now, if you set everything up at the begining of the frame, the GPU is idle, so it wouldn't have to stop. In addition, if the states are already set to the correct values, the calls might be skipped by the driver.

I'd recommend you Do what is easier for you in your app. The difference is so small it isn't worth the effort to build your application around this limitation. If it doesn't matter, go with the first one.

Thanks sirob! Since Direct3D "checks" render states at each frame, it seems to me that the options 1 and 2 give the same performance...

Share this post


Link to post
Share on other sites
Whilst your questions seem to have been answered, I thought I'd throw this in...

A general rule of thumb with state changes is "the less, the better" - so if you can reduce the number of state changing calls then that's A Good Thing™. If you can get away with changing them in an OnResetDevice() rather than OnFrameRender() that's an obvious saving.

The other thing is that, as has been said, taking the easy option is perfectly valid (for now). Consider the "Get it right, then get it tight" motto - sort your code out so that it does what you want it to do; then, if performance sucks, go back and examine where it sucks and optimize it.

hth
Jack

Share this post


Link to post
Share on other sites
Quote:
Doesn't Direct3D internally configure the render states in the rendering process, to each frame? Wouldn't this be like the option 2?


Actually, Direct3D will check whether your state is redundant, and not apply it again, unless your device is created with PUREDEVICE. If it's a "pure" device then it'll skip all those checks (which may take a bit of time), but it will funnel all changes through to the card, even if the change is redundant. If you have good state management in the app, PUREDEVICE is actually faster.

If there are specific states you need, one approach is to create a state block with those states, and apply that state block when you need the device to be in that particular state. State blocks are pretty flexible; similar to display lists on OpenGL, except you can change them after they're created, and they can't actually issue geometry.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!