optimize with atan2 instead of pow

More
1 year 3 months ago #1 by thrive4
optimize with atan2 instead of pow was created by thrive4
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.

Please Log in or Create an account to join the conversation.

More
1 year 3 months ago - 1 year 3 months ago #2 by Marty McFly
Replied by Marty McFly on topic optimize with atan2 instead of pow
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: 1 year 3 months ago by Marty McFly.
The following user(s) said Thank You: brussell

Please Log in or Create an account to join the conversation.

More
1 year 3 months ago #3 by thrive4
Replied by thrive4 on topic optimize with atan2 instead of pow
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.

Please Log in or Create an account to join the conversation.

More
1 year 3 months ago - 1 year 3 months ago #4 by Marty McFly
Replied by Marty McFly on topic optimize with atan2 instead of pow
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: 1 year 3 months ago by Marty McFly.

Please Log in or Create an account to join the conversation.

More
1 year 3 months ago - 1 year 3 months ago #5 by thrive4
Replied by thrive4 on topic optimize with atan2 instead of pow
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: 1 year 3 months ago by thrive4.

Please Log in or Create an account to join the conversation.