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;

One thought on “DirectX: Why examples call XMMatrixTranspose

  1. Many thanks for this. Working out the reasons for generating the transpose there had me stumped. Very helpful post.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s