blob: d6a5e84f8ed29b7ec480a4f05a3931993054a68f [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"
Jim Van Verth6a7a7042017-09-11 11:04:10 -04009#include "GrAtlasedShaderHelpers.h"
Brian Osman4a3f5c82018-09-18 16:16:38 -040010#include "GrCaps.h"
11#include "GrShaderCaps.h"
joshualitteb2a6762014-12-04 11:35:33 -080012#include "GrTexture.h"
13#include "SkDistanceFieldGen.h"
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"
Chris Daltonc17bf322017-10-24 10:59:03 -060020#include "glsl/GrGLSLVertexGeoBuilder.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:
Brian Salomon5c6ac642017-12-19 11:09:32 -050027 GrGLDistanceFieldA8TextGeoProc() = default;
jvanverth@google.comd830d132013-11-11 20:54:09 +000028
mtklein36352bf2015-03-25 18:17:31 -070029 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -070030 const GrDistanceFieldA8TextGeoProc& dfTexEffect =
31 args.fGP.cast<GrDistanceFieldA8TextGeoProc>();
Chris Dalton60283612018-02-14 13:38:14 -070032 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
jvanverth@google.comd830d132013-11-11 20:54:09 +000033
egdaniel4ca2e602015-11-18 08:01:26 -080034 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -080035 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -080036 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -080037
38 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -080039 varyingHandler->emitAttributes(dfTexEffect);
joshualittabb52a12015-01-13 15:02:10 -080040
Robert Phillips8296e752017-08-25 08:45:21 -040041 const char* atlasSizeInvName;
42 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040043 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -040044 kHigh_GrSLPrecision,
45 "AtlasSizeInv",
46 &atlasSizeInvName);
jvanverth21deace2015-04-01 12:43:48 -070047#ifdef SK_GAMMA_APPLY_TO_A8
48 // adjust based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -070049 const char* distanceAdjustUniName = nullptr;
jvanverth21deace2015-04-01 12:43:48 -070050 // width, height, 1/(3*width)
Ethan Nicholasf7b88202017-09-18 14:10:39 -040051 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -080052 "DistanceAdjust", &distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -070053#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000054
joshualitt9b989322014-12-15 14:16:27 -080055 // Setup pass through color
Brian Salomonbfd51832017-01-04 13:22:08 -050056 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000057
joshualittabb52a12015-01-13 15:02:10 -080058 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -040059 gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
joshualitt2dd1ae02014-12-03 06:24:10 -080060
joshualittabb52a12015-01-13 15:02:10 -080061 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -080062 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -080063 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -080064 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -040065 dfTexEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -050066 dfTexEffect.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -070067 args.fFPCoordTransformHandler);
joshualitt4973d9d2014-11-08 09:24:25 -080068
jvanverthbb4a1cf2015-04-07 09:06:00 -070069 // add varyings
Chris Dalton27372882017-12-08 13:34:21 -070070 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verthdfc738b2018-11-19 17:15:27 +000071 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
72 GrGLSLVarying texIdx(texIdxType);
Chris Dalton27372882017-12-08 13:34:21 -070073 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -040074 append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -040075 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -040076
jvanverthcf371bb2016-03-10 11:10:43 -080077 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
78 kUniformScale_DistanceFieldEffectMask;
jvanverthbb4a1cf2015-04-07 09:06:00 -070079 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -070080 bool isGammaCorrect =
81 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
Jim Van Verth90e89b32017-07-06 16:36:55 -040082 bool isAliased =
83 SkToBool(dfTexEffect.getFlags() & kAliased_DistanceFieldEffectFlag);
halcanary9d524f22016-03-29 09:03:52 -070084
jvanverthfdf7ccc2015-01-27 08:19:33 -080085 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -040086 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -040087 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040088 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
89 texIdx, "uv", "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -080090
Ethan Nicholasf7b88202017-09-18 14:10:39 -040091 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -040092 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverth21deace2015-04-01 12:43:48 -070093#ifdef SK_GAMMA_APPLY_TO_A8
94 // adjust width based on gamma
egdaniel4ca2e602015-11-18 08:01:26 -080095 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -070096#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000097
Ethan Nicholasf7b88202017-09-18 14:10:39 -040098 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -080099 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700100 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700101 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800102 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000103
jvanverth354eba52015-03-16 11:32:49 -0700104 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700105#ifdef SK_VULKAN
106 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));",
107 st.fsIn());
108#else
109 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
egdaniel4ca2e602015-11-18 08:01:26 -0800110 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));",
111 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700112#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800113 } else if (isSimilarity) {
114 // For similarity transform, we adjust the effect of the transformation on the distance
115 // by using the length of the gradient of the texture coordinates. We use st coordinates
116 // to ensure we're mapping 1:1 from texel space to pixel space.
117 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
118
119 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700120#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400121 fragBuilder->codeAppendf("half st_grad_len = length(dFdx(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700122#else
123 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400124 fragBuilder->codeAppendf("half st_grad_len = length(dFdy(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700125#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800126 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverth354eba52015-03-16 11:32:49 -0700127 } else {
128 // For general transforms, to determine the amount of correction we multiply a unit
129 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
130 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400131 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700132 // the length of the gradient may be 0, so we need to check for this
133 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400134 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800135 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400136 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800137 fragBuilder->codeAppend("} else {");
138 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
139 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700140
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400141 fragBuilder->codeAppendf("half2 Jdx = dFdx(%s);", st.fsIn());
142 fragBuilder->codeAppendf("half2 Jdy = dFdy(%s);", st.fsIn());
143 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800144 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000145
146 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800147 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000148 }
brianosman0586f5c2016-04-12 12:48:21 -0700149
Jim Van Verth90e89b32017-07-06 16:36:55 -0400150 if (isAliased) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400151 fragBuilder->codeAppend("half val = distance > 0 ? 1.0 : 0.0;");
Jim Van Verth90e89b32017-07-06 16:36:55 -0400152 } else if (isGammaCorrect) {
153 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
154 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want
155 // distance mapped linearly to coverage, so use a linear step:
brianosman0586f5c2016-04-12 12:48:21 -0700156 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400157 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0586f5c2016-04-12 12:48:21 -0700158 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400159 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0586f5c2016-04-12 12:48:21 -0700160 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000161
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400162 fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000163 }
164
bsalomona624bf32016-09-20 09:12:47 -0700165 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
166 FPCoordTransformIter&& transformIter) override {
Robert Phillips8296e752017-08-25 08:45:21 -0400167 const GrDistanceFieldA8TextGeoProc& dfa8gp = proc.cast<GrDistanceFieldA8TextGeoProc>();
168
jvanverth2d2a68c2014-06-10 06:42:56 -0700169#ifdef SK_GAMMA_APPLY_TO_A8
Robert Phillips8296e752017-08-25 08:45:21 -0400170 float distanceAdjust = dfa8gp.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700171 if (distanceAdjust != fDistanceAdjust) {
Robert Phillipse2538312017-08-24 17:47:23 +0000172 fDistanceAdjust = distanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400173 pdman.set1f(fDistanceAdjustUni, distanceAdjust);
jvanverth2d2a68c2014-06-10 06:42:56 -0700174 }
175#endif
joshualitt5559ca22015-05-21 15:50:36 -0700176
Brian Salomon7eae3e02018-08-07 14:02:38 +0000177 const SkISize& atlasSize = dfa8gp.atlasSize();
178 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
Robert Phillips8296e752017-08-25 08:45:21 -0400179
Brian Salomon7eae3e02018-08-07 14:02:38 +0000180 if (fAtlasSize != atlasSize) {
181 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
182 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400183 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500184 this->setTransformDataHelper(dfa8gp.localMatrix(), pdman, &transformIter);
commit-bot@chromium.org8fe2ee12014-03-26 18:03:05 +0000185 }
186
robertphillips46d36f02015-01-18 08:14:14 -0800187 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500188 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700189 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700190 const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800191 uint32_t key = dfTexEffect.getFlags();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800192 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400193 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000194 }
195
jvanverth@google.comd830d132013-11-11 20:54:09 +0000196private:
mtklein50282b42015-01-22 07:59:52 -0800197#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500198 float fDistanceAdjust = -1.f;
jvanverth21deace2015-04-01 12:43:48 -0700199 UniformHandle fDistanceAdjustUni;
mtklein50282b42015-01-22 07:59:52 -0800200#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500201 SkISize fAtlasSize = {0, 0};
Robert Phillips8296e752017-08-25 08:45:21 -0400202 UniformHandle fAtlasSizeInvUniform;
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000203
egdaniele659a582015-11-13 09:55:43 -0800204 typedef GrGLSLGeometryProcessor INHERITED;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000205};
206
207///////////////////////////////////////////////////////////////////////////////
208
Brian Osman4a3f5c82018-09-18 16:16:38 -0400209GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(const GrShaderCaps& caps,
210 const sk_sp<GrTextureProxy>* proxies,
Brian Salomon92be2f72018-06-19 14:33:47 -0400211 int numProxies,
212 const GrSamplerState& params,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500213#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon92be2f72018-06-19 14:33:47 -0400214 float distanceAdjust,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500215#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400216 uint32_t flags,
217 const SkMatrix& localMatrix)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400218 : INHERITED(kGrDistanceFieldA8TextGeoProc_ClassID)
Brian Salomon92be2f72018-06-19 14:33:47 -0400219 , fLocalMatrix(localMatrix)
220 , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500221#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400222 , fDistanceAdjust(distanceAdjust)
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500223#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400224{
Robert Phillips4bc70112018-03-01 10:24:02 -0500225 SkASSERT(numProxies <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500226 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400227
Brian Salomon5c6ac642017-12-19 11:09:32 -0500228 if (flags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400229 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500230 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400231 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500232 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500233 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType };
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000234 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
235 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500236 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400237
Brian Salomon7eae3e02018-08-07 14:02:38 +0000238 if (numProxies) {
239 fAtlasSize = proxies[0]->isize();
240 }
Robert Phillips4bc70112018-03-01 10:24:02 -0500241 for (int i = 0; i < numProxies; ++i) {
242 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000243 SkASSERT(proxies[i]->isize() == fAtlasSize);
244 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400245 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400246 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500247}
248
Robert Phillips4bc70112018-03-01 10:24:02 -0500249void GrDistanceFieldA8TextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
250 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400251 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500252 SkASSERT(numProxies <= kMaxTextures);
253
Brian Salomon7eae3e02018-08-07 14:02:38 +0000254 if (!fTextureSamplers[0].isInitialized()) {
255 fAtlasSize = proxies[0]->isize();
256 }
257
Robert Phillips4bc70112018-03-01 10:24:02 -0500258 for (int i = 0; i < numProxies; ++i) {
259 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000260 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500261 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000262 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400263 }
264 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400265 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400266}
267
Brian Salomon94efbf52016-11-29 13:43:05 -0500268void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800269 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700270 GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800271}
272
Brian Salomon94efbf52016-11-29 13:43:05 -0500273GrGLSLPrimitiveProcessor*
274GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700275 return new GrGLDistanceFieldA8TextGeoProc();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000276}
277
278///////////////////////////////////////////////////////////////////////////////
279
jvanverth502286d2015-04-08 12:37:51 -0700280GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000281
Hal Canary6f6961e2017-01-31 13:50:44 -0500282#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700283sk_sp<GrGeometryProcessor> GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) {
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500284 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
285 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400286 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
287 d->textureProxy(texIdx),
288 nullptr,
289 nullptr,
290 nullptr
291 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500292
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400293 GrSamplerState::WrapMode wrapModes[2];
294 GrTest::TestWrapModes(d->fRandom, wrapModes);
295 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
296 ? GrSamplerState::Filter::kBilerp
297 : GrSamplerState::Filter::kNearest);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000298
jvanverthcf371bb2016-03-10 11:10:43 -0800299 uint32_t flags = 0;
300 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
301 if (flags & kSimilarity_DistanceFieldEffectFlag) {
302 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
303 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500304 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Mike Reede1367b42017-12-15 14:28:56 -0500305#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500306 float lum = d->fRandom->nextF();
Mike Reede1367b42017-12-15 14:28:56 -0500307#endif
Brian Osman4a3f5c82018-09-18 16:16:38 -0400308 return GrDistanceFieldA8TextGeoProc::Make(*d->caps()->shaderCaps(),
309 proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400310 samplerState,
jvanverth2d2a68c2014-06-10 06:42:56 -0700311#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500312 lum,
jvanverth2d2a68c2014-06-10 06:42:56 -0700313#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500314 flags, localMatrix);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000315}
Hal Canary6f6961e2017-01-31 13:50:44 -0500316#endif
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000317
318///////////////////////////////////////////////////////////////////////////////
319
egdaniele659a582015-11-13 09:55:43 -0800320class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700321public:
joshualitt465283c2015-09-11 08:19:35 -0700322 GrGLDistanceFieldPathGeoProc()
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400323 : fMatrix(SkMatrix::InvalidMatrix())
Robert Phillips8296e752017-08-25 08:45:21 -0400324 , fAtlasSize({0,0}) {
325 }
jvanverthfa38a302014-10-06 05:59:05 -0700326
mtklein36352bf2015-03-25 18:17:31 -0700327 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
Brian Salomon5c6ac642017-12-19 11:09:32 -0500328 const GrDistanceFieldPathGeoProc& dfPathEffect =
329 args.fGP.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700330
Chris Dalton60283612018-02-14 13:38:14 -0700331 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
jvanverthfa38a302014-10-06 05:59:05 -0700332
egdaniel4ca2e602015-11-18 08:01:26 -0800333 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800334 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800335 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800336
337 // emit attributes
Brian Salomon5c6ac642017-12-19 11:09:32 -0500338 varyingHandler->emitAttributes(dfPathEffect);
joshualittabb52a12015-01-13 15:02:10 -0800339
Robert Phillips8296e752017-08-25 08:45:21 -0400340 const char* atlasSizeInvName;
341 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400342 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400343 kHigh_GrSLPrecision,
344 "AtlasSizeInv",
345 &atlasSizeInvName);
346
Chris Dalton27372882017-12-08 13:34:21 -0700347 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000348 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
349 GrGLSLVarying texIdx(texIdxType);
Chris Dalton27372882017-12-08 13:34:21 -0700350 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -0400351 append_index_uv_varyings(args, dfPathEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
352 &texIdx, &st);
jvanverthfa38a302014-10-06 05:59:05 -0700353
joshualitt9b989322014-12-15 14:16:27 -0800354 // setup pass through color
Brian Salomon5c6ac642017-12-19 11:09:32 -0500355 varyingHandler->addPassThroughAttribute(dfPathEffect.inColor(), args.fOutputColor);
reede4ef1ca2015-02-17 18:38:38 -0800356
Brian Salomon5c6ac642017-12-19 11:09:32 -0500357 if (dfPathEffect.matrix().hasPerspective()) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400358 // Setup position
359 this->writeOutputPosition(vertBuilder,
360 uniformHandler,
361 gpArgs,
Brian Salomon92be2f72018-06-19 14:33:47 -0400362 dfPathEffect.inPosition().name(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500363 dfPathEffect.matrix(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400364 &fMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -0800365
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400366 // emit transforms
367 this->emitTransforms(vertBuilder,
368 varyingHandler,
369 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400370 dfPathEffect.inPosition().asShaderVar(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400371 args.fFPCoordTransformHandler);
372 } else {
373 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -0400374 this->writeOutputPosition(vertBuilder, gpArgs, dfPathEffect.inPosition().name());
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400375
376 // emit transforms
377 this->emitTransforms(vertBuilder,
378 varyingHandler,
379 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400380 dfPathEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500381 dfPathEffect.matrix(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400382 args.fFPCoordTransformHandler);
383 }
joshualitt4973d9d2014-11-08 09:24:25 -0800384
jvanverthfdf7ccc2015-01-27 08:19:33 -0800385 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400386 fragBuilder->codeAppendf("float2 uv = %s;", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400387 fragBuilder->codeAppend("half4 texColor;");
Brian Salomon5c6ac642017-12-19 11:09:32 -0500388 append_multitexture_lookup(args, dfPathEffect.numTextureSamplers(), texIdx, "uv",
389 "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -0800390
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400391 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400392 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverthfa38a302014-10-06 05:59:05 -0700393
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400394 fragBuilder->codeAppend("half afwidth;");
Brian Salomon5c6ac642017-12-19 11:09:32 -0500395 bool isUniformScale = (dfPathEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
396 kUniformScale_DistanceFieldEffectMask;
397 bool isSimilarity = SkToBool(dfPathEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosman0e3c5542016-04-13 13:56:21 -0700398 bool isGammaCorrect =
Brian Salomon5c6ac642017-12-19 11:09:32 -0500399 SkToBool(dfPathEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
jvanverthcf371bb2016-03-10 11:10:43 -0800400 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700401 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700402 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800403 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700404
jvanverthfa38a302014-10-06 05:59:05 -0700405 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700406#ifdef SK_VULKAN
Robert Phillips8296e752017-08-25 08:45:21 -0400407 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));",
408 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700409#else
410 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Robert Phillips8296e752017-08-25 08:45:21 -0400411 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));",
412 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700413#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800414 } else if (isSimilarity) {
415 // For similarity transform, we adjust the effect of the transformation on the distance
416 // by using the length of the gradient of the texture coordinates. We use st coordinates
417 // to ensure we're mapping 1:1 from texel space to pixel space.
jvanverthcf371bb2016-03-10 11:10:43 -0800418
419 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700420#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400421 fragBuilder->codeAppendf("half st_grad_len = length(dFdx(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700422#else
423 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400424 fragBuilder->codeAppendf("half st_grad_len = length(dFdy(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700425#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800426 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverthfa38a302014-10-06 05:59:05 -0700427 } else {
jvanverth354eba52015-03-16 11:32:49 -0700428 // For general transforms, to determine the amount of correction we multiply a unit
429 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
430 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400431 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700432 // the length of the gradient may be 0, so we need to check for this
433 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400434 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800435 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400436 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800437 fragBuilder->codeAppend("} else {");
438 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
439 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700440
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400441 fragBuilder->codeAppendf("half2 Jdx = dFdx(%s);", st.fsIn());
442 fragBuilder->codeAppendf("half2 Jdy = dFdy(%s);", st.fsIn());
443 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
444 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
jvanverthfa38a302014-10-06 05:59:05 -0700445
446 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800447 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
jvanverthfa38a302014-10-06 05:59:05 -0700448 }
brianosman0e3c5542016-04-13 13:56:21 -0700449 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
450 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
451 // mapped linearly to coverage, so use a linear step:
452 if (isGammaCorrect) {
453 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400454 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0e3c5542016-04-13 13:56:21 -0700455 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400456 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0e3c5542016-04-13 13:56:21 -0700457 }
jvanverthfa38a302014-10-06 05:59:05 -0700458
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400459 fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
jvanverthfa38a302014-10-06 05:59:05 -0700460 }
461
bsalomona624bf32016-09-20 09:12:47 -0700462 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
463 FPCoordTransformIter&& transformIter) override {
joshualitt9b989322014-12-15 14:16:27 -0800464
joshualitte578a952015-05-14 10:09:13 -0700465 const GrDistanceFieldPathGeoProc& dfpgp = proc.cast<GrDistanceFieldPathGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700466
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400467 if (dfpgp.matrix().hasPerspective() && !fMatrix.cheapEqualTo(dfpgp.matrix())) {
468 fMatrix = dfpgp.matrix();
469 float matrix[3 * 3];
470 GrGLSLGetMatrix<3>(matrix, fMatrix);
471 pdman.setMatrix3f(fMatrixUniform, matrix);
joshualitt5559ca22015-05-21 15:50:36 -0700472 }
Robert Phillips8296e752017-08-25 08:45:21 -0400473
Brian Salomon7eae3e02018-08-07 14:02:38 +0000474 const SkISize& atlasSize = dfpgp.atlasSize();
475 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
476 if (fAtlasSize != atlasSize) {
477 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
478 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400479 }
480
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400481 if (dfpgp.matrix().hasPerspective()) {
482 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
483 } else {
484 this->setTransformDataHelper(dfpgp.matrix(), pdman, &transformIter);
485 }
jvanverthfa38a302014-10-06 05:59:05 -0700486 }
487
robertphillips46d36f02015-01-18 08:14:14 -0800488 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500489 const GrShaderCaps&,
jvanverthfa38a302014-10-06 05:59:05 -0700490 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700491 const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700492
joshualitt8fc6c2d2014-12-22 15:27:05 -0800493 uint32_t key = dfTexEffect.getFlags();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400494 key |= ComputePosKey(dfTexEffect.matrix()) << 16;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800495 b->add32(key);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400496 b->add32(dfTexEffect.matrix().hasPerspective());
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400497 b->add32(dfTexEffect.numTextureSamplers());
jvanverthfa38a302014-10-06 05:59:05 -0700498 }
499
500private:
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400501 SkMatrix fMatrix; // view matrix if perspective, local matrix otherwise
502 UniformHandle fMatrixUniform;
Robert Phillips8296e752017-08-25 08:45:21 -0400503
504 SkISize fAtlasSize;
505 UniformHandle fAtlasSizeInvUniform;
jvanverthfa38a302014-10-06 05:59:05 -0700506
egdaniele659a582015-11-13 09:55:43 -0800507 typedef GrGLSLGeometryProcessor INHERITED;
jvanverthfa38a302014-10-06 05:59:05 -0700508};
509
510///////////////////////////////////////////////////////////////////////////////
Brian Salomon92be2f72018-06-19 14:33:47 -0400511
Brian Osman4a3f5c82018-09-18 16:16:38 -0400512GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(const GrShaderCaps& caps,
513 const SkMatrix& matrix,
Brian Salomon92be2f72018-06-19 14:33:47 -0400514 const sk_sp<GrTextureProxy>* proxies,
515 int numProxies,
516 const GrSamplerState& params,
517 uint32_t flags)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400518 : INHERITED(kGrDistanceFieldPathGeoProc_ClassID)
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400519 , fMatrix(matrix)
Brian Salomon92be2f72018-06-19 14:33:47 -0400520 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500521 SkASSERT(numProxies <= kMaxTextures);
Mike Klein5045e502018-06-19 01:40:57 +0000522 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400523
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500524 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
525 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000526 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
527 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500528 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000529
530 if (numProxies) {
531 fAtlasSize = proxies[0]->isize();
532 }
533
Robert Phillips4bc70112018-03-01 10:24:02 -0500534 for (int i = 0; i < numProxies; ++i) {
535 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000536 SkASSERT(proxies[i]->isize() == fAtlasSize);
537 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400538 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400539 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500540}
541
Robert Phillips4bc70112018-03-01 10:24:02 -0500542void GrDistanceFieldPathGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
543 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400544 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500545 SkASSERT(numProxies <= kMaxTextures);
546
Brian Salomon7eae3e02018-08-07 14:02:38 +0000547 if (!fTextureSamplers[0].isInitialized()) {
548 fAtlasSize = proxies[0]->isize();
549 }
550
Robert Phillips4bc70112018-03-01 10:24:02 -0500551 for (int i = 0; i < numProxies; ++i) {
552 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000553 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500554
555 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000556 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400557 }
558 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400559 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400560}
561
Brian Salomon94efbf52016-11-29 13:43:05 -0500562void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800563 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700564 GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800565}
566
Brian Salomon94efbf52016-11-29 13:43:05 -0500567GrGLSLPrimitiveProcessor*
568GrDistanceFieldPathGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700569 return new GrGLDistanceFieldPathGeoProc();
jvanverthfa38a302014-10-06 05:59:05 -0700570}
571
572///////////////////////////////////////////////////////////////////////////////
573
jvanverth502286d2015-04-08 12:37:51 -0700574GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc);
jvanverthfa38a302014-10-06 05:59:05 -0700575
Hal Canary6f6961e2017-01-31 13:50:44 -0500576#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700577sk_sp<GrGeometryProcessor> GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700578 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
579 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400580 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
581 d->textureProxy(texIdx),
582 nullptr,
583 nullptr,
584 nullptr
585 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500586
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400587 GrSamplerState::WrapMode wrapModes[2];
588 GrTest::TestWrapModes(d->fRandom, wrapModes);
589 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
590 ? GrSamplerState::Filter::kBilerp
591 : GrSamplerState::Filter::kNearest);
jvanverthfa38a302014-10-06 05:59:05 -0700592
jvanverthcf371bb2016-03-10 11:10:43 -0800593 uint32_t flags = 0;
594 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
595 if (flags & kSimilarity_DistanceFieldEffectFlag) {
596 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
597 }
598
Brian Osman4a3f5c82018-09-18 16:16:38 -0400599 return GrDistanceFieldPathGeoProc::Make(*d->caps()->shaderCaps(),
600 GrTest::TestMatrix(d->fRandom),
Robert Phillips4bc70112018-03-01 10:24:02 -0500601 proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400602 samplerState,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400603 flags);
jvanverthfa38a302014-10-06 05:59:05 -0700604}
Hal Canary6f6961e2017-01-31 13:50:44 -0500605#endif
jvanverthfa38a302014-10-06 05:59:05 -0700606
607///////////////////////////////////////////////////////////////////////////////
608
egdaniele659a582015-11-13 09:55:43 -0800609class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000610public:
Brian Salomon5c6ac642017-12-19 11:09:32 -0500611 GrGLDistanceFieldLCDTextGeoProc() : fAtlasSize({0, 0}) {
jvanverth502286d2015-04-08 12:37:51 -0700612 fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
jvanverth21deace2015-04-01 12:43:48 -0700613 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000614
mtklein36352bf2015-03-25 18:17:31 -0700615 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700616 const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
617 args.fGP.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000618
egdaniel4ca2e602015-11-18 08:01:26 -0800619 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800620 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800621 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800622
623 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800624 varyingHandler->emitAttributes(dfTexEffect);
egdaniel4ca2e602015-11-18 08:01:26 -0800625
Robert Phillips8296e752017-08-25 08:45:21 -0400626 const char* atlasSizeInvName;
627 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400628 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400629 kHigh_GrSLPrecision,
630 "AtlasSizeInv",
631 &atlasSizeInvName);
632
Chris Dalton60283612018-02-14 13:38:14 -0700633 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittabb52a12015-01-13 15:02:10 -0800634
joshualitt9b989322014-12-15 14:16:27 -0800635 // setup pass through color
Brian Salomonbfd51832017-01-04 13:22:08 -0500636 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualitt9b989322014-12-15 14:16:27 -0800637
joshualittabb52a12015-01-13 15:02:10 -0800638 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -0400639 gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
joshualitt4973d9d2014-11-08 09:24:25 -0800640
joshualittabb52a12015-01-13 15:02:10 -0800641 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800642 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800643 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800644 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400645 dfTexEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500646 dfTexEffect.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -0700647 args.fFPCoordTransformHandler);
joshualittabb52a12015-01-13 15:02:10 -0800648
jvanverthbb4a1cf2015-04-07 09:06:00 -0700649 // set up varyings
Chris Dalton27372882017-12-08 13:34:21 -0700650 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000651 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
652 GrGLSLVarying texIdx(texIdxType);
Chris Dalton27372882017-12-08 13:34:21 -0700653 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -0400654 append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -0400655 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -0400656
Chris Dalton27372882017-12-08 13:34:21 -0700657 GrGLSLVarying delta(kFloat_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600658 varyingHandler->addVarying("Delta", &delta);
Robert Phillips8296e752017-08-25 08:45:21 -0400659 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) {
660 vertBuilder->codeAppendf("%s = -%s.x/3.0;", delta.vsOut(), atlasSizeInvName);
661 } else {
662 vertBuilder->codeAppendf("%s = %s.x/3.0;", delta.vsOut(), atlasSizeInvName);
663 }
664
665 // add frag shader code
jvanverthcf371bb2016-03-10 11:10:43 -0800666 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
667 kUniformScale_DistanceFieldEffectMask;
668 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -0700669 bool isGammaCorrect =
670 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
joshualitt30ba4362014-08-21 20:18:45 -0700671
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000672 // create LCD offset adjusted by inverse of transform
jvanverthfdf7ccc2015-01-27 08:19:33 -0800673 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400674 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
joshualitt922c8b12015-08-07 09:55:23 -0700675
jvanverth78f07182014-07-30 06:17:59 -0700676 if (isUniformScale) {
jvanverthe499adf2016-07-20 12:22:14 -0700677#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400678 fragBuilder->codeAppendf("half st_grad_len = abs(dFdx(%s.x));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700679#else
680 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400681 fragBuilder->codeAppendf("half st_grad_len = abs(dFdy(%s.y));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700682#endif
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400683 fragBuilder->codeAppendf("half2 offset = half2(st_grad_len*%s, 0.0);", delta.fsIn());
jvanverthcf371bb2016-03-10 11:10:43 -0800684 } else if (isSimilarity) {
685 // For a similarity matrix with rotation, the gradient will not be aligned
686 // with the texel coordinate axes, so we need to calculate it.
jvanverthe499adf2016-07-20 12:22:14 -0700687#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400688 fragBuilder->codeAppendf("half2 st_grad = dFdx(%s);", st.fsIn());
689 fragBuilder->codeAppendf("half2 offset = %s*st_grad;", delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700690#else
jvanverthcf371bb2016-03-10 11:10:43 -0800691 // We use dFdy because of a Mali 400 bug, and rotate -90 degrees to
692 // get the gradient in the x direction.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400693 fragBuilder->codeAppendf("half2 st_grad = dFdy(%s);", st.fsIn());
694 fragBuilder->codeAppendf("half2 offset = %s*half2(st_grad.y, -st_grad.x);",
Robert Phillips8296e752017-08-25 08:45:21 -0400695 delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700696#endif
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400697 fragBuilder->codeAppend("half st_grad_len = length(st_grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000698 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400699 fragBuilder->codeAppendf("half2 st = %s;\n", st.fsIn());
jvanverthbb4a1cf2015-04-07 09:06:00 -0700700
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400701 fragBuilder->codeAppend("half2 Jdx = dFdx(st);");
702 fragBuilder->codeAppend("half2 Jdy = dFdy(st);");
703 fragBuilder->codeAppendf("half2 offset = %s*Jdx;", delta.fsIn());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000704 }
705
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400706 // sample the texture by index
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400707 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400708 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
709 texIdx, "uv", "texColor");
Jim Van Verth6c251d22017-09-08 12:29:07 -0400710
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400711 // green is distance to uv center
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400712 fragBuilder->codeAppend("half3 distance;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400713 fragBuilder->codeAppend("distance.y = texColor.r;");
714 // red is distance to left offset
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400715 fragBuilder->codeAppend("half2 uv_adjusted = uv - offset;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400716 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
717 texIdx, "uv_adjusted", "texColor");
718 fragBuilder->codeAppend("distance.x = texColor.r;");
719 // blue is distance to right offset
720 fragBuilder->codeAppend("uv_adjusted = uv + offset;");
721 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
722 texIdx, "uv_adjusted", "texColor");
723 fragBuilder->codeAppend("distance.z = texColor.r;");
724
725 fragBuilder->codeAppend("distance = "
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400726 "half3(" SK_DistanceFieldMultiplier ")*(distance - half3(" SK_DistanceFieldThreshold"));");
jvanverth2d2a68c2014-06-10 06:42:56 -0700727
jvanverth21deace2015-04-01 12:43:48 -0700728 // adjust width based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -0700729 const char* distanceAdjustUniName = nullptr;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400730 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -0800731 "DistanceAdjust", &distanceAdjustUniName);
egdaniel4ca2e602015-11-18 08:01:26 -0800732 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700733
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000734 // To be strictly correct, we should compute the anti-aliasing factor separately
735 // for each color component. However, this is only important when using perspective
736 // transformations, and even then using a single factor seems like a reasonable
737 // trade-off between quality and speed.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400738 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -0800739 if (isSimilarity) {
halcanary9d524f22016-03-29 09:03:52 -0700740 // For similarity transform (uniform scale-only is a subset of this), we adjust for the
741 // effect of the transformation on the distance by using the length of the gradient of
jvanverthcf371bb2016-03-10 11:10:43 -0800742 // the texture coordinates. We use st coordinates to ensure we're mapping 1:1 from texel
743 // space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700744
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000745 // this gives us a smooth step across approximately one fragment
jvanverthcf371bb2016-03-10 11:10:43 -0800746 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*st_grad_len;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000747 } else {
jvanverth354eba52015-03-16 11:32:49 -0700748 // For general transforms, to determine the amount of correction we multiply a unit
749 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
750 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400751 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance.r), dFdy(distance.r));");
jvanverthd68a5502015-03-16 12:58:43 -0700752 // the length of the gradient may be 0, so we need to check for this
753 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400754 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800755 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400756 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800757 fragBuilder->codeAppend("} else {");
758 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
759 fragBuilder->codeAppend("}");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400760 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800761 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000762
763 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800764 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000765 }
766
brianosman0586f5c2016-04-12 12:48:21 -0700767 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
768 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
769 // mapped linearly to coverage, so use a linear step:
brianosmanb461d342016-04-13 13:10:14 -0700770 if (isGammaCorrect) {
Greg Daniel55923822017-05-22 16:34:34 -0400771 fragBuilder->codeAppendf("%s = "
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400772 "half4(saturate((distance + half3(afwidth)) / half3(2.0 * afwidth)), 1.0);",
Greg Daniel55923822017-05-22 16:34:34 -0400773 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700774 } else {
Greg Daniel55923822017-05-22 16:34:34 -0400775 fragBuilder->codeAppendf(
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400776 "%s = half4(smoothstep(half3(-afwidth), half3(afwidth), distance), 1.0);",
Greg Daniel55923822017-05-22 16:34:34 -0400777 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700778 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000779 }
780
bsalomona624bf32016-09-20 09:12:47 -0700781 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& processor,
782 FPCoordTransformIter&& transformIter) override {
jvanverth21deace2015-04-01 12:43:48 -0700783 SkASSERT(fDistanceAdjustUni.isValid());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000784
joshualitt5559ca22015-05-21 15:50:36 -0700785 const GrDistanceFieldLCDTextGeoProc& dflcd = processor.cast<GrDistanceFieldLCDTextGeoProc>();
786 GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700787 if (wa != fDistanceAdjust) {
788 pdman.set3f(fDistanceAdjustUni,
789 wa.fR,
790 wa.fG,
791 wa.fB);
792 fDistanceAdjust = wa;
jvanverth2d2a68c2014-06-10 06:42:56 -0700793 }
joshualitt9b989322014-12-15 14:16:27 -0800794
Brian Salomon7eae3e02018-08-07 14:02:38 +0000795 const SkISize& atlasSize = dflcd.atlasSize();
796 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
797 if (fAtlasSize != atlasSize) {
798 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
799 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400800 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500801 this->setTransformDataHelper(dflcd.localMatrix(), pdman, &transformIter);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000802 }
803
robertphillips46d36f02015-01-18 08:14:14 -0800804 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500805 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700806 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700807 const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000808
joshualitt8fc6c2d2014-12-22 15:27:05 -0800809 uint32_t key = dfTexEffect.getFlags();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800810 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400811 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000812 }
813
814private:
jvanverth502286d2015-04-08 12:37:51 -0700815 GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400816 UniformHandle fDistanceAdjustUni;
817
818 SkISize fAtlasSize;
819 UniformHandle fAtlasSizeInvUniform;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000820
egdaniele659a582015-11-13 09:55:43 -0800821 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000822};
823
824///////////////////////////////////////////////////////////////////////////////
Mike Klein5045e502018-06-19 01:40:57 +0000825
Brian Osman4a3f5c82018-09-18 16:16:38 -0400826GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(const GrShaderCaps& caps,
827 const sk_sp<GrTextureProxy>* proxies,
Brian Salomon92be2f72018-06-19 14:33:47 -0400828 int numProxies,
829 const GrSamplerState& params,
830 DistanceAdjust distanceAdjust,
831 uint32_t flags,
832 const SkMatrix& localMatrix)
833 : INHERITED(kGrDistanceFieldLCDTextGeoProc_ClassID)
834 , fLocalMatrix(localMatrix)
835 , fDistanceAdjust(distanceAdjust)
836 , fFlags(flags & kLCD_DistanceFieldEffectMask) {
837 SkASSERT(numProxies <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500838 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag));
Brian Salomon92be2f72018-06-19 14:33:47 -0400839
Brian Salomon5c6ac642017-12-19 11:09:32 -0500840 if (fFlags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400841 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500842 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400843 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500844 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500845 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000846 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
847 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500848 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400849
Brian Salomon7eae3e02018-08-07 14:02:38 +0000850 if (numProxies) {
851 fAtlasSize = proxies[0]->isize();
852 }
853
Robert Phillips4bc70112018-03-01 10:24:02 -0500854 for (int i = 0; i < numProxies; ++i) {
855 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000856 SkASSERT(proxies[i]->isize() == fAtlasSize);
857 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400858 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400859 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500860}
861
Robert Phillips4bc70112018-03-01 10:24:02 -0500862void GrDistanceFieldLCDTextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
863 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400864 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500865 SkASSERT(numProxies <= kMaxTextures);
866
Brian Salomon7eae3e02018-08-07 14:02:38 +0000867 if (!fTextureSamplers[0].isInitialized()) {
868 fAtlasSize = proxies[0]->isize();
869 }
870
Robert Phillips4bc70112018-03-01 10:24:02 -0500871 for (int i = 0; i < numProxies; ++i) {
872 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000873 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500874
875 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000876 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400877 }
878 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400879 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400880}
881
Brian Salomon94efbf52016-11-29 13:43:05 -0500882void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800883 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700884 GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800885}
886
Brian Salomon94efbf52016-11-29 13:43:05 -0500887GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700888 return new GrGLDistanceFieldLCDTextGeoProc();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000889}
890
891///////////////////////////////////////////////////////////////////////////////
892
jvanverth502286d2015-04-08 12:37:51 -0700893GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000894
Hal Canary6f6961e2017-01-31 13:50:44 -0500895#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700896sk_sp<GrGeometryProcessor> GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700897 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
898 GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400899 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
900 d->textureProxy(texIdx),
901 nullptr,
902 nullptr,
903 nullptr
904 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500905
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400906 GrSamplerState::WrapMode wrapModes[2];
907 GrTest::TestWrapModes(d->fRandom, wrapModes);
908 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
909 ? GrSamplerState::Filter::kBilerp
910 : GrSamplerState::Filter::kNearest);
jvanverth21deace2015-04-01 12:43:48 -0700911 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
jvanverth78f07182014-07-30 06:17:59 -0700912 uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
jvanverthcf371bb2016-03-10 11:10:43 -0800913 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
914 if (flags & kSimilarity_DistanceFieldEffectFlag) {
915 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
916 }
joshualitt0067ff52015-07-08 14:26:19 -0700917 flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500918 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Brian Osman4a3f5c82018-09-18 16:16:38 -0400919 return GrDistanceFieldLCDTextGeoProc::Make(*d->caps()->shaderCaps(), proxies, 1, samplerState,
920 wa, flags, localMatrix);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000921}
Hal Canary6f6961e2017-01-31 13:50:44 -0500922#endif