Shader Optimization

This page gives you an overview of how to optimize shaders.

General Speed of Built-in Functions
Shader hardware will do multiplication, addition and MAD in one cycle.

Generally, only,  ,  ,  ,   and   can be assumed to be implemented in hardware, every other function is made up of those parts. These are called "special functions" and they are slower than arithmetic. They are generally assumed to take 4 cycles

SFU
On Nvidia, special functions are run on a separate lane, so they do not cost anything if mixed in with arithmetic, but they still run at 1/4 rate (1/8 on old fermi cards) so using multiple special functions in a row will still cost the same as on more classical cards.

Vectors and Matrices
Vectors are a collection of multiple scalars, the cost every operation on them is multiplied by the number of components of the vector.

So  is 3x more expensive than

vec3 * float is as expensive as vec3 * vec3

Matrix multiplications are not the same as simple vector / scalar multiplications, they are way more expensive


 * is 4 cycles
 * is 9 cycles
 * is 16 cycles
 * is 8 cycles
 * is 27 cycles
 * is 64 cycles!