CPU / GPU balance

Started by
11 comments, last by Code-R 18 years, 4 months ago
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.
Advertisement
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.

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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
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).
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?
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...

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)
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.
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.
newb question
What is profiling?

This topic is closed to new replies.

Advertisement