aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/effects
diff options
context:
space:
mode:
authorGravatar rileya@google.com <rileya@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-07-03 13:43:35 +0000
committerGravatar rileya@google.com <rileya@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-07-03 13:43:35 +0000
commit3e33258cd15faae4a2906ddcc586e4008ee20e68 (patch)
treef58144c5d7a953f095a2288a7aaffcdb5985895d /src/gpu/effects
parentdbc5d28979bef1c92aa79e0dc1b5d27a41de5c9a (diff)
Added GPU implementation of 2-point conical gradient.
Review URL: https://codereview.appspot.com/6354062 git-svn-id: http://skia.googlecode.com/svn/trunk@4442 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/gpu/effects')
-rw-r--r--src/gpu/effects/GrGradientEffects.cpp321
-rw-r--r--src/gpu/effects/GrGradientEffects.h38
2 files changed, 349 insertions, 10 deletions
diff --git a/src/gpu/effects/GrGradientEffects.cpp b/src/gpu/effects/GrGradientEffects.cpp
index d876d62990..005002fc95 100644
--- a/src/gpu/effects/GrGradientEffects.cpp
+++ b/src/gpu/effects/GrGradientEffects.cpp
@@ -35,9 +35,9 @@ private:
};
void GrGLRadialGradient::emitFS(GrGLShaderBuilder* state,
- const char* outputColor,
- const char* inputColor,
- const char* samplerName) {
+ const char* outputColor,
+ const char* inputColor,
+ const char* samplerName) {
state->fSampleCoords.printf("vec2(length(%s.xy), 0.5)",
state->fSampleCoords.c_str());
state->fComplexCoord = true;
@@ -161,7 +161,7 @@ void GrGLRadial2Gradient::setupVariables(GrGLShaderBuilder* state, int stage) {
}
void GrGLRadial2Gradient::emitVS(GrGLShaderBuilder* state,
- const char* vertexCoords) {
+ const char* vertexCoords) {
GrStringBuilder* code = &state->fVSCode;
GrStringBuilder p2;
GrStringBuilder p3;
@@ -179,9 +179,9 @@ void GrGLRadial2Gradient::emitVS(GrGLShaderBuilder* state,
}
void GrGLRadial2Gradient::emitFS(GrGLShaderBuilder* state,
- const char* outputColor,
- const char* inputColor,
- const char* samplerName) {
+ const char* outputColor,
+ const char* inputColor,
+ const char* samplerName) {
GrStringBuilder* code = &state->fFSCode;
GrStringBuilder cName("c");
GrStringBuilder ac4Name("ac4");
@@ -259,9 +259,9 @@ void GrGLRadial2Gradient::initUniforms(const GrGLInterface* gl, int programID) {
}
void GrGLRadial2Gradient::setData(const GrGLInterface* gl,
- const GrGLTexture& texture,
- const GrCustomStage& baseData,
- int stageNum) {
+ const GrGLTexture& texture,
+ const GrCustomStage& baseData,
+ int stageNum) {
const GrRadial2Gradient& data =
static_cast<const GrRadial2Gradient&>(baseData);
GrAssert(data.isDegenerate() == fIsDegenerate);
@@ -323,6 +323,307 @@ bool GrRadial2Gradient::isEqual(const GrCustomStage& sBase) const {
/////////////////////////////////////////////////////////////////////
+class GrGLConical2Gradient : public GrGLProgramStage {
+
+public:
+
+ GrGLConical2Gradient(const GrProgramStageFactory& factory,
+ const GrCustomStage&);
+ virtual ~GrGLConical2Gradient() { }
+
+ virtual void setupVariables(GrGLShaderBuilder* state,
+ int stage) SK_OVERRIDE;
+ virtual void emitVS(GrGLShaderBuilder* state,
+ const char* vertexCoords) SK_OVERRIDE;
+ virtual void emitFS(GrGLShaderBuilder* state,
+ const char* outputColor,
+ const char* inputColor,
+ const char* samplerName) SK_OVERRIDE;
+ virtual void initUniforms(const GrGLInterface*, int programID) SK_OVERRIDE;
+ virtual void setData(const GrGLInterface*,
+ const GrGLTexture&,
+ const GrCustomStage&,
+ int stageNum) SK_OVERRIDE;
+
+ static StageKey GenKey(const GrCustomStage& s) {
+ return (static_cast<const GrConical2Gradient&>(s).isDegenerate());
+ }
+
+protected:
+
+ const GrGLShaderVar* fVSParamVar;
+ GrGLint fVSParamLocation;
+ const GrGLShaderVar* fFSParamVar;
+ GrGLint fFSParamLocation;
+
+ const char* fVSVaryingName;
+ const char* fFSVaryingName;
+
+ bool fIsDegenerate;
+
+ // @{
+ /// Values last uploaded as uniforms
+
+ GrScalar fCachedCenter;
+ GrScalar fCachedRadius;
+ GrScalar fCachedDiffRadius;
+
+ // @}
+
+private:
+
+ typedef GrGLProgramStage INHERITED;
+
+};
+
+GrGLConical2Gradient::GrGLConical2Gradient(
+ const GrProgramStageFactory& factory,
+ const GrCustomStage& baseData)
+ : INHERITED(factory)
+ , fVSParamVar(NULL)
+ , fFSParamVar(NULL)
+ , fVSVaryingName(NULL)
+ , fFSVaryingName(NULL)
+ , fCachedCenter(GR_ScalarMax)
+ , fCachedRadius(-GR_ScalarMax)
+ , fCachedDiffRadius(-GR_ScalarMax) {
+
+ const GrConical2Gradient& data =
+ static_cast<const GrConical2Gradient&>(baseData);
+ fIsDegenerate = data.isDegenerate();
+}
+
+void GrGLConical2Gradient::setupVariables(GrGLShaderBuilder* state, int stage) {
+ // 2 copies of uniform array, 1 for each of vertex & fragment shader,
+ // to work around Xoom bug. Doesn't seem to cause performance decrease
+ // in test apps, but need to keep an eye on it.
+ fVSParamVar = &state->addUniform(
+ GrGLShaderBuilder::kVertex_VariableLifetime,
+ kFloat_GrSLType, "uConical2VSParams", stage, 6);
+ fFSParamVar = &state->addUniform(
+ GrGLShaderBuilder::kFragment_VariableLifetime,
+ kFloat_GrSLType, "uConical2FSParams", stage, 6);
+
+ fVSParamLocation = GrGLProgramStage::kUseUniform;
+ fFSParamLocation = GrGLProgramStage::kUseUniform;
+
+ // For radial gradients without perspective we can pass the linear
+ // part of the quadratic as a varying.
+ if (state->fVaryingDims == state->fCoordDims) {
+ state->addVarying(kFloat_GrSLType, "Conical2BCoeff", stage,
+ &fVSVaryingName, &fFSVaryingName);
+ }
+}
+
+void GrGLConical2Gradient::emitVS(GrGLShaderBuilder* state,
+ const char* vertexCoords) {
+ GrStringBuilder* code = &state->fVSCode;
+ GrStringBuilder p2; // distance between centers
+ GrStringBuilder p3; // start radius
+ GrStringBuilder p5; // difference in radii (r1 - r0)
+ fVSParamVar->appendArrayAccess(2, &p2);
+ fVSParamVar->appendArrayAccess(3, &p3);
+ fVSParamVar->appendArrayAccess(5, &p5);
+
+ // For radial gradients without perspective we can pass the linear
+ // part of the quadratic as a varying.
+ if (state->fVaryingDims == state->fCoordDims) {
+ // r2Var = -2 * (r2Parm[2] * varCoord.x - r2Param[3] * r2Param[5])
+ code->appendf("\t%s = -2.0 * (%s * %s.x + %s * %s);\n",
+ fVSVaryingName, p2.c_str(),
+ vertexCoords, p3.c_str(), p5.c_str());
+ }
+}
+
+void GrGLConical2Gradient::emitFS(GrGLShaderBuilder* state,
+ const char* outputColor,
+ const char* inputColor,
+ const char* samplerName) {
+ GrStringBuilder* code = &state->fFSCode;
+
+ GrStringBuilder cName("c");
+ GrStringBuilder ac4Name("ac4");
+ GrStringBuilder dName("d");
+ GrStringBuilder qName("q");
+ GrStringBuilder r0Name("r0");
+ GrStringBuilder r1Name("r1");
+ GrStringBuilder tName("t");
+ GrStringBuilder p0; // 4a
+ GrStringBuilder p1; // 1/(2a)
+ GrStringBuilder p2; // distance between centers
+ GrStringBuilder p3; // start radius
+ GrStringBuilder p4; // start radius squared
+ GrStringBuilder p5; // difference in radii (r1 - r0)
+ fFSParamVar->appendArrayAccess(0, &p0);
+ fFSParamVar->appendArrayAccess(1, &p1);
+ fFSParamVar->appendArrayAccess(2, &p2);
+ fFSParamVar->appendArrayAccess(3, &p3);
+ fFSParamVar->appendArrayAccess(4, &p4);
+ fFSParamVar->appendArrayAccess(5, &p5);
+
+ // If we we're able to interpolate the linear component,
+ // bVar is the varying; otherwise compute it
+ GrStringBuilder bVar;
+ if (state->fCoordDims == state->fVaryingDims) {
+ bVar = fFSVaryingName;
+ GrAssert(2 == state->fVaryingDims);
+ } else {
+ GrAssert(3 == state->fVaryingDims);
+ bVar = "b";
+ code->appendf("\tfloat %s = -2.0 * (%s * %s.x + %s * %s);\n",
+ bVar.c_str(), p2.c_str(), state->fSampleCoords.c_str(),
+ p3.c_str(), p5.c_str());
+ }
+
+ // c = (x^2)+(y^2) - params[4]
+ code->appendf("\tfloat %s = dot(%s, %s) - %s;\n", cName.c_str(),
+ state->fSampleCoords.c_str(), state->fSampleCoords.c_str(),
+ p4.c_str());
+
+ // Non-degenerate case (quadratic)
+ if (!fIsDegenerate) {
+
+ // ac4 = params[0] * c
+ code->appendf("\tfloat %s = %s * %s;\n", ac4Name.c_str(), p0.c_str(),
+ cName.c_str());
+
+ // d = b^2 - ac4
+ code->appendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(),
+ bVar.c_str(), bVar.c_str(), ac4Name.c_str());
+
+ // if discriminant is < 0, set to transparent black and return
+ code->appendf("\tif (%s < 0.0) {\n\t\tgl_FragColor = vec4(0.0,0.0,0.0,"
+ "0.0);\n\t\treturn;\n\t}\n", dName.c_str());
+
+ // intermediate value we'll use to compute the roots
+ // q = -0.5 * (b +/- sqrt(d))
+ code->appendf("\tfloat %s = -0.5f * (%s + (%s < 0.0 ? -1.0 : 1.0)"
+ " * sqrt(%s));\n", qName.c_str(), bVar.c_str(),
+ bVar.c_str(), dName.c_str());
+
+ // compute both roots
+ // r0 = q * params[1]
+ code->appendf("\tfloat %s = %s * %s;\n", r0Name.c_str(), qName.c_str(),
+ p1.c_str());
+ // r1 = c / q
+ code->appendf("\tfloat %s = %s / %s;\n", r1Name.c_str(), cName.c_str(),
+ qName.c_str());
+
+ // Note: If there are two roots that both generate radius(t) > 0, the
+ // Canvas spec says to choose the larger t.
+
+ // so we'll look at the larger one first:
+ code->appendf("\tfloat %s = max(%s, %s);\n", tName.c_str(),
+ r0Name.c_str(), r1Name.c_str());
+
+ // if r(t) for the larger root is <= 0, try the other one
+ code->appendf("\tif (%s * %s + %s <= 0.0) {\n", tName.c_str(),
+ p5.c_str(), p3.c_str());
+ code->appendf("\t\t%s = min(%s, %s);\n", tName.c_str(),
+ r0Name.c_str(), r1Name.c_str());
+
+ // if r(t) for the smaller root is also <= 0, set the fragment to
+ // transparent black and return
+ code->appendf("\t\tif (%s * %s + %s <= 0.0) {\n\t\t\tgl_FragColor = "
+ "vec4(0.0,0.0,0.0,0.0);\n\t\t\treturn;\n\t\t}\n",
+ tName.c_str(), p5.c_str(), p3.c_str());
+
+ code->appendf("\t}\n");
+ } else {
+
+ // linear case: t = -c/b
+ code->appendf("\tfloat %s = -(%s / %s);\n", tName.c_str(),
+ cName.c_str(), bVar.c_str());
+
+ // set the fragment to transparent black and return if r(t) is <= 0
+ code->appendf("\tif (%s * %s + %s <= 0.0) {\n\t\tgl_FragColor = vec4("
+ "0.0,0.0,0.0,0.0);\n\t\treturn;\n\t}\n", tName.c_str(),
+ p5.c_str(), p3.c_str());
+ }
+ state->fComplexCoord = true;
+
+ // x coord is: t
+ // y coord is 0.5 (texture is effectively 1D)
+ state->fSampleCoords.printf("\tvec2(%s, 0.5)", tName.c_str());
+
+ state->emitDefaultFetch(outputColor, samplerName);
+}
+
+void GrGLConical2Gradient::initUniforms(const GrGLInterface* gl, int programID) {
+ GR_GL_CALL_RET(gl, fVSParamLocation,
+ GetUniformLocation(programID, fVSParamVar->getName().c_str()));
+ GR_GL_CALL_RET(gl, fFSParamLocation,
+ GetUniformLocation(programID, fFSParamVar->getName().c_str()));
+}
+
+void GrGLConical2Gradient::setData(const GrGLInterface* gl,
+ const GrGLTexture& texture,
+ const GrCustomStage& baseData,
+ int stageNum) {
+ const GrConical2Gradient& data =
+ static_cast<const GrConical2Gradient&>(baseData);
+ GrAssert(data.isDegenerate() == fIsDegenerate);
+ GrScalar centerX1 = data.center();
+ GrScalar radius0 = data.radius();
+ GrScalar diffRadius = data.diffRadius();
+
+ if (fCachedCenter != centerX1 ||
+ fCachedRadius != radius0 ||
+ fCachedDiffRadius != diffRadius) {
+
+ GrScalar a = GrMul(centerX1, centerX1) - diffRadius * diffRadius;
+
+ // When we're in the degenerate (linear) case, the second
+ // value will be INF but the program doesn't read it. (We
+ // use the same 6 uniforms even though we don't need them
+ // all in the linear case just to keep the code complexity
+ // down).
+ float values[6] = {
+ GrScalarToFloat(a * 4),
+ 1.f / (GrScalarToFloat(a)),
+ GrScalarToFloat(centerX1),
+ GrScalarToFloat(radius0),
+ GrScalarToFloat(SkScalarMul(radius0, radius0)),
+ GrScalarToFloat(diffRadius)
+ };
+
+ GR_GL_CALL(gl, Uniform1fv(fVSParamLocation, 6, values));
+ GR_GL_CALL(gl, Uniform1fv(fFSParamLocation, 6, values));
+ fCachedCenter = centerX1;
+ fCachedRadius = radius0;
+ fCachedDiffRadius = diffRadius;
+ }
+}
+
+
+/////////////////////////////////////////////////////////////////////
+
+GrConical2Gradient::GrConical2Gradient(GrScalar center,
+ GrScalar radius,
+ GrScalar diffRadius)
+ : fCenterX1 (center)
+ , fRadius0 (radius)
+ , fDiffRadius (diffRadius) {
+
+}
+
+GrConical2Gradient::~GrConical2Gradient() {
+
+}
+
+
+const GrProgramStageFactory& GrConical2Gradient::getFactory() const {
+ return GrTProgramStageFactory<GrConical2Gradient>::getInstance();
+}
+
+bool GrConical2Gradient::isEqual(const GrCustomStage& sBase) const {
+ const GrConical2Gradient& s = static_cast<const GrConical2Gradient&>(sBase);
+ return (this->isDegenerate() == s.isDegenerate());
+}
+
+/////////////////////////////////////////////////////////////////////
+
+
class GrGLSweepGradient : public GrGLProgramStage {
public:
diff --git a/src/gpu/effects/GrGradientEffects.h b/src/gpu/effects/GrGradientEffects.h
index 9bcf9dd772..cf6827f251 100644
--- a/src/gpu/effects/GrGradientEffects.h
+++ b/src/gpu/effects/GrGradientEffects.h
@@ -92,6 +92,44 @@ private:
typedef GrCustomStage INHERITED;
};
+class GrGLConical2Gradient;
+
+class GrConical2Gradient : public GrCustomStage {
+
+public:
+
+ GrConical2Gradient(GrScalar center, GrScalar radius, GrScalar diffRadius);
+ virtual ~GrConical2Gradient();
+
+ static const char* Name() { return "Two-Point Conical Gradient"; }
+ virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE;
+ virtual bool isEqual(const GrCustomStage&) const SK_OVERRIDE;
+
+ // The radial gradient parameters can collapse to a linear (instead
+ // of quadratic) equation.
+ bool isDegenerate() const { return SkScalarAbs(fDiffRadius) ==
+ SkScalarAbs(fCenterX1); }
+ GrScalar center() const { return fCenterX1; }
+ GrScalar diffRadius() const { return fDiffRadius; }
+ GrScalar radius() const { return fRadius0; }
+
+ typedef GrGLConical2Gradient GLProgramStage;
+
+private:
+
+ // @{
+ // Cache of values - these can change arbitrarily, EXCEPT
+ // we shouldn't change between degenerate and non-degenerate?!
+
+ GrScalar fCenterX1;
+ GrScalar fRadius0;
+ GrScalar fDiffRadius;
+
+ // @}
+
+ typedef GrCustomStage INHERITED;
+};
+
class GrGLSweepGradient;
class GrSweepGradient : public GrCustomStage {