blob: 2ef7f9d31dd7289d62ce2a7fbd6043e6736f9981 [file] [log] [blame]
jvanverth@google.comd830d132013-11-11 20:54:09 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
jvanverth8ed3b9a2015-04-09 08:00:49 -07008#include "GrDistanceFieldGeoProc.h"
Jim Van Verth6a7a7042017-09-11 11:04:10 -04009#include "GrAtlasedShaderHelpers.h"
Brian Osman4a3f5c82018-09-18 16:16:38 -040010#include "GrCaps.h"
11#include "GrShaderCaps.h"
joshualitteb2a6762014-12-04 11:35:33 -080012#include "GrTexture.h"
13#include "SkDistanceFieldGen.h"
egdaniel2d721d32015-11-11 13:06:05 -080014#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniele659a582015-11-13 09:55:43 -080015#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070016#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080017#include "glsl/GrGLSLUniformHandler.h"
egdaniel64c47282015-11-13 06:54:19 -080018#include "glsl/GrGLSLUtil.h"
egdaniel0eafe792015-11-20 14:01:22 -080019#include "glsl/GrGLSLVarying.h"
Chris Daltonc17bf322017-10-24 10:59:03 -060020#include "glsl/GrGLSLVertexGeoBuilder.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000021
jvanverth21deace2015-04-01 12:43:48 -070022// Assuming a radius of a little less than the diagonal of the fragment
jvanverth24ba0082015-03-19 11:34:13 -070023#define SK_DistanceFieldAAFactor "0.65"
jvanverth2d2a68c2014-06-10 06:42:56 -070024
egdaniele659a582015-11-13 09:55:43 -080025class GrGLDistanceFieldA8TextGeoProc : public GrGLSLGeometryProcessor {
jvanverth@google.comd830d132013-11-11 20:54:09 +000026public:
Brian Salomon5c6ac642017-12-19 11:09:32 -050027 GrGLDistanceFieldA8TextGeoProc() = default;
jvanverth@google.comd830d132013-11-11 20:54:09 +000028
mtklein36352bf2015-03-25 18:17:31 -070029 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -070030 const GrDistanceFieldA8TextGeoProc& dfTexEffect =
31 args.fGP.cast<GrDistanceFieldA8TextGeoProc>();
Chris Dalton60283612018-02-14 13:38:14 -070032 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
jvanverth@google.comd830d132013-11-11 20:54:09 +000033
egdaniel4ca2e602015-11-18 08:01:26 -080034 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -080035 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -080036 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -080037
38 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -080039 varyingHandler->emitAttributes(dfTexEffect);
joshualittabb52a12015-01-13 15:02:10 -080040
Robert Phillips8296e752017-08-25 08:45:21 -040041 const char* atlasSizeInvName;
42 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040043 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -040044 kHigh_GrSLPrecision,
45 "AtlasSizeInv",
46 &atlasSizeInvName);
jvanverth21deace2015-04-01 12:43:48 -070047#ifdef SK_GAMMA_APPLY_TO_A8
48 // adjust based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -070049 const char* distanceAdjustUniName = nullptr;
jvanverth21deace2015-04-01 12:43:48 -070050 // width, height, 1/(3*width)
Ethan Nicholasf7b88202017-09-18 14:10:39 -040051 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -080052 "DistanceAdjust", &distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -070053#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000054
joshualitt9b989322014-12-15 14:16:27 -080055 // Setup pass through color
Brian Salomonbfd51832017-01-04 13:22:08 -050056 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000057
joshualittabb52a12015-01-13 15:02:10 -080058 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -040059 gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
joshualitt2dd1ae02014-12-03 06:24:10 -080060
joshualittabb52a12015-01-13 15:02:10 -080061 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -080062 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -080063 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -080064 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -040065 dfTexEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -050066 dfTexEffect.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -070067 args.fFPCoordTransformHandler);
joshualitt4973d9d2014-11-08 09:24:25 -080068
jvanverthbb4a1cf2015-04-07 09:06:00 -070069 // add varyings
Chris Dalton27372882017-12-08 13:34:21 -070070 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verthfc4f7682018-01-25 16:26:25 -050071 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
72 GrGLSLVarying texIdx(texIdxType);
Chris Dalton27372882017-12-08 13:34:21 -070073 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -040074 append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -040075 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -040076
jvanverthcf371bb2016-03-10 11:10:43 -080077 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
78 kUniformScale_DistanceFieldEffectMask;
jvanverthbb4a1cf2015-04-07 09:06:00 -070079 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -070080 bool isGammaCorrect =
81 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
Jim Van Verth90e89b32017-07-06 16:36:55 -040082 bool isAliased =
83 SkToBool(dfTexEffect.getFlags() & kAliased_DistanceFieldEffectFlag);
halcanary9d524f22016-03-29 09:03:52 -070084
jvanverthfdf7ccc2015-01-27 08:19:33 -080085 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -040086 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -040087 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040088 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
89 texIdx, "uv", "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -080090
Ethan Nicholasf7b88202017-09-18 14:10:39 -040091 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -040092 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverth21deace2015-04-01 12:43:48 -070093#ifdef SK_GAMMA_APPLY_TO_A8
94 // adjust width based on gamma
egdaniel4ca2e602015-11-18 08:01:26 -080095 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -070096#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000097
Ethan Nicholasf7b88202017-09-18 14:10:39 -040098 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -080099 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700100 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700101 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800102 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000103
jvanverth354eba52015-03-16 11:32:49 -0700104 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700105#ifdef SK_VULKAN
106 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));",
107 st.fsIn());
108#else
109 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
egdaniel4ca2e602015-11-18 08:01:26 -0800110 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));",
111 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700112#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800113 } else if (isSimilarity) {
114 // For similarity transform, we adjust the effect of the transformation on the distance
115 // by using the length of the gradient of the texture coordinates. We use st coordinates
116 // to ensure we're mapping 1:1 from texel space to pixel space.
117 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
118
119 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700120#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400121 fragBuilder->codeAppendf("half st_grad_len = length(dFdx(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700122#else
123 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400124 fragBuilder->codeAppendf("half st_grad_len = length(dFdy(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700125#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800126 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverth354eba52015-03-16 11:32:49 -0700127 } else {
128 // For general transforms, to determine the amount of correction we multiply a unit
129 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
130 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400131 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700132 // the length of the gradient may be 0, so we need to check for this
133 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400134 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800135 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400136 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800137 fragBuilder->codeAppend("} else {");
138 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
139 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700140
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400141 fragBuilder->codeAppendf("half2 Jdx = dFdx(%s);", st.fsIn());
142 fragBuilder->codeAppendf("half2 Jdy = dFdy(%s);", st.fsIn());
143 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800144 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000145
146 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800147 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000148 }
brianosman0586f5c2016-04-12 12:48:21 -0700149
Jim Van Verth90e89b32017-07-06 16:36:55 -0400150 if (isAliased) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400151 fragBuilder->codeAppend("half val = distance > 0 ? 1.0 : 0.0;");
Jim Van Verth90e89b32017-07-06 16:36:55 -0400152 } else if (isGammaCorrect) {
153 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
154 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want
155 // distance mapped linearly to coverage, so use a linear step:
brianosman0586f5c2016-04-12 12:48:21 -0700156 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400157 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0586f5c2016-04-12 12:48:21 -0700158 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400159 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0586f5c2016-04-12 12:48:21 -0700160 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000161
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400162 fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000163 }
164
bsalomona624bf32016-09-20 09:12:47 -0700165 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
166 FPCoordTransformIter&& transformIter) override {
Robert Phillips8296e752017-08-25 08:45:21 -0400167 const GrDistanceFieldA8TextGeoProc& dfa8gp = proc.cast<GrDistanceFieldA8TextGeoProc>();
168
jvanverth2d2a68c2014-06-10 06:42:56 -0700169#ifdef SK_GAMMA_APPLY_TO_A8
Robert Phillips8296e752017-08-25 08:45:21 -0400170 float distanceAdjust = dfa8gp.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700171 if (distanceAdjust != fDistanceAdjust) {
Robert Phillipse2538312017-08-24 17:47:23 +0000172 fDistanceAdjust = distanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400173 pdman.set1f(fDistanceAdjustUni, distanceAdjust);
jvanverth2d2a68c2014-06-10 06:42:56 -0700174 }
175#endif
joshualitt5559ca22015-05-21 15:50:36 -0700176
Brian Salomon7eae3e02018-08-07 14:02:38 +0000177 const SkISize& atlasSize = dfa8gp.atlasSize();
178 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
Robert Phillips8296e752017-08-25 08:45:21 -0400179
Brian Salomon7eae3e02018-08-07 14:02:38 +0000180 if (fAtlasSize != atlasSize) {
181 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
182 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400183 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500184 this->setTransformDataHelper(dfa8gp.localMatrix(), pdman, &transformIter);
commit-bot@chromium.org8fe2ee12014-03-26 18:03:05 +0000185 }
186
robertphillips46d36f02015-01-18 08:14:14 -0800187 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500188 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700189 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700190 const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800191 uint32_t key = dfTexEffect.getFlags();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800192 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400193 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000194 }
195
jvanverth@google.comd830d132013-11-11 20:54:09 +0000196private:
mtklein50282b42015-01-22 07:59:52 -0800197#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500198 float fDistanceAdjust = -1.f;
jvanverth21deace2015-04-01 12:43:48 -0700199 UniformHandle fDistanceAdjustUni;
mtklein50282b42015-01-22 07:59:52 -0800200#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500201 SkISize fAtlasSize = {0, 0};
Robert Phillips8296e752017-08-25 08:45:21 -0400202 UniformHandle fAtlasSizeInvUniform;
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000203
egdaniele659a582015-11-13 09:55:43 -0800204 typedef GrGLSLGeometryProcessor INHERITED;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000205};
206
207///////////////////////////////////////////////////////////////////////////////
208
Brian Salomon92be2f72018-06-19 14:33:47 -0400209constexpr GrPrimitiveProcessor::Attribute GrDistanceFieldA8TextGeoProc::kInColor;
Brian Salomon92be2f72018-06-19 14:33:47 -0400210
Brian Osman4a3f5c82018-09-18 16:16:38 -0400211GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(const GrShaderCaps& caps,
212 const sk_sp<GrTextureProxy>* proxies,
Brian Salomon92be2f72018-06-19 14:33:47 -0400213 int numProxies,
214 const GrSamplerState& params,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500215#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon92be2f72018-06-19 14:33:47 -0400216 float distanceAdjust,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500217#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400218 uint32_t flags,
219 const SkMatrix& localMatrix)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400220 : INHERITED(kGrDistanceFieldA8TextGeoProc_ClassID)
Brian Salomon92be2f72018-06-19 14:33:47 -0400221 , fLocalMatrix(localMatrix)
222 , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500223#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400224 , fDistanceAdjust(distanceAdjust)
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500225#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400226{
Robert Phillips4bc70112018-03-01 10:24:02 -0500227 SkASSERT(numProxies <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500228 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400229
Brian Salomon5c6ac642017-12-19 11:09:32 -0500230 if (flags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400231 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500232 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400233 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500234 }
Brian Osman4a3f5c82018-09-18 16:16:38 -0400235 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
236 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Salomon92be2f72018-06-19 14:33:47 -0400237 this->setVertexAttributeCnt(3);
238
Brian Salomon7eae3e02018-08-07 14:02:38 +0000239 if (numProxies) {
240 fAtlasSize = proxies[0]->isize();
241 }
Robert Phillips4bc70112018-03-01 10:24:02 -0500242 for (int i = 0; i < numProxies; ++i) {
243 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000244 SkASSERT(proxies[i]->isize() == fAtlasSize);
245 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
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()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000263 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
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 kHigh_GrSLPrecision,
345 "AtlasSizeInv",
346 &atlasSizeInvName);
347
Chris Dalton27372882017-12-08 13:34:21 -0700348 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500349 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
Robert Phillips8296e752017-08-25 08:45:21 -0400408 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));",
409 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.
Robert Phillips8296e752017-08-25 08:45:21 -0400412 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));",
413 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 Nicholasf7b88202017-09-18 14:10:39 -0400422 fragBuilder->codeAppendf("half st_grad_len = 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 Nicholasf7b88202017-09-18 14:10:39 -0400425 fragBuilder->codeAppendf("half st_grad_len = 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 Nicholasf7b88202017-09-18 14:10:39 -0400432 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), 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 {");
439 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
440 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700441
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400442 fragBuilder->codeAppendf("half2 Jdx = dFdx(%s);", st.fsIn());
443 fragBuilder->codeAppendf("half2 Jdy = dFdy(%s);", st.fsIn());
444 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 -0400512constexpr GrPrimitiveProcessor::Attribute GrDistanceFieldPathGeoProc::kInPosition;
513constexpr GrPrimitiveProcessor::Attribute GrDistanceFieldPathGeoProc::kInColor;
Brian Salomon92be2f72018-06-19 14:33:47 -0400514
Brian Osman4a3f5c82018-09-18 16:16:38 -0400515GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(const GrShaderCaps& caps,
516 const SkMatrix& matrix,
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 Osman4a3f5c82018-09-18 16:16:38 -0400527 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
528 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Salomon92be2f72018-06-19 14:33:47 -0400529 this->setVertexAttributeCnt(3);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000530
531 if (numProxies) {
532 fAtlasSize = proxies[0]->isize();
533 }
534
Robert Phillips4bc70112018-03-01 10:24:02 -0500535 for (int i = 0; i < numProxies; ++i) {
536 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000537 SkASSERT(proxies[i]->isize() == fAtlasSize);
538 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400539 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400540 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500541}
542
Robert Phillips4bc70112018-03-01 10:24:02 -0500543void GrDistanceFieldPathGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
544 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400545 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500546 SkASSERT(numProxies <= kMaxTextures);
547
Brian Salomon7eae3e02018-08-07 14:02:38 +0000548 if (!fTextureSamplers[0].isInitialized()) {
549 fAtlasSize = proxies[0]->isize();
550 }
551
Robert Phillips4bc70112018-03-01 10:24:02 -0500552 for (int i = 0; i < numProxies; ++i) {
553 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000554 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500555
556 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000557 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400558 }
559 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400560 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400561}
562
Brian Salomon94efbf52016-11-29 13:43:05 -0500563void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800564 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700565 GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800566}
567
Brian Salomon94efbf52016-11-29 13:43:05 -0500568GrGLSLPrimitiveProcessor*
569GrDistanceFieldPathGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700570 return new GrGLDistanceFieldPathGeoProc();
jvanverthfa38a302014-10-06 05:59:05 -0700571}
572
Brian Salomon92be2f72018-06-19 14:33:47 -0400573const GrPrimitiveProcessor::Attribute& GrDistanceFieldPathGeoProc::onVertexAttribute(int i) const {
Brian Osman4a3f5c82018-09-18 16:16:38 -0400574 return IthAttribute(i, kInPosition, kInColor, fInTextureCoords);
Brian Salomon92be2f72018-06-19 14:33:47 -0400575}
576
jvanverthfa38a302014-10-06 05:59:05 -0700577///////////////////////////////////////////////////////////////////////////////
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),
Robert Phillips4bc70112018-03-01 10:24:02 -0500606 proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400607 samplerState,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400608 flags);
jvanverthfa38a302014-10-06 05:59:05 -0700609}
Hal Canary6f6961e2017-01-31 13:50:44 -0500610#endif
jvanverthfa38a302014-10-06 05:59:05 -0700611
612///////////////////////////////////////////////////////////////////////////////
613
egdaniele659a582015-11-13 09:55:43 -0800614class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000615public:
Brian Salomon5c6ac642017-12-19 11:09:32 -0500616 GrGLDistanceFieldLCDTextGeoProc() : fAtlasSize({0, 0}) {
jvanverth502286d2015-04-08 12:37:51 -0700617 fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
jvanverth21deace2015-04-01 12:43:48 -0700618 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000619
mtklein36352bf2015-03-25 18:17:31 -0700620 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700621 const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
622 args.fGP.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000623
egdaniel4ca2e602015-11-18 08:01:26 -0800624 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800625 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800626 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800627
628 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800629 varyingHandler->emitAttributes(dfTexEffect);
egdaniel4ca2e602015-11-18 08:01:26 -0800630
Robert Phillips8296e752017-08-25 08:45:21 -0400631 const char* atlasSizeInvName;
632 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400633 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400634 kHigh_GrSLPrecision,
635 "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 Verthfc4f7682018-01-25 16:26:25 -0500656 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 Nicholasf7b88202017-09-18 14:10:39 -0400683 fragBuilder->codeAppendf("half st_grad_len = 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 Nicholasf7b88202017-09-18 14:10:39 -0400686 fragBuilder->codeAppendf("half st_grad_len = abs(dFdy(%s.y));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700687#endif
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400688 fragBuilder->codeAppendf("half2 offset = half2(st_grad_len*%s, 0.0);", delta.fsIn());
jvanverthcf371bb2016-03-10 11:10:43 -0800689 } else if (isSimilarity) {
690 // For a similarity matrix with rotation, the gradient will not be aligned
691 // with the texel coordinate axes, so we need to calculate it.
jvanverthe499adf2016-07-20 12:22:14 -0700692#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400693 fragBuilder->codeAppendf("half2 st_grad = dFdx(%s);", st.fsIn());
694 fragBuilder->codeAppendf("half2 offset = %s*st_grad;", delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700695#else
jvanverthcf371bb2016-03-10 11:10:43 -0800696 // We use dFdy because of a Mali 400 bug, and rotate -90 degrees to
697 // get the gradient in the x direction.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400698 fragBuilder->codeAppendf("half2 st_grad = dFdy(%s);", st.fsIn());
699 fragBuilder->codeAppendf("half2 offset = %s*half2(st_grad.y, -st_grad.x);",
Robert Phillips8296e752017-08-25 08:45:21 -0400700 delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700701#endif
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400702 fragBuilder->codeAppend("half st_grad_len = length(st_grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000703 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400704 fragBuilder->codeAppendf("half2 st = %s;\n", st.fsIn());
jvanverthbb4a1cf2015-04-07 09:06:00 -0700705
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400706 fragBuilder->codeAppend("half2 Jdx = dFdx(st);");
707 fragBuilder->codeAppend("half2 Jdy = dFdy(st);");
708 fragBuilder->codeAppendf("half2 offset = %s*Jdx;", delta.fsIn());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000709 }
710
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400711 // sample the texture by index
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400712 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400713 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
714 texIdx, "uv", "texColor");
Jim Van Verth6c251d22017-09-08 12:29:07 -0400715
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400716 // green is distance to uv center
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400717 fragBuilder->codeAppend("half3 distance;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400718 fragBuilder->codeAppend("distance.y = texColor.r;");
719 // red is distance to left offset
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400720 fragBuilder->codeAppend("half2 uv_adjusted = uv - offset;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400721 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
722 texIdx, "uv_adjusted", "texColor");
723 fragBuilder->codeAppend("distance.x = texColor.r;");
724 // blue is distance to right offset
725 fragBuilder->codeAppend("uv_adjusted = uv + offset;");
726 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
727 texIdx, "uv_adjusted", "texColor");
728 fragBuilder->codeAppend("distance.z = texColor.r;");
729
730 fragBuilder->codeAppend("distance = "
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400731 "half3(" SK_DistanceFieldMultiplier ")*(distance - half3(" SK_DistanceFieldThreshold"));");
jvanverth2d2a68c2014-06-10 06:42:56 -0700732
jvanverth21deace2015-04-01 12:43:48 -0700733 // adjust width based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -0700734 const char* distanceAdjustUniName = nullptr;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400735 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -0800736 "DistanceAdjust", &distanceAdjustUniName);
egdaniel4ca2e602015-11-18 08:01:26 -0800737 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700738
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000739 // To be strictly correct, we should compute the anti-aliasing factor separately
740 // for each color component. However, this is only important when using perspective
741 // transformations, and even then using a single factor seems like a reasonable
742 // trade-off between quality and speed.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400743 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -0800744 if (isSimilarity) {
halcanary9d524f22016-03-29 09:03:52 -0700745 // For similarity transform (uniform scale-only is a subset of this), we adjust for the
746 // effect of the transformation on the distance by using the length of the gradient of
jvanverthcf371bb2016-03-10 11:10:43 -0800747 // the texture coordinates. We use st coordinates to ensure we're mapping 1:1 from texel
748 // space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700749
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000750 // this gives us a smooth step across approximately one fragment
jvanverthcf371bb2016-03-10 11:10:43 -0800751 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*st_grad_len;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000752 } else {
jvanverth354eba52015-03-16 11:32:49 -0700753 // For general transforms, to determine the amount of correction we multiply a unit
754 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
755 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400756 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance.r), 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 {");
763 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
764 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 Salomon92be2f72018-06-19 14:33:47 -0400831constexpr GrPrimitiveProcessor::Attribute GrDistanceFieldLCDTextGeoProc::kInColor;
Brian Salomon92be2f72018-06-19 14:33:47 -0400832
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 Osman4a3f5c82018-09-18 16:16:38 -0400852 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
853 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Salomon92be2f72018-06-19 14:33:47 -0400854 this->setVertexAttributeCnt(3);
855
Brian Salomon7eae3e02018-08-07 14:02:38 +0000856 if (numProxies) {
857 fAtlasSize = proxies[0]->isize();
858 }
859
Robert Phillips4bc70112018-03-01 10:24:02 -0500860 for (int i = 0; i < numProxies; ++i) {
861 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000862 SkASSERT(proxies[i]->isize() == fAtlasSize);
863 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400864 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400865 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500866}
867
Robert Phillips4bc70112018-03-01 10:24:02 -0500868void GrDistanceFieldLCDTextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
869 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400870 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500871 SkASSERT(numProxies <= kMaxTextures);
872
Brian Salomon7eae3e02018-08-07 14:02:38 +0000873 if (!fTextureSamplers[0].isInitialized()) {
874 fAtlasSize = proxies[0]->isize();
875 }
876
Robert Phillips4bc70112018-03-01 10:24:02 -0500877 for (int i = 0; i < numProxies; ++i) {
878 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000879 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500880
881 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000882 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
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
Brian Salomon92be2f72018-06-19 14:33:47 -0400897const GrPrimitiveProcessor::Attribute& GrDistanceFieldLCDTextGeoProc::onVertexAttribute(
898 int i) const {
Brian Osman4a3f5c82018-09-18 16:16:38 -0400899 return IthAttribute(i, fInPosition, kInColor, fInTextureCoords);
Brian Salomon92be2f72018-06-19 14:33:47 -0400900}
901
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000902///////////////////////////////////////////////////////////////////////////////
903
jvanverth502286d2015-04-08 12:37:51 -0700904GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000905
Hal Canary6f6961e2017-01-31 13:50:44 -0500906#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700907sk_sp<GrGeometryProcessor> GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700908 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
909 GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400910 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
911 d->textureProxy(texIdx),
912 nullptr,
913 nullptr,
914 nullptr
915 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500916
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400917 GrSamplerState::WrapMode wrapModes[2];
918 GrTest::TestWrapModes(d->fRandom, wrapModes);
919 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
920 ? GrSamplerState::Filter::kBilerp
921 : GrSamplerState::Filter::kNearest);
jvanverth21deace2015-04-01 12:43:48 -0700922 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
jvanverth78f07182014-07-30 06:17:59 -0700923 uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
jvanverthcf371bb2016-03-10 11:10:43 -0800924 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
925 if (flags & kSimilarity_DistanceFieldEffectFlag) {
926 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
927 }
joshualitt0067ff52015-07-08 14:26:19 -0700928 flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500929 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Brian Osman4a3f5c82018-09-18 16:16:38 -0400930 return GrDistanceFieldLCDTextGeoProc::Make(*d->caps()->shaderCaps(), proxies, 1, samplerState,
931 wa, flags, localMatrix);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000932}
Hal Canary6f6961e2017-01-31 13:50:44 -0500933#endif