1 /* 2 * Copyright 2013 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef GrBezierEffect_DEFINED 9 #define GrBezierEffect_DEFINED 10 11 #include "include/private/GrTypesPriv.h" 12 #include "src/core/SkArenaAlloc.h" 13 #include "src/gpu/GrCaps.h" 14 #include "src/gpu/GrGeometryProcessor.h" 15 #include "src/gpu/GrProcessor.h" 16 17 /** 18 * Shader is based off of Loop-Blinn Quadratic GPU Rendering 19 * The output of this effect is a hairline edge for conics. 20 * Conics specified by implicit equation K^2 - LM. 21 * K, L, and M, are the first three values of the vertex attribute, 22 * the fourth value is not used. Distance is calculated using a 23 * first order approximation from the taylor series. 24 * Coverage for AA is max(0, 1-distance). 25 * 26 * Test were also run using a second order distance approximation. 27 * There were two versions of the second order approx. The first version 28 * is of roughly the form: 29 * f(q) = |f(p)| - ||f'(p)||*||q-p|| - ||f''(p)||*||q-p||^2. 30 * The second is similar: 31 * f(q) = |f(p)| + ||f'(p)||*||q-p|| + ||f''(p)||*||q-p||^2. 32 * The exact version of the equations can be found in the paper 33 * "Distance Approximations for Rasterizing Implicit Curves" by Gabriel Taubin 34 * 35 * In both versions we solve the quadratic for ||q-p||. 36 * Version 1: 37 * gFM is magnitude of first partials and gFM2 is magnitude of 2nd partials (as derived from paper) 38 * builder->fsCodeAppend("\t\tedgeAlpha = (sqrt(gFM*gFM+4.0*func*gF2M) - gFM)/(2.0*gF2M);\n"); 39 * Version 2: 40 * builder->fsCodeAppend("\t\tedgeAlpha = (gFM - sqrt(gFM*gFM-4.0*func*gF2M))/(2.0*gF2M);\n"); 41 * 42 * Also note that 2nd partials of k,l,m are zero 43 * 44 * When comparing the two second order approximations to the first order approximations, 45 * the following results were found. Version 1 tends to underestimate the distances, thus it 46 * basically increases all the error that we were already seeing in the first order 47 * approx. So this version is not the one to use. Version 2 has the opposite effect 48 * and tends to overestimate the distances. This is much closer to what we are 49 * looking for. It is able to render ellipses (even thin ones) without the need to chop. 50 * However, it can not handle thin hyperbolas well and thus would still rely on 51 * chopping to tighten the clipping. Another side effect of the overestimating is 52 * that the curves become much thinner and "ropey". If all that was ever rendered 53 * were "not too thin" curves and ellipses then 2nd order may have an advantage since 54 * only one geometry would need to be rendered. However no benches were run comparing 55 * chopped first order and non chopped 2nd order. 56 */ 57 class GrGLConicEffect; 58 59 class GrConicEffect : public GrGeometryProcessor { 60 public: 61 static GrGeometryProcessor* Make(SkArenaAlloc* arena, 62 const SkPMColor4f& color, 63 const SkMatrix& viewMatrix, 64 const GrCaps& caps, 65 const SkMatrix& localMatrix, 66 bool usesLocalCoords, 67 uint8_t coverage = 0xff) { 68 if (!caps.shaderCaps()->shaderDerivativeSupport()) { 69 return nullptr; 70 } 71 72 return arena->make([&](void* ptr) { 73 return new (ptr) GrConicEffect(color, viewMatrix, coverage, localMatrix, 74 usesLocalCoords); 75 }); 76 } 77 78 ~GrConicEffect() override; 79 name()80 const char* name() const override { return "Conic"; } 81 82 void addToKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override; 83 84 std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override; 85 86 private: 87 class Impl; 88 89 GrConicEffect(const SkPMColor4f&, const SkMatrix& viewMatrix, uint8_t coverage, 90 const SkMatrix& localMatrix, bool usesLocalCoords); 91 inPosition()92 inline const Attribute& inPosition() const { return kAttributes[0]; } inConicCoeffs()93 inline const Attribute& inConicCoeffs() const { return kAttributes[1]; } 94 95 SkPMColor4f fColor; 96 SkMatrix fViewMatrix; 97 SkMatrix fLocalMatrix; 98 bool fUsesLocalCoords; 99 uint8_t fCoverageScale; 100 inline static constexpr Attribute kAttributes[] = { 101 {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType}, 102 {"inConicCoeffs", kFloat4_GrVertexAttribType, kHalf4_GrSLType} 103 }; 104 105 GR_DECLARE_GEOMETRY_PROCESSOR_TEST 106 107 using INHERITED = GrGeometryProcessor; 108 }; 109 110 /////////////////////////////////////////////////////////////////////////////// 111 /** 112 * The output of this effect is a hairline edge for quadratics. 113 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first 114 * two components of the vertex attribute. At the three control points that define 115 * the Quadratic, u, v have the values {0,0}, {1/2, 0}, and {1, 1} respectively. 116 * Coverage for AA is min(0, 1-distance). 3rd & 4th cimponent unused. 117 * Requires shader derivative instruction support. 118 */ 119 class GrGLQuadEffect; 120 121 class GrQuadEffect : public GrGeometryProcessor { 122 public: 123 static GrGeometryProcessor* Make(SkArenaAlloc* arena, 124 const SkPMColor4f& color, 125 const SkMatrix& viewMatrix, 126 const GrCaps& caps, 127 const SkMatrix& localMatrix, 128 bool usesLocalCoords, 129 uint8_t coverage = 0xff) { 130 if (!caps.shaderCaps()->shaderDerivativeSupport()) { 131 return nullptr; 132 } 133 134 return arena->make([&](void* ptr) { 135 return new (ptr) GrQuadEffect(color, viewMatrix, coverage, localMatrix, 136 usesLocalCoords); 137 }); 138 } 139 140 ~GrQuadEffect() override; 141 name()142 const char* name() const override { return "Quad"; } 143 144 void addToKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override; 145 146 std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override; 147 148 private: 149 class Impl; 150 151 GrQuadEffect(const SkPMColor4f&, const SkMatrix& viewMatrix, uint8_t coverage, 152 const SkMatrix& localMatrix, bool usesLocalCoords); 153 inPosition()154 inline const Attribute& inPosition() const { return kAttributes[0]; } inHairQuadEdge()155 inline const Attribute& inHairQuadEdge() const { return kAttributes[1]; } 156 157 SkPMColor4f fColor; 158 SkMatrix fViewMatrix; 159 SkMatrix fLocalMatrix; 160 bool fUsesLocalCoords; 161 uint8_t fCoverageScale; 162 163 inline static constexpr Attribute kAttributes[] = { 164 {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType}, 165 {"inHairQuadEdge", kFloat4_GrVertexAttribType, kHalf4_GrSLType} 166 }; 167 168 GR_DECLARE_GEOMETRY_PROCESSOR_TEST 169 170 using INHERITED = GrGeometryProcessor; 171 }; 172 173 #endif 174