blob: 4dca884734f102a168d9adb6a4ec2756564c150a [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#include "GrBezierEffect.h"
9
10#include "gl/GrGLEffect.h"
11#include "gl/GrGLSL.h"
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000012#include "gl/GrGLVertexEffect.h"
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000013#include "GrTBackendEffectFactory.h"
14
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000015class GrGLConicEffect : public GrGLVertexEffect {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000016public:
17 GrGLConicEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
18
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000019 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000020 const GrDrawEffect& drawEffect,
21 EffectKey key,
22 const char* outputColor,
23 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000024 const TransformedCoordsArray&,
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000025 const TextureSamplerArray&) SK_OVERRIDE;
26
27 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
28
29 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
30
31private:
32 GrBezierEdgeType fEdgeType;
33
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000034 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000035};
skia.committer@gmail.com44a77c82013-08-23 07:01:29 +000036
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000037GrGLConicEffect::GrGLConicEffect(const GrBackendEffectFactory& factory,
38 const GrDrawEffect& drawEffect)
39 : INHERITED (factory) {
40 const GrConicEffect& ce = drawEffect.castEffect<GrConicEffect>();
41 fEdgeType = ce.getEdgeType();
42}
43
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000044void GrGLConicEffect::emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000045 const GrDrawEffect& drawEffect,
46 EffectKey key,
47 const char* outputColor,
48 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000049 const TransformedCoordsArray&,
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000050 const TextureSamplerArray& samplers) {
51 const char *vsName, *fsName;
52
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000053 builder->addVarying(kVec4f_GrSLType, "ConicCoeffs",
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +000054 &vsName, &fsName);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000055 const SkString* attr0Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000056 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
57 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000058
59 builder->fsCodeAppend("\t\tfloat edgeAlpha;\n");
60
61 switch (fEdgeType) {
62 case kHairAA_GrBezierEdgeType: {
63 SkAssertResult(builder->enableFeature(
64 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
65 builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName);
66 builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName);
67 builder->fsCodeAppendf("\t\tfloat dfdx =\n"
68 "\t\t\t2.0*%s.x*dklmdx.x - %s.y*dklmdx.z - %s.z*dklmdx.y;\n",
69 fsName, fsName, fsName);
70 builder->fsCodeAppendf("\t\tfloat dfdy =\n"
71 "\t\t\t2.0*%s.x*dklmdy.x - %s.y*dklmdy.z - %s.z*dklmdy.y;\n",
72 fsName, fsName, fsName);
73 builder->fsCodeAppend("\t\tvec2 gF = vec2(dfdx, dfdy);\n");
74 builder->fsCodeAppend("\t\tfloat gFM = sqrt(dot(gF, gF));\n");
75 builder->fsCodeAppendf("\t\tfloat func = %s.x*%s.x - %s.y*%s.z;\n", fsName, fsName,
76 fsName, fsName);
77 builder->fsCodeAppend("\t\tfunc = abs(func);\n");
78 builder->fsCodeAppend("\t\tedgeAlpha = func / gFM;\n");
79 builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
80 // Add line below for smooth cubic ramp
81 // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n");
82 break;
83 }
84 case kFillAA_GrBezierEdgeType: {
85 SkAssertResult(builder->enableFeature(
86 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
87 builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName);
88 builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName);
89 builder->fsCodeAppendf("\t\tfloat dfdx =\n"
90 "\t\t\t2.0*%s.x*dklmdx.x - %s.y*dklmdx.z - %s.z*dklmdx.y;\n",
91 fsName, fsName, fsName);
92 builder->fsCodeAppendf("\t\tfloat dfdy =\n"
93 "\t\t\t2.0*%s.x*dklmdy.x - %s.y*dklmdy.z - %s.z*dklmdy.y;\n",
94 fsName, fsName, fsName);
95 builder->fsCodeAppend("\t\tvec2 gF = vec2(dfdx, dfdy);\n");
96 builder->fsCodeAppend("\t\tfloat gFM = sqrt(dot(gF, gF));\n");
97 builder->fsCodeAppendf("\t\tfloat func = %s.x*%s.x - %s.y*%s.z;\n", fsName, fsName,
98 fsName, fsName);
99 builder->fsCodeAppend("\t\tedgeAlpha = func / gFM;\n");
100 builder->fsCodeAppend("\t\tedgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1.0);\n");
101 // Add line below for smooth cubic ramp
102 // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n");
103 break;
104 }
105 case kFillNoAA_GrBezierEdgeType: {
106 builder->fsCodeAppendf("\t\tedgeAlpha = %s.x*%s.x - %s.y*%s.z;\n", fsName, fsName,
107 fsName, fsName);
108 builder->fsCodeAppend("\t\tedgeAlpha = float(edgeAlpha < 0.0);\n");
109 break;
110 }
111 }
112
113 SkString modulate;
114 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
115 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
116}
117
118GrGLEffect::EffectKey GrGLConicEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
119 const GrConicEffect& ce = drawEffect.castEffect<GrConicEffect>();
120 return ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2;
121}
122
123//////////////////////////////////////////////////////////////////////////////
124
125GrConicEffect::~GrConicEffect() {}
126
127const GrBackendEffectFactory& GrConicEffect::getFactory() const {
128 return GrTBackendEffectFactory<GrConicEffect>::getInstance();
129}
130
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000131GrConicEffect::GrConicEffect(GrBezierEdgeType edgeType) : GrVertexEffect() {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000132 this->addVertexAttrib(kVec4f_GrSLType);
133 fEdgeType = edgeType;
134}
135
136bool GrConicEffect::onIsEqual(const GrEffect& other) const {
137 const GrConicEffect& ce = CastEffect<GrConicEffect>(other);
138 return (ce.fEdgeType == fEdgeType);
139}
140
141//////////////////////////////////////////////////////////////////////////////
142
143GR_DEFINE_EFFECT_TEST(GrConicEffect);
144
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000145GrEffectRef* GrConicEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000146 GrContext*,
147 const GrDrawTargetCaps& caps,
148 GrTexture*[]) {
149 const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->nextULessThan(3));
150 return GrConicEffect::Create(edgeType, caps);
151}
152
153//////////////////////////////////////////////////////////////////////////////
154// Quad
155//////////////////////////////////////////////////////////////////////////////
156
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000157class GrGLQuadEffect : public GrGLVertexEffect {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000158public:
159 GrGLQuadEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
160
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000161 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000162 const GrDrawEffect& drawEffect,
163 EffectKey key,
164 const char* outputColor,
165 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000166 const TransformedCoordsArray&,
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000167 const TextureSamplerArray&) SK_OVERRIDE;
168
169 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
170
171 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
172
173private:
174 GrBezierEdgeType fEdgeType;
175
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000176 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000177};
skia.committer@gmail.com44a77c82013-08-23 07:01:29 +0000178
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000179GrGLQuadEffect::GrGLQuadEffect(const GrBackendEffectFactory& factory,
180 const GrDrawEffect& drawEffect)
181 : INHERITED (factory) {
182 const GrQuadEffect& ce = drawEffect.castEffect<GrQuadEffect>();
183 fEdgeType = ce.getEdgeType();
184}
185
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000186void GrGLQuadEffect::emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000187 const GrDrawEffect& drawEffect,
188 EffectKey key,
189 const char* outputColor,
190 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000191 const TransformedCoordsArray&,
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000192 const TextureSamplerArray& samplers) {
193 const char *vsName, *fsName;
194
195 const SkString* attrName =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000196 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000197 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n");
198
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000199 builder->addVarying(kVec4f_GrSLType, "HairQuadEdge", &vsName, &fsName);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000200
201 switch (fEdgeType) {
202 case kHairAA_GrBezierEdgeType: {
203 SkAssertResult(builder->enableFeature(
204 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
205 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
206 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
207 builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
208 "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
209 fsName, fsName);
210 builder->fsCodeAppendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName,
211 fsName);
212 builder->fsCodeAppend("\t\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n");
213 builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
214 // Add line below for smooth cubic ramp
215 // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n");
216 break;
217 }
218 case kFillAA_GrBezierEdgeType: {
219 SkAssertResult(builder->enableFeature(
220 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
221 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
222 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
223 builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
224 "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
225 fsName, fsName);
226 builder->fsCodeAppendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName,
227 fsName);
228 builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha / sqrt(dot(gF, gF));\n");
229 builder->fsCodeAppend("\t\tedgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1.0);\n");
230 // Add line below for smooth cubic ramp
231 // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n");
232 break;
233 }
234 case kFillNoAA_GrBezierEdgeType: {
235 builder->fsCodeAppendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName,
236 fsName);
237 builder->fsCodeAppend("\t\tedgeAlpha = float(edgeAlpha < 0.0);\n");
238 break;
239 }
240 }
241
242 SkString modulate;
243 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
244 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
245
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000246 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000247}
248
249GrGLEffect::EffectKey GrGLQuadEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
250 const GrQuadEffect& ce = drawEffect.castEffect<GrQuadEffect>();
251 return ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2;
252}
253
254//////////////////////////////////////////////////////////////////////////////
255
256GrQuadEffect::~GrQuadEffect() {}
257
258const GrBackendEffectFactory& GrQuadEffect::getFactory() const {
259 return GrTBackendEffectFactory<GrQuadEffect>::getInstance();
260}
261
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000262GrQuadEffect::GrQuadEffect(GrBezierEdgeType edgeType) : GrVertexEffect() {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000263 this->addVertexAttrib(kVec4f_GrSLType);
264 fEdgeType = edgeType;
265}
266
267bool GrQuadEffect::onIsEqual(const GrEffect& other) const {
268 const GrQuadEffect& ce = CastEffect<GrQuadEffect>(other);
269 return (ce.fEdgeType == fEdgeType);
270}
271
272//////////////////////////////////////////////////////////////////////////////
273
274GR_DEFINE_EFFECT_TEST(GrQuadEffect);
275
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000276GrEffectRef* GrQuadEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000277 GrContext*,
278 const GrDrawTargetCaps& caps,
279 GrTexture*[]) {
280 const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->nextULessThan(3));
281 return GrQuadEffect::Create(edgeType, caps);
282}
283
284//////////////////////////////////////////////////////////////////////////////
285// Cubic
286//////////////////////////////////////////////////////////////////////////////
287
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000288class GrGLCubicEffect : public GrGLVertexEffect {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000289public:
290 GrGLCubicEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
291
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000292 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000293 const GrDrawEffect& drawEffect,
294 EffectKey key,
295 const char* outputColor,
296 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000297 const TransformedCoordsArray&,
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000298 const TextureSamplerArray&) SK_OVERRIDE;
299
300 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
301
302 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
303
304private:
305 GrBezierEdgeType fEdgeType;
306
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000307 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000308};
skia.committer@gmail.com44a77c82013-08-23 07:01:29 +0000309
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000310GrGLCubicEffect::GrGLCubicEffect(const GrBackendEffectFactory& factory,
311 const GrDrawEffect& drawEffect)
312 : INHERITED (factory) {
313 const GrCubicEffect& ce = drawEffect.castEffect<GrCubicEffect>();
314 fEdgeType = ce.getEdgeType();
315}
316
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000317void GrGLCubicEffect::emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000318 const GrDrawEffect& drawEffect,
319 EffectKey key,
320 const char* outputColor,
321 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000322 const TransformedCoordsArray&,
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000323 const TextureSamplerArray& samplers) {
324 const char *vsName, *fsName;
325
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000326 builder->addVarying(kVec4f_GrSLType, "CubicCoeffs",
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000327 &vsName, &fsName);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000328 const SkString* attr0Name =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000329 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
330 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000331
332 builder->fsCodeAppend("\t\tfloat edgeAlpha;\n");
333
334 switch (fEdgeType) {
335 case kHairAA_GrBezierEdgeType: {
336 SkAssertResult(builder->enableFeature(
337 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
338 builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName);
339 builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName);
340 builder->fsCodeAppendf("\t\tfloat dfdx =\n"
341 "\t\t3.0*%s.x*%s.x*dklmdx.x - %s.y*dklmdx.z - %s.z*dklmdx.y;\n",
342 fsName, fsName, fsName, fsName);
343 builder->fsCodeAppendf("\t\tfloat dfdy =\n"
344 "\t\t3.0*%s.x*%s.x*dklmdy.x - %s.y*dklmdy.z - %s.z*dklmdy.y;\n",
345 fsName, fsName, fsName, fsName);
346 builder->fsCodeAppend("\t\tvec2 gF = vec2(dfdx, dfdy);\n");
347 builder->fsCodeAppend("\t\tfloat gFM = sqrt(dot(gF, gF));\n");
348 builder->fsCodeAppendf("\t\tfloat func = %s.x*%s.x*%s.x - %s.y*%s.z;\n",
349 fsName, fsName, fsName, fsName, fsName);
350 builder->fsCodeAppend("\t\tfunc = abs(func);\n");
351 builder->fsCodeAppend("\t\tedgeAlpha = func / gFM;\n");
352 builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
353 // Add line below for smooth cubic ramp
354 // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n");
355 break;
356 }
357 case kFillAA_GrBezierEdgeType: {
358 SkAssertResult(builder->enableFeature(
359 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
360 builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName);
361 builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName);
362 builder->fsCodeAppendf("\t\tfloat dfdx =\n"
363 "\t\t3.0*%s.x*%s.x*dklmdx.x - %s.y*dklmdx.z - %s.z*dklmdx.y;\n",
364 fsName, fsName, fsName, fsName);
365 builder->fsCodeAppendf("\t\tfloat dfdy =\n"
366 "\t\t3.0*%s.x*%s.x*dklmdy.x - %s.y*dklmdy.z - %s.z*dklmdy.y;\n",
367 fsName, fsName, fsName, fsName);
368 builder->fsCodeAppend("\t\tvec2 gF = vec2(dfdx, dfdy);\n");
369 builder->fsCodeAppend("\t\tfloat gFM = sqrt(dot(gF, gF));\n");
370 builder->fsCodeAppendf("\t\tfloat func = %s.x*%s.x*%s.x - %s.y*%s.z;\n",
371 fsName, fsName, fsName, fsName, fsName);
372 builder->fsCodeAppend("\t\tedgeAlpha = func / gFM;\n");
373 builder->fsCodeAppend("\t\tedgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1.0);\n");
374 // Add line below for smooth cubic ramp
375 // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n");
376 break;
377 }
378 case kFillNoAA_GrBezierEdgeType: {
379 builder->fsCodeAppendf("\t\tedgeAlpha = %s.x*%s.x*%s.x - %s.y*%s.z;\n",
380 fsName, fsName, fsName, fsName, fsName);
381 builder->fsCodeAppend("\t\tedgeAlpha = float(edgeAlpha < 0.0);\n");
382 break;
383 }
384 }
385
386 SkString modulate;
387 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
388 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
389}
390
391GrGLEffect::EffectKey GrGLCubicEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
392 const GrCubicEffect& ce = drawEffect.castEffect<GrCubicEffect>();
393 return ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2;
394}
395
396//////////////////////////////////////////////////////////////////////////////
397
398GrCubicEffect::~GrCubicEffect() {}
399
400const GrBackendEffectFactory& GrCubicEffect::getFactory() const {
401 return GrTBackendEffectFactory<GrCubicEffect>::getInstance();
402}
403
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000404GrCubicEffect::GrCubicEffect(GrBezierEdgeType edgeType) : GrVertexEffect() {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000405 this->addVertexAttrib(kVec4f_GrSLType);
406 fEdgeType = edgeType;
407}
408
409bool GrCubicEffect::onIsEqual(const GrEffect& other) const {
410 const GrCubicEffect& ce = CastEffect<GrCubicEffect>(other);
411 return (ce.fEdgeType == fEdgeType);
412}
413
414//////////////////////////////////////////////////////////////////////////////
415
416GR_DEFINE_EFFECT_TEST(GrCubicEffect);
417
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000418GrEffectRef* GrCubicEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000419 GrContext*,
420 const GrDrawTargetCaps& caps,
421 GrTexture*[]) {
422 const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->nextULessThan(3));
423 return GrCubicEffect::Create(edgeType, caps);
424}