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.

Points Inside a Triangle<br />

N-Patches<br />

A unique point inside a triangle can be computed via the three vertices defining<br />

the triangle and the barycentric coordinates of this interior point. The three vertices<br />

for each triangle are placed into constant memory, and we store two of the<br />

barycentric coordinates in the vertex stream (k can be computed from i and j). A<br />

vertex stream triangle index is used to select which set of three vertices in constant<br />

memory makes up the triangle with which we are currently working.<br />

Here we hit a small issue: Some vertices belong to more than one triangle.<br />

We have to duplicate each vertex attached to more than one triangle and give each<br />

one a separate index.<br />

//HLSL code for calculating interior points of a number of triangles.<br />

float3 VertexPos[3 * NUM BASE TRIANGLE];<br />

void main(float3 vertexStream : POSITION0)<br />

{<br />

float i = vertexStream.x;<br />

float j = vertexStream.y<br />

float k=1.0–i–j;<br />

float baseIndex = vertexStream.z * 256; // un-normalize index<br />

float3 pos = i*VertexPos[ (baseIndex*3) +0]+<br />

j*VertexPos[ (baseIndex*3) +1]+<br />

k*VertexPos[(baseIndex*3) +2];<br />

}<br />

N-Patches (Curved PN Patches [3]) are a type of bicubic patch where the control<br />

points are determined from a triangle’s vertex positions and normals. N-Patches<br />

come in two variations, both with cubic interpolated position, but they differ in<br />

whether the normal is interpolated linearly or quadratically. The algorithm calculates<br />

the control points for the patch and then evaluates at each point on the base<br />

triangle.<br />

Effectively, there are two frequencies at which this vertex shader needs executing;<br />

the control points need calculating only once per patch, whereas the evaluation<br />

needs running at every vertex. Some consoles can execute this pattern on<br />

the GPU, but on current PC architectures you can either generate the control<br />

points on the CPU and upload them to vertex constant memory or recalculate the<br />

control points at every vertex. The first uses CPU power per patch, and each<br />

patch uses more constant memory (for linear normal N-Patches, 39 floats versus<br />

18 for vertices), whereas recalculating at every vertex uses a lot of vertex shader<br />

power but allows better batching and has lower CPU overhead.<br />

float3 VertexPos[3 * NUM BASE TRIANGLE];<br />

float3 VertexNormals[3 * NUM BASE TRIANGLE];<br />

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

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

// bicubic control points<br />

float3 b300,b030,b003, b210,b120,b021, b201,b102,b012, b111;<br />

9

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

Saved successfully!

Ooh no, something went wrong!