2010年6月6日 星期日

Matrix multiple in HLSL

In HLSL, We could use constant float to transit the value into shader code. There is a confusing problem to me, in row-major and column major rule will have the same result?

When we use setConstantF to the register, shader's assembly use vector to record them. At processing time, these vector use dot product operator with you variable. So when you use mul( pos, WorldMtx);
WorldMtx is:
{
_00, _01, _02, _03,
_10, _11, _12, _13,
_20, _21, _22, _23,
_30, _31, _32, _33
}
the WorldMtx need to be transpose at software, and the process is:
Output.x = dot( vector(pos.xyz, 1.0f), vector(_00,_10,_20,_30) );
Output.y = dot( vector(pos.xyz, 1.0f), vector(_01,_11,_21,_31) );
Output.z = dot( vector(pos.xyz, 1.0f), vector(_02,_12,_22,_32) );

Or you also can use mul(WoldMtx, pos); , and don't transpose WorldMtx. The result is:
Output.x = mul( vector(_00,_01,_02,_03), pos.x ) + mul(_30, 1.0f);
Output.y = mul( vector(_10,_11,_12,_13), pos.y ) + mul(_31, 1.0f);
Output.z = mul( vector(_20,_21,_22,_23), pos.z ) + mul(_32, 1.0f);

Although the result is the same, but you can imagine the second one has more operator in the shader code.

沒有留言:

張貼留言