# What's the difference between dot and * in HLSL

This topic is 791 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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)

##### Share on other sites
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

##### Share on other sites

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.

##### Share on other sites

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.

##### Share on other sites

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;

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633700
• Total Posts
3013418
×