传统的OpenGL(在文档,示例等中)使用列主矩阵和列向量.由于矩阵向量变换涉及矩阵行与列向量的点积,因此GPU是否会受到跨越和空间局部性的损失? GPU是否重新安排GLSL和GPU汇编代码之间的4×4矩阵来解决这个问题?
如果它们按行排列,似乎可以使点积更快地获取矩阵的前四个浮点数,这样GPU只需要进行一次内存访问而不是四次.
最佳答案 OpenGL使用列主要表示法,但这仅仅是一种表示法.底层存储格式如您所料.
列主要表示法中的以下矩阵:
xx yx zx wx
xy yy zy wy
xz yz zz wz
0 0 0 1
存储在内存中,如下所示:
xx xy xz 0 yx yy yz 0 zx zy zz 0 wx wy wz 1
在执行Matrix *矢量产品时,确实可以在4个完全合并的访问中获取每个16字节.
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.