blob: 314a9843648818831ef621c17eca85d12bd6d1e7 [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 Verth07110942018-11-20 14:46:41 -050071 GrGLSLVarying texIdx(kFloat_GrSLType);
Chris Dalton27372882017-12-08 13:34:21 -070072 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -040073 append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -040074 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -040075
jvanverthcf371bb2016-03-10 11:10:43 -080076 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
77 kUniformScale_DistanceFieldEffectMask;
jvanverthbb4a1cf2015-04-07 09:06:00 -070078 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -070079 bool isGammaCorrect =
80 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
Jim Van Verth90e89b32017-07-06 16:36:55 -040081 bool isAliased =
82 SkToBool(dfTexEffect.getFlags() & kAliased_DistanceFieldEffectFlag);
halcanary9d524f22016-03-29 09:03:52 -070083
jvanverthfdf7ccc2015-01-27 08:19:33 -080084 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -040085 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -040086 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040087 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
88 texIdx, "uv", "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -080089
Ethan Nicholasf7b88202017-09-18 14:10:39 -040090 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -040091 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverth21deace2015-04-01 12:43:48 -070092#ifdef SK_GAMMA_APPLY_TO_A8
93 // adjust width based on gamma
egdaniel4ca2e602015-11-18 08:01:26 -080094 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -070095#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000096
Ethan Nicholasf7b88202017-09-18 14:10:39 -040097 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -080098 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -070099 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700100 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800101 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000102
jvanverth354eba52015-03-16 11:32:49 -0700103 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700104#ifdef SK_VULKAN
105 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));",
106 st.fsIn());
107#else
108 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
egdaniel4ca2e602015-11-18 08:01:26 -0800109 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));",
110 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700111#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800112 } else if (isSimilarity) {
113 // For similarity transform, we adjust the effect of the transformation on the distance
114 // by using the length of the gradient of the texture coordinates. We use st coordinates
115 // to ensure we're mapping 1:1 from texel space to pixel space.
116 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
117
118 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700119#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400120 fragBuilder->codeAppendf("half st_grad_len = length(dFdx(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700121#else
122 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400123 fragBuilder->codeAppendf("half st_grad_len = length(dFdy(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700124#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800125 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverth354eba52015-03-16 11:32:49 -0700126 } else {
127 // For general transforms, to determine the amount of correction we multiply a unit
128 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
129 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400130 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700131 // the length of the gradient may be 0, so we need to check for this
132 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400133 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800134 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400135 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800136 fragBuilder->codeAppend("} else {");
137 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
138 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700139
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400140 fragBuilder->codeAppendf("half2 Jdx = dFdx(%s);", st.fsIn());
141 fragBuilder->codeAppendf("half2 Jdy = dFdy(%s);", st.fsIn());
142 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800143 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000144
145 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800146 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000147 }
brianosman0586f5c2016-04-12 12:48:21 -0700148
Jim Van Verth90e89b32017-07-06 16:36:55 -0400149 if (isAliased) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400150 fragBuilder->codeAppend("half val = distance > 0 ? 1.0 : 0.0;");
Jim Van Verth90e89b32017-07-06 16:36:55 -0400151 } else if (isGammaCorrect) {
152 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
153 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want
154 // distance mapped linearly to coverage, so use a linear step:
brianosman0586f5c2016-04-12 12:48:21 -0700155 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400156 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0586f5c2016-04-12 12:48:21 -0700157 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400158 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0586f5c2016-04-12 12:48:21 -0700159 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000160
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400161 fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000162 }
163
bsalomona624bf32016-09-20 09:12:47 -0700164 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
165 FPCoordTransformIter&& transformIter) override {
Robert Phillips8296e752017-08-25 08:45:21 -0400166 const GrDistanceFieldA8TextGeoProc& dfa8gp = proc.cast<GrDistanceFieldA8TextGeoProc>();
167
jvanverth2d2a68c2014-06-10 06:42:56 -0700168#ifdef SK_GAMMA_APPLY_TO_A8
Robert Phillips8296e752017-08-25 08:45:21 -0400169 float distanceAdjust = dfa8gp.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700170 if (distanceAdjust != fDistanceAdjust) {
Robert Phillipse2538312017-08-24 17:47:23 +0000171 fDistanceAdjust = distanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400172 pdman.set1f(fDistanceAdjustUni, distanceAdjust);
jvanverth2d2a68c2014-06-10 06:42:56 -0700173 }
174#endif
joshualitt5559ca22015-05-21 15:50:36 -0700175
Brian Salomon7eae3e02018-08-07 14:02:38 +0000176 const SkISize& atlasSize = dfa8gp.atlasSize();
177 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
Robert Phillips8296e752017-08-25 08:45:21 -0400178
Brian Salomon7eae3e02018-08-07 14:02:38 +0000179 if (fAtlasSize != atlasSize) {
180 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
181 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400182 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500183 this->setTransformDataHelper(dfa8gp.localMatrix(), pdman, &transformIter);
commit-bot@chromium.org8fe2ee12014-03-26 18:03:05 +0000184 }
185
robertphillips46d36f02015-01-18 08:14:14 -0800186 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500187 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700188 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700189 const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800190 uint32_t key = dfTexEffect.getFlags();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800191 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400192 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000193 }
194
jvanverth@google.comd830d132013-11-11 20:54:09 +0000195private:
mtklein50282b42015-01-22 07:59:52 -0800196#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500197 float fDistanceAdjust = -1.f;
jvanverth21deace2015-04-01 12:43:48 -0700198 UniformHandle fDistanceAdjustUni;
mtklein50282b42015-01-22 07:59:52 -0800199#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500200 SkISize fAtlasSize = {0, 0};
Robert Phillips8296e752017-08-25 08:45:21 -0400201 UniformHandle fAtlasSizeInvUniform;
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000202
egdaniele659a582015-11-13 09:55:43 -0800203 typedef GrGLSLGeometryProcessor INHERITED;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000204};
205
206///////////////////////////////////////////////////////////////////////////////
207
Brian Osman4a3f5c82018-09-18 16:16:38 -0400208GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(const GrShaderCaps& caps,
209 const sk_sp<GrTextureProxy>* proxies,
Brian Salomon92be2f72018-06-19 14:33:47 -0400210 int numProxies,
211 const GrSamplerState& params,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500212#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon92be2f72018-06-19 14:33:47 -0400213 float distanceAdjust,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500214#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400215 uint32_t flags,
216 const SkMatrix& localMatrix)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400217 : INHERITED(kGrDistanceFieldA8TextGeoProc_ClassID)
Brian Salomon92be2f72018-06-19 14:33:47 -0400218 , fLocalMatrix(localMatrix)
219 , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500220#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400221 , fDistanceAdjust(distanceAdjust)
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500222#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400223{
Robert Phillips4bc70112018-03-01 10:24:02 -0500224 SkASSERT(numProxies <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500225 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400226
Brian Salomon5c6ac642017-12-19 11:09:32 -0500227 if (flags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400228 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500229 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400230 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500231 }
Jim Van Verth07110942018-11-20 14:46:41 -0500232 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000233 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
234 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500235 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400236
Brian Salomon7eae3e02018-08-07 14:02:38 +0000237 if (numProxies) {
238 fAtlasSize = proxies[0]->isize();
239 }
Robert Phillips4bc70112018-03-01 10:24:02 -0500240 for (int i = 0; i < numProxies; ++i) {
241 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000242 SkASSERT(proxies[i]->isize() == fAtlasSize);
243 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400244 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400245 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500246}
247
Robert Phillips4bc70112018-03-01 10:24:02 -0500248void GrDistanceFieldA8TextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
249 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400250 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500251 SkASSERT(numProxies <= kMaxTextures);
252
Brian Salomon7eae3e02018-08-07 14:02:38 +0000253 if (!fTextureSamplers[0].isInitialized()) {
254 fAtlasSize = proxies[0]->isize();
255 }
256
Robert Phillips4bc70112018-03-01 10:24:02 -0500257 for (int i = 0; i < numProxies; ++i) {
258 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000259 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500260 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000261 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400262 }
263 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400264 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400265}
266
Brian Salomon94efbf52016-11-29 13:43:05 -0500267void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800268 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700269 GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800270}
271
Brian Salomon94efbf52016-11-29 13:43:05 -0500272GrGLSLPrimitiveProcessor*
273GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700274 return new GrGLDistanceFieldA8TextGeoProc();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000275}
276
277///////////////////////////////////////////////////////////////////////////////
278
jvanverth502286d2015-04-08 12:37:51 -0700279GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000280
Hal Canary6f6961e2017-01-31 13:50:44 -0500281#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700282sk_sp<GrGeometryProcessor> GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) {
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500283 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
284 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400285 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
286 d->textureProxy(texIdx),
287 nullptr,
288 nullptr,
289 nullptr
290 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500291
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400292 GrSamplerState::WrapMode wrapModes[2];
293 GrTest::TestWrapModes(d->fRandom, wrapModes);
294 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
295 ? GrSamplerState::Filter::kBilerp
296 : GrSamplerState::Filter::kNearest);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000297
jvanverthcf371bb2016-03-10 11:10:43 -0800298 uint32_t flags = 0;
299 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
300 if (flags & kSimilarity_DistanceFieldEffectFlag) {
301 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
302 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500303 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Mike Reede1367b42017-12-15 14:28:56 -0500304#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500305 float lum = d->fRandom->nextF();
Mike Reede1367b42017-12-15 14:28:56 -0500306#endif
Brian Osman4a3f5c82018-09-18 16:16:38 -0400307 return GrDistanceFieldA8TextGeoProc::Make(*d->caps()->shaderCaps(),
308 proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400309 samplerState,
jvanverth2d2a68c2014-06-10 06:42:56 -0700310#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500311 lum,
jvanverth2d2a68c2014-06-10 06:42:56 -0700312#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500313 flags, localMatrix);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000314}
Hal Canary6f6961e2017-01-31 13:50:44 -0500315#endif
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000316
317///////////////////////////////////////////////////////////////////////////////
318
egdaniele659a582015-11-13 09:55:43 -0800319class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700320public:
joshualitt465283c2015-09-11 08:19:35 -0700321 GrGLDistanceFieldPathGeoProc()
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400322 : fMatrix(SkMatrix::InvalidMatrix())
Robert Phillips8296e752017-08-25 08:45:21 -0400323 , fAtlasSize({0,0}) {
324 }
jvanverthfa38a302014-10-06 05:59:05 -0700325
mtklein36352bf2015-03-25 18:17:31 -0700326 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
Brian Salomon5c6ac642017-12-19 11:09:32 -0500327 const GrDistanceFieldPathGeoProc& dfPathEffect =
328 args.fGP.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700329
Chris Dalton60283612018-02-14 13:38:14 -0700330 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
jvanverthfa38a302014-10-06 05:59:05 -0700331
egdaniel4ca2e602015-11-18 08:01:26 -0800332 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800333 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800334 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800335
336 // emit attributes
Brian Salomon5c6ac642017-12-19 11:09:32 -0500337 varyingHandler->emitAttributes(dfPathEffect);
joshualittabb52a12015-01-13 15:02:10 -0800338
Robert Phillips8296e752017-08-25 08:45:21 -0400339 const char* atlasSizeInvName;
340 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400341 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400342 kHigh_GrSLPrecision,
343 "AtlasSizeInv",
344 &atlasSizeInvName);
345
Chris Dalton27372882017-12-08 13:34:21 -0700346 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verth07110942018-11-20 14:46:41 -0500347 GrGLSLVarying texIdx(kFloat_GrSLType);
Chris Dalton27372882017-12-08 13:34:21 -0700348 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -0400349 append_index_uv_varyings(args, dfPathEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
350 &texIdx, &st);
jvanverthfa38a302014-10-06 05:59:05 -0700351
joshualitt9b989322014-12-15 14:16:27 -0800352 // setup pass through color
Brian Salomon5c6ac642017-12-19 11:09:32 -0500353 varyingHandler->addPassThroughAttribute(dfPathEffect.inColor(), args.fOutputColor);
reede4ef1ca2015-02-17 18:38:38 -0800354
Brian Salomon5c6ac642017-12-19 11:09:32 -0500355 if (dfPathEffect.matrix().hasPerspective()) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400356 // Setup position
357 this->writeOutputPosition(vertBuilder,
358 uniformHandler,
359 gpArgs,
Brian Salomon92be2f72018-06-19 14:33:47 -0400360 dfPathEffect.inPosition().name(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500361 dfPathEffect.matrix(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400362 &fMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -0800363
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400364 // emit transforms
365 this->emitTransforms(vertBuilder,
366 varyingHandler,
367 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400368 dfPathEffect.inPosition().asShaderVar(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400369 args.fFPCoordTransformHandler);
370 } else {
371 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -0400372 this->writeOutputPosition(vertBuilder, gpArgs, dfPathEffect.inPosition().name());
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400373
374 // emit transforms
375 this->emitTransforms(vertBuilder,
376 varyingHandler,
377 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400378 dfPathEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500379 dfPathEffect.matrix(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400380 args.fFPCoordTransformHandler);
381 }
joshualitt4973d9d2014-11-08 09:24:25 -0800382
jvanverthfdf7ccc2015-01-27 08:19:33 -0800383 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400384 fragBuilder->codeAppendf("float2 uv = %s;", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400385 fragBuilder->codeAppend("half4 texColor;");
Brian Salomon5c6ac642017-12-19 11:09:32 -0500386 append_multitexture_lookup(args, dfPathEffect.numTextureSamplers(), texIdx, "uv",
387 "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -0800388
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400389 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400390 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverthfa38a302014-10-06 05:59:05 -0700391
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400392 fragBuilder->codeAppend("half afwidth;");
Brian Salomon5c6ac642017-12-19 11:09:32 -0500393 bool isUniformScale = (dfPathEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
394 kUniformScale_DistanceFieldEffectMask;
395 bool isSimilarity = SkToBool(dfPathEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosman0e3c5542016-04-13 13:56:21 -0700396 bool isGammaCorrect =
Brian Salomon5c6ac642017-12-19 11:09:32 -0500397 SkToBool(dfPathEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
jvanverthcf371bb2016-03-10 11:10:43 -0800398 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700399 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700400 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800401 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700402
jvanverthfa38a302014-10-06 05:59:05 -0700403 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700404#ifdef SK_VULKAN
Robert Phillips8296e752017-08-25 08:45:21 -0400405 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));",
406 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700407#else
408 // 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 -0400409 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));",
410 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700411#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800412 } else if (isSimilarity) {
413 // For similarity transform, we adjust the effect of the transformation on the distance
414 // by using the length of the gradient of the texture coordinates. We use st coordinates
415 // to ensure we're mapping 1:1 from texel space to pixel space.
jvanverthcf371bb2016-03-10 11:10:43 -0800416
417 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700418#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400419 fragBuilder->codeAppendf("half st_grad_len = length(dFdx(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700420#else
421 // 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 -0400422 fragBuilder->codeAppendf("half st_grad_len = length(dFdy(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700423#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800424 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverthfa38a302014-10-06 05:59:05 -0700425 } else {
jvanverth354eba52015-03-16 11:32:49 -0700426 // For general transforms, to determine the amount of correction we multiply a unit
427 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
428 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400429 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700430 // the length of the gradient may be 0, so we need to check for this
431 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400432 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800433 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400434 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800435 fragBuilder->codeAppend("} else {");
436 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
437 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700438
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400439 fragBuilder->codeAppendf("half2 Jdx = dFdx(%s);", st.fsIn());
440 fragBuilder->codeAppendf("half2 Jdy = dFdy(%s);", st.fsIn());
441 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
442 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
jvanverthfa38a302014-10-06 05:59:05 -0700443
444 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800445 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
jvanverthfa38a302014-10-06 05:59:05 -0700446 }
brianosman0e3c5542016-04-13 13:56:21 -0700447 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
448 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
449 // mapped linearly to coverage, so use a linear step:
450 if (isGammaCorrect) {
451 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400452 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0e3c5542016-04-13 13:56:21 -0700453 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400454 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0e3c5542016-04-13 13:56:21 -0700455 }
jvanverthfa38a302014-10-06 05:59:05 -0700456
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400457 fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
jvanverthfa38a302014-10-06 05:59:05 -0700458 }
459
bsalomona624bf32016-09-20 09:12:47 -0700460 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
461 FPCoordTransformIter&& transformIter) override {
joshualitt9b989322014-12-15 14:16:27 -0800462
joshualitte578a952015-05-14 10:09:13 -0700463 const GrDistanceFieldPathGeoProc& dfpgp = proc.cast<GrDistanceFieldPathGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700464
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400465 if (dfpgp.matrix().hasPerspective() && !fMatrix.cheapEqualTo(dfpgp.matrix())) {
466 fMatrix = dfpgp.matrix();
467 float matrix[3 * 3];
468 GrGLSLGetMatrix<3>(matrix, fMatrix);
469 pdman.setMatrix3f(fMatrixUniform, matrix);
joshualitt5559ca22015-05-21 15:50:36 -0700470 }
Robert Phillips8296e752017-08-25 08:45:21 -0400471
Brian Salomon7eae3e02018-08-07 14:02:38 +0000472 const SkISize& atlasSize = dfpgp.atlasSize();
473 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
474 if (fAtlasSize != atlasSize) {
475 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
476 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400477 }
478
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400479 if (dfpgp.matrix().hasPerspective()) {
480 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
481 } else {
482 this->setTransformDataHelper(dfpgp.matrix(), pdman, &transformIter);
483 }
jvanverthfa38a302014-10-06 05:59:05 -0700484 }
485
robertphillips46d36f02015-01-18 08:14:14 -0800486 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500487 const GrShaderCaps&,
jvanverthfa38a302014-10-06 05:59:05 -0700488 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700489 const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700490
joshualitt8fc6c2d2014-12-22 15:27:05 -0800491 uint32_t key = dfTexEffect.getFlags();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400492 key |= ComputePosKey(dfTexEffect.matrix()) << 16;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800493 b->add32(key);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400494 b->add32(dfTexEffect.matrix().hasPerspective());
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400495 b->add32(dfTexEffect.numTextureSamplers());
jvanverthfa38a302014-10-06 05:59:05 -0700496 }
497
498private:
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400499 SkMatrix fMatrix; // view matrix if perspective, local matrix otherwise
500 UniformHandle fMatrixUniform;
Robert Phillips8296e752017-08-25 08:45:21 -0400501
502 SkISize fAtlasSize;
503 UniformHandle fAtlasSizeInvUniform;
jvanverthfa38a302014-10-06 05:59:05 -0700504
egdaniele659a582015-11-13 09:55:43 -0800505 typedef GrGLSLGeometryProcessor INHERITED;
jvanverthfa38a302014-10-06 05:59:05 -0700506};
507
508///////////////////////////////////////////////////////////////////////////////
Brian Salomon92be2f72018-06-19 14:33:47 -0400509
Brian Osman4a3f5c82018-09-18 16:16:38 -0400510GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(const GrShaderCaps& caps,
511 const SkMatrix& matrix,
Brian Osmanc906d252018-12-04 11:17:46 -0500512 bool wideColor,
Brian Salomon92be2f72018-06-19 14:33:47 -0400513 const sk_sp<GrTextureProxy>* proxies,
514 int numProxies,
515 const GrSamplerState& params,
516 uint32_t flags)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400517 : INHERITED(kGrDistanceFieldPathGeoProc_ClassID)
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400518 , fMatrix(matrix)
Brian Salomon92be2f72018-06-19 14:33:47 -0400519 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500520 SkASSERT(numProxies <= kMaxTextures);
Mike Klein5045e502018-06-19 01:40:57 +0000521 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400522
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500523 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Osmanc906d252018-12-04 11:17:46 -0500524 fInColor = MakeColorAttribute("inColor", wideColor);
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000525 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
526 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500527 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000528
529 if (numProxies) {
530 fAtlasSize = proxies[0]->isize();
531 }
532
Robert Phillips4bc70112018-03-01 10:24:02 -0500533 for (int i = 0; i < numProxies; ++i) {
534 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000535 SkASSERT(proxies[i]->isize() == fAtlasSize);
536 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400537 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400538 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500539}
540
Robert Phillips4bc70112018-03-01 10:24:02 -0500541void GrDistanceFieldPathGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
542 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400543 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500544 SkASSERT(numProxies <= kMaxTextures);
545
Brian Salomon7eae3e02018-08-07 14:02:38 +0000546 if (!fTextureSamplers[0].isInitialized()) {
547 fAtlasSize = proxies[0]->isize();
548 }
549
Robert Phillips4bc70112018-03-01 10:24:02 -0500550 for (int i = 0; i < numProxies; ++i) {
551 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000552 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500553
554 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000555 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400556 }
557 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400558 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400559}
560
Brian Salomon94efbf52016-11-29 13:43:05 -0500561void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800562 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700563 GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800564}
565
Brian Salomon94efbf52016-11-29 13:43:05 -0500566GrGLSLPrimitiveProcessor*
567GrDistanceFieldPathGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700568 return new GrGLDistanceFieldPathGeoProc();
jvanverthfa38a302014-10-06 05:59:05 -0700569}
570
571///////////////////////////////////////////////////////////////////////////////
572
jvanverth502286d2015-04-08 12:37:51 -0700573GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc);
jvanverthfa38a302014-10-06 05:59:05 -0700574
Hal Canary6f6961e2017-01-31 13:50:44 -0500575#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700576sk_sp<GrGeometryProcessor> GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700577 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
578 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400579 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
580 d->textureProxy(texIdx),
581 nullptr,
582 nullptr,
583 nullptr
584 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500585
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400586 GrSamplerState::WrapMode wrapModes[2];
587 GrTest::TestWrapModes(d->fRandom, wrapModes);
588 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
589 ? GrSamplerState::Filter::kBilerp
590 : GrSamplerState::Filter::kNearest);
jvanverthfa38a302014-10-06 05:59:05 -0700591
jvanverthcf371bb2016-03-10 11:10:43 -0800592 uint32_t flags = 0;
593 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
594 if (flags & kSimilarity_DistanceFieldEffectFlag) {
595 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
596 }
597
Brian Osman4a3f5c82018-09-18 16:16:38 -0400598 return GrDistanceFieldPathGeoProc::Make(*d->caps()->shaderCaps(),
599 GrTest::TestMatrix(d->fRandom),
Brian Osmanc906d252018-12-04 11:17:46 -0500600 d->fRandom->nextBool(),
Robert Phillips4bc70112018-03-01 10:24:02 -0500601 proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400602 samplerState,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400603 flags);
jvanverthfa38a302014-10-06 05:59:05 -0700604}
Hal Canary6f6961e2017-01-31 13:50:44 -0500605#endif
jvanverthfa38a302014-10-06 05:59:05 -0700606
607///////////////////////////////////////////////////////////////////////////////
608
egdaniele659a582015-11-13 09:55:43 -0800609class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000610public:
Brian Salomon5c6ac642017-12-19 11:09:32 -0500611 GrGLDistanceFieldLCDTextGeoProc() : fAtlasSize({0, 0}) {
jvanverth502286d2015-04-08 12:37:51 -0700612 fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
jvanverth21deace2015-04-01 12:43:48 -0700613 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000614
mtklein36352bf2015-03-25 18:17:31 -0700615 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700616 const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
617 args.fGP.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000618
egdaniel4ca2e602015-11-18 08:01:26 -0800619 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800620 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800621 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800622
623 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800624 varyingHandler->emitAttributes(dfTexEffect);
egdaniel4ca2e602015-11-18 08:01:26 -0800625
Robert Phillips8296e752017-08-25 08:45:21 -0400626 const char* atlasSizeInvName;
627 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400628 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400629 kHigh_GrSLPrecision,
630 "AtlasSizeInv",
631 &atlasSizeInvName);
632
Chris Dalton60283612018-02-14 13:38:14 -0700633 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittabb52a12015-01-13 15:02:10 -0800634
joshualitt9b989322014-12-15 14:16:27 -0800635 // setup pass through color
Brian Salomonbfd51832017-01-04 13:22:08 -0500636 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualitt9b989322014-12-15 14:16:27 -0800637
joshualittabb52a12015-01-13 15:02:10 -0800638 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -0400639 gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
joshualitt4973d9d2014-11-08 09:24:25 -0800640
joshualittabb52a12015-01-13 15:02:10 -0800641 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800642 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800643 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800644 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400645 dfTexEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500646 dfTexEffect.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -0700647 args.fFPCoordTransformHandler);
joshualittabb52a12015-01-13 15:02:10 -0800648
jvanverthbb4a1cf2015-04-07 09:06:00 -0700649 // set up varyings
Chris Dalton27372882017-12-08 13:34:21 -0700650 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verth07110942018-11-20 14:46:41 -0500651 GrGLSLVarying texIdx(kFloat_GrSLType);
Chris Dalton27372882017-12-08 13:34:21 -0700652 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -0400653 append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -0400654 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -0400655
Chris Dalton27372882017-12-08 13:34:21 -0700656 GrGLSLVarying delta(kFloat_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600657 varyingHandler->addVarying("Delta", &delta);
Robert Phillips8296e752017-08-25 08:45:21 -0400658 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) {
659 vertBuilder->codeAppendf("%s = -%s.x/3.0;", delta.vsOut(), atlasSizeInvName);
660 } else {
661 vertBuilder->codeAppendf("%s = %s.x/3.0;", delta.vsOut(), atlasSizeInvName);
662 }
663
664 // add frag shader code
jvanverthcf371bb2016-03-10 11:10:43 -0800665 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
666 kUniformScale_DistanceFieldEffectMask;
667 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -0700668 bool isGammaCorrect =
669 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
joshualitt30ba4362014-08-21 20:18:45 -0700670
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000671 // create LCD offset adjusted by inverse of transform
jvanverthfdf7ccc2015-01-27 08:19:33 -0800672 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400673 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
joshualitt922c8b12015-08-07 09:55:23 -0700674
jvanverth78f07182014-07-30 06:17:59 -0700675 if (isUniformScale) {
jvanverthe499adf2016-07-20 12:22:14 -0700676#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400677 fragBuilder->codeAppendf("half st_grad_len = abs(dFdx(%s.x));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700678#else
679 // 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 -0400680 fragBuilder->codeAppendf("half st_grad_len = abs(dFdy(%s.y));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700681#endif
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400682 fragBuilder->codeAppendf("half2 offset = half2(st_grad_len*%s, 0.0);", delta.fsIn());
jvanverthcf371bb2016-03-10 11:10:43 -0800683 } else if (isSimilarity) {
684 // For a similarity matrix with rotation, the gradient will not be aligned
685 // with the texel coordinate axes, so we need to calculate it.
jvanverthe499adf2016-07-20 12:22:14 -0700686#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400687 fragBuilder->codeAppendf("half2 st_grad = dFdx(%s);", st.fsIn());
688 fragBuilder->codeAppendf("half2 offset = %s*st_grad;", delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700689#else
jvanverthcf371bb2016-03-10 11:10:43 -0800690 // We use dFdy because of a Mali 400 bug, and rotate -90 degrees to
691 // get the gradient in the x direction.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400692 fragBuilder->codeAppendf("half2 st_grad = dFdy(%s);", st.fsIn());
693 fragBuilder->codeAppendf("half2 offset = %s*half2(st_grad.y, -st_grad.x);",
Robert Phillips8296e752017-08-25 08:45:21 -0400694 delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700695#endif
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400696 fragBuilder->codeAppend("half st_grad_len = length(st_grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000697 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400698 fragBuilder->codeAppendf("half2 st = %s;\n", st.fsIn());
jvanverthbb4a1cf2015-04-07 09:06:00 -0700699
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400700 fragBuilder->codeAppend("half2 Jdx = dFdx(st);");
701 fragBuilder->codeAppend("half2 Jdy = dFdy(st);");
702 fragBuilder->codeAppendf("half2 offset = %s*Jdx;", delta.fsIn());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000703 }
704
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400705 // sample the texture by index
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400706 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400707 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
708 texIdx, "uv", "texColor");
Jim Van Verth6c251d22017-09-08 12:29:07 -0400709
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400710 // green is distance to uv center
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400711 fragBuilder->codeAppend("half3 distance;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400712 fragBuilder->codeAppend("distance.y = texColor.r;");
713 // red is distance to left offset
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400714 fragBuilder->codeAppend("half2 uv_adjusted = uv - offset;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400715 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
716 texIdx, "uv_adjusted", "texColor");
717 fragBuilder->codeAppend("distance.x = texColor.r;");
718 // blue is distance to right offset
719 fragBuilder->codeAppend("uv_adjusted = uv + offset;");
720 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
721 texIdx, "uv_adjusted", "texColor");
722 fragBuilder->codeAppend("distance.z = texColor.r;");
723
724 fragBuilder->codeAppend("distance = "
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400725 "half3(" SK_DistanceFieldMultiplier ")*(distance - half3(" SK_DistanceFieldThreshold"));");
jvanverth2d2a68c2014-06-10 06:42:56 -0700726
jvanverth21deace2015-04-01 12:43:48 -0700727 // adjust width based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -0700728 const char* distanceAdjustUniName = nullptr;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400729 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -0800730 "DistanceAdjust", &distanceAdjustUniName);
egdaniel4ca2e602015-11-18 08:01:26 -0800731 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700732
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000733 // To be strictly correct, we should compute the anti-aliasing factor separately
734 // for each color component. However, this is only important when using perspective
735 // transformations, and even then using a single factor seems like a reasonable
736 // trade-off between quality and speed.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400737 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -0800738 if (isSimilarity) {
halcanary9d524f22016-03-29 09:03:52 -0700739 // For similarity transform (uniform scale-only is a subset of this), we adjust for the
740 // effect of the transformation on the distance by using the length of the gradient of
jvanverthcf371bb2016-03-10 11:10:43 -0800741 // the texture coordinates. We use st coordinates to ensure we're mapping 1:1 from texel
742 // space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700743
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000744 // this gives us a smooth step across approximately one fragment
jvanverthcf371bb2016-03-10 11:10:43 -0800745 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*st_grad_len;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000746 } else {
jvanverth354eba52015-03-16 11:32:49 -0700747 // For general transforms, to determine the amount of correction we multiply a unit
748 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
749 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400750 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance.r), dFdy(distance.r));");
jvanverthd68a5502015-03-16 12:58:43 -0700751 // the length of the gradient may be 0, so we need to check for this
752 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400753 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800754 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400755 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800756 fragBuilder->codeAppend("} else {");
757 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
758 fragBuilder->codeAppend("}");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400759 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800760 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000761
762 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800763 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000764 }
765
brianosman0586f5c2016-04-12 12:48:21 -0700766 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
767 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
768 // mapped linearly to coverage, so use a linear step:
brianosmanb461d342016-04-13 13:10:14 -0700769 if (isGammaCorrect) {
Greg Daniel55923822017-05-22 16:34:34 -0400770 fragBuilder->codeAppendf("%s = "
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400771 "half4(saturate((distance + half3(afwidth)) / half3(2.0 * afwidth)), 1.0);",
Greg Daniel55923822017-05-22 16:34:34 -0400772 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700773 } else {
Greg Daniel55923822017-05-22 16:34:34 -0400774 fragBuilder->codeAppendf(
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400775 "%s = half4(smoothstep(half3(-afwidth), half3(afwidth), distance), 1.0);",
Greg Daniel55923822017-05-22 16:34:34 -0400776 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700777 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000778 }
779
bsalomona624bf32016-09-20 09:12:47 -0700780 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& processor,
781 FPCoordTransformIter&& transformIter) override {
jvanverth21deace2015-04-01 12:43:48 -0700782 SkASSERT(fDistanceAdjustUni.isValid());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000783
joshualitt5559ca22015-05-21 15:50:36 -0700784 const GrDistanceFieldLCDTextGeoProc& dflcd = processor.cast<GrDistanceFieldLCDTextGeoProc>();
785 GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700786 if (wa != fDistanceAdjust) {
787 pdman.set3f(fDistanceAdjustUni,
788 wa.fR,
789 wa.fG,
790 wa.fB);
791 fDistanceAdjust = wa;
jvanverth2d2a68c2014-06-10 06:42:56 -0700792 }
joshualitt9b989322014-12-15 14:16:27 -0800793
Brian Salomon7eae3e02018-08-07 14:02:38 +0000794 const SkISize& atlasSize = dflcd.atlasSize();
795 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
796 if (fAtlasSize != atlasSize) {
797 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
798 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400799 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500800 this->setTransformDataHelper(dflcd.localMatrix(), pdman, &transformIter);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000801 }
802
robertphillips46d36f02015-01-18 08:14:14 -0800803 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500804 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700805 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700806 const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000807
joshualitt8fc6c2d2014-12-22 15:27:05 -0800808 uint32_t key = dfTexEffect.getFlags();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800809 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400810 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000811 }
812
813private:
jvanverth502286d2015-04-08 12:37:51 -0700814 GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400815 UniformHandle fDistanceAdjustUni;
816
817 SkISize fAtlasSize;
818 UniformHandle fAtlasSizeInvUniform;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000819
egdaniele659a582015-11-13 09:55:43 -0800820 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000821};
822
823///////////////////////////////////////////////////////////////////////////////
Mike Klein5045e502018-06-19 01:40:57 +0000824
Brian Osman4a3f5c82018-09-18 16:16:38 -0400825GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(const GrShaderCaps& caps,
826 const sk_sp<GrTextureProxy>* proxies,
Brian Salomon92be2f72018-06-19 14:33:47 -0400827 int numProxies,
828 const GrSamplerState& params,
829 DistanceAdjust distanceAdjust,
830 uint32_t flags,
831 const SkMatrix& localMatrix)
832 : INHERITED(kGrDistanceFieldLCDTextGeoProc_ClassID)
833 , fLocalMatrix(localMatrix)
834 , fDistanceAdjust(distanceAdjust)
835 , fFlags(flags & kLCD_DistanceFieldEffectMask) {
836 SkASSERT(numProxies <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500837 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag));
Brian Salomon92be2f72018-06-19 14:33:47 -0400838
Brian Salomon5c6ac642017-12-19 11:09:32 -0500839 if (fFlags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400840 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500841 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400842 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500843 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500844 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000845 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
846 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500847 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400848
Brian Salomon7eae3e02018-08-07 14:02:38 +0000849 if (numProxies) {
850 fAtlasSize = proxies[0]->isize();
851 }
852
Robert Phillips4bc70112018-03-01 10:24:02 -0500853 for (int i = 0; i < numProxies; ++i) {
854 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000855 SkASSERT(proxies[i]->isize() == fAtlasSize);
856 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400857 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400858 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500859}
860
Robert Phillips4bc70112018-03-01 10:24:02 -0500861void GrDistanceFieldLCDTextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
862 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400863 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500864 SkASSERT(numProxies <= kMaxTextures);
865
Brian Salomon7eae3e02018-08-07 14:02:38 +0000866 if (!fTextureSamplers[0].isInitialized()) {
867 fAtlasSize = proxies[0]->isize();
868 }
869
Robert Phillips4bc70112018-03-01 10:24:02 -0500870 for (int i = 0; i < numProxies; ++i) {
871 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000872 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500873
874 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000875 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400876 }
877 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400878 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400879}
880
Brian Salomon94efbf52016-11-29 13:43:05 -0500881void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800882 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700883 GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800884}
885
Brian Salomon94efbf52016-11-29 13:43:05 -0500886GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700887 return new GrGLDistanceFieldLCDTextGeoProc();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000888}
889
890///////////////////////////////////////////////////////////////////////////////
891
jvanverth502286d2015-04-08 12:37:51 -0700892GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000893
Hal Canary6f6961e2017-01-31 13:50:44 -0500894#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700895sk_sp<GrGeometryProcessor> GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700896 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
897 GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400898 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
899 d->textureProxy(texIdx),
900 nullptr,
901 nullptr,
902 nullptr
903 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500904
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400905 GrSamplerState::WrapMode wrapModes[2];
906 GrTest::TestWrapModes(d->fRandom, wrapModes);
907 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
908 ? GrSamplerState::Filter::kBilerp
909 : GrSamplerState::Filter::kNearest);
jvanverth21deace2015-04-01 12:43:48 -0700910 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
jvanverth78f07182014-07-30 06:17:59 -0700911 uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
jvanverthcf371bb2016-03-10 11:10:43 -0800912 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
913 if (flags & kSimilarity_DistanceFieldEffectFlag) {
914 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
915 }
joshualitt0067ff52015-07-08 14:26:19 -0700916 flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500917 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Brian Osman4a3f5c82018-09-18 16:16:38 -0400918 return GrDistanceFieldLCDTextGeoProc::Make(*d->caps()->shaderCaps(), proxies, 1, samplerState,
919 wa, flags, localMatrix);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000920}
Hal Canary6f6961e2017-01-31 13:50:44 -0500921#endif