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.

Appendix<br />

Say you’d like to create a lookup table containing tableSize entries for a function<br />

f(x) in range [x min, x max]. The values stored in an array of constant registers c tableBase<br />

... c tableBase + tableSize –1look like this:<br />

0 �i�tableSize � xmax � xmin<br />

�<br />

ctableBase� � f� i xmin �i� �<br />

� tableSize<br />

�1<br />

�<br />

To do a lookup you now need to map a value x from [x min, x max]to[tableBase,<br />

tableBase + tableSize – 1]:<br />

x�x index �<br />

x � x<br />

min<br />

max min<br />

This can be decoupled to:<br />

x<br />

index �<br />

x � x<br />

max min<br />

�( tableSize �1) �tableBase<br />

xmin<br />

�( tableSize �1) � �(<br />

tableSize<br />

�1) �tableBase<br />

x � x<br />

max min<br />

In the equation above, everything but x is invariant. Taking a closer look reveals<br />

that it can be expressed in terms of a mad:<br />

index �indexScale �x�indexOffset tableSize �1<br />

indexScale �<br />

xmax � xmin<br />

xmin<br />

indexOffset �� �( tableSize �1)�tableBase<br />

x � x<br />

max min<br />

Since tableBase can be used as a fixed relative offset when fetching values from<br />

the lookup table (as can be seen in the vertex shader sample code above),<br />

indexOffset can be rewritten as:<br />

xmin<br />

indexOffset �� �( tableSize �1)<br />

x � x<br />

max min<br />

Section I — Geometry Manipulation <strong>Tricks</strong><br />

Using Lookup Tables in Vertex <strong>Shader</strong>s<br />

17

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

Saved successfully!

Ooh no, something went wrong!