blob: ddb249b587ac86dd297a22b2e2d8d58f4fc6f848 [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
skia.committer@gmail.com44a77c82013-08-23 07:01:29 +00009#define GrBezierEffect_DEFINED
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000010
bsalomoneb1cb5c2015-05-22 08:01:09 -070011#include "GrCaps.h"
joshualittb0a8a372014-09-23 09:50:21 -070012#include "GrProcessor.h"
joshualitt249af152014-09-15 11:41:13 -070013#include "GrGeometryProcessor.h"
egdaniel605dd0f2014-11-12 08:35:25 -080014#include "GrInvariantOutput.h"
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000015#include "GrTypesPriv.h"
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000016
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 */
57class GrGLConicEffect;
58
joshualitt249af152014-09-15 11:41:13 -070059class GrConicEffect : public GrGeometryProcessor {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000060public:
joshualitt2e3b3e32014-12-09 13:31:14 -080061 static GrGeometryProcessor* Create(GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -080062 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -080063 const GrPrimitiveEdgeType edgeType,
bsalomon4b91f762015-05-19 09:29:46 -070064 const GrCaps& caps,
joshualittd27f73e2014-12-29 07:43:36 -080065 const SkMatrix& localMatrix,
joshualittb8c241a2015-05-19 08:23:30 -070066 bool usesLocalCoords,
joshualitt2e3b3e32014-12-09 13:31:14 -080067 uint8_t coverage = 0xff) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000068 switch (edgeType) {
joshualittb0a8a372014-09-23 09:50:21 -070069 case kFillAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -070070 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
halcanary96fcdcc2015-08-27 07:41:13 -070071 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000072 }
halcanary385fe4d2015-08-26 13:07:48 -070073 return new GrConicEffect(color, viewMatrix, coverage, kFillAA_GrProcessorEdgeType,
74 localMatrix, usesLocalCoords);
joshualittb0a8a372014-09-23 09:50:21 -070075 case kHairlineAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -070076 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
halcanary96fcdcc2015-08-27 07:41:13 -070077 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000078 }
halcanary385fe4d2015-08-26 13:07:48 -070079 return new GrConicEffect(color, viewMatrix, coverage,
80 kHairlineAA_GrProcessorEdgeType, localMatrix,
81 usesLocalCoords);
joshualittb0a8a372014-09-23 09:50:21 -070082 case kFillBW_GrProcessorEdgeType:
halcanary385fe4d2015-08-26 13:07:48 -070083 return new GrConicEffect(color, viewMatrix, coverage, kFillBW_GrProcessorEdgeType,
84 localMatrix, usesLocalCoords);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000085 default:
halcanary96fcdcc2015-08-27 07:41:13 -070086 return nullptr;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000087 }
88 }
89
90 virtual ~GrConicEffect();
91
mtklein36352bf2015-03-25 18:17:31 -070092 const char* name() const override { return "Conic"; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000093
joshualitt71c92602015-01-14 08:12:47 -080094 inline const Attribute* inPosition() const { return fInPosition; }
95 inline const Attribute* inConicCoeffs() const { return fInConicCoeffs; }
joshualittb0a8a372014-09-23 09:50:21 -070096 inline bool isAntiAliased() const { return GrProcessorEdgeTypeIsAA(fEdgeType); }
97 inline bool isFilled() const { return GrProcessorEdgeTypeIsFill(fEdgeType); }
98 inline GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
joshualitt88c23fc2015-05-13 14:18:07 -070099 GrColor color() const { return fColor; }
joshualittb8c241a2015-05-19 08:23:30 -0700100 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
joshualitte578a952015-05-14 10:09:13 -0700101 const SkMatrix& viewMatrix() const { return fViewMatrix; }
joshualitte3ababe2015-05-15 07:56:07 -0700102 const SkMatrix& localMatrix() const { return fLocalMatrix; }
joshualittb8c241a2015-05-19 08:23:30 -0700103 bool usesLocalCoords() const { return fUsesLocalCoords; }
104 uint8_t coverageScale() const { return fCoverageScale; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000105
egdaniel57d3b032015-11-13 11:57:27 -0800106 void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000107
egdaniel57d3b032015-11-13 11:57:27 -0800108 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000109
110private:
joshualitt8059eb92014-12-29 15:10:07 -0800111 GrConicEffect(GrColor, const SkMatrix& viewMatrix, uint8_t coverage, GrPrimitiveEdgeType,
joshualittb8c241a2015-05-19 08:23:30 -0700112 const SkMatrix& localMatrix, bool usesLocalCoords);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000113
joshualitt88c23fc2015-05-13 14:18:07 -0700114 GrColor fColor;
joshualitte578a952015-05-14 10:09:13 -0700115 SkMatrix fViewMatrix;
joshualitte3ababe2015-05-15 07:56:07 -0700116 SkMatrix fLocalMatrix;
joshualittb8c241a2015-05-19 08:23:30 -0700117 bool fUsesLocalCoords;
joshualitt88c23fc2015-05-13 14:18:07 -0700118 uint8_t fCoverageScale;
119 GrPrimitiveEdgeType fEdgeType;
joshualitt71c92602015-01-14 08:12:47 -0800120 const Attribute* fInPosition;
121 const Attribute* fInConicCoeffs;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000122
joshualittb0a8a372014-09-23 09:50:21 -0700123 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000124
joshualitt249af152014-09-15 11:41:13 -0700125 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000126};
127
128///////////////////////////////////////////////////////////////////////////////
129/**
130 * The output of this effect is a hairline edge for quadratics.
131 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first
132 * two components of the vertex attribute. At the three control points that define
133 * the Quadratic, u, v have the values {0,0}, {1/2, 0}, and {1, 1} respectively.
134 * Coverage for AA is min(0, 1-distance). 3rd & 4th cimponent unused.
135 * Requires shader derivative instruction support.
136 */
137class GrGLQuadEffect;
138
joshualitt249af152014-09-15 11:41:13 -0700139class GrQuadEffect : public GrGeometryProcessor {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000140public:
joshualitt2e3b3e32014-12-09 13:31:14 -0800141 static GrGeometryProcessor* Create(GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800142 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800143 const GrPrimitiveEdgeType edgeType,
bsalomon4b91f762015-05-19 09:29:46 -0700144 const GrCaps& caps,
joshualittd27f73e2014-12-29 07:43:36 -0800145 const SkMatrix& localMatrix,
joshualittb8c241a2015-05-19 08:23:30 -0700146 bool usesLocalCoords,
joshualitt2e3b3e32014-12-09 13:31:14 -0800147 uint8_t coverage = 0xff) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000148 switch (edgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700149 case kFillAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -0700150 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700151 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000152 }
halcanary385fe4d2015-08-26 13:07:48 -0700153 return new GrQuadEffect(color, viewMatrix, coverage, kFillAA_GrProcessorEdgeType,
154 localMatrix, usesLocalCoords);
joshualittb0a8a372014-09-23 09:50:21 -0700155 case kHairlineAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -0700156 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700157 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000158 }
halcanary385fe4d2015-08-26 13:07:48 -0700159 return new GrQuadEffect(color, viewMatrix, coverage,
160 kHairlineAA_GrProcessorEdgeType, localMatrix,
161 usesLocalCoords);
joshualittb0a8a372014-09-23 09:50:21 -0700162 case kFillBW_GrProcessorEdgeType:
halcanary385fe4d2015-08-26 13:07:48 -0700163 return new GrQuadEffect(color, viewMatrix, coverage, kFillBW_GrProcessorEdgeType,
164 localMatrix, usesLocalCoords);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000165 default:
halcanary96fcdcc2015-08-27 07:41:13 -0700166 return nullptr;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000167 }
168 }
169
170 virtual ~GrQuadEffect();
171
mtklein36352bf2015-03-25 18:17:31 -0700172 const char* name() const override { return "Quad"; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000173
joshualitt71c92602015-01-14 08:12:47 -0800174 inline const Attribute* inPosition() const { return fInPosition; }
175 inline const Attribute* inHairQuadEdge() const { return fInHairQuadEdge; }
joshualittb0a8a372014-09-23 09:50:21 -0700176 inline bool isAntiAliased() const { return GrProcessorEdgeTypeIsAA(fEdgeType); }
177 inline bool isFilled() const { return GrProcessorEdgeTypeIsFill(fEdgeType); }
178 inline GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
joshualitt88c23fc2015-05-13 14:18:07 -0700179 GrColor color() const { return fColor; }
joshualittb8c241a2015-05-19 08:23:30 -0700180 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
joshualitte578a952015-05-14 10:09:13 -0700181 const SkMatrix& viewMatrix() const { return fViewMatrix; }
joshualitte3ababe2015-05-15 07:56:07 -0700182 const SkMatrix& localMatrix() const { return fLocalMatrix; }
joshualittb8c241a2015-05-19 08:23:30 -0700183 bool usesLocalCoords() const { return fUsesLocalCoords; }
184 uint8_t coverageScale() const { return fCoverageScale; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000185
egdaniel57d3b032015-11-13 11:57:27 -0800186 void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000187
egdaniel57d3b032015-11-13 11:57:27 -0800188 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000189
190private:
joshualitt8059eb92014-12-29 15:10:07 -0800191 GrQuadEffect(GrColor, const SkMatrix& viewMatrix, uint8_t coverage, GrPrimitiveEdgeType,
joshualittb8c241a2015-05-19 08:23:30 -0700192 const SkMatrix& localMatrix, bool usesLocalCoords);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000193
joshualitt88c23fc2015-05-13 14:18:07 -0700194 GrColor fColor;
joshualitte578a952015-05-14 10:09:13 -0700195 SkMatrix fViewMatrix;
joshualitte3ababe2015-05-15 07:56:07 -0700196 SkMatrix fLocalMatrix;
joshualittb8c241a2015-05-19 08:23:30 -0700197 bool fUsesLocalCoords;
joshualitt88c23fc2015-05-13 14:18:07 -0700198 uint8_t fCoverageScale;
199 GrPrimitiveEdgeType fEdgeType;
joshualitt71c92602015-01-14 08:12:47 -0800200 const Attribute* fInPosition;
201 const Attribute* fInHairQuadEdge;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000202
joshualittb0a8a372014-09-23 09:50:21 -0700203 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000204
joshualitt249af152014-09-15 11:41:13 -0700205 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000206};
207
208//////////////////////////////////////////////////////////////////////////////
209/**
210 * Shader is based off of "Resolution Independent Curve Rendering using
211 * Programmable Graphics Hardware" by Loop and Blinn.
212 * The output of this effect is a hairline edge for non rational cubics.
213 * Cubics are specified by implicit equation K^3 - LM.
214 * K, L, and M, are the first three values of the vertex attribute,
215 * the fourth value is not used. Distance is calculated using a
216 * first order approximation from the taylor series.
217 * Coverage for AA is max(0, 1-distance).
218 */
219class GrGLCubicEffect;
220
joshualitt249af152014-09-15 11:41:13 -0700221class GrCubicEffect : public GrGeometryProcessor {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000222public:
joshualitt2e3b3e32014-12-09 13:31:14 -0800223 static GrGeometryProcessor* Create(GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800224 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800225 const GrPrimitiveEdgeType edgeType,
bsalomon4b91f762015-05-19 09:29:46 -0700226 const GrCaps& caps) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000227 switch (edgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700228 case kFillAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -0700229 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700230 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000231 }
halcanary385fe4d2015-08-26 13:07:48 -0700232 return new GrCubicEffect(color, viewMatrix, kFillAA_GrProcessorEdgeType);
joshualittb0a8a372014-09-23 09:50:21 -0700233 case kHairlineAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -0700234 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700235 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000236 }
halcanary385fe4d2015-08-26 13:07:48 -0700237 return new GrCubicEffect(color, viewMatrix, kHairlineAA_GrProcessorEdgeType);
joshualittb0a8a372014-09-23 09:50:21 -0700238 case kFillBW_GrProcessorEdgeType:
halcanary385fe4d2015-08-26 13:07:48 -0700239 return new GrCubicEffect(color, viewMatrix, kFillBW_GrProcessorEdgeType);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000240 default:
halcanary96fcdcc2015-08-27 07:41:13 -0700241 return nullptr;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000242 }
243 }
244
245 virtual ~GrCubicEffect();
246
mtklein36352bf2015-03-25 18:17:31 -0700247 const char* name() const override { return "Cubic"; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000248
joshualitt71c92602015-01-14 08:12:47 -0800249 inline const Attribute* inPosition() const { return fInPosition; }
250 inline const Attribute* inCubicCoeffs() const { return fInCubicCoeffs; }
joshualittb0a8a372014-09-23 09:50:21 -0700251 inline bool isAntiAliased() const { return GrProcessorEdgeTypeIsAA(fEdgeType); }
252 inline bool isFilled() const { return GrProcessorEdgeTypeIsFill(fEdgeType); }
253 inline GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
joshualitt88c23fc2015-05-13 14:18:07 -0700254 GrColor color() const { return fColor; }
joshualittb8c241a2015-05-19 08:23:30 -0700255 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
joshualitte578a952015-05-14 10:09:13 -0700256 const SkMatrix& viewMatrix() const { return fViewMatrix; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000257
egdaniel57d3b032015-11-13 11:57:27 -0800258 void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000259
egdaniel57d3b032015-11-13 11:57:27 -0800260 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000261
262private:
joshualitt8059eb92014-12-29 15:10:07 -0800263 GrCubicEffect(GrColor, const SkMatrix& viewMatrix, GrPrimitiveEdgeType);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000264
joshualitt88c23fc2015-05-13 14:18:07 -0700265 GrColor fColor;
joshualitte578a952015-05-14 10:09:13 -0700266 SkMatrix fViewMatrix;
joshualitt88c23fc2015-05-13 14:18:07 -0700267 GrPrimitiveEdgeType fEdgeType;
joshualitt71c92602015-01-14 08:12:47 -0800268 const Attribute* fInPosition;
269 const Attribute* fInCubicCoeffs;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000270
joshualittb0a8a372014-09-23 09:50:21 -0700271 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000272
joshualitt249af152014-09-15 11:41:13 -0700273 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000274};
275
276#endif