michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef GrBezierEffect_DEFINED michael@0: #define GrBezierEffect_DEFINED michael@0: michael@0: #include "GrDrawTargetCaps.h" michael@0: #include "GrEffect.h" michael@0: #include "GrVertexEffect.h" michael@0: #include "GrTypesPriv.h" michael@0: michael@0: /** michael@0: * Shader is based off of Loop-Blinn Quadratic GPU Rendering michael@0: * The output of this effect is a hairline edge for conics. michael@0: * Conics specified by implicit equation K^2 - LM. michael@0: * K, L, and M, are the first three values of the vertex attribute, michael@0: * the fourth value is not used. Distance is calculated using a michael@0: * first order approximation from the taylor series. michael@0: * Coverage for AA is max(0, 1-distance). michael@0: * michael@0: * Test were also run using a second order distance approximation. michael@0: * There were two versions of the second order approx. The first version michael@0: * is of roughly the form: michael@0: * f(q) = |f(p)| - ||f'(p)||*||q-p|| - ||f''(p)||*||q-p||^2. michael@0: * The second is similar: michael@0: * f(q) = |f(p)| + ||f'(p)||*||q-p|| + ||f''(p)||*||q-p||^2. michael@0: * The exact version of the equations can be found in the paper michael@0: * "Distance Approximations for Rasterizing Implicit Curves" by Gabriel Taubin michael@0: * michael@0: * In both versions we solve the quadratic for ||q-p||. michael@0: * Version 1: michael@0: * gFM is magnitude of first partials and gFM2 is magnitude of 2nd partials (as derived from paper) michael@0: * builder->fsCodeAppend("\t\tedgeAlpha = (sqrt(gFM*gFM+4.0*func*gF2M) - gFM)/(2.0*gF2M);\n"); michael@0: * Version 2: michael@0: * builder->fsCodeAppend("\t\tedgeAlpha = (gFM - sqrt(gFM*gFM-4.0*func*gF2M))/(2.0*gF2M);\n"); michael@0: * michael@0: * Also note that 2nd partials of k,l,m are zero michael@0: * michael@0: * When comparing the two second order approximations to the first order approximations, michael@0: * the following results were found. Version 1 tends to underestimate the distances, thus it michael@0: * basically increases all the error that we were already seeing in the first order michael@0: * approx. So this version is not the one to use. Version 2 has the opposite effect michael@0: * and tends to overestimate the distances. This is much closer to what we are michael@0: * looking for. It is able to render ellipses (even thin ones) without the need to chop. michael@0: * However, it can not handle thin hyperbolas well and thus would still rely on michael@0: * chopping to tighten the clipping. Another side effect of the overestimating is michael@0: * that the curves become much thinner and "ropey". If all that was ever rendered michael@0: * were "not too thin" curves and ellipses then 2nd order may have an advantage since michael@0: * only one geometry would need to be rendered. However no benches were run comparing michael@0: * chopped first order and non chopped 2nd order. michael@0: */ michael@0: class GrGLConicEffect; michael@0: michael@0: class GrConicEffect : public GrVertexEffect { michael@0: public: michael@0: static GrEffectRef* Create(const GrEffectEdgeType edgeType, const GrDrawTargetCaps& caps) { michael@0: GR_CREATE_STATIC_EFFECT(gConicFillAA, GrConicEffect, (kFillAA_GrEffectEdgeType)); michael@0: GR_CREATE_STATIC_EFFECT(gConicHairAA, GrConicEffect, (kHairlineAA_GrEffectEdgeType)); michael@0: GR_CREATE_STATIC_EFFECT(gConicFillBW, GrConicEffect, (kFillBW_GrEffectEdgeType)); michael@0: switch (edgeType) { michael@0: case kFillAA_GrEffectEdgeType: michael@0: if (!caps.shaderDerivativeSupport()) { michael@0: return NULL; michael@0: } michael@0: gConicFillAA->ref(); michael@0: return gConicFillAA; michael@0: case kHairlineAA_GrEffectEdgeType: michael@0: if (!caps.shaderDerivativeSupport()) { michael@0: return NULL; michael@0: } michael@0: gConicHairAA->ref(); michael@0: return gConicHairAA; michael@0: case kFillBW_GrEffectEdgeType: michael@0: gConicFillBW->ref(); michael@0: return gConicFillBW; michael@0: default: michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: virtual ~GrConicEffect(); michael@0: michael@0: static const char* Name() { return "Conic"; } michael@0: michael@0: inline bool isAntiAliased() const { return GrEffectEdgeTypeIsAA(fEdgeType); } michael@0: inline bool isFilled() const { return GrEffectEdgeTypeIsFill(fEdgeType); } michael@0: inline GrEffectEdgeType getEdgeType() const { return fEdgeType; } michael@0: michael@0: typedef GrGLConicEffect GLEffect; michael@0: michael@0: virtual void getConstantColorComponents(GrColor* color, michael@0: uint32_t* validFlags) const SK_OVERRIDE { michael@0: *validFlags = 0; michael@0: } michael@0: michael@0: virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; michael@0: michael@0: private: michael@0: GrConicEffect(GrEffectEdgeType); michael@0: michael@0: virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE; michael@0: michael@0: GrEffectEdgeType fEdgeType; michael@0: michael@0: GR_DECLARE_EFFECT_TEST; michael@0: michael@0: typedef GrVertexEffect INHERITED; michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: /** michael@0: * The output of this effect is a hairline edge for quadratics. michael@0: * Quadratic specified by 0=u^2-v canonical coords. u and v are the first michael@0: * two components of the vertex attribute. At the three control points that define michael@0: * the Quadratic, u, v have the values {0,0}, {1/2, 0}, and {1, 1} respectively. michael@0: * Coverage for AA is min(0, 1-distance). 3rd & 4th cimponent unused. michael@0: * Requires shader derivative instruction support. michael@0: */ michael@0: class GrGLQuadEffect; michael@0: michael@0: class GrQuadEffect : public GrVertexEffect { michael@0: public: michael@0: static GrEffectRef* Create(const GrEffectEdgeType edgeType, const GrDrawTargetCaps& caps) { michael@0: GR_CREATE_STATIC_EFFECT(gQuadFillAA, GrQuadEffect, (kFillAA_GrEffectEdgeType)); michael@0: GR_CREATE_STATIC_EFFECT(gQuadHairAA, GrQuadEffect, (kHairlineAA_GrEffectEdgeType)); michael@0: GR_CREATE_STATIC_EFFECT(gQuadFillBW, GrQuadEffect, (kFillBW_GrEffectEdgeType)); michael@0: switch (edgeType) { michael@0: case kFillAA_GrEffectEdgeType: michael@0: if (!caps.shaderDerivativeSupport()) { michael@0: return NULL; michael@0: } michael@0: gQuadFillAA->ref(); michael@0: return gQuadFillAA; michael@0: case kHairlineAA_GrEffectEdgeType: michael@0: if (!caps.shaderDerivativeSupport()) { michael@0: return NULL; michael@0: } michael@0: gQuadHairAA->ref(); michael@0: return gQuadHairAA; michael@0: case kFillBW_GrEffectEdgeType: michael@0: gQuadFillBW->ref(); michael@0: return gQuadFillBW; michael@0: default: michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: virtual ~GrQuadEffect(); michael@0: michael@0: static const char* Name() { return "Quad"; } michael@0: michael@0: inline bool isAntiAliased() const { return GrEffectEdgeTypeIsAA(fEdgeType); } michael@0: inline bool isFilled() const { return GrEffectEdgeTypeIsFill(fEdgeType); } michael@0: inline GrEffectEdgeType getEdgeType() const { return fEdgeType; } michael@0: michael@0: typedef GrGLQuadEffect GLEffect; michael@0: michael@0: virtual void getConstantColorComponents(GrColor* color, michael@0: uint32_t* validFlags) const SK_OVERRIDE { michael@0: *validFlags = 0; michael@0: } michael@0: michael@0: virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; michael@0: michael@0: private: michael@0: GrQuadEffect(GrEffectEdgeType); michael@0: michael@0: virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE; michael@0: michael@0: GrEffectEdgeType fEdgeType; michael@0: michael@0: GR_DECLARE_EFFECT_TEST; michael@0: michael@0: typedef GrVertexEffect INHERITED; michael@0: }; michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: /** michael@0: * Shader is based off of "Resolution Independent Curve Rendering using michael@0: * Programmable Graphics Hardware" by Loop and Blinn. michael@0: * The output of this effect is a hairline edge for non rational cubics. michael@0: * Cubics are specified by implicit equation K^3 - LM. michael@0: * K, L, and M, are the first three values of the vertex attribute, michael@0: * the fourth value is not used. Distance is calculated using a michael@0: * first order approximation from the taylor series. michael@0: * Coverage for AA is max(0, 1-distance). michael@0: */ michael@0: class GrGLCubicEffect; michael@0: michael@0: class GrCubicEffect : public GrVertexEffect { michael@0: public: michael@0: static GrEffectRef* Create(const GrEffectEdgeType edgeType, const GrDrawTargetCaps& caps) { michael@0: GR_CREATE_STATIC_EFFECT(gCubicFillAA, GrCubicEffect, (kFillAA_GrEffectEdgeType)); michael@0: GR_CREATE_STATIC_EFFECT(gCubicHairAA, GrCubicEffect, (kHairlineAA_GrEffectEdgeType)); michael@0: GR_CREATE_STATIC_EFFECT(gCubicFillBW, GrCubicEffect, (kFillBW_GrEffectEdgeType)); michael@0: switch (edgeType) { michael@0: case kFillAA_GrEffectEdgeType: michael@0: if (!caps.shaderDerivativeSupport()) { michael@0: return NULL; michael@0: } michael@0: gCubicFillAA->ref(); michael@0: return gCubicFillAA; michael@0: case kHairlineAA_GrEffectEdgeType: michael@0: if (!caps.shaderDerivativeSupport()) { michael@0: return NULL; michael@0: } michael@0: gCubicHairAA->ref(); michael@0: return gCubicHairAA; michael@0: case kFillBW_GrEffectEdgeType: michael@0: gCubicFillBW->ref(); michael@0: return gCubicFillBW; michael@0: default: michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: virtual ~GrCubicEffect(); michael@0: michael@0: static const char* Name() { return "Cubic"; } michael@0: michael@0: inline bool isAntiAliased() const { return GrEffectEdgeTypeIsAA(fEdgeType); } michael@0: inline bool isFilled() const { return GrEffectEdgeTypeIsFill(fEdgeType); } michael@0: inline GrEffectEdgeType getEdgeType() const { return fEdgeType; } michael@0: michael@0: typedef GrGLCubicEffect GLEffect; michael@0: michael@0: virtual void getConstantColorComponents(GrColor* color, michael@0: uint32_t* validFlags) const SK_OVERRIDE { michael@0: *validFlags = 0; michael@0: } michael@0: michael@0: virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; michael@0: michael@0: private: michael@0: GrCubicEffect(GrEffectEdgeType); michael@0: michael@0: virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE; michael@0: michael@0: GrEffectEdgeType fEdgeType; michael@0: michael@0: GR_DECLARE_EFFECT_TEST; michael@0: michael@0: typedef GrVertexEffect INHERITED; michael@0: }; michael@0: michael@0: #endif