blob: 08c36dc0150f229f112ff696195a5c7368dcf72e [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);
Brian Salomon67529b22019-08-13 15:31:04 -0400244 fTextureSamplers[i].reset(proxies[i]->textureType(), params, proxies[i]->textureSwizzle());
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 Salomon67529b22019-08-13 15:31:04 -0400262 fTextureSamplers[i].reset(proxies[i]->textureType(), params,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400263 proxies[i]->textureSwizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400264 }
265 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400266 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400267}
268
Brian Salomon94efbf52016-11-29 13:43:05 -0500269void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800270 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700271 GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800272}
273
Brian Salomon94efbf52016-11-29 13:43:05 -0500274GrGLSLPrimitiveProcessor*
275GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700276 return new GrGLDistanceFieldA8TextGeoProc();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000277}
278
279///////////////////////////////////////////////////////////////////////////////
280
jvanverth502286d2015-04-08 12:37:51 -0700281GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000282
Hal Canary6f6961e2017-01-31 13:50:44 -0500283#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700284sk_sp<GrGeometryProcessor> GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) {
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500285 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
286 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400287 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
288 d->textureProxy(texIdx),
289 nullptr,
290 nullptr,
291 nullptr
292 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500293
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400294 GrSamplerState::WrapMode wrapModes[2];
295 GrTest::TestWrapModes(d->fRandom, wrapModes);
296 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
297 ? GrSamplerState::Filter::kBilerp
298 : GrSamplerState::Filter::kNearest);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000299
jvanverthcf371bb2016-03-10 11:10:43 -0800300 uint32_t flags = 0;
301 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
302 if (flags & kSimilarity_DistanceFieldEffectFlag) {
303 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
304 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500305 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Mike Reede1367b42017-12-15 14:28:56 -0500306#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500307 float lum = d->fRandom->nextF();
Mike Reede1367b42017-12-15 14:28:56 -0500308#endif
Brian Osman4a3f5c82018-09-18 16:16:38 -0400309 return GrDistanceFieldA8TextGeoProc::Make(*d->caps()->shaderCaps(),
310 proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400311 samplerState,
jvanverth2d2a68c2014-06-10 06:42:56 -0700312#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500313 lum,
jvanverth2d2a68c2014-06-10 06:42:56 -0700314#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500315 flags, localMatrix);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000316}
Hal Canary6f6961e2017-01-31 13:50:44 -0500317#endif
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000318
319///////////////////////////////////////////////////////////////////////////////
320
egdaniele659a582015-11-13 09:55:43 -0800321class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700322public:
joshualitt465283c2015-09-11 08:19:35 -0700323 GrGLDistanceFieldPathGeoProc()
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400324 : fMatrix(SkMatrix::InvalidMatrix())
Robert Phillips8296e752017-08-25 08:45:21 -0400325 , fAtlasSize({0,0}) {
326 }
jvanverthfa38a302014-10-06 05:59:05 -0700327
mtklein36352bf2015-03-25 18:17:31 -0700328 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
Brian Salomon5c6ac642017-12-19 11:09:32 -0500329 const GrDistanceFieldPathGeoProc& dfPathEffect =
330 args.fGP.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700331
Chris Dalton60283612018-02-14 13:38:14 -0700332 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
jvanverthfa38a302014-10-06 05:59:05 -0700333
egdaniel4ca2e602015-11-18 08:01:26 -0800334 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800335 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800336 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800337
338 // emit attributes
Brian Salomon5c6ac642017-12-19 11:09:32 -0500339 varyingHandler->emitAttributes(dfPathEffect);
joshualittabb52a12015-01-13 15:02:10 -0800340
Robert Phillips8296e752017-08-25 08:45:21 -0400341 const char* atlasSizeInvName;
342 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400343 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400344 "AtlasSizeInv",
345 &atlasSizeInvName);
346
Chris Dalton27372882017-12-08 13:34:21 -0700347 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verth1694a862018-12-17 19:48:42 +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
Ethan Nicholase1f55022019-02-05 17:17:40 -0500407 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
408 "*half(dFdx(%s.x)));", 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.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500411 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
412 "*half(dFdy(%s.y)));", 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 Nicholase1f55022019-02-05 17:17:40 -0500421 fragBuilder->codeAppendf("half st_grad_len = half(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 Nicholase1f55022019-02-05 17:17:40 -0500424 fragBuilder->codeAppendf("half st_grad_len = half(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 Nicholase1f55022019-02-05 17:17:40 -0500431 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), "
432 "dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700433 // the length of the gradient may be 0, so we need to check for this
434 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400435 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800436 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400437 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800438 fragBuilder->codeAppend("} else {");
Ethan Nicholase1f55022019-02-05 17:17:40 -0500439 fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
egdaniel4ca2e602015-11-18 08:01:26 -0800440 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700441
Ethan Nicholase1f55022019-02-05 17:17:40 -0500442 fragBuilder->codeAppendf("half2 Jdx = half2(dFdx(%s));", st.fsIn());
443 fragBuilder->codeAppendf("half2 Jdy = half2(dFdy(%s));", st.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400444 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
445 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
jvanverthfa38a302014-10-06 05:59:05 -0700446
447 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800448 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
jvanverthfa38a302014-10-06 05:59:05 -0700449 }
brianosman0e3c5542016-04-13 13:56:21 -0700450 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
451 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
452 // mapped linearly to coverage, so use a linear step:
453 if (isGammaCorrect) {
454 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400455 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0e3c5542016-04-13 13:56:21 -0700456 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400457 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0e3c5542016-04-13 13:56:21 -0700458 }
jvanverthfa38a302014-10-06 05:59:05 -0700459
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400460 fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
jvanverthfa38a302014-10-06 05:59:05 -0700461 }
462
bsalomona624bf32016-09-20 09:12:47 -0700463 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
464 FPCoordTransformIter&& transformIter) override {
joshualitt9b989322014-12-15 14:16:27 -0800465
joshualitte578a952015-05-14 10:09:13 -0700466 const GrDistanceFieldPathGeoProc& dfpgp = proc.cast<GrDistanceFieldPathGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700467
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400468 if (dfpgp.matrix().hasPerspective() && !fMatrix.cheapEqualTo(dfpgp.matrix())) {
469 fMatrix = dfpgp.matrix();
470 float matrix[3 * 3];
471 GrGLSLGetMatrix<3>(matrix, fMatrix);
472 pdman.setMatrix3f(fMatrixUniform, matrix);
joshualitt5559ca22015-05-21 15:50:36 -0700473 }
Robert Phillips8296e752017-08-25 08:45:21 -0400474
Brian Salomon7eae3e02018-08-07 14:02:38 +0000475 const SkISize& atlasSize = dfpgp.atlasSize();
476 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
477 if (fAtlasSize != atlasSize) {
478 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
479 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400480 }
481
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400482 if (dfpgp.matrix().hasPerspective()) {
483 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
484 } else {
485 this->setTransformDataHelper(dfpgp.matrix(), pdman, &transformIter);
486 }
jvanverthfa38a302014-10-06 05:59:05 -0700487 }
488
robertphillips46d36f02015-01-18 08:14:14 -0800489 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500490 const GrShaderCaps&,
jvanverthfa38a302014-10-06 05:59:05 -0700491 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700492 const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700493
joshualitt8fc6c2d2014-12-22 15:27:05 -0800494 uint32_t key = dfTexEffect.getFlags();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400495 key |= ComputePosKey(dfTexEffect.matrix()) << 16;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800496 b->add32(key);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400497 b->add32(dfTexEffect.matrix().hasPerspective());
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400498 b->add32(dfTexEffect.numTextureSamplers());
jvanverthfa38a302014-10-06 05:59:05 -0700499 }
500
501private:
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400502 SkMatrix fMatrix; // view matrix if perspective, local matrix otherwise
503 UniformHandle fMatrixUniform;
Robert Phillips8296e752017-08-25 08:45:21 -0400504
505 SkISize fAtlasSize;
506 UniformHandle fAtlasSizeInvUniform;
jvanverthfa38a302014-10-06 05:59:05 -0700507
egdaniele659a582015-11-13 09:55:43 -0800508 typedef GrGLSLGeometryProcessor INHERITED;
jvanverthfa38a302014-10-06 05:59:05 -0700509};
510
511///////////////////////////////////////////////////////////////////////////////
Brian Salomon92be2f72018-06-19 14:33:47 -0400512
Brian Osman4a3f5c82018-09-18 16:16:38 -0400513GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(const GrShaderCaps& caps,
514 const SkMatrix& matrix,
Brian Osmanc906d252018-12-04 11:17:46 -0500515 bool wideColor,
Brian Salomon92be2f72018-06-19 14:33:47 -0400516 const sk_sp<GrTextureProxy>* proxies,
517 int numProxies,
518 const GrSamplerState& params,
519 uint32_t flags)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400520 : INHERITED(kGrDistanceFieldPathGeoProc_ClassID)
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400521 , fMatrix(matrix)
Brian Salomon92be2f72018-06-19 14:33:47 -0400522 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500523 SkASSERT(numProxies <= kMaxTextures);
Mike Klein5045e502018-06-19 01:40:57 +0000524 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400525
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500526 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Osmanc906d252018-12-04 11:17:46 -0500527 fInColor = MakeColorAttribute("inColor", wideColor);
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000528 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
529 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500530 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000531
532 if (numProxies) {
533 fAtlasSize = proxies[0]->isize();
534 }
535
Robert Phillips4bc70112018-03-01 10:24:02 -0500536 for (int i = 0; i < numProxies; ++i) {
537 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000538 SkASSERT(proxies[i]->isize() == fAtlasSize);
Brian Salomon67529b22019-08-13 15:31:04 -0400539 fTextureSamplers[i].reset(proxies[i]->textureType(), params, proxies[i]->textureSwizzle());
Jim Van Vertha950b632017-09-12 11:54:11 -0400540 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400541 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500542}
543
Robert Phillips4bc70112018-03-01 10:24:02 -0500544void GrDistanceFieldPathGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
545 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400546 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500547 SkASSERT(numProxies <= kMaxTextures);
548
Brian Salomon7eae3e02018-08-07 14:02:38 +0000549 if (!fTextureSamplers[0].isInitialized()) {
550 fAtlasSize = proxies[0]->isize();
551 }
552
Robert Phillips4bc70112018-03-01 10:24:02 -0500553 for (int i = 0; i < numProxies; ++i) {
554 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000555 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500556
557 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon67529b22019-08-13 15:31:04 -0400558 fTextureSamplers[i].reset(proxies[i]->textureType(), params,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400559 proxies[i]->textureSwizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400560 }
561 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400562 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400563}
564
Brian Salomon94efbf52016-11-29 13:43:05 -0500565void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800566 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700567 GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800568}
569
Brian Salomon94efbf52016-11-29 13:43:05 -0500570GrGLSLPrimitiveProcessor*
571GrDistanceFieldPathGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700572 return new GrGLDistanceFieldPathGeoProc();
jvanverthfa38a302014-10-06 05:59:05 -0700573}
574
575///////////////////////////////////////////////////////////////////////////////
576
jvanverth502286d2015-04-08 12:37:51 -0700577GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc);
jvanverthfa38a302014-10-06 05:59:05 -0700578
Hal Canary6f6961e2017-01-31 13:50:44 -0500579#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700580sk_sp<GrGeometryProcessor> GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700581 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
582 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400583 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
584 d->textureProxy(texIdx),
585 nullptr,
586 nullptr,
587 nullptr
588 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500589
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400590 GrSamplerState::WrapMode wrapModes[2];
591 GrTest::TestWrapModes(d->fRandom, wrapModes);
592 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
593 ? GrSamplerState::Filter::kBilerp
594 : GrSamplerState::Filter::kNearest);
jvanverthfa38a302014-10-06 05:59:05 -0700595
jvanverthcf371bb2016-03-10 11:10:43 -0800596 uint32_t flags = 0;
597 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
598 if (flags & kSimilarity_DistanceFieldEffectFlag) {
599 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
600 }
601
Brian Osman4a3f5c82018-09-18 16:16:38 -0400602 return GrDistanceFieldPathGeoProc::Make(*d->caps()->shaderCaps(),
603 GrTest::TestMatrix(d->fRandom),
Brian Osmanc906d252018-12-04 11:17:46 -0500604 d->fRandom->nextBool(),
Robert Phillips4bc70112018-03-01 10:24:02 -0500605 proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400606 samplerState,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400607 flags);
jvanverthfa38a302014-10-06 05:59:05 -0700608}
Hal Canary6f6961e2017-01-31 13:50:44 -0500609#endif
jvanverthfa38a302014-10-06 05:59:05 -0700610
611///////////////////////////////////////////////////////////////////////////////
612
egdaniele659a582015-11-13 09:55:43 -0800613class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000614public:
Brian Salomon5c6ac642017-12-19 11:09:32 -0500615 GrGLDistanceFieldLCDTextGeoProc() : fAtlasSize({0, 0}) {
jvanverth502286d2015-04-08 12:37:51 -0700616 fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
jvanverth21deace2015-04-01 12:43:48 -0700617 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000618
mtklein36352bf2015-03-25 18:17:31 -0700619 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700620 const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
621 args.fGP.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000622
egdaniel4ca2e602015-11-18 08:01:26 -0800623 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800624 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800625 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800626
627 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800628 varyingHandler->emitAttributes(dfTexEffect);
egdaniel4ca2e602015-11-18 08:01:26 -0800629
Robert Phillips8296e752017-08-25 08:45:21 -0400630 const char* atlasSizeInvName;
631 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400632 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400633 "AtlasSizeInv",
634 &atlasSizeInvName);
635
Chris Dalton60283612018-02-14 13:38:14 -0700636 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittabb52a12015-01-13 15:02:10 -0800637
joshualitt9b989322014-12-15 14:16:27 -0800638 // setup pass through color
Brian Salomonbfd51832017-01-04 13:22:08 -0500639 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualitt9b989322014-12-15 14:16:27 -0800640
joshualittabb52a12015-01-13 15:02:10 -0800641 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -0400642 gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
joshualitt4973d9d2014-11-08 09:24:25 -0800643
joshualittabb52a12015-01-13 15:02:10 -0800644 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800645 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800646 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800647 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400648 dfTexEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500649 dfTexEffect.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -0700650 args.fFPCoordTransformHandler);
joshualittabb52a12015-01-13 15:02:10 -0800651
jvanverthbb4a1cf2015-04-07 09:06:00 -0700652 // set up varyings
Chris Dalton27372882017-12-08 13:34:21 -0700653 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verth1694a862018-12-17 19:48:42 +0000654 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
655 GrGLSLVarying texIdx(texIdxType);
Chris Dalton27372882017-12-08 13:34:21 -0700656 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -0400657 append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -0400658 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -0400659
Chris Dalton27372882017-12-08 13:34:21 -0700660 GrGLSLVarying delta(kFloat_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600661 varyingHandler->addVarying("Delta", &delta);
Robert Phillips8296e752017-08-25 08:45:21 -0400662 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) {
663 vertBuilder->codeAppendf("%s = -%s.x/3.0;", delta.vsOut(), atlasSizeInvName);
664 } else {
665 vertBuilder->codeAppendf("%s = %s.x/3.0;", delta.vsOut(), atlasSizeInvName);
666 }
667
668 // add frag shader code
jvanverthcf371bb2016-03-10 11:10:43 -0800669 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
670 kUniformScale_DistanceFieldEffectMask;
671 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -0700672 bool isGammaCorrect =
673 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
joshualitt30ba4362014-08-21 20:18:45 -0700674
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000675 // create LCD offset adjusted by inverse of transform
jvanverthfdf7ccc2015-01-27 08:19:33 -0800676 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400677 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
joshualitt922c8b12015-08-07 09:55:23 -0700678
jvanverth78f07182014-07-30 06:17:59 -0700679 if (isUniformScale) {
jvanverthe499adf2016-07-20 12:22:14 -0700680#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500681 fragBuilder->codeAppendf("half st_grad_len = half(abs(dFdx(%s.x)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700682#else
683 // 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 -0500684 fragBuilder->codeAppendf("half st_grad_len = half(abs(dFdy(%s.y)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700685#endif
Ethan Nicholase1f55022019-02-05 17:17:40 -0500686 fragBuilder->codeAppendf("half2 offset = half2(half(st_grad_len*%s), 0.0);",
687 delta.fsIn());
jvanverthcf371bb2016-03-10 11:10:43 -0800688 } else if (isSimilarity) {
689 // For a similarity matrix with rotation, the gradient will not be aligned
690 // with the texel coordinate axes, so we need to calculate it.
jvanverthe499adf2016-07-20 12:22:14 -0700691#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500692 fragBuilder->codeAppendf("half2 st_grad = half2(dFdx(%s));", st.fsIn());
693 fragBuilder->codeAppendf("half2 offset = half(%s)*st_grad;", delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700694#else
jvanverthcf371bb2016-03-10 11:10:43 -0800695 // We use dFdy because of a Mali 400 bug, and rotate -90 degrees to
696 // get the gradient in the x direction.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500697 fragBuilder->codeAppendf("half2 st_grad = half2(dFdy(%s));", st.fsIn());
698 fragBuilder->codeAppendf("half2 offset = half2(%s*float2(st_grad.y, -st_grad.x));",
Robert Phillips8296e752017-08-25 08:45:21 -0400699 delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700700#endif
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400701 fragBuilder->codeAppend("half st_grad_len = length(st_grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000702 } else {
Ethan Nicholase1f55022019-02-05 17:17:40 -0500703 fragBuilder->codeAppendf("half2 st = half2(%s);\n", st.fsIn());
jvanverthbb4a1cf2015-04-07 09:06:00 -0700704
Ethan Nicholase1f55022019-02-05 17:17:40 -0500705 fragBuilder->codeAppend("half2 Jdx = half2(dFdx(st));");
706 fragBuilder->codeAppend("half2 Jdy = half2(dFdy(st));");
707 fragBuilder->codeAppendf("half2 offset = half2(half(%s))*Jdx;", delta.fsIn());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000708 }
709
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400710 // sample the texture by index
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400711 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400712 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
713 texIdx, "uv", "texColor");
Jim Van Verth6c251d22017-09-08 12:29:07 -0400714
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400715 // green is distance to uv center
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400716 fragBuilder->codeAppend("half3 distance;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400717 fragBuilder->codeAppend("distance.y = texColor.r;");
718 // red is distance to left offset
Ethan Nicholase1f55022019-02-05 17:17:40 -0500719 fragBuilder->codeAppend("half2 uv_adjusted = half2(uv) - offset;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400720 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
721 texIdx, "uv_adjusted", "texColor");
722 fragBuilder->codeAppend("distance.x = texColor.r;");
723 // blue is distance to right offset
Ethan Nicholase1f55022019-02-05 17:17:40 -0500724 fragBuilder->codeAppend("uv_adjusted = half2(uv) + offset;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400725 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
726 texIdx, "uv_adjusted", "texColor");
727 fragBuilder->codeAppend("distance.z = texColor.r;");
728
729 fragBuilder->codeAppend("distance = "
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400730 "half3(" SK_DistanceFieldMultiplier ")*(distance - half3(" SK_DistanceFieldThreshold"));");
jvanverth2d2a68c2014-06-10 06:42:56 -0700731
jvanverth21deace2015-04-01 12:43:48 -0700732 // adjust width based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -0700733 const char* distanceAdjustUniName = nullptr;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400734 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -0800735 "DistanceAdjust", &distanceAdjustUniName);
egdaniel4ca2e602015-11-18 08:01:26 -0800736 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700737
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000738 // To be strictly correct, we should compute the anti-aliasing factor separately
739 // for each color component. However, this is only important when using perspective
740 // transformations, and even then using a single factor seems like a reasonable
741 // trade-off between quality and speed.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400742 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -0800743 if (isSimilarity) {
halcanary9d524f22016-03-29 09:03:52 -0700744 // For similarity transform (uniform scale-only is a subset of this), we adjust for the
745 // effect of the transformation on the distance by using the length of the gradient of
jvanverthcf371bb2016-03-10 11:10:43 -0800746 // the texture coordinates. We use st coordinates to ensure we're mapping 1:1 from texel
747 // space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700748
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000749 // this gives us a smooth step across approximately one fragment
jvanverthcf371bb2016-03-10 11:10:43 -0800750 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*st_grad_len;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000751 } else {
jvanverth354eba52015-03-16 11:32:49 -0700752 // For general transforms, to determine the amount of correction we multiply a unit
753 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
754 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500755 fragBuilder->codeAppend("half2 dist_grad = half2(half(dFdx(distance.r)), "
756 "half(dFdy(distance.r)));");
jvanverthd68a5502015-03-16 12:58:43 -0700757 // the length of the gradient may be 0, so we need to check for this
758 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400759 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800760 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400761 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800762 fragBuilder->codeAppend("} else {");
Ethan Nicholase1f55022019-02-05 17:17:40 -0500763 fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
egdaniel4ca2e602015-11-18 08:01:26 -0800764 fragBuilder->codeAppend("}");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400765 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800766 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000767
768 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800769 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000770 }
771
brianosman0586f5c2016-04-12 12:48:21 -0700772 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
773 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
774 // mapped linearly to coverage, so use a linear step:
brianosmanb461d342016-04-13 13:10:14 -0700775 if (isGammaCorrect) {
Greg Daniel55923822017-05-22 16:34:34 -0400776 fragBuilder->codeAppendf("%s = "
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400777 "half4(saturate((distance + half3(afwidth)) / half3(2.0 * afwidth)), 1.0);",
Greg Daniel55923822017-05-22 16:34:34 -0400778 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700779 } else {
Greg Daniel55923822017-05-22 16:34:34 -0400780 fragBuilder->codeAppendf(
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400781 "%s = half4(smoothstep(half3(-afwidth), half3(afwidth), distance), 1.0);",
Greg Daniel55923822017-05-22 16:34:34 -0400782 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700783 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000784 }
785
bsalomona624bf32016-09-20 09:12:47 -0700786 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& processor,
787 FPCoordTransformIter&& transformIter) override {
jvanverth21deace2015-04-01 12:43:48 -0700788 SkASSERT(fDistanceAdjustUni.isValid());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000789
joshualitt5559ca22015-05-21 15:50:36 -0700790 const GrDistanceFieldLCDTextGeoProc& dflcd = processor.cast<GrDistanceFieldLCDTextGeoProc>();
791 GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700792 if (wa != fDistanceAdjust) {
793 pdman.set3f(fDistanceAdjustUni,
794 wa.fR,
795 wa.fG,
796 wa.fB);
797 fDistanceAdjust = wa;
jvanverth2d2a68c2014-06-10 06:42:56 -0700798 }
joshualitt9b989322014-12-15 14:16:27 -0800799
Brian Salomon7eae3e02018-08-07 14:02:38 +0000800 const SkISize& atlasSize = dflcd.atlasSize();
801 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
802 if (fAtlasSize != atlasSize) {
803 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
804 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400805 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500806 this->setTransformDataHelper(dflcd.localMatrix(), pdman, &transformIter);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000807 }
808
robertphillips46d36f02015-01-18 08:14:14 -0800809 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500810 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700811 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700812 const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000813
joshualitt8fc6c2d2014-12-22 15:27:05 -0800814 uint32_t key = dfTexEffect.getFlags();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800815 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400816 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000817 }
818
819private:
jvanverth502286d2015-04-08 12:37:51 -0700820 GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400821 UniformHandle fDistanceAdjustUni;
822
823 SkISize fAtlasSize;
824 UniformHandle fAtlasSizeInvUniform;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000825
egdaniele659a582015-11-13 09:55:43 -0800826 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000827};
828
829///////////////////////////////////////////////////////////////////////////////
Mike Klein5045e502018-06-19 01:40:57 +0000830
Brian Osman4a3f5c82018-09-18 16:16:38 -0400831GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(const GrShaderCaps& caps,
832 const sk_sp<GrTextureProxy>* proxies,
Brian Salomon92be2f72018-06-19 14:33:47 -0400833 int numProxies,
834 const GrSamplerState& params,
835 DistanceAdjust distanceAdjust,
836 uint32_t flags,
837 const SkMatrix& localMatrix)
838 : INHERITED(kGrDistanceFieldLCDTextGeoProc_ClassID)
839 , fLocalMatrix(localMatrix)
840 , fDistanceAdjust(distanceAdjust)
841 , fFlags(flags & kLCD_DistanceFieldEffectMask) {
842 SkASSERT(numProxies <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500843 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag));
Brian Salomon92be2f72018-06-19 14:33:47 -0400844
Brian Salomon5c6ac642017-12-19 11:09:32 -0500845 if (fFlags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400846 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500847 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400848 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500849 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500850 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000851 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
852 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500853 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400854
Brian Salomon7eae3e02018-08-07 14:02:38 +0000855 if (numProxies) {
856 fAtlasSize = proxies[0]->isize();
857 }
858
Robert Phillips4bc70112018-03-01 10:24:02 -0500859 for (int i = 0; i < numProxies; ++i) {
860 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000861 SkASSERT(proxies[i]->isize() == fAtlasSize);
Brian Salomon67529b22019-08-13 15:31:04 -0400862 fTextureSamplers[i].reset(proxies[i]->textureType(), params, proxies[i]->textureSwizzle());
Jim Van Vertha950b632017-09-12 11:54:11 -0400863 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400864 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500865}
866
Robert Phillips4bc70112018-03-01 10:24:02 -0500867void GrDistanceFieldLCDTextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
868 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400869 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500870 SkASSERT(numProxies <= kMaxTextures);
871
Brian Salomon7eae3e02018-08-07 14:02:38 +0000872 if (!fTextureSamplers[0].isInitialized()) {
873 fAtlasSize = proxies[0]->isize();
874 }
875
Robert Phillips4bc70112018-03-01 10:24:02 -0500876 for (int i = 0; i < numProxies; ++i) {
877 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000878 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500879
880 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon67529b22019-08-13 15:31:04 -0400881 fTextureSamplers[i].reset(proxies[i]->textureType(), params,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400882 proxies[i]->textureSwizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400883 }
884 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400885 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400886}
887
Brian Salomon94efbf52016-11-29 13:43:05 -0500888void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800889 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700890 GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800891}
892
Brian Salomon94efbf52016-11-29 13:43:05 -0500893GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700894 return new GrGLDistanceFieldLCDTextGeoProc();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000895}
896
897///////////////////////////////////////////////////////////////////////////////
898
jvanverth502286d2015-04-08 12:37:51 -0700899GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000900
Hal Canary6f6961e2017-01-31 13:50:44 -0500901#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700902sk_sp<GrGeometryProcessor> GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700903 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
904 GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400905 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
906 d->textureProxy(texIdx),
907 nullptr,
908 nullptr,
909 nullptr
910 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500911
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400912 GrSamplerState::WrapMode wrapModes[2];
913 GrTest::TestWrapModes(d->fRandom, wrapModes);
914 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
915 ? GrSamplerState::Filter::kBilerp
916 : GrSamplerState::Filter::kNearest);
jvanverth21deace2015-04-01 12:43:48 -0700917 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
jvanverth78f07182014-07-30 06:17:59 -0700918 uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
jvanverthcf371bb2016-03-10 11:10:43 -0800919 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
920 if (flags & kSimilarity_DistanceFieldEffectFlag) {
921 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
922 }
joshualitt0067ff52015-07-08 14:26:19 -0700923 flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500924 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Brian Osman4a3f5c82018-09-18 16:16:38 -0400925 return GrDistanceFieldLCDTextGeoProc::Make(*d->caps()->shaderCaps(), proxies, 1, samplerState,
926 wa, flags, localMatrix);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000927}
Hal Canary6f6961e2017-01-31 13:50:44 -0500928#endif