4
\$\begingroup\$

So, the standard way to transform vertices and then pass to the fragment shader in GLSL is something like this:

uniform mat4 u_modelview;
attribute vec4 a_position;

void main() {
    gl_Position = u_modelview * a_position;
}

However, I am working in 2D so there is redundancy in the 4x4 matrix. Would it be more efficient for me to do this?

uniform mat3 u_modelview;
attribute vec3 a_position;

void main() {
    gl_Position = vec4(u_modelview * a_position, 1.0);
}

gl_Position requires a 4 component vector so an extra operation is required at output. However the matrix multiplication is for 9 elements instead of 16. Can I do any better?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I think that this question was really valid because it prevents people (like me) from making the exact same performance mistake. \$\endgroup\$ Feb 6, 2012 at 2:29

2 Answers 2

6
\$\begingroup\$

This is correct way how to save few bits. As it is correct it is also little bit useless. GPU is highly optimized for 3D algebra and can do matrix-vector multiplication pretty fast. Same fast in 2D or 3D. This would be slightly faster on CPU, but not on GPU.

But it is ok and smart also. Just try to keep your eye on real problems like correct memory usage (textures, vertex buffers) and algorithms. Not on "micro optimizations".

And no, you can't make it better.

\$\endgroup\$
6
\$\begingroup\$

The GPU in an Xbox 360 is 240 billion flops, and it's a joke compared to modern desktop GPUs. You're not going to make any significant difference at all by spending 7 less flops in a shader. This is especially true if the GPU implements SIMD for 4 floats, which it almost certainly will because 4x4 matrices are by far and away the most common, which would make it nothing. This is not the right place to be spending time looking for optimizations.

\$\endgroup\$

You must log in to answer this question.