Difference between mul(vector, matix) & mul(matrix, vector)

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
liudingjun
Posts: 15
Joined: Thu May 26, 2011 12:32 pm

Difference between mul(vector, matix) & mul(matrix, vector)

Post by liudingjun »

hi, what the difference between mul(vector, matix) and mul(matrix, vector) in HLSL?
Most of time, we are using mul(vector, matrix) to transfrom a vector to some other space which specified by this matrix.
But sometimes, we must use mul(matrix, vector) instead of mul(vector, matrix).
I checked the D3D documents, it seems the relationship between them is like this:
mul(matix, vector) = mul(vector, transpose(matrix)).
So it makes me very confused about when and where I should use mul(matrix, vector).
Or Is there any one who know the geometry meaning of mul(matrix, vector)?
Mel
Competition winner
Posts: 2293
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Difference between mul(vector, matix) & mul(matrix, vect

Post by Mel »

The diference in the order can be translated into the asembly shader generated. But it depends on the API too. In DirectX the matrices are stored in a ROW major order, Which means that the rows are stored consecutively in memory. This way, a mul(vector,matrix) translates to 4 dot products directly, while the mul(matrix,vector) translates into a transpose operation (to place the numbers in the proper places in memory) and then 4 dot products, which gives the proper result. but in OpenGL is just the oposite! because in OpenGL the matrices are stored in a COLUMN major order.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
fmx

Re: Difference between mul(vector, matix) & mul(matrix, vect

Post by fmx »

I get confused about this too, but Mel is right:
it depends on the ordering of the Matrices being passed to the shader, as well as the API

Irrlicht uses DX row-major style matrices which is good for HLSL users

The only time you would use mul(matrix, vector) in HLSL is if the matrix you're passing to the shader is tranpsosed, eg usually normal matrices.
So you should be using mul(matrix, vector) mostly

GLSL uses mul(matrix, vector) with vanilla column-ordered matrices, so this needs to be reversed when working with irrlicht matrices
liudingjun
Posts: 15
Joined: Thu May 26, 2011 12:32 pm

Re: Difference between mul(vector, matix) & mul(matrix, vect

Post by liudingjun »

Thank you for your explanation.
Here I checked D3D ParallaxOcclusionMapping sample, in the ParallaxOcclusionMapping.fx there is a line of code write like this:
// Compute denormalized light vector in world space:
float3 vLightWS = g_LightDir;

// Normalize the light and view vectors and transform it to the tangent space:
float3x3 mWorldToTangent = float3x3( vTangentWS, vBinormalWS, vNormalWS );

// Propagate the view and the light vectors (in tangent space):
Out.vLightTS = mul( vLightWS, mWorldToTangent );
Out.vViewTS = mul( mWorldToTangent, vViewWS );

To transform the light direction into tangent space, they use mul(vector, matrix). But when they transform the view direction, they use mul(matrix,vector). I tried to replace the code like this:
Out.vViewTS = mul( vViewWS , mWorldToTangent );
It seems works OK. But when the angle between then view and surface become smaller, some artefact appear.
Right now, I am trying to integrate the ParallaxOcclusionMapping effect into the Irrlicht, this will make the normal effect pretty cool!
Is there any body can explan why?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Difference between mul(vector, matix) & mul(matrix, vect

Post by mongoose7 »

I think you have done the correct thing. You can probably expect artifacts as the angle becomes smaller, if you are try to occlude. I don't know the technique so I can't say how the artifacts are controlled normally. But I think you should shift your attention to the next phase - how the occlusion is achieved.
Post Reply