« Fragment Programs-ATI R3xx vs NVidia NV34 | Main | Quick and Dirty Vectorization »

May 10, 2005

Fragment Program Snippets

Here's some instruction sequences for common operations:

Compare against zero:

ABS value.x, value.x;
SUB value.x, 0, value.x;
CMP result, value.x, nonZero, isZero;

Compare against nearly zero (-epsilon<x<epsilon)

MAD value.x, value.x, value.x, negativeEpsilonSquared;
CMP result, value.x, isZero, nonZero;

floor()

FRC floor.x, value.x;
SUB floor.x, value.x, floor.x;

ceil()

FRC ceil.x, value.x;
MUL compare, ceil.x, -1;
SUB ceil.x, 1, ceil.x;
CMP ceil.x, compare, ceil.x, 0;
ADD ceil.x,

The tiling snippets need a couple of common variables, which can be put in the program.local[] parameters:

PARAM bounds = { textureWidth, textureHeight, 0, 0 };
PARAM recipBounds = { 1/textureWidth, 1/textureHeight, 0, 0 };

Tile texture coordinates by repeating:

MUL coords, coords, recipBounds;
FRC coords, coords;
MUL coords, coords, bounds;

Tile texture coordinates by mirroring:

MUL coords, coords, recipTwiceBounds;
FRC coords, coords;
MUL coords, coords, twiceBounds;
SUB coords, bounds, coords;
ABS coords, coords;
SUB coords, bounds, coords;

Clamp to a border:

# check if (x<0) or (y<0)
CMP compareResult, coords, 0, 1;

# Check if ((width-x)<0) or ((height-y)<0)
SUB coords, bounds, coords;
CMP compareResult, coords, 0, compareResult;

# x and y are either 0 if out of range, or 1 if in. Multiply them
# together so that the result is 0 if either are our of range, or
# 1 if they're both inside. Nudge the result down so we can
# compare easily
MAD compareResult.x, compareResult.x, compareResult.y, -0.1
CMP color, compareResult, borderColor, inputColor;

Convert a straight alpha color to premultiplied with black:

MUL premultColor, straightColor, straightColor.a;
MOV premultColor.a, straightColor.a;

Convert from premultiplied with black to straight alpha:

ADD premultColor.a, premultColor.a, 0.0000001;
RCP recipAlpha.x, premultColor.a;
MUL straightColor, premultColor, recipAlpha.x;
MOV straightColor.a, premultColor.a;

Posted by petewarden at May 10, 2005 05:04 PM

Comments

Post a comment




Remember Me?