blob: f1b22fa8a4c9c1e5ddd396e882d10fb3deddc764 [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
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000011#include "GrDrawTargetCaps.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,
64 const GrDrawTargetCaps& caps,
joshualittd27f73e2014-12-29 07:43:36 -080065 const SkMatrix& localMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -080066 uint8_t coverage = 0xff) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000067 switch (edgeType) {
joshualittb0a8a372014-09-23 09:50:21 -070068 case kFillAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -070069 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000070 return NULL;
71 }
joshualitt8059eb92014-12-29 15:10:07 -080072 return SkNEW_ARGS(GrConicEffect, (color, viewMatrix, coverage,
73 kFillAA_GrProcessorEdgeType,
joshualittd27f73e2014-12-29 07:43:36 -080074 localMatrix));
joshualittb0a8a372014-09-23 09:50:21 -070075 case kHairlineAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -070076 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000077 return NULL;
78 }
joshualitt8059eb92014-12-29 15:10:07 -080079 return SkNEW_ARGS(GrConicEffect, (color, viewMatrix, coverage,
joshualittd27f73e2014-12-29 07:43:36 -080080 kHairlineAA_GrProcessorEdgeType,
81 localMatrix));
joshualittb0a8a372014-09-23 09:50:21 -070082 case kFillBW_GrProcessorEdgeType:
joshualitt8059eb92014-12-29 15:10:07 -080083 return SkNEW_ARGS(GrConicEffect, (color, viewMatrix, coverage,
84 kFillBW_GrProcessorEdgeType,
joshualittd27f73e2014-12-29 07:43:36 -080085 localMatrix));
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000086 default:
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000087 return NULL;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000088 }
89 }
90
91 virtual ~GrConicEffect();
92
mtklein36352bf2015-03-25 18:17:31 -070093 const char* name() const override { return "Conic"; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000094
joshualitt71c92602015-01-14 08:12:47 -080095 inline const Attribute* inPosition() const { return fInPosition; }
96 inline const Attribute* inConicCoeffs() const { return fInConicCoeffs; }
joshualittb0a8a372014-09-23 09:50:21 -070097 inline bool isAntiAliased() const { return GrProcessorEdgeTypeIsAA(fEdgeType); }
98 inline bool isFilled() const { return GrProcessorEdgeTypeIsFill(fEdgeType); }
99 inline GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
joshualitt88c23fc2015-05-13 14:18:07 -0700100 GrColor color() const { return 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; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000103
joshualitteb2a6762014-12-04 11:35:33 -0800104 virtual void getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700105 const GrGLSLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -0700106 GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000107
joshualittabb52a12015-01-13 15:02:10 -0800108 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700109 const GrGLSLCaps&) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000110
mtklein36352bf2015-03-25 18:17:31 -0700111 void initBatchTracker(GrBatchTracker*, const GrPipelineInfo&) const override;
joshualitt9b989322014-12-15 14:16:27 -0800112
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000113private:
joshualitt8059eb92014-12-29 15:10:07 -0800114 GrConicEffect(GrColor, const SkMatrix& viewMatrix, uint8_t coverage, GrPrimitiveEdgeType,
115 const SkMatrix& localMatrix);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000116
joshualitt88c23fc2015-05-13 14:18:07 -0700117 GrColor fColor;
joshualitte578a952015-05-14 10:09:13 -0700118 SkMatrix fViewMatrix;
joshualitte3ababe2015-05-15 07:56:07 -0700119 SkMatrix fLocalMatrix;
joshualitt88c23fc2015-05-13 14:18:07 -0700120 uint8_t fCoverageScale;
121 GrPrimitiveEdgeType fEdgeType;
joshualitt71c92602015-01-14 08:12:47 -0800122 const Attribute* fInPosition;
123 const Attribute* fInConicCoeffs;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000124
joshualittb0a8a372014-09-23 09:50:21 -0700125 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000126
joshualitt249af152014-09-15 11:41:13 -0700127 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000128};
129
130///////////////////////////////////////////////////////////////////////////////
131/**
132 * The output of this effect is a hairline edge for quadratics.
133 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first
134 * two components of the vertex attribute. At the three control points that define
135 * the Quadratic, u, v have the values {0,0}, {1/2, 0}, and {1, 1} respectively.
136 * Coverage for AA is min(0, 1-distance). 3rd & 4th cimponent unused.
137 * Requires shader derivative instruction support.
138 */
139class GrGLQuadEffect;
140
joshualitt249af152014-09-15 11:41:13 -0700141class GrQuadEffect : public GrGeometryProcessor {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000142public:
joshualitt2e3b3e32014-12-09 13:31:14 -0800143 static GrGeometryProcessor* Create(GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800144 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800145 const GrPrimitiveEdgeType edgeType,
146 const GrDrawTargetCaps& caps,
joshualittd27f73e2014-12-29 07:43:36 -0800147 const SkMatrix& localMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800148 uint8_t coverage = 0xff) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000149 switch (edgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700150 case kFillAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -0700151 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000152 return NULL;
153 }
joshualitt8059eb92014-12-29 15:10:07 -0800154 return SkNEW_ARGS(GrQuadEffect, (color, viewMatrix, coverage,
155 kFillAA_GrProcessorEdgeType,
joshualittd27f73e2014-12-29 07:43:36 -0800156 localMatrix));
joshualittb0a8a372014-09-23 09:50:21 -0700157 case kHairlineAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -0700158 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000159 return NULL;
160 }
joshualitt8059eb92014-12-29 15:10:07 -0800161 return SkNEW_ARGS(GrQuadEffect, (color, viewMatrix, coverage,
162 kHairlineAA_GrProcessorEdgeType,
joshualittd27f73e2014-12-29 07:43:36 -0800163 localMatrix));
joshualittb0a8a372014-09-23 09:50:21 -0700164 case kFillBW_GrProcessorEdgeType:
joshualitt8059eb92014-12-29 15:10:07 -0800165 return SkNEW_ARGS(GrQuadEffect, (color, viewMatrix, coverage,
166 kFillBW_GrProcessorEdgeType,
joshualittd27f73e2014-12-29 07:43:36 -0800167 localMatrix));
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000168 default:
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000169 return NULL;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000170 }
171 }
172
173 virtual ~GrQuadEffect();
174
mtklein36352bf2015-03-25 18:17:31 -0700175 const char* name() const override { return "Quad"; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000176
joshualitt71c92602015-01-14 08:12:47 -0800177 inline const Attribute* inPosition() const { return fInPosition; }
178 inline const Attribute* inHairQuadEdge() const { return fInHairQuadEdge; }
joshualittb0a8a372014-09-23 09:50:21 -0700179 inline bool isAntiAliased() const { return GrProcessorEdgeTypeIsAA(fEdgeType); }
180 inline bool isFilled() const { return GrProcessorEdgeTypeIsFill(fEdgeType); }
181 inline GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
joshualitt88c23fc2015-05-13 14:18:07 -0700182 GrColor color() const { return fColor; }
joshualitte578a952015-05-14 10:09:13 -0700183 const SkMatrix& viewMatrix() const { return fViewMatrix; }
joshualitte3ababe2015-05-15 07:56:07 -0700184 const SkMatrix& localMatrix() const { return fLocalMatrix; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000185
joshualitteb2a6762014-12-04 11:35:33 -0800186 virtual void getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700187 const GrGLSLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -0700188 GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000189
joshualittabb52a12015-01-13 15:02:10 -0800190 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700191 const GrGLSLCaps&) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000192
mtklein36352bf2015-03-25 18:17:31 -0700193 void initBatchTracker(GrBatchTracker*, const GrPipelineInfo&) const override;
joshualitt9b989322014-12-15 14:16:27 -0800194
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000195private:
joshualitt8059eb92014-12-29 15:10:07 -0800196 GrQuadEffect(GrColor, const SkMatrix& viewMatrix, uint8_t coverage, GrPrimitiveEdgeType,
197 const SkMatrix& localMatrix);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000198
joshualitt88c23fc2015-05-13 14:18:07 -0700199 GrColor fColor;
joshualitte578a952015-05-14 10:09:13 -0700200 SkMatrix fViewMatrix;
joshualitte3ababe2015-05-15 07:56:07 -0700201 SkMatrix fLocalMatrix;
joshualitt88c23fc2015-05-13 14:18:07 -0700202 uint8_t fCoverageScale;
203 GrPrimitiveEdgeType fEdgeType;
joshualitt71c92602015-01-14 08:12:47 -0800204 const Attribute* fInPosition;
205 const Attribute* fInHairQuadEdge;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000206
joshualittb0a8a372014-09-23 09:50:21 -0700207 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000208
joshualitt249af152014-09-15 11:41:13 -0700209 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000210};
211
212//////////////////////////////////////////////////////////////////////////////
213/**
214 * Shader is based off of "Resolution Independent Curve Rendering using
215 * Programmable Graphics Hardware" by Loop and Blinn.
216 * The output of this effect is a hairline edge for non rational cubics.
217 * Cubics are specified by implicit equation K^3 - LM.
218 * K, L, and M, are the first three values of the vertex attribute,
219 * the fourth value is not used. Distance is calculated using a
220 * first order approximation from the taylor series.
221 * Coverage for AA is max(0, 1-distance).
222 */
223class GrGLCubicEffect;
224
joshualitt249af152014-09-15 11:41:13 -0700225class GrCubicEffect : public GrGeometryProcessor {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000226public:
joshualitt2e3b3e32014-12-09 13:31:14 -0800227 static GrGeometryProcessor* Create(GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800228 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800229 const GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700230 const GrDrawTargetCaps& caps) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000231 switch (edgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700232 case kFillAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -0700233 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000234 return NULL;
235 }
joshualitt8059eb92014-12-29 15:10:07 -0800236 return SkNEW_ARGS(GrCubicEffect, (color, viewMatrix, kFillAA_GrProcessorEdgeType));
joshualittb0a8a372014-09-23 09:50:21 -0700237 case kHairlineAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -0700238 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000239 return NULL;
240 }
joshualitt8059eb92014-12-29 15:10:07 -0800241 return SkNEW_ARGS(GrCubicEffect, (color, viewMatrix,
242 kHairlineAA_GrProcessorEdgeType));
joshualittb0a8a372014-09-23 09:50:21 -0700243 case kFillBW_GrProcessorEdgeType:
joshualitt8059eb92014-12-29 15:10:07 -0800244 return SkNEW_ARGS(GrCubicEffect, (color, viewMatrix,
245 kFillBW_GrProcessorEdgeType));
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000246 default:
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000247 return NULL;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000248 }
249 }
250
251 virtual ~GrCubicEffect();
252
mtklein36352bf2015-03-25 18:17:31 -0700253 const char* name() const override { return "Cubic"; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000254
joshualitt71c92602015-01-14 08:12:47 -0800255 inline const Attribute* inPosition() const { return fInPosition; }
256 inline const Attribute* inCubicCoeffs() const { return fInCubicCoeffs; }
joshualittb0a8a372014-09-23 09:50:21 -0700257 inline bool isAntiAliased() const { return GrProcessorEdgeTypeIsAA(fEdgeType); }
258 inline bool isFilled() const { return GrProcessorEdgeTypeIsFill(fEdgeType); }
259 inline GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
joshualitt88c23fc2015-05-13 14:18:07 -0700260 GrColor color() const { return fColor; }
joshualitte578a952015-05-14 10:09:13 -0700261 const SkMatrix& viewMatrix() const { return fViewMatrix; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000262
joshualitteb2a6762014-12-04 11:35:33 -0800263 virtual void getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700264 const GrGLSLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -0700265 GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000266
joshualittabb52a12015-01-13 15:02:10 -0800267 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700268 const GrGLSLCaps&) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000269
mtklein36352bf2015-03-25 18:17:31 -0700270 void initBatchTracker(GrBatchTracker*, const GrPipelineInfo&) const override;
joshualitt9b989322014-12-15 14:16:27 -0800271
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000272private:
joshualitt8059eb92014-12-29 15:10:07 -0800273 GrCubicEffect(GrColor, const SkMatrix& viewMatrix, GrPrimitiveEdgeType);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000274
joshualitt88c23fc2015-05-13 14:18:07 -0700275 GrColor fColor;
joshualitte578a952015-05-14 10:09:13 -0700276 SkMatrix fViewMatrix;
joshualitt88c23fc2015-05-13 14:18:07 -0700277 GrPrimitiveEdgeType fEdgeType;
joshualitt71c92602015-01-14 08:12:47 -0800278 const Attribute* fInPosition;
279 const Attribute* fInCubicCoeffs;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000280
joshualittb0a8a372014-09-23 09:50:21 -0700281 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000282
joshualitt249af152014-09-15 11:41:13 -0700283 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000284};
285
286#endif