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.

What remains to do is write the vertex shader to animate each particle. The<br />

code will be split into several pieces showing all necessary steps to get the stars<br />

spinning on the GPU. The following part computes the table index.<br />

#define srcPos v0 // (x, y, z, 1)<br />

#define temp0 r0<br />

#define temp1 r1<br />

#define temp2 r2<br />

#define worldPos r3<br />

#define TABLE INDEX 9<br />

#define TABLE BASE 10<br />

vs.1.1<br />

#ifdef DX9<br />

dcl position0 srcPos<br />

#endif<br />

// calculate d^2 and table index<br />

dp3 temp0, srcPos, srcPos<br />

mad temp1, temp0, c[TABLE INDEX].x, c[TABLE INDEX].y<br />

// get fraction of table index<br />

expp temp0.y, temp1.y<br />

// set table index for relative addressing of lookup table<br />

#ifdef DX9<br />

add a0.x, temp1.y, -temp0.y<br />

#else // DX8<br />

mov a0.x, temp1.y<br />

#endif<br />

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

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

15<br />

The first section of the vertex shader determines the table index for the lookup<br />

table. It calculates d 2 and applies the index scale and offset constant. Why mad can<br />

be used to evaluate the table index in a single instruction and how to set up the<br />

index scale and offset constant for lookup tables covering arbitrary intervals is<br />

shown in the appendix to this article.<br />

When copying the table index to a 0, care must be taken. According to the<br />

<strong>DirectX</strong> 8.1 specs, moving a value into the address register automatically computes<br />

the floor of that value — exactly the behavior we are after. Quite the<br />

contrary if you use <strong>DirectX</strong> 9. Here you have to do the floor calculation yourself<br />

because a value moved into the address register gets rounded to the nearest integer.<br />

This would obviously break the interpolation code due to a possibly incorrect<br />

index in a 0.<br />

The following part of the shader calculates the linearly interpolated table<br />

lookup value. It fetches the values for a 0.x and a 0.x + 1 from the lookup table.<br />

Then it takes the already-computed fraction of the table index to blend between<br />

them.

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

Saved successfully!

Ooh no, something went wrong!