Advertisement

What's the difference between dot and * in HLSL

Started by October 11, 2016 02:18 AM
4 comments, last by ferhua 8 years, 1 month ago

I am following tutorials to implement directional light in directx, the diffuse part is as below


diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f);
float diffuseFactor = dot(lightVec, normal);
diffuse = saturate(diffuseFactor * mat.Diffuse * L.Diffuse);

what confused me is the result of diffuse.

mat.Diffuse * L.Diffuse should be a float4, how could it be saved to "diffuse", a float type variable.

the same question to the ambient part


ambient = float4(0.0f, 0.0f, 0.0f, 0.0f);
ambient = mat.Ambient * L.Ambient;

both mat.Ambient and L.Ambient are float4 types, after the * operation, it should be a float type.

I find materials on the internet and found there are only two multiply operations of vectors,dot product and cross product, so why the code mentioned above still use *, but not dot function?

Dot is a dot product.
'*' is a component-wise multiplication.


float4 a, b;
float4 c = a*b; //c = (a.x*b.x,a.y*b.y,a.z*b.z, a.w*b.w)
When you multiply float by float4, the result is float4. The float will be broadcasted to float4.


float a;
float4 b;
float4 c = a*b; //c = (a*b.x,a*b.y,a*b.z,a*b.w)
Advertisement
the multiplication operator on vectors in HLSL is the Hadamard product/component-wise multiplication, i.e. a * b is a vector with the elements (a.x * b.x, a.y * b.y, ...).

the dot product is the sum of the above, i.e. dot(a, b) is a scalar with the value a.x * b.x + a.y * b.y ...

I am following tutorials to implement directional light in directx, the diffuse part is as below

diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f);
float diffuseFactor = dot(lightVec, normal);
diffuse = saturate(diffuseFactor * mat.Diffuse * L.Diffuse);
what confused me is the result of diffuse.
mat.Diffuse * L.Diffuse should be a float4, how could it be saved to "diffuse", a float type variable.


I dunno, that's definitely surprising if diffuse is a float. just as surprising as the previous assignment of float4(0, 0, 0, 0). are you sure it's not float4?

the same question to the ambient part

ambient = float4(0.0f, 0.0f, 0.0f, 0.0f);
ambient = mat.Ambient * L.Ambient;
both mat.Ambient and L.Ambient are float4 types, after the * operation, it should be a float type.


again, no idea why ambient would be float rather than float4

I find materials on the internet and found there are only two multiply operations of vectors,dot product and cross product, so why the code mentioned above still use *, but not dot function?


there aren't only two, the Hadamard product just isn't used as often as the dot and cross products

That all looks fine, assuming diffuse and ambient are float4's, which they almost certainly should be if you want lights that aren't just white.

That all looks fine, assuming diffuse and ambient are float4's, which they almost certainly should be if you want lights that aren't just white.

Diffuse and ambient should be float3. There is no sense of calculating light value for alpha channel. It's just wasteful.

Thank you all, I have mistaken here


diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f);
float diffuseFactor = dot(lightVec, normal);
diffuse = saturate(diffuseFactor * mat.Diffuse * L.Diffuse);

the correct one should be


diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f);
float diffuseFactor = saturate(dot(lightVec, normal));
diffuse = diffuseFactor * mat.Diffuse * L.Diffuse;

This topic is closed to new replies.

Advertisement