Ibrahim Ipek Ibrahim Ipek -4 years ago 54
C++ Question

Which one is better in matrix multiplication? GLM's overloaded * operator or using directly shaders

In GLM we have a overloaded '*' operator for multiplying matrices.

We can do matrix multiplication like this with using this operator in GLM:

glm::mat4 MVP = Projection * View * Model;
//after that pass MVP to uniform 'MVP'

But in other way, we can pass Projection, View and Model to three different uniforms and do multiplication in shader program.

GLM runs on CPU but shader programs run on GPU. Because of architecture of GPU, we can do matrix operations more faster than CPU in shader programs.

But i can't be sure. Which way is faster?

Answer Source

Ideally one would pass them separately if you need them separately in your shader for other purposes. If all you need is MVP, its best to multiply and pass an MVP as a single matrix to your shader.

What is best, depends a lot on the number of vertices you render as well as whether your rendering has multiple draw calls with few vertices or few draw calls with many vertices.

Always multiplication on CPU would be better since its once per draw call, instead of once per vertex. If the values need to vary between shaders, then one has to perform the multiplication in the shader.

I think I was too quick to answer, but this might be very similar to the question Should I calculate matrices on the GPU or on the CPU?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download