[PROGRAMMING TIPS] Direct3D 10 to Direct3D 11: Transpose Your Matrices
Not a killer tip but if you have to port a Direct3D 9 or Direct3D 10 app to Direct3D 11, I think this tip will be useful. I spent around one hour before finding the solution in the DirectX SDK doc.
When you migrate an app from DX9 to DX10, there ‘s nothing special to do with your transformation matrices:
D3DXMATRIX proj_mat; D3DXMATRIX view_mat; D3DXMATRIX model_mat; D3DXMATRIX MVP; MVP = model_mat * view_mat * proj_mat; shader->effect->SetMatrix(hMVP, &MVP); // DX9 pMVP->SetMatrix((float*)&MVP); //DX10: same thing
Now when you migrate a DX9 or DX10 app to DX11, you have to transpose your transformation matrices before use them in your shaders. In the DX10 SDK doc (Samples and Tutorials > Direct3D 11 > Tutorials > Tutorials 4: 3D Spaces), you can read this:
Also, because matrices are arranged differently in memory in C++ and HLSL, we must transpose the matrices before updating them.
To transpose your matrices, just use D3DXMatrixTranspose():
where cb is a D3D11 constant buffer, tMVP the transposed matrix and MVP the transformation matrix in DX9/DX10 format.
This operation is absolutely fundamental, otherwise your meshes will look odd:
D3D11 – D3D10 matrices used without transpose.
D3D11 – D3D10 matrices have been transposed.
Actually (if I’m not wrong), Direct3D 11 stores the matrices in the same way than… OpenGL! At last, Direct3D does what OpenGL has always done…
What does it mean?
Simply that matrices you use with OpenGL can be directly used with Direct3D 11, no need anymore to reverse the order of matrices when you need to concat them. Cool!
Maybe the DX team should add this tips in the section “Migrating to Direct3D 11” 😉