I'm coding my first Shader in my little game engine, and I have some questions about it's performance and common approaches.

  1. Is the Shader code processed by the video card instead of the PC processor? Just so I know if it's possible to share some calculations to save some processor power.

  2. Generally, should I do the math calculations in my code or the Shader? I could calculate them all (lightning for example) and just send the final values to be multiplied/used to the Shader, but what's the best approach?

  3. Shaders as far as I could understand are the main party responsible for visual effects, so for example, if I want to add a "blur" effect to only one object on screen, should I use an if/switch statement in the Shader or should I have different Shader/ProgramIDs and I just switch the call glUseProgram(programID)?

Sorry if some questions seems stupid, and thanks for your time!

  • \$\begingroup\$ Keep in mind that most modern GPUs have many cores, which allow for some monster parallel computations. I'd go into some more depth and actually answer your questions, but I'm typing this from my phone in the car. \$\endgroup\$ Apr 3, 2013 at 3:41

2 Answers 2


Shader code is uploaded to the video card and compiled by it, usually at the beginning of your program. The video card uses one of it's hundreds (or thousands) of cores to run through your shaders for each vertex or fragment (etc).

You should avoid doing work multiple times. If you can calculate something once on the CPU and make the calculation available to the shader, that's a lot better than asking the shader to calculate it multiple times.

Conditional logic (loops, if statement, etc) can cause some really nasty performance hits in your shaders, so avoid using them whenever you can. I would expect you to be changing the shader program many times as you draw a scene.


For performance issues, you might want to check out the AMD GPU ShaderAnalyzer. It's free software, and helps you see the bottleneck of your shader, as well as approximate performance with ATI/AMD cards. I don't know if it supports Nvidia cards though.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .