blob: 92d404ad217c1ea42a9db3a13f314496bb344781 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/gpu/GrTexture.h"
9#include "src/core/SkDistanceFieldGen.h"
10#include "src/gpu/GrCaps.h"
11#include "src/gpu/GrShaderCaps.h"
12#include "src/gpu/effects/GrAtlasedShaderHelpers.h"
13#include "src/gpu/effects/GrDistanceFieldGeoProc.h"
14#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
15#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
16#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
17#include "src/gpu/glsl/GrGLSLUniformHandler.h"
18#include "src/gpu/glsl/GrGLSLUtil.h"
19#include "src/gpu/glsl/GrGLSLVarying.h"
20#include "src/gpu/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 "AtlasSizeInv",
45 &atlasSizeInvName);
jvanverth21deace2015-04-01 12:43:48 -070046#ifdef SK_GAMMA_APPLY_TO_A8
47 // adjust based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -070048 const char* distanceAdjustUniName = nullptr;
jvanverth21deace2015-04-01 12:43:48 -070049 // width, height, 1/(3*width)
Ethan Nicholasf7b88202017-09-18 14:10:39 -040050 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -080051 "DistanceAdjust", &distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -070052#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000053
joshualitt9b989322014-12-15 14:16:27 -080054 // Setup pass through color
Brian Salomonbfd51832017-01-04 13:22:08 -050055 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000056
joshualittabb52a12015-01-13 15:02:10 -080057 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -040058 gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
joshualitt2dd1ae02014-12-03 06:24:10 -080059
joshualittabb52a12015-01-13 15:02:10 -080060 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -080061 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -080062 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -080063 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -040064 dfTexEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -050065 dfTexEffect.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -070066 args.fFPCoordTransformHandler);
joshualitt4973d9d2014-11-08 09:24:25 -080067
jvanverthbb4a1cf2015-04-07 09:06:00 -070068 // add varyings
Chris Dalton27372882017-12-08 13:34:21 -070069 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verth1694a862018-12-17 19:48:42 +000070 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
71 GrGLSLVarying texIdx(texIdxType);
Chris Dalton27372882017-12-08 13:34:21 -070072 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -040073 append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -040074 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -040075
jvanverthcf371bb2016-03-10 11:10:43 -080076 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
77 kUniformScale_DistanceFieldEffectMask;
jvanverthbb4a1cf2015-04-07 09:06:00 -070078 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -070079 bool isGammaCorrect =
80 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
Jim Van Verth90e89b32017-07-06 16:36:55 -040081 bool isAliased =
82 SkToBool(dfTexEffect.getFlags() & kAliased_DistanceFieldEffectFlag);
halcanary9d524f22016-03-29 09:03:52 -070083
jvanverthfdf7ccc2015-01-27 08:19:33 -080084 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -040085 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -040086 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040087 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
88 texIdx, "uv", "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -080089
Ethan Nicholasf7b88202017-09-18 14:10:39 -040090 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -040091 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverth21deace2015-04-01 12:43:48 -070092#ifdef SK_GAMMA_APPLY_TO_A8
93 // adjust width based on gamma
egdaniel4ca2e602015-11-18 08:01:26 -080094 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -070095#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000096
Ethan Nicholasf7b88202017-09-18 14:10:39 -040097 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -080098 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -070099 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700100 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800101 // 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 +0000102
jvanverth354eba52015-03-16 11:32:49 -0700103 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700104#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500105 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
106 "*half(dFdx(%s.x)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700107#else
108 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500109 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
110 "*half(dFdy(%s.y)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700111#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800112 } else if (isSimilarity) {
113 // For similarity transform, we adjust the effect of the transformation on the distance
114 // by using the length of the gradient of the texture coordinates. We use st coordinates
115 // to ensure we're mapping 1:1 from texel space to pixel space.
116 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
117
118 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700119#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500120 fragBuilder->codeAppendf("half st_grad_len = length(half2(dFdx(%s)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700121#else
122 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500123 fragBuilder->codeAppendf("half st_grad_len = length(half2(dFdy(%s)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700124#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800125 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverth354eba52015-03-16 11:32:49 -0700126 } else {
127 // For general transforms, to determine the amount of correction we multiply a unit
128 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
129 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500130 fragBuilder->codeAppend("half2 dist_grad = half2(float2(dFdx(distance), "
131 "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 {");
Ethan Nicholase1f55022019-02-05 17:17:40 -0500138 fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
egdaniel4ca2e602015-11-18 08:01:26 -0800139 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700140
Ethan Nicholase1f55022019-02-05 17:17:40 -0500141 fragBuilder->codeAppendf("half2 Jdx = half2(dFdx(%s));", st.fsIn());
142 fragBuilder->codeAppendf("half2 Jdy = half2(dFdy(%s));", st.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400143 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 }
Jim Van Verth1694a862018-12-17 19:48:42 +0000233 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);
Greg Daniel2c3398d2019-06-19 11:58:01 -0400244 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params,
245 proxies[i]->textureSwizzle());
Jim Van Vertha950b632017-09-12 11:54:11 -0400246 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400247 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500248}
249
Robert Phillips4bc70112018-03-01 10:24:02 -0500250void GrDistanceFieldA8TextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
251 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400252 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500253 SkASSERT(numProxies <= kMaxTextures);
254
Brian Salomon7eae3e02018-08-07 14:02:38 +0000255 if (!fTextureSamplers[0].isInitialized()) {
256 fAtlasSize = proxies[0]->isize();
257 }
258
Robert Phillips4bc70112018-03-01 10:24:02 -0500259 for (int i = 0; i < numProxies; ++i) {
260 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000261 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500262 if (!fTextureSamplers[i].isInitialized()) {
Greg Daniel2c3398d2019-06-19 11:58:01 -0400263 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params,
264 proxies[i]->textureSwizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400265 }
266 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400267 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400268}
269
Brian Salomon94efbf52016-11-29 13:43:05 -0500270void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800271 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700272 GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800273}
274
Brian Salomon94efbf52016-11-29 13:43:05 -0500275GrGLSLPrimitiveProcessor*
276GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700277 return new GrGLDistanceFieldA8TextGeoProc();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000278}
279
280///////////////////////////////////////////////////////////////////////////////
281
jvanverth502286d2015-04-08 12:37:51 -0700282GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000283
Hal Canary6f6961e2017-01-31 13:50:44 -0500284#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700285sk_sp<GrGeometryProcessor> GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) {
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500286 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
287 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400288 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
289 d->textureProxy(texIdx),
290 nullptr,
291 nullptr,
292 nullptr
293 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500294
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400295 GrSamplerState::WrapMode wrapModes[2];
296 GrTest::TestWrapModes(d->fRandom, wrapModes);
297 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
298 ? GrSamplerState::Filter::kBilerp
299 : GrSamplerState::Filter::kNearest);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000300
jvanverthcf371bb2016-03-10 11:10:43 -0800301 uint32_t flags = 0;
302 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
303 if (flags & kSimilarity_DistanceFieldEffectFlag) {
304 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
305 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500306 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Mike Reede1367b42017-12-15 14:28:56 -0500307#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500308 float lum = d->fRandom->nextF();
Mike Reede1367b42017-12-15 14:28:56 -0500309#endif
Brian Osman4a3f5c82018-09-18 16:16:38 -0400310 return GrDistanceFieldA8TextGeoProc::Make(*d->caps()->shaderCaps(),
311 proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400312 samplerState,
jvanverth2d2a68c2014-06-10 06:42:56 -0700313#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500314 lum,
jvanverth2d2a68c2014-06-10 06:42:56 -0700315#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500316 flags, localMatrix);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000317}
Hal Canary6f6961e2017-01-31 13:50:44 -0500318#endif
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000319
320///////////////////////////////////////////////////////////////////////////////
321
egdaniele659a582015-11-13 09:55:43 -0800322class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700323public:
joshualitt465283c2015-09-11 08:19:35 -0700324 GrGLDistanceFieldPathGeoProc()
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400325 : fMatrix(SkMatrix::InvalidMatrix())
Robert Phillips8296e752017-08-25 08:45:21 -0400326 , fAtlasSize({0,0}) {
327 }
jvanverthfa38a302014-10-06 05:59:05 -0700328
mtklein36352bf2015-03-25 18:17:31 -0700329 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
Brian Salomon5c6ac642017-12-19 11:09:32 -0500330 const GrDistanceFieldPathGeoProc& dfPathEffect =
331 args.fGP.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700332
Chris Dalton60283612018-02-14 13:38:14 -0700333 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
jvanverthfa38a302014-10-06 05:59:05 -0700334
egdaniel4ca2e602015-11-18 08:01:26 -0800335 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800336 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800337 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800338
339 // emit attributes
Brian Salomon5c6ac642017-12-19 11:09:32 -0500340 varyingHandler->emitAttributes(dfPathEffect);
joshualittabb52a12015-01-13 15:02:10 -0800341
Robert Phillips8296e752017-08-25 08:45:21 -0400342 const char* atlasSizeInvName;
343 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400344 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400345 "AtlasSizeInv",
346 &atlasSizeInvName);
347
Chris Dalton27372882017-12-08 13:34:21 -0700348 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verth1694a862018-12-17 19:48:42 +0000349 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
350 GrGLSLVarying texIdx(texIdxType);
Chris Dalton27372882017-12-08 13:34:21 -0700351 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -0400352 append_index_uv_varyings(args, dfPathEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
353 &texIdx, &st);
jvanverthfa38a302014-10-06 05:59:05 -0700354
joshualitt9b989322014-12-15 14:16:27 -0800355 // setup pass through color
Brian Salomon5c6ac642017-12-19 11:09:32 -0500356 varyingHandler->addPassThroughAttribute(dfPathEffect.inColor(), args.fOutputColor);
reede4ef1ca2015-02-17 18:38:38 -0800357
Brian Salomon5c6ac642017-12-19 11:09:32 -0500358 if (dfPathEffect.matrix().hasPerspective()) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400359 // Setup position
360 this->writeOutputPosition(vertBuilder,
361 uniformHandler,
362 gpArgs,
Brian Salomon92be2f72018-06-19 14:33:47 -0400363 dfPathEffect.inPosition().name(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500364 dfPathEffect.matrix(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400365 &fMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -0800366
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400367 // emit transforms
368 this->emitTransforms(vertBuilder,
369 varyingHandler,
370 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400371 dfPathEffect.inPosition().asShaderVar(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400372 args.fFPCoordTransformHandler);
373 } else {
374 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -0400375 this->writeOutputPosition(vertBuilder, gpArgs, dfPathEffect.inPosition().name());
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400376
377 // emit transforms
378 this->emitTransforms(vertBuilder,
379 varyingHandler,
380 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400381 dfPathEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500382 dfPathEffect.matrix(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400383 args.fFPCoordTransformHandler);
384 }
joshualitt4973d9d2014-11-08 09:24:25 -0800385
jvanverthfdf7ccc2015-01-27 08:19:33 -0800386 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400387 fragBuilder->codeAppendf("float2 uv = %s;", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400388 fragBuilder->codeAppend("half4 texColor;");
Brian Salomon5c6ac642017-12-19 11:09:32 -0500389 append_multitexture_lookup(args, dfPathEffect.numTextureSamplers(), texIdx, "uv",
390 "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -0800391
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400392 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400393 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverthfa38a302014-10-06 05:59:05 -0700394
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400395 fragBuilder->codeAppend("half afwidth;");
Brian Salomon5c6ac642017-12-19 11:09:32 -0500396 bool isUniformScale = (dfPathEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
397 kUniformScale_DistanceFieldEffectMask;
398 bool isSimilarity = SkToBool(dfPathEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosman0e3c5542016-04-13 13:56:21 -0700399 bool isGammaCorrect =
Brian Salomon5c6ac642017-12-19 11:09:32 -0500400 SkToBool(dfPathEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
jvanverthcf371bb2016-03-10 11:10:43 -0800401 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700402 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700403 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800404 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700405
jvanverthfa38a302014-10-06 05:59:05 -0700406 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700407#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500408 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
409 "*half(dFdx(%s.x)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700410#else
411 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500412 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
413 "*half(dFdy(%s.y)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700414#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800415 } else if (isSimilarity) {
416 // For similarity transform, we adjust the effect of the transformation on the distance
417 // by using the length of the gradient of the texture coordinates. We use st coordinates
418 // to ensure we're mapping 1:1 from texel space to pixel space.
jvanverthcf371bb2016-03-10 11:10:43 -0800419
420 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700421#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500422 fragBuilder->codeAppendf("half st_grad_len = half(length(dFdx(%s)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700423#else
424 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500425 fragBuilder->codeAppendf("half st_grad_len = half(length(dFdy(%s)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700426#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800427 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverthfa38a302014-10-06 05:59:05 -0700428 } else {
jvanverth354eba52015-03-16 11:32:49 -0700429 // For general transforms, to determine the amount of correction we multiply a unit
430 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
431 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500432 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), "
433 "dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700434 // the length of the gradient may be 0, so we need to check for this
435 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400436 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800437 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400438 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800439 fragBuilder->codeAppend("} else {");
Ethan Nicholase1f55022019-02-05 17:17:40 -0500440 fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
egdaniel4ca2e602015-11-18 08:01:26 -0800441 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700442
Ethan Nicholase1f55022019-02-05 17:17:40 -0500443 fragBuilder->codeAppendf("half2 Jdx = half2(dFdx(%s));", st.fsIn());
444 fragBuilder->codeAppendf("half2 Jdy = half2(dFdy(%s));", st.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400445 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
446 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
jvanverthfa38a302014-10-06 05:59:05 -0700447
448 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800449 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
jvanverthfa38a302014-10-06 05:59:05 -0700450 }
brianosman0e3c5542016-04-13 13:56:21 -0700451 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
452 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
453 // mapped linearly to coverage, so use a linear step:
454 if (isGammaCorrect) {
455 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400456 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0e3c5542016-04-13 13:56:21 -0700457 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400458 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0e3c5542016-04-13 13:56:21 -0700459 }
jvanverthfa38a302014-10-06 05:59:05 -0700460
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400461 fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
jvanverthfa38a302014-10-06 05:59:05 -0700462 }
463
bsalomona624bf32016-09-20 09:12:47 -0700464 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
465 FPCoordTransformIter&& transformIter) override {
joshualitt9b989322014-12-15 14:16:27 -0800466
joshualitte578a952015-05-14 10:09:13 -0700467 const GrDistanceFieldPathGeoProc& dfpgp = proc.cast<GrDistanceFieldPathGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700468
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400469 if (dfpgp.matrix().hasPerspective() && !fMatrix.cheapEqualTo(dfpgp.matrix())) {
470 fMatrix = dfpgp.matrix();
471 float matrix[3 * 3];
472 GrGLSLGetMatrix<3>(matrix, fMatrix);
473 pdman.setMatrix3f(fMatrixUniform, matrix);
joshualitt5559ca22015-05-21 15:50:36 -0700474 }
Robert Phillips8296e752017-08-25 08:45:21 -0400475
Brian Salomon7eae3e02018-08-07 14:02:38 +0000476 const SkISize& atlasSize = dfpgp.atlasSize();
477 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
478 if (fAtlasSize != atlasSize) {
479 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
480 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400481 }
482
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400483 if (dfpgp.matrix().hasPerspective()) {
484 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
485 } else {
486 this->setTransformDataHelper(dfpgp.matrix(), pdman, &transformIter);
487 }
jvanverthfa38a302014-10-06 05:59:05 -0700488 }
489
robertphillips46d36f02015-01-18 08:14:14 -0800490 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500491 const GrShaderCaps&,
jvanverthfa38a302014-10-06 05:59:05 -0700492 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700493 const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700494
joshualitt8fc6c2d2014-12-22 15:27:05 -0800495 uint32_t key = dfTexEffect.getFlags();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400496 key |= ComputePosKey(dfTexEffect.matrix()) << 16;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800497 b->add32(key);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400498 b->add32(dfTexEffect.matrix().hasPerspective());
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400499 b->add32(dfTexEffect.numTextureSamplers());
jvanverthfa38a302014-10-06 05:59:05 -0700500 }
501
502private:
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400503 SkMatrix fMatrix; // view matrix if perspective, local matrix otherwise
504 UniformHandle fMatrixUniform;
Robert Phillips8296e752017-08-25 08:45:21 -0400505
506 SkISize fAtlasSize;
507 UniformHandle fAtlasSizeInvUniform;
jvanverthfa38a302014-10-06 05:59:05 -0700508
egdaniele659a582015-11-13 09:55:43 -0800509 typedef GrGLSLGeometryProcessor INHERITED;
jvanverthfa38a302014-10-06 05:59:05 -0700510};
511
512///////////////////////////////////////////////////////////////////////////////
Brian Salomon92be2f72018-06-19 14:33:47 -0400513
Brian Osman4a3f5c82018-09-18 16:16:38 -0400514GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(const GrShaderCaps& caps,
515 const SkMatrix& matrix,
Brian Osmanc906d252018-12-04 11:17:46 -0500516 bool wideColor,
Brian Salomon92be2f72018-06-19 14:33:47 -0400517 const sk_sp<GrTextureProxy>* proxies,
518 int numProxies,
519 const GrSamplerState& params,
520 uint32_t flags)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400521 : INHERITED(kGrDistanceFieldPathGeoProc_ClassID)
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400522 , fMatrix(matrix)
Brian Salomon92be2f72018-06-19 14:33:47 -0400523 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500524 SkASSERT(numProxies <= kMaxTextures);
Mike Klein5045e502018-06-19 01:40:57 +0000525 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400526
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500527 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Osmanc906d252018-12-04 11:17:46 -0500528 fInColor = MakeColorAttribute("inColor", wideColor);
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000529 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
530 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500531 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000532
533 if (numProxies) {
534 fAtlasSize = proxies[0]->isize();
535 }
536
Robert Phillips4bc70112018-03-01 10:24:02 -0500537 for (int i = 0; i < numProxies; ++i) {
538 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000539 SkASSERT(proxies[i]->isize() == fAtlasSize);
Greg Daniel2c3398d2019-06-19 11:58:01 -0400540 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params,
541 proxies[i]->textureSwizzle());
Jim Van Vertha950b632017-09-12 11:54:11 -0400542 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400543 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500544}
545
Robert Phillips4bc70112018-03-01 10:24:02 -0500546void GrDistanceFieldPathGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
547 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400548 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500549 SkASSERT(numProxies <= kMaxTextures);
550
Brian Salomon7eae3e02018-08-07 14:02:38 +0000551 if (!fTextureSamplers[0].isInitialized()) {
552 fAtlasSize = proxies[0]->isize();
553 }
554
Robert Phillips4bc70112018-03-01 10:24:02 -0500555 for (int i = 0; i < numProxies; ++i) {
556 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000557 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500558
559 if (!fTextureSamplers[i].isInitialized()) {
Greg Daniel2c3398d2019-06-19 11:58:01 -0400560 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params,
561 proxies[i]->textureSwizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400562 }
563 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400564 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400565}
566
Brian Salomon94efbf52016-11-29 13:43:05 -0500567void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800568 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700569 GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800570}
571
Brian Salomon94efbf52016-11-29 13:43:05 -0500572GrGLSLPrimitiveProcessor*
573GrDistanceFieldPathGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700574 return new GrGLDistanceFieldPathGeoProc();
jvanverthfa38a302014-10-06 05:59:05 -0700575}
576
577///////////////////////////////////////////////////////////////////////////////
578
jvanverth502286d2015-04-08 12:37:51 -0700579GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc);
jvanverthfa38a302014-10-06 05:59:05 -0700580
Hal Canary6f6961e2017-01-31 13:50:44 -0500581#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700582sk_sp<GrGeometryProcessor> GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700583 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
584 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400585 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
586 d->textureProxy(texIdx),
587 nullptr,
588 nullptr,
589 nullptr
590 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500591
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400592 GrSamplerState::WrapMode wrapModes[2];
593 GrTest::TestWrapModes(d->fRandom, wrapModes);
594 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
595 ? GrSamplerState::Filter::kBilerp
596 : GrSamplerState::Filter::kNearest);
jvanverthfa38a302014-10-06 05:59:05 -0700597
jvanverthcf371bb2016-03-10 11:10:43 -0800598 uint32_t flags = 0;
599 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
600 if (flags & kSimilarity_DistanceFieldEffectFlag) {
601 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
602 }
603
Brian Osman4a3f5c82018-09-18 16:16:38 -0400604 return GrDistanceFieldPathGeoProc::Make(*d->caps()->shaderCaps(),
605 GrTest::TestMatrix(d->fRandom),
Brian Osmanc906d252018-12-04 11:17:46 -0500606 d->fRandom->nextBool(),
Robert Phillips4bc70112018-03-01 10:24:02 -0500607 proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400608 samplerState,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400609 flags);
jvanverthfa38a302014-10-06 05:59:05 -0700610}
Hal Canary6f6961e2017-01-31 13:50:44 -0500611#endif
jvanverthfa38a302014-10-06 05:59:05 -0700612
613///////////////////////////////////////////////////////////////////////////////
614
egdaniele659a582015-11-13 09:55:43 -0800615class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000616public:
Brian Salomon5c6ac642017-12-19 11:09:32 -0500617 GrGLDistanceFieldLCDTextGeoProc() : fAtlasSize({0, 0}) {
jvanverth502286d2015-04-08 12:37:51 -0700618 fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
jvanverth21deace2015-04-01 12:43:48 -0700619 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000620
mtklein36352bf2015-03-25 18:17:31 -0700621 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700622 const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
623 args.fGP.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000624
egdaniel4ca2e602015-11-18 08:01:26 -0800625 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800626 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800627 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800628
629 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800630 varyingHandler->emitAttributes(dfTexEffect);
egdaniel4ca2e602015-11-18 08:01:26 -0800631
Robert Phillips8296e752017-08-25 08:45:21 -0400632 const char* atlasSizeInvName;
633 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400634 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400635 "AtlasSizeInv",
636 &atlasSizeInvName);
637
Chris Dalton60283612018-02-14 13:38:14 -0700638 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittabb52a12015-01-13 15:02:10 -0800639
joshualitt9b989322014-12-15 14:16:27 -0800640 // setup pass through color
Brian Salomonbfd51832017-01-04 13:22:08 -0500641 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualitt9b989322014-12-15 14:16:27 -0800642
joshualittabb52a12015-01-13 15:02:10 -0800643 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -0400644 gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
joshualitt4973d9d2014-11-08 09:24:25 -0800645
joshualittabb52a12015-01-13 15:02:10 -0800646 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800647 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800648 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800649 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400650 dfTexEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500651 dfTexEffect.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -0700652 args.fFPCoordTransformHandler);
joshualittabb52a12015-01-13 15:02:10 -0800653
jvanverthbb4a1cf2015-04-07 09:06:00 -0700654 // set up varyings
Chris Dalton27372882017-12-08 13:34:21 -0700655 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verth1694a862018-12-17 19:48:42 +0000656 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
657 GrGLSLVarying texIdx(texIdxType);
Chris Dalton27372882017-12-08 13:34:21 -0700658 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -0400659 append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -0400660 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -0400661
Chris Dalton27372882017-12-08 13:34:21 -0700662 GrGLSLVarying delta(kFloat_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600663 varyingHandler->addVarying("Delta", &delta);
Robert Phillips8296e752017-08-25 08:45:21 -0400664 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) {
665 vertBuilder->codeAppendf("%s = -%s.x/3.0;", delta.vsOut(), atlasSizeInvName);
666 } else {
667 vertBuilder->codeAppendf("%s = %s.x/3.0;", delta.vsOut(), atlasSizeInvName);
668 }
669
670 // add frag shader code
jvanverthcf371bb2016-03-10 11:10:43 -0800671 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
672 kUniformScale_DistanceFieldEffectMask;
673 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -0700674 bool isGammaCorrect =
675 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
joshualitt30ba4362014-08-21 20:18:45 -0700676
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000677 // create LCD offset adjusted by inverse of transform
jvanverthfdf7ccc2015-01-27 08:19:33 -0800678 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400679 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
joshualitt922c8b12015-08-07 09:55:23 -0700680
jvanverth78f07182014-07-30 06:17:59 -0700681 if (isUniformScale) {
jvanverthe499adf2016-07-20 12:22:14 -0700682#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500683 fragBuilder->codeAppendf("half st_grad_len = half(abs(dFdx(%s.x)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700684#else
685 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500686 fragBuilder->codeAppendf("half st_grad_len = half(abs(dFdy(%s.y)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700687#endif
Ethan Nicholase1f55022019-02-05 17:17:40 -0500688 fragBuilder->codeAppendf("half2 offset = half2(half(st_grad_len*%s), 0.0);",
689 delta.fsIn());
jvanverthcf371bb2016-03-10 11:10:43 -0800690 } else if (isSimilarity) {
691 // For a similarity matrix with rotation, the gradient will not be aligned
692 // with the texel coordinate axes, so we need to calculate it.
jvanverthe499adf2016-07-20 12:22:14 -0700693#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500694 fragBuilder->codeAppendf("half2 st_grad = half2(dFdx(%s));", st.fsIn());
695 fragBuilder->codeAppendf("half2 offset = half(%s)*st_grad;", delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700696#else
jvanverthcf371bb2016-03-10 11:10:43 -0800697 // We use dFdy because of a Mali 400 bug, and rotate -90 degrees to
698 // get the gradient in the x direction.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500699 fragBuilder->codeAppendf("half2 st_grad = half2(dFdy(%s));", st.fsIn());
700 fragBuilder->codeAppendf("half2 offset = half2(%s*float2(st_grad.y, -st_grad.x));",
Robert Phillips8296e752017-08-25 08:45:21 -0400701 delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700702#endif
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400703 fragBuilder->codeAppend("half st_grad_len = length(st_grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000704 } else {
Ethan Nicholase1f55022019-02-05 17:17:40 -0500705 fragBuilder->codeAppendf("half2 st = half2(%s);\n", st.fsIn());
jvanverthbb4a1cf2015-04-07 09:06:00 -0700706
Ethan Nicholase1f55022019-02-05 17:17:40 -0500707 fragBuilder->codeAppend("half2 Jdx = half2(dFdx(st));");
708 fragBuilder->codeAppend("half2 Jdy = half2(dFdy(st));");
709 fragBuilder->codeAppendf("half2 offset = half2(half(%s))*Jdx;", delta.fsIn());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000710 }
711
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400712 // sample the texture by index
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400713 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400714 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
715 texIdx, "uv", "texColor");
Jim Van Verth6c251d22017-09-08 12:29:07 -0400716
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400717 // green is distance to uv center
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400718 fragBuilder->codeAppend("half3 distance;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400719 fragBuilder->codeAppend("distance.y = texColor.r;");
720 // red is distance to left offset
Ethan Nicholase1f55022019-02-05 17:17:40 -0500721 fragBuilder->codeAppend("half2 uv_adjusted = half2(uv) - offset;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400722 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
723 texIdx, "uv_adjusted", "texColor");
724 fragBuilder->codeAppend("distance.x = texColor.r;");
725 // blue is distance to right offset
Ethan Nicholase1f55022019-02-05 17:17:40 -0500726 fragBuilder->codeAppend("uv_adjusted = half2(uv) + offset;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400727 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
728 texIdx, "uv_adjusted", "texColor");
729 fragBuilder->codeAppend("distance.z = texColor.r;");
730
731 fragBuilder->codeAppend("distance = "
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400732 "half3(" SK_DistanceFieldMultiplier ")*(distance - half3(" SK_DistanceFieldThreshold"));");
jvanverth2d2a68c2014-06-10 06:42:56 -0700733
jvanverth21deace2015-04-01 12:43:48 -0700734 // adjust width based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -0700735 const char* distanceAdjustUniName = nullptr;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400736 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -0800737 "DistanceAdjust", &distanceAdjustUniName);
egdaniel4ca2e602015-11-18 08:01:26 -0800738 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700739
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000740 // To be strictly correct, we should compute the anti-aliasing factor separately
741 // for each color component. However, this is only important when using perspective
742 // transformations, and even then using a single factor seems like a reasonable
743 // trade-off between quality and speed.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400744 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -0800745 if (isSimilarity) {
halcanary9d524f22016-03-29 09:03:52 -0700746 // For similarity transform (uniform scale-only is a subset of this), we adjust for the
747 // effect of the transformation on the distance by using the length of the gradient of
jvanverthcf371bb2016-03-10 11:10:43 -0800748 // the texture coordinates. We use st coordinates to ensure we're mapping 1:1 from texel
749 // space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700750
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000751 // this gives us a smooth step across approximately one fragment
jvanverthcf371bb2016-03-10 11:10:43 -0800752 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*st_grad_len;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000753 } else {
jvanverth354eba52015-03-16 11:32:49 -0700754 // For general transforms, to determine the amount of correction we multiply a unit
755 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
756 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500757 fragBuilder->codeAppend("half2 dist_grad = half2(half(dFdx(distance.r)), "
758 "half(dFdy(distance.r)));");
jvanverthd68a5502015-03-16 12:58:43 -0700759 // the length of the gradient may be 0, so we need to check for this
760 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400761 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800762 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400763 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800764 fragBuilder->codeAppend("} else {");
Ethan Nicholase1f55022019-02-05 17:17:40 -0500765 fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
egdaniel4ca2e602015-11-18 08:01:26 -0800766 fragBuilder->codeAppend("}");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400767 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800768 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000769
770 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800771 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000772 }
773
brianosman0586f5c2016-04-12 12:48:21 -0700774 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
775 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
776 // mapped linearly to coverage, so use a linear step:
brianosmanb461d342016-04-13 13:10:14 -0700777 if (isGammaCorrect) {
Greg Daniel55923822017-05-22 16:34:34 -0400778 fragBuilder->codeAppendf("%s = "
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400779 "half4(saturate((distance + half3(afwidth)) / half3(2.0 * afwidth)), 1.0);",
Greg Daniel55923822017-05-22 16:34:34 -0400780 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700781 } else {
Greg Daniel55923822017-05-22 16:34:34 -0400782 fragBuilder->codeAppendf(
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400783 "%s = half4(smoothstep(half3(-afwidth), half3(afwidth), distance), 1.0);",
Greg Daniel55923822017-05-22 16:34:34 -0400784 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700785 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000786 }
787
bsalomona624bf32016-09-20 09:12:47 -0700788 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& processor,
789 FPCoordTransformIter&& transformIter) override {
jvanverth21deace2015-04-01 12:43:48 -0700790 SkASSERT(fDistanceAdjustUni.isValid());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000791
joshualitt5559ca22015-05-21 15:50:36 -0700792 const GrDistanceFieldLCDTextGeoProc& dflcd = processor.cast<GrDistanceFieldLCDTextGeoProc>();
793 GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700794 if (wa != fDistanceAdjust) {
795 pdman.set3f(fDistanceAdjustUni,
796 wa.fR,
797 wa.fG,
798 wa.fB);
799 fDistanceAdjust = wa;
jvanverth2d2a68c2014-06-10 06:42:56 -0700800 }
joshualitt9b989322014-12-15 14:16:27 -0800801
Brian Salomon7eae3e02018-08-07 14:02:38 +0000802 const SkISize& atlasSize = dflcd.atlasSize();
803 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
804 if (fAtlasSize != atlasSize) {
805 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
806 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400807 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500808 this->setTransformDataHelper(dflcd.localMatrix(), pdman, &transformIter);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000809 }
810
robertphillips46d36f02015-01-18 08:14:14 -0800811 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500812 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700813 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700814 const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000815
joshualitt8fc6c2d2014-12-22 15:27:05 -0800816 uint32_t key = dfTexEffect.getFlags();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800817 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400818 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000819 }
820
821private:
jvanverth502286d2015-04-08 12:37:51 -0700822 GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400823 UniformHandle fDistanceAdjustUni;
824
825 SkISize fAtlasSize;
826 UniformHandle fAtlasSizeInvUniform;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000827
egdaniele659a582015-11-13 09:55:43 -0800828 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000829};
830
831///////////////////////////////////////////////////////////////////////////////
Mike Klein5045e502018-06-19 01:40:57 +0000832
Brian Osman4a3f5c82018-09-18 16:16:38 -0400833GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(const GrShaderCaps& caps,
834 const sk_sp<GrTextureProxy>* proxies,
Brian Salomon92be2f72018-06-19 14:33:47 -0400835 int numProxies,
836 const GrSamplerState& params,
837 DistanceAdjust distanceAdjust,
838 uint32_t flags,
839 const SkMatrix& localMatrix)
840 : INHERITED(kGrDistanceFieldLCDTextGeoProc_ClassID)
841 , fLocalMatrix(localMatrix)
842 , fDistanceAdjust(distanceAdjust)
843 , fFlags(flags & kLCD_DistanceFieldEffectMask) {
844 SkASSERT(numProxies <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500845 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag));
Brian Salomon92be2f72018-06-19 14:33:47 -0400846
Brian Salomon5c6ac642017-12-19 11:09:32 -0500847 if (fFlags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400848 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500849 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400850 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500851 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500852 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000853 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
854 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500855 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400856
Brian Salomon7eae3e02018-08-07 14:02:38 +0000857 if (numProxies) {
858 fAtlasSize = proxies[0]->isize();
859 }
860
Robert Phillips4bc70112018-03-01 10:24:02 -0500861 for (int i = 0; i < numProxies; ++i) {
862 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000863 SkASSERT(proxies[i]->isize() == fAtlasSize);
Greg Daniel2c3398d2019-06-19 11:58:01 -0400864 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params,
865 proxies[i]->textureSwizzle());
Jim Van Vertha950b632017-09-12 11:54:11 -0400866 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400867 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500868}
869
Robert Phillips4bc70112018-03-01 10:24:02 -0500870void GrDistanceFieldLCDTextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
871 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400872 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500873 SkASSERT(numProxies <= kMaxTextures);
874
Brian Salomon7eae3e02018-08-07 14:02:38 +0000875 if (!fTextureSamplers[0].isInitialized()) {
876 fAtlasSize = proxies[0]->isize();
877 }
878
Robert Phillips4bc70112018-03-01 10:24:02 -0500879 for (int i = 0; i < numProxies; ++i) {
880 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000881 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500882
883 if (!fTextureSamplers[i].isInitialized()) {
Greg Daniel2c3398d2019-06-19 11:58:01 -0400884 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params,
885 proxies[i]->textureSwizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400886 }
887 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400888 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400889}
890
Brian Salomon94efbf52016-11-29 13:43:05 -0500891void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800892 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700893 GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800894}
895
Brian Salomon94efbf52016-11-29 13:43:05 -0500896GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700897 return new GrGLDistanceFieldLCDTextGeoProc();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000898}
899
900///////////////////////////////////////////////////////////////////////////////
901
jvanverth502286d2015-04-08 12:37:51 -0700902GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000903
Hal Canary6f6961e2017-01-31 13:50:44 -0500904#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700905sk_sp<GrGeometryProcessor> GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700906 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
907 GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400908 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
909 d->textureProxy(texIdx),
910 nullptr,
911 nullptr,
912 nullptr
913 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500914
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400915 GrSamplerState::WrapMode wrapModes[2];
916 GrTest::TestWrapModes(d->fRandom, wrapModes);
917 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
918 ? GrSamplerState::Filter::kBilerp
919 : GrSamplerState::Filter::kNearest);
jvanverth21deace2015-04-01 12:43:48 -0700920 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
jvanverth78f07182014-07-30 06:17:59 -0700921 uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
jvanverthcf371bb2016-03-10 11:10:43 -0800922 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
923 if (flags & kSimilarity_DistanceFieldEffectFlag) {
924 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
925 }
joshualitt0067ff52015-07-08 14:26:19 -0700926 flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500927 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Brian Osman4a3f5c82018-09-18 16:16:38 -0400928 return GrDistanceFieldLCDTextGeoProc::Make(*d->caps()->shaderCaps(), proxies, 1, samplerState,
929 wa, flags, localMatrix);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000930}
Hal Canary6f6961e2017-01-31 13:50:44 -0500931#endif