Differences in vector and matrix storage in each language system or programming interface

Source: Internet
Author: User
Tags mul

The transpose () used in the text represents the matrix transpose function, and Mul () represents the matrix multiplication.

This paper attempts to explain some fuzzy concepts about matrix operation and storage in DX,HLSL,CG,OPENGGL,GLSL.

0, first figure out, the matrix to transform the row/column vector, how the matrix is expected.

Suppose there is a row vector p, a column vector q, a given "asymmetric matrix" A can be used to transform P can be expressed as Mul (P,a) (this is called post-multiply), can also be used to transform Q, expressed as Mul (A,Q) (this is called pre-multiply).

But a usually can only represent an expected transformation (such as the rotation of the translation of what), here is the assumption that a represents the expected transformation of the line vector p, that mul (p,a) is our expected transformation, and Mul (A,Q) represents the transformation is not what we expected, if we want to use A to transform Y, Mul ( Transpose (A), q) is what we expected.

As follows:

Mul (p,a):

The results are recorded as FLOAT3 (X*M11+Y*M21+Z*M31,X*M12+Y*M22+Z*M32,X*M13+Y*M23+Z*M33), which is the expected result.

Mul (A,Q):

The results are recorded as FLOAT3 (X*M11+Y*M12+Z*M13,X*M21+Y*M22+Z*M23,X*M31+Y*M32+Z*M33) and are not expected results.

Mul (Transpose (A), q):

The results are recorded as FLOAT3 (X*M11+Y*M21+Z*M31,X*M12+Y*M22+Z*M32,X*M13+Y*M23+Z*M33), which is the same as our expected results.

This is actually a simple linear algebra result:. For row vectors, if the desired transformation matrix is true, then for the column vectors, the matrix that conforms to the same expected transformation is.

1, and then figure out what is row-major and column-major.

A matrix A (below):

In memory, if stored in the order of {{M11,M12,M13},{M21,M22,M23},{M31,M32,M33}}, we say that the matrix is stored in row-major (row-first) way;

If you store in the order of {{M11,M21,M31},{M12,M22,M32},{M13,M23,M33}}, we say that the matrix is stored in the same way as Column-major (column first).

2, view:

Row-major vs. Column-major is just a storage order thing and doesn ' t has anything to does with what kind of vectors .

"(In a system,) the so-called Row-major and column-major are just two sequential ways of data storage (matrix), which is completely irrelevant to the (system) use of which vectors (line vectors or column vectors). 】

3, several conclusions.

Because some languages can change the way the matrix is stored by preprocessing directives, the conclusion is based on the default state of each system.

We assume that m= is an expected transformation in DirectX, then =

So for DX,HLSL,CG,OPENGGL,GLSL, there are:

DirectX: Use the row vector, which is stored using the Row-major method.

No. 0点, knowing that the matrix of the expected transformation is m=, the result is stored in row-major order as follows: {{M11,M12,M13},{M21,M22,M23},{M31,M32,M33}}

HLSL : The row vector is used by default and is stored by default using the Column-major method.

Because the line vector is also used, the matrix of the expected transformation is m=, and the result is stored in column-major order: {{M11,M21,M31},{M12,M22,M32},{M13,M23,M33}}

In particular, HLSL is the shader language of DX, but HLSL is not stored in the same way as DirectX's matrix, and the stored order results are different.

The classic MVP conversion in HLSL is written as: Out.position = Mul (in. Objectposition, worldviewprojection);

You can also use the line vector: Out.position = Mul (Transpose (worldviewprojection), in. Objectposition);

Reference: https://msdn.microsoft.com/en-us/library/windows/desktop/bb509634 (v=vs.85). Aspx#matrix_ordering mentions:

Matrix packing order for uniform parameters are set to column-major by default. This means each column of the matrix was stored in a single constant register.

CG : The default is to use the column vector, which is stored by default using the Row-major method.

Because the column uses the row vector, the matrix that knows the expected transformation through the No. 0点 is =, the result is in row-major storage order: {{M11,M21,M31},{M12,M22,M32},{M13,M23,M33}}

In particular, although we often say that CG is similar to HLSL, the matrix form and matrix used by default in CG are not the same as the HLSL, but they are stored in the same order.

CG,HLSL can change the way the matrix is stored by preprocessing commands.

The classic MVP conversion in CG is written as: Out.position = Mul (worldviewprojection,in. objectposition);// This is just the opposite of HLSL.

You can also use the line vector: Out.position = Mul (in. Objectposition,transpose (worldviewprojection));

OpenGL : The column vector is used by default and is stored by default using Column-major.

Because the column vectors are used, the matrix of the expected transformation is =, and the result is in column-major storage order: {{M11,M12,M13},{M21,M22,M23},{M31,M32,M33}}

GLSL : with OpenGL

4, as can be seen from the 3rd, in the default storage situation: DirectX and OPENGL/GLSL Matrix storage order of the same, HLSL and CG matrix storage order of the same.

Resources:

https://fgiesen.wordpress.com/2012/02/12/row-major-vs-column-major-row-vectors-vs-column-vectors/

https://fgiesen.wordpress.com/2011/05/04/row-major-vs-column-major-and-gl-es/

Http://www.cnblogs.com/soroman/archive/2008/03/21/1115571.html

http://www.catalinzima.com/2012/12/a-word-on-matrices/

Http://http.developer.nvidia.com/Cg/mul.html

Https://github.com/kakashidinho/Cg2glsl

Differences in vector and matrix storage in each language system or programming interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.