blob: b0ddd91fab9ad18c230dcfa3e3b328c1bd464c8a [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 Verth1694a862018-12-17 19:48:42 +000071 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
72 GrGLSLVarying texIdx(texIdxType);
Chris Dalton27372882017-12-08 13:34:21 -070073 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -040074 append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -040075 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -040076
jvanverthcf371bb2016-03-10 11:10:43 -080077 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
78 kUniformScale_DistanceFieldEffectMask;
jvanverthbb4a1cf2015-04-07 09:06:00 -070079 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -070080 bool isGammaCorrect =
81 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
Jim Van Verth90e89b32017-07-06 16:36:55 -040082 bool isAliased =
83 SkToBool(dfTexEffect.getFlags() & kAliased_DistanceFieldEffectFlag);
halcanary9d524f22016-03-29 09:03:52 -070084
jvanverthfdf7ccc2015-01-27 08:19:33 -080085 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -040086 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -040087 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040088 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
89 texIdx, "uv", "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -080090
Ethan Nicholasf7b88202017-09-18 14:10:39 -040091 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -040092 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverth21deace2015-04-01 12:43:48 -070093#ifdef SK_GAMMA_APPLY_TO_A8
94 // adjust width based on gamma
egdaniel4ca2e602015-11-18 08:01:26 -080095 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -070096#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000097
Ethan Nicholasf7b88202017-09-18 14:10:39 -040098 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -080099 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700100 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700101 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800102 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000103
jvanverth354eba52015-03-16 11:32:49 -0700104 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700105#ifdef SK_VULKAN
106 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));",
107 st.fsIn());
108#else
109 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
egdaniel4ca2e602015-11-18 08:01:26 -0800110 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));",
111 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700112#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800113 } else if (isSimilarity) {
114 // For similarity transform, we adjust the effect of the transformation on the distance
115 // by using the length of the gradient of the texture coordinates. We use st coordinates
116 // to ensure we're mapping 1:1 from texel space to pixel space.
117 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
118
119 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700120#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400121 fragBuilder->codeAppendf("half st_grad_len = length(dFdx(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700122#else
123 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400124 fragBuilder->codeAppendf("half st_grad_len = length(dFdy(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700125#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800126 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverth354eba52015-03-16 11:32:49 -0700127 } else {
128 // For general transforms, to determine the amount of correction we multiply a unit
129 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
130 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400131 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700132 // the length of the gradient may be 0, so we need to check for this
133 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400134 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800135 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400136 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800137 fragBuilder->codeAppend("} else {");
138 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
139 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700140
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400141 fragBuilder->codeAppendf("half2 Jdx = dFdx(%s);", st.fsIn());
142 fragBuilder->codeAppendf("half2 Jdy = dFdy(%s);", st.fsIn());
143 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800144 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000145
146 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800147 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000148 }
brianosman0586f5c2016-04-12 12:48:21 -0700149
Jim Van Verth90e89b32017-07-06 16:36:55 -0400150 if (isAliased) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400151 fragBuilder->codeAppend("half val = distance > 0 ? 1.0 : 0.0;");
Jim Van Verth90e89b32017-07-06 16:36:55 -0400152 } else if (isGammaCorrect) {
153 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
154 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want
155 // distance mapped linearly to coverage, so use a linear step:
brianosman0586f5c2016-04-12 12:48:21 -0700156 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400157 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0586f5c2016-04-12 12:48:21 -0700158 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400159 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0586f5c2016-04-12 12:48:21 -0700160 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000161
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400162 fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000163 }
164
bsalomona624bf32016-09-20 09:12:47 -0700165 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
166 FPCoordTransformIter&& transformIter) override {
Robert Phillips8296e752017-08-25 08:45:21 -0400167 const GrDistanceFieldA8TextGeoProc& dfa8gp = proc.cast<GrDistanceFieldA8TextGeoProc>();
168
jvanverth2d2a68c2014-06-10 06:42:56 -0700169#ifdef SK_GAMMA_APPLY_TO_A8
Robert Phillips8296e752017-08-25 08:45:21 -0400170 float distanceAdjust = dfa8gp.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700171 if (distanceAdjust != fDistanceAdjust) {
Robert Phillipse2538312017-08-24 17:47:23 +0000172 fDistanceAdjust = distanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400173 pdman.set1f(fDistanceAdjustUni, distanceAdjust);
jvanverth2d2a68c2014-06-10 06:42:56 -0700174 }
175#endif
joshualitt5559ca22015-05-21 15:50:36 -0700176
Brian Salomon7eae3e02018-08-07 14:02:38 +0000177 const SkISize& atlasSize = dfa8gp.atlasSize();
178 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
Robert Phillips8296e752017-08-25 08:45:21 -0400179
Brian Salomon7eae3e02018-08-07 14:02:38 +0000180 if (fAtlasSize != atlasSize) {
181 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
182 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400183 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500184 this->setTransformDataHelper(dfa8gp.localMatrix(), pdman, &transformIter);
commit-bot@chromium.org8fe2ee12014-03-26 18:03:05 +0000185 }
186
robertphillips46d36f02015-01-18 08:14:14 -0800187 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500188 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700189 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700190 const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800191 uint32_t key = dfTexEffect.getFlags();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800192 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400193 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000194 }
195
jvanverth@google.comd830d132013-11-11 20:54:09 +0000196private:
mtklein50282b42015-01-22 07:59:52 -0800197#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500198 float fDistanceAdjust = -1.f;
jvanverth21deace2015-04-01 12:43:48 -0700199 UniformHandle fDistanceAdjustUni;
mtklein50282b42015-01-22 07:59:52 -0800200#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500201 SkISize fAtlasSize = {0, 0};
Robert Phillips8296e752017-08-25 08:45:21 -0400202 UniformHandle fAtlasSizeInvUniform;
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000203
egdaniele659a582015-11-13 09:55:43 -0800204 typedef GrGLSLGeometryProcessor INHERITED;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000205};
206
207///////////////////////////////////////////////////////////////////////////////
208
Brian Osman4a3f5c82018-09-18 16:16:38 -0400209GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(const GrShaderCaps& caps,
210 const sk_sp<GrTextureProxy>* proxies,
Brian Salomon92be2f72018-06-19 14:33:47 -0400211 int numProxies,
212 const GrSamplerState& params,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500213#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon92be2f72018-06-19 14:33:47 -0400214 float distanceAdjust,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500215#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400216 uint32_t flags,
217 const SkMatrix& localMatrix)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400218 : INHERITED(kGrDistanceFieldA8TextGeoProc_ClassID)
Brian Salomon92be2f72018-06-19 14:33:47 -0400219 , fLocalMatrix(localMatrix)
220 , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500221#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400222 , fDistanceAdjust(distanceAdjust)
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500223#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400224{
Robert Phillips4bc70112018-03-01 10:24:02 -0500225 SkASSERT(numProxies <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500226 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400227
Brian Salomon5c6ac642017-12-19 11:09:32 -0500228 if (flags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400229 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500230 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400231 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500232 }
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);
244 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400245 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400246 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500247}
248
Robert Phillips4bc70112018-03-01 10:24:02 -0500249void GrDistanceFieldA8TextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
250 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400251 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500252 SkASSERT(numProxies <= kMaxTextures);
253
Brian Salomon7eae3e02018-08-07 14:02:38 +0000254 if (!fTextureSamplers[0].isInitialized()) {
255 fAtlasSize = proxies[0]->isize();
256 }
257
Robert Phillips4bc70112018-03-01 10:24:02 -0500258 for (int i = 0; i < numProxies; ++i) {
259 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000260 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500261 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000262 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400263 }
264 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400265 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400266}
267
Brian Salomon94efbf52016-11-29 13:43:05 -0500268void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800269 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700270 GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800271}
272
Brian Salomon94efbf52016-11-29 13:43:05 -0500273GrGLSLPrimitiveProcessor*
274GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700275 return new GrGLDistanceFieldA8TextGeoProc();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000276}
277
278///////////////////////////////////////////////////////////////////////////////
279
jvanverth502286d2015-04-08 12:37:51 -0700280GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000281
Hal Canary6f6961e2017-01-31 13:50:44 -0500282#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700283sk_sp<GrGeometryProcessor> GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) {
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500284 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
285 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400286 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
287 d->textureProxy(texIdx),
288 nullptr,
289 nullptr,
290 nullptr
291 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500292
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400293 GrSamplerState::WrapMode wrapModes[2];
294 GrTest::TestWrapModes(d->fRandom, wrapModes);
295 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
296 ? GrSamplerState::Filter::kBilerp
297 : GrSamplerState::Filter::kNearest);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000298
jvanverthcf371bb2016-03-10 11:10:43 -0800299 uint32_t flags = 0;
300 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
301 if (flags & kSimilarity_DistanceFieldEffectFlag) {
302 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
303 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500304 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Mike Reede1367b42017-12-15 14:28:56 -0500305#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500306 float lum = d->fRandom->nextF();
Mike Reede1367b42017-12-15 14:28:56 -0500307#endif
Brian Osman4a3f5c82018-09-18 16:16:38 -0400308 return GrDistanceFieldA8TextGeoProc::Make(*d->caps()->shaderCaps(),
309 proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400310 samplerState,
jvanverth2d2a68c2014-06-10 06:42:56 -0700311#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500312 lum,
jvanverth2d2a68c2014-06-10 06:42:56 -0700313#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500314 flags, localMatrix);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000315}
Hal Canary6f6961e2017-01-31 13:50:44 -0500316#endif
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000317
318///////////////////////////////////////////////////////////////////////////////
319
egdaniele659a582015-11-13 09:55:43 -0800320class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700321public:
joshualitt465283c2015-09-11 08:19:35 -0700322 GrGLDistanceFieldPathGeoProc()
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400323 : fMatrix(SkMatrix::InvalidMatrix())
Robert Phillips8296e752017-08-25 08:45:21 -0400324 , fAtlasSize({0,0}) {
325 }
jvanverthfa38a302014-10-06 05:59:05 -0700326
mtklein36352bf2015-03-25 18:17:31 -0700327 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
Brian Salomon5c6ac642017-12-19 11:09:32 -0500328 const GrDistanceFieldPathGeoProc& dfPathEffect =
329 args.fGP.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700330
Chris Dalton60283612018-02-14 13:38:14 -0700331 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
jvanverthfa38a302014-10-06 05:59:05 -0700332
egdaniel4ca2e602015-11-18 08:01:26 -0800333 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800334 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800335 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800336
337 // emit attributes
Brian Salomon5c6ac642017-12-19 11:09:32 -0500338 varyingHandler->emitAttributes(dfPathEffect);
joshualittabb52a12015-01-13 15:02:10 -0800339
Robert Phillips8296e752017-08-25 08:45:21 -0400340 const char* atlasSizeInvName;
341 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400342 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400343 kHigh_GrSLPrecision,
344 "AtlasSizeInv",
345 &atlasSizeInvName);
346
Chris Dalton27372882017-12-08 13:34:21 -0700347 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van 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
Robert Phillips8296e752017-08-25 08:45:21 -0400407 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));",
408 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700409#else
410 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Robert Phillips8296e752017-08-25 08:45:21 -0400411 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));",
412 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700413#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800414 } else if (isSimilarity) {
415 // For similarity transform, we adjust the effect of the transformation on the distance
416 // by using the length of the gradient of the texture coordinates. We use st coordinates
417 // to ensure we're mapping 1:1 from texel space to pixel space.
jvanverthcf371bb2016-03-10 11:10:43 -0800418
419 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700420#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400421 fragBuilder->codeAppendf("half st_grad_len = length(dFdx(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700422#else
423 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400424 fragBuilder->codeAppendf("half st_grad_len = length(dFdy(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700425#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800426 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverthfa38a302014-10-06 05:59:05 -0700427 } else {
jvanverth354eba52015-03-16 11:32:49 -0700428 // For general transforms, to determine the amount of correction we multiply a unit
429 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
430 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400431 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700432 // the length of the gradient may be 0, so we need to check for this
433 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400434 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800435 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400436 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800437 fragBuilder->codeAppend("} else {");
438 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
439 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700440
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400441 fragBuilder->codeAppendf("half2 Jdx = dFdx(%s);", st.fsIn());
442 fragBuilder->codeAppendf("half2 Jdy = dFdy(%s);", st.fsIn());
443 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
444 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
jvanverthfa38a302014-10-06 05:59:05 -0700445
446 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800447 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
jvanverthfa38a302014-10-06 05:59:05 -0700448 }
brianosman0e3c5542016-04-13 13:56:21 -0700449 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
450 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
451 // mapped linearly to coverage, so use a linear step:
452 if (isGammaCorrect) {
453 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400454 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0e3c5542016-04-13 13:56:21 -0700455 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400456 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0e3c5542016-04-13 13:56:21 -0700457 }
jvanverthfa38a302014-10-06 05:59:05 -0700458
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400459 fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
jvanverthfa38a302014-10-06 05:59:05 -0700460 }
461
bsalomona624bf32016-09-20 09:12:47 -0700462 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
463 FPCoordTransformIter&& transformIter) override {
joshualitt9b989322014-12-15 14:16:27 -0800464
joshualitte578a952015-05-14 10:09:13 -0700465 const GrDistanceFieldPathGeoProc& dfpgp = proc.cast<GrDistanceFieldPathGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700466
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400467 if (dfpgp.matrix().hasPerspective() && !fMatrix.cheapEqualTo(dfpgp.matrix())) {
468 fMatrix = dfpgp.matrix();
469 float matrix[3 * 3];
470 GrGLSLGetMatrix<3>(matrix, fMatrix);
471 pdman.setMatrix3f(fMatrixUniform, matrix);
joshualitt5559ca22015-05-21 15:50:36 -0700472 }
Robert Phillips8296e752017-08-25 08:45:21 -0400473
Brian Salomon7eae3e02018-08-07 14:02:38 +0000474 const SkISize& atlasSize = dfpgp.atlasSize();
475 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
476 if (fAtlasSize != atlasSize) {
477 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
478 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400479 }
480
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400481 if (dfpgp.matrix().hasPerspective()) {
482 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
483 } else {
484 this->setTransformDataHelper(dfpgp.matrix(), pdman, &transformIter);
485 }
jvanverthfa38a302014-10-06 05:59:05 -0700486 }
487
robertphillips46d36f02015-01-18 08:14:14 -0800488 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500489 const GrShaderCaps&,
jvanverthfa38a302014-10-06 05:59:05 -0700490 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700491 const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700492
joshualitt8fc6c2d2014-12-22 15:27:05 -0800493 uint32_t key = dfTexEffect.getFlags();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400494 key |= ComputePosKey(dfTexEffect.matrix()) << 16;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800495 b->add32(key);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400496 b->add32(dfTexEffect.matrix().hasPerspective());
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400497 b->add32(dfTexEffect.numTextureSamplers());
jvanverthfa38a302014-10-06 05:59:05 -0700498 }
499
500private:
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400501 SkMatrix fMatrix; // view matrix if perspective, local matrix otherwise
502 UniformHandle fMatrixUniform;
Robert Phillips8296e752017-08-25 08:45:21 -0400503
504 SkISize fAtlasSize;
505 UniformHandle fAtlasSizeInvUniform;
jvanverthfa38a302014-10-06 05:59:05 -0700506
egdaniele659a582015-11-13 09:55:43 -0800507 typedef GrGLSLGeometryProcessor INHERITED;
jvanverthfa38a302014-10-06 05:59:05 -0700508};
509
510///////////////////////////////////////////////////////////////////////////////
Brian Salomon92be2f72018-06-19 14:33:47 -0400511
Brian Osman4a3f5c82018-09-18 16:16:38 -0400512GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(const GrShaderCaps& caps,
513 const SkMatrix& matrix,
Brian Osmanc906d252018-12-04 11:17:46 -0500514 bool wideColor,
Brian Salomon92be2f72018-06-19 14:33:47 -0400515 const sk_sp<GrTextureProxy>* proxies,
516 int numProxies,
517 const GrSamplerState& params,
518 uint32_t flags)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400519 : INHERITED(kGrDistanceFieldPathGeoProc_ClassID)
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400520 , fMatrix(matrix)
Brian Salomon92be2f72018-06-19 14:33:47 -0400521 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500522 SkASSERT(numProxies <= kMaxTextures);
Mike Klein5045e502018-06-19 01:40:57 +0000523 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400524
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500525 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Osmanc906d252018-12-04 11:17:46 -0500526 fInColor = MakeColorAttribute("inColor", wideColor);
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000527 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
528 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500529 this->setVertexAttributes(&fInPosition, 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
573///////////////////////////////////////////////////////////////////////////////
574
jvanverth502286d2015-04-08 12:37:51 -0700575GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc);
jvanverthfa38a302014-10-06 05:59:05 -0700576
Hal Canary6f6961e2017-01-31 13:50:44 -0500577#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700578sk_sp<GrGeometryProcessor> GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700579 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
580 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400581 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
582 d->textureProxy(texIdx),
583 nullptr,
584 nullptr,
585 nullptr
586 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500587
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400588 GrSamplerState::WrapMode wrapModes[2];
589 GrTest::TestWrapModes(d->fRandom, wrapModes);
590 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
591 ? GrSamplerState::Filter::kBilerp
592 : GrSamplerState::Filter::kNearest);
jvanverthfa38a302014-10-06 05:59:05 -0700593
jvanverthcf371bb2016-03-10 11:10:43 -0800594 uint32_t flags = 0;
595 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
596 if (flags & kSimilarity_DistanceFieldEffectFlag) {
597 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
598 }
599
Brian Osman4a3f5c82018-09-18 16:16:38 -0400600 return GrDistanceFieldPathGeoProc::Make(*d->caps()->shaderCaps(),
601 GrTest::TestMatrix(d->fRandom),
Brian Osmanc906d252018-12-04 11:17:46 -0500602 d->fRandom->nextBool(),
Robert Phillips4bc70112018-03-01 10:24:02 -0500603 proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400604 samplerState,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400605 flags);
jvanverthfa38a302014-10-06 05:59:05 -0700606}
Hal Canary6f6961e2017-01-31 13:50:44 -0500607#endif
jvanverthfa38a302014-10-06 05:59:05 -0700608
609///////////////////////////////////////////////////////////////////////////////
610
egdaniele659a582015-11-13 09:55:43 -0800611class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000612public:
Brian Salomon5c6ac642017-12-19 11:09:32 -0500613 GrGLDistanceFieldLCDTextGeoProc() : fAtlasSize({0, 0}) {
jvanverth502286d2015-04-08 12:37:51 -0700614 fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
jvanverth21deace2015-04-01 12:43:48 -0700615 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000616
mtklein36352bf2015-03-25 18:17:31 -0700617 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700618 const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
619 args.fGP.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000620
egdaniel4ca2e602015-11-18 08:01:26 -0800621 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800622 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800623 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800624
625 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800626 varyingHandler->emitAttributes(dfTexEffect);
egdaniel4ca2e602015-11-18 08:01:26 -0800627
Robert Phillips8296e752017-08-25 08:45:21 -0400628 const char* atlasSizeInvName;
629 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400630 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400631 kHigh_GrSLPrecision,
632 "AtlasSizeInv",
633 &atlasSizeInvName);
634
Chris Dalton60283612018-02-14 13:38:14 -0700635 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittabb52a12015-01-13 15:02:10 -0800636
joshualitt9b989322014-12-15 14:16:27 -0800637 // setup pass through color
Brian Salomonbfd51832017-01-04 13:22:08 -0500638 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualitt9b989322014-12-15 14:16:27 -0800639
joshualittabb52a12015-01-13 15:02:10 -0800640 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -0400641 gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
joshualitt4973d9d2014-11-08 09:24:25 -0800642
joshualittabb52a12015-01-13 15:02:10 -0800643 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800644 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800645 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800646 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400647 dfTexEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500648 dfTexEffect.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -0700649 args.fFPCoordTransformHandler);
joshualittabb52a12015-01-13 15:02:10 -0800650
jvanverthbb4a1cf2015-04-07 09:06:00 -0700651 // set up varyings
Chris Dalton27372882017-12-08 13:34:21 -0700652 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verth1694a862018-12-17 19:48:42 +0000653 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
654 GrGLSLVarying texIdx(texIdxType);
Chris Dalton27372882017-12-08 13:34:21 -0700655 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -0400656 append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -0400657 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -0400658
Chris Dalton27372882017-12-08 13:34:21 -0700659 GrGLSLVarying delta(kFloat_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600660 varyingHandler->addVarying("Delta", &delta);
Robert Phillips8296e752017-08-25 08:45:21 -0400661 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) {
662 vertBuilder->codeAppendf("%s = -%s.x/3.0;", delta.vsOut(), atlasSizeInvName);
663 } else {
664 vertBuilder->codeAppendf("%s = %s.x/3.0;", delta.vsOut(), atlasSizeInvName);
665 }
666
667 // add frag shader code
jvanverthcf371bb2016-03-10 11:10:43 -0800668 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
669 kUniformScale_DistanceFieldEffectMask;
670 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -0700671 bool isGammaCorrect =
672 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
joshualitt30ba4362014-08-21 20:18:45 -0700673
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000674 // create LCD offset adjusted by inverse of transform
jvanverthfdf7ccc2015-01-27 08:19:33 -0800675 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400676 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
joshualitt922c8b12015-08-07 09:55:23 -0700677
jvanverth78f07182014-07-30 06:17:59 -0700678 if (isUniformScale) {
jvanverthe499adf2016-07-20 12:22:14 -0700679#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400680 fragBuilder->codeAppendf("half st_grad_len = abs(dFdx(%s.x));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700681#else
682 // 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 -0400683 fragBuilder->codeAppendf("half st_grad_len = abs(dFdy(%s.y));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700684#endif
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400685 fragBuilder->codeAppendf("half2 offset = half2(st_grad_len*%s, 0.0);", delta.fsIn());
jvanverthcf371bb2016-03-10 11:10:43 -0800686 } else if (isSimilarity) {
687 // For a similarity matrix with rotation, the gradient will not be aligned
688 // with the texel coordinate axes, so we need to calculate it.
jvanverthe499adf2016-07-20 12:22:14 -0700689#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400690 fragBuilder->codeAppendf("half2 st_grad = dFdx(%s);", st.fsIn());
691 fragBuilder->codeAppendf("half2 offset = %s*st_grad;", delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700692#else
jvanverthcf371bb2016-03-10 11:10:43 -0800693 // We use dFdy because of a Mali 400 bug, and rotate -90 degrees to
694 // get the gradient in the x direction.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400695 fragBuilder->codeAppendf("half2 st_grad = dFdy(%s);", st.fsIn());
696 fragBuilder->codeAppendf("half2 offset = %s*half2(st_grad.y, -st_grad.x);",
Robert Phillips8296e752017-08-25 08:45:21 -0400697 delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700698#endif
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400699 fragBuilder->codeAppend("half st_grad_len = length(st_grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000700 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400701 fragBuilder->codeAppendf("half2 st = %s;\n", st.fsIn());
jvanverthbb4a1cf2015-04-07 09:06:00 -0700702
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400703 fragBuilder->codeAppend("half2 Jdx = dFdx(st);");
704 fragBuilder->codeAppend("half2 Jdy = dFdy(st);");
705 fragBuilder->codeAppendf("half2 offset = %s*Jdx;", delta.fsIn());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000706 }
707
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400708 // sample the texture by index
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400709 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400710 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
711 texIdx, "uv", "texColor");
Jim Van Verth6c251d22017-09-08 12:29:07 -0400712
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400713 // green is distance to uv center
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400714 fragBuilder->codeAppend("half3 distance;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400715 fragBuilder->codeAppend("distance.y = texColor.r;");
716 // red is distance to left offset
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400717 fragBuilder->codeAppend("half2 uv_adjusted = uv - offset;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400718 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
719 texIdx, "uv_adjusted", "texColor");
720 fragBuilder->codeAppend("distance.x = texColor.r;");
721 // blue is distance to right offset
722 fragBuilder->codeAppend("uv_adjusted = uv + offset;");
723 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
724 texIdx, "uv_adjusted", "texColor");
725 fragBuilder->codeAppend("distance.z = texColor.r;");
726
727 fragBuilder->codeAppend("distance = "
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400728 "half3(" SK_DistanceFieldMultiplier ")*(distance - half3(" SK_DistanceFieldThreshold"));");
jvanverth2d2a68c2014-06-10 06:42:56 -0700729
jvanverth21deace2015-04-01 12:43:48 -0700730 // adjust width based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -0700731 const char* distanceAdjustUniName = nullptr;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400732 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -0800733 "DistanceAdjust", &distanceAdjustUniName);
egdaniel4ca2e602015-11-18 08:01:26 -0800734 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700735
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000736 // To be strictly correct, we should compute the anti-aliasing factor separately
737 // for each color component. However, this is only important when using perspective
738 // transformations, and even then using a single factor seems like a reasonable
739 // trade-off between quality and speed.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400740 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -0800741 if (isSimilarity) {
halcanary9d524f22016-03-29 09:03:52 -0700742 // For similarity transform (uniform scale-only is a subset of this), we adjust for the
743 // effect of the transformation on the distance by using the length of the gradient of
jvanverthcf371bb2016-03-10 11:10:43 -0800744 // the texture coordinates. We use st coordinates to ensure we're mapping 1:1 from texel
745 // space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700746
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000747 // this gives us a smooth step across approximately one fragment
jvanverthcf371bb2016-03-10 11:10:43 -0800748 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*st_grad_len;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000749 } else {
jvanverth354eba52015-03-16 11:32:49 -0700750 // For general transforms, to determine the amount of correction we multiply a unit
751 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
752 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400753 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance.r), dFdy(distance.r));");
jvanverthd68a5502015-03-16 12:58:43 -0700754 // the length of the gradient may be 0, so we need to check for this
755 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400756 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800757 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400758 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800759 fragBuilder->codeAppend("} else {");
760 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
761 fragBuilder->codeAppend("}");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400762 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800763 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000764
765 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800766 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000767 }
768
brianosman0586f5c2016-04-12 12:48:21 -0700769 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
770 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
771 // mapped linearly to coverage, so use a linear step:
brianosmanb461d342016-04-13 13:10:14 -0700772 if (isGammaCorrect) {
Greg Daniel55923822017-05-22 16:34:34 -0400773 fragBuilder->codeAppendf("%s = "
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400774 "half4(saturate((distance + half3(afwidth)) / half3(2.0 * afwidth)), 1.0);",
Greg Daniel55923822017-05-22 16:34:34 -0400775 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700776 } else {
Greg Daniel55923822017-05-22 16:34:34 -0400777 fragBuilder->codeAppendf(
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400778 "%s = half4(smoothstep(half3(-afwidth), half3(afwidth), distance), 1.0);",
Greg Daniel55923822017-05-22 16:34:34 -0400779 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700780 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000781 }
782
bsalomona624bf32016-09-20 09:12:47 -0700783 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& processor,
784 FPCoordTransformIter&& transformIter) override {
jvanverth21deace2015-04-01 12:43:48 -0700785 SkASSERT(fDistanceAdjustUni.isValid());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000786
joshualitt5559ca22015-05-21 15:50:36 -0700787 const GrDistanceFieldLCDTextGeoProc& dflcd = processor.cast<GrDistanceFieldLCDTextGeoProc>();
788 GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700789 if (wa != fDistanceAdjust) {
790 pdman.set3f(fDistanceAdjustUni,
791 wa.fR,
792 wa.fG,
793 wa.fB);
794 fDistanceAdjust = wa;
jvanverth2d2a68c2014-06-10 06:42:56 -0700795 }
joshualitt9b989322014-12-15 14:16:27 -0800796
Brian Salomon7eae3e02018-08-07 14:02:38 +0000797 const SkISize& atlasSize = dflcd.atlasSize();
798 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
799 if (fAtlasSize != atlasSize) {
800 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
801 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400802 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500803 this->setTransformDataHelper(dflcd.localMatrix(), pdman, &transformIter);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000804 }
805
robertphillips46d36f02015-01-18 08:14:14 -0800806 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500807 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700808 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700809 const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000810
joshualitt8fc6c2d2014-12-22 15:27:05 -0800811 uint32_t key = dfTexEffect.getFlags();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800812 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400813 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000814 }
815
816private:
jvanverth502286d2015-04-08 12:37:51 -0700817 GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400818 UniformHandle fDistanceAdjustUni;
819
820 SkISize fAtlasSize;
821 UniformHandle fAtlasSizeInvUniform;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000822
egdaniele659a582015-11-13 09:55:43 -0800823 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000824};
825
826///////////////////////////////////////////////////////////////////////////////
Mike Klein5045e502018-06-19 01:40:57 +0000827
Brian Osman4a3f5c82018-09-18 16:16:38 -0400828GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(const GrShaderCaps& caps,
829 const sk_sp<GrTextureProxy>* proxies,
Brian Salomon92be2f72018-06-19 14:33:47 -0400830 int numProxies,
831 const GrSamplerState& params,
832 DistanceAdjust distanceAdjust,
833 uint32_t flags,
834 const SkMatrix& localMatrix)
835 : INHERITED(kGrDistanceFieldLCDTextGeoProc_ClassID)
836 , fLocalMatrix(localMatrix)
837 , fDistanceAdjust(distanceAdjust)
838 , fFlags(flags & kLCD_DistanceFieldEffectMask) {
839 SkASSERT(numProxies <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500840 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag));
Brian Salomon92be2f72018-06-19 14:33:47 -0400841
Brian Salomon5c6ac642017-12-19 11:09:32 -0500842 if (fFlags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400843 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500844 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400845 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500846 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500847 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000848 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
849 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500850 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400851
Brian Salomon7eae3e02018-08-07 14:02:38 +0000852 if (numProxies) {
853 fAtlasSize = proxies[0]->isize();
854 }
855
Robert Phillips4bc70112018-03-01 10:24:02 -0500856 for (int i = 0; i < numProxies; ++i) {
857 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000858 SkASSERT(proxies[i]->isize() == fAtlasSize);
859 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400860 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400861 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500862}
863
Robert Phillips4bc70112018-03-01 10:24:02 -0500864void GrDistanceFieldLCDTextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
865 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400866 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500867 SkASSERT(numProxies <= kMaxTextures);
868
Brian Salomon7eae3e02018-08-07 14:02:38 +0000869 if (!fTextureSamplers[0].isInitialized()) {
870 fAtlasSize = proxies[0]->isize();
871 }
872
Robert Phillips4bc70112018-03-01 10:24:02 -0500873 for (int i = 0; i < numProxies; ++i) {
874 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000875 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500876
877 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000878 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400879 }
880 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400881 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400882}
883
Brian Salomon94efbf52016-11-29 13:43:05 -0500884void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800885 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700886 GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800887}
888
Brian Salomon94efbf52016-11-29 13:43:05 -0500889GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700890 return new GrGLDistanceFieldLCDTextGeoProc();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000891}
892
893///////////////////////////////////////////////////////////////////////////////
894
jvanverth502286d2015-04-08 12:37:51 -0700895GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000896
Hal Canary6f6961e2017-01-31 13:50:44 -0500897#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700898sk_sp<GrGeometryProcessor> GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700899 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
900 GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400901 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
902 d->textureProxy(texIdx),
903 nullptr,
904 nullptr,
905 nullptr
906 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500907
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400908 GrSamplerState::WrapMode wrapModes[2];
909 GrTest::TestWrapModes(d->fRandom, wrapModes);
910 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
911 ? GrSamplerState::Filter::kBilerp
912 : GrSamplerState::Filter::kNearest);
jvanverth21deace2015-04-01 12:43:48 -0700913 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
jvanverth78f07182014-07-30 06:17:59 -0700914 uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
jvanverthcf371bb2016-03-10 11:10:43 -0800915 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
916 if (flags & kSimilarity_DistanceFieldEffectFlag) {
917 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
918 }
joshualitt0067ff52015-07-08 14:26:19 -0700919 flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500920 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Brian Osman4a3f5c82018-09-18 16:16:38 -0400921 return GrDistanceFieldLCDTextGeoProc::Make(*d->caps()->shaderCaps(), proxies, 1, samplerState,
922 wa, flags, localMatrix);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000923}
Hal Canary6f6961e2017-01-31 13:50:44 -0500924#endif