13.12.2012 Views

ShaderX Shader Programming Tips & Tricks With DirectX 9

ShaderX Shader Programming Tips & Tricks With DirectX 9

ShaderX Shader Programming Tips & Tricks With DirectX 9

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Lookup Tables in<br />

Vertex <strong>Shader</strong>s<br />

Carsten Wenzel<br />

When writing vertex shader code, you almost always want to squeeze out a few<br />

instructions. Maybe you have to do it in order to stay within the instruction limit,<br />

which can easily be reached when doing complex animation and lighting calculations.<br />

Or maybe you simply want to speed up your code to gain some extra frames<br />

per second. Both goals can be achieved by encoding functions and terms in your<br />

vertex shader that consume a lot of instructions (and thus time) to evaluate.<br />

Another potential scenario would be the use of empirical data for certain calculations.<br />

This is where lookup tables can come in handy.<br />

A table lookup can be implemented quite easily using the address register a 0<br />

to index an array of constant registers c tableBase ... c tableBase + tableSize –1containing the<br />

actual table data. Generally, you want to keep the table as small as possible.<br />

Therefore, it is often necessary to interpolate between consecutive table values.<br />

Here’s an example. Say your lookup table stores values of a continuous function<br />

f(x) for all integers x in the range [0, 10]. Now it happens that you need to look up<br />

the value for f(3.25). The exact value isn’t stored in the lookup table. To get an<br />

estimated result, we could use the fractional part of the index value as the blend<br />

factor for a linear interpolation, i.e.:<br />

f( 325 . ) � f[ 3] �025 . �( f[ 4] � f[<br />

3])<br />

Do not forget about the Nyquist theorem 1 when representing continuous functions<br />

via lookup tables, or else you’ll face aliasing. That is, make sure the table is<br />

not too small — which implies that encoding terms and functions by means of<br />

lookup tables is not feasible if the range you’re interested in exhibits high frequencies.<br />

Also note that the table size directly affects the precision of the interpolated<br />

result.<br />

To demonstrate how a table lookup translates into actual shader code, let’s<br />

start with a description of a sample application. Imagine you’d like to write a particle<br />

effect that simulates stars in a galaxy. They are placed in clusters on the x/z<br />

plane with some variation in y and spin around the y axis with the galaxy center<br />

being the pivot point. Rotation speed is based on the squared distance (0 = d 2 =<br />

1 The Nyquist theorem describes one of the most important rules of sampling. To fully reproduce a<br />

continuous signal one needs to sample it with a frequency at least twice that of the highest frequency<br />

contained in the original signal. For example, to reproduce a full 20 kHz audio signal it has to be sampled at<br />

least 40,000 times a second.<br />

13

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!