blob: 7700c58fe6937c0e711c674eccc937bd7241bc18 [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,
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()) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000071 return NULL;
72 }
joshualitt8059eb92014-12-29 15:10:07 -080073 return SkNEW_ARGS(GrConicEffect, (color, viewMatrix, coverage,
74 kFillAA_GrProcessorEdgeType,
joshualittb8c241a2015-05-19 08:23:30 -070075 localMatrix, usesLocalCoords));
joshualittb0a8a372014-09-23 09:50:21 -070076 case kHairlineAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -070077 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000078 return NULL;
79 }
joshualitt8059eb92014-12-29 15:10:07 -080080 return SkNEW_ARGS(GrConicEffect, (color, viewMatrix, coverage,
joshualittd27f73e2014-12-29 07:43:36 -080081 kHairlineAA_GrProcessorEdgeType,
joshualittb8c241a2015-05-19 08:23:30 -070082 localMatrix, usesLocalCoords));
joshualittb0a8a372014-09-23 09:50:21 -070083 case kFillBW_GrProcessorEdgeType:
joshualitt8059eb92014-12-29 15:10:07 -080084 return SkNEW_ARGS(GrConicEffect, (color, viewMatrix, coverage,
85 kFillBW_GrProcessorEdgeType,
joshualittb8c241a2015-05-19 08:23:30 -070086 localMatrix, usesLocalCoords));
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000087 default:
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000088 return NULL;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000089 }
90 }
91
92 virtual ~GrConicEffect();
93
mtklein36352bf2015-03-25 18:17:31 -070094 const char* name() const override { return "Conic"; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000095
joshualitt71c92602015-01-14 08:12:47 -080096 inline const Attribute* inPosition() const { return fInPosition; }
97 inline const Attribute* inConicCoeffs() const { return fInConicCoeffs; }
joshualittb0a8a372014-09-23 09:50:21 -070098 inline bool isAntiAliased() const { return GrProcessorEdgeTypeIsAA(fEdgeType); }
99 inline bool isFilled() const { return GrProcessorEdgeTypeIsFill(fEdgeType); }
100 inline GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
joshualitt88c23fc2015-05-13 14:18:07 -0700101 GrColor color() const { return fColor; }
joshualittb8c241a2015-05-19 08:23:30 -0700102 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
joshualitte578a952015-05-14 10:09:13 -0700103 const SkMatrix& viewMatrix() const { return fViewMatrix; }
joshualitte3ababe2015-05-15 07:56:07 -0700104 const SkMatrix& localMatrix() const { return fLocalMatrix; }
joshualittb8c241a2015-05-19 08:23:30 -0700105 bool usesLocalCoords() const { return fUsesLocalCoords; }
106 uint8_t coverageScale() const { return fCoverageScale; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000107
joshualitteb2a6762014-12-04 11:35:33 -0800108 virtual void getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700109 const GrGLSLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -0700110 GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000111
joshualittabb52a12015-01-13 15:02:10 -0800112 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700113 const GrGLSLCaps&) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000114
115private:
joshualitt8059eb92014-12-29 15:10:07 -0800116 GrConicEffect(GrColor, const SkMatrix& viewMatrix, uint8_t coverage, GrPrimitiveEdgeType,
joshualittb8c241a2015-05-19 08:23:30 -0700117 const SkMatrix& localMatrix, bool usesLocalCoords);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000118
joshualitt88c23fc2015-05-13 14:18:07 -0700119 GrColor fColor;
joshualitte578a952015-05-14 10:09:13 -0700120 SkMatrix fViewMatrix;
joshualitte3ababe2015-05-15 07:56:07 -0700121 SkMatrix fLocalMatrix;
joshualittb8c241a2015-05-19 08:23:30 -0700122 bool fUsesLocalCoords;
joshualitt88c23fc2015-05-13 14:18:07 -0700123 uint8_t fCoverageScale;
124 GrPrimitiveEdgeType fEdgeType;
joshualitt71c92602015-01-14 08:12:47 -0800125 const Attribute* fInPosition;
126 const Attribute* fInConicCoeffs;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000127
joshualittb0a8a372014-09-23 09:50:21 -0700128 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000129
joshualitt249af152014-09-15 11:41:13 -0700130 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000131};
132
133///////////////////////////////////////////////////////////////////////////////
134/**
135 * The output of this effect is a hairline edge for quadratics.
136 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first
137 * two components of the vertex attribute. At the three control points that define
138 * the Quadratic, u, v have the values {0,0}, {1/2, 0}, and {1, 1} respectively.
139 * Coverage for AA is min(0, 1-distance). 3rd & 4th cimponent unused.
140 * Requires shader derivative instruction support.
141 */
142class GrGLQuadEffect;
143
joshualitt249af152014-09-15 11:41:13 -0700144class GrQuadEffect : public GrGeometryProcessor {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000145public:
joshualitt2e3b3e32014-12-09 13:31:14 -0800146 static GrGeometryProcessor* Create(GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800147 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800148 const GrPrimitiveEdgeType edgeType,
149 const GrDrawTargetCaps& caps,
joshualittd27f73e2014-12-29 07:43:36 -0800150 const SkMatrix& localMatrix,
joshualittb8c241a2015-05-19 08:23:30 -0700151 bool usesLocalCoords,
joshualitt2e3b3e32014-12-09 13:31:14 -0800152 uint8_t coverage = 0xff) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000153 switch (edgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700154 case kFillAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -0700155 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000156 return NULL;
157 }
joshualitt8059eb92014-12-29 15:10:07 -0800158 return SkNEW_ARGS(GrQuadEffect, (color, viewMatrix, coverage,
159 kFillAA_GrProcessorEdgeType,
joshualittb8c241a2015-05-19 08:23:30 -0700160 localMatrix, usesLocalCoords));
joshualittb0a8a372014-09-23 09:50:21 -0700161 case kHairlineAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -0700162 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000163 return NULL;
164 }
joshualitt8059eb92014-12-29 15:10:07 -0800165 return SkNEW_ARGS(GrQuadEffect, (color, viewMatrix, coverage,
166 kHairlineAA_GrProcessorEdgeType,
joshualittb8c241a2015-05-19 08:23:30 -0700167 localMatrix, usesLocalCoords));
joshualittb0a8a372014-09-23 09:50:21 -0700168 case kFillBW_GrProcessorEdgeType:
joshualitt8059eb92014-12-29 15:10:07 -0800169 return SkNEW_ARGS(GrQuadEffect, (color, viewMatrix, coverage,
170 kFillBW_GrProcessorEdgeType,
joshualittb8c241a2015-05-19 08:23:30 -0700171 localMatrix, usesLocalCoords));
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000172 default:
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000173 return NULL;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000174 }
175 }
176
177 virtual ~GrQuadEffect();
178
mtklein36352bf2015-03-25 18:17:31 -0700179 const char* name() const override { return "Quad"; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000180
joshualitt71c92602015-01-14 08:12:47 -0800181 inline const Attribute* inPosition() const { return fInPosition; }
182 inline const Attribute* inHairQuadEdge() const { return fInHairQuadEdge; }
joshualittb0a8a372014-09-23 09:50:21 -0700183 inline bool isAntiAliased() const { return GrProcessorEdgeTypeIsAA(fEdgeType); }
184 inline bool isFilled() const { return GrProcessorEdgeTypeIsFill(fEdgeType); }
185 inline GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
joshualitt88c23fc2015-05-13 14:18:07 -0700186 GrColor color() const { return fColor; }
joshualittb8c241a2015-05-19 08:23:30 -0700187 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
joshualitte578a952015-05-14 10:09:13 -0700188 const SkMatrix& viewMatrix() const { return fViewMatrix; }
joshualitte3ababe2015-05-15 07:56:07 -0700189 const SkMatrix& localMatrix() const { return fLocalMatrix; }
joshualittb8c241a2015-05-19 08:23:30 -0700190 bool usesLocalCoords() const { return fUsesLocalCoords; }
191 uint8_t coverageScale() const { return fCoverageScale; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000192
joshualitteb2a6762014-12-04 11:35:33 -0800193 virtual void getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700194 const GrGLSLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -0700195 GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000196
joshualittabb52a12015-01-13 15:02:10 -0800197 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700198 const GrGLSLCaps&) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000199
200private:
joshualitt8059eb92014-12-29 15:10:07 -0800201 GrQuadEffect(GrColor, const SkMatrix& viewMatrix, uint8_t coverage, GrPrimitiveEdgeType,
joshualittb8c241a2015-05-19 08:23:30 -0700202 const SkMatrix& localMatrix, bool usesLocalCoords);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000203
joshualitt88c23fc2015-05-13 14:18:07 -0700204 GrColor fColor;
joshualitte578a952015-05-14 10:09:13 -0700205 SkMatrix fViewMatrix;
joshualitte3ababe2015-05-15 07:56:07 -0700206 SkMatrix fLocalMatrix;
joshualittb8c241a2015-05-19 08:23:30 -0700207 bool fUsesLocalCoords;
joshualitt88c23fc2015-05-13 14:18:07 -0700208 uint8_t fCoverageScale;
209 GrPrimitiveEdgeType fEdgeType;
joshualitt71c92602015-01-14 08:12:47 -0800210 const Attribute* fInPosition;
211 const Attribute* fInHairQuadEdge;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000212
joshualittb0a8a372014-09-23 09:50:21 -0700213 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000214
joshualitt249af152014-09-15 11:41:13 -0700215 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000216};
217
218//////////////////////////////////////////////////////////////////////////////
219/**
220 * Shader is based off of "Resolution Independent Curve Rendering using
221 * Programmable Graphics Hardware" by Loop and Blinn.
222 * The output of this effect is a hairline edge for non rational cubics.
223 * Cubics are specified by implicit equation K^3 - LM.
224 * K, L, and M, are the first three values of the vertex attribute,
225 * the fourth value is not used. Distance is calculated using a
226 * first order approximation from the taylor series.
227 * Coverage for AA is max(0, 1-distance).
228 */
229class GrGLCubicEffect;
230
joshualitt249af152014-09-15 11:41:13 -0700231class GrCubicEffect : public GrGeometryProcessor {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000232public:
joshualitt2e3b3e32014-12-09 13:31:14 -0800233 static GrGeometryProcessor* Create(GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800234 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800235 const GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700236 const GrDrawTargetCaps& caps) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000237 switch (edgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700238 case kFillAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -0700239 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000240 return NULL;
241 }
joshualitt8059eb92014-12-29 15:10:07 -0800242 return SkNEW_ARGS(GrCubicEffect, (color, viewMatrix, kFillAA_GrProcessorEdgeType));
joshualittb0a8a372014-09-23 09:50:21 -0700243 case kHairlineAA_GrProcessorEdgeType:
jvanverthe9c0fc62015-04-29 11:18:05 -0700244 if (!caps.shaderCaps()->shaderDerivativeSupport()) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000245 return NULL;
246 }
joshualitt8059eb92014-12-29 15:10:07 -0800247 return SkNEW_ARGS(GrCubicEffect, (color, viewMatrix,
248 kHairlineAA_GrProcessorEdgeType));
joshualittb0a8a372014-09-23 09:50:21 -0700249 case kFillBW_GrProcessorEdgeType:
joshualitt8059eb92014-12-29 15:10:07 -0800250 return SkNEW_ARGS(GrCubicEffect, (color, viewMatrix,
251 kFillBW_GrProcessorEdgeType));
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000252 default:
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000253 return NULL;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000254 }
255 }
256
257 virtual ~GrCubicEffect();
258
mtklein36352bf2015-03-25 18:17:31 -0700259 const char* name() const override { return "Cubic"; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000260
joshualitt71c92602015-01-14 08:12:47 -0800261 inline const Attribute* inPosition() const { return fInPosition; }
262 inline const Attribute* inCubicCoeffs() const { return fInCubicCoeffs; }
joshualittb0a8a372014-09-23 09:50:21 -0700263 inline bool isAntiAliased() const { return GrProcessorEdgeTypeIsAA(fEdgeType); }
264 inline bool isFilled() const { return GrProcessorEdgeTypeIsFill(fEdgeType); }
265 inline GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
joshualitt88c23fc2015-05-13 14:18:07 -0700266 GrColor color() const { return fColor; }
joshualittb8c241a2015-05-19 08:23:30 -0700267 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
joshualitte578a952015-05-14 10:09:13 -0700268 const SkMatrix& viewMatrix() const { return fViewMatrix; }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000269
joshualitteb2a6762014-12-04 11:35:33 -0800270 virtual void getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700271 const GrGLSLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -0700272 GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000273
joshualittabb52a12015-01-13 15:02:10 -0800274 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700275 const GrGLSLCaps&) const override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000276
277private:
joshualitt8059eb92014-12-29 15:10:07 -0800278 GrCubicEffect(GrColor, const SkMatrix& viewMatrix, GrPrimitiveEdgeType);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000279
joshualitt88c23fc2015-05-13 14:18:07 -0700280 GrColor fColor;
joshualitte578a952015-05-14 10:09:13 -0700281 SkMatrix fViewMatrix;
joshualitt88c23fc2015-05-13 14:18:07 -0700282 GrPrimitiveEdgeType fEdgeType;
joshualitt71c92602015-01-14 08:12:47 -0800283 const Attribute* fInPosition;
284 const Attribute* fInCubicCoeffs;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000285
joshualittb0a8a372014-09-23 09:50:21 -0700286 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000287
joshualitt249af152014-09-15 11:41:13 -0700288 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000289};
290
291#endif