fast alternative to sin/cos/tan

A0c9c0649c5deacc0ae3b7f7721c94d2
0
starstutter 101 Aug 13, 2009 at 00:02

I was wondering if there is a faster alternative or approximation to these functions in HLSL. I realize that they can be replaced with texture look-ups, but I’ve heard that these math operations have become faster than textue samples in recent years. I also understand that I can store a lighting model in a texture, but then I would still have to do at least 8 samples.

So, plain and simple, any other functions or tricks I could use?

thanks in advance :)

9 Replies

Please log in or register to post a reply.

8676d29610e6c98d6dd2d9c38528cd9c
0
alphadog 101 Aug 13, 2009 at 00:17

How accurate? For very, very fast, precompute some lookup tables. Of course, the more accurate, the bigger the table. Else, you want to look into Chebyshev. The book Numerical Recipes covers this pretty well.

A0c9c0649c5deacc0ae3b7f7721c94d2
0
starstutter 101 Aug 13, 2009 at 00:23

Are you talking about an array of half or float values? I would have to interpolate between then right?

Btw, I guess on a scale of 1 to 10 in terms of speed (with 1 being the regular functions in HLSL), I guess maybe 5-7

8676d29610e6c98d6dd2d9c38528cd9c
0
alphadog 101 Aug 13, 2009 at 00:26

Yeah, an array. And, yes, assuming you need to get values that lie between your pre-computed ones, then interpolation (simplest being linear, else you can get slightly fancy and get a little more accuracy) would be the way to go.

A0c9c0649c5deacc0ae3b7f7721c94d2
0
starstutter 101 Aug 13, 2009 at 00:29

ok, I’ll give it a shot and get back to you, thanks

8676d29610e6c98d6dd2d9c38528cd9c
0
alphadog 101 Aug 13, 2009 at 00:31

Of course, wikipedia is your friend: http://en.wikipedia.org/wiki/Lookup_table

8676d29610e6c98d6dd2d9c38528cd9c
0
alphadog 101 Aug 13, 2009 at 00:36

Duh, don’t forget our local heavy contributor: http://www.devmaster.net/forums/showthread.php?t=5784

A0c9c0649c5deacc0ae3b7f7721c94d2
0
starstutter 101 Aug 13, 2009 at 00:43

The result is actually slower, but I might know why:

I generated a lookup table like you said:

const half aCosT[64] =
{1.5708,
1.55517,
1.53954,
1.5239,
1.50826,
1.49259,……….. and so on…

then referenced it like this:

half theta_r = aCosT[(VdotN * 0.5) * 64]; <- VdotN stays between 0.0f - 1.0f

I rember reading in an ATI paper that referencing (indexing) with a float value is costly. Any way to perform this lookup without using a float (or half)?

A0c9c0649c5deacc0ae3b7f7721c94d2
0
starstutter 101 Aug 13, 2009 at 00:46

oh, well then I’ll try nicks idea :)