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

1

6

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?

Kathrinekathryn answered 19/7, 2015 at 18:15 Comment(4)
Surely it depends on what you're going to actually do with the matrix?Quadrisect
Well, let's say i just want to create a 3d scene with the camera and the projection in it. If i transpose the transformed matrix, what will that do?Kathrinekathryn
No, but specifically what mathematical operation do you intend to with WVP? Transposing doesn't really do anything; it just replaces pre-multiplication of column vectors with post-multiplication of row vectors (and vice versa). Without context, that's about all that can be said ;)Quadrisect
The pattern is that the transpose happens just before setting it into a Constant Buffer which is sent to an HLSL shader... It is in fact to make the row-major matrix into a column-major matrix.Amadus
A
4

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.

Anastomosis answered 19/7, 2015 at 19:44 Comment(3)
HLSL requires your matrices to be supplied in column-major order ah thank you very much, this explains everything for me now :)Kathrinekathryn
HLSL doesn't require it, it just defaults to column-major. This is covered explicitly in the DirectXMath Programmer's Guide in the Working with D3DXMath section under Using DirectXMath with Direct3D and a link to the related HLSL docs. In short: DirectXMath supports either left- or right-handed viewing coordinates, but all matrices are row-major. You can tell HLSL to use row-major, but it defaults to column-major which is slightly faster. Hence the transpose just before setting a CB.Amadus
To make this more explicit, I've added this topic to the SimpleMath docs on the DirectX Tool Kit GitHub wiki.Amadus

© 2022 - 2024 — McMap. All rights reserved.