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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

under D3DCAPS9.DeclType, the specific bit is D3DTCAPS_datatype, and the<br />

type to use is D3DDECLTYPE_datatype (where the data type is from the list<br />

above).<br />

Reclaiming Two Bits<br />

When DEC3N or UDEC3 formats are used, we seem to have lost two bits, but<br />

even two bits can be used quite effectively, so we want them back (e.g., if you<br />

have per-vertex branching, you could store the number of bones here). By causing<br />

two different vertex elements to point to the same memory in the vertex<br />

buffer, we can get access to our two bits (this requires the overlapped stream<br />

offset cap bit to be set).<br />

The vertex stream declaration for a single stream if we stored normals (a common<br />

use) as UDEC3 and wanted to reclaim our two bits is below. The vertex<br />

shader can now bind NORMAL0 to access the data as UDEC3 and NORMAL1 as<br />

UBYTE4.<br />

D3DVERTEXELEMENT9 decl[] =<br />

{<br />

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

Using Vertex <strong>Shader</strong>s for Geometry Compression<br />

Figure 1: Data from vertex stream element to vertex register<br />

// first element, a ‘normal’ UDEC3 declaration<br />

{ 0, // stream number<br />

0, // stream offset in bytes<br />

D3DDECLTYPE UDEC3, // vertex type for this access<br />

D3DDECLMETHOD DEFAULT, // not used so leave at default<br />

D3DDECLUSAGE NORMAL, // usage (used to bind in the vertex shader)<br />

0 // usage number (you can have n normals)<br />

},<br />

// second element, a UBYTE4 that accesses the same memory as the normal above<br />

{ 0, // stream number, same as first element<br />

0, // stream offset, same as first element<br />

D3DDECLTYPE UBYTE4, // vertex type for this access<br />

D3DDECLMETHOD DEFAULT, // not used so leave at default<br />

5

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

Saved successfully!

Ooh no, something went wrong!