blob: 72f12517f877a37e450a59d40036280bbff633e6 [file] [log] [blame]
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +00001/*
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 "GrEffect.h"
12#include "GrDrawTargetCaps.h"
13
14enum GrBezierEdgeType {
15 kFillAA_GrBezierEdgeType,
16 kHairAA_GrBezierEdgeType,
17 kFillNoAA_GrBezierEdgeType,
18};
19
20static inline bool GrBezierEdgeTypeIsFill(const GrBezierEdgeType edgeType) {
21 return (kHairAA_GrBezierEdgeType != edgeType);
22}
23
24static inline bool GrBezierEdgeTypeIsAA(const GrBezierEdgeType edgeType) {
25 return (kFillNoAA_GrBezierEdgeType != edgeType);
26}
27
28/**
29 * Shader is based off of Loop-Blinn Quadratic GPU Rendering
30 * The output of this effect is a hairline edge for conics.
31 * Conics specified by implicit equation K^2 - LM.
32 * K, L, and M, are the first three values of the vertex attribute,
33 * the fourth value is not used. Distance is calculated using a
34 * first order approximation from the taylor series.
35 * Coverage for AA is max(0, 1-distance).
36 *
37 * Test were also run using a second order distance approximation.
38 * There were two versions of the second order approx. The first version
39 * is of roughly the form:
40 * f(q) = |f(p)| - ||f'(p)||*||q-p|| - ||f''(p)||*||q-p||^2.
41 * The second is similar:
42 * f(q) = |f(p)| + ||f'(p)||*||q-p|| + ||f''(p)||*||q-p||^2.
43 * The exact version of the equations can be found in the paper
44 * "Distance Approximations for Rasterizing Implicit Curves" by Gabriel Taubin
45 *
46 * In both versions we solve the quadratic for ||q-p||.
47 * Version 1:
48 * gFM is magnitude of first partials and gFM2 is magnitude of 2nd partials (as derived from paper)
49 * builder->fsCodeAppend("\t\tedgeAlpha = (sqrt(gFM*gFM+4.0*func*gF2M) - gFM)/(2.0*gF2M);\n");
50 * Version 2:
51 * builder->fsCodeAppend("\t\tedgeAlpha = (gFM - sqrt(gFM*gFM-4.0*func*gF2M))/(2.0*gF2M);\n");
52 *
53 * Also note that 2nd partials of k,l,m are zero
54 *
55 * When comparing the two second order approximations to the first order approximations,
56 * the following results were found. Version 1 tends to underestimate the distances, thus it
57 * basically increases all the error that we were already seeing in the first order
58 * approx. So this version is not the one to use. Version 2 has the opposite effect
59 * and tends to overestimate the distances. This is much closer to what we are
60 * looking for. It is able to render ellipses (even thin ones) without the need to chop.
61 * However, it can not handle thin hyperbolas well and thus would still rely on
62 * chopping to tighten the clipping. Another side effect of the overestimating is
63 * that the curves become much thinner and "ropey". If all that was ever rendered
64 * were "not too thin" curves and ellipses then 2nd order may have an advantage since
65 * only one geometry would need to be rendered. However no benches were run comparing
66 * chopped first order and non chopped 2nd order.
67 */
68class GrGLConicEffect;
69
70class GrConicEffect : public GrEffect {
71public:
72 static GrEffectRef* Create(const GrBezierEdgeType edgeType, const GrDrawTargetCaps& caps) {
73 GR_CREATE_STATIC_EFFECT(gConicFillAA, GrConicEffect, (edgeType));
74 GR_CREATE_STATIC_EFFECT(gConicHairAA, GrConicEffect, (edgeType));
75 GR_CREATE_STATIC_EFFECT(gConicFillNoAA, GrConicEffect, (edgeType));
76 if (kFillAA_GrBezierEdgeType == edgeType) {
77 if (!caps.shaderDerivativeSupport()) {
78 return NULL;
79 }
80 gConicFillAA->ref();
81 return gConicFillAA;
82 } else if (kHairAA_GrBezierEdgeType == edgeType) {
83 if (!caps.shaderDerivativeSupport()) {
84 return NULL;
85 }
86 gConicHairAA->ref();
87 return gConicHairAA;
88 } else {
89 gConicFillNoAA->ref();
90 return gConicFillNoAA;
91 }
92 }
93
94 virtual ~GrConicEffect();
95
96 static const char* Name() { return "Conic"; }
97
98 inline bool isAntiAliased() const { return GrBezierEdgeTypeIsAA(fEdgeType); }
99 inline bool isFilled() const { return GrBezierEdgeTypeIsFill(fEdgeType); }
100 inline GrBezierEdgeType getEdgeType() const { return fEdgeType; }
101
102 typedef GrGLConicEffect GLEffect;
103
104 virtual void getConstantColorComponents(GrColor* color,
105 uint32_t* validFlags) const SK_OVERRIDE {
106 *validFlags = 0;
107 }
108
109 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
110
111private:
112 GrConicEffect(GrBezierEdgeType);
113
114 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE;
115
116 GrBezierEdgeType fEdgeType;
117
118 GR_DECLARE_EFFECT_TEST;
119
120 typedef GrEffect INHERITED;
121};
122
123///////////////////////////////////////////////////////////////////////////////
124/**
125 * The output of this effect is a hairline edge for quadratics.
126 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first
127 * two components of the vertex attribute. At the three control points that define
128 * the Quadratic, u, v have the values {0,0}, {1/2, 0}, and {1, 1} respectively.
129 * Coverage for AA is min(0, 1-distance). 3rd & 4th cimponent unused.
130 * Requires shader derivative instruction support.
131 */
132class GrGLQuadEffect;
133
134class GrQuadEffect : public GrEffect {
135public:
136 static GrEffectRef* Create(const GrBezierEdgeType edgeType, const GrDrawTargetCaps& caps) {
137 GR_CREATE_STATIC_EFFECT(gQuadFillAA, GrQuadEffect, (kFillAA_GrBezierEdgeType));
138 GR_CREATE_STATIC_EFFECT(gQuadHairAA, GrQuadEffect, (kHairAA_GrBezierEdgeType));
139 GR_CREATE_STATIC_EFFECT(gQuadFillNoAA, GrQuadEffect, (kFillNoAA_GrBezierEdgeType));
140 if (kFillAA_GrBezierEdgeType == edgeType) {
141 if (!caps.shaderDerivativeSupport()) {
142 return NULL;
143 }
144 gQuadFillAA->ref();
145 return gQuadFillAA;
146 } else if (kHairAA_GrBezierEdgeType == edgeType) {
147 if (!caps.shaderDerivativeSupport()) {
148 return NULL;
149 }
150 gQuadHairAA->ref();
151 return gQuadHairAA;
152 } else {
153 gQuadFillNoAA->ref();
154 return gQuadFillNoAA;
155 }
156 }
157
158 virtual ~GrQuadEffect();
159
160 static const char* Name() { return "Quad"; }
161
162 inline bool isAntiAliased() const { return GrBezierEdgeTypeIsAA(fEdgeType); }
163 inline bool isFilled() const { return GrBezierEdgeTypeIsFill(fEdgeType); }
164 inline GrBezierEdgeType getEdgeType() const { return fEdgeType; }
165
166 typedef GrGLQuadEffect GLEffect;
167
168 virtual void getConstantColorComponents(GrColor* color,
169 uint32_t* validFlags) const SK_OVERRIDE {
170 *validFlags = 0;
171 }
172
173 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
174
175private:
176 GrQuadEffect(GrBezierEdgeType);
177
178 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE;
179
180 GrBezierEdgeType fEdgeType;
181
182 GR_DECLARE_EFFECT_TEST;
183
184 typedef GrEffect INHERITED;
185};
186
187//////////////////////////////////////////////////////////////////////////////
188/**
189 * Shader is based off of "Resolution Independent Curve Rendering using
190 * Programmable Graphics Hardware" by Loop and Blinn.
191 * The output of this effect is a hairline edge for non rational cubics.
192 * Cubics are specified by implicit equation K^3 - LM.
193 * K, L, and M, are the first three values of the vertex attribute,
194 * the fourth value is not used. Distance is calculated using a
195 * first order approximation from the taylor series.
196 * Coverage for AA is max(0, 1-distance).
197 */
198class GrGLCubicEffect;
199
200class GrCubicEffect : public GrEffect {
201public:
202 static GrEffectRef* Create(const GrBezierEdgeType edgeType, const GrDrawTargetCaps& caps) {
203 GR_CREATE_STATIC_EFFECT(gCubicFillAA, GrCubicEffect, (kFillAA_GrBezierEdgeType));
204 GR_CREATE_STATIC_EFFECT(gCubicHairAA, GrCubicEffect, (kHairAA_GrBezierEdgeType));
205 GR_CREATE_STATIC_EFFECT(gCubicFillNoAA, GrCubicEffect, (kFillNoAA_GrBezierEdgeType));
206 if (kFillAA_GrBezierEdgeType == edgeType) {
207 if (!caps.shaderDerivativeSupport()) {
208 return NULL;
209 }
210 gCubicFillAA->ref();
211 return gCubicFillAA;
212 } else if (kHairAA_GrBezierEdgeType == edgeType) {
213 if (!caps.shaderDerivativeSupport()) {
214 return NULL;
215 }
216 gCubicHairAA->ref();
217 return gCubicHairAA;
218 } else {
219 gCubicFillNoAA->ref();
220 return gCubicFillNoAA;
221 }
222 }
223
224 virtual ~GrCubicEffect();
225
226 static const char* Name() { return "Cubic"; }
227
228 inline bool isAntiAliased() const { return GrBezierEdgeTypeIsAA(fEdgeType); }
229 inline bool isFilled() const { return GrBezierEdgeTypeIsFill(fEdgeType); }
230 inline GrBezierEdgeType getEdgeType() const { return fEdgeType; }
231
232 typedef GrGLCubicEffect GLEffect;
233
234 virtual void getConstantColorComponents(GrColor* color,
235 uint32_t* validFlags) const SK_OVERRIDE {
236 *validFlags = 0;
237 }
238
239 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
240
241private:
242 GrCubicEffect(GrBezierEdgeType);
243
244 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE;
245
246 GrBezierEdgeType fEdgeType;
247
248 GR_DECLARE_EFFECT_TEST;
249
250 typedef GrEffect INHERITED;
251};
252
253#endif