PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Optimizing Texture Gradient Setup

i'm currently in the process of optimizing my 3d library. one thing that really takes a long time is texture gradient calculation. here's the code that does this for a triangle (klFloat is a class that wraps a fixed point data type, SSHIFT is only there to improve fixed point's numeric range, it serves no algorithmic purpose):

Code:

struct TexGradients {
void set(const DeviceVertex *pVertices)
{
int Counter;
enum { SSHIFT = 6 }; // allows us to move numbers into a good range for fixed point.
// for highest precision we apply it back as late as possible (see below)

klFloat dx02 = (pVertices[0].pos[0] - pVertices[2].pos[0]); dx02>>=SSHIFT;
klFloat dx12 = (pVertices[1].pos[0] - pVertices[2].pos[0]); dx12>>=SSHIFT;
klFloat dy02 = (pVertices[0].pos[1] - pVertices[2].pos[1]); dy02>>=SSHIFT;
klFloat dy12 = (pVertices[1].pos[1] - pVertices[2].pos[1]); dy12>>=SSHIFT;
klFloat OneOverdX = (dx12 * dy02) - (dx02 * dy12);
OneOverdX.inverse();

for(Counter = 0;Counter < 3;Counter++) {
klFloat OneOverZ = pVertices[Counter].texZ;
OneOverZ.inverse();

aOneOverZ[Counter] = OneOverZ;
aUOverZ[Counter] = pVertices[Counter].tex[0] * OneOverZ;
aVOverZ[Counter] = pVertices[Counter].tex[1] * OneOverZ;
}

dOneOverZdX = OneOverdX * (((aOneOverZ[1] - aOneOverZ[2]) * dy02) - ((aOneOverZ[0] - aOneOverZ[2]) * dy12));
dOneOverZdX >>= SSHIFT;
dOneOverZdY = OneOverdX * (((aOneOverZ[0] - aOneOverZ[2]) * dx12) - ((aOneOverZ[1] - aOneOverZ[2]) * dx02));
dOneOverZdY >>= SSHIFT;

dUOverZdX = OneOverdX * (((aUOverZ[1] - aUOverZ[2]) * dy02) - ((aUOverZ[0] - aUOverZ[2]) * dy12));
dUOverZdX >>= SSHIFT;
dUOverZdY = OneOverdX * (((aUOverZ[0] - aUOverZ[2]) * dx12) - ((aUOverZ[1] - aUOverZ[2]) * dx02));
dUOverZdY >>= SSHIFT;

dVOverZdX = OneOverdX * (((aVOverZ[1] - aVOverZ[2]) * dy02) - ((aVOverZ[0] - aVOverZ[2]) * dy12));
dVOverZdX >>= SSHIFT;
dVOverZdY = OneOverdX * (((aVOverZ[0] - aVOverZ[2]) * dx12) - ((aVOverZ[1] - aVOverZ[2]) * dx02));
dVOverZdY >>= SSHIFT;
}

klFloat aOneOverZ[3]; // 1/z for each vertex
klFloat aUOverZ[3]; // u/z for each vertex
klFloat aVOverZ[3]; // v/z for each vertex
klFloat dOneOverZdX, dOneOverZdY, // d(1/z)/dX, d(1/z)/dY
dUOverZdX, dUOverZdY, // d(u/z)/dX, d(u/z)/dY
dVOverZdX, dVOverZdY; // d(v/z)/dX, d(v/z)/dY
};


so these are really a lot of multiplications and even 4 divisions (hidden in the function inverse). any ideas on how to do this faster?

thx,
DANIEL
What happens in OneOverdX.inverse()? How fast does it run if you just hardcode in a test value instead of actually calling inverse?

Dan East
i have several implementations for klFloat. the most fastest one uses the intel gpp library and calls gppInvHP_16_32s() for inverse().

what i'm searching for (don't know if it exists) is a completely different way to calculate the gradients, since this code is really very complex (4 divides and 26 multiplications).

DANIEL
You should only need one divide to calculate all the gradients... I don't have any source code here to look at, but I am pretty sure about this.

The D3D graphic drivers I wrote for the Matrox Mystique and G100 were only using one division. The inverse of the triangle's area if I remember correctly.
hmm, i'd love to see that code :)

to my understanding i need at least one divide (1/z) per vertex, since i want to interpolate between all u/z, v/z and 1/z.

DANIEL
Oh sure... You do need one per vertex to figure out u/z and v/z. I was assuming this was done in a previous stage of your pipeline (during projection).

Then there is another one for the gradients. That makes it 4 allright.
why would i calculate 1/z during projection?
what i do is dividing by w but not by z...

DANIEL
drgoldie wrote:why would i calculate 1/z during projection?
what i do is dividing by w but not by z...


That's what I meant.