c++matrixdirect3d11

Why do we need to use the "transpose" of a transformed matrix? (direct3D11)


I have read the SimpleMath and also read the Programmers guide articles, but I can't seem to put my head around the purpose of transposing a matrix once it has been "transformed"

I mean, I understand what the transpose of a matrix is. I just don't understand why we need to actually take the transpose.

Take this code snippet for example..(assuming the matrices have already been created for the CameraView and the CameraProjection)

World = XMMatrixIdentity();                             

WVP = World * CameraView * CameraProjection;

XMMatrixTranspose(WVP)      

So my question is, what is the purpose of getting the transpose of WVP? what purpose does that serve for Direct3D 11?


Solution

  • First of all, let's see how matrices can be represented in memory. Consider the following matrix.

    1 2 3
    4 5 6
    7 8 9
    

    All values stored in computer memory are stored sequentially, there is no concept of "row" and "column", only address. If you represent the matrix above in row-major order, the float values in the matrix will be stored linearly in memory like this:

    Lowest address [ 1 2 3 4 5 6 7 8 9 ] Highest address

    If, on the other hand, you represent this same matrix in column-major order, the float values in the matrix will be stored in memory like this:

    Lowest address [ 1 4 7 2 5 8 3 6 9 ] Highest address

    So in row-major order, consecutive values of rows are contiguous in memory, whereas in column-major order, consecutive values of columns are contiguous in memory.

    Now, HLSL requires your matrices to be supplied in column-major order, but DirectXMath stores its matrices in row-major order because its implementation is faster that way, so you have to transpose it so that it gets fed into HLSL shaders in column-major order.

    Correction:

    HLSL defaults to taking your matrices in column-major order, but DirectXMath stores its matrices in row-major order because its implementation is faster that way, so one solution is to transpose the matrices so that they get fed into HLSL shaders in column-major order. Alternatively, you can override this default so that HLSL takes your matrices in row-major order, and then you wouldn't have to transpose them.