Sign in to follow this  
Quat

row_major

Recommended Posts

Quat    568
I am working on an instancing test and my objects were not showing up correctly. I looked at the d3d10 Instancing sample and noticed they prefix row_major for their instanced matrix:

row_major float4x4 mTransform : mTransform;

Adding this prefix fixed my code. How come this is necessary? I do not need this prefix for my global shader float4x4 variables. I do row-vector-matrix style ordered multiplication: mul(vector, matrix).

Share this post


Link to post
Share on other sites
MJP    19788
[quote name='Quat' timestamp='1313770678' post='4851259']
I am working on an instancing test and my objects were not showing up correctly. I looked at the d3d10 Instancing sample and noticed they prefix row_major for their instanced matrix:

row_major float4x4 mTransform : mTransform;

Adding this prefix fixed my code. How come this is necessary? I do not need this prefix for my global shader float4x4 variables. I do row-vector-matrix style ordered multiplication: mul(vector, matrix).
[/quote]

Shaders default to column-major packing in constant buffers, unless you pass D3D10_SHADER_PACK_MATRIX_ROW_MAJOR when compiling them. They do this because vector-matrix transformation can be expressed a bit more efficiently with column-major matrices. If you use the effects framework, it will automatically transpose matrices for you when you set them so that they patch the packing order. For instancing it makes more sense to use row-major packing, so that you can memcpy all of your matrices into your constant buffer without needing to transpose them.

Share this post


Link to post
Share on other sites
Furidan    124
I am sorry for posting it here, but the topic is quite close to what I would like to ask.

If shaders prefer to work with column-major matrices (so that the vector-matrix multiplication can be done with 4 dot-products) why does D3DX use row-major matrices?
For my engine I am now thinking of using column-major matrices for system memory, but since both Direct3D and OpenGL prefer "translation in 12, 13, 14" matrix layout instead of "translation in 3, 7, 11" one, there seems to be a hidden reason behind storing matrices as "12,13,14" in the system memory and as "3,7,11" in the video memory.

Is there really a serious performance-related reason for that or is that just a legacy from the software vertex processing times?

Share this post


Link to post
Share on other sites
Ripiz    539
[quote name='MJP' timestamp='1313777343' post='4851305']
For instancing it makes more sense to use row-major packing, so that you can memcpy all of your matrices into your constant buffer without needing to transpose them.
[/quote]
Why we can't memcpy() column-major matrices? It's still simply 64 bytes in the memory, just in different order.


[quote name='Furidan' timestamp='1314811737' post='4855978']
why does D3DX use row-major matrices?
[/quote]
I'm not exactly sure, but I think DirectX9 was row-major (or maybe previous versions). Changing math in the update would mess up a lot of code which assumes row-major order, therefore they couldn't do it. (random guess, not actually an information)

Share this post


Link to post
Share on other sites
MJP    19788
Furidan:

I couldn't really tell you why math libraries typically use row-major matrices, it's not really my area of expertise. If I recall correctly CPU SIMD instruction sets like SSE didn't have dot product support so that may have been a factor.

Ripiz:

I wasn't saying you can't memcpy column-major matrices, I was saying that you can't memcpy row-major matrices into a buffer that expects column-major matrices. You would have to transpose each matrix individually as you copy it in.

Share this post


Link to post
Share on other sites
Furidan    124
[quote name='MJP' timestamp='1314857634' post='4856159']
Furidan:

I couldn't really tell you why math libraries typically use row-major matrices, it's not really my area of expertise. If I recall correctly CPU SIMD instruction sets like SSE didn't have dot product support so that may have been a factor.
[/quote]

Thank you for the info. I will just use column-major matrices everywhere then [img]http://public.gamedev.net/public/style_emoticons/default/smile.gif[/img].

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this