Jump to content

  • Log In with Google      Sign In   
  • Create Account


HLSL distance vs dot


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 hdxpete   Members   -  Reputation: 449

Like
0Likes
Like

Posted 21 November 2012 - 07:00 PM

Doing point lights in one of my shaders recently. Someone with more experience then i suggested i use distance as opposed to dot for the attenuation calculations

so i wrote
attenFactors.z = dot(lightPos, lightPos);
attenFactors.y = sqrt(attenFactors.z);

this person suggested i rewrite it to
attenFactors.y = distance(lightPos);
attenFactors.z = attenFactors.y * attenFactors.y;

to me they are equivalent operations so i thought that mine would be slightly faster since im removing a multiplication. however we are talking 1 operation right?. so does it really matter?

Sponsor:

#2 MJP   Moderators   -  Reputation: 10223

Like
0Likes
Like

Posted 21 November 2012 - 07:18 PM

Functionally those are exactly the same. Dotting a vector with itself is equal summing the squares of its components, which is exactly what distance() will do. I would guess that once the code is optimized you'd also end up running the exact same instructions, at least on a modern GPU that uses scalar operations per-thread. Code like that usually comes from people writing for older GPU's where it might actually issue a dedicated dot product instruction.

Edited by MJP, 21 November 2012 - 07:22 PM.


#3 hdxpete   Members   -  Reputation: 449

Like
0Likes
Like

Posted 21 November 2012 - 11:17 PM

i figured dot might be hardware accelerated, but what your saying is making me ask.... distance doesn't do a square root?

#4 MJP   Moderators   -  Reputation: 10223

Like
1Likes
Like

Posted 22 November 2012 - 12:31 AM

The hardware may or may not have a native dot product instruction, it would depend on the GPU. The internal ISA actually changes pretty frequently among different chipset versions, and like I mentioned before the latest architectures from both Nvidia and AMD both only use scalar instructions.

distance() does compute the square root of the sum, I just left that part out for brevity.

#5 hdxpete   Members   -  Reputation: 449

Like
0Likes
Like

Posted 22 November 2012 - 12:46 AM

ok. thanks for the info. very helpful ^_^




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS