4

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?

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • Surely it depends on what you're going to actually **do** with the matrix? – Oliver Charlesworth Jul 19 '15 at 18:46
  • 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? – Soon_to_be_code_master Jul 19 '15 at 18:52
  • 1
    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 ;) – Oliver Charlesworth Jul 19 '15 at 18:57
  • 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. – Chuck Walbourn Jul 20 '15 at 04:13

1 Answers1

3

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.

danieltm64
  • 471
  • 2
  • 4
  • `HLSL requires your matrices to be supplied in column-major order` ah thank you very much, this explains everything for me now :) – Soon_to_be_code_master Jul 19 '15 at 22:16
  • 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](https://msdn.microsoft.com/en-us/library/windows/desktop/bb509634.aspx#Matrix_Ordering). **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. – Chuck Walbourn Jul 20 '15 at 04:10
  • To make this more explicit, I've added this topic to the [SimpleMath](https://github.com/Microsoft/DirectXTK/wiki/SimpleMath) docs on the [DirectX Tool Kit](http://go.microsoft.com/fwlink/?LinkId=248929) GitHub wiki. – Chuck Walbourn Jul 20 '15 at 04:26