Sign in to follow this  

CPU / GPU balance

This topic is 4399 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 am currently learning to program the GPU with shaders and have found the line between "code this on the CPU" and "code this on the GPU" to be blurry at times. Is it good practice to port as many calculations as possible to the GPU? Could you suggest any definite guidelines? Thanks.

Share this post


Link to post
Share on other sites
Preprocessing like calculating the view matrix or general matrix calculations like concatenating or transposing should be done on the CPU instead of the vertex shaders.
These operations are costly and redundant (per shader/frame) and should thus be kept on the CPU-side and passed as a preprocessed shader parameters.

Share this post


Link to post
Share on other sites
The only way to know for sure is to profile.

In most situations, the GPU (especially the vertex pipe) is under-utilized. That would suggest moving as much calculation to the GPU as possible (even if it seems redundant and wasteful). However, there may be tradeoffs even doing that. Moving more calculation to the GPU may mean a bigger vertex buffer and more shader constants, which can have an impact on CPU performance.

Anyway, the standard rules of optimization still apply: do it the "right" way first, then profile it and figure out how to make it faster.

Share this post


Link to post
Share on other sites
Quote:

I am currently learning to program the GPU with shaders and have found the line between "code this on the CPU" and "code this on the GPU" to be blurry at times. Is it good practice to port as many calculations as possible to the GPU? Could you suggest any definite guidelines?


A few things to keep in mind when using the GPU.

1) If you can make use of the fixed function pipeline, use it!

2) If you make any calculation on the GPU, try to avoid the use of that result in the CPU. The video mem to system mem bandwith is really bad. This probably implies that if you are using a vertex shader, you probably need a pixel shader too for any special affects you do (other that the fixed function pipeline).

3) Try to minimize the number of times data is transferred between CPU and GPU. batching can be a great perf improver. Dont overload the GPU either by pushing everything to the GPU. A balance is required, which profiling will point to.

4) Remember, using the GPU is useful only when the CPU and the GPU work in parallel. Avoid making calls which would cause one to wait for the other to finish. (For instance in DirectX, when you lock dynamic vertex buffers, pay attention to the flags).

No matter how good your design is, you are bound to hit some perf bottlenecks.

Always profile, understand why and optimize!

Quote:
Original post by JohnBolton
The only way to know for sure is to profile.
<snip>
Anyway, the standard rules of optimization still apply: do it the "right" way first, then profile it and figure out how to make it faster.


I don't agree with you completely. Most performance bottlenecks usually turn out to be design issues and redoing the design again and again can be a cumbersome and painful process. For small projects this method works fine, but for big ones like a decent sized game, it is advisable to first learn as much as possible about the architectures involved (AGP, video card..) etc and then think about the best design you can, code it up and then constantly profile it.

Moreover, unless the design architecture is good, we cannot hope to achieve reasonable performance on many different PC configurations.

What you said, "profile and optimize", definitely applies to head_hunter's question as he posed it, but I would not say that it is a "standard rule of optimization" unless some kind of thought has gone into the design process.

(Probably by putting right in quotes, you are saying what I am trying to say. So perhaps we agree after all).

Share this post


Link to post
Share on other sites
Quote:
Original post by d000hg
Quote:
Original post by Aryabhatta
1) If you can make use of the fixed function pipeline, use it!

I'm expecting somebody more knowledgeable than me to disagree with this...


ATI cards simulate the fixed function pipeline with shaders anyway (well they did anyway)

Share this post


Link to post
Share on other sites
The simple answer is, balance it so you get 100% load on the CPU, and 100% load on GPU. [smile]

If you find that is a bottleneck when run on the CPU, it might be possible to move it to the GPU.

Quote:

1) If you can make use of the fixed function pipeline, use it!

All modern (less than 3 years old, roughly) cards simulate this using shaders. The only reason to use the FFP is that it's simpler to for the programmer.

Share this post


Link to post
Share on other sites
Quote:
Original post by head_hunter
Thanks for the replies!

I can see all of you agree on profiling as a guide to optimization so what tools do you use to profile your shaders? Can you recommend any for Cg?


Usually the video card manufactures provide tools to do such stuff. Look at the website of your video card provider for debugging tools.

Share this post


Link to post
Share on other sites
Quote:
Original post by d000hg
Quote:
Original post by Aryabhatta
1) If you can make use of the fixed function pipeline, use it!

I'm expecting somebody more knowledgeable than me to disagree with this...


I meant completely rely on Fixed function pipeline.

If you game has a feature set that can use only the fixed function pipeline, you think there is a chance that we can improve the performance by using our own shaders which do stuff better than what the video card manufactures do? It might be possible but I think it is highly unlikey. Your time will be better spent in coding a better texture manager etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by head_hunter
Thanks for the replies!

I can see all of you agree on profiling as a guide to optimization so what tools do you use to profile your shaders? Can you recommend any for Cg?


one word:
NVShaderPerf.

Share this post


Link to post
Share on other sites

This topic is 4399 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this