Welcome, Guest.
Username: Password: Remember me

TOPIC: optimize with atan2 instead of pow

optimize with atan2 instead of pow 2 months 1 week ago #1

Hello,

Came across this at:
shadertoyunofficial.wordpress.com/2016/0...ks-in-shadertoyglsl/
Quote:
'pow(x,y) is doing exp(y*log(x)): costly, not valid for x<0, not perfect precision.
for x^2, do prefer x*x !
for 2^x use exp2(x). the reverse log2 also exist (both in most langages ).
atan API also includes the 2 parameters version doing atan2(y,x)'

Tried it with bacondither's AdaptiveSharpen.fx (granted a bit older version then
the current one) and did see a 'reasonable' performance increase however
brussell's EyeAdaption.fx actually performed a bit less after modifying
the pow's to atan2's.
github.com/crosire/reshade-shaders/blob/...s/AdaptiveSharpen.fx
github.com/crosire/reshade-shaders/blob/...aders/EyeAdaption.fx

So...all I can say is give it a try maybe it could help.
At least there are some other nice tips on that page that might be useful.
The administrator has disabled public write access.

optimize with atan2 instead of pow 2 months 1 week ago #2

atan2 is I believe the single most expensive intrinsic HLSL function there is. Log and exp are quarter rate instructions, mul is full rate, so pow usually takes 9 cycles. Atan2 is a macro that spawns dozens of instructions, also conditions since it has to consider the signs of both arguments. Inverse trigonometry is to be avoid at all cost so asin, acos, atan.
Last Edit: 2 months 1 week ago by Marty McFly.
The administrator has disabled public write access.
The following user(s) said Thank You: brussell

optimize with atan2 instead of pow 2 months 1 week ago #3

Hmmm you might have a point
developer.download.nvidia.com/cg/atan2.html
developer.download.nvidia.com/cg/pow.html
Note the 'Reference Implementation' in both cases.

That said the Shadertoy people are normally very 'savvy'
but then again so are you.
The administrator has disabled public write access.

optimize with atan2 instead of pow 2 months 1 week ago #4

The person who made that post on the link you posted earlier is Fabrice Neyret, he's a programmer tier that I only wish to ever reach. That being said, none of the stuff he says mentioned you should use atan over pow which do completely different things anyways - he merely mentions that if the base is 2, don't use pow but exp2 and if you square or such, use x*x or x*x*x instead since it's faster. And the part about the atan just says that the atan() is the canonical inverse tangens, defined from 0 to 180 degrees, where atan2 has the full 360 degrees (hence the sign comparisons to determine which quadrant it's in), saying that you don't need to implement such things yourself.
Last Edit: 2 months 1 week ago by Marty McFly.
The administrator has disabled public write access.

optimize with atan2 instead of pow 2 months 1 week ago #5

Oh dear, you are completely right I conflated the points, and
I was actually quoting them in my initial post....
Thank you for the correction.
Last Edit: 2 months 1 week ago by thrive4.
The administrator has disabled public write access.