Best to pre-calc in the vertex shader or fragment?

I’m only just getting into GLSL after coming across from writing pixel shaders in Adobe PixelBender. I have written a few fractal shaders that are just a single quad with the fragment shader doing all the heavy lifting.

There are quite a lot of calculations that are the same for all pixels that I’m currently doing in the fragment shader by assigning local fragment variables (like creating rotation matrices).
Am I right in thinking that all calculations in the fragment shader will be repeated for every pixel, whereas if I were to move the common ones into the vertex shader then they would only be calculated once per vertex?

Thanks for any help, as it’s obvious I am still very new to this :slight_smile:

Tom

Depends on the scene complexity. There are situations where your rendered scene has more vertices than pixels :wink:

Generally push calculations to the shader that will be called less frequent than the other per drawing calls.

A vertex shader is called for every vertex pushed to the pipeline.

A fragment shader is called for every pixel written to the screen buffer.

Basically, yes. Fragment shaders are ran for each fragment so its possible that a fragment shader will run more than once for a pixel depending on the rendering technique, scene’s depth complexity, if early-z is enabled, etc.

If something is constant across all fragments, perhaps the rotation matrices you mentioned, you can compute the value once on the CPU and pass it to the shader using a uniform.

Regards,
Patrick