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):
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
- 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