blob: 57afe95daeb6cc0f182f3b1d40db9b28f21856c0 [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
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500106 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
107 "*half(dFdx(%s.x)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700108#else
109 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500110 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
111 "*half(dFdy(%s.y)));", 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 Nicholas91c1d082019-02-05 15:23:07 -0500121 fragBuilder->codeAppendf("half st_grad_len = length(half2(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 Nicholas91c1d082019-02-05 15:23:07 -0500124 fragBuilder->codeAppendf("half st_grad_len = length(half2(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 Nicholas91c1d082019-02-05 15:23:07 -0500131 fragBuilder->codeAppend("half2 dist_grad = half2(float2(dFdx(distance), "
132 "dFdy(distance)));");
jvanverthd68a5502015-03-16 12:58:43 -0700133 // the length of the gradient may be 0, so we need to check for this
134 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400135 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800136 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400137 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800138 fragBuilder->codeAppend("} else {");
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500139 fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
egdaniel4ca2e602015-11-18 08:01:26 -0800140 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700141
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500142 fragBuilder->codeAppendf("half2 Jdx = half2(dFdx(%s));", st.fsIn());
143 fragBuilder->codeAppendf("half2 Jdy = half2(dFdy(%s));", st.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400144 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800145 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000146
147 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800148 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000149 }
brianosman0586f5c2016-04-12 12:48:21 -0700150
Jim Van Verth90e89b32017-07-06 16:36:55 -0400151 if (isAliased) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400152 fragBuilder->codeAppend("half val = distance > 0 ? 1.0 : 0.0;");
Jim Van Verth90e89b32017-07-06 16:36:55 -0400153 } else if (isGammaCorrect) {
154 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
155 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want
156 // distance mapped linearly to coverage, so use a linear step:
brianosman0586f5c2016-04-12 12:48:21 -0700157 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400158 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0586f5c2016-04-12 12:48:21 -0700159 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400160 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0586f5c2016-04-12 12:48:21 -0700161 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000162
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400163 fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000164 }
165
bsalomona624bf32016-09-20 09:12:47 -0700166 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
167 FPCoordTransformIter&& transformIter) override {
Robert Phillips8296e752017-08-25 08:45:21 -0400168 const GrDistanceFieldA8TextGeoProc& dfa8gp = proc.cast<GrDistanceFieldA8TextGeoProc>();
169
jvanverth2d2a68c2014-06-10 06:42:56 -0700170#ifdef SK_GAMMA_APPLY_TO_A8
Robert Phillips8296e752017-08-25 08:45:21 -0400171 float distanceAdjust = dfa8gp.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700172 if (distanceAdjust != fDistanceAdjust) {
Robert Phillipse2538312017-08-24 17:47:23 +0000173 fDistanceAdjust = distanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400174 pdman.set1f(fDistanceAdjustUni, distanceAdjust);
jvanverth2d2a68c2014-06-10 06:42:56 -0700175 }
176#endif
joshualitt5559ca22015-05-21 15:50:36 -0700177
Brian Salomon7eae3e02018-08-07 14:02:38 +0000178 const SkISize& atlasSize = dfa8gp.atlasSize();
179 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
Robert Phillips8296e752017-08-25 08:45:21 -0400180
Brian Salomon7eae3e02018-08-07 14:02:38 +0000181 if (fAtlasSize != atlasSize) {
182 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
183 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400184 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500185 this->setTransformDataHelper(dfa8gp.localMatrix(), pdman, &transformIter);
commit-bot@chromium.org8fe2ee12014-03-26 18:03:05 +0000186 }
187
robertphillips46d36f02015-01-18 08:14:14 -0800188 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500189 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700190 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700191 const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800192 uint32_t key = dfTexEffect.getFlags();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800193 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400194 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000195 }
196
jvanverth@google.comd830d132013-11-11 20:54:09 +0000197private:
mtklein50282b42015-01-22 07:59:52 -0800198#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500199 float fDistanceAdjust = -1.f;
jvanverth21deace2015-04-01 12:43:48 -0700200 UniformHandle fDistanceAdjustUni;
mtklein50282b42015-01-22 07:59:52 -0800201#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500202 SkISize fAtlasSize = {0, 0};
Robert Phillips8296e752017-08-25 08:45:21 -0400203 UniformHandle fAtlasSizeInvUniform;
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000204
egdaniele659a582015-11-13 09:55:43 -0800205 typedef GrGLSLGeometryProcessor INHERITED;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000206};
207
208///////////////////////////////////////////////////////////////////////////////
209
Brian Osman4a3f5c82018-09-18 16:16:38 -0400210GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(const GrShaderCaps& caps,
211 const sk_sp<GrTextureProxy>* proxies,
Brian Salomon92be2f72018-06-19 14:33:47 -0400212 int numProxies,
213 const GrSamplerState& params,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500214#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon92be2f72018-06-19 14:33:47 -0400215 float distanceAdjust,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500216#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400217 uint32_t flags,
218 const SkMatrix& localMatrix)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400219 : INHERITED(kGrDistanceFieldA8TextGeoProc_ClassID)
Brian Salomon92be2f72018-06-19 14:33:47 -0400220 , fLocalMatrix(localMatrix)
221 , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500222#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400223 , fDistanceAdjust(distanceAdjust)
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500224#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400225{
Robert Phillips4bc70112018-03-01 10:24:02 -0500226 SkASSERT(numProxies <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500227 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400228
Brian Salomon5c6ac642017-12-19 11:09:32 -0500229 if (flags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400230 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500231 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400232 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500233 }
Jim Van Verth1694a862018-12-17 19:48:42 +0000234 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType };
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000235 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
236 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500237 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400238
Brian Salomon7eae3e02018-08-07 14:02:38 +0000239 if (numProxies) {
240 fAtlasSize = proxies[0]->isize();
241 }
Robert Phillips4bc70112018-03-01 10:24:02 -0500242 for (int i = 0; i < numProxies; ++i) {
243 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000244 SkASSERT(proxies[i]->isize() == fAtlasSize);
245 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400246 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400247 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500248}
249
Robert Phillips4bc70112018-03-01 10:24:02 -0500250void GrDistanceFieldA8TextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
251 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400252 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500253 SkASSERT(numProxies <= kMaxTextures);
254
Brian Salomon7eae3e02018-08-07 14:02:38 +0000255 if (!fTextureSamplers[0].isInitialized()) {
256 fAtlasSize = proxies[0]->isize();
257 }
258
Robert Phillips4bc70112018-03-01 10:24:02 -0500259 for (int i = 0; i < numProxies; ++i) {
260 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000261 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500262 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000263 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400264 }
265 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400266 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400267}
268
Brian Salomon94efbf52016-11-29 13:43:05 -0500269void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800270 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700271 GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800272}
273
Brian Salomon94efbf52016-11-29 13:43:05 -0500274GrGLSLPrimitiveProcessor*
275GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700276 return new GrGLDistanceFieldA8TextGeoProc();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000277}
278
279///////////////////////////////////////////////////////////////////////////////
280
jvanverth502286d2015-04-08 12:37:51 -0700281GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000282
Hal Canary6f6961e2017-01-31 13:50:44 -0500283#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700284sk_sp<GrGeometryProcessor> GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) {
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500285 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
286 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400287 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
288 d->textureProxy(texIdx),
289 nullptr,
290 nullptr,
291 nullptr
292 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500293
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400294 GrSamplerState::WrapMode wrapModes[2];
295 GrTest::TestWrapModes(d->fRandom, wrapModes);
296 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
297 ? GrSamplerState::Filter::kBilerp
298 : GrSamplerState::Filter::kNearest);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000299
jvanverthcf371bb2016-03-10 11:10:43 -0800300 uint32_t flags = 0;
301 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
302 if (flags & kSimilarity_DistanceFieldEffectFlag) {
303 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
304 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500305 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Mike Reede1367b42017-12-15 14:28:56 -0500306#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500307 float lum = d->fRandom->nextF();
Mike Reede1367b42017-12-15 14:28:56 -0500308#endif
Brian Osman4a3f5c82018-09-18 16:16:38 -0400309 return GrDistanceFieldA8TextGeoProc::Make(*d->caps()->shaderCaps(),
310 proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400311 samplerState,
jvanverth2d2a68c2014-06-10 06:42:56 -0700312#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500313 lum,
jvanverth2d2a68c2014-06-10 06:42:56 -0700314#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500315 flags, localMatrix);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000316}
Hal Canary6f6961e2017-01-31 13:50:44 -0500317#endif
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000318
319///////////////////////////////////////////////////////////////////////////////
320
egdaniele659a582015-11-13 09:55:43 -0800321class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700322public:
joshualitt465283c2015-09-11 08:19:35 -0700323 GrGLDistanceFieldPathGeoProc()
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400324 : fMatrix(SkMatrix::InvalidMatrix())
Robert Phillips8296e752017-08-25 08:45:21 -0400325 , fAtlasSize({0,0}) {
326 }
jvanverthfa38a302014-10-06 05:59:05 -0700327
mtklein36352bf2015-03-25 18:17:31 -0700328 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
Brian Salomon5c6ac642017-12-19 11:09:32 -0500329 const GrDistanceFieldPathGeoProc& dfPathEffect =
330 args.fGP.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700331
Chris Dalton60283612018-02-14 13:38:14 -0700332 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
jvanverthfa38a302014-10-06 05:59:05 -0700333
egdaniel4ca2e602015-11-18 08:01:26 -0800334 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800335 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800336 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800337
338 // emit attributes
Brian Salomon5c6ac642017-12-19 11:09:32 -0500339 varyingHandler->emitAttributes(dfPathEffect);
joshualittabb52a12015-01-13 15:02:10 -0800340
Robert Phillips8296e752017-08-25 08:45:21 -0400341 const char* atlasSizeInvName;
342 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400343 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400344 kHigh_GrSLPrecision,
345 "AtlasSizeInv",
346 &atlasSizeInvName);
347
Chris Dalton27372882017-12-08 13:34:21 -0700348 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verth1694a862018-12-17 19:48:42 +0000349 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
350 GrGLSLVarying texIdx(texIdxType);
Chris Dalton27372882017-12-08 13:34:21 -0700351 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -0400352 append_index_uv_varyings(args, dfPathEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
353 &texIdx, &st);
jvanverthfa38a302014-10-06 05:59:05 -0700354
joshualitt9b989322014-12-15 14:16:27 -0800355 // setup pass through color
Brian Salomon5c6ac642017-12-19 11:09:32 -0500356 varyingHandler->addPassThroughAttribute(dfPathEffect.inColor(), args.fOutputColor);
reede4ef1ca2015-02-17 18:38:38 -0800357
Brian Salomon5c6ac642017-12-19 11:09:32 -0500358 if (dfPathEffect.matrix().hasPerspective()) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400359 // Setup position
360 this->writeOutputPosition(vertBuilder,
361 uniformHandler,
362 gpArgs,
Brian Salomon92be2f72018-06-19 14:33:47 -0400363 dfPathEffect.inPosition().name(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500364 dfPathEffect.matrix(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400365 &fMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -0800366
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400367 // emit transforms
368 this->emitTransforms(vertBuilder,
369 varyingHandler,
370 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400371 dfPathEffect.inPosition().asShaderVar(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400372 args.fFPCoordTransformHandler);
373 } else {
374 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -0400375 this->writeOutputPosition(vertBuilder, gpArgs, dfPathEffect.inPosition().name());
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400376
377 // emit transforms
378 this->emitTransforms(vertBuilder,
379 varyingHandler,
380 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400381 dfPathEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500382 dfPathEffect.matrix(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400383 args.fFPCoordTransformHandler);
384 }
joshualitt4973d9d2014-11-08 09:24:25 -0800385
jvanverthfdf7ccc2015-01-27 08:19:33 -0800386 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400387 fragBuilder->codeAppendf("float2 uv = %s;", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400388 fragBuilder->codeAppend("half4 texColor;");
Brian Salomon5c6ac642017-12-19 11:09:32 -0500389 append_multitexture_lookup(args, dfPathEffect.numTextureSamplers(), texIdx, "uv",
390 "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -0800391
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400392 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400393 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverthfa38a302014-10-06 05:59:05 -0700394
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400395 fragBuilder->codeAppend("half afwidth;");
Brian Salomon5c6ac642017-12-19 11:09:32 -0500396 bool isUniformScale = (dfPathEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
397 kUniformScale_DistanceFieldEffectMask;
398 bool isSimilarity = SkToBool(dfPathEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosman0e3c5542016-04-13 13:56:21 -0700399 bool isGammaCorrect =
Brian Salomon5c6ac642017-12-19 11:09:32 -0500400 SkToBool(dfPathEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
jvanverthcf371bb2016-03-10 11:10:43 -0800401 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700402 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700403 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800404 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700405
jvanverthfa38a302014-10-06 05:59:05 -0700406 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700407#ifdef SK_VULKAN
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500408 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
409 "*half(dFdx(%s.x)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700410#else
411 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500412 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
413 "*half(dFdy(%s.y)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700414#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800415 } else if (isSimilarity) {
416 // For similarity transform, we adjust the effect of the transformation on the distance
417 // by using the length of the gradient of the texture coordinates. We use st coordinates
418 // to ensure we're mapping 1:1 from texel space to pixel space.
jvanverthcf371bb2016-03-10 11:10:43 -0800419
420 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700421#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400422 fragBuilder->codeAppendf("half st_grad_len = length(dFdx(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700423#else
424 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400425 fragBuilder->codeAppendf("half st_grad_len = length(dFdy(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700426#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800427 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverthfa38a302014-10-06 05:59:05 -0700428 } else {
jvanverth354eba52015-03-16 11:32:49 -0700429 // For general transforms, to determine the amount of correction we multiply a unit
430 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
431 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500432 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), "
433 "dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700434 // the length of the gradient may be 0, so we need to check for this
435 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400436 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800437 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400438 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800439 fragBuilder->codeAppend("} else {");
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500440 fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
egdaniel4ca2e602015-11-18 08:01:26 -0800441 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700442
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500443 fragBuilder->codeAppendf("half2 Jdx = half2(dFdx(%s));", st.fsIn());
444 fragBuilder->codeAppendf("half2 Jdy = half2(dFdy(%s));", st.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400445 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
446 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
jvanverthfa38a302014-10-06 05:59:05 -0700447
448 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800449 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
jvanverthfa38a302014-10-06 05:59:05 -0700450 }
brianosman0e3c5542016-04-13 13:56:21 -0700451 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
452 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
453 // mapped linearly to coverage, so use a linear step:
454 if (isGammaCorrect) {
455 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400456 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0e3c5542016-04-13 13:56:21 -0700457 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400458 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0e3c5542016-04-13 13:56:21 -0700459 }
jvanverthfa38a302014-10-06 05:59:05 -0700460
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400461 fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
jvanverthfa38a302014-10-06 05:59:05 -0700462 }
463
bsalomona624bf32016-09-20 09:12:47 -0700464 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
465 FPCoordTransformIter&& transformIter) override {
joshualitt9b989322014-12-15 14:16:27 -0800466
joshualitte578a952015-05-14 10:09:13 -0700467 const GrDistanceFieldPathGeoProc& dfpgp = proc.cast<GrDistanceFieldPathGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700468
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400469 if (dfpgp.matrix().hasPerspective() && !fMatrix.cheapEqualTo(dfpgp.matrix())) {
470 fMatrix = dfpgp.matrix();
471 float matrix[3 * 3];
472 GrGLSLGetMatrix<3>(matrix, fMatrix);
473 pdman.setMatrix3f(fMatrixUniform, matrix);
joshualitt5559ca22015-05-21 15:50:36 -0700474 }
Robert Phillips8296e752017-08-25 08:45:21 -0400475
Brian Salomon7eae3e02018-08-07 14:02:38 +0000476 const SkISize& atlasSize = dfpgp.atlasSize();
477 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
478 if (fAtlasSize != atlasSize) {
479 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
480 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400481 }
482
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400483 if (dfpgp.matrix().hasPerspective()) {
484 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
485 } else {
486 this->setTransformDataHelper(dfpgp.matrix(), pdman, &transformIter);
487 }
jvanverthfa38a302014-10-06 05:59:05 -0700488 }
489
robertphillips46d36f02015-01-18 08:14:14 -0800490 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500491 const GrShaderCaps&,
jvanverthfa38a302014-10-06 05:59:05 -0700492 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700493 const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700494
joshualitt8fc6c2d2014-12-22 15:27:05 -0800495 uint32_t key = dfTexEffect.getFlags();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400496 key |= ComputePosKey(dfTexEffect.matrix()) << 16;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800497 b->add32(key);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400498 b->add32(dfTexEffect.matrix().hasPerspective());
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400499 b->add32(dfTexEffect.numTextureSamplers());
jvanverthfa38a302014-10-06 05:59:05 -0700500 }
501
502private:
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400503 SkMatrix fMatrix; // view matrix if perspective, local matrix otherwise
504 UniformHandle fMatrixUniform;
Robert Phillips8296e752017-08-25 08:45:21 -0400505
506 SkISize fAtlasSize;
507 UniformHandle fAtlasSizeInvUniform;
jvanverthfa38a302014-10-06 05:59:05 -0700508
egdaniele659a582015-11-13 09:55:43 -0800509 typedef GrGLSLGeometryProcessor INHERITED;
jvanverthfa38a302014-10-06 05:59:05 -0700510};
511
512///////////////////////////////////////////////////////////////////////////////
Brian Salomon92be2f72018-06-19 14:33:47 -0400513
Brian Osman4a3f5c82018-09-18 16:16:38 -0400514GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(const GrShaderCaps& caps,
515 const SkMatrix& matrix,
Brian Osmanc906d252018-12-04 11:17:46 -0500516 bool wideColor,
Brian Salomon92be2f72018-06-19 14:33:47 -0400517 const sk_sp<GrTextureProxy>* proxies,
518 int numProxies,
519 const GrSamplerState& params,
520 uint32_t flags)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400521 : INHERITED(kGrDistanceFieldPathGeoProc_ClassID)
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400522 , fMatrix(matrix)
Brian Salomon92be2f72018-06-19 14:33:47 -0400523 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500524 SkASSERT(numProxies <= kMaxTextures);
Mike Klein5045e502018-06-19 01:40:57 +0000525 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400526
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500527 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Osmanc906d252018-12-04 11:17:46 -0500528 fInColor = MakeColorAttribute("inColor", wideColor);
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000529 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
530 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500531 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000532
533 if (numProxies) {
534 fAtlasSize = proxies[0]->isize();
535 }
536
Robert Phillips4bc70112018-03-01 10:24:02 -0500537 for (int i = 0; i < numProxies; ++i) {
538 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000539 SkASSERT(proxies[i]->isize() == fAtlasSize);
540 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400541 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400542 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500543}
544
Robert Phillips4bc70112018-03-01 10:24:02 -0500545void GrDistanceFieldPathGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
546 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400547 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500548 SkASSERT(numProxies <= kMaxTextures);
549
Brian Salomon7eae3e02018-08-07 14:02:38 +0000550 if (!fTextureSamplers[0].isInitialized()) {
551 fAtlasSize = proxies[0]->isize();
552 }
553
Robert Phillips4bc70112018-03-01 10:24:02 -0500554 for (int i = 0; i < numProxies; ++i) {
555 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000556 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500557
558 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000559 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400560 }
561 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400562 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400563}
564
Brian Salomon94efbf52016-11-29 13:43:05 -0500565void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800566 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700567 GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800568}
569
Brian Salomon94efbf52016-11-29 13:43:05 -0500570GrGLSLPrimitiveProcessor*
571GrDistanceFieldPathGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700572 return new GrGLDistanceFieldPathGeoProc();
jvanverthfa38a302014-10-06 05:59:05 -0700573}
574
575///////////////////////////////////////////////////////////////////////////////
576
jvanverth502286d2015-04-08 12:37:51 -0700577GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc);
jvanverthfa38a302014-10-06 05:59:05 -0700578
Hal Canary6f6961e2017-01-31 13:50:44 -0500579#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700580sk_sp<GrGeometryProcessor> GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700581 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
582 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400583 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
584 d->textureProxy(texIdx),
585 nullptr,
586 nullptr,
587 nullptr
588 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500589
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400590 GrSamplerState::WrapMode wrapModes[2];
591 GrTest::TestWrapModes(d->fRandom, wrapModes);
592 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
593 ? GrSamplerState::Filter::kBilerp
594 : GrSamplerState::Filter::kNearest);
jvanverthfa38a302014-10-06 05:59:05 -0700595
jvanverthcf371bb2016-03-10 11:10:43 -0800596 uint32_t flags = 0;
597 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
598 if (flags & kSimilarity_DistanceFieldEffectFlag) {
599 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
600 }
601
Brian Osman4a3f5c82018-09-18 16:16:38 -0400602 return GrDistanceFieldPathGeoProc::Make(*d->caps()->shaderCaps(),
603 GrTest::TestMatrix(d->fRandom),
Brian Osmanc906d252018-12-04 11:17:46 -0500604 d->fRandom->nextBool(),
Robert Phillips4bc70112018-03-01 10:24:02 -0500605 proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400606 samplerState,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400607 flags);
jvanverthfa38a302014-10-06 05:59:05 -0700608}
Hal Canary6f6961e2017-01-31 13:50:44 -0500609#endif
jvanverthfa38a302014-10-06 05:59:05 -0700610
611///////////////////////////////////////////////////////////////////////////////
612
egdaniele659a582015-11-13 09:55:43 -0800613class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000614public:
Brian Salomon5c6ac642017-12-19 11:09:32 -0500615 GrGLDistanceFieldLCDTextGeoProc() : fAtlasSize({0, 0}) {
jvanverth502286d2015-04-08 12:37:51 -0700616 fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
jvanverth21deace2015-04-01 12:43:48 -0700617 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000618
mtklein36352bf2015-03-25 18:17:31 -0700619 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700620 const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
621 args.fGP.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000622
egdaniel4ca2e602015-11-18 08:01:26 -0800623 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800624 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800625 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800626
627 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800628 varyingHandler->emitAttributes(dfTexEffect);
egdaniel4ca2e602015-11-18 08:01:26 -0800629
Robert Phillips8296e752017-08-25 08:45:21 -0400630 const char* atlasSizeInvName;
631 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400632 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400633 kHigh_GrSLPrecision,
634 "AtlasSizeInv",
635 &atlasSizeInvName);
636
Chris Dalton60283612018-02-14 13:38:14 -0700637 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittabb52a12015-01-13 15:02:10 -0800638
joshualitt9b989322014-12-15 14:16:27 -0800639 // setup pass through color
Brian Salomonbfd51832017-01-04 13:22:08 -0500640 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualitt9b989322014-12-15 14:16:27 -0800641
joshualittabb52a12015-01-13 15:02:10 -0800642 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -0400643 gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
joshualitt4973d9d2014-11-08 09:24:25 -0800644
joshualittabb52a12015-01-13 15:02:10 -0800645 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800646 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800647 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800648 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400649 dfTexEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500650 dfTexEffect.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -0700651 args.fFPCoordTransformHandler);
joshualittabb52a12015-01-13 15:02:10 -0800652
jvanverthbb4a1cf2015-04-07 09:06:00 -0700653 // set up varyings
Chris Dalton27372882017-12-08 13:34:21 -0700654 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verth1694a862018-12-17 19:48:42 +0000655 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
656 GrGLSLVarying texIdx(texIdxType);
Chris Dalton27372882017-12-08 13:34:21 -0700657 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -0400658 append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -0400659 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -0400660
Chris Dalton27372882017-12-08 13:34:21 -0700661 GrGLSLVarying delta(kFloat_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600662 varyingHandler->addVarying("Delta", &delta);
Robert Phillips8296e752017-08-25 08:45:21 -0400663 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) {
664 vertBuilder->codeAppendf("%s = -%s.x/3.0;", delta.vsOut(), atlasSizeInvName);
665 } else {
666 vertBuilder->codeAppendf("%s = %s.x/3.0;", delta.vsOut(), atlasSizeInvName);
667 }
668
669 // add frag shader code
jvanverthcf371bb2016-03-10 11:10:43 -0800670 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
671 kUniformScale_DistanceFieldEffectMask;
672 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -0700673 bool isGammaCorrect =
674 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
joshualitt30ba4362014-08-21 20:18:45 -0700675
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000676 // create LCD offset adjusted by inverse of transform
jvanverthfdf7ccc2015-01-27 08:19:33 -0800677 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400678 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
joshualitt922c8b12015-08-07 09:55:23 -0700679
jvanverth78f07182014-07-30 06:17:59 -0700680 if (isUniformScale) {
jvanverthe499adf2016-07-20 12:22:14 -0700681#ifdef SK_VULKAN
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500682 fragBuilder->codeAppendf("half st_grad_len = half(abs(dFdx(%s.x)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700683#else
684 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500685 fragBuilder->codeAppendf("half st_grad_len = half(abs(dFdy(%s.y)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700686#endif
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500687 fragBuilder->codeAppendf("half2 offset = half2(half(st_grad_len*%s), 0.0);",
688 delta.fsIn());
jvanverthcf371bb2016-03-10 11:10:43 -0800689 } else if (isSimilarity) {
690 // For a similarity matrix with rotation, the gradient will not be aligned
691 // with the texel coordinate axes, so we need to calculate it.
jvanverthe499adf2016-07-20 12:22:14 -0700692#ifdef SK_VULKAN
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500693 fragBuilder->codeAppendf("half2 st_grad = half2(dFdx(%s));", st.fsIn());
694 fragBuilder->codeAppendf("half2 offset = half(%s)*st_grad;", delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700695#else
jvanverthcf371bb2016-03-10 11:10:43 -0800696 // We use dFdy because of a Mali 400 bug, and rotate -90 degrees to
697 // get the gradient in the x direction.
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500698 fragBuilder->codeAppendf("half2 st_grad = half2(dFdy(%s));", st.fsIn());
699 fragBuilder->codeAppendf("half2 offset = half2(%s*float2(st_grad.y, -st_grad.x));",
Robert Phillips8296e752017-08-25 08:45:21 -0400700 delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700701#endif
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400702 fragBuilder->codeAppend("half st_grad_len = length(st_grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000703 } else {
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500704 fragBuilder->codeAppendf("half2 st = half2(%s);\n", st.fsIn());
jvanverthbb4a1cf2015-04-07 09:06:00 -0700705
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500706 fragBuilder->codeAppend("half2 Jdx = half2(dFdx(st));");
707 fragBuilder->codeAppend("half2 Jdy = half2(dFdy(st));");
708 fragBuilder->codeAppendf("half2 offset = half2(half(%s))*Jdx;", delta.fsIn());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000709 }
710
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400711 // sample the texture by index
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400712 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400713 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
714 texIdx, "uv", "texColor");
Jim Van Verth6c251d22017-09-08 12:29:07 -0400715
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400716 // green is distance to uv center
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400717 fragBuilder->codeAppend("half3 distance;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400718 fragBuilder->codeAppend("distance.y = texColor.r;");
719 // red is distance to left offset
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500720 fragBuilder->codeAppend("half2 uv_adjusted = half2(uv) - offset;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400721 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
722 texIdx, "uv_adjusted", "texColor");
723 fragBuilder->codeAppend("distance.x = texColor.r;");
724 // blue is distance to right offset
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500725 fragBuilder->codeAppend("uv_adjusted = half2(uv) + offset;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400726 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
727 texIdx, "uv_adjusted", "texColor");
728 fragBuilder->codeAppend("distance.z = texColor.r;");
729
730 fragBuilder->codeAppend("distance = "
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400731 "half3(" SK_DistanceFieldMultiplier ")*(distance - half3(" SK_DistanceFieldThreshold"));");
jvanverth2d2a68c2014-06-10 06:42:56 -0700732
jvanverth21deace2015-04-01 12:43:48 -0700733 // adjust width based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -0700734 const char* distanceAdjustUniName = nullptr;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400735 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -0800736 "DistanceAdjust", &distanceAdjustUniName);
egdaniel4ca2e602015-11-18 08:01:26 -0800737 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700738
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000739 // To be strictly correct, we should compute the anti-aliasing factor separately
740 // for each color component. However, this is only important when using perspective
741 // transformations, and even then using a single factor seems like a reasonable
742 // trade-off between quality and speed.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400743 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -0800744 if (isSimilarity) {
halcanary9d524f22016-03-29 09:03:52 -0700745 // For similarity transform (uniform scale-only is a subset of this), we adjust for the
746 // effect of the transformation on the distance by using the length of the gradient of
jvanverthcf371bb2016-03-10 11:10:43 -0800747 // the texture coordinates. We use st coordinates to ensure we're mapping 1:1 from texel
748 // space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700749
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000750 // this gives us a smooth step across approximately one fragment
jvanverthcf371bb2016-03-10 11:10:43 -0800751 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*st_grad_len;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000752 } else {
jvanverth354eba52015-03-16 11:32:49 -0700753 // For general transforms, to determine the amount of correction we multiply a unit
754 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
755 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500756 fragBuilder->codeAppend("half2 dist_grad = half2(half(dFdx(distance.r)), "
757 "half(dFdy(distance.r)));");
jvanverthd68a5502015-03-16 12:58:43 -0700758 // the length of the gradient may be 0, so we need to check for this
759 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400760 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800761 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400762 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800763 fragBuilder->codeAppend("} else {");
Ethan Nicholas91c1d082019-02-05 15:23:07 -0500764 fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
egdaniel4ca2e602015-11-18 08:01:26 -0800765 fragBuilder->codeAppend("}");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400766 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800767 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000768
769 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800770 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000771 }
772
brianosman0586f5c2016-04-12 12:48:21 -0700773 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
774 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
775 // mapped linearly to coverage, so use a linear step:
brianosmanb461d342016-04-13 13:10:14 -0700776 if (isGammaCorrect) {
Greg Daniel55923822017-05-22 16:34:34 -0400777 fragBuilder->codeAppendf("%s = "
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400778 "half4(saturate((distance + half3(afwidth)) / half3(2.0 * afwidth)), 1.0);",
Greg Daniel55923822017-05-22 16:34:34 -0400779 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700780 } else {
Greg Daniel55923822017-05-22 16:34:34 -0400781 fragBuilder->codeAppendf(
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400782 "%s = half4(smoothstep(half3(-afwidth), half3(afwidth), distance), 1.0);",
Greg Daniel55923822017-05-22 16:34:34 -0400783 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700784 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000785 }
786
bsalomona624bf32016-09-20 09:12:47 -0700787 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& processor,
788 FPCoordTransformIter&& transformIter) override {
jvanverth21deace2015-04-01 12:43:48 -0700789 SkASSERT(fDistanceAdjustUni.isValid());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000790
joshualitt5559ca22015-05-21 15:50:36 -0700791 const GrDistanceFieldLCDTextGeoProc& dflcd = processor.cast<GrDistanceFieldLCDTextGeoProc>();
792 GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700793 if (wa != fDistanceAdjust) {
794 pdman.set3f(fDistanceAdjustUni,
795 wa.fR,
796 wa.fG,
797 wa.fB);
798 fDistanceAdjust = wa;
jvanverth2d2a68c2014-06-10 06:42:56 -0700799 }
joshualitt9b989322014-12-15 14:16:27 -0800800
Brian Salomon7eae3e02018-08-07 14:02:38 +0000801 const SkISize& atlasSize = dflcd.atlasSize();
802 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
803 if (fAtlasSize != atlasSize) {
804 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
805 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400806 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500807 this->setTransformDataHelper(dflcd.localMatrix(), pdman, &transformIter);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000808 }
809
robertphillips46d36f02015-01-18 08:14:14 -0800810 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500811 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700812 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700813 const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000814
joshualitt8fc6c2d2014-12-22 15:27:05 -0800815 uint32_t key = dfTexEffect.getFlags();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800816 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400817 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000818 }
819
820private:
jvanverth502286d2015-04-08 12:37:51 -0700821 GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400822 UniformHandle fDistanceAdjustUni;
823
824 SkISize fAtlasSize;
825 UniformHandle fAtlasSizeInvUniform;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000826
egdaniele659a582015-11-13 09:55:43 -0800827 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000828};
829
830///////////////////////////////////////////////////////////////////////////////
Mike Klein5045e502018-06-19 01:40:57 +0000831
Brian Osman4a3f5c82018-09-18 16:16:38 -0400832GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(const GrShaderCaps& caps,
833 const sk_sp<GrTextureProxy>* proxies,
Brian Salomon92be2f72018-06-19 14:33:47 -0400834 int numProxies,
835 const GrSamplerState& params,
836 DistanceAdjust distanceAdjust,
837 uint32_t flags,
838 const SkMatrix& localMatrix)
839 : INHERITED(kGrDistanceFieldLCDTextGeoProc_ClassID)
840 , fLocalMatrix(localMatrix)
841 , fDistanceAdjust(distanceAdjust)
842 , fFlags(flags & kLCD_DistanceFieldEffectMask) {
843 SkASSERT(numProxies <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500844 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag));
Brian Salomon92be2f72018-06-19 14:33:47 -0400845
Brian Salomon5c6ac642017-12-19 11:09:32 -0500846 if (fFlags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400847 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500848 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400849 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500850 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500851 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000852 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
853 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500854 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400855
Brian Salomon7eae3e02018-08-07 14:02:38 +0000856 if (numProxies) {
857 fAtlasSize = proxies[0]->isize();
858 }
859
Robert Phillips4bc70112018-03-01 10:24:02 -0500860 for (int i = 0; i < numProxies; ++i) {
861 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000862 SkASSERT(proxies[i]->isize() == fAtlasSize);
863 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400864 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400865 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500866}
867
Robert Phillips4bc70112018-03-01 10:24:02 -0500868void GrDistanceFieldLCDTextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
869 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400870 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500871 SkASSERT(numProxies <= kMaxTextures);
872
Brian Salomon7eae3e02018-08-07 14:02:38 +0000873 if (!fTextureSamplers[0].isInitialized()) {
874 fAtlasSize = proxies[0]->isize();
875 }
876
Robert Phillips4bc70112018-03-01 10:24:02 -0500877 for (int i = 0; i < numProxies; ++i) {
878 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000879 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500880
881 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000882 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400883 }
884 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400885 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400886}
887
Brian Salomon94efbf52016-11-29 13:43:05 -0500888void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800889 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700890 GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800891}
892
Brian Salomon94efbf52016-11-29 13:43:05 -0500893GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700894 return new GrGLDistanceFieldLCDTextGeoProc();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000895}
896
897///////////////////////////////////////////////////////////////////////////////
898
jvanverth502286d2015-04-08 12:37:51 -0700899GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000900
Hal Canary6f6961e2017-01-31 13:50:44 -0500901#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700902sk_sp<GrGeometryProcessor> GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700903 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
904 GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400905 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
906 d->textureProxy(texIdx),
907 nullptr,
908 nullptr,
909 nullptr
910 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500911
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400912 GrSamplerState::WrapMode wrapModes[2];
913 GrTest::TestWrapModes(d->fRandom, wrapModes);
914 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
915 ? GrSamplerState::Filter::kBilerp
916 : GrSamplerState::Filter::kNearest);
jvanverth21deace2015-04-01 12:43:48 -0700917 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
jvanverth78f07182014-07-30 06:17:59 -0700918 uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
jvanverthcf371bb2016-03-10 11:10:43 -0800919 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
920 if (flags & kSimilarity_DistanceFieldEffectFlag) {
921 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
922 }
joshualitt0067ff52015-07-08 14:26:19 -0700923 flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500924 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Brian Osman4a3f5c82018-09-18 16:16:38 -0400925 return GrDistanceFieldLCDTextGeoProc::Make(*d->caps()->shaderCaps(), proxies, 1, samplerState,
926 wa, flags, localMatrix);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000927}
Hal Canary6f6961e2017-01-31 13:50:44 -0500928#endif