Matrix Manipulation inside shader or outside?

After having a look at libraries like glm, and seeing that the OpenGL 3.2 tutorials also use a ‘utils.c’ that implements some matrix manipulation routines for basic stuff like rotation etc, Im wondering what the general wisdom around here is:

Send matrices to the shader and manipulate in there, or do it outside via glm etc and send the results to the shader?

I always thought rotation etc should be done via passing an ‘angle’ variable and letting the shaders do the rest but I see people doing otherwise.

“it depends” :slight_smile:
Sometimes it is better to precompute as much as you can on CPU, taking advantage of double precision and no extra computation on the realtime shader.

I think for me it depends on how many times I have to do it. For example, calculating one rotation matrix via an angle and sending it down is simple. One a GPU shader, it would have to be recalculated thousands of times for all the vertices. Millions if it’s in the fragment shader. CPU libraries also provide much more powerful functions and easier debugging.

I guess in the end though, it’s all about benchmarking. If you’re doing a lot of shading calculations anyway and that isn’t the bottleneck of your application, it’s not a big deal either way.

Almost always outside the shader from a pure graphics throughput perspective.

If you manipulate inside the shader you’re doing the work per vertex and that is often much more workload than necessary.

It will still depend a lot on the number vertices transformed per matrix change and where your bottlenecks are in your application.

Hmmm…Makes sense.

So im guessing if we have more than one transform then concatenating all the transforms into one matrix and then sending it over is generally better…