user755921
user755921

Reputation:

Does using column-major matrices in OpenGL cause striding on the GPU?

OpenGL conventionally (in documentation, examples etc.) uses column-major matrices and column vectors. Since a matrix-vector transform involves the dot product of the matrix rows with the column vector, does the GPU suffer striding and loss of spatial locality? Does the GPU re-arrange the 4x4 matrices between the GLSL and the GPU assembly code to remedy this?

It seems like it would make the dot-products faster to grab the first four floats of the matrix if they were laid out in rows, such that the GPU only has to make one memory access instead of four.

Upvotes: 4

Views: 1200

Answers (1)

user703016
user703016

Reputation: 37975

OpenGL uses column-major notation, but that is merely a notation. The underlying storage format is as you would expect.

The following matrix in column-major notation:

xx yx zx wx
xy yy zy wy
xz yz zz wz
0  0  0  1

is stored in memory like this:

xx xy xz 0 yx yy yz 0 zx zy zz 0 wx wy wz 1

Which, when executing a Matrix * vector product, can indeed be fetched in 4 perfectly coalesced accesses of 16 bytes each.

From the docs:

9.005 Are OpenGL matrices column-major or row-major?

For programming purposes, OpenGL matrices are 16-value arrays with base vectors laid out contiguously in memory. The translation components occupy the 13th, 14th, and 15th elements of the 16-element matrix, where indices are numbered from 1 to 16 as described in section 2.11.2 of the OpenGL 2.1 Specification.

Column-major versus row-major is purely a notational convention. Note that post-multiplying with column-major matrices produces the same result as pre-multiplying with row-major matrices. The OpenGL Specification and the OpenGL Reference Manual both use column-major notation. You can use any notation, as long as it's clearly stated.

Sadly, the use of column-major format in the spec and blue book has resulted in endless confusion in the OpenGL programming community. Column-major notation suggests that matrices are not laid out in memory as a programmer would expect.

Upvotes: 3

Related Questions