Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
1  
I think that this question was really valid because it prevents people (like me) from making the exact same performance mistake. –  Robert Massaioli Feb 6 '12 at 2:29

2 Answers 2

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.