# Awake Yet? Good! Optimizing Inverse Trig for AMD GPUs.

## Subject: General Tech, Graphics Cards | December 2, 2014 - 03:11 AM | Scott Michaud

Tagged: amd, GCN, dice, frostbite

Inverse trigonometric functions are difficult to compute. Their use is often avoided like the plague. If, however, the value is absolutely necessary, it will probably be solved by approximations or, if possible, replacing them with easier functions by clever use of trig identities.

If you want to see how the experts approach this problem, then Sébastien Lagarde, a senior developer of the Frostbite engine at DICE, goes into detail with a blog post. By detail, I mean you will see some GPU assembly being stepped through by the end of it. What makes this particularly interesting is the diagrams at the end, showing what each method outputs as represented by the shading of a sphere.

If you are feeling brave, take a look.

Source: Sébastien Lagarde

After eating a good breakfast and a coke(hate coffee), I was feeling really good and energetic, ready for anything at work...

... then I read, or rather attempted to read this article and now my brain hurts and I feel sleepy again.

I have to admit, of all the math I've had to learn, Trigonometry was by far the worst. Even Vector Calculus wasn't as bad as Trig. And it's all because of the definitions! They are hard to remember! Sines, cosines, tangents, secants. You get lost in the sea of strange names, lost in the tangled web of their complex relationships with each other, and buried in a vast wasteland of imaginary numbers. Curse you, square root of -1!

Even Vector Calculus wasn't as hard on my brain as this was.

Quick and dirty approximations are the only way for these types of calculations, if frame rates are to be maintained, short of having your own quantum computer. The Atom itself is the best quantum lighting effects computer, the Atom along with photons. I can still notice some artifacts in the top row of images, but at 30 frames per sec. that would be un-noticeable. Just add a little diffuse material emission in the settings and some specular lighting and things will look much better. I wish at least one more sample run could have been run with a texture applied to the sphere. Siggraph is the conference to attend, if you have the math skills. The math libraries that go into the code base of all graphics software/drivers is very much PHD level, and millions of hours have gone into the development, maintenance, and improvement of algorithms and functions. And this trigonometric functions lesson is a good example of one small part of the massive code base required to accurately approximate lighting in software/mathematical equations for graphics. I can not wait until GPUs start to get dedicated Ray Tracing hardware, and does anyone at PcPer know if the PowerVR wizard GPU is in any mobile products yet?

Computers would be nowhere without Scientific Programmers, just look into all the formulas and algorithms that go into just PCI, and error correction for even the simplest devices. The average laptop comes with about 5+ dedicated CPUs in addition to the main CPU that runs the OS, and all that communication requires some form of trigonometric functions, and other high level math.

For graphics use it's probably enough with these approximations since the visuals look reasonable, even if not perfectly accurate.

But I wonder how useful they are when you need numerical accuracy through a lot of calculations.

Tiny errors can in some cases affect the end result in huge ways.

## Post new comment