blob: 90b91b0bd2c34cd7fe44a2ef0f5778dcbd1051c5 [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
joshualittb0a8a372014-09-23 09:50:21 -070010#include "gl/GrGLProcessor.h"
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000011#include "gl/GrGLSL.h"
joshualitt249af152014-09-15 11:41:13 -070012#include "gl/GrGLGeometryProcessor.h"
joshualitteb2a6762014-12-04 11:35:33 -080013#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000014
joshualitt9b989322014-12-15 14:16:27 -080015struct ConicBatchTracker {
16 GrGPInput fInputColorType;
17 GrColor fColor;
18 uint8_t fCoverageScale;
joshualitt290c09b2014-12-19 13:45:20 -080019 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -080020};
21
joshualitt249af152014-09-15 11:41:13 -070022class GrGLConicEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000023public:
joshualitteb2a6762014-12-04 11:35:33 -080024 GrGLConicEffect(const GrGeometryProcessor&,
joshualitt87f48d92014-12-04 10:41:40 -080025 const GrBatchTracker&);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000026
mtklein36352bf2015-03-25 18:17:31 -070027 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000028
joshualitt87f48d92014-12-04 10:41:40 -080029 static inline void GenKey(const GrGeometryProcessor&,
30 const GrBatchTracker&,
31 const GrGLCaps&,
32 GrProcessorKeyBuilder*);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000033
joshualitt9b989322014-12-15 14:16:27 -080034 virtual void setData(const GrGLProgramDataManager& pdman,
joshualittee2af952014-12-30 09:04:15 -080035 const GrPrimitiveProcessor& primProc,
mtklein36352bf2015-03-25 18:17:31 -070036 const GrBatchTracker& bt) override {
joshualittee2af952014-12-30 09:04:15 -080037 this->setUniformViewMatrix(pdman, primProc.viewMatrix());
38
joshualitt9b989322014-12-15 14:16:27 -080039 const ConicBatchTracker& local = bt.cast<ConicBatchTracker>();
40 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
41 GrGLfloat c[4];
42 GrColorToRGBAFloat(local.fColor, c);
43 pdman.set4fv(fColorUniform, 1, c);
44 fColor = local.fColor;
45 }
46 if (0xff != local.fCoverageScale && fCoverageScale != local.fCoverageScale) {
47 pdman.set1f(fCoverageScaleUniform, GrNormalizeByteToFloat(local.fCoverageScale));
48 fCoverageScale = local.fCoverageScale;
49 }
50 }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000051
52private:
joshualitt9b989322014-12-15 14:16:27 -080053 GrColor fColor;
54 uint8_t fCoverageScale;
joshualittb0a8a372014-09-23 09:50:21 -070055 GrPrimitiveEdgeType fEdgeType;
joshualitt9b989322014-12-15 14:16:27 -080056 UniformHandle fColorUniform;
57 UniformHandle fCoverageScaleUniform;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000058
joshualitt249af152014-09-15 11:41:13 -070059 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000060};
skia.committer@gmail.com44a77c82013-08-23 07:01:29 +000061
joshualitteb2a6762014-12-04 11:35:33 -080062GrGLConicEffect::GrGLConicEffect(const GrGeometryProcessor& processor,
joshualitt9b989322014-12-15 14:16:27 -080063 const GrBatchTracker& bt)
64 : fColor(GrColor_ILLEGAL), fCoverageScale(0xff) {
joshualitt87f48d92014-12-04 10:41:40 -080065 const GrConicEffect& ce = processor.cast<GrConicEffect>();
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000066 fEdgeType = ce.getEdgeType();
67}
68
robertphillips46d36f02015-01-18 08:14:14 -080069void GrGLConicEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualitt9b989322014-12-15 14:16:27 -080070 GrGLGPBuilder* pb = args.fPB;
joshualitt2dd1ae02014-12-03 06:24:10 -080071 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
72 const GrConicEffect& gp = args.fGP.cast<GrConicEffect>();
joshualitt9b989322014-12-15 14:16:27 -080073 const ConicBatchTracker& local = args.fBT.cast<ConicBatchTracker>();
joshualitt2dd1ae02014-12-03 06:24:10 -080074
joshualittabb52a12015-01-13 15:02:10 -080075 // emit attributes
76 vsBuilder->emitAttributes(gp);
77
joshualitt74077b92014-10-24 11:26:03 -070078 GrGLVertToFrag v(kVec4f_GrSLType);
79 args.fPB->addVarying("ConicCoeffs", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -080080 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), gp.inConicCoeffs()->fName);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000081
joshualitt9b989322014-12-15 14:16:27 -080082 // Setup pass through color
83 this->setupColorPassThrough(args.fPB, local.fInputColorType, args.fOutputColor, NULL,
84 &fColorUniform);
85
joshualittabb52a12015-01-13 15:02:10 -080086 // Setup position
joshualittdd219872015-02-12 14:48:42 -080087 this->setupPosition(pb, gpArgs, gp.inPosition()->fName, gp.viewMatrix());
joshualitt4973d9d2014-11-08 09:24:25 -080088
joshualittabb52a12015-01-13 15:02:10 -080089 // emit transforms with position
robertphillips46d36f02015-01-18 08:14:14 -080090 this->emitTransforms(pb, gpArgs->fPositionVar, gp.inPosition()->fName, gp.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -080091 args.fTransformsIn, args.fTransformsOut);
92
joshualittc369e7c2014-10-22 10:56:26 -070093 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
egdaniel9bd5bbf2014-08-29 11:12:30 -070094 fsBuilder->codeAppend("float edgeAlpha;");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000095
96 switch (fEdgeType) {
joshualittb0a8a372014-09-23 09:50:21 -070097 case kHairlineAA_GrProcessorEdgeType: {
joshualitt30ba4362014-08-21 20:18:45 -070098 SkAssertResult(fsBuilder->enableFeature(
99 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
joshualitt74077b92014-10-24 11:26:03 -0700100 fsBuilder->codeAppendf("vec3 dklmdx = dFdx(%s.xyz);", v.fsIn());
101 fsBuilder->codeAppendf("vec3 dklmdy = dFdy(%s.xyz);", v.fsIn());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700102 fsBuilder->codeAppendf("float dfdx ="
103 "2.0 * %s.x * dklmdx.x - %s.y * dklmdx.z - %s.z * dklmdx.y;",
joshualitt74077b92014-10-24 11:26:03 -0700104 v.fsIn(), v.fsIn(), v.fsIn());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700105 fsBuilder->codeAppendf("float dfdy ="
106 "2.0 * %s.x * dklmdy.x - %s.y * dklmdy.z - %s.z * dklmdy.y;",
joshualitt74077b92014-10-24 11:26:03 -0700107 v.fsIn(), v.fsIn(), v.fsIn());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700108 fsBuilder->codeAppend("vec2 gF = vec2(dfdx, dfdy);");
109 fsBuilder->codeAppend("float gFM = sqrt(dot(gF, gF));");
joshualitt74077b92014-10-24 11:26:03 -0700110 fsBuilder->codeAppendf("float func = %s.x*%s.x - %s.y*%s.z;", v.fsIn(), v.fsIn(),
111 v.fsIn(), v.fsIn());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700112 fsBuilder->codeAppend("func = abs(func);");
113 fsBuilder->codeAppend("edgeAlpha = func / gFM;");
114 fsBuilder->codeAppend("edgeAlpha = max(1.0 - edgeAlpha, 0.0);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000115 // Add line below for smooth cubic ramp
egdaniel9bd5bbf2014-08-29 11:12:30 -0700116 // fsBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000117 break;
118 }
joshualittb0a8a372014-09-23 09:50:21 -0700119 case kFillAA_GrProcessorEdgeType: {
joshualitt30ba4362014-08-21 20:18:45 -0700120 SkAssertResult(fsBuilder->enableFeature(
121 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
joshualitt74077b92014-10-24 11:26:03 -0700122 fsBuilder->codeAppendf("vec3 dklmdx = dFdx(%s.xyz);", v.fsIn());
123 fsBuilder->codeAppendf("vec3 dklmdy = dFdy(%s.xyz);", v.fsIn());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700124 fsBuilder->codeAppendf("float dfdx ="
125 "2.0 * %s.x * dklmdx.x - %s.y * dklmdx.z - %s.z * dklmdx.y;",
joshualitt74077b92014-10-24 11:26:03 -0700126 v.fsIn(), v.fsIn(), v.fsIn());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700127 fsBuilder->codeAppendf("float dfdy ="
128 "2.0 * %s.x * dklmdy.x - %s.y * dklmdy.z - %s.z * dklmdy.y;",
joshualitt74077b92014-10-24 11:26:03 -0700129 v.fsIn(), v.fsIn(), v.fsIn());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700130 fsBuilder->codeAppend("vec2 gF = vec2(dfdx, dfdy);");
131 fsBuilder->codeAppend("float gFM = sqrt(dot(gF, gF));");
joshualitt74077b92014-10-24 11:26:03 -0700132 fsBuilder->codeAppendf("float func = %s.x * %s.x - %s.y * %s.z;", v.fsIn(), v.fsIn(),
133 v.fsIn(), v.fsIn());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700134 fsBuilder->codeAppend("edgeAlpha = func / gFM;");
135 fsBuilder->codeAppend("edgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1.0);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000136 // Add line below for smooth cubic ramp
egdaniel9bd5bbf2014-08-29 11:12:30 -0700137 // fsBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000138 break;
139 }
joshualittb0a8a372014-09-23 09:50:21 -0700140 case kFillBW_GrProcessorEdgeType: {
joshualitt74077b92014-10-24 11:26:03 -0700141 fsBuilder->codeAppendf("edgeAlpha = %s.x * %s.x - %s.y * %s.z;", v.fsIn(), v.fsIn(),
142 v.fsIn(), v.fsIn());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700143 fsBuilder->codeAppend("edgeAlpha = float(edgeAlpha < 0.0);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000144 break;
145 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000146 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000147 SkFAIL("Shouldn't get here");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000148 }
149
joshualitt9b989322014-12-15 14:16:27 -0800150 if (0xff != local.fCoverageScale) {
151 const char* coverageScale;
152 fCoverageScaleUniform = pb->addUniform(GrGLProgramBuilder::kFragment_Visibility,
joshualittdd219872015-02-12 14:48:42 -0800153 kFloat_GrSLType,
154 kDefault_GrSLPrecision,
155 "Coverage",
156 &coverageScale);
joshualitt9b989322014-12-15 14:16:27 -0800157 fsBuilder->codeAppendf("%s = vec4(%s * edgeAlpha);", args.fOutputCoverage, coverageScale);
158 } else {
159 fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
160 }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000161}
162
robertphillips46d36f02015-01-18 08:14:14 -0800163void GrGLConicEffect::GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -0800164 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800165 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700166 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -0800167 const GrConicEffect& ce = gp.cast<GrConicEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800168 const ConicBatchTracker& local = bt.cast<ConicBatchTracker>();
bsalomon63e99f72014-07-21 08:03:14 -0700169 uint32_t key = ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2;
joshualitt9b989322014-12-15 14:16:27 -0800170 key |= kUniform_GrGPInput == local.fInputColorType ? 0x4 : 0x0;
171 key |= 0xff != local.fCoverageScale ? 0x8 : 0x0;
robertphillips46d36f02015-01-18 08:14:14 -0800172 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x10 : 0x0;
173 key |= ComputePosKey(gp.viewMatrix()) << 5;
bsalomon63e99f72014-07-21 08:03:14 -0700174 b->add32(key);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000175}
176
177//////////////////////////////////////////////////////////////////////////////
178
179GrConicEffect::~GrConicEffect() {}
180
joshualitteb2a6762014-12-04 11:35:33 -0800181void GrConicEffect::getGLProcessorKey(const GrBatchTracker& bt,
182 const GrGLCaps& caps,
183 GrProcessorKeyBuilder* b) const {
184 GrGLConicEffect::GenKey(*this, bt, caps, b);
185}
186
joshualittabb52a12015-01-13 15:02:10 -0800187GrGLPrimitiveProcessor* GrConicEffect::createGLInstance(const GrBatchTracker& bt,
188 const GrGLCaps&) const {
joshualitteb2a6762014-12-04 11:35:33 -0800189 return SkNEW_ARGS(GrGLConicEffect, (*this, bt));
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000190}
191
joshualitt8059eb92014-12-29 15:10:07 -0800192GrConicEffect::GrConicEffect(GrColor color, const SkMatrix& viewMatrix, uint8_t coverage,
193 GrPrimitiveEdgeType edgeType, const SkMatrix& localMatrix)
194 : INHERITED(color, viewMatrix, localMatrix)
195 , fCoverageScale(coverage)
196 , fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800197 this->initClassID<GrConicEffect>();
joshualitt71c92602015-01-14 08:12:47 -0800198 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
199 fInConicCoeffs = &this->addVertexAttrib(Attribute("inConicCoeffs",
joshualitt2dd1ae02014-12-03 06:24:10 -0800200 kVec4f_GrVertexAttribType));
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000201}
202
bsalomon0e08fc12014-10-15 08:19:04 -0700203bool GrConicEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700204 const GrConicEffect& ce = other.cast<GrConicEffect>();
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000205 return (ce.fEdgeType == fEdgeType);
206}
207
joshualitt4d8da812015-01-28 12:53:54 -0800208void GrConicEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -0800209 ConicBatchTracker* local = bt->cast<ConicBatchTracker>();
210 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
211 local->fCoverageScale = fCoverageScale;
joshualitt290c09b2014-12-19 13:45:20 -0800212 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800213}
214
joshualitt290c09b2014-12-19 13:45:20 -0800215bool GrConicEffect::onCanMakeEqual(const GrBatchTracker& m,
216 const GrGeometryProcessor& that,
217 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800218 const ConicBatchTracker& mine = m.cast<ConicBatchTracker>();
219 const ConicBatchTracker& theirs = t.cast<ConicBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -0800220 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
221 that, theirs.fUsesLocalCoords) &&
222 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -0800223 theirs.fInputColorType, theirs.fColor) &&
224 mine.fCoverageScale == theirs.fCoverageScale;
225}
226
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000227//////////////////////////////////////////////////////////////////////////////
228
joshualittb0a8a372014-09-23 09:50:21 -0700229GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrConicEffect);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000230
joshualittb0a8a372014-09-23 09:50:21 -0700231GrGeometryProcessor* GrConicEffect::TestCreate(SkRandom* random,
232 GrContext*,
233 const GrDrawTargetCaps& caps,
234 GrTexture*[]) {
235 GrGeometryProcessor* gp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000236 do {
joshualittb0a8a372014-09-23 09:50:21 -0700237 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
238 random->nextULessThan(kGrProcessorEdgeTypeCnt));
joshualitt8059eb92014-12-29 15:10:07 -0800239 gp = GrConicEffect::Create(GrRandomColor(random), GrProcessorUnitTest::TestMatrix(random),
240 edgeType, caps,
joshualittd27f73e2014-12-29 07:43:36 -0800241 GrProcessorUnitTest::TestMatrix(random));
joshualittb0a8a372014-09-23 09:50:21 -0700242 } while (NULL == gp);
243 return gp;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000244}
245
246//////////////////////////////////////////////////////////////////////////////
247// Quad
248//////////////////////////////////////////////////////////////////////////////
249
joshualitt9b989322014-12-15 14:16:27 -0800250struct QuadBatchTracker {
251 GrGPInput fInputColorType;
252 GrColor fColor;
253 uint8_t fCoverageScale;
joshualitt290c09b2014-12-19 13:45:20 -0800254 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800255};
256
joshualitt249af152014-09-15 11:41:13 -0700257class GrGLQuadEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000258public:
joshualitteb2a6762014-12-04 11:35:33 -0800259 GrGLQuadEffect(const GrGeometryProcessor&,
joshualitt87f48d92014-12-04 10:41:40 -0800260 const GrBatchTracker&);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000261
mtklein36352bf2015-03-25 18:17:31 -0700262 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000263
joshualitt87f48d92014-12-04 10:41:40 -0800264 static inline void GenKey(const GrGeometryProcessor&,
265 const GrBatchTracker&,
266 const GrGLCaps&,
267 GrProcessorKeyBuilder*);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000268
joshualitt9b989322014-12-15 14:16:27 -0800269 virtual void setData(const GrGLProgramDataManager& pdman,
joshualittee2af952014-12-30 09:04:15 -0800270 const GrPrimitiveProcessor& primProc,
mtklein36352bf2015-03-25 18:17:31 -0700271 const GrBatchTracker& bt) override {
joshualittee2af952014-12-30 09:04:15 -0800272 this->setUniformViewMatrix(pdman, primProc.viewMatrix());
273
joshualitt9b989322014-12-15 14:16:27 -0800274 const QuadBatchTracker& local = bt.cast<QuadBatchTracker>();
275 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
276 GrGLfloat c[4];
277 GrColorToRGBAFloat(local.fColor, c);
278 pdman.set4fv(fColorUniform, 1, c);
279 fColor = local.fColor;
280 }
281 if (0xff != local.fCoverageScale && local.fCoverageScale != fCoverageScale) {
282 pdman.set1f(fCoverageScaleUniform, GrNormalizeByteToFloat(local.fCoverageScale));
283 fCoverageScale = local.fCoverageScale;
284 }
285 }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000286
287private:
joshualitt9b989322014-12-15 14:16:27 -0800288 GrColor fColor;
289 uint8_t fCoverageScale;
joshualittb0a8a372014-09-23 09:50:21 -0700290 GrPrimitiveEdgeType fEdgeType;
joshualitt9b989322014-12-15 14:16:27 -0800291 UniformHandle fColorUniform;
292 UniformHandle fCoverageScaleUniform;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000293
joshualitt249af152014-09-15 11:41:13 -0700294 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000295};
skia.committer@gmail.com44a77c82013-08-23 07:01:29 +0000296
joshualitteb2a6762014-12-04 11:35:33 -0800297GrGLQuadEffect::GrGLQuadEffect(const GrGeometryProcessor& processor,
joshualitt9b989322014-12-15 14:16:27 -0800298 const GrBatchTracker& bt)
299 : fColor(GrColor_ILLEGAL), fCoverageScale(0xff) {
joshualitt87f48d92014-12-04 10:41:40 -0800300 const GrQuadEffect& ce = processor.cast<GrQuadEffect>();
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000301 fEdgeType = ce.getEdgeType();
302}
303
robertphillips46d36f02015-01-18 08:14:14 -0800304void GrGLQuadEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualitt9b989322014-12-15 14:16:27 -0800305 GrGLGPBuilder* pb = args.fPB;
joshualitt2dd1ae02014-12-03 06:24:10 -0800306 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
307 const GrQuadEffect& gp = args.fGP.cast<GrQuadEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800308 const QuadBatchTracker& local = args.fBT.cast<QuadBatchTracker>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800309
joshualittabb52a12015-01-13 15:02:10 -0800310 // emit attributes
311 vsBuilder->emitAttributes(gp);
312
joshualitt74077b92014-10-24 11:26:03 -0700313 GrGLVertToFrag v(kVec4f_GrSLType);
314 args.fPB->addVarying("HairQuadEdge", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800315 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), gp.inHairQuadEdge()->fName);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000316
joshualitt9b989322014-12-15 14:16:27 -0800317 // Setup pass through color
318 this->setupColorPassThrough(args.fPB, local.fInputColorType, args.fOutputColor, NULL,
319 &fColorUniform);
320
joshualittabb52a12015-01-13 15:02:10 -0800321 // Setup position
joshualittdd219872015-02-12 14:48:42 -0800322 this->setupPosition(pb, gpArgs, gp.inPosition()->fName, gp.viewMatrix());
joshualitt4973d9d2014-11-08 09:24:25 -0800323
joshualittabb52a12015-01-13 15:02:10 -0800324 // emit transforms with position
robertphillips46d36f02015-01-18 08:14:14 -0800325 this->emitTransforms(pb, gpArgs->fPositionVar, gp.inPosition()->fName, gp.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -0800326 args.fTransformsIn, args.fTransformsOut);
327
joshualittc369e7c2014-10-22 10:56:26 -0700328 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
egdanielb2f94d12014-08-29 10:08:36 -0700329 fsBuilder->codeAppendf("float edgeAlpha;");
joshualitt30ba4362014-08-21 20:18:45 -0700330
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000331 switch (fEdgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700332 case kHairlineAA_GrProcessorEdgeType: {
joshualitt30ba4362014-08-21 20:18:45 -0700333 SkAssertResult(fsBuilder->enableFeature(
334 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
joshualitt74077b92014-10-24 11:26:03 -0700335 fsBuilder->codeAppendf("vec2 duvdx = dFdx(%s.xy);", v.fsIn());
336 fsBuilder->codeAppendf("vec2 duvdy = dFdy(%s.xy);", v.fsIn());
egdanielb2f94d12014-08-29 10:08:36 -0700337 fsBuilder->codeAppendf("vec2 gF = vec2(2.0 * %s.x * duvdx.x - duvdx.y,"
338 " 2.0 * %s.x * duvdy.x - duvdy.y);",
joshualitt74077b92014-10-24 11:26:03 -0700339 v.fsIn(), v.fsIn());
340 fsBuilder->codeAppendf("edgeAlpha = (%s.x * %s.x - %s.y);", v.fsIn(), v.fsIn(), v.fsIn());
egdanielb2f94d12014-08-29 10:08:36 -0700341 fsBuilder->codeAppend("edgeAlpha = sqrt(edgeAlpha * edgeAlpha / dot(gF, gF));");
342 fsBuilder->codeAppend("edgeAlpha = max(1.0 - edgeAlpha, 0.0);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000343 // Add line below for smooth cubic ramp
egdanielb2f94d12014-08-29 10:08:36 -0700344 // fsBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000345 break;
346 }
joshualittb0a8a372014-09-23 09:50:21 -0700347 case kFillAA_GrProcessorEdgeType: {
joshualitt30ba4362014-08-21 20:18:45 -0700348 SkAssertResult(fsBuilder->enableFeature(
349 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
joshualitt74077b92014-10-24 11:26:03 -0700350 fsBuilder->codeAppendf("vec2 duvdx = dFdx(%s.xy);", v.fsIn());
351 fsBuilder->codeAppendf("vec2 duvdy = dFdy(%s.xy);", v.fsIn());
egdanielb2f94d12014-08-29 10:08:36 -0700352 fsBuilder->codeAppendf("vec2 gF = vec2(2.0 * %s.x * duvdx.x - duvdx.y,"
353 " 2.0 * %s.x * duvdy.x - duvdy.y);",
joshualitt74077b92014-10-24 11:26:03 -0700354 v.fsIn(), v.fsIn());
355 fsBuilder->codeAppendf("edgeAlpha = (%s.x * %s.x - %s.y);", v.fsIn(), v.fsIn(), v.fsIn());
egdanielb2f94d12014-08-29 10:08:36 -0700356 fsBuilder->codeAppend("edgeAlpha = edgeAlpha / sqrt(dot(gF, gF));");
357 fsBuilder->codeAppend("edgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1.0);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000358 // Add line below for smooth cubic ramp
egdanielb2f94d12014-08-29 10:08:36 -0700359 // fsBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000360 break;
361 }
joshualittb0a8a372014-09-23 09:50:21 -0700362 case kFillBW_GrProcessorEdgeType: {
joshualitt74077b92014-10-24 11:26:03 -0700363 fsBuilder->codeAppendf("edgeAlpha = (%s.x * %s.x - %s.y);", v.fsIn(), v.fsIn(), v.fsIn());
egdanielb2f94d12014-08-29 10:08:36 -0700364 fsBuilder->codeAppend("edgeAlpha = float(edgeAlpha < 0.0);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000365 break;
366 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000367 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000368 SkFAIL("Shouldn't get here");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000369 }
370
joshualitt9b989322014-12-15 14:16:27 -0800371 if (0xff != local.fCoverageScale) {
372 const char* coverageScale;
373 fCoverageScaleUniform = pb->addUniform(GrGLProgramBuilder::kFragment_Visibility,
374 kFloat_GrSLType,
375 kDefault_GrSLPrecision,
376 "Coverage",
377 &coverageScale);
378 fsBuilder->codeAppendf("%s = vec4(%s * edgeAlpha);", args.fOutputCoverage, coverageScale);
379 } else {
380 fsBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
381 }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000382}
383
robertphillips46d36f02015-01-18 08:14:14 -0800384void GrGLQuadEffect::GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -0800385 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800386 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700387 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -0800388 const GrQuadEffect& ce = gp.cast<GrQuadEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800389 const QuadBatchTracker& local = bt.cast<QuadBatchTracker>();
bsalomon63e99f72014-07-21 08:03:14 -0700390 uint32_t key = ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2;
joshualitt9b989322014-12-15 14:16:27 -0800391 key |= kUniform_GrGPInput == local.fInputColorType ? 0x4 : 0x0;
392 key |= 0xff != local.fCoverageScale ? 0x8 : 0x0;
robertphillips46d36f02015-01-18 08:14:14 -0800393 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x10 : 0x0;
394 key |= ComputePosKey(gp.viewMatrix()) << 5;
bsalomon63e99f72014-07-21 08:03:14 -0700395 b->add32(key);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000396}
397
398//////////////////////////////////////////////////////////////////////////////
399
400GrQuadEffect::~GrQuadEffect() {}
401
joshualitteb2a6762014-12-04 11:35:33 -0800402void GrQuadEffect::getGLProcessorKey(const GrBatchTracker& bt,
403 const GrGLCaps& caps,
404 GrProcessorKeyBuilder* b) const {
405 GrGLQuadEffect::GenKey(*this, bt, caps, b);
406}
407
joshualittabb52a12015-01-13 15:02:10 -0800408GrGLPrimitiveProcessor* GrQuadEffect::createGLInstance(const GrBatchTracker& bt,
409 const GrGLCaps&) const {
joshualitteb2a6762014-12-04 11:35:33 -0800410 return SkNEW_ARGS(GrGLQuadEffect, (*this, bt));
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000411}
412
joshualitt8059eb92014-12-29 15:10:07 -0800413GrQuadEffect::GrQuadEffect(GrColor color, const SkMatrix& viewMatrix, uint8_t coverage,
414 GrPrimitiveEdgeType edgeType, const SkMatrix& localMatrix)
415 : INHERITED(color, viewMatrix, localMatrix)
416 , fCoverageScale(coverage)
417 , fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800418 this->initClassID<GrQuadEffect>();
joshualitt71c92602015-01-14 08:12:47 -0800419 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
420 fInHairQuadEdge = &this->addVertexAttrib(Attribute("inHairQuadEdge",
joshualitt2dd1ae02014-12-03 06:24:10 -0800421 kVec4f_GrVertexAttribType));
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000422}
423
bsalomon0e08fc12014-10-15 08:19:04 -0700424bool GrQuadEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700425 const GrQuadEffect& ce = other.cast<GrQuadEffect>();
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000426 return (ce.fEdgeType == fEdgeType);
427}
428
joshualitt4d8da812015-01-28 12:53:54 -0800429void GrQuadEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -0800430 QuadBatchTracker* local = bt->cast<QuadBatchTracker>();
431 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
432 local->fCoverageScale = fCoverageScale;
joshualitt290c09b2014-12-19 13:45:20 -0800433 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800434}
435
joshualitt290c09b2014-12-19 13:45:20 -0800436bool GrQuadEffect::onCanMakeEqual(const GrBatchTracker& m,
437 const GrGeometryProcessor& that,
438 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800439 const QuadBatchTracker& mine = m.cast<QuadBatchTracker>();
440 const QuadBatchTracker& theirs = t.cast<QuadBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -0800441 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
442 that, theirs.fUsesLocalCoords) &&
443 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -0800444 theirs.fInputColorType, theirs.fColor) &&
445 mine.fCoverageScale == theirs.fCoverageScale;
446}
447
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000448//////////////////////////////////////////////////////////////////////////////
449
joshualittb0a8a372014-09-23 09:50:21 -0700450GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrQuadEffect);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000451
joshualittb0a8a372014-09-23 09:50:21 -0700452GrGeometryProcessor* GrQuadEffect::TestCreate(SkRandom* random,
453 GrContext*,
454 const GrDrawTargetCaps& caps,
455 GrTexture*[]) {
456 GrGeometryProcessor* gp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000457 do {
joshualittb0a8a372014-09-23 09:50:21 -0700458 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
459 random->nextULessThan(kGrProcessorEdgeTypeCnt));
joshualitt8059eb92014-12-29 15:10:07 -0800460 gp = GrQuadEffect::Create(GrRandomColor(random),
461 GrProcessorUnitTest::TestMatrix(random),
462 edgeType, caps,
joshualittd27f73e2014-12-29 07:43:36 -0800463 GrProcessorUnitTest::TestMatrix(random));
joshualittb0a8a372014-09-23 09:50:21 -0700464 } while (NULL == gp);
465 return gp;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000466}
467
468//////////////////////////////////////////////////////////////////////////////
469// Cubic
470//////////////////////////////////////////////////////////////////////////////
471
joshualitt9b989322014-12-15 14:16:27 -0800472struct CubicBatchTracker {
473 GrGPInput fInputColorType;
474 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -0800475 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800476};
477
joshualitt249af152014-09-15 11:41:13 -0700478class GrGLCubicEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000479public:
joshualitteb2a6762014-12-04 11:35:33 -0800480 GrGLCubicEffect(const GrGeometryProcessor&,
joshualitt87f48d92014-12-04 10:41:40 -0800481 const GrBatchTracker&);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000482
mtklein36352bf2015-03-25 18:17:31 -0700483 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000484
joshualitt87f48d92014-12-04 10:41:40 -0800485 static inline void GenKey(const GrGeometryProcessor&,
486 const GrBatchTracker&,
487 const GrGLCaps&,
488 GrProcessorKeyBuilder*);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000489
joshualitt9b989322014-12-15 14:16:27 -0800490 virtual void setData(const GrGLProgramDataManager& pdman,
joshualittee2af952014-12-30 09:04:15 -0800491 const GrPrimitiveProcessor& primProc,
mtklein36352bf2015-03-25 18:17:31 -0700492 const GrBatchTracker& bt) override {
joshualittee2af952014-12-30 09:04:15 -0800493 this->setUniformViewMatrix(pdman, primProc.viewMatrix());
494
joshualitt9b989322014-12-15 14:16:27 -0800495 const CubicBatchTracker& local = bt.cast<CubicBatchTracker>();
496 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
497 GrGLfloat c[4];
498 GrColorToRGBAFloat(local.fColor, c);
499 pdman.set4fv(fColorUniform, 1, c);
500 fColor = local.fColor;
501 }
502 }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000503
504private:
joshualitt9b989322014-12-15 14:16:27 -0800505 GrColor fColor;
joshualittb0a8a372014-09-23 09:50:21 -0700506 GrPrimitiveEdgeType fEdgeType;
joshualitt9b989322014-12-15 14:16:27 -0800507 UniformHandle fColorUniform;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000508
joshualitt249af152014-09-15 11:41:13 -0700509 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000510};
skia.committer@gmail.com44a77c82013-08-23 07:01:29 +0000511
joshualitteb2a6762014-12-04 11:35:33 -0800512GrGLCubicEffect::GrGLCubicEffect(const GrGeometryProcessor& processor,
joshualitt9b989322014-12-15 14:16:27 -0800513 const GrBatchTracker&)
514 : fColor(GrColor_ILLEGAL) {
joshualittb0a8a372014-09-23 09:50:21 -0700515 const GrCubicEffect& ce = processor.cast<GrCubicEffect>();
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000516 fEdgeType = ce.getEdgeType();
517}
518
robertphillips46d36f02015-01-18 08:14:14 -0800519void GrGLCubicEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -0700520 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
joshualitt2dd1ae02014-12-03 06:24:10 -0800521 const GrCubicEffect& gp = args.fGP.cast<GrCubicEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800522 const CubicBatchTracker& local = args.fBT.cast<CubicBatchTracker>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800523
joshualittabb52a12015-01-13 15:02:10 -0800524 // emit attributes
525 vsBuilder->emitAttributes(gp);
526
joshualitt2dd1ae02014-12-03 06:24:10 -0800527 GrGLVertToFrag v(kVec4f_GrSLType);
bsalomonc0bd6482014-12-09 10:04:14 -0800528 args.fPB->addVarying("CubicCoeffs", &v, kHigh_GrSLPrecision);
joshualitt2dd1ae02014-12-03 06:24:10 -0800529 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), gp.inCubicCoeffs()->fName);
530
joshualitt9b989322014-12-15 14:16:27 -0800531 // Setup pass through color
532 this->setupColorPassThrough(args.fPB, local.fInputColorType, args.fOutputColor, NULL,
533 &fColorUniform);
534
joshualittabb52a12015-01-13 15:02:10 -0800535 // Setup position
joshualittdd219872015-02-12 14:48:42 -0800536 this->setupPosition(args.fPB, gpArgs, gp.inPosition()->fName, gp.viewMatrix());
joshualitt4973d9d2014-11-08 09:24:25 -0800537
joshualittabb52a12015-01-13 15:02:10 -0800538 // emit transforms with position
robertphillips46d36f02015-01-18 08:14:14 -0800539 this->emitTransforms(args.fPB, gpArgs->fPositionVar, gp.inPosition()->fName, gp.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -0800540 args.fTransformsIn, args.fTransformsOut);
541
joshualittc369e7c2014-10-22 10:56:26 -0700542 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
egdaniel9bd5bbf2014-08-29 11:12:30 -0700543
bsalomonc0bd6482014-12-09 10:04:14 -0800544 GrGLShaderVar edgeAlpha("edgeAlpha", kFloat_GrSLType, 0, kHigh_GrSLPrecision);
545 GrGLShaderVar dklmdx("dklmdx", kVec3f_GrSLType, 0, kHigh_GrSLPrecision);
546 GrGLShaderVar dklmdy("dklmdy", kVec3f_GrSLType, 0, kHigh_GrSLPrecision);
547 GrGLShaderVar dfdx("dfdx", kFloat_GrSLType, 0, kHigh_GrSLPrecision);
548 GrGLShaderVar dfdy("dfdy", kFloat_GrSLType, 0, kHigh_GrSLPrecision);
549 GrGLShaderVar gF("gF", kVec2f_GrSLType, 0, kHigh_GrSLPrecision);
550 GrGLShaderVar gFM("gFM", kFloat_GrSLType, 0, kHigh_GrSLPrecision);
551 GrGLShaderVar func("func", kFloat_GrSLType, 0, kHigh_GrSLPrecision);
egdaniel9bd5bbf2014-08-29 11:12:30 -0700552
553 fsBuilder->declAppend(edgeAlpha);
554 fsBuilder->declAppend(dklmdx);
555 fsBuilder->declAppend(dklmdy);
556 fsBuilder->declAppend(dfdx);
557 fsBuilder->declAppend(dfdy);
558 fsBuilder->declAppend(gF);
559 fsBuilder->declAppend(gFM);
560 fsBuilder->declAppend(func);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000561
562 switch (fEdgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700563 case kHairlineAA_GrProcessorEdgeType: {
joshualitt30ba4362014-08-21 20:18:45 -0700564 SkAssertResult(fsBuilder->enableFeature(
565 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
joshualitt74077b92014-10-24 11:26:03 -0700566 fsBuilder->codeAppendf("%s = dFdx(%s.xyz);", dklmdx.c_str(), v.fsIn());
567 fsBuilder->codeAppendf("%s = dFdy(%s.xyz);", dklmdy.c_str(), v.fsIn());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700568 fsBuilder->codeAppendf("%s = 3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
joshualitt74077b92014-10-24 11:26:03 -0700569 dfdx.c_str(), v.fsIn(), v.fsIn(), dklmdx.c_str(), v.fsIn(),
570 dklmdx.c_str(), v.fsIn(), dklmdx.c_str());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700571 fsBuilder->codeAppendf("%s = 3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
joshualitt74077b92014-10-24 11:26:03 -0700572 dfdy.c_str(), v.fsIn(), v.fsIn(), dklmdy.c_str(), v.fsIn(),
573 dklmdy.c_str(), v.fsIn(), dklmdy.c_str());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700574 fsBuilder->codeAppendf("%s = vec2(%s, %s);", gF.c_str(), dfdx.c_str(), dfdy.c_str());
575 fsBuilder->codeAppendf("%s = sqrt(dot(%s, %s));", gFM.c_str(), gF.c_str(), gF.c_str());
576 fsBuilder->codeAppendf("%s = %s.x * %s.x * %s.x - %s.y * %s.z;",
joshualitt74077b92014-10-24 11:26:03 -0700577 func.c_str(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700578 fsBuilder->codeAppendf("%s = abs(%s);", func.c_str(), func.c_str());
579 fsBuilder->codeAppendf("%s = %s / %s;",
580 edgeAlpha.c_str(), func.c_str(), gFM.c_str());
581 fsBuilder->codeAppendf("%s = max(1.0 - %s, 0.0);",
582 edgeAlpha.c_str(), edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000583 // Add line below for smooth cubic ramp
egdaniel9bd5bbf2014-08-29 11:12:30 -0700584 // fsBuilder->codeAppendf("%s = %s * %s * (3.0 - 2.0 * %s);",
585 // edgeAlpha.c_str(), edgeAlpha.c_str(), edgeAlpha.c_str(),
586 // edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000587 break;
588 }
joshualittb0a8a372014-09-23 09:50:21 -0700589 case kFillAA_GrProcessorEdgeType: {
joshualitt30ba4362014-08-21 20:18:45 -0700590 SkAssertResult(fsBuilder->enableFeature(
591 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
joshualitt74077b92014-10-24 11:26:03 -0700592 fsBuilder->codeAppendf("%s = dFdx(%s.xyz);", dklmdx.c_str(), v.fsIn());
593 fsBuilder->codeAppendf("%s = dFdy(%s.xyz);", dklmdy.c_str(), v.fsIn());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700594 fsBuilder->codeAppendf("%s ="
595 "3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
joshualitt74077b92014-10-24 11:26:03 -0700596 dfdx.c_str(), v.fsIn(), v.fsIn(), dklmdx.c_str(), v.fsIn(),
597 dklmdx.c_str(), v.fsIn(), dklmdx.c_str());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700598 fsBuilder->codeAppendf("%s = 3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
joshualitt74077b92014-10-24 11:26:03 -0700599 dfdy.c_str(), v.fsIn(), v.fsIn(), dklmdy.c_str(), v.fsIn(),
600 dklmdy.c_str(), v.fsIn(), dklmdy.c_str());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700601 fsBuilder->codeAppendf("%s = vec2(%s, %s);", gF.c_str(), dfdx.c_str(), dfdy.c_str());
602 fsBuilder->codeAppendf("%s = sqrt(dot(%s, %s));", gFM.c_str(), gF.c_str(), gF.c_str());
603 fsBuilder->codeAppendf("%s = %s.x * %s.x * %s.x - %s.y * %s.z;",
joshualitt74077b92014-10-24 11:26:03 -0700604 func.c_str(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700605 fsBuilder->codeAppendf("%s = %s / %s;",
606 edgeAlpha.c_str(), func.c_str(), gFM.c_str());
607 fsBuilder->codeAppendf("%s = clamp(1.0 - %s, 0.0, 1.0);",
608 edgeAlpha.c_str(), edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000609 // Add line below for smooth cubic ramp
egdaniel9bd5bbf2014-08-29 11:12:30 -0700610 // fsBuilder->codeAppendf("%s = %s * %s * (3.0 - 2.0 * %s);",
611 // edgeAlpha.c_str(), edgeAlpha.c_str(), edgeAlpha.c_str(),
612 // edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000613 break;
614 }
joshualittb0a8a372014-09-23 09:50:21 -0700615 case kFillBW_GrProcessorEdgeType: {
egdaniel9bd5bbf2014-08-29 11:12:30 -0700616 fsBuilder->codeAppendf("%s = %s.x * %s.x * %s.x - %s.y * %s.z;",
joshualitt74077b92014-10-24 11:26:03 -0700617 edgeAlpha.c_str(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn());
egdaniel9bd5bbf2014-08-29 11:12:30 -0700618 fsBuilder->codeAppendf("%s = float(%s < 0.0);", edgeAlpha.c_str(), edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000619 break;
620 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000621 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000622 SkFAIL("Shouldn't get here");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000623 }
624
joshualitt2dd1ae02014-12-03 06:24:10 -0800625
626 fsBuilder->codeAppendf("%s = vec4(%s);", args.fOutputCoverage, edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000627}
628
robertphillips46d36f02015-01-18 08:14:14 -0800629void GrGLCubicEffect::GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -0800630 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800631 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700632 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -0800633 const GrCubicEffect& ce = gp.cast<GrCubicEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800634 const CubicBatchTracker& local = bt.cast<CubicBatchTracker>();
bsalomon63e99f72014-07-21 08:03:14 -0700635 uint32_t key = ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2;
joshualitt9b989322014-12-15 14:16:27 -0800636 key |= kUniform_GrGPInput == local.fInputColorType ? 0x4 : 0x8;
robertphillips46d36f02015-01-18 08:14:14 -0800637 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x10 : 0x0;
638 key |= ComputePosKey(gp.viewMatrix()) << 5;
bsalomon63e99f72014-07-21 08:03:14 -0700639 b->add32(key);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000640}
641
642//////////////////////////////////////////////////////////////////////////////
643
644GrCubicEffect::~GrCubicEffect() {}
645
joshualitteb2a6762014-12-04 11:35:33 -0800646void GrCubicEffect::getGLProcessorKey(const GrBatchTracker& bt,
647 const GrGLCaps& caps,
648 GrProcessorKeyBuilder* b) const {
649 GrGLCubicEffect::GenKey(*this, bt, caps, b);
650}
651
joshualittabb52a12015-01-13 15:02:10 -0800652GrGLPrimitiveProcessor* GrCubicEffect::createGLInstance(const GrBatchTracker& bt,
653 const GrGLCaps&) const {
joshualitteb2a6762014-12-04 11:35:33 -0800654 return SkNEW_ARGS(GrGLCubicEffect, (*this, bt));
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000655}
656
joshualitt8059eb92014-12-29 15:10:07 -0800657GrCubicEffect::GrCubicEffect(GrColor color, const SkMatrix& viewMatrix,
658 GrPrimitiveEdgeType edgeType)
659 : INHERITED(color, viewMatrix), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800660 this->initClassID<GrCubicEffect>();
joshualitt71c92602015-01-14 08:12:47 -0800661 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
662 fInCubicCoeffs = &this->addVertexAttrib(Attribute("inCubicCoeffs",
joshualitt2dd1ae02014-12-03 06:24:10 -0800663 kVec4f_GrVertexAttribType));
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000664}
665
bsalomon0e08fc12014-10-15 08:19:04 -0700666bool GrCubicEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700667 const GrCubicEffect& ce = other.cast<GrCubicEffect>();
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000668 return (ce.fEdgeType == fEdgeType);
669}
670
joshualitt4d8da812015-01-28 12:53:54 -0800671void GrCubicEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -0800672 CubicBatchTracker* local = bt->cast<CubicBatchTracker>();
673 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -0800674 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800675}
676
joshualitt290c09b2014-12-19 13:45:20 -0800677bool GrCubicEffect::onCanMakeEqual(const GrBatchTracker& m,
678 const GrGeometryProcessor& that,
679 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800680 const CubicBatchTracker& mine = m.cast<CubicBatchTracker>();
681 const CubicBatchTracker& theirs = t.cast<CubicBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -0800682 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
683 that, theirs.fUsesLocalCoords) &&
684 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -0800685 theirs.fInputColorType, theirs.fColor);
686}
687
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000688//////////////////////////////////////////////////////////////////////////////
689
joshualittb0a8a372014-09-23 09:50:21 -0700690GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrCubicEffect);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000691
joshualittb0a8a372014-09-23 09:50:21 -0700692GrGeometryProcessor* GrCubicEffect::TestCreate(SkRandom* random,
693 GrContext*,
694 const GrDrawTargetCaps& caps,
695 GrTexture*[]) {
696 GrGeometryProcessor* gp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000697 do {
joshualittb0a8a372014-09-23 09:50:21 -0700698 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
699 random->nextULessThan(kGrProcessorEdgeTypeCnt));
joshualitt8059eb92014-12-29 15:10:07 -0800700 gp = GrCubicEffect::Create(GrRandomColor(random),
701 GrProcessorUnitTest::TestMatrix(random), edgeType, caps);
joshualittb0a8a372014-09-23 09:50:21 -0700702 } while (NULL == gp);
703 return gp;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000704}
egdaniel9bd5bbf2014-08-29 11:12:30 -0700705