View Full Version : Matrix Manipulation inside shader or outside?

04-02-2010, 09:24 AM
After having a look at libraries like glm (http://glm.g-truc.net/), 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.

04-02-2010, 09:56 AM
"it depends" :)
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.

04-02-2010, 11:24 AM
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.

04-02-2010, 11:33 AM
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.

04-02-2010, 03:17 PM
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...