blob: 1894192d5888b25fb8cd7792d452e5bc815159a1 [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
egdaniel7ea439b2015-12-03 09:20:44 -080010#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniele659a582015-11-13 09:55:43 -080011#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070012#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080013#include "glsl/GrGLSLUniformHandler.h"
egdaniel64c47282015-11-13 06:54:19 -080014#include "glsl/GrGLSLUtil.h"
egdaniel0eafe792015-11-20 14:01:22 -080015#include "glsl/GrGLSLVarying.h"
egdaniel7ea439b2015-12-03 09:20:44 -080016#include "glsl/GrGLSLVertexShaderBuilder.h"
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000017
egdaniele659a582015-11-13 09:55:43 -080018class GrGLConicEffect : public GrGLSLGeometryProcessor {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000019public:
joshualitt465283c2015-09-11 08:19:35 -070020 GrGLConicEffect(const GrGeometryProcessor&);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000021
mtklein36352bf2015-03-25 18:17:31 -070022 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000023
joshualitt87f48d92014-12-04 10:41:40 -080024 static inline void GenKey(const GrGeometryProcessor&,
jvanverthcfc18862015-04-28 08:48:20 -070025 const GrGLSLCaps&,
joshualitt87f48d92014-12-04 10:41:40 -080026 GrProcessorKeyBuilder*);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000027
bsalomon56b7dc42016-09-19 09:26:40 -070028 void setData(const GrGLSLProgramDataManager& pdman,
29 const GrPrimitiveProcessor& primProc) override {
joshualitte578a952015-05-14 10:09:13 -070030 const GrConicEffect& ce = primProc.cast<GrConicEffect>();
joshualitt5559ca22015-05-21 15:50:36 -070031
32 if (!ce.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(ce.viewMatrix())) {
33 fViewMatrix = ce.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -070034 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -080035 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -070036 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
37 }
joshualittee2af952014-12-30 09:04:15 -080038
joshualittb8c241a2015-05-19 08:23:30 -070039 if (ce.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -070040 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -070041 GrColorToRGBAFloat(ce.color(), c);
joshualitt9b989322014-12-15 14:16:27 -080042 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -070043 fColor = ce.color();
joshualitt9b989322014-12-15 14:16:27 -080044 }
joshualittb8c241a2015-05-19 08:23:30 -070045
46 if (ce.coverageScale() != 0xff && ce.coverageScale() != fCoverageScale) {
47 pdman.set1f(fCoverageScaleUniform, GrNormalizeByteToFloat(ce.coverageScale()));
48 fCoverageScale = ce.coverageScale();
joshualitt9b989322014-12-15 14:16:27 -080049 }
bsalomon56b7dc42016-09-19 09:26:40 -070050 }
51
52 void setTransformData(const GrPrimitiveProcessor& primProc,
53 const GrGLSLProgramDataManager& pdman,
54 int index,
55 const SkTArray<const GrCoordTransform*, true>& transforms) override {
56 this->setTransformDataHelper(primProc.cast<GrConicEffect>().localMatrix(), pdman, index,
57 transforms);
joshualitte3ababe2015-05-15 07:56:07 -070058 }
59
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000060private:
joshualitt5559ca22015-05-21 15:50:36 -070061 SkMatrix fViewMatrix;
joshualitt9b989322014-12-15 14:16:27 -080062 GrColor fColor;
63 uint8_t fCoverageScale;
joshualittb0a8a372014-09-23 09:50:21 -070064 GrPrimitiveEdgeType fEdgeType;
joshualitt9b989322014-12-15 14:16:27 -080065 UniformHandle fColorUniform;
66 UniformHandle fCoverageScaleUniform;
joshualitt5559ca22015-05-21 15:50:36 -070067 UniformHandle fViewMatrixUniform;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000068
egdaniele659a582015-11-13 09:55:43 -080069 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000070};
skia.committer@gmail.com44a77c82013-08-23 07:01:29 +000071
joshualitt465283c2015-09-11 08:19:35 -070072GrGLConicEffect::GrGLConicEffect(const GrGeometryProcessor& processor)
joshualitt5559ca22015-05-21 15:50:36 -070073 : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL), fCoverageScale(0xff) {
joshualitt87f48d92014-12-04 10:41:40 -080074 const GrConicEffect& ce = processor.cast<GrConicEffect>();
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000075 fEdgeType = ce.getEdgeType();
76}
77
robertphillips46d36f02015-01-18 08:14:14 -080078void GrGLConicEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
egdaniel4ca2e602015-11-18 08:01:26 -080079 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
joshualitt2dd1ae02014-12-03 06:24:10 -080080 const GrConicEffect& gp = args.fGP.cast<GrConicEffect>();
egdaniel0eafe792015-11-20 14:01:22 -080081 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -080082 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualitt2dd1ae02014-12-03 06:24:10 -080083
joshualittabb52a12015-01-13 15:02:10 -080084 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -080085 varyingHandler->emitAttributes(gp);
joshualittabb52a12015-01-13 15:02:10 -080086
egdaniel8dcdedc2015-11-11 06:27:20 -080087 GrGLSLVertToFrag v(kVec4f_GrSLType);
robertphillips96afa522015-12-09 07:54:24 -080088 varyingHandler->addVarying("ConicCoeffs", &v, kHigh_GrSLPrecision);
egdaniel4ca2e602015-11-18 08:01:26 -080089 vertBuilder->codeAppendf("%s = %s;", v.vsOut(), gp.inConicCoeffs()->fName);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000090
cdalton85285412016-02-18 12:37:07 -080091 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -080092 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -070093 if (!gp.colorIgnored()) {
egdaniel7ea439b2015-12-03 09:20:44 -080094 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -070095 }
joshualitt9b989322014-12-15 14:16:27 -080096
joshualittabb52a12015-01-13 15:02:10 -080097 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -080098 this->setupPosition(vertBuilder,
99 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800100 gpArgs,
101 gp.inPosition()->fName,
102 gp.viewMatrix(),
103 &fViewMatrixUniform);
joshualitt4973d9d2014-11-08 09:24:25 -0800104
joshualittabb52a12015-01-13 15:02:10 -0800105 // emit transforms with position
egdaniel7ea439b2015-12-03 09:20:44 -0800106 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800107 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800108 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800109 gpArgs->fPositionVar,
110 gp.inPosition()->fName,
111 gp.localMatrix(),
bsalomon56b7dc42016-09-19 09:26:40 -0700112 args.fTransformsIn,
113 args.fTransformsOut);
joshualittabb52a12015-01-13 15:02:10 -0800114
robertphillips2eb10092015-12-11 04:59:36 -0800115 // TODO: this precision check should actually be a check on the number of bits
116 // high and medium provide and the selection of the lowest level that suffices.
117 // Additionally we should assert that the upstream code only lets us get here if
118 // either high or medium provides the required number of bits.
119 GrSLPrecision precision = kHigh_GrSLPrecision;
120 const GrShaderCaps::PrecisionInfo& highP = args.fGLSLCaps->getFloatShaderPrecisionInfo(
121 kFragment_GrShaderType,
122 kHigh_GrSLPrecision);
123 if (!highP.supported()) {
124 precision = kMedium_GrSLPrecision;
125 }
126
127 GrGLSLShaderVar edgeAlpha("edgeAlpha", kFloat_GrSLType, 0, precision);
128 GrGLSLShaderVar dklmdx("dklmdx", kVec3f_GrSLType, 0, precision);
129 GrGLSLShaderVar dklmdy("dklmdy", kVec3f_GrSLType, 0, precision);
130 GrGLSLShaderVar dfdx("dfdx", kFloat_GrSLType, 0, precision);
131 GrGLSLShaderVar dfdy("dfdy", kFloat_GrSLType, 0, precision);
132 GrGLSLShaderVar gF("gF", kVec2f_GrSLType, 0, precision);
133 GrGLSLShaderVar gFM("gFM", kFloat_GrSLType, 0, precision);
134 GrGLSLShaderVar func("func", kFloat_GrSLType, 0, precision);
robertphillips96afa522015-12-09 07:54:24 -0800135
136 fragBuilder->declAppend(edgeAlpha);
137 fragBuilder->declAppend(dklmdx);
138 fragBuilder->declAppend(dklmdy);
139 fragBuilder->declAppend(dfdx);
140 fragBuilder->declAppend(dfdy);
141 fragBuilder->declAppend(gF);
142 fragBuilder->declAppend(gFM);
143 fragBuilder->declAppend(func);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000144
145 switch (fEdgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700146 case kHairlineAA_GrProcessorEdgeType: {
egdaniel4ca2e602015-11-18 08:01:26 -0800147 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800148 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
robertphillips96afa522015-12-09 07:54:24 -0800149 fragBuilder->codeAppendf("%s = dFdx(%s.xyz);", dklmdx.c_str(), v.fsIn());
150 fragBuilder->codeAppendf("%s = dFdy(%s.xyz);", dklmdy.c_str(), v.fsIn());
151 fragBuilder->codeAppendf("%s = 2.0 * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
152 dfdx.c_str(),
153 v.fsIn(), dklmdx.c_str(),
154 v.fsIn(), dklmdx.c_str(),
155 v.fsIn(), dklmdx.c_str());
156 fragBuilder->codeAppendf("%s = 2.0 * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
157 dfdy.c_str(),
158 v.fsIn(), dklmdy.c_str(),
159 v.fsIn(), dklmdy.c_str(),
160 v.fsIn(), dklmdy.c_str());
161 fragBuilder->codeAppendf("%s = vec2(%s, %s);", gF.c_str(), dfdx.c_str(), dfdy.c_str());
162 fragBuilder->codeAppendf("%s = sqrt(dot(%s, %s));",
163 gFM.c_str(), gF.c_str(), gF.c_str());
164 fragBuilder->codeAppendf("%s = %s.x*%s.x - %s.y*%s.z;",
165 func.c_str(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn());
166 fragBuilder->codeAppendf("%s = abs(%s);", func.c_str(), func.c_str());
167 fragBuilder->codeAppendf("%s = %s / %s;",
168 edgeAlpha.c_str(), func.c_str(), gFM.c_str());
169 fragBuilder->codeAppendf("%s = max(1.0 - %s, 0.0);",
170 edgeAlpha.c_str(), edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000171 // Add line below for smooth cubic ramp
egdaniel4ca2e602015-11-18 08:01:26 -0800172 // fragBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000173 break;
174 }
joshualittb0a8a372014-09-23 09:50:21 -0700175 case kFillAA_GrProcessorEdgeType: {
egdaniel4ca2e602015-11-18 08:01:26 -0800176 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800177 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
robertphillips96afa522015-12-09 07:54:24 -0800178 fragBuilder->codeAppendf("%s = dFdx(%s.xyz);", dklmdx.c_str(), v.fsIn());
179 fragBuilder->codeAppendf("%s = dFdy(%s.xyz);", dklmdy.c_str(), v.fsIn());
180 fragBuilder->codeAppendf("%s ="
181 "2.0 * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
182 dfdx.c_str(),
183 v.fsIn(), dklmdx.c_str(),
184 v.fsIn(), dklmdx.c_str(),
185 v.fsIn(), dklmdx.c_str());
186 fragBuilder->codeAppendf("%s ="
187 "2.0 * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
188 dfdy.c_str(),
189 v.fsIn(), dklmdy.c_str(),
190 v.fsIn(), dklmdy.c_str(),
191 v.fsIn(), dklmdy.c_str());
192 fragBuilder->codeAppendf("%s = vec2(%s, %s);", gF.c_str(), dfdx.c_str(), dfdy.c_str());
193 fragBuilder->codeAppendf("%s = sqrt(dot(%s, %s));",
194 gFM.c_str(), gF.c_str(), gF.c_str());
195 fragBuilder->codeAppendf("%s = %s.x * %s.x - %s.y * %s.z;",
196 func.c_str(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn());
197 fragBuilder->codeAppendf("%s = %s / %s;",
198 edgeAlpha.c_str(), func.c_str(), gFM.c_str());
199 fragBuilder->codeAppendf("%s = clamp(1.0 - %s, 0.0, 1.0);",
200 edgeAlpha.c_str(), edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000201 // Add line below for smooth cubic ramp
egdaniel4ca2e602015-11-18 08:01:26 -0800202 // fragBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000203 break;
204 }
joshualittb0a8a372014-09-23 09:50:21 -0700205 case kFillBW_GrProcessorEdgeType: {
robertphillips96afa522015-12-09 07:54:24 -0800206 fragBuilder->codeAppendf("%s = %s.x * %s.x - %s.y * %s.z;",
207 edgeAlpha.c_str(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn());
208 fragBuilder->codeAppendf("%s = float(%s < 0.0);",
209 edgeAlpha.c_str(), edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000210 break;
211 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000212 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000213 SkFAIL("Shouldn't get here");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000214 }
215
joshualittb8c241a2015-05-19 08:23:30 -0700216 // TODO should we really be doing this?
217 if (gp.coverageScale() != 0xff) {
joshualitt9b989322014-12-15 14:16:27 -0800218 const char* coverageScale;
cdalton5e58cee2016-02-11 12:49:47 -0800219 fCoverageScaleUniform = uniformHandler->addUniform(kFragment_GrShaderFlag,
220 kFloat_GrSLType,
221 kHigh_GrSLPrecision,
222 "Coverage",
223 &coverageScale);
robertphillips96afa522015-12-09 07:54:24 -0800224 fragBuilder->codeAppendf("%s = vec4(%s * %s);",
225 args.fOutputCoverage, coverageScale, edgeAlpha.c_str());
joshualitt9b989322014-12-15 14:16:27 -0800226 } else {
robertphillips96afa522015-12-09 07:54:24 -0800227 fragBuilder->codeAppendf("%s = vec4(%s);", args.fOutputCoverage, edgeAlpha.c_str());
joshualitt9b989322014-12-15 14:16:27 -0800228 }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000229}
230
robertphillips46d36f02015-01-18 08:14:14 -0800231void GrGLConicEffect::GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700232 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700233 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -0800234 const GrConicEffect& ce = gp.cast<GrConicEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700235 uint32_t key = ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2;
joshualittb8c241a2015-05-19 08:23:30 -0700236 key |= GrColor_ILLEGAL != ce.color() ? 0x4 : 0x0;
237 key |= 0xff != ce.coverageScale() ? 0x8 : 0x0;
238 key |= ce.usesLocalCoords() && ce.localMatrix().hasPerspective() ? 0x10 : 0x0;
joshualitte578a952015-05-14 10:09:13 -0700239 key |= ComputePosKey(ce.viewMatrix()) << 5;
bsalomon63e99f72014-07-21 08:03:14 -0700240 b->add32(key);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000241}
242
243//////////////////////////////////////////////////////////////////////////////
244
245GrConicEffect::~GrConicEffect() {}
246
egdaniel57d3b032015-11-13 11:57:27 -0800247void GrConicEffect::getGLSLProcessorKey(const GrGLSLCaps& caps,
248 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700249 GrGLConicEffect::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800250}
251
egdaniel57d3b032015-11-13 11:57:27 -0800252GrGLSLPrimitiveProcessor* GrConicEffect::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700253 return new GrGLConicEffect(*this);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000254}
255
joshualitt8059eb92014-12-29 15:10:07 -0800256GrConicEffect::GrConicEffect(GrColor color, const SkMatrix& viewMatrix, uint8_t coverage,
joshualittb8c241a2015-05-19 08:23:30 -0700257 GrPrimitiveEdgeType edgeType, const SkMatrix& localMatrix,
258 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700259 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700260 , fViewMatrix(viewMatrix)
joshualitte3ababe2015-05-15 07:56:07 -0700261 , fLocalMatrix(viewMatrix)
joshualittb8c241a2015-05-19 08:23:30 -0700262 , fUsesLocalCoords(usesLocalCoords)
joshualitt8059eb92014-12-29 15:10:07 -0800263 , fCoverageScale(coverage)
264 , fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800265 this->initClassID<GrConicEffect>();
bsalomon6cb807b2016-08-17 11:33:39 -0700266 fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType,
267 kHigh_GrSLPrecision);
268 fInConicCoeffs = &this->addVertexAttrib("inConicCoeffs", kVec4f_GrVertexAttribType);
joshualitt9b989322014-12-15 14:16:27 -0800269}
270
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000271//////////////////////////////////////////////////////////////////////////////
272
joshualittb0a8a372014-09-23 09:50:21 -0700273GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrConicEffect);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000274
bungeman06ca8ec2016-06-09 08:01:03 -0700275sk_sp<GrGeometryProcessor> GrConicEffect::TestCreate(GrProcessorTestData* d) {
276 sk_sp<GrGeometryProcessor> gp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000277 do {
joshualitt0067ff52015-07-08 14:26:19 -0700278 GrPrimitiveEdgeType edgeType =
279 static_cast<GrPrimitiveEdgeType>(
280 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
bungeman06ca8ec2016-06-09 08:01:03 -0700281 gp = GrConicEffect::Make(GrRandomColor(d->fRandom), GrTest::TestMatrix(d->fRandom),
282 edgeType, *d->fCaps,
283 GrTest::TestMatrix(d->fRandom), d->fRandom->nextBool());
halcanary96fcdcc2015-08-27 07:41:13 -0700284 } while (nullptr == gp);
joshualittb0a8a372014-09-23 09:50:21 -0700285 return gp;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000286}
287
288//////////////////////////////////////////////////////////////////////////////
289// Quad
290//////////////////////////////////////////////////////////////////////////////
291
egdaniele659a582015-11-13 09:55:43 -0800292class GrGLQuadEffect : public GrGLSLGeometryProcessor {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000293public:
joshualitt465283c2015-09-11 08:19:35 -0700294 GrGLQuadEffect(const GrGeometryProcessor&);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000295
mtklein36352bf2015-03-25 18:17:31 -0700296 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000297
joshualitt87f48d92014-12-04 10:41:40 -0800298 static inline void GenKey(const GrGeometryProcessor&,
jvanverthcfc18862015-04-28 08:48:20 -0700299 const GrGLSLCaps&,
joshualitt87f48d92014-12-04 10:41:40 -0800300 GrProcessorKeyBuilder*);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000301
bsalomon56b7dc42016-09-19 09:26:40 -0700302 void setData(const GrGLSLProgramDataManager& pdman,
303 const GrPrimitiveProcessor& primProc) override {
joshualitte578a952015-05-14 10:09:13 -0700304 const GrQuadEffect& qe = primProc.cast<GrQuadEffect>();
joshualitt5559ca22015-05-21 15:50:36 -0700305
306 if (!qe.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(qe.viewMatrix())) {
307 fViewMatrix = qe.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700308 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800309 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700310 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
311 }
joshualittee2af952014-12-30 09:04:15 -0800312
joshualittb8c241a2015-05-19 08:23:30 -0700313 if (qe.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -0700314 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -0700315 GrColorToRGBAFloat(qe.color(), c);
joshualitt9b989322014-12-15 14:16:27 -0800316 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -0700317 fColor = qe.color();
joshualitt9b989322014-12-15 14:16:27 -0800318 }
joshualittb8c241a2015-05-19 08:23:30 -0700319
320 if (qe.coverageScale() != 0xff && qe.coverageScale() != fCoverageScale) {
321 pdman.set1f(fCoverageScaleUniform, GrNormalizeByteToFloat(qe.coverageScale()));
322 fCoverageScale = qe.coverageScale();
joshualitt9b989322014-12-15 14:16:27 -0800323 }
bsalomon56b7dc42016-09-19 09:26:40 -0700324 }
325
326 void setTransformData(const GrPrimitiveProcessor& primProc,
327 const GrGLSLProgramDataManager& pdman,
328 int index,
329 const SkTArray<const GrCoordTransform*, true>& transforms) override {
330 this->setTransformDataHelper(primProc.cast<GrQuadEffect>().localMatrix(), pdman, index,
331 transforms);
joshualitte3ababe2015-05-15 07:56:07 -0700332 }
333
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000334private:
joshualitt5559ca22015-05-21 15:50:36 -0700335 SkMatrix fViewMatrix;
joshualitt9b989322014-12-15 14:16:27 -0800336 GrColor fColor;
337 uint8_t fCoverageScale;
joshualittb0a8a372014-09-23 09:50:21 -0700338 GrPrimitiveEdgeType fEdgeType;
joshualitt9b989322014-12-15 14:16:27 -0800339 UniformHandle fColorUniform;
340 UniformHandle fCoverageScaleUniform;
joshualitt5559ca22015-05-21 15:50:36 -0700341 UniformHandle fViewMatrixUniform;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000342
egdaniele659a582015-11-13 09:55:43 -0800343 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000344};
skia.committer@gmail.com44a77c82013-08-23 07:01:29 +0000345
joshualitt465283c2015-09-11 08:19:35 -0700346GrGLQuadEffect::GrGLQuadEffect(const GrGeometryProcessor& processor)
joshualitt5559ca22015-05-21 15:50:36 -0700347 : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL), fCoverageScale(0xff) {
joshualitt87f48d92014-12-04 10:41:40 -0800348 const GrQuadEffect& ce = processor.cast<GrQuadEffect>();
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000349 fEdgeType = ce.getEdgeType();
350}
351
robertphillips46d36f02015-01-18 08:14:14 -0800352void GrGLQuadEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
egdaniel4ca2e602015-11-18 08:01:26 -0800353 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
joshualitt2dd1ae02014-12-03 06:24:10 -0800354 const GrQuadEffect& gp = args.fGP.cast<GrQuadEffect>();
egdaniel0eafe792015-11-20 14:01:22 -0800355 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800356 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualitt2dd1ae02014-12-03 06:24:10 -0800357
joshualittabb52a12015-01-13 15:02:10 -0800358 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800359 varyingHandler->emitAttributes(gp);
joshualittabb52a12015-01-13 15:02:10 -0800360
egdaniel8dcdedc2015-11-11 06:27:20 -0800361 GrGLSLVertToFrag v(kVec4f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800362 varyingHandler->addVarying("HairQuadEdge", &v);
egdaniel4ca2e602015-11-18 08:01:26 -0800363 vertBuilder->codeAppendf("%s = %s;", v.vsOut(), gp.inHairQuadEdge()->fName);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000364
cdalton85285412016-02-18 12:37:07 -0800365 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -0800366 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -0700367 if (!gp.colorIgnored()) {
egdaniel7ea439b2015-12-03 09:20:44 -0800368 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -0700369 }
joshualitt9b989322014-12-15 14:16:27 -0800370
joshualittabb52a12015-01-13 15:02:10 -0800371 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -0800372 this->setupPosition(vertBuilder,
373 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800374 gpArgs,
375 gp.inPosition()->fName,
376 gp.viewMatrix(),
377 &fViewMatrixUniform);
joshualitt4973d9d2014-11-08 09:24:25 -0800378
joshualittabb52a12015-01-13 15:02:10 -0800379 // emit transforms with position
egdaniel7ea439b2015-12-03 09:20:44 -0800380 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800381 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800382 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800383 gpArgs->fPositionVar,
384 gp.inPosition()->fName,
385 gp.localMatrix(),
bsalomon56b7dc42016-09-19 09:26:40 -0700386 args.fTransformsIn,
387 args.fTransformsOut);
joshualittabb52a12015-01-13 15:02:10 -0800388
egdaniel4ca2e602015-11-18 08:01:26 -0800389 fragBuilder->codeAppendf("float edgeAlpha;");
joshualitt30ba4362014-08-21 20:18:45 -0700390
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000391 switch (fEdgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700392 case kHairlineAA_GrProcessorEdgeType: {
egdaniel4ca2e602015-11-18 08:01:26 -0800393 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800394 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
egdaniel4ca2e602015-11-18 08:01:26 -0800395 fragBuilder->codeAppendf("vec2 duvdx = dFdx(%s.xy);", v.fsIn());
396 fragBuilder->codeAppendf("vec2 duvdy = dFdy(%s.xy);", v.fsIn());
397 fragBuilder->codeAppendf("vec2 gF = vec2(2.0 * %s.x * duvdx.x - duvdx.y,"
398 " 2.0 * %s.x * duvdy.x - duvdy.y);",
399 v.fsIn(), v.fsIn());
400 fragBuilder->codeAppendf("edgeAlpha = (%s.x * %s.x - %s.y);",
401 v.fsIn(), v.fsIn(), v.fsIn());
402 fragBuilder->codeAppend("edgeAlpha = sqrt(edgeAlpha * edgeAlpha / dot(gF, gF));");
403 fragBuilder->codeAppend("edgeAlpha = max(1.0 - edgeAlpha, 0.0);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000404 // Add line below for smooth cubic ramp
egdaniel4ca2e602015-11-18 08:01:26 -0800405 // fragBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000406 break;
407 }
joshualittb0a8a372014-09-23 09:50:21 -0700408 case kFillAA_GrProcessorEdgeType: {
egdaniel4ca2e602015-11-18 08:01:26 -0800409 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800410 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
egdaniel4ca2e602015-11-18 08:01:26 -0800411 fragBuilder->codeAppendf("vec2 duvdx = dFdx(%s.xy);", v.fsIn());
412 fragBuilder->codeAppendf("vec2 duvdy = dFdy(%s.xy);", v.fsIn());
413 fragBuilder->codeAppendf("vec2 gF = vec2(2.0 * %s.x * duvdx.x - duvdx.y,"
414 " 2.0 * %s.x * duvdy.x - duvdy.y);",
415 v.fsIn(), v.fsIn());
416 fragBuilder->codeAppendf("edgeAlpha = (%s.x * %s.x - %s.y);",
417 v.fsIn(), v.fsIn(), v.fsIn());
418 fragBuilder->codeAppend("edgeAlpha = edgeAlpha / sqrt(dot(gF, gF));");
419 fragBuilder->codeAppend("edgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1.0);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000420 // Add line below for smooth cubic ramp
egdaniel4ca2e602015-11-18 08:01:26 -0800421 // fragBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000422 break;
423 }
joshualittb0a8a372014-09-23 09:50:21 -0700424 case kFillBW_GrProcessorEdgeType: {
egdaniel4ca2e602015-11-18 08:01:26 -0800425 fragBuilder->codeAppendf("edgeAlpha = (%s.x * %s.x - %s.y);",
426 v.fsIn(), v.fsIn(), v.fsIn());
427 fragBuilder->codeAppend("edgeAlpha = float(edgeAlpha < 0.0);");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000428 break;
429 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000430 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000431 SkFAIL("Shouldn't get here");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000432 }
433
joshualittb8c241a2015-05-19 08:23:30 -0700434 if (0xff != gp.coverageScale()) {
joshualitt9b989322014-12-15 14:16:27 -0800435 const char* coverageScale;
cdalton5e58cee2016-02-11 12:49:47 -0800436 fCoverageScaleUniform = uniformHandler->addUniform(kFragment_GrShaderFlag,
437 kFloat_GrSLType,
438 kDefault_GrSLPrecision,
439 "Coverage",
440 &coverageScale);
egdaniel4ca2e602015-11-18 08:01:26 -0800441 fragBuilder->codeAppendf("%s = vec4(%s * edgeAlpha);", args.fOutputCoverage, coverageScale);
joshualitt9b989322014-12-15 14:16:27 -0800442 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800443 fragBuilder->codeAppendf("%s = vec4(edgeAlpha);", args.fOutputCoverage);
joshualitt9b989322014-12-15 14:16:27 -0800444 }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000445}
446
robertphillips46d36f02015-01-18 08:14:14 -0800447void GrGLQuadEffect::GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700448 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700449 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -0800450 const GrQuadEffect& ce = gp.cast<GrQuadEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700451 uint32_t key = ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2;
joshualittb8c241a2015-05-19 08:23:30 -0700452 key |= ce.color() != GrColor_ILLEGAL ? 0x4 : 0x0;
453 key |= ce.coverageScale() != 0xff ? 0x8 : 0x0;
454 key |= ce.usesLocalCoords() && ce.localMatrix().hasPerspective() ? 0x10 : 0x0;
joshualitte578a952015-05-14 10:09:13 -0700455 key |= ComputePosKey(ce.viewMatrix()) << 5;
bsalomon63e99f72014-07-21 08:03:14 -0700456 b->add32(key);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000457}
458
459//////////////////////////////////////////////////////////////////////////////
460
461GrQuadEffect::~GrQuadEffect() {}
462
egdaniel57d3b032015-11-13 11:57:27 -0800463void GrQuadEffect::getGLSLProcessorKey(const GrGLSLCaps& caps,
464 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700465 GrGLQuadEffect::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800466}
467
egdaniel57d3b032015-11-13 11:57:27 -0800468GrGLSLPrimitiveProcessor* GrQuadEffect::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700469 return new GrGLQuadEffect(*this);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000470}
471
joshualitt8059eb92014-12-29 15:10:07 -0800472GrQuadEffect::GrQuadEffect(GrColor color, const SkMatrix& viewMatrix, uint8_t coverage,
joshualittb8c241a2015-05-19 08:23:30 -0700473 GrPrimitiveEdgeType edgeType, const SkMatrix& localMatrix,
474 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700475 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700476 , fViewMatrix(viewMatrix)
joshualitte3ababe2015-05-15 07:56:07 -0700477 , fLocalMatrix(localMatrix)
joshualittb8c241a2015-05-19 08:23:30 -0700478 , fUsesLocalCoords(usesLocalCoords)
joshualitt8059eb92014-12-29 15:10:07 -0800479 , fCoverageScale(coverage)
480 , fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800481 this->initClassID<GrQuadEffect>();
bsalomon6cb807b2016-08-17 11:33:39 -0700482 fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType,
483 kHigh_GrSLPrecision);
484 fInHairQuadEdge = &this->addVertexAttrib("inHairQuadEdge", kVec4f_GrVertexAttribType);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000485}
486
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000487//////////////////////////////////////////////////////////////////////////////
488
joshualittb0a8a372014-09-23 09:50:21 -0700489GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrQuadEffect);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000490
bungeman06ca8ec2016-06-09 08:01:03 -0700491sk_sp<GrGeometryProcessor> GrQuadEffect::TestCreate(GrProcessorTestData* d) {
492 sk_sp<GrGeometryProcessor> gp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000493 do {
joshualittb0a8a372014-09-23 09:50:21 -0700494 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
joshualitt0067ff52015-07-08 14:26:19 -0700495 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
bungeman06ca8ec2016-06-09 08:01:03 -0700496 gp = GrQuadEffect::Make(GrRandomColor(d->fRandom),
497 GrTest::TestMatrix(d->fRandom),
498 edgeType, *d->fCaps,
499 GrTest::TestMatrix(d->fRandom),
500 d->fRandom->nextBool());
halcanary96fcdcc2015-08-27 07:41:13 -0700501 } while (nullptr == gp);
joshualittb0a8a372014-09-23 09:50:21 -0700502 return gp;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000503}
504
505//////////////////////////////////////////////////////////////////////////////
506// Cubic
507//////////////////////////////////////////////////////////////////////////////
508
egdaniele659a582015-11-13 09:55:43 -0800509class GrGLCubicEffect : public GrGLSLGeometryProcessor {
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000510public:
joshualitt465283c2015-09-11 08:19:35 -0700511 GrGLCubicEffect(const GrGeometryProcessor&);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000512
mtklein36352bf2015-03-25 18:17:31 -0700513 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000514
joshualitt87f48d92014-12-04 10:41:40 -0800515 static inline void GenKey(const GrGeometryProcessor&,
jvanverthcfc18862015-04-28 08:48:20 -0700516 const GrGLSLCaps&,
joshualitt87f48d92014-12-04 10:41:40 -0800517 GrProcessorKeyBuilder*);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000518
bsalomon56b7dc42016-09-19 09:26:40 -0700519 void setData(const GrGLSLProgramDataManager& pdman,
520 const GrPrimitiveProcessor& primProc) override {
joshualitte578a952015-05-14 10:09:13 -0700521 const GrCubicEffect& ce = primProc.cast<GrCubicEffect>();
joshualitt5559ca22015-05-21 15:50:36 -0700522
523 if (!ce.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(ce.viewMatrix())) {
524 fViewMatrix = ce.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700525 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800526 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700527 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
528 }
joshualittee2af952014-12-30 09:04:15 -0800529
joshualittb8c241a2015-05-19 08:23:30 -0700530 if (ce.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -0700531 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -0700532 GrColorToRGBAFloat(ce.color(), c);
joshualitt9b989322014-12-15 14:16:27 -0800533 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -0700534 fColor = ce.color();
joshualitt9b989322014-12-15 14:16:27 -0800535 }
536 }
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000537
538private:
joshualitt5559ca22015-05-21 15:50:36 -0700539 SkMatrix fViewMatrix;
joshualitt9b989322014-12-15 14:16:27 -0800540 GrColor fColor;
joshualittb0a8a372014-09-23 09:50:21 -0700541 GrPrimitiveEdgeType fEdgeType;
joshualitt9b989322014-12-15 14:16:27 -0800542 UniformHandle fColorUniform;
joshualitt5559ca22015-05-21 15:50:36 -0700543 UniformHandle fViewMatrixUniform;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000544
egdaniele659a582015-11-13 09:55:43 -0800545 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000546};
skia.committer@gmail.com44a77c82013-08-23 07:01:29 +0000547
joshualitt465283c2015-09-11 08:19:35 -0700548GrGLCubicEffect::GrGLCubicEffect(const GrGeometryProcessor& processor)
joshualitt5559ca22015-05-21 15:50:36 -0700549 : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL) {
joshualittb0a8a372014-09-23 09:50:21 -0700550 const GrCubicEffect& ce = processor.cast<GrCubicEffect>();
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000551 fEdgeType = ce.getEdgeType();
552}
553
robertphillips46d36f02015-01-18 08:14:14 -0800554void GrGLCubicEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
egdaniel4ca2e602015-11-18 08:01:26 -0800555 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
joshualitt2dd1ae02014-12-03 06:24:10 -0800556 const GrCubicEffect& gp = args.fGP.cast<GrCubicEffect>();
egdaniel0eafe792015-11-20 14:01:22 -0800557 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800558 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualitt2dd1ae02014-12-03 06:24:10 -0800559
joshualittabb52a12015-01-13 15:02:10 -0800560 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800561 varyingHandler->emitAttributes(gp);
joshualittabb52a12015-01-13 15:02:10 -0800562
egdaniel8dcdedc2015-11-11 06:27:20 -0800563 GrGLSLVertToFrag v(kVec4f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800564 varyingHandler->addVarying("CubicCoeffs", &v, kHigh_GrSLPrecision);
egdaniel4ca2e602015-11-18 08:01:26 -0800565 vertBuilder->codeAppendf("%s = %s;", v.vsOut(), gp.inCubicCoeffs()->fName);
joshualitt2dd1ae02014-12-03 06:24:10 -0800566
cdalton85285412016-02-18 12:37:07 -0800567 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -0800568 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -0700569 if (!gp.colorIgnored()) {
egdaniel7ea439b2015-12-03 09:20:44 -0800570 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -0700571 }
joshualitt9b989322014-12-15 14:16:27 -0800572
joshualittabb52a12015-01-13 15:02:10 -0800573 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -0800574 this->setupPosition(vertBuilder,
575 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800576 gpArgs,
577 gp.inPosition()->fName,
578 gp.viewMatrix(),
joshualitt5559ca22015-05-21 15:50:36 -0700579 &fViewMatrixUniform);
joshualitt4973d9d2014-11-08 09:24:25 -0800580
joshualittabb52a12015-01-13 15:02:10 -0800581 // emit transforms with position
egdaniel7ea439b2015-12-03 09:20:44 -0800582 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800583 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800584 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800585 gpArgs->fPositionVar,
586 gp.inPosition()->fName,
bsalomon56b7dc42016-09-19 09:26:40 -0700587 args.fTransformsIn,
588 args.fTransformsOut);
joshualittabb52a12015-01-13 15:02:10 -0800589
egdaniel9bd5bbf2014-08-29 11:12:30 -0700590
egdaniel0d3f0612015-10-21 10:45:48 -0700591 GrGLSLShaderVar edgeAlpha("edgeAlpha", kFloat_GrSLType, 0, kHigh_GrSLPrecision);
592 GrGLSLShaderVar dklmdx("dklmdx", kVec3f_GrSLType, 0, kHigh_GrSLPrecision);
593 GrGLSLShaderVar dklmdy("dklmdy", kVec3f_GrSLType, 0, kHigh_GrSLPrecision);
594 GrGLSLShaderVar dfdx("dfdx", kFloat_GrSLType, 0, kHigh_GrSLPrecision);
595 GrGLSLShaderVar dfdy("dfdy", kFloat_GrSLType, 0, kHigh_GrSLPrecision);
596 GrGLSLShaderVar gF("gF", kVec2f_GrSLType, 0, kHigh_GrSLPrecision);
597 GrGLSLShaderVar gFM("gFM", kFloat_GrSLType, 0, kHigh_GrSLPrecision);
598 GrGLSLShaderVar func("func", kFloat_GrSLType, 0, kHigh_GrSLPrecision);
egdaniel9bd5bbf2014-08-29 11:12:30 -0700599
egdaniel4ca2e602015-11-18 08:01:26 -0800600 fragBuilder->declAppend(edgeAlpha);
601 fragBuilder->declAppend(dklmdx);
602 fragBuilder->declAppend(dklmdy);
603 fragBuilder->declAppend(dfdx);
604 fragBuilder->declAppend(dfdy);
605 fragBuilder->declAppend(gF);
606 fragBuilder->declAppend(gFM);
607 fragBuilder->declAppend(func);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000608
609 switch (fEdgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700610 case kHairlineAA_GrProcessorEdgeType: {
egdaniel4ca2e602015-11-18 08:01:26 -0800611 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800612 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
egdaniel4ca2e602015-11-18 08:01:26 -0800613 fragBuilder->codeAppendf("%s = dFdx(%s.xyz);", dklmdx.c_str(), v.fsIn());
614 fragBuilder->codeAppendf("%s = dFdy(%s.xyz);", dklmdy.c_str(), v.fsIn());
615 fragBuilder->codeAppendf("%s = 3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
616 dfdx.c_str(), v.fsIn(), v.fsIn(), dklmdx.c_str(), v.fsIn(),
617 dklmdx.c_str(), v.fsIn(), dklmdx.c_str());
618 fragBuilder->codeAppendf("%s = 3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
619 dfdy.c_str(), v.fsIn(), v.fsIn(), dklmdy.c_str(), v.fsIn(),
620 dklmdy.c_str(), v.fsIn(), dklmdy.c_str());
621 fragBuilder->codeAppendf("%s = vec2(%s, %s);", gF.c_str(), dfdx.c_str(), dfdy.c_str());
622 fragBuilder->codeAppendf("%s = sqrt(dot(%s, %s));",
623 gFM.c_str(), gF.c_str(), gF.c_str());
624 fragBuilder->codeAppendf("%s = %s.x * %s.x * %s.x - %s.y * %s.z;",
625 func.c_str(), v.fsIn(), v.fsIn(),
626 v.fsIn(), v.fsIn(), v.fsIn());
627 fragBuilder->codeAppendf("%s = abs(%s);", func.c_str(), func.c_str());
628 fragBuilder->codeAppendf("%s = %s / %s;",
629 edgeAlpha.c_str(), func.c_str(), gFM.c_str());
630 fragBuilder->codeAppendf("%s = max(1.0 - %s, 0.0);",
631 edgeAlpha.c_str(), edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000632 // Add line below for smooth cubic ramp
egdaniel4ca2e602015-11-18 08:01:26 -0800633 // fragBuilder->codeAppendf("%s = %s * %s * (3.0 - 2.0 * %s);",
egdaniel9bd5bbf2014-08-29 11:12:30 -0700634 // edgeAlpha.c_str(), edgeAlpha.c_str(), edgeAlpha.c_str(),
635 // edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000636 break;
637 }
joshualittb0a8a372014-09-23 09:50:21 -0700638 case kFillAA_GrProcessorEdgeType: {
egdaniel4ca2e602015-11-18 08:01:26 -0800639 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800640 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
egdaniel4ca2e602015-11-18 08:01:26 -0800641 fragBuilder->codeAppendf("%s = dFdx(%s.xyz);", dklmdx.c_str(), v.fsIn());
642 fragBuilder->codeAppendf("%s = dFdy(%s.xyz);", dklmdy.c_str(), v.fsIn());
643 fragBuilder->codeAppendf("%s ="
644 "3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
645 dfdx.c_str(), v.fsIn(), v.fsIn(), dklmdx.c_str(), v.fsIn(),
646 dklmdx.c_str(), v.fsIn(), dklmdx.c_str());
647 fragBuilder->codeAppendf("%s = 3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;",
648 dfdy.c_str(), v.fsIn(), v.fsIn(), dklmdy.c_str(), v.fsIn(),
649 dklmdy.c_str(), v.fsIn(), dklmdy.c_str());
650 fragBuilder->codeAppendf("%s = vec2(%s, %s);", gF.c_str(), dfdx.c_str(), dfdy.c_str());
651 fragBuilder->codeAppendf("%s = sqrt(dot(%s, %s));",
652 gFM.c_str(), gF.c_str(), gF.c_str());
653 fragBuilder->codeAppendf("%s = %s.x * %s.x * %s.x - %s.y * %s.z;",
robertphillips96afa522015-12-09 07:54:24 -0800654 func.c_str(),
655 v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn());
egdaniel4ca2e602015-11-18 08:01:26 -0800656 fragBuilder->codeAppendf("%s = %s / %s;",
657 edgeAlpha.c_str(), func.c_str(), gFM.c_str());
658 fragBuilder->codeAppendf("%s = clamp(1.0 - %s, 0.0, 1.0);",
659 edgeAlpha.c_str(), edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000660 // Add line below for smooth cubic ramp
egdaniel4ca2e602015-11-18 08:01:26 -0800661 // fragBuilder->codeAppendf("%s = %s * %s * (3.0 - 2.0 * %s);",
egdaniel9bd5bbf2014-08-29 11:12:30 -0700662 // edgeAlpha.c_str(), edgeAlpha.c_str(), edgeAlpha.c_str(),
663 // edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000664 break;
665 }
joshualittb0a8a372014-09-23 09:50:21 -0700666 case kFillBW_GrProcessorEdgeType: {
egdaniel4ca2e602015-11-18 08:01:26 -0800667 fragBuilder->codeAppendf("%s = %s.x * %s.x * %s.x - %s.y * %s.z;",
668 edgeAlpha.c_str(), v.fsIn(), v.fsIn(),
669 v.fsIn(), v.fsIn(), v.fsIn());
670 fragBuilder->codeAppendf("%s = float(%s < 0.0);", edgeAlpha.c_str(), edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000671 break;
672 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000673 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000674 SkFAIL("Shouldn't get here");
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000675 }
676
joshualitt2dd1ae02014-12-03 06:24:10 -0800677
egdaniel4ca2e602015-11-18 08:01:26 -0800678 fragBuilder->codeAppendf("%s = vec4(%s);", args.fOutputCoverage, edgeAlpha.c_str());
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000679}
680
robertphillips46d36f02015-01-18 08:14:14 -0800681void GrGLCubicEffect::GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700682 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700683 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -0800684 const GrCubicEffect& ce = gp.cast<GrCubicEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700685 uint32_t key = ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2;
joshualittb8c241a2015-05-19 08:23:30 -0700686 key |= ce.color() != GrColor_ILLEGAL ? 0x4 : 0x8;
joshualitte578a952015-05-14 10:09:13 -0700687 key |= ComputePosKey(ce.viewMatrix()) << 5;
bsalomon63e99f72014-07-21 08:03:14 -0700688 b->add32(key);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000689}
690
691//////////////////////////////////////////////////////////////////////////////
692
693GrCubicEffect::~GrCubicEffect() {}
694
egdaniel57d3b032015-11-13 11:57:27 -0800695void GrCubicEffect::getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700696 GrGLCubicEffect::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800697}
698
egdaniel57d3b032015-11-13 11:57:27 -0800699GrGLSLPrimitiveProcessor* GrCubicEffect::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700700 return new GrGLCubicEffect(*this);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000701}
702
joshualitt8059eb92014-12-29 15:10:07 -0800703GrCubicEffect::GrCubicEffect(GrColor color, const SkMatrix& viewMatrix,
704 GrPrimitiveEdgeType edgeType)
joshualitte578a952015-05-14 10:09:13 -0700705 : fColor(color)
706 , fViewMatrix(viewMatrix)
joshualitt88c23fc2015-05-13 14:18:07 -0700707 , fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800708 this->initClassID<GrCubicEffect>();
bsalomon6cb807b2016-08-17 11:33:39 -0700709 fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType,
710 kHigh_GrSLPrecision);
711 fInCubicCoeffs = &this->addVertexAttrib("inCubicCoeffs", kVec4f_GrVertexAttribType);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000712}
713
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000714//////////////////////////////////////////////////////////////////////////////
715
joshualittb0a8a372014-09-23 09:50:21 -0700716GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrCubicEffect);
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000717
bungeman06ca8ec2016-06-09 08:01:03 -0700718sk_sp<GrGeometryProcessor> GrCubicEffect::TestCreate(GrProcessorTestData* d) {
719 sk_sp<GrGeometryProcessor> gp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000720 do {
joshualitt0067ff52015-07-08 14:26:19 -0700721 GrPrimitiveEdgeType edgeType =
722 static_cast<GrPrimitiveEdgeType>(
723 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
bungeman06ca8ec2016-06-09 08:01:03 -0700724 gp = GrCubicEffect::Make(GrRandomColor(d->fRandom),
725 GrTest::TestMatrix(d->fRandom), edgeType, *d->fCaps);
halcanary96fcdcc2015-08-27 07:41:13 -0700726 } while (nullptr == gp);
joshualittb0a8a372014-09-23 09:50:21 -0700727 return gp;
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000728}