Skip to content

Commit

Permalink
librender: eliminate redundant calcualations for interpolators
Browse files Browse the repository at this point in the history
Invert the gradient matrix once instead of for each parameter
  • Loading branch information
jbush001 committed Apr 3, 2015
1 parent aa822a2 commit 6a0bd87
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
24 changes: 11 additions & 13 deletions software/libs/librender/LinearInterpolator.h
Expand Up @@ -23,26 +23,24 @@ namespace librender
{

//
// 2D linear interpolator. Given the value of a parameter at 3 points in a 2D space,
// determine the value at any other arbitrary point.
// 2D linear interpolator.
//

class LinearInterpolator
{
public:
void init(float x0, float y0, float c0, float x1,
float y1, float c1, float x2, float y2, float c2)
// The values a, b, c, d are a matrix that transform the coefficients into
// the standard basis.
// | a b |
// | c d |
void init(float a, float b, float c, float d, float x0, float y0,
float c0, float c1, float c2)
{
float a = x1 - x0;
float b = y1 - y0;
float c = x2 - x0;
float d = y2 - y0;
// Multiply by the matrix to find gradients
float e = c1 - c0;
float f = c2 - c0;

// Use Cramer's rule to solve for X and Y gradients.
float oneOverDeterminant = 1.0 / (a * d - b * c);
fXGradient = (e * d - b * f) * oneOverDeterminant;
fYGradient = (a * f - e * c) * oneOverDeterminant;
fXGradient = a * e + b * f;
fYGradient = c * e + d * f;

// Compute c at 0, 0
fC00 = c0 + -x0 * fXGradient + -y0 * fYGradient;
Expand Down
50 changes: 35 additions & 15 deletions software/libs/librender/ParameterInterpolator.h
Expand Up @@ -46,26 +46,44 @@ class ParameterInterpolator
float x1, float y1, float z1,
float x2, float y2, float z2)
{
fOneOverZInterpolator.init(x0, y0, 1.0f / z0, x1, y1, 1.0f / z1, x2, y2, 1.0f / z2);
fNumParams = 0;
fX0 = x0;
fY0 = y0;
fZ0 = z0;
fX1 = x1;
fY1 = y1;
fZ1 = z1;
fX2 = x2;
fY2 = y2;
fZ2 = z2;

// We can express the deltas of any parameter as the
// following system of equations, where gX and gY
// represent the change of the coefficient for changes
// in X and Y. and c_n represents the coefficient at each
// point:
// | a b | | gX | = | c1 - c0 |
// | c d | | gY | | c2 - c0 |
float a = x1 - x0;
float b = y1 - y0;
float c = x2 - x0;
float d = y2 - y0;

// Invert the matrix so we can find the gradients given
// the coefficients.
float oneOverDeterminant = 1.0 / (a * d - b * c);
fA00 = d * oneOverDeterminant;
fA10 = -c * oneOverDeterminant;
fA01 = -b * oneOverDeterminant;
fA11 = a * oneOverDeterminant;

// Compute one over Z
fOneOverZInterpolator.init(fA00, fA01, fA10, fA11, fX0, fY0, 1.0f / z0,
1.0f / z1, 1.0f / z2);
fNumParams = 0;
}

// c1, c2, and c2 represent the value of the parameter at the three
// triangle points specified in setUpTriangle.
void setUpParam(float c0, float c1, float c2)
{
fParamOverZInterpolator[fNumParams++].init(fX0, fY0, c0 / fZ0,
fX1, fY1, c1 / fZ1,
fX2, fY2, c2 / fZ2);
fParamOverZInterpolator[fNumParams++].init(fA00, fA01, fA10, fA11,
fX0, fY0, c0 / fZ0, c1 / fZ1, c2 / fZ2);
}

// Compute 16 parameter values
Expand All @@ -88,15 +106,17 @@ class ParameterInterpolator
LinearInterpolator fOneOverZInterpolator;
LinearInterpolator fParamOverZInterpolator[kMaxParams];
int fNumParams = 0;
float fX0;
float fY0;
float fZ0;
float fX1;
float fY1;
float fZ1;
float fX2;
float fY2;
float fZ2;
float fX0;
float fY0;

// Inverse gradient matrix
float fA00;
float fA01;
float fA10;
float fA11;
};

}

0 comments on commit 6a0bd87

Please sign in to comment.