DirectX: Why examples call XMMatrixTranspose

I started looking into DirectX over spring break from college and came across this little interesting issue. I noticed that the Microsoft examples were calling XMMatrixTranspose in locations such as:

cb.mWorld = XMMatrixTranspose( g_World );
cb.mView = XMMatrixTranspose( g_View );
cb.mProjection = XMMatrixTranspose( g_Projection );

which surprised me because I always assumed that DirectX is all row major as far as matrix math goes.

Turns out the GLSL shader system uses column major as a default. I am not sure were this comes from; probably somewhere from the past, but I couldn’t let it just be like that. Transposing matrixes is an unnecessary code bottleneck, so better avoid it where possible.

The simple solution is to add the following to your shader:

#pragma pack_matrix( row_major )

and your program should be able to feed the shaders row major matrixes now. This means that the above code is now:

cb.mWorld = g_World;
cb.mView = g_View;
cb.mProjection = g_Projection;