diff options
-rw-r--r-- | gyp/gpu.gypi | 2 | ||||
-rw-r--r-- | src/gpu/GrAAHairLinePathRenderer.cpp | 349 | ||||
-rw-r--r-- | src/gpu/GrPathUtils.cpp | 2 | ||||
-rw-r--r-- | src/gpu/effects/GrBezierEffect.cpp | 390 | ||||
-rw-r--r-- | src/gpu/effects/GrBezierEffect.h | 253 |
5 files changed, 345 insertions, 651 deletions
diff --git a/gyp/gpu.gypi b/gyp/gpu.gypi index 33fbfae637..c71abbf273 100644 --- a/gyp/gpu.gypi +++ b/gyp/gpu.gypi @@ -130,8 +130,6 @@ '<(skia_src_path)/gpu/effects/Gr1DKernelEffect.h', '<(skia_src_path)/gpu/effects/GrConfigConversionEffect.cpp', '<(skia_src_path)/gpu/effects/GrConfigConversionEffect.h', - '<(skia_src_path)/gpu/effects/GrBezierEffect.cpp', - '<(skia_src_path)/gpu/effects/GrBezierEffect.h', '<(skia_src_path)/gpu/effects/GrConvolutionEffect.cpp', '<(skia_src_path)/gpu/effects/GrConvolutionEffect.h', '<(skia_src_path)/gpu/effects/GrSimpleTextureEffect.cpp', diff --git a/src/gpu/GrAAHairLinePathRenderer.cpp b/src/gpu/GrAAHairLinePathRenderer.cpp index 526bd3ebe5..f8c47d829e 100644 --- a/src/gpu/GrAAHairLinePathRenderer.cpp +++ b/src/gpu/GrAAHairLinePathRenderer.cpp @@ -19,7 +19,8 @@ #include "SkStroke.h" #include "SkTemplates.h" -#include "effects/GrBezierEffect.h" +#include "gl/GrGLEffect.h" +#include "gl/GrGLSL.h" namespace { // quadratics are rendered as 5-sided polys in order to bound the @@ -693,6 +694,346 @@ void add_line(const SkPoint p[2], } +/** + * Shader is based off of "Resolution Independent Curve Rendering using + * Programmable Graphics Hardware" by Loop and Blinn. + * The output of this effect is a hairline edge for non rational cubics. + * Cubics are specified by implicit equation K^3 - LM. + * K, L, and M, are the first three values of the vertex attribute, + * the fourth value is not used. Distance is calculated using a + * first order approximation from the taylor series. + * Coverage is max(0, 1-distance). + */ +class HairCubicEdgeEffect : public GrEffect { +public: + static GrEffectRef* Create() { + GR_CREATE_STATIC_EFFECT(gHairCubicEdgeEffect, HairCubicEdgeEffect, ()); + gHairCubicEdgeEffect->ref(); + return gHairCubicEdgeEffect; + } + + virtual ~HairCubicEdgeEffect() {} + + static const char* Name() { return "HairCubicEdge"; } + + virtual void getConstantColorComponents(GrColor* color, + uint32_t* validFlags) const SK_OVERRIDE { + *validFlags = 0; + } + + virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { + return GrTBackendEffectFactory<HairCubicEdgeEffect>::getInstance(); + } + + class GLEffect : public GrGLEffect { + public: + GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) + : INHERITED (factory) {} + + virtual void emitCode(GrGLShaderBuilder* builder, + const GrDrawEffect& drawEffect, + EffectKey key, + const char* outputColor, + const char* inputColor, + const TextureSamplerArray& samplers) SK_OVERRIDE { + const char *vsName, *fsName; + + SkAssertResult(builder->enableFeature( + GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); + builder->addVarying(kVec4f_GrSLType, "CubicCoeffs", + &vsName, &fsName); + const SkString* attr0Name = + builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); + builder->vsCodeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str()); + + builder->fsCodeAppend("\t\tfloat edgeAlpha;\n"); + + builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName); + builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName); + builder->fsCodeAppendf("\t\tfloat dfdx =\n" + "\t\t3.0*%s.x*%s.x*dklmdx.x - %s.y*dklmdx.z - %s.z*dklmdx.y;\n", + fsName, fsName, fsName, fsName); + builder->fsCodeAppendf("\t\tfloat dfdy =\n" + "\t\t3.0*%s.x*%s.x*dklmdy.x - %s.y*dklmdy.z - %s.z*dklmdy.y;\n", + fsName, fsName, fsName, fsName); + builder->fsCodeAppend("\t\tvec2 gF = vec2(dfdx, dfdy);\n"); + builder->fsCodeAppend("\t\tfloat gFM = sqrt(dot(gF, gF));\n"); + builder->fsCodeAppendf("\t\tfloat func = abs(%s.x*%s.x*%s.x - %s.y*%s.z);\n", + fsName, fsName, fsName, fsName, fsName); + builder->fsCodeAppend("\t\tedgeAlpha = func / gFM;\n"); + builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); + // Add line below for smooth cubic ramp + // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n"); + + SkString modulate; + GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); + builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); + } + + static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { + return 0x0; + } + + virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} + + private: + typedef GrGLEffect INHERITED; + }; +private: + HairCubicEdgeEffect() { + this->addVertexAttrib(kVec4f_GrSLType); + } + + virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { + return true; + } + + GR_DECLARE_EFFECT_TEST; + + typedef GrEffect INHERITED; +}; + +/** + * Shader is based off of Loop-Blinn Quadratic GPU Rendering + * The output of this effect is a hairline edge for conics. + * Conics specified by implicit equation K^2 - LM. + * K, L, and M, are the first three values of the vertex attribute, + * the fourth value is not used. Distance is calculated using a + * first order approximation from the taylor series. + * Coverage is max(0, 1-distance). + */ + +/** + * Test were also run using a second order distance approximation. + * There were two versions of the second order approx. The first version + * is of roughly the form: + * f(q) = |f(p)| - ||f'(p)||*||q-p|| - ||f''(p)||*||q-p||^2. + * The second is similar: + * f(q) = |f(p)| + ||f'(p)||*||q-p|| + ||f''(p)||*||q-p||^2. + * The exact version of the equations can be found in the paper + * "Distance Approximations for Rasterizing Implicit Curves" by Gabriel Taubin + * + * In both versions we solve the quadratic for ||q-p||. + * Version 1: + * gFM is magnitude of first partials and gFM2 is magnitude of 2nd partials (as derived from paper) + * builder->fsCodeAppend("\t\tedgeAlpha = (sqrt(gFM*gFM+4.0*func*gF2M) - gFM)/(2.0*gF2M);\n"); + * Version 2: + * builder->fsCodeAppend("\t\tedgeAlpha = (gFM - sqrt(gFM*gFM-4.0*func*gF2M))/(2.0*gF2M);\n"); + * + * Also note that 2nd partials of k,l,m are zero + * + * When comparing the two second order approximations to the first order approximations, + * the following results were found. Version 1 tends to underestimate the distances, thus it + * basically increases all the error that we were already seeing in the first order + * approx. So this version is not the one to use. Version 2 has the opposite effect + * and tends to overestimate the distances. This is much closer to what we are + * looking for. It is able to render ellipses (even thin ones) without the need to chop. + * However, it can not handle thin hyperbolas well and thus would still rely on + * chopping to tighten the clipping. Another side effect of the overestimating is + * that the curves become much thinner and "ropey". If all that was ever rendered + * were "not too thin" curves and ellipses then 2nd order may have an advantage since + * only one geometry would need to be rendered. However no benches were run comparing + * chopped first order and non chopped 2nd order. + */ +class HairConicEdgeEffect : public GrEffect { +public: + static GrEffectRef* Create() { + GR_CREATE_STATIC_EFFECT(gHairConicEdgeEffect, HairConicEdgeEffect, ()); + gHairConicEdgeEffect->ref(); + return gHairConicEdgeEffect; + } + + virtual ~HairConicEdgeEffect() {} + + static const char* Name() { return "HairConicEdge"; } + + virtual void getConstantColorComponents(GrColor* color, + uint32_t* validFlags) const SK_OVERRIDE { + *validFlags = 0; + } + + virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { + return GrTBackendEffectFactory<HairConicEdgeEffect>::getInstance(); + } + + class GLEffect : public GrGLEffect { + public: + GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) + : INHERITED (factory) {} + + virtual void emitCode(GrGLShaderBuilder* builder, + const GrDrawEffect& drawEffect, + EffectKey key, + const char* outputColor, + const char* inputColor, + const TextureSamplerArray& samplers) SK_OVERRIDE { + const char *vsName, *fsName; + + SkAssertResult(builder->enableFeature( + GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); + builder->addVarying(kVec4f_GrSLType, "ConicCoeffs", + &vsName, &fsName); + const SkString* attr0Name = + builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); + builder->vsCodeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str()); + + builder->fsCodeAppend("\t\tfloat edgeAlpha;\n"); + + builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName); + builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName); + builder->fsCodeAppendf("\t\tfloat dfdx =\n" + "\t\t\t2.0*%s.x*dklmdx.x - %s.y*dklmdx.z - %s.z*dklmdx.y;\n", + fsName, fsName, fsName); + builder->fsCodeAppendf("\t\tfloat dfdy =\n" + "\t\t\t2.0*%s.x*dklmdy.x - %s.y*dklmdy.z - %s.z*dklmdy.y;\n", + fsName, fsName, fsName); + builder->fsCodeAppend("\t\tvec2 gF = vec2(dfdx, dfdy);\n"); + builder->fsCodeAppend("\t\tfloat gFM = sqrt(dot(gF, gF));\n"); + builder->fsCodeAppendf("\t\tfloat func = abs(%s.x*%s.x - %s.y*%s.z);\n", fsName, fsName, + fsName, fsName); + builder->fsCodeAppend("\t\tedgeAlpha = func / gFM;\n"); + builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); + // Add line below for smooth cubic ramp + // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n"); + + SkString modulate; + GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); + builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); + } + + static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { + return 0x0; + } + + virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} + + private: + typedef GrGLEffect INHERITED; + }; + +private: + HairConicEdgeEffect() { + this->addVertexAttrib(kVec4f_GrSLType); + } + + virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { + return true; + } + + GR_DECLARE_EFFECT_TEST; + + typedef GrEffect INHERITED; +}; + +GR_DEFINE_EFFECT_TEST(HairConicEdgeEffect); + +GrEffectRef* HairConicEdgeEffect::TestCreate(SkMWCRandom* random, + GrContext*, + const GrDrawTargetCaps& caps, + GrTexture*[]) { + return caps.shaderDerivativeSupport() ? HairConicEdgeEffect::Create() : NULL; +} + +/** + * The output of this effect is a hairline edge for quadratics. + * Quadratic specified by 0=u^2-v canonical coords. u and v are the first + * two components of the vertex attribute. Uses unsigned distance. + * Coverage is min(0, 1-distance). 3rd & 4th component unused. + * Requires shader derivative instruction support. + */ +class HairQuadEdgeEffect : public GrEffect { +public: + + static GrEffectRef* Create() { + GR_CREATE_STATIC_EFFECT(gHairQuadEdgeEffect, HairQuadEdgeEffect, ()); + gHairQuadEdgeEffect->ref(); + return gHairQuadEdgeEffect; + } + + virtual ~HairQuadEdgeEffect() {} + + static const char* Name() { return "HairQuadEdge"; } + + virtual void getConstantColorComponents(GrColor* color, + uint32_t* validFlags) const SK_OVERRIDE { + *validFlags = 0; + } + + virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { + return GrTBackendEffectFactory<HairQuadEdgeEffect>::getInstance(); + } + + class GLEffect : public GrGLEffect { + public: + GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) + : INHERITED (factory) {} + + virtual void emitCode(GrGLShaderBuilder* builder, + const GrDrawEffect& drawEffect, + EffectKey key, + const char* outputColor, + const char* inputColor, + const TextureSamplerArray& samplers) SK_OVERRIDE { + const char *vsName, *fsName; + const SkString* attrName = + builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); + builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n"); + + SkAssertResult(builder->enableFeature( + GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); + builder->addVarying(kVec4f_GrSLType, "HairQuadEdge", &vsName, &fsName); + + builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); + builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); + builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n" + "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n", + fsName, fsName); + builder->fsCodeAppendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, + fsName); + builder->fsCodeAppend("\t\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n"); + builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); + + SkString modulate; + GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); + builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); + + builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); + } + + static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { + return 0x0; + } + + virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} + + private: + typedef GrGLEffect INHERITED; + }; + +private: + HairQuadEdgeEffect() { + this->addVertexAttrib(kVec4f_GrSLType); + } + + virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { + return true; + } + + GR_DECLARE_EFFECT_TEST; + + typedef GrEffect INHERITED; +}; + +GR_DEFINE_EFFECT_TEST(HairQuadEdgeEffect); + +GrEffectRef* HairQuadEdgeEffect::TestCreate(SkMWCRandom* random, + GrContext*, + const GrDrawTargetCaps& caps, + GrTexture*[]) { + // Doesn't work without derivative instructions. + return caps.shaderDerivativeSupport() ? HairQuadEdgeEffect::Create() : NULL; +} + /////////////////////////////////////////////////////////////////////////////// namespace { @@ -956,10 +1297,8 @@ bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path, static const int kEdgeAttrIndex = 1; - GrEffectRef* hairQuadEffect = GrQuadEffect::Create(kHairAA_GrBezierEdgeType, - *target->caps()); - GrEffectRef* hairConicEffect = GrConicEffect::Create(kHairAA_GrBezierEdgeType, - *target->caps()); + GrEffectRef* hairQuadEffect = HairQuadEdgeEffect::Create(); + GrEffectRef* hairConicEffect = HairConicEdgeEffect::Create(); // Check devBounds SkASSERT(check_bounds<BezierVertex>(drawState, devBounds, arg.vertices(), diff --git a/src/gpu/GrPathUtils.cpp b/src/gpu/GrPathUtils.cpp index d412b8cd9d..e354e413f1 100644 --- a/src/gpu/GrPathUtils.cpp +++ b/src/gpu/GrPathUtils.cpp @@ -639,7 +639,7 @@ static void set_loop_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkSc // If (d0 < 0 && sign(k1) > 0) || (d0 > 0 && sign(k1) < 0), // we need to flip the orientation of our curve. // This is done by negating the k and l values - if ( (d[0] < 0 && k[1] > 0) || (d[0] > 0 && k[1] < 0)) { + if ( (d[0] < 0 && k[1] < 0) || (d[0] > 0 && k[1] > 0)) { for (int i = 0; i < 4; ++i) { k[i] = -k[i]; l[i] = -l[i]; diff --git a/src/gpu/effects/GrBezierEffect.cpp b/src/gpu/effects/GrBezierEffect.cpp deleted file mode 100644 index f4cebd4063..0000000000 --- a/src/gpu/effects/GrBezierEffect.cpp +++ /dev/null @@ -1,390 +0,0 @@ -/* - * Copyright 2013 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include "GrBezierEffect.h" - -#include "gl/GrGLEffect.h" -#include "gl/GrGLSL.h" -#include "GrTBackendEffectFactory.h" - -class GrGLConicEffect : public GrGLEffect { -public: - GrGLConicEffect(const GrBackendEffectFactory&, const GrDrawEffect&); - - virtual void emitCode(GrGLShaderBuilder* builder, - const GrDrawEffect& drawEffect, - EffectKey key, - const char* outputColor, - const char* inputColor, - const TextureSamplerArray&) SK_OVERRIDE; - - static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); - - virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} - -private: - GrBezierEdgeType fEdgeType; - - typedef GrGLEffect INHERITED; -}; - -GrGLConicEffect::GrGLConicEffect(const GrBackendEffectFactory& factory, - const GrDrawEffect& drawEffect) - : INHERITED (factory) { - const GrConicEffect& ce = drawEffect.castEffect<GrConicEffect>(); - fEdgeType = ce.getEdgeType(); -} - -void GrGLConicEffect::emitCode(GrGLShaderBuilder* builder, - const GrDrawEffect& drawEffect, - EffectKey key, - const char* outputColor, - const char* inputColor, - const TextureSamplerArray& samplers) { - const char *vsName, *fsName; - - const bool antiAlias = GrBezierEdgeTypeIsAA(fEdgeType); - const bool fill = GrBezierEdgeTypeIsFill(fEdgeType); - - if (antiAlias) { - SkAssertResult(builder->enableFeature( - GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); - } - builder->addVarying(kVec4f_GrSLType, "ConicCoeffs", - &vsName, &fsName); - const SkString* attr0Name = - builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); - builder->vsCodeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str()); - - builder->fsCodeAppend("\t\tfloat edgeAlpha;\n"); - - if (antiAlias) { - builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName); - builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName); - builder->fsCodeAppendf("\t\tfloat dfdx =\n" - "\t\t\t2.0*%s.x*dklmdx.x - %s.y*dklmdx.z - %s.z*dklmdx.y;\n", - fsName, fsName, fsName); - builder->fsCodeAppendf("\t\tfloat dfdy =\n" - "\t\t\t2.0*%s.x*dklmdy.x - %s.y*dklmdy.z - %s.z*dklmdy.y;\n", - fsName, fsName, fsName); - builder->fsCodeAppend("\t\tvec2 gF = vec2(dfdx, dfdy);\n"); - builder->fsCodeAppend("\t\tfloat gFM = sqrt(dot(gF, gF));\n"); - } - builder->fsCodeAppendf("\t\tfloat func = %s.x*%s.x - %s.y*%s.z;\n", fsName, fsName, - fsName, fsName); - if (!fill) { - builder->fsCodeAppend("\t\tfunc = abs(func);\n"); - } - if (antiAlias) { - builder->fsCodeAppend("\t\tedgeAlpha = func / gFM;\n"); - } - if (fill) { - if (antiAlias) { - builder->fsCodeAppend("\t\tedgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1.0);\n"); - // Add line below for smooth cubic ramp - // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n"); - } else { - builder->fsCodeAppend("\t\tedgeAlpha = float(func < 0.0);\n"); - } - } else { - builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); - // Add line below for smooth cubic ramp - // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n"); - } - - SkString modulate; - GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); - builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); -} - -GrGLEffect::EffectKey GrGLConicEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { - const GrConicEffect& ce = drawEffect.castEffect<GrConicEffect>(); - return ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2; -} - -////////////////////////////////////////////////////////////////////////////// - -GrConicEffect::~GrConicEffect() {} - -const GrBackendEffectFactory& GrConicEffect::getFactory() const { - return GrTBackendEffectFactory<GrConicEffect>::getInstance(); -} - -GrConicEffect::GrConicEffect(GrBezierEdgeType edgeType) : GrEffect() { - this->addVertexAttrib(kVec4f_GrSLType); - fEdgeType = edgeType; -} - -bool GrConicEffect::onIsEqual(const GrEffect& other) const { - const GrConicEffect& ce = CastEffect<GrConicEffect>(other); - return (ce.fEdgeType == fEdgeType); -} - -////////////////////////////////////////////////////////////////////////////// - -GR_DEFINE_EFFECT_TEST(GrConicEffect); - -GrEffectRef* GrConicEffect::TestCreate(SkMWCRandom* random, - GrContext*, - const GrDrawTargetCaps& caps, - GrTexture*[]) { - const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->nextULessThan(3)); - return GrConicEffect::Create(edgeType, caps); -} - -////////////////////////////////////////////////////////////////////////////// -// Quad -////////////////////////////////////////////////////////////////////////////// - -class GrGLQuadEffect : public GrGLEffect { -public: - GrGLQuadEffect(const GrBackendEffectFactory&, const GrDrawEffect&); - - virtual void emitCode(GrGLShaderBuilder* builder, - const GrDrawEffect& drawEffect, - EffectKey key, - const char* outputColor, - const char* inputColor, - const TextureSamplerArray&) SK_OVERRIDE; - - static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); - - virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} - -private: - GrBezierEdgeType fEdgeType; - - typedef GrGLEffect INHERITED; -}; - -GrGLQuadEffect::GrGLQuadEffect(const GrBackendEffectFactory& factory, - const GrDrawEffect& drawEffect) - : INHERITED (factory) { - const GrQuadEffect& ce = drawEffect.castEffect<GrQuadEffect>(); - fEdgeType = ce.getEdgeType(); -} - -void GrGLQuadEffect::emitCode(GrGLShaderBuilder* builder, - const GrDrawEffect& drawEffect, - EffectKey key, - const char* outputColor, - const char* inputColor, - const TextureSamplerArray& samplers) { - const char *vsName, *fsName; - - bool antiAlias = GrBezierEdgeTypeIsAA(fEdgeType); - bool fill = GrBezierEdgeTypeIsFill(fEdgeType); - - const SkString* attrName = - builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); - builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n"); - - if (antiAlias) { - SkAssertResult(builder->enableFeature( - GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); - } - builder->addVarying(kVec4f_GrSLType, "HairQuadEdge", &vsName, &fsName); - - if (antiAlias) { - builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); - builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); - builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n" - "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n", - fsName, fsName); - } - builder->fsCodeAppendf("\t\tfloat func = (%s.x*%s.x - %s.y);\n", fsName, fsName, - fsName); - if (!fill) { - builder->fsCodeAppend("\t\tfunc = abs(func);\n"); - } - if (antiAlias) { - builder->fsCodeAppend("\t\tedgeAlpha = func / sqrt(dot(gF, gF));\n"); - } - if (fill) { - if (antiAlias) { - builder->fsCodeAppend("\t\tedgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1.0);\n"); - // Add line below for smooth cubic ramp - // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n"); - } else { - builder->fsCodeAppend("\t\tedgeAlpha = float(func < 0.0);\n"); - } - } else { - builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); - // Add line below for smooth cubic ramp - // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n"); - } - - SkString modulate; - GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); - builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); - - builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); -} - -GrGLEffect::EffectKey GrGLQuadEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { - const GrQuadEffect& ce = drawEffect.castEffect<GrQuadEffect>(); - return ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2; -} - -////////////////////////////////////////////////////////////////////////////// - -GrQuadEffect::~GrQuadEffect() {} - -const GrBackendEffectFactory& GrQuadEffect::getFactory() const { - return GrTBackendEffectFactory<GrQuadEffect>::getInstance(); -} - -GrQuadEffect::GrQuadEffect(GrBezierEdgeType edgeType) : GrEffect() { - this->addVertexAttrib(kVec4f_GrSLType); - fEdgeType = edgeType; -} - -bool GrQuadEffect::onIsEqual(const GrEffect& other) const { - const GrQuadEffect& ce = CastEffect<GrQuadEffect>(other); - return (ce.fEdgeType == fEdgeType); -} - -////////////////////////////////////////////////////////////////////////////// - -GR_DEFINE_EFFECT_TEST(GrQuadEffect); - -GrEffectRef* GrQuadEffect::TestCreate(SkMWCRandom* random, - GrContext*, - const GrDrawTargetCaps& caps, - GrTexture*[]) { - const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->nextULessThan(3)); - return GrQuadEffect::Create(edgeType, caps); -} - -////////////////////////////////////////////////////////////////////////////// -// Cubic -////////////////////////////////////////////////////////////////////////////// - -class GrGLCubicEffect : public GrGLEffect { -public: - GrGLCubicEffect(const GrBackendEffectFactory&, const GrDrawEffect&); - - virtual void emitCode(GrGLShaderBuilder* builder, - const GrDrawEffect& drawEffect, - EffectKey key, - const char* outputColor, - const char* inputColor, - const TextureSamplerArray&) SK_OVERRIDE; - - static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); - - virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} - -private: - GrBezierEdgeType fEdgeType; - - typedef GrGLEffect INHERITED; -}; - -GrGLCubicEffect::GrGLCubicEffect(const GrBackendEffectFactory& factory, - const GrDrawEffect& drawEffect) - : INHERITED (factory) { - const GrCubicEffect& ce = drawEffect.castEffect<GrCubicEffect>(); - fEdgeType = ce.getEdgeType(); -} - -void GrGLCubicEffect::emitCode(GrGLShaderBuilder* builder, - const GrDrawEffect& drawEffect, - EffectKey key, - const char* outputColor, - const char* inputColor, - const TextureSamplerArray& samplers) { - const char *vsName, *fsName; - - bool antiAlias = GrBezierEdgeTypeIsAA(fEdgeType); - bool fill = GrBezierEdgeTypeIsFill(fEdgeType); - - if (antiAlias) { - SkAssertResult(builder->enableFeature( - GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); - } - builder->addVarying(kVec4f_GrSLType, "CubicCoeffs", - &vsName, &fsName); - const SkString* attr0Name = - builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); - builder->vsCodeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str()); - - builder->fsCodeAppend("\t\tfloat edgeAlpha;\n"); - - if (antiAlias) { - builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName); - builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName); - builder->fsCodeAppendf("\t\tfloat dfdx =\n" - "\t\t3.0*%s.x*%s.x*dklmdx.x - %s.y*dklmdx.z - %s.z*dklmdx.y;\n", - fsName, fsName, fsName, fsName); - builder->fsCodeAppendf("\t\tfloat dfdy =\n" - "\t\t3.0*%s.x*%s.x*dklmdy.x - %s.y*dklmdy.z - %s.z*dklmdy.y;\n", - fsName, fsName, fsName, fsName); - builder->fsCodeAppend("\t\tvec2 gF = vec2(dfdx, dfdy);\n"); - builder->fsCodeAppend("\t\tfloat gFM = sqrt(dot(gF, gF));\n"); - } - builder->fsCodeAppendf("\t\tfloat func = %s.x*%s.x*%s.x - %s.y*%s.z;\n", - fsName, fsName, fsName, fsName, fsName); - if (!fill) { - builder->fsCodeAppend("\t\tfunc = abs(func);\n"); - } - if (antiAlias) { - builder->fsCodeAppend("\t\tedgeAlpha = func / gFM;\n"); - } - if (fill) { - if (antiAlias) { - builder->fsCodeAppend("\t\tedgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1.0);\n"); - // Add line below for smooth cubic ramp - // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n"); - } else { - builder->fsCodeAppend("\t\tedgeAlpha = float(func < 0.0);\n"); - } - } else { - builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); - // Add line below for smooth cubic ramp - // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n"); - } - - SkString modulate; - GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); - builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); -} - -GrGLEffect::EffectKey GrGLCubicEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { - const GrCubicEffect& ce = drawEffect.castEffect<GrCubicEffect>(); - return ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2; -} - -////////////////////////////////////////////////////////////////////////////// - -GrCubicEffect::~GrCubicEffect() {} - -const GrBackendEffectFactory& GrCubicEffect::getFactory() const { - return GrTBackendEffectFactory<GrCubicEffect>::getInstance(); -} - -GrCubicEffect::GrCubicEffect(GrBezierEdgeType edgeType) : GrEffect() { - this->addVertexAttrib(kVec4f_GrSLType); - fEdgeType = edgeType; -} - -bool GrCubicEffect::onIsEqual(const GrEffect& other) const { - const GrCubicEffect& ce = CastEffect<GrCubicEffect>(other); - return (ce.fEdgeType == fEdgeType); -} - -////////////////////////////////////////////////////////////////////////////// - -GR_DEFINE_EFFECT_TEST(GrCubicEffect); - -GrEffectRef* GrCubicEffect::TestCreate(SkMWCRandom* random, - GrContext*, - const GrDrawTargetCaps& caps, - GrTexture*[]) { - const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->nextULessThan(3)); - return GrCubicEffect::Create(edgeType, caps); -} diff --git a/src/gpu/effects/GrBezierEffect.h b/src/gpu/effects/GrBezierEffect.h deleted file mode 100644 index 72f12517f8..0000000000 --- a/src/gpu/effects/GrBezierEffect.h +++ /dev/null @@ -1,253 +0,0 @@ -/* - * Copyright 2013 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#ifndef GrBezierEffect_DEFINED -#define GrBezierEffect_DEFINED - -#include "GrEffect.h" -#include "GrDrawTargetCaps.h" - -enum GrBezierEdgeType { - kFillAA_GrBezierEdgeType, - kHairAA_GrBezierEdgeType, - kFillNoAA_GrBezierEdgeType, -}; - -static inline bool GrBezierEdgeTypeIsFill(const GrBezierEdgeType edgeType) { - return (kHairAA_GrBezierEdgeType != edgeType); -} - -static inline bool GrBezierEdgeTypeIsAA(const GrBezierEdgeType edgeType) { - return (kFillNoAA_GrBezierEdgeType != edgeType); -} - -/** - * Shader is based off of Loop-Blinn Quadratic GPU Rendering - * The output of this effect is a hairline edge for conics. - * Conics specified by implicit equation K^2 - LM. - * K, L, and M, are the first three values of the vertex attribute, - * the fourth value is not used. Distance is calculated using a - * first order approximation from the taylor series. - * Coverage for AA is max(0, 1-distance). - * - * Test were also run using a second order distance approximation. - * There were two versions of the second order approx. The first version - * is of roughly the form: - * f(q) = |f(p)| - ||f'(p)||*||q-p|| - ||f''(p)||*||q-p||^2. - * The second is similar: - * f(q) = |f(p)| + ||f'(p)||*||q-p|| + ||f''(p)||*||q-p||^2. - * The exact version of the equations can be found in the paper - * "Distance Approximations for Rasterizing Implicit Curves" by Gabriel Taubin - * - * In both versions we solve the quadratic for ||q-p||. - * Version 1: - * gFM is magnitude of first partials and gFM2 is magnitude of 2nd partials (as derived from paper) - * builder->fsCodeAppend("\t\tedgeAlpha = (sqrt(gFM*gFM+4.0*func*gF2M) - gFM)/(2.0*gF2M);\n"); - * Version 2: - * builder->fsCodeAppend("\t\tedgeAlpha = (gFM - sqrt(gFM*gFM-4.0*func*gF2M))/(2.0*gF2M);\n"); - * - * Also note that 2nd partials of k,l,m are zero - * - * When comparing the two second order approximations to the first order approximations, - * the following results were found. Version 1 tends to underestimate the distances, thus it - * basically increases all the error that we were already seeing in the first order - * approx. So this version is not the one to use. Version 2 has the opposite effect - * and tends to overestimate the distances. This is much closer to what we are - * looking for. It is able to render ellipses (even thin ones) without the need to chop. - * However, it can not handle thin hyperbolas well and thus would still rely on - * chopping to tighten the clipping. Another side effect of the overestimating is - * that the curves become much thinner and "ropey". If all that was ever rendered - * were "not too thin" curves and ellipses then 2nd order may have an advantage since - * only one geometry would need to be rendered. However no benches were run comparing - * chopped first order and non chopped 2nd order. - */ -class GrGLConicEffect; - -class GrConicEffect : public GrEffect { -public: - static GrEffectRef* Create(const GrBezierEdgeType edgeType, const GrDrawTargetCaps& caps) { - GR_CREATE_STATIC_EFFECT(gConicFillAA, GrConicEffect, (edgeType)); - GR_CREATE_STATIC_EFFECT(gConicHairAA, GrConicEffect, (edgeType)); - GR_CREATE_STATIC_EFFECT(gConicFillNoAA, GrConicEffect, (edgeType)); - if (kFillAA_GrBezierEdgeType == edgeType) { - if (!caps.shaderDerivativeSupport()) { - return NULL; - } - gConicFillAA->ref(); - return gConicFillAA; - } else if (kHairAA_GrBezierEdgeType == edgeType) { - if (!caps.shaderDerivativeSupport()) { - return NULL; - } - gConicHairAA->ref(); - return gConicHairAA; - } else { - gConicFillNoAA->ref(); - return gConicFillNoAA; - } - } - - virtual ~GrConicEffect(); - - static const char* Name() { return "Conic"; } - - inline bool isAntiAliased() const { return GrBezierEdgeTypeIsAA(fEdgeType); } - inline bool isFilled() const { return GrBezierEdgeTypeIsFill(fEdgeType); } - inline GrBezierEdgeType getEdgeType() const { return fEdgeType; } - - typedef GrGLConicEffect GLEffect; - - virtual void getConstantColorComponents(GrColor* color, - uint32_t* validFlags) const SK_OVERRIDE { - *validFlags = 0; - } - - virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; - -private: - GrConicEffect(GrBezierEdgeType); - - virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE; - - GrBezierEdgeType fEdgeType; - - GR_DECLARE_EFFECT_TEST; - - typedef GrEffect INHERITED; -}; - -/////////////////////////////////////////////////////////////////////////////// -/** - * The output of this effect is a hairline edge for quadratics. - * Quadratic specified by 0=u^2-v canonical coords. u and v are the first - * two components of the vertex attribute. At the three control points that define - * the Quadratic, u, v have the values {0,0}, {1/2, 0}, and {1, 1} respectively. - * Coverage for AA is min(0, 1-distance). 3rd & 4th cimponent unused. - * Requires shader derivative instruction support. - */ -class GrGLQuadEffect; - -class GrQuadEffect : public GrEffect { -public: - static GrEffectRef* Create(const GrBezierEdgeType edgeType, const GrDrawTargetCaps& caps) { - GR_CREATE_STATIC_EFFECT(gQuadFillAA, GrQuadEffect, (kFillAA_GrBezierEdgeType)); - GR_CREATE_STATIC_EFFECT(gQuadHairAA, GrQuadEffect, (kHairAA_GrBezierEdgeType)); - GR_CREATE_STATIC_EFFECT(gQuadFillNoAA, GrQuadEffect, (kFillNoAA_GrBezierEdgeType)); - if (kFillAA_GrBezierEdgeType == edgeType) { - if (!caps.shaderDerivativeSupport()) { - return NULL; - } - gQuadFillAA->ref(); - return gQuadFillAA; - } else if (kHairAA_GrBezierEdgeType == edgeType) { - if (!caps.shaderDerivativeSupport()) { - return NULL; - } - gQuadHairAA->ref(); - return gQuadHairAA; - } else { - gQuadFillNoAA->ref(); - return gQuadFillNoAA; - } - } - - virtual ~GrQuadEffect(); - - static const char* Name() { return "Quad"; } - - inline bool isAntiAliased() const { return GrBezierEdgeTypeIsAA(fEdgeType); } - inline bool isFilled() const { return GrBezierEdgeTypeIsFill(fEdgeType); } - inline GrBezierEdgeType getEdgeType() const { return fEdgeType; } - - typedef GrGLQuadEffect GLEffect; - - virtual void getConstantColorComponents(GrColor* color, - uint32_t* validFlags) const SK_OVERRIDE { - *validFlags = 0; - } - - virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; - -private: - GrQuadEffect(GrBezierEdgeType); - - virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE; - - GrBezierEdgeType fEdgeType; - - GR_DECLARE_EFFECT_TEST; - - typedef GrEffect INHERITED; -}; - -////////////////////////////////////////////////////////////////////////////// -/** - * Shader is based off of "Resolution Independent Curve Rendering using - * Programmable Graphics Hardware" by Loop and Blinn. - * The output of this effect is a hairline edge for non rational cubics. - * Cubics are specified by implicit equation K^3 - LM. - * K, L, and M, are the first three values of the vertex attribute, - * the fourth value is not used. Distance is calculated using a - * first order approximation from the taylor series. - * Coverage for AA is max(0, 1-distance). - */ -class GrGLCubicEffect; - -class GrCubicEffect : public GrEffect { -public: - static GrEffectRef* Create(const GrBezierEdgeType edgeType, const GrDrawTargetCaps& caps) { - GR_CREATE_STATIC_EFFECT(gCubicFillAA, GrCubicEffect, (kFillAA_GrBezierEdgeType)); - GR_CREATE_STATIC_EFFECT(gCubicHairAA, GrCubicEffect, (kHairAA_GrBezierEdgeType)); - GR_CREATE_STATIC_EFFECT(gCubicFillNoAA, GrCubicEffect, (kFillNoAA_GrBezierEdgeType)); - if (kFillAA_GrBezierEdgeType == edgeType) { - if (!caps.shaderDerivativeSupport()) { - return NULL; - } - gCubicFillAA->ref(); - return gCubicFillAA; - } else if (kHairAA_GrBezierEdgeType == edgeType) { - if (!caps.shaderDerivativeSupport()) { - return NULL; - } - gCubicHairAA->ref(); - return gCubicHairAA; - } else { - gCubicFillNoAA->ref(); - return gCubicFillNoAA; - } - } - - virtual ~GrCubicEffect(); - - static const char* Name() { return "Cubic"; } - - inline bool isAntiAliased() const { return GrBezierEdgeTypeIsAA(fEdgeType); } - inline bool isFilled() const { return GrBezierEdgeTypeIsFill(fEdgeType); } - inline GrBezierEdgeType getEdgeType() const { return fEdgeType; } - - typedef GrGLCubicEffect GLEffect; - - virtual void getConstantColorComponents(GrColor* color, - uint32_t* validFlags) const SK_OVERRIDE { - *validFlags = 0; - } - - virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; - -private: - GrCubicEffect(GrBezierEdgeType); - - virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE; - - GrBezierEdgeType fEdgeType; - - GR_DECLARE_EFFECT_TEST; - - typedef GrEffect INHERITED; -}; - -#endif |