blob: b77a1f5a5d21fce5660895dee26070a8c8b12254 [file] [log] [blame]
jvanverth@google.comd830d132013-11-11 20:54:09 +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
jvanverth8ed3b9a2015-04-09 08:00:49 -07008#include "GrDistanceFieldGeoProc.h"
egdaniel605dd0f2014-11-12 08:35:25 -08009#include "GrInvariantOutput.h"
joshualitteb2a6762014-12-04 11:35:33 -080010#include "GrTexture.h"
jvanverth21deace2015-04-01 12:43:48 -070011
joshualitteb2a6762014-12-04 11:35:33 -080012#include "SkDistanceFieldGen.h"
jvanverth21deace2015-04-01 12:43:48 -070013
egdaniel2d721d32015-11-11 13:06:05 -080014#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniele659a582015-11-13 09:55:43 -080015#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070016#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080017#include "glsl/GrGLSLUniformHandler.h"
egdaniel64c47282015-11-13 06:54:19 -080018#include "glsl/GrGLSLUtil.h"
egdaniel0eafe792015-11-20 14:01:22 -080019#include "glsl/GrGLSLVarying.h"
20#include "glsl/GrGLSLVertexShaderBuilder.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000021
jvanverth21deace2015-04-01 12:43:48 -070022// Assuming a radius of a little less than the diagonal of the fragment
jvanverth24ba0082015-03-19 11:34:13 -070023#define SK_DistanceFieldAAFactor "0.65"
jvanverth2d2a68c2014-06-10 06:42:56 -070024
egdaniele659a582015-11-13 09:55:43 -080025class GrGLDistanceFieldA8TextGeoProc : public GrGLSLGeometryProcessor {
jvanverth@google.comd830d132013-11-11 20:54:09 +000026public:
joshualitt465283c2015-09-11 08:19:35 -070027 GrGLDistanceFieldA8TextGeoProc()
joshualitt5559ca22015-05-21 15:50:36 -070028 : fViewMatrix(SkMatrix::InvalidMatrix())
jvanverth9564ce62014-09-16 05:45:19 -070029#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -070030 , fDistanceAdjust(-1.0f)
jvanverth9564ce62014-09-16 05:45:19 -070031#endif
32 {}
jvanverth@google.comd830d132013-11-11 20:54:09 +000033
mtklein36352bf2015-03-25 18:17:31 -070034 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -070035 const GrDistanceFieldA8TextGeoProc& dfTexEffect =
36 args.fGP.cast<GrDistanceFieldA8TextGeoProc>();
cdalton85285412016-02-18 12:37:07 -080037 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel4ca2e602015-11-18 08:01:26 -080038 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -080039 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
jvanverth@google.comd830d132013-11-11 20:54:09 +000040
egdaniel4ca2e602015-11-18 08:01:26 -080041 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -080042 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -080043 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -080044
45 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -080046 varyingHandler->emitAttributes(dfTexEffect);
joshualittabb52a12015-01-13 15:02:10 -080047
jvanverth21deace2015-04-01 12:43:48 -070048#ifdef SK_GAMMA_APPLY_TO_A8
49 // adjust based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -070050 const char* distanceAdjustUniName = nullptr;
jvanverth21deace2015-04-01 12:43:48 -070051 // width, height, 1/(3*width)
cdalton5e58cee2016-02-11 12:49:47 -080052 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -080053 kFloat_GrSLType, kDefault_GrSLPrecision,
54 "DistanceAdjust", &distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -070055#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000056
joshualitt9b989322014-12-15 14:16:27 -080057 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -070058 if (!dfTexEffect.colorIgnored()) {
joshualitt53f26aa2015-12-10 07:29:54 -080059 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualittb8c241a2015-05-19 08:23:30 -070060 }
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000061
joshualittabb52a12015-01-13 15:02:10 -080062 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -080063 this->setupPosition(vertBuilder,
64 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -080065 gpArgs,
66 dfTexEffect.inPosition()->fName,
67 dfTexEffect.viewMatrix(),
joshualitt5559ca22015-05-21 15:50:36 -070068 &fViewMatrixUniform);
joshualitt2dd1ae02014-12-03 06:24:10 -080069
joshualittabb52a12015-01-13 15:02:10 -080070 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -080071 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -080072 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -080073 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -080074 gpArgs->fPositionVar,
75 dfTexEffect.inPosition()->fName,
76 args.fTransformsIn,
77 args.fTransformsOut);
joshualitt4973d9d2014-11-08 09:24:25 -080078
jvanverthbb4a1cf2015-04-07 09:06:00 -070079 // add varyings
egdaniel8dcdedc2015-11-11 06:27:20 -080080 GrGLSLVertToFrag recipScale(kFloat_GrSLType);
jvanverth7023a002016-02-22 11:25:32 -080081 GrGLSLVertToFrag uv(kVec2f_GrSLType);
jvanverthbb4a1cf2015-04-07 09:06:00 -070082 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
jvanverth7023a002016-02-22 11:25:32 -080083 varyingHandler->addVarying("TextureCoords", &uv, kHigh_GrSLPrecision);
84 vertBuilder->codeAppendf("%s = %s;", uv.vsOut(), dfTexEffect.inTextureCoords()->fName);
jvanverthbb4a1cf2015-04-07 09:06:00 -070085
joshualitt922c8b12015-08-07 09:55:23 -070086 // compute numbers to be hardcoded to convert texture coordinates from int to float
87 SkASSERT(dfTexEffect.numTextures() == 1);
88 GrTexture* atlas = dfTexEffect.textureAccess(0).getTexture();
joshualitt7375d6b2015-08-07 13:36:44 -070089 SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
joshualitt922c8b12015-08-07 09:55:23 -070090
jvanverth7023a002016-02-22 11:25:32 -080091 GrGLSLVertToFrag st(kVec2f_GrSLType);
92 varyingHandler->addVarying("IntTextureCoords", &st, kHigh_GrSLPrecision);
93 vertBuilder->codeAppendf("%s = vec2(%d, %d) * %s;", st.vsOut(),
94 atlas->width(), atlas->height(),
egdaniel4ca2e602015-11-18 08:01:26 -080095 dfTexEffect.inTextureCoords()->fName);
jvanverthbb4a1cf2015-04-07 09:06:00 -070096
jvanverthfdf7ccc2015-01-27 08:19:33 -080097 // Use highp to work around aliasing issues
egdaniela2e3e0f2015-11-19 07:23:45 -080098 fragBuilder->codeAppend(GrGLSLShaderVar::PrecisionString(args.fGLSLCaps,
egdaniel4ca2e602015-11-18 08:01:26 -080099 kHigh_GrSLPrecision));
100 fragBuilder->codeAppendf("vec2 uv = %s;\n", uv.fsIn());
jvanverthfdf7ccc2015-01-27 08:19:33 -0800101
egdaniel4ca2e602015-11-18 08:01:26 -0800102 fragBuilder->codeAppend("\tfloat texColor = ");
103 fragBuilder->appendTextureLookup(args.fSamplers[0],
104 "uv",
105 kVec2f_GrSLType);
106 fragBuilder->codeAppend(".r;\n");
107 fragBuilder->codeAppend("\tfloat distance = "
jvanverthfdf7ccc2015-01-27 08:19:33 -0800108 SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFieldThreshold ");");
jvanverth21deace2015-04-01 12:43:48 -0700109#ifdef SK_GAMMA_APPLY_TO_A8
110 // adjust width based on gamma
egdaniel4ca2e602015-11-18 08:01:26 -0800111 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700112#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000113
egdaniel4ca2e602015-11-18 08:01:26 -0800114 fragBuilder->codeAppend("float afwidth;");
jvanverthbb4a1cf2015-04-07 09:06:00 -0700115 if (isSimilarity) {
jvanverth354eba52015-03-16 11:32:49 -0700116 // For uniform scale, we adjust for the effect of the transformation on the distance
117 // by using the length of the gradient of the texture coordinates. We use st coordinates
jvanverth221360a2015-04-15 12:31:22 -0700118 // to ensure we're mapping 1:1 from texel space to pixel space.
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000119
jvanverth354eba52015-03-16 11:32:49 -0700120 // this gives us a smooth step across approximately one fragment
jvanverth221360a2015-04-15 12:31:22 -0700121 // we use y to work around a Mali400 bug in the x direction
egdaniel4ca2e602015-11-18 08:01:26 -0800122 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));",
123 st.fsIn());
jvanverth354eba52015-03-16 11:32:49 -0700124 } else {
125 // For general transforms, to determine the amount of correction we multiply a unit
126 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
127 // (which is the inverse transform for this fragment) and take the length of the result.
egdaniel4ca2e602015-11-18 08:01:26 -0800128 fragBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700129 // the length of the gradient may be 0, so we need to check for this
130 // this also compensates for the Adreno, which likes to drop tiles on division by 0
egdaniel4ca2e602015-11-18 08:01:26 -0800131 fragBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);");
132 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
133 fragBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);");
134 fragBuilder->codeAppend("} else {");
135 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
136 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700137
egdaniel4ca2e602015-11-18 08:01:26 -0800138 fragBuilder->codeAppendf("vec2 Jdx = dFdx(%s);", st.fsIn());
139 fragBuilder->codeAppendf("vec2 Jdy = dFdy(%s);", st.fsIn());
140 fragBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
141 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000142
143 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800144 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000145 }
egdaniel4ca2e602015-11-18 08:01:26 -0800146 fragBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distance);");
jvanverth@google.comd830d132013-11-11 20:54:09 +0000147
egdaniel4ca2e602015-11-18 08:01:26 -0800148 fragBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000149 }
150
egdaniel018fb622015-10-28 07:26:40 -0700151 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc) override {
jvanverth2d2a68c2014-06-10 06:42:56 -0700152#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth502286d2015-04-08 12:37:51 -0700153 const GrDistanceFieldA8TextGeoProc& dfTexEffect = proc.cast<GrDistanceFieldA8TextGeoProc>();
jvanverth21deace2015-04-01 12:43:48 -0700154 float distanceAdjust = dfTexEffect.getDistanceAdjust();
155 if (distanceAdjust != fDistanceAdjust) {
156 pdman.set1f(fDistanceAdjustUni, distanceAdjust);
157 fDistanceAdjust = distanceAdjust;
jvanverth2d2a68c2014-06-10 06:42:56 -0700158 }
159#endif
joshualitte578a952015-05-14 10:09:13 -0700160 const GrDistanceFieldA8TextGeoProc& dfa8gp = proc.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700161
162 if (!dfa8gp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dfa8gp.viewMatrix())) {
163 fViewMatrix = dfa8gp.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700164 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800165 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700166 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
167 }
commit-bot@chromium.org8fe2ee12014-03-26 18:03:05 +0000168 }
169
robertphillips46d36f02015-01-18 08:14:14 -0800170 static inline void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700171 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700172 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700173 const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800174 uint32_t key = dfTexEffect.getFlags();
joshualitt53f26aa2015-12-10 07:29:54 -0800175 key |= dfTexEffect.colorIgnored() << 16;
joshualitte578a952015-05-14 10:09:13 -0700176 key |= ComputePosKey(dfTexEffect.viewMatrix()) << 25;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800177 b->add32(key);
joshualitt922c8b12015-08-07 09:55:23 -0700178
179 // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
180 SkASSERT(gp.numTextures() == 1);
181 GrTexture* atlas = gp.textureAccess(0).getTexture();
182 SkASSERT(atlas);
183 b->add32(atlas->width());
184 b->add32(atlas->height());
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000185 }
186
jvanverth@google.comd830d132013-11-11 20:54:09 +0000187private:
joshualitt5559ca22015-05-21 15:50:36 -0700188 SkMatrix fViewMatrix;
joshualitt5559ca22015-05-21 15:50:36 -0700189 UniformHandle fViewMatrixUniform;
mtklein50282b42015-01-22 07:59:52 -0800190#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -0700191 float fDistanceAdjust;
192 UniformHandle fDistanceAdjustUni;
mtklein50282b42015-01-22 07:59:52 -0800193#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000194
egdaniele659a582015-11-13 09:55:43 -0800195 typedef GrGLSLGeometryProcessor INHERITED;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000196};
197
198///////////////////////////////////////////////////////////////////////////////
199
jvanverth502286d2015-04-08 12:37:51 -0700200GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800201 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800202 GrTexture* texture,
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000203 const GrTextureParams& params,
jvanverth2d2a68c2014-06-10 06:42:56 -0700204#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -0700205 float distanceAdjust,
jvanverth2d2a68c2014-06-10 06:42:56 -0700206#endif
joshualittb8c241a2015-05-19 08:23:30 -0700207 uint32_t flags,
208 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700209 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700210 , fViewMatrix(viewMatrix)
joshualitt2e3b3e32014-12-09 13:31:14 -0800211 , fTextureAccess(texture, params)
jvanverth2d2a68c2014-06-10 06:42:56 -0700212#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -0700213 , fDistanceAdjust(distanceAdjust)
jvanverth2d2a68c2014-06-10 06:42:56 -0700214#endif
joshualitt249af152014-09-15 11:41:13 -0700215 , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
halcanary96fcdcc2015-08-27 07:41:13 -0700216 , fInColor(nullptr)
joshualittb8c241a2015-05-19 08:23:30 -0700217 , fUsesLocalCoords(usesLocalCoords) {
jvanverth78f07182014-07-30 06:17:59 -0700218 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
jvanverth502286d2015-04-08 12:37:51 -0700219 this->initClassID<GrDistanceFieldA8TextGeoProc>();
senorblancof2539d52015-05-20 14:03:42 -0700220 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
221 kHigh_GrSLPrecision));
joshualitt53f26aa2015-12-10 07:29:54 -0800222 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt71c92602015-01-14 08:12:47 -0800223 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
jvanverth7023a002016-02-22 11:25:32 -0800224 kVec2us_GrVertexAttribType,
225 kHigh_GrSLPrecision));
jvanverth@google.comd830d132013-11-11 20:54:09 +0000226 this->addTextureAccess(&fTextureAccess);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000227}
228
egdaniel57d3b032015-11-13 11:57:27 -0800229void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
230 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700231 GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800232}
233
egdaniel57d3b032015-11-13 11:57:27 -0800234GrGLSLPrimitiveProcessor* GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700235 return new GrGLDistanceFieldA8TextGeoProc();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000236}
237
238///////////////////////////////////////////////////////////////////////////////
239
jvanverth502286d2015-04-08 12:37:51 -0700240GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000241
bsalomonc21b09e2015-08-28 18:46:56 -0700242const GrGeometryProcessor* GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700243 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
244 GrProcessorUnitTest::kAlphaTextureIdx;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000245 static const SkShader::TileMode kTileModes[] = {
246 SkShader::kClamp_TileMode,
247 SkShader::kRepeat_TileMode,
248 SkShader::kMirror_TileMode,
249 };
250 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700251 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
252 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
jvanverth@google.comd830d132013-11-11 20:54:09 +0000253 };
joshualitt0067ff52015-07-08 14:26:19 -0700254 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
jvanverth@google.comd830d132013-11-11 20:54:09 +0000255 GrTextureParams::kNone_FilterMode);
256
joshualitt0067ff52015-07-08 14:26:19 -0700257 return GrDistanceFieldA8TextGeoProc::Create(GrRandomColor(d->fRandom),
258 GrTest::TestMatrix(d->fRandom),
259 d->fTextures[texIdx], params,
jvanverth2d2a68c2014-06-10 06:42:56 -0700260#ifdef SK_GAMMA_APPLY_TO_A8
joshualitt0067ff52015-07-08 14:26:19 -0700261 d->fRandom->nextF(),
jvanverth2d2a68c2014-06-10 06:42:56 -0700262#endif
joshualitt0067ff52015-07-08 14:26:19 -0700263 d->fRandom->nextBool() ?
joshualittb8c241a2015-05-19 08:23:30 -0700264 kSimilarity_DistanceFieldEffectFlag : 0,
joshualitt0067ff52015-07-08 14:26:19 -0700265 d->fRandom->nextBool());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000266}
267
268///////////////////////////////////////////////////////////////////////////////
269
egdaniele659a582015-11-13 09:55:43 -0800270class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700271public:
joshualitt465283c2015-09-11 08:19:35 -0700272 GrGLDistanceFieldPathGeoProc()
joshualitt5559ca22015-05-21 15:50:36 -0700273 : fViewMatrix(SkMatrix::InvalidMatrix())
joshualitt5559ca22015-05-21 15:50:36 -0700274 , fTextureSize(SkISize::Make(-1, -1)) {}
jvanverthfa38a302014-10-06 05:59:05 -0700275
mtklein36352bf2015-03-25 18:17:31 -0700276 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700277 const GrDistanceFieldPathGeoProc& dfTexEffect = args.fGP.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700278
cdalton85285412016-02-18 12:37:07 -0800279 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel4ca2e602015-11-18 08:01:26 -0800280 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800281 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
jvanverthfa38a302014-10-06 05:59:05 -0700282
egdaniel4ca2e602015-11-18 08:01:26 -0800283 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800284 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800285 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800286
287 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800288 varyingHandler->emitAttributes(dfTexEffect);
joshualittabb52a12015-01-13 15:02:10 -0800289
egdaniel8dcdedc2015-11-11 06:27:20 -0800290 GrGLSLVertToFrag v(kVec2f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800291 varyingHandler->addVarying("TextureCoords", &v, kHigh_GrSLPrecision);
jvanverthfa38a302014-10-06 05:59:05 -0700292
joshualitt9b989322014-12-15 14:16:27 -0800293 // setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -0700294 if (!dfTexEffect.colorIgnored()) {
joshualitt53f26aa2015-12-10 07:29:54 -0800295 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualittb8c241a2015-05-19 08:23:30 -0700296 }
egdaniel4ca2e602015-11-18 08:01:26 -0800297 vertBuilder->codeAppendf("%s = %s;", v.vsOut(), dfTexEffect.inTextureCoords()->fName);
reede4ef1ca2015-02-17 18:38:38 -0800298
joshualittabb52a12015-01-13 15:02:10 -0800299 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -0800300 this->setupPosition(vertBuilder,
301 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800302 gpArgs,
303 dfTexEffect.inPosition()->fName,
304 dfTexEffect.viewMatrix(),
joshualitt5559ca22015-05-21 15:50:36 -0700305 &fViewMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -0800306
307 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800308 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800309 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800310 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800311 gpArgs->fPositionVar,
312 dfTexEffect.inPosition()->fName,
313 args.fTransformsIn,
314 args.fTransformsOut);
joshualitt4973d9d2014-11-08 09:24:25 -0800315
halcanary96fcdcc2015-08-27 07:41:13 -0700316 const char* textureSizeUniName = nullptr;
cdalton5e58cee2016-02-11 12:49:47 -0800317 fTextureSizeUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800318 kVec2f_GrSLType, kDefault_GrSLPrecision,
319 "TextureSize", &textureSizeUniName);
reede4ef1ca2015-02-17 18:38:38 -0800320
jvanverthfdf7ccc2015-01-27 08:19:33 -0800321 // Use highp to work around aliasing issues
egdaniela2e3e0f2015-11-19 07:23:45 -0800322 fragBuilder->codeAppend(GrGLSLShaderVar::PrecisionString(args.fGLSLCaps,
egdaniel4ca2e602015-11-18 08:01:26 -0800323 kHigh_GrSLPrecision));
324 fragBuilder->codeAppendf("vec2 uv = %s;", v.fsIn());
jvanverthfdf7ccc2015-01-27 08:19:33 -0800325
egdaniel4ca2e602015-11-18 08:01:26 -0800326 fragBuilder->codeAppend("float texColor = ");
327 fragBuilder->appendTextureLookup(args.fSamplers[0],
328 "uv",
329 kVec2f_GrSLType);
330 fragBuilder->codeAppend(".r;");
331 fragBuilder->codeAppend("float distance = "
jvanverthfdf7ccc2015-01-27 08:19:33 -0800332 SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFieldThreshold ");");
jvanverthfa38a302014-10-06 05:59:05 -0700333
egdaniela2e3e0f2015-11-19 07:23:45 -0800334 fragBuilder->codeAppend(GrGLSLShaderVar::PrecisionString(args.fGLSLCaps,
egdaniel4ca2e602015-11-18 08:01:26 -0800335 kHigh_GrSLPrecision));
336 fragBuilder->codeAppendf("vec2 st = uv*%s;", textureSizeUniName);
337 fragBuilder->codeAppend("float afwidth;");
jvanverthfa38a302014-10-06 05:59:05 -0700338 if (dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag) {
jvanverth354eba52015-03-16 11:32:49 -0700339 // For uniform scale, we adjust for the effect of the transformation on the distance
340 // by using the length of the gradient of the texture coordinates. We use st coordinates
341 // to ensure we're mapping 1:1 from texel space to pixel space.
342
jvanverthfa38a302014-10-06 05:59:05 -0700343 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800344 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(st.y));");
jvanverthfa38a302014-10-06 05:59:05 -0700345 } else {
jvanverth354eba52015-03-16 11:32:49 -0700346 // For general transforms, to determine the amount of correction we multiply a unit
347 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
348 // (which is the inverse transform for this fragment) and take the length of the result.
egdaniel4ca2e602015-11-18 08:01:26 -0800349 fragBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700350 // the length of the gradient may be 0, so we need to check for this
351 // this also compensates for the Adreno, which likes to drop tiles on division by 0
egdaniel4ca2e602015-11-18 08:01:26 -0800352 fragBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);");
353 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
354 fragBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);");
355 fragBuilder->codeAppend("} else {");
356 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
357 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700358
egdaniel4ca2e602015-11-18 08:01:26 -0800359 fragBuilder->codeAppend("vec2 Jdx = dFdx(st);");
360 fragBuilder->codeAppend("vec2 Jdy = dFdy(st);");
361 fragBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
362 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
jvanverthfa38a302014-10-06 05:59:05 -0700363
364 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800365 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
jvanverthfa38a302014-10-06 05:59:05 -0700366 }
egdaniel4ca2e602015-11-18 08:01:26 -0800367 fragBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distance);");
jvanverthfa38a302014-10-06 05:59:05 -0700368
egdaniel4ca2e602015-11-18 08:01:26 -0800369 fragBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage);
jvanverthfa38a302014-10-06 05:59:05 -0700370 }
371
egdaniel018fb622015-10-28 07:26:40 -0700372 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc) override {
reede4ef1ca2015-02-17 18:38:38 -0800373 SkASSERT(fTextureSizeUni.isValid());
jvanverthfa38a302014-10-06 05:59:05 -0700374
joshualitt87f48d92014-12-04 10:41:40 -0800375 GrTexture* texture = proc.texture(0);
jvanverthfa38a302014-10-06 05:59:05 -0700376 if (texture->width() != fTextureSize.width() ||
377 texture->height() != fTextureSize.height()) {
378 fTextureSize = SkISize::Make(texture->width(), texture->height());
reede4ef1ca2015-02-17 18:38:38 -0800379 pdman.set2f(fTextureSizeUni,
380 SkIntToScalar(fTextureSize.width()),
381 SkIntToScalar(fTextureSize.height()));
jvanverthfa38a302014-10-06 05:59:05 -0700382 }
joshualitt9b989322014-12-15 14:16:27 -0800383
joshualitte578a952015-05-14 10:09:13 -0700384 const GrDistanceFieldPathGeoProc& dfpgp = proc.cast<GrDistanceFieldPathGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700385
386 if (!dfpgp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dfpgp.viewMatrix())) {
387 fViewMatrix = dfpgp.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700388 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800389 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700390 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
391 }
jvanverthfa38a302014-10-06 05:59:05 -0700392 }
393
robertphillips46d36f02015-01-18 08:14:14 -0800394 static inline void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700395 const GrGLSLCaps&,
jvanverthfa38a302014-10-06 05:59:05 -0700396 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700397 const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700398
joshualitt8fc6c2d2014-12-22 15:27:05 -0800399 uint32_t key = dfTexEffect.getFlags();
joshualittb8c241a2015-05-19 08:23:30 -0700400 key |= dfTexEffect.colorIgnored() << 16;
joshualitte578a952015-05-14 10:09:13 -0700401 key |= ComputePosKey(dfTexEffect.viewMatrix()) << 25;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800402 b->add32(key);
jvanverthfa38a302014-10-06 05:59:05 -0700403 }
404
405private:
reede4ef1ca2015-02-17 18:38:38 -0800406 UniformHandle fTextureSizeUni;
joshualitt5559ca22015-05-21 15:50:36 -0700407 UniformHandle fViewMatrixUniform;
408 SkMatrix fViewMatrix;
joshualitt9b989322014-12-15 14:16:27 -0800409 SkISize fTextureSize;
jvanverthfa38a302014-10-06 05:59:05 -0700410
egdaniele659a582015-11-13 09:55:43 -0800411 typedef GrGLSLGeometryProcessor INHERITED;
jvanverthfa38a302014-10-06 05:59:05 -0700412};
413
414///////////////////////////////////////////////////////////////////////////////
415
jvanverth502286d2015-04-08 12:37:51 -0700416GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(
joshualitt2e3b3e32014-12-09 13:31:14 -0800417 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800418 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800419 GrTexture* texture,
420 const GrTextureParams& params,
joshualittb8c241a2015-05-19 08:23:30 -0700421 uint32_t flags,
422 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700423 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700424 , fViewMatrix(viewMatrix)
joshualitt2e3b3e32014-12-09 13:31:14 -0800425 , fTextureAccess(texture, params)
jvanverthfa38a302014-10-06 05:59:05 -0700426 , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
halcanary96fcdcc2015-08-27 07:41:13 -0700427 , fInColor(nullptr)
joshualittb8c241a2015-05-19 08:23:30 -0700428 , fUsesLocalCoords(usesLocalCoords) {
jvanverthfa38a302014-10-06 05:59:05 -0700429 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
jvanverth502286d2015-04-08 12:37:51 -0700430 this->initClassID<GrDistanceFieldPathGeoProc>();
senorblancof2539d52015-05-20 14:03:42 -0700431 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
432 kHigh_GrSLPrecision));
joshualitt53f26aa2015-12-10 07:29:54 -0800433 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt71c92602015-01-14 08:12:47 -0800434 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
joshualittb8c241a2015-05-19 08:23:30 -0700435 kVec2f_GrVertexAttribType));
jvanverthfa38a302014-10-06 05:59:05 -0700436 this->addTextureAccess(&fTextureAccess);
437}
438
egdaniel57d3b032015-11-13 11:57:27 -0800439void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
440 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700441 GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800442}
443
egdaniel57d3b032015-11-13 11:57:27 -0800444GrGLSLPrimitiveProcessor* GrDistanceFieldPathGeoProc::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700445 return new GrGLDistanceFieldPathGeoProc();
jvanverthfa38a302014-10-06 05:59:05 -0700446}
447
448///////////////////////////////////////////////////////////////////////////////
449
jvanverth502286d2015-04-08 12:37:51 -0700450GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc);
jvanverthfa38a302014-10-06 05:59:05 -0700451
bsalomonc21b09e2015-08-28 18:46:56 -0700452const GrGeometryProcessor* GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700453 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
454 : GrProcessorUnitTest::kAlphaTextureIdx;
jvanverthfa38a302014-10-06 05:59:05 -0700455 static const SkShader::TileMode kTileModes[] = {
456 SkShader::kClamp_TileMode,
457 SkShader::kRepeat_TileMode,
458 SkShader::kMirror_TileMode,
459 };
460 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700461 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
462 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
jvanverthfa38a302014-10-06 05:59:05 -0700463 };
joshualitt0067ff52015-07-08 14:26:19 -0700464 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode
465 : GrTextureParams::kNone_FilterMode);
jvanverthfa38a302014-10-06 05:59:05 -0700466
joshualitt0067ff52015-07-08 14:26:19 -0700467 return GrDistanceFieldPathGeoProc::Create(GrRandomColor(d->fRandom),
468 GrTest::TestMatrix(d->fRandom),
469 d->fTextures[texIdx],
jvanverth502286d2015-04-08 12:37:51 -0700470 params,
joshualitt0067ff52015-07-08 14:26:19 -0700471 d->fRandom->nextBool() ?
joshualittb8c241a2015-05-19 08:23:30 -0700472 kSimilarity_DistanceFieldEffectFlag : 0,
joshualitt0067ff52015-07-08 14:26:19 -0700473 d->fRandom->nextBool());
jvanverthfa38a302014-10-06 05:59:05 -0700474}
475
476///////////////////////////////////////////////////////////////////////////////
477
egdaniele659a582015-11-13 09:55:43 -0800478class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000479public:
joshualitt465283c2015-09-11 08:19:35 -0700480 GrGLDistanceFieldLCDTextGeoProc()
joshualitt53f26aa2015-12-10 07:29:54 -0800481 : fViewMatrix(SkMatrix::InvalidMatrix()) {
jvanverth502286d2015-04-08 12:37:51 -0700482 fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
jvanverth21deace2015-04-01 12:43:48 -0700483 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000484
mtklein36352bf2015-03-25 18:17:31 -0700485 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700486 const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
487 args.fGP.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000488
egdaniel4ca2e602015-11-18 08:01:26 -0800489 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800490 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800491 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800492
493 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800494 varyingHandler->emitAttributes(dfTexEffect);
egdaniel4ca2e602015-11-18 08:01:26 -0800495
cdalton85285412016-02-18 12:37:07 -0800496 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittabb52a12015-01-13 15:02:10 -0800497
joshualitt9b989322014-12-15 14:16:27 -0800498 // setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -0700499 if (!dfTexEffect.colorIgnored()) {
joshualitt53f26aa2015-12-10 07:29:54 -0800500 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualittb8c241a2015-05-19 08:23:30 -0700501 }
joshualitt9b989322014-12-15 14:16:27 -0800502
joshualittabb52a12015-01-13 15:02:10 -0800503 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -0800504 this->setupPosition(vertBuilder,
505 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800506 gpArgs,
507 dfTexEffect.inPosition()->fName,
508 dfTexEffect.viewMatrix(),
joshualitt5559ca22015-05-21 15:50:36 -0700509 &fViewMatrixUniform);
joshualitt4973d9d2014-11-08 09:24:25 -0800510
joshualittabb52a12015-01-13 15:02:10 -0800511 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800512 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800513 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800514 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800515 gpArgs->fPositionVar,
516 dfTexEffect.inPosition()->fName,
517 args.fTransformsIn,
518 args.fTransformsOut);
joshualittabb52a12015-01-13 15:02:10 -0800519
jvanverthbb4a1cf2015-04-07 09:06:00 -0700520 // set up varyings
521 bool isUniformScale = SkToBool(dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask);
egdaniel8dcdedc2015-11-11 06:27:20 -0800522 GrGLSLVertToFrag recipScale(kFloat_GrSLType);
jvanverth7023a002016-02-22 11:25:32 -0800523 GrGLSLVertToFrag uv(kVec2f_GrSLType);
524 varyingHandler->addVarying("TextureCoords", &uv, kHigh_GrSLPrecision);
525 vertBuilder->codeAppendf("%s = %s;", uv.vsOut(), dfTexEffect.inTextureCoords()->fName);
jvanverthbb4a1cf2015-04-07 09:06:00 -0700526
joshualitt922c8b12015-08-07 09:55:23 -0700527 // compute numbers to be hardcoded to convert texture coordinates from int to float
528 SkASSERT(dfTexEffect.numTextures() == 1);
529 GrTexture* atlas = dfTexEffect.textureAccess(0).getTexture();
joshualitt7375d6b2015-08-07 13:36:44 -0700530 SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
joshualitt922c8b12015-08-07 09:55:23 -0700531
jvanverth7023a002016-02-22 11:25:32 -0800532 GrGLSLVertToFrag st(kVec2f_GrSLType);
533 varyingHandler->addVarying("IntTextureCoords", &st, kHigh_GrSLPrecision);
534 vertBuilder->codeAppendf("%s = vec2(%d, %d) * %s;", st.vsOut(),
535 atlas->width(), atlas->height(),
egdaniel4ca2e602015-11-18 08:01:26 -0800536 dfTexEffect.inTextureCoords()->fName);
jvanverthbb4a1cf2015-04-07 09:06:00 -0700537
538 // add frag shader code
joshualitt30ba4362014-08-21 20:18:45 -0700539
egdaniel4ca2e602015-11-18 08:01:26 -0800540 SkAssertResult(fragBuilder->enableFeature(
egdaniel2d721d32015-11-11 13:06:05 -0800541 GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature));
joshualitt30ba4362014-08-21 20:18:45 -0700542
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000543 // create LCD offset adjusted by inverse of transform
jvanverthfdf7ccc2015-01-27 08:19:33 -0800544 // Use highp to work around aliasing issues
egdaniela2e3e0f2015-11-19 07:23:45 -0800545 fragBuilder->codeAppend(GrGLSLShaderVar::PrecisionString(args.fGLSLCaps,
egdaniel4ca2e602015-11-18 08:01:26 -0800546 kHigh_GrSLPrecision));
547 fragBuilder->codeAppendf("vec2 uv = %s;\n", uv.fsIn());
egdaniela2e3e0f2015-11-19 07:23:45 -0800548 fragBuilder->codeAppend(GrGLSLShaderVar::PrecisionString(args.fGLSLCaps,
egdaniel4ca2e602015-11-18 08:01:26 -0800549 kHigh_GrSLPrecision));
joshualitt922c8b12015-08-07 09:55:23 -0700550
551 SkScalar lcdDelta = 1.0f / (3.0f * atlas->width());
jvanverth5a105ff2015-02-18 11:36:35 -0800552 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) {
egdaniel4ca2e602015-11-18 08:01:26 -0800553 fragBuilder->codeAppendf("float delta = -%.*f;\n", SK_FLT_DECIMAL_DIG, lcdDelta);
jvanverth5a105ff2015-02-18 11:36:35 -0800554 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800555 fragBuilder->codeAppendf("float delta = %.*f;\n", SK_FLT_DECIMAL_DIG, lcdDelta);
jvanverth5a105ff2015-02-18 11:36:35 -0800556 }
jvanverth78f07182014-07-30 06:17:59 -0700557 if (isUniformScale) {
egdaniel4ca2e602015-11-18 08:01:26 -0800558 fragBuilder->codeAppendf("float dy = abs(dFdy(%s.y));", st.fsIn());
559 fragBuilder->codeAppend("vec2 offset = vec2(dy*delta, 0.0);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000560 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800561 fragBuilder->codeAppendf("vec2 st = %s;\n", st.fsIn());
jvanverthbb4a1cf2015-04-07 09:06:00 -0700562
egdaniel4ca2e602015-11-18 08:01:26 -0800563 fragBuilder->codeAppend("vec2 Jdx = dFdx(st);");
564 fragBuilder->codeAppend("vec2 Jdy = dFdy(st);");
565 fragBuilder->codeAppend("vec2 offset = delta*Jdx;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000566 }
567
568 // green is distance to uv center
egdaniel4ca2e602015-11-18 08:01:26 -0800569 fragBuilder->codeAppend("\tvec4 texColor = ");
570 fragBuilder->appendTextureLookup(args.fSamplers[0], "uv", kVec2f_GrSLType);
571 fragBuilder->codeAppend(";\n");
572 fragBuilder->codeAppend("\tvec3 distance;\n");
573 fragBuilder->codeAppend("\tdistance.y = texColor.r;\n");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000574 // red is distance to left offset
egdaniel4ca2e602015-11-18 08:01:26 -0800575 fragBuilder->codeAppend("\tvec2 uv_adjusted = uv - offset;\n");
576 fragBuilder->codeAppend("\ttexColor = ");
577 fragBuilder->appendTextureLookup(args.fSamplers[0], "uv_adjusted", kVec2f_GrSLType);
578 fragBuilder->codeAppend(";\n");
579 fragBuilder->codeAppend("\tdistance.x = texColor.r;\n");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000580 // blue is distance to right offset
egdaniel4ca2e602015-11-18 08:01:26 -0800581 fragBuilder->codeAppend("\tuv_adjusted = uv + offset;\n");
582 fragBuilder->codeAppend("\ttexColor = ");
583 fragBuilder->appendTextureLookup(args.fSamplers[0], "uv_adjusted", kVec2f_GrSLType);
584 fragBuilder->codeAppend(";\n");
585 fragBuilder->codeAppend("\tdistance.z = texColor.r;\n");
jvanverth2d2a68c2014-06-10 06:42:56 -0700586
egdaniel4ca2e602015-11-18 08:01:26 -0800587 fragBuilder->codeAppend("\tdistance = "
jvanverthada68ef2014-11-03 14:00:24 -0800588 "vec3(" SK_DistanceFieldMultiplier ")*(distance - vec3(" SK_DistanceFieldThreshold"));");
jvanverth2d2a68c2014-06-10 06:42:56 -0700589
jvanverth21deace2015-04-01 12:43:48 -0700590 // adjust width based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -0700591 const char* distanceAdjustUniName = nullptr;
cdalton5e58cee2016-02-11 12:49:47 -0800592 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800593 kVec3f_GrSLType, kDefault_GrSLPrecision,
594 "DistanceAdjust", &distanceAdjustUniName);
egdaniel4ca2e602015-11-18 08:01:26 -0800595 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700596
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000597 // To be strictly correct, we should compute the anti-aliasing factor separately
598 // for each color component. However, this is only important when using perspective
599 // transformations, and even then using a single factor seems like a reasonable
600 // trade-off between quality and speed.
egdaniel4ca2e602015-11-18 08:01:26 -0800601 fragBuilder->codeAppend("float afwidth;");
jvanverth78f07182014-07-30 06:17:59 -0700602 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700603 // For uniform scale, we adjust for the effect of the transformation on the distance
604 // by using the length of the gradient of the texture coordinates. We use st coordinates
605 // to ensure we're mapping 1:1 from texel space to pixel space.
606
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000607 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800608 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*dy;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000609 } else {
jvanverth354eba52015-03-16 11:32:49 -0700610 // For general transforms, to determine the amount of correction we multiply a unit
611 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
612 // (which is the inverse transform for this fragment) and take the length of the result.
egdaniel4ca2e602015-11-18 08:01:26 -0800613 fragBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance.r), dFdy(distance.r));");
jvanverthd68a5502015-03-16 12:58:43 -0700614 // the length of the gradient may be 0, so we need to check for this
615 // this also compensates for the Adreno, which likes to drop tiles on division by 0
egdaniel4ca2e602015-11-18 08:01:26 -0800616 fragBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);");
617 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
618 fragBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);");
619 fragBuilder->codeAppend("} else {");
620 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
621 fragBuilder->codeAppend("}");
622 fragBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
623 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000624
625 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800626 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000627 }
628
egdaniel4ca2e602015-11-18 08:01:26 -0800629 fragBuilder->codeAppend(
jvanverth21deace2015-04-01 12:43:48 -0700630 "vec4 val = vec4(smoothstep(vec3(-afwidth), vec3(afwidth), distance), 1.0);");
egdaniel27b63352015-09-15 13:13:50 -0700631 // set alpha to be max of rgb coverage
egdaniel4ca2e602015-11-18 08:01:26 -0800632 fragBuilder->codeAppend("val.a = max(max(val.r, val.g), val.b);");
jvanverth2d2a68c2014-06-10 06:42:56 -0700633
egdaniel4ca2e602015-11-18 08:01:26 -0800634 fragBuilder->codeAppendf("%s = val;", args.fOutputCoverage);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000635 }
636
egdaniel018fb622015-10-28 07:26:40 -0700637 void setData(const GrGLSLProgramDataManager& pdman,
joshualitt465283c2015-09-11 08:19:35 -0700638 const GrPrimitiveProcessor& processor) override {
jvanverth21deace2015-04-01 12:43:48 -0700639 SkASSERT(fDistanceAdjustUni.isValid());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000640
joshualitt5559ca22015-05-21 15:50:36 -0700641 const GrDistanceFieldLCDTextGeoProc& dflcd = processor.cast<GrDistanceFieldLCDTextGeoProc>();
642 GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700643 if (wa != fDistanceAdjust) {
644 pdman.set3f(fDistanceAdjustUni,
645 wa.fR,
646 wa.fG,
647 wa.fB);
648 fDistanceAdjust = wa;
jvanverth2d2a68c2014-06-10 06:42:56 -0700649 }
joshualitt9b989322014-12-15 14:16:27 -0800650
joshualitt5559ca22015-05-21 15:50:36 -0700651 if (!dflcd.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dflcd.viewMatrix())) {
652 fViewMatrix = dflcd.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700653 float viewMatrix[3 * 3];
egdaniel64c47282015-11-13 06:54:19 -0800654 GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix);
joshualitt5559ca22015-05-21 15:50:36 -0700655 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
656 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000657 }
658
robertphillips46d36f02015-01-18 08:14:14 -0800659 static inline void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700660 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700661 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700662 const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000663
joshualitt8fc6c2d2014-12-22 15:27:05 -0800664 uint32_t key = dfTexEffect.getFlags();
joshualittb8c241a2015-05-19 08:23:30 -0700665 key |= dfTexEffect.colorIgnored() << 16;
joshualitte578a952015-05-14 10:09:13 -0700666 key |= ComputePosKey(dfTexEffect.viewMatrix()) << 25;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800667 b->add32(key);
joshualitt922c8b12015-08-07 09:55:23 -0700668
669 // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
670 SkASSERT(gp.numTextures() == 1);
671 GrTexture* atlas = gp.textureAccess(0).getTexture();
672 SkASSERT(atlas);
673 b->add32(atlas->width());
674 b->add32(atlas->height());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000675 }
676
677private:
joshualitt5559ca22015-05-21 15:50:36 -0700678 SkMatrix fViewMatrix;
joshualitt5559ca22015-05-21 15:50:36 -0700679 UniformHandle fViewMatrixUniform;
jvanverth21deace2015-04-01 12:43:48 -0700680 UniformHandle fColorUniform;
jvanverth502286d2015-04-08 12:37:51 -0700681 GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust;
jvanverth21deace2015-04-01 12:43:48 -0700682 UniformHandle fDistanceAdjustUni;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000683
egdaniele659a582015-11-13 09:55:43 -0800684 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000685};
686
687///////////////////////////////////////////////////////////////////////////////
688
jvanverth502286d2015-04-08 12:37:51 -0700689GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(
joshualitt8059eb92014-12-29 15:10:07 -0800690 GrColor color, const SkMatrix& viewMatrix,
jvanverth2d2a68c2014-06-10 06:42:56 -0700691 GrTexture* texture, const GrTextureParams& params,
jvanverth21deace2015-04-01 12:43:48 -0700692 DistanceAdjust distanceAdjust,
joshualittb8c241a2015-05-19 08:23:30 -0700693 uint32_t flags, bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700694 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700695 , fViewMatrix(viewMatrix)
joshualitt2e3b3e32014-12-09 13:31:14 -0800696 , fTextureAccess(texture, params)
jvanverth21deace2015-04-01 12:43:48 -0700697 , fDistanceAdjust(distanceAdjust)
joshualittb8c241a2015-05-19 08:23:30 -0700698 , fFlags(flags & kLCD_DistanceFieldEffectMask)
699 , fUsesLocalCoords(usesLocalCoords) {
jvanverth78f07182014-07-30 06:17:59 -0700700 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag));
jvanverth502286d2015-04-08 12:37:51 -0700701 this->initClassID<GrDistanceFieldLCDTextGeoProc>();
senorblancof2539d52015-05-20 14:03:42 -0700702 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
703 kHigh_GrSLPrecision));
joshualitt53f26aa2015-12-10 07:29:54 -0800704 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt71c92602015-01-14 08:12:47 -0800705 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
jvanverth7023a002016-02-22 11:25:32 -0800706 kVec2us_GrVertexAttribType,
707 kHigh_GrSLPrecision));
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000708 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000709}
710
egdaniel57d3b032015-11-13 11:57:27 -0800711void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
712 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700713 GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800714}
715
egdaniel57d3b032015-11-13 11:57:27 -0800716GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700717 return new GrGLDistanceFieldLCDTextGeoProc();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000718}
719
720///////////////////////////////////////////////////////////////////////////////
721
jvanverth502286d2015-04-08 12:37:51 -0700722GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000723
bsalomonc21b09e2015-08-28 18:46:56 -0700724const GrGeometryProcessor* GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700725 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
726 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000727 static const SkShader::TileMode kTileModes[] = {
728 SkShader::kClamp_TileMode,
729 SkShader::kRepeat_TileMode,
730 SkShader::kMirror_TileMode,
731 };
732 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700733 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
734 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000735 };
joshualitt0067ff52015-07-08 14:26:19 -0700736 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000737 GrTextureParams::kNone_FilterMode);
jvanverth21deace2015-04-01 12:43:48 -0700738 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
jvanverth78f07182014-07-30 06:17:59 -0700739 uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
joshualitt0067ff52015-07-08 14:26:19 -0700740 flags |= d->fRandom->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0;
741 flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
742 return GrDistanceFieldLCDTextGeoProc::Create(GrRandomColor(d->fRandom),
743 GrTest::TestMatrix(d->fRandom),
744 d->fTextures[texIdx], params,
jvanverth502286d2015-04-08 12:37:51 -0700745 wa,
joshualittb8c241a2015-05-19 08:23:30 -0700746 flags,
joshualitt0067ff52015-07-08 14:26:19 -0700747 d->fRandom->nextBool());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000748}