## Matrices and Pixel Bender

*This is a guest post from Bob Archer, a Development Lead in the Adobe Image Foundation team. I ofter see people doing a lot of extra math in their kernels when they could have simply done a matrix multiplication. This post won’t explain linear algebra to you, but if you’ve been confused by Pixel Bender’s column major order or aren’t sure how it works, it should hopefully give you some pointers… [End of intro]
*

I thought this would be of interest to everyone – I finally have the definitive guide on how matrix math works within Pixel Bender.

Pixel Bender matrixes are in column-major order. This means that this line:

float3x3 m = float3x3( a, b, c, d, e, f, g, h, i );

Sets up a matrix that looks like this:

a d g b e h c f i

If I access a matrix using a single [] operator I get these results:

m[ 0 ] == float3( a, b, c ); m[ 1 ] == float3( d, e, f ); m[ 2 ] == float3( g, h, i );

If I set up a vector like this:

float3 v = float3( X, Y, Z );

and do some vector / matrix or matrix / vector multiplications I get these results:

v * m == float3( Xa+Yb+Zc, Xd+Ye+Zf, Xg+Yh+Zi ) m * v == float3( Xa+Yd+Zg, Xb+Ye+Zh, Xc+Yf+Zi )

i.e. v * m does this calculation:

( X Y Z ) * a d g b e h c f i

while m * v does this calculation:

a d g X b e h * Y c f i Z

If we are multiplying two matrices together:

float3x3 m1 = float3x3( a, c, b, d, e, f, g, h, i ); float3x3 m2 = float3x3( J, K, L, M, N, O, P, Q, R );

m1 * m2 does this calculation:

a d g J M P aJ+dK+gL aM+dN+gO aP+dQ+gR b e h * K N Q == bJ+eK+hL bM+eN+hO bP+eQ+hR c f i L O R cJ+fK+iL cM+fN+iO cP+fQ+iR

Some references:

http://en.wikipedia.org/wiki/Row-major_order

http://everything2.com/title/Column+major+arrays+vs.+row+major+arrays