blob: 13672da6ea2b094671f5cbb37700d3cb06eb8ac5 [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
egdaniele659a582015-11-13 09:55:43 -080010#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080011#include "glsl/GrGLSLProgramBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070012#include "glsl/GrGLSLProgramDataManager.h"
egdaniel64c47282015-11-13 06:54:19 -080013#include "glsl/GrGLSLUtil.h"
egdaniel0eafe792015-11-20 14:01:22 -080014#include "glsl/GrGLSLVarying.h"
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000015
egdaniele659a582015-11-13 09:55:43 -080016class GrGLConicEffect : public GrGLSLGeometryProcessor {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000017public:
joshualitt465283c2015-09-11 08:19:35 -070018 GrGLConicEffect(const GrGeometryProcessor&);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000019
mtklein36352bf2015-03-25 18:17:31 -070020 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000021
joshualitt87f48d92014-12-04 10:41:40 -080022 static inline void GenKey(const GrGeometryProcessor&,
jvanverthcfc18862015-04-28 08:48:20 -070023 const GrGLSLCaps&,
joshualitt87f48d92014-12-04 10:41:40 -080024 GrProcessorKeyBuilder*);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000025
egdaniel018fb622015-10-28 07:26:40 -070026 void setData(const GrGLSLProgramDataManager& pdman,
joshualitt465283c2015-09-11 08:19:35 -070027 const GrPrimitiveProcessor& primProc) override {
joshualitte578a952015-05-14 10:09:13 -070028 const GrConicEffect& ce = primProc.cast<GrConicEffect>();
joshualitt5559ca22015-05-21 15:50:36 -070029
30 if (!ce.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(ce.viewMatrix())) {
31 fViewMatrix = ce.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -070032 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -080033 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -070034 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
35 }
joshualittee2af952014-12-30 09:04:15 -080036
joshualittb8c241a2015-05-19 08:23:30 -070037 if (ce.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -070038 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -070039 GrColorToRGBAFloat(ce.color(), c);
joshualitt9b989322014-12-15 14:16:27 -080040 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -070041 fColor = ce.color();
joshualitt9b989322014-12-15 14:16:27 -080042 }
joshualittb8c241a2015-05-19 08:23:30 -070043
44 if (ce.coverageScale() != 0xff && ce.coverageScale() != fCoverageScale) {
45 pdman.set1f(fCoverageScaleUniform, GrNormalizeByteToFloat(ce.coverageScale()));
46 fCoverageScale = ce.coverageScale();
joshualitt9b989322014-12-15 14:16:27 -080047 }
48 }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000049
joshualitte3ababe2015-05-15 07:56:07 -070050 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -070051 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -070052 int index,
53 const SkTArray<const GrCoordTransform*, true>& transforms) override {
54 this->setTransformDataHelper<GrConicEffect>(primProc, pdman, index, transforms);
55 }
56
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000057private:
joshualitt5559ca22015-05-21 15:50:36 -070058 SkMatrix fViewMatrix;
joshualitt9b989322014-12-15 14:16:27 -080059 GrColor fColor;
60 uint8_t fCoverageScale;
joshualittb0a8a372014-09-23 09:50:21 -070061 GrPrimitiveEdgeType fEdgeType;
joshualitt9b989322014-12-15 14:16:27 -080062 UniformHandle fColorUniform;
63 UniformHandle fCoverageScaleUniform;
joshualitt5559ca22015-05-21 15:50:36 -070064 UniformHandle fViewMatrixUniform;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000065
egdaniele659a582015-11-13 09:55:43 -080066 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000067};
skia.committer@gmail.com44a77c82013-08-23 07:01:29 +000068
joshualitt465283c2015-09-11 08:19:35 -070069GrGLConicEffect::GrGLConicEffect(const GrGeometryProcessor& processor)
joshualitt5559ca22015-05-21 15:50:36 -070070 : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL), fCoverageScale(0xff) {
joshualitt87f48d92014-12-04 10:41:40 -080071 const GrConicEffect& ce = processor.cast<GrConicEffect>();
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000072 fEdgeType = ce.getEdgeType();
73}
74
robertphillips46d36f02015-01-18 08:14:14 -080075void GrGLConicEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
egdaniel8dcdedc2015-11-11 06:27:20 -080076 GrGLSLGPBuilder* pb = args.fPB;
egdaniel4ca2e602015-11-18 08:01:26 -080077 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
joshualitt2dd1ae02014-12-03 06:24:10 -080078 const GrConicEffect& gp = args.fGP.cast<GrConicEffect>();
egdaniel0eafe792015-11-20 14:01:22 -080079 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
joshualitt2dd1ae02014-12-03 06:24:10 -080080
joshualittabb52a12015-01-13 15:02:10 -080081 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -080082 varyingHandler->emitAttributes(gp);
joshualittabb52a12015-01-13 15:02:10 -080083
egdaniel8dcdedc2015-11-11 06:27:20 -080084 GrGLSLVertToFrag v(kVec4f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -080085 varyingHandler->addVarying("ConicCoeffs", &v);
egdaniel4ca2e602015-11-18 08:01:26 -080086 vertBuilder->codeAppendf("%s = %s;", v.vsOut(), gp.inConicCoeffs()->fName);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000087
egdaniel4ca2e602015-11-18 08:01:26 -080088 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -080089 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -070090 if (!gp.colorIgnored()) {
egdaniel4ca2e602015-11-18 08:01:26 -080091 this->setupUniformColor(args.fPB, fragBuilder, args.fOutputColor, &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -070092 }
joshualitt9b989322014-12-15 14:16:27 -080093
joshualittabb52a12015-01-13 15:02:10 -080094 // Setup position
egdaniel4ca2e602015-11-18 08:01:26 -080095 this->setupPosition(pb,
96 vertBuilder,
97 gpArgs,
98 gp.inPosition()->fName,
99 gp.viewMatrix(),
100 &fViewMatrixUniform);
joshualitt4973d9d2014-11-08 09:24:25 -0800101
joshualittabb52a12015-01-13 15:02:10 -0800102 // emit transforms with position
egdaniel4ca2e602015-11-18 08:01:26 -0800103 this->emitTransforms(pb,
104 vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800105 varyingHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800106 gpArgs->fPositionVar,
107 gp.inPosition()->fName,
108 gp.localMatrix(),
109 args.fTransformsIn,
110 args.fTransformsOut);
joshualittabb52a12015-01-13 15:02:10 -0800111
egdaniel4ca2e602015-11-18 08:01:26 -0800112 fragBuilder->codeAppend("float edgeAlpha;");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000113
114 switch (fEdgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700115 case kHairlineAA_GrProcessorEdgeType: {
egdaniel4ca2e602015-11-18 08:01:26 -0800116 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800117 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
egdaniel4ca2e602015-11-18 08:01:26 -0800118 fragBuilder->codeAppendf("vec3 dklmdx = dFdx(%s.xyz);", v.fsIn());
119 fragBuilder->codeAppendf("vec3 dklmdy = dFdy(%s.xyz);", v.fsIn());
120 fragBuilder->codeAppendf("float dfdx ="
121 "2.0 * %s.x * dklmdx.x - %s.y * dklmdx.z - %s.z * dklmdx.y;",
122 v.fsIn(), v.fsIn(), v.fsIn());
123 fragBuilder->codeAppendf("float dfdy ="
124 "2.0 * %s.x * dklmdy.x - %s.y * dklmdy.z - %s.z * dklmdy.y;",
125 v.fsIn(), v.fsIn(), v.fsIn());
126 fragBuilder->codeAppend("vec2 gF = vec2(dfdx, dfdy);");
127 fragBuilder->codeAppend("float gFM = sqrt(dot(gF, gF));");
128 fragBuilder->codeAppendf("float func = %s.x*%s.x - %s.y*%s.z;", v.fsIn(), v.fsIn(),
129 v.fsIn(), v.fsIn());
130 fragBuilder->codeAppend("func = abs(func);");
131 fragBuilder->codeAppend("edgeAlpha = func / gFM;");
132 fragBuilder->codeAppend("edgeAlpha = max(1.0 - edgeAlpha, 0.0);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000133 // Add line below for smooth cubic ramp
egdaniel4ca2e602015-11-18 08:01:26 -0800134 // fragBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000135 break;
136 }
joshualittb0a8a372014-09-23 09:50:21 -0700137 case kFillAA_GrProcessorEdgeType: {
egdaniel4ca2e602015-11-18 08:01:26 -0800138 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800139 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
egdaniel4ca2e602015-11-18 08:01:26 -0800140 fragBuilder->codeAppendf("vec3 dklmdx = dFdx(%s.xyz);", v.fsIn());
141 fragBuilder->codeAppendf("vec3 dklmdy = dFdy(%s.xyz);", v.fsIn());
142 fragBuilder->codeAppendf("float dfdx ="
143 "2.0 * %s.x * dklmdx.x - %s.y * dklmdx.z - %s.z * dklmdx.y;",
144 v.fsIn(), v.fsIn(), v.fsIn());
145 fragBuilder->codeAppendf("float dfdy ="
146 "2.0 * %s.x * dklmdy.x - %s.y * dklmdy.z - %s.z * dklmdy.y;",
147 v.fsIn(), v.fsIn(), v.fsIn());
148 fragBuilder->codeAppend("vec2 gF = vec2(dfdx, dfdy);");
149 fragBuilder->codeAppend("float gFM = sqrt(dot(gF, gF));");
150 fragBuilder->codeAppendf("float func = %s.x * %s.x - %s.y * %s.z;", v.fsIn(), v.fsIn(),
151 v.fsIn(), v.fsIn());
152 fragBuilder->codeAppend("edgeAlpha = func / gFM;");
153 fragBuilder->codeAppend("edgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1.0);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000154 // Add line below for smooth cubic ramp
egdaniel4ca2e602015-11-18 08:01:26 -0800155 // fragBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000156 break;
157 }
joshualittb0a8a372014-09-23 09:50:21 -0700158 case kFillBW_GrProcessorEdgeType: {
egdaniel4ca2e602015-11-18 08:01:26 -0800159 fragBuilder->codeAppendf("edgeAlpha = %s.x * %s.x - %s.y * %s.z;", v.fsIn(), v.fsIn(),
160 v.fsIn(), v.fsIn());
161 fragBuilder->codeAppend("edgeAlpha = float(edgeAlpha < 0.0);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000162 break;
163 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000164 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000165 SkFAIL("Shouldn't get here");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000166 }
167
joshualittb8c241a2015-05-19 08:23:30 -0700168 // TODO should we really be doing this?
169 if (gp.coverageScale() != 0xff) {
joshualitt9b989322014-12-15 14:16:27 -0800170 const char* coverageScale;
egdaniel2d721d32015-11-11 13:06:05 -0800171 fCoverageScaleUniform = pb->addUniform(GrGLSLProgramBuilder::kFragment_Visibility,
joshualittdd219872015-02-12 14:48:42 -0800172 kFloat_GrSLType,
173 kDefault_GrSLPrecision,
174 "Coverage",
175 &coverageScale);
egdaniel4ca2e602015-11-18 08:01:26 -0800176 fragBuilder->codeAppendf("%s = vec4(%s * edgeAlpha);", args.fOutputCoverage, coverageScale);
joshualitt9b989322014-12-15 14:16:27 -0800177 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800178 fragBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
joshualitt9b989322014-12-15 14:16:27 -0800179 }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000180}
181
robertphillips46d36f02015-01-18 08:14:14 -0800182void GrGLConicEffect::GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700183 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700184 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -0800185 const GrConicEffect& ce = gp.cast<GrConicEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700186 uint32_t key = ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2;
joshualittb8c241a2015-05-19 08:23:30 -0700187 key |= GrColor_ILLEGAL != ce.color() ? 0x4 : 0x0;
188 key |= 0xff != ce.coverageScale() ? 0x8 : 0x0;
189 key |= ce.usesLocalCoords() && ce.localMatrix().hasPerspective() ? 0x10 : 0x0;
joshualitte578a952015-05-14 10:09:13 -0700190 key |= ComputePosKey(ce.viewMatrix()) << 5;
bsalomon63e99f72014-07-21 08:03:14 -0700191 b->add32(key);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000192}
193
194//////////////////////////////////////////////////////////////////////////////
195
196GrConicEffect::~GrConicEffect() {}
197
egdaniel57d3b032015-11-13 11:57:27 -0800198void GrConicEffect::getGLSLProcessorKey(const GrGLSLCaps& caps,
199 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700200 GrGLConicEffect::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800201}
202
egdaniel57d3b032015-11-13 11:57:27 -0800203GrGLSLPrimitiveProcessor* GrConicEffect::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700204 return new GrGLConicEffect(*this);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000205}
206
joshualitt8059eb92014-12-29 15:10:07 -0800207GrConicEffect::GrConicEffect(GrColor color, const SkMatrix& viewMatrix, uint8_t coverage,
joshualittb8c241a2015-05-19 08:23:30 -0700208 GrPrimitiveEdgeType edgeType, const SkMatrix& localMatrix,
209 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700210 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700211 , fViewMatrix(viewMatrix)
joshualitte3ababe2015-05-15 07:56:07 -0700212 , fLocalMatrix(viewMatrix)
joshualittb8c241a2015-05-19 08:23:30 -0700213 , fUsesLocalCoords(usesLocalCoords)
joshualitt8059eb92014-12-29 15:10:07 -0800214 , fCoverageScale(coverage)
215 , fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800216 this->initClassID<GrConicEffect>();
senorblancof2539d52015-05-20 14:03:42 -0700217 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
218 kHigh_GrSLPrecision));
joshualitt71c92602015-01-14 08:12:47 -0800219 fInConicCoeffs = &this->addVertexAttrib(Attribute("inConicCoeffs",
joshualittb8c241a2015-05-19 08:23:30 -0700220 kVec4f_GrVertexAttribType));
joshualitt9b989322014-12-15 14:16:27 -0800221}
222
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000223//////////////////////////////////////////////////////////////////////////////
224
joshualittb0a8a372014-09-23 09:50:21 -0700225GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrConicEffect);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000226
bsalomonc21b09e2015-08-28 18:46:56 -0700227const GrGeometryProcessor* GrConicEffect::TestCreate(GrProcessorTestData* d) {
joshualittb0a8a372014-09-23 09:50:21 -0700228 GrGeometryProcessor* gp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000229 do {
joshualitt0067ff52015-07-08 14:26:19 -0700230 GrPrimitiveEdgeType edgeType =
231 static_cast<GrPrimitiveEdgeType>(
232 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
233 gp = GrConicEffect::Create(GrRandomColor(d->fRandom), GrTest::TestMatrix(d->fRandom),
234 edgeType, *d->fCaps,
235 GrTest::TestMatrix(d->fRandom), d->fRandom->nextBool());
halcanary96fcdcc2015-08-27 07:41:13 -0700236 } while (nullptr == gp);
joshualittb0a8a372014-09-23 09:50:21 -0700237 return gp;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000238}
239
240//////////////////////////////////////////////////////////////////////////////
241// Quad
242//////////////////////////////////////////////////////////////////////////////
243
egdaniele659a582015-11-13 09:55:43 -0800244class GrGLQuadEffect : public GrGLSLGeometryProcessor {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000245public:
joshualitt465283c2015-09-11 08:19:35 -0700246 GrGLQuadEffect(const GrGeometryProcessor&);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000247
mtklein36352bf2015-03-25 18:17:31 -0700248 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000249
joshualitt87f48d92014-12-04 10:41:40 -0800250 static inline void GenKey(const GrGeometryProcessor&,
jvanverthcfc18862015-04-28 08:48:20 -0700251 const GrGLSLCaps&,
joshualitt87f48d92014-12-04 10:41:40 -0800252 GrProcessorKeyBuilder*);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000253
egdaniel018fb622015-10-28 07:26:40 -0700254 void setData(const GrGLSLProgramDataManager& pdman,
joshualitt465283c2015-09-11 08:19:35 -0700255 const GrPrimitiveProcessor& primProc) override {
joshualitte578a952015-05-14 10:09:13 -0700256 const GrQuadEffect& qe = primProc.cast<GrQuadEffect>();
joshualitt5559ca22015-05-21 15:50:36 -0700257
258 if (!qe.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(qe.viewMatrix())) {
259 fViewMatrix = qe.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700260 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800261 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700262 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
263 }
joshualittee2af952014-12-30 09:04:15 -0800264
joshualittb8c241a2015-05-19 08:23:30 -0700265 if (qe.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -0700266 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -0700267 GrColorToRGBAFloat(qe.color(), c);
joshualitt9b989322014-12-15 14:16:27 -0800268 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -0700269 fColor = qe.color();
joshualitt9b989322014-12-15 14:16:27 -0800270 }
joshualittb8c241a2015-05-19 08:23:30 -0700271
272 if (qe.coverageScale() != 0xff && qe.coverageScale() != fCoverageScale) {
273 pdman.set1f(fCoverageScaleUniform, GrNormalizeByteToFloat(qe.coverageScale()));
274 fCoverageScale = qe.coverageScale();
joshualitt9b989322014-12-15 14:16:27 -0800275 }
276 }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000277
joshualitte3ababe2015-05-15 07:56:07 -0700278 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -0700279 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -0700280 int index,
281 const SkTArray<const GrCoordTransform*, true>& transforms) override {
282 this->setTransformDataHelper<GrQuadEffect>(primProc, pdman, index, transforms);
283 }
284
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000285private:
joshualitt5559ca22015-05-21 15:50:36 -0700286 SkMatrix fViewMatrix;
joshualitt9b989322014-12-15 14:16:27 -0800287 GrColor fColor;
288 uint8_t fCoverageScale;
joshualittb0a8a372014-09-23 09:50:21 -0700289 GrPrimitiveEdgeType fEdgeType;
joshualitt9b989322014-12-15 14:16:27 -0800290 UniformHandle fColorUniform;
291 UniformHandle fCoverageScaleUniform;
joshualitt5559ca22015-05-21 15:50:36 -0700292 UniformHandle fViewMatrixUniform;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000293
egdaniele659a582015-11-13 09:55:43 -0800294 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000295};
skia.committer@gmail.com44a77c82013-08-23 07:01:29 +0000296
joshualitt465283c2015-09-11 08:19:35 -0700297GrGLQuadEffect::GrGLQuadEffect(const GrGeometryProcessor& processor)
joshualitt5559ca22015-05-21 15:50:36 -0700298 : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL), fCoverageScale(0xff) {
joshualitt87f48d92014-12-04 10:41:40 -0800299 const GrQuadEffect& ce = processor.cast<GrQuadEffect>();
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000300 fEdgeType = ce.getEdgeType();
301}
302
robertphillips46d36f02015-01-18 08:14:14 -0800303void GrGLQuadEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
egdaniel8dcdedc2015-11-11 06:27:20 -0800304 GrGLSLGPBuilder* pb = args.fPB;
egdaniel4ca2e602015-11-18 08:01:26 -0800305 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
joshualitt2dd1ae02014-12-03 06:24:10 -0800306 const GrQuadEffect& gp = args.fGP.cast<GrQuadEffect>();
egdaniel0eafe792015-11-20 14:01:22 -0800307 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
joshualitt2dd1ae02014-12-03 06:24:10 -0800308
joshualittabb52a12015-01-13 15:02:10 -0800309 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800310 varyingHandler->emitAttributes(gp);
joshualittabb52a12015-01-13 15:02:10 -0800311
egdaniel8dcdedc2015-11-11 06:27:20 -0800312 GrGLSLVertToFrag v(kVec4f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800313 varyingHandler->addVarying("HairQuadEdge", &v);
egdaniel4ca2e602015-11-18 08:01:26 -0800314 vertBuilder->codeAppendf("%s = %s;", v.vsOut(), gp.inHairQuadEdge()->fName);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000315
egdaniel4ca2e602015-11-18 08:01:26 -0800316 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -0800317 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -0700318 if (!gp.colorIgnored()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800319 this->setupUniformColor(args.fPB, fragBuilder, args.fOutputColor, &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -0700320 }
joshualitt9b989322014-12-15 14:16:27 -0800321
joshualittabb52a12015-01-13 15:02:10 -0800322 // Setup position
egdaniel4ca2e602015-11-18 08:01:26 -0800323 this->setupPosition(pb,
324 vertBuilder,
325 gpArgs,
326 gp.inPosition()->fName,
327 gp.viewMatrix(),
328 &fViewMatrixUniform);
joshualitt4973d9d2014-11-08 09:24:25 -0800329
joshualittabb52a12015-01-13 15:02:10 -0800330 // emit transforms with position
egdaniel4ca2e602015-11-18 08:01:26 -0800331 this->emitTransforms(pb,
332 vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800333 varyingHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800334 gpArgs->fPositionVar,
335 gp.inPosition()->fName,
336 gp.localMatrix(),
337 args.fTransformsIn,
338 args.fTransformsOut);
joshualittabb52a12015-01-13 15:02:10 -0800339
egdaniel4ca2e602015-11-18 08:01:26 -0800340 fragBuilder->codeAppendf("float edgeAlpha;");
joshualitt30ba4362014-08-21 20:18:45 -0700341
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000342 switch (fEdgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700343 case kHairlineAA_GrProcessorEdgeType: {
egdaniel4ca2e602015-11-18 08:01:26 -0800344 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800345 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
egdaniel4ca2e602015-11-18 08:01:26 -0800346 fragBuilder->codeAppendf("vec2 duvdx = dFdx(%s.xy);", v.fsIn());
347 fragBuilder->codeAppendf("vec2 duvdy = dFdy(%s.xy);", v.fsIn());
348 fragBuilder->codeAppendf("vec2 gF = vec2(2.0 * %s.x * duvdx.x - duvdx.y,"
349 " 2.0 * %s.x * duvdy.x - duvdy.y);",
350 v.fsIn(), v.fsIn());
351 fragBuilder->codeAppendf("edgeAlpha = (%s.x * %s.x - %s.y);",
352 v.fsIn(), v.fsIn(), v.fsIn());
353 fragBuilder->codeAppend("edgeAlpha = sqrt(edgeAlpha * edgeAlpha / dot(gF, gF));");
354 fragBuilder->codeAppend("edgeAlpha = max(1.0 - edgeAlpha, 0.0);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000355 // Add line below for smooth cubic ramp
egdaniel4ca2e602015-11-18 08:01:26 -0800356 // fragBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000357 break;
358 }
joshualittb0a8a372014-09-23 09:50:21 -0700359 case kFillAA_GrProcessorEdgeType: {
egdaniel4ca2e602015-11-18 08:01:26 -0800360 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800361 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
egdaniel4ca2e602015-11-18 08:01:26 -0800362 fragBuilder->codeAppendf("vec2 duvdx = dFdx(%s.xy);", v.fsIn());
363 fragBuilder->codeAppendf("vec2 duvdy = dFdy(%s.xy);", v.fsIn());
364 fragBuilder->codeAppendf("vec2 gF = vec2(2.0 * %s.x * duvdx.x - duvdx.y,"
365 " 2.0 * %s.x * duvdy.x - duvdy.y);",
366 v.fsIn(), v.fsIn());
367 fragBuilder->codeAppendf("edgeAlpha = (%s.x * %s.x - %s.y);",
368 v.fsIn(), v.fsIn(), v.fsIn());
369 fragBuilder->codeAppend("edgeAlpha = edgeAlpha / sqrt(dot(gF, gF));");
370 fragBuilder->codeAppend("edgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1.0);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000371 // Add line below for smooth cubic ramp
egdaniel4ca2e602015-11-18 08:01:26 -0800372 // fragBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000373 break;
374 }
joshualittb0a8a372014-09-23 09:50:21 -0700375 case kFillBW_GrProcessorEdgeType: {
egdaniel4ca2e602015-11-18 08:01:26 -0800376 fragBuilder->codeAppendf("edgeAlpha = (%s.x * %s.x - %s.y);",
377 v.fsIn(), v.fsIn(), v.fsIn());
378 fragBuilder->codeAppend("edgeAlpha = float(edgeAlpha < 0.0);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000379 break;
380 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000381 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000382 SkFAIL("Shouldn't get here");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000383 }
384
joshualittb8c241a2015-05-19 08:23:30 -0700385 if (0xff != gp.coverageScale()) {
joshualitt9b989322014-12-15 14:16:27 -0800386 const char* coverageScale;
egdaniel2d721d32015-11-11 13:06:05 -0800387 fCoverageScaleUniform = pb->addUniform(GrGLSLProgramBuilder::kFragment_Visibility,
egdaniel4ca2e602015-11-18 08:01:26 -0800388 kFloat_GrSLType,
389 kDefault_GrSLPrecision,
390 "Coverage",
391 &coverageScale);
392 fragBuilder->codeAppendf("%s = vec4(%s * edgeAlpha);", args.fOutputCoverage, coverageScale);
joshualitt9b989322014-12-15 14:16:27 -0800393 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800394 fragBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
joshualitt9b989322014-12-15 14:16:27 -0800395 }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000396}
397
robertphillips46d36f02015-01-18 08:14:14 -0800398void GrGLQuadEffect::GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700399 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700400 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -0800401 const GrQuadEffect& ce = gp.cast<GrQuadEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700402 uint32_t key = ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2;
joshualittb8c241a2015-05-19 08:23:30 -0700403 key |= ce.color() != GrColor_ILLEGAL ? 0x4 : 0x0;
404 key |= ce.coverageScale() != 0xff ? 0x8 : 0x0;
405 key |= ce.usesLocalCoords() && ce.localMatrix().hasPerspective() ? 0x10 : 0x0;
joshualitte578a952015-05-14 10:09:13 -0700406 key |= ComputePosKey(ce.viewMatrix()) << 5;
bsalomon63e99f72014-07-21 08:03:14 -0700407 b->add32(key);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000408}
409
410//////////////////////////////////////////////////////////////////////////////
411
412GrQuadEffect::~GrQuadEffect() {}
413
egdaniel57d3b032015-11-13 11:57:27 -0800414void GrQuadEffect::getGLSLProcessorKey(const GrGLSLCaps& caps,
415 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700416 GrGLQuadEffect::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800417}
418
egdaniel57d3b032015-11-13 11:57:27 -0800419GrGLSLPrimitiveProcessor* GrQuadEffect::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700420 return new GrGLQuadEffect(*this);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000421}
422
joshualitt8059eb92014-12-29 15:10:07 -0800423GrQuadEffect::GrQuadEffect(GrColor color, const SkMatrix& viewMatrix, uint8_t coverage,
joshualittb8c241a2015-05-19 08:23:30 -0700424 GrPrimitiveEdgeType edgeType, const SkMatrix& localMatrix,
425 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700426 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700427 , fViewMatrix(viewMatrix)
joshualitte3ababe2015-05-15 07:56:07 -0700428 , fLocalMatrix(localMatrix)
joshualittb8c241a2015-05-19 08:23:30 -0700429 , fUsesLocalCoords(usesLocalCoords)
joshualitt8059eb92014-12-29 15:10:07 -0800430 , fCoverageScale(coverage)
431 , fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800432 this->initClassID<GrQuadEffect>();
senorblancof2539d52015-05-20 14:03:42 -0700433 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
434 kHigh_GrSLPrecision));
joshualitt71c92602015-01-14 08:12:47 -0800435 fInHairQuadEdge = &this->addVertexAttrib(Attribute("inHairQuadEdge",
joshualitt2dd1ae02014-12-03 06:24:10 -0800436 kVec4f_GrVertexAttribType));
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000437}
438
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000439//////////////////////////////////////////////////////////////////////////////
440
joshualittb0a8a372014-09-23 09:50:21 -0700441GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrQuadEffect);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000442
bsalomonc21b09e2015-08-28 18:46:56 -0700443const GrGeometryProcessor* GrQuadEffect::TestCreate(GrProcessorTestData* d) {
joshualittb0a8a372014-09-23 09:50:21 -0700444 GrGeometryProcessor* gp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000445 do {
joshualittb0a8a372014-09-23 09:50:21 -0700446 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
joshualitt0067ff52015-07-08 14:26:19 -0700447 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
448 gp = GrQuadEffect::Create(GrRandomColor(d->fRandom),
449 GrTest::TestMatrix(d->fRandom),
450 edgeType, *d->fCaps,
451 GrTest::TestMatrix(d->fRandom),
452 d->fRandom->nextBool());
halcanary96fcdcc2015-08-27 07:41:13 -0700453 } while (nullptr == gp);
joshualittb0a8a372014-09-23 09:50:21 -0700454 return gp;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000455}
456
457//////////////////////////////////////////////////////////////////////////////
458// Cubic
459//////////////////////////////////////////////////////////////////////////////
460
egdaniele659a582015-11-13 09:55:43 -0800461class GrGLCubicEffect : public GrGLSLGeometryProcessor {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000462public:
joshualitt465283c2015-09-11 08:19:35 -0700463 GrGLCubicEffect(const GrGeometryProcessor&);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000464
mtklein36352bf2015-03-25 18:17:31 -0700465 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000466
joshualitt87f48d92014-12-04 10:41:40 -0800467 static inline void GenKey(const GrGeometryProcessor&,
jvanverthcfc18862015-04-28 08:48:20 -0700468 const GrGLSLCaps&,
joshualitt87f48d92014-12-04 10:41:40 -0800469 GrProcessorKeyBuilder*);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000470
egdaniel018fb622015-10-28 07:26:40 -0700471 void setData(const GrGLSLProgramDataManager& pdman,
joshualitt465283c2015-09-11 08:19:35 -0700472 const GrPrimitiveProcessor& primProc) override {
joshualitte578a952015-05-14 10:09:13 -0700473 const GrCubicEffect& ce = primProc.cast<GrCubicEffect>();
joshualitt5559ca22015-05-21 15:50:36 -0700474
475 if (!ce.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(ce.viewMatrix())) {
476 fViewMatrix = ce.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700477 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800478 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700479 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
480 }
joshualittee2af952014-12-30 09:04:15 -0800481
joshualittb8c241a2015-05-19 08:23:30 -0700482 if (ce.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -0700483 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -0700484 GrColorToRGBAFloat(ce.color(), c);
joshualitt9b989322014-12-15 14:16:27 -0800485 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -0700486 fColor = ce.color();
joshualitt9b989322014-12-15 14:16:27 -0800487 }
488 }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000489
490private:
joshualitt5559ca22015-05-21 15:50:36 -0700491 SkMatrix fViewMatrix;
joshualitt9b989322014-12-15 14:16:27 -0800492 GrColor fColor;
joshualittb0a8a372014-09-23 09:50:21 -0700493 GrPrimitiveEdgeType fEdgeType;
joshualitt9b989322014-12-15 14:16:27 -0800494 UniformHandle fColorUniform;
joshualitt5559ca22015-05-21 15:50:36 -0700495 UniformHandle fViewMatrixUniform;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000496
egdaniele659a582015-11-13 09:55:43 -0800497 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000498};
skia.committer@gmail.com44a77c82013-08-23 07:01:29 +0000499
joshualitt465283c2015-09-11 08:19:35 -0700500GrGLCubicEffect::GrGLCubicEffect(const GrGeometryProcessor& processor)
joshualitt5559ca22015-05-21 15:50:36 -0700501 : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL) {
joshualittb0a8a372014-09-23 09:50:21 -0700502 const GrCubicEffect& ce = processor.cast<GrCubicEffect>();
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000503 fEdgeType = ce.getEdgeType();
504}
505
robertphillips46d36f02015-01-18 08:14:14 -0800506void GrGLCubicEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
egdaniel4ca2e602015-11-18 08:01:26 -0800507 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
joshualitt2dd1ae02014-12-03 06:24:10 -0800508 const GrCubicEffect& gp = args.fGP.cast<GrCubicEffect>();
egdaniel0eafe792015-11-20 14:01:22 -0800509 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
joshualitt2dd1ae02014-12-03 06:24:10 -0800510
joshualittabb52a12015-01-13 15:02:10 -0800511 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800512 varyingHandler->emitAttributes(gp);
joshualittabb52a12015-01-13 15:02:10 -0800513
egdaniel8dcdedc2015-11-11 06:27:20 -0800514 GrGLSLVertToFrag v(kVec4f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800515 varyingHandler->addVarying("CubicCoeffs", &v, kHigh_GrSLPrecision);
egdaniel4ca2e602015-11-18 08:01:26 -0800516 vertBuilder->codeAppendf("%s = %s;", v.vsOut(), gp.inCubicCoeffs()->fName);
joshualitt2dd1ae02014-12-03 06:24:10 -0800517
egdaniel4ca2e602015-11-18 08:01:26 -0800518 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -0800519 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -0700520 if (!gp.colorIgnored()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800521 this->setupUniformColor(args.fPB, fragBuilder, args.fOutputColor, &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -0700522 }
joshualitt9b989322014-12-15 14:16:27 -0800523
joshualittabb52a12015-01-13 15:02:10 -0800524 // Setup position
egdaniel4ca2e602015-11-18 08:01:26 -0800525 this->setupPosition(args.fPB,
526 vertBuilder,
527 gpArgs,
528 gp.inPosition()->fName,
529 gp.viewMatrix(),
joshualitt5559ca22015-05-21 15:50:36 -0700530 &fViewMatrixUniform);
joshualitt4973d9d2014-11-08 09:24:25 -0800531
joshualittabb52a12015-01-13 15:02:10 -0800532 // emit transforms with position
egdaniel4ca2e602015-11-18 08:01:26 -0800533 this->emitTransforms(args.fPB,
534 vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800535 varyingHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800536 gpArgs->fPositionVar,
537 gp.inPosition()->fName,
538 args.fTransformsIn,
joshualitte3ababe2015-05-15 07:56:07 -0700539 args.fTransformsOut);
joshualittabb52a12015-01-13 15:02:10 -0800540
egdaniel9bd5bbf2014-08-29 11:12:30 -0700541
egdaniel0d3f0612015-10-21 10:45:48 -0700542 GrGLSLShaderVar edgeAlpha("edgeAlpha", kFloat_GrSLType, 0, kHigh_GrSLPrecision);
543 GrGLSLShaderVar dklmdx("dklmdx", kVec3f_GrSLType, 0, kHigh_GrSLPrecision);
544 GrGLSLShaderVar dklmdy("dklmdy", kVec3f_GrSLType, 0, kHigh_GrSLPrecision);
545 GrGLSLShaderVar dfdx("dfdx", kFloat_GrSLType, 0, kHigh_GrSLPrecision);
546 GrGLSLShaderVar dfdy("dfdy", kFloat_GrSLType, 0, kHigh_GrSLPrecision);
547 GrGLSLShaderVar gF("gF", kVec2f_GrSLType, 0, kHigh_GrSLPrecision);
548 GrGLSLShaderVar gFM("gFM", kFloat_GrSLType, 0, kHigh_GrSLPrecision);
549 GrGLSLShaderVar func("func", kFloat_GrSLType, 0, kHigh_GrSLPrecision);
egdaniel9bd5bbf2014-08-29 11:12:30 -0700550
egdaniel4ca2e602015-11-18 08:01:26 -0800551 fragBuilder->declAppend(edgeAlpha);
552 fragBuilder->declAppend(dklmdx);
553 fragBuilder->declAppend(dklmdy);
554 fragBuilder->declAppend(dfdx);
555 fragBuilder->declAppend(dfdy);
556 fragBuilder->declAppend(gF);
557 fragBuilder->declAppend(gFM);
558 fragBuilder->declAppend(func);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000559
560 switch (fEdgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700561 case kHairlineAA_GrProcessorEdgeType: {
egdaniel4ca2e602015-11-18 08:01:26 -0800562 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800563 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
egdaniel4ca2e602015-11-18 08:01:26 -0800564 fragBuilder->codeAppendf("%s = dFdx(%s.xyz);", dklmdx.c_str(), v.fsIn());
565 fragBuilder->codeAppendf("%s = dFdy(%s.xyz);", dklmdy.c_str(), v.fsIn());
566 fragBuilder->codeAppendf("%s = 3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
567 dfdx.c_str(), v.fsIn(), v.fsIn(), dklmdx.c_str(), v.fsIn(),
568 dklmdx.c_str(), v.fsIn(), dklmdx.c_str());
569 fragBuilder->codeAppendf("%s = 3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
570 dfdy.c_str(), v.fsIn(), v.fsIn(), dklmdy.c_str(), v.fsIn(),
571 dklmdy.c_str(), v.fsIn(), dklmdy.c_str());
572 fragBuilder->codeAppendf("%s = vec2(%s, %s);", gF.c_str(), dfdx.c_str(), dfdy.c_str());
573 fragBuilder->codeAppendf("%s = sqrt(dot(%s, %s));",
574 gFM.c_str(), gF.c_str(), gF.c_str());
575 fragBuilder->codeAppendf("%s = %s.x * %s.x * %s.x - %s.y * %s.z;",
576 func.c_str(), v.fsIn(), v.fsIn(),
577 v.fsIn(), v.fsIn(), v.fsIn());
578 fragBuilder->codeAppendf("%s = abs(%s);", func.c_str(), func.c_str());
579 fragBuilder->codeAppendf("%s = %s / %s;",
580 edgeAlpha.c_str(), func.c_str(), gFM.c_str());
581 fragBuilder->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
egdaniel4ca2e602015-11-18 08:01:26 -0800584 // fragBuilder->codeAppendf("%s = %s * %s * (3.0 - 2.0 * %s);",
egdaniel9bd5bbf2014-08-29 11:12:30 -0700585 // 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: {
egdaniel4ca2e602015-11-18 08:01:26 -0800590 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800591 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
egdaniel4ca2e602015-11-18 08:01:26 -0800592 fragBuilder->codeAppendf("%s = dFdx(%s.xyz);", dklmdx.c_str(), v.fsIn());
593 fragBuilder->codeAppendf("%s = dFdy(%s.xyz);", dklmdy.c_str(), v.fsIn());
594 fragBuilder->codeAppendf("%s ="
595 "3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
596 dfdx.c_str(), v.fsIn(), v.fsIn(), dklmdx.c_str(), v.fsIn(),
597 dklmdx.c_str(), v.fsIn(), dklmdx.c_str());
598 fragBuilder->codeAppendf("%s = 3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
599 dfdy.c_str(), v.fsIn(), v.fsIn(), dklmdy.c_str(), v.fsIn(),
600 dklmdy.c_str(), v.fsIn(), dklmdy.c_str());
601 fragBuilder->codeAppendf("%s = vec2(%s, %s);", gF.c_str(), dfdx.c_str(), dfdy.c_str());
602 fragBuilder->codeAppendf("%s = sqrt(dot(%s, %s));",
603 gFM.c_str(), gF.c_str(), gF.c_str());
604 fragBuilder->codeAppendf("%s = %s.x * %s.x * %s.x - %s.y * %s.z;",
605 func.c_str(), v.fsIn(), v.fsIn(),
606 v.fsIn(), v.fsIn(), v.fsIn());
607 fragBuilder->codeAppendf("%s = %s / %s;",
608 edgeAlpha.c_str(), func.c_str(), gFM.c_str());
609 fragBuilder->codeAppendf("%s = clamp(1.0 - %s, 0.0, 1.0);",
610 edgeAlpha.c_str(), edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000611 // Add line below for smooth cubic ramp
egdaniel4ca2e602015-11-18 08:01:26 -0800612 // fragBuilder->codeAppendf("%s = %s * %s * (3.0 - 2.0 * %s);",
egdaniel9bd5bbf2014-08-29 11:12:30 -0700613 // edgeAlpha.c_str(), edgeAlpha.c_str(), edgeAlpha.c_str(),
614 // edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000615 break;
616 }
joshualittb0a8a372014-09-23 09:50:21 -0700617 case kFillBW_GrProcessorEdgeType: {
egdaniel4ca2e602015-11-18 08:01:26 -0800618 fragBuilder->codeAppendf("%s = %s.x * %s.x * %s.x - %s.y * %s.z;",
619 edgeAlpha.c_str(), v.fsIn(), v.fsIn(),
620 v.fsIn(), v.fsIn(), v.fsIn());
621 fragBuilder->codeAppendf("%s = float(%s < 0.0);", edgeAlpha.c_str(), edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000622 break;
623 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000624 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000625 SkFAIL("Shouldn't get here");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000626 }
627
joshualitt2dd1ae02014-12-03 06:24:10 -0800628
egdaniel4ca2e602015-11-18 08:01:26 -0800629 fragBuilder->codeAppendf("%s = vec4(%s);", args.fOutputCoverage, edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000630}
631
robertphillips46d36f02015-01-18 08:14:14 -0800632void GrGLCubicEffect::GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700633 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700634 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -0800635 const GrCubicEffect& ce = gp.cast<GrCubicEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700636 uint32_t key = ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2;
joshualittb8c241a2015-05-19 08:23:30 -0700637 key |= ce.color() != GrColor_ILLEGAL ? 0x4 : 0x8;
joshualitte578a952015-05-14 10:09:13 -0700638 key |= ComputePosKey(ce.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
egdaniel57d3b032015-11-13 11:57:27 -0800646void GrCubicEffect::getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700647 GrGLCubicEffect::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800648}
649
egdaniel57d3b032015-11-13 11:57:27 -0800650GrGLSLPrimitiveProcessor* GrCubicEffect::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700651 return new GrGLCubicEffect(*this);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000652}
653
joshualitt8059eb92014-12-29 15:10:07 -0800654GrCubicEffect::GrCubicEffect(GrColor color, const SkMatrix& viewMatrix,
655 GrPrimitiveEdgeType edgeType)
joshualitte578a952015-05-14 10:09:13 -0700656 : fColor(color)
657 , fViewMatrix(viewMatrix)
joshualitt88c23fc2015-05-13 14:18:07 -0700658 , fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800659 this->initClassID<GrCubicEffect>();
senorblancof2539d52015-05-20 14:03:42 -0700660 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
661 kHigh_GrSLPrecision));
joshualitt71c92602015-01-14 08:12:47 -0800662 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
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000666//////////////////////////////////////////////////////////////////////////////
667
joshualittb0a8a372014-09-23 09:50:21 -0700668GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrCubicEffect);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000669
bsalomonc21b09e2015-08-28 18:46:56 -0700670const GrGeometryProcessor* GrCubicEffect::TestCreate(GrProcessorTestData* d) {
joshualittb0a8a372014-09-23 09:50:21 -0700671 GrGeometryProcessor* gp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000672 do {
joshualitt0067ff52015-07-08 14:26:19 -0700673 GrPrimitiveEdgeType edgeType =
674 static_cast<GrPrimitiveEdgeType>(
675 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
676 gp = GrCubicEffect::Create(GrRandomColor(d->fRandom),
677 GrTest::TestMatrix(d->fRandom), edgeType, *d->fCaps);
halcanary96fcdcc2015-08-27 07:41:13 -0700678 } while (nullptr == gp);
joshualittb0a8a372014-09-23 09:50:21 -0700679 return gp;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000680}
egdaniel9bd5bbf2014-08-29 11:12:30 -0700681