Loading or pushing and popping matrices?

Started by
3 comments, last by hellraiser 16 years, 8 months ago
Hi, I'd like to know which of the following methods is faster in OpenGL when it comes to loading/resetting the modelview and projection matrices: - to load the matrix by using glLoadMatrixf - to rely on a set of glPushMatrix/glPopMatrix calls At the moment I am using a combination of the two, but I tend to self-convince myself that glLoadMatrixf should be faster. Which is the fastest method in your personal experience?
Advertisement
i would think that push/pop is faster

since loadmatrix would result in sending the matrix to the gpu, while push/pop tells the gpu with a command what to do..

i dont know if this is true, but it should be correct
If the GPU supports a matrix stack on board, then glPush and pop will be faster but I doubt that any of them do.

Also, don't worry about it. Don't optimize too early.
If you have the matrix ready, just use glLoadMatrix. It's only 16 floats that will be sent.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
GPUs don't have hardware matrix stacks. The current matrices are pushed down to the card when you make a draw call, along with other relevant state.

LoadMatrix is essentially a memory copy. PushMatrix is essentially a pointer increment and a memory copy. (I don't know if drivers implement some kind of adaptive growth strategy, but even if they do, the costs will get amortized quickly.) Either way, it doesn't matter at all. Don't concern yourself with such trivial nonsense.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
I was thinking that maybe OpenGL does some sort of 'state init' processing whenever a modelview or projection matrix is loaded and that maybe pushing/popping simply pushed/popped that 'state' making it the fastest method of the two.

But I'm glad I was overcomplicating this issue and that there are no performance issues involved!

Thanks to all for the invaluable input given.

This topic is closed to new replies.

Advertisement