Matrix - Buffer, row - column representation problem

Greeting,


I represent my matrix buffer with the following data on the GPU:

Address: Data:

0x00 0 1 2 3

0x01 4 5 6 7

0x02 8 9 10 11

0x03 12 13 14 15


With the following shading code:


float4 (out) = float4x4 (m) * float4 (in)


According to apple's documentation


float3 u = m * v;

is equivalent to:

u = v.x * m[0];

u += v.y * m[1];

u += v.z * m[2];


My question is does m[0] refer to <0, 1, 2, 3> or <0, 4, 8, 12>?

As my experiment shows the GPU does compute the matrix as m[0] = <0, 1, 2, 3>

But common sense tell us m[0] should be the column <0, 4, 8, 12> otherwise it's very hard to read and debug


If m[0] did refer to <0, 1, 2, 3>, is there any way to transpose a matrix in shading file?

Or we have to work on transposed matrix all the time?


Thanks!

Answered by Jessy in 87704022

Matrix components are constructed and consumed in column-major order.

https://developer.apple.com/library/ios/documentation/Metal/Reference/MetalShadingLanguageGuide/data-types/data-types.html

The reason that the results are not consistent with your expectations is that you are not constructing your matrix in column major order. Here's the matrix you're making:

0 | 4 | 8 | 12

1 | 5 | 9 | 13

2 | 6 | 10 | 14

3 | 7 | 11 | 15

Accepted Answer

Matrix components are constructed and consumed in column-major order.

https://developer.apple.com/library/ios/documentation/Metal/Reference/MetalShadingLanguageGuide/data-types/data-types.html

The reason that the results are not consistent with your expectations is that you are not constructing your matrix in column major order. Here's the matrix you're making:

0 | 4 | 8 | 12

1 | 5 | 9 | 13

2 | 6 | 10 | 14

3 | 7 | 11 | 15

Thanks,


I can't believe apple make it column-major order with out clearly specifying it,

or it is a standard only I get confused?

That quotation is from the documentation. That was clear enough for me, but you can file a documentation bug if you have an idea about where they could put it which could educate people who are wired like you.


As for standards, I come from Unity and don't know the CPU APIs well, but I worked a lot with Cg, which was row-major, and GLSL, which was column-major. Neither option is good, for readability. Named subscripts would be better. However, I don't think that we could do that without a speed decrease. As such, Xcode should be the one to diagram this for us, not documentation or a forum.

Matrix - Buffer, row - column representation problem
 
 
Q