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<GrConicEffect>(color, viewMatrix, coverage, localMatrix, |
73 | usesLocalCoords); |
74 | } |
75 | |
76 | ~GrConicEffect() override; |
77 | |
78 | const char* name() const override { return "Conic" ; } |
79 | |
80 | inline const Attribute& inPosition() const { return kAttributes[0]; } |
81 | inline const Attribute& inConicCoeffs() const { return kAttributes[1]; } |
82 | inline bool isAntiAliased() const { return true; } |
83 | inline bool isFilled() const { return false; } |
84 | const SkPMColor4f& color() const { return fColor; } |
85 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
86 | const SkMatrix& localMatrix() const { return fLocalMatrix; } |
87 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
88 | uint8_t coverageScale() const { return fCoverageScale; } |
89 | |
90 | void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override; |
91 | |
92 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override; |
93 | |
94 | private: |
95 | friend class ::SkArenaAlloc; // for access to ctor |
96 | |
97 | GrConicEffect(const SkPMColor4f&, const SkMatrix& viewMatrix, uint8_t coverage, |
98 | const SkMatrix& localMatrix, bool usesLocalCoords); |
99 | |
100 | SkPMColor4f fColor; |
101 | SkMatrix fViewMatrix; |
102 | SkMatrix fLocalMatrix; |
103 | bool fUsesLocalCoords; |
104 | uint8_t fCoverageScale; |
105 | static constexpr Attribute kAttributes[] = { |
106 | {"inPosition" , kFloat2_GrVertexAttribType, kFloat2_GrSLType}, |
107 | {"inConicCoeffs" , kFloat4_GrVertexAttribType, kHalf4_GrSLType} |
108 | }; |
109 | |
110 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
111 | |
112 | typedef GrGeometryProcessor INHERITED; |
113 | }; |
114 | |
115 | /////////////////////////////////////////////////////////////////////////////// |
116 | /** |
117 | * The output of this effect is a hairline edge for quadratics. |
118 | * Quadratic specified by 0=u^2-v canonical coords. u and v are the first |
119 | * two components of the vertex attribute. At the three control points that define |
120 | * the Quadratic, u, v have the values {0,0}, {1/2, 0}, and {1, 1} respectively. |
121 | * Coverage for AA is min(0, 1-distance). 3rd & 4th cimponent unused. |
122 | * Requires shader derivative instruction support. |
123 | */ |
124 | class GrGLQuadEffect; |
125 | |
126 | class GrQuadEffect : public GrGeometryProcessor { |
127 | public: |
128 | static GrGeometryProcessor* Make(SkArenaAlloc* arena, |
129 | const SkPMColor4f& color, |
130 | const SkMatrix& viewMatrix, |
131 | const GrCaps& caps, |
132 | const SkMatrix& localMatrix, |
133 | bool usesLocalCoords, |
134 | uint8_t coverage = 0xff) { |
135 | if (!caps.shaderCaps()->shaderDerivativeSupport()) { |
136 | return nullptr; |
137 | } |
138 | |
139 | return arena->make<GrQuadEffect>(color, viewMatrix, coverage, localMatrix, usesLocalCoords); |
140 | } |
141 | |
142 | ~GrQuadEffect() override; |
143 | |
144 | const char* name() const override { return "Quad" ; } |
145 | |
146 | inline const Attribute& inPosition() const { return kAttributes[0]; } |
147 | inline const Attribute& inHairQuadEdge() const { return kAttributes[1]; } |
148 | inline bool isAntiAliased() const { return true; } |
149 | inline bool isFilled() const { return false; } |
150 | const SkPMColor4f& color() const { return fColor; } |
151 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
152 | const SkMatrix& localMatrix() const { return fLocalMatrix; } |
153 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
154 | uint8_t coverageScale() const { return fCoverageScale; } |
155 | |
156 | void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override; |
157 | |
158 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override; |
159 | |
160 | private: |
161 | friend class ::SkArenaAlloc; // for access to ctor |
162 | |
163 | GrQuadEffect(const SkPMColor4f&, const SkMatrix& viewMatrix, uint8_t coverage, |
164 | const SkMatrix& localMatrix, bool usesLocalCoords); |
165 | |
166 | SkPMColor4f fColor; |
167 | SkMatrix fViewMatrix; |
168 | SkMatrix fLocalMatrix; |
169 | bool fUsesLocalCoords; |
170 | uint8_t fCoverageScale; |
171 | |
172 | static constexpr Attribute kAttributes[] = { |
173 | {"inPosition" , kFloat2_GrVertexAttribType, kFloat2_GrSLType}, |
174 | {"inHairQuadEdge" , kFloat4_GrVertexAttribType, kHalf4_GrSLType} |
175 | }; |
176 | |
177 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
178 | |
179 | typedef GrGeometryProcessor INHERITED; |
180 | }; |
181 | |
182 | #endif |
183 | |