blob: 04f73c8ebd64939a17acd789afdc8a2d056e26b2 [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"
joshualitteb2a6762014-12-04 11:35:33 -080010#include "GrTexture.h"
11#include "SkDistanceFieldGen.h"
egdaniel2d721d32015-11-11 13:06:05 -080012#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniele659a582015-11-13 09:55:43 -080013#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070014#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080015#include "glsl/GrGLSLUniformHandler.h"
egdaniel64c47282015-11-13 06:54:19 -080016#include "glsl/GrGLSLUtil.h"
egdaniel0eafe792015-11-20 14:01:22 -080017#include "glsl/GrGLSLVarying.h"
Chris Daltonc17bf322017-10-24 10:59:03 -060018#include "glsl/GrGLSLVertexGeoBuilder.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000019
jvanverth21deace2015-04-01 12:43:48 -070020// Assuming a radius of a little less than the diagonal of the fragment
jvanverth24ba0082015-03-19 11:34:13 -070021#define SK_DistanceFieldAAFactor "0.65"
jvanverth2d2a68c2014-06-10 06:42:56 -070022
egdaniele659a582015-11-13 09:55:43 -080023class GrGLDistanceFieldA8TextGeoProc : public GrGLSLGeometryProcessor {
jvanverth@google.comd830d132013-11-11 20:54:09 +000024public:
Brian Salomon5c6ac642017-12-19 11:09:32 -050025 GrGLDistanceFieldA8TextGeoProc() = default;
jvanverth@google.comd830d132013-11-11 20:54:09 +000026
mtklein36352bf2015-03-25 18:17:31 -070027 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -070028 const GrDistanceFieldA8TextGeoProc& dfTexEffect =
29 args.fGP.cast<GrDistanceFieldA8TextGeoProc>();
Chris Dalton60283612018-02-14 13:38:14 -070030 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
jvanverth@google.comd830d132013-11-11 20:54:09 +000031
egdaniel4ca2e602015-11-18 08:01:26 -080032 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -080033 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -080034 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -080035
36 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -080037 varyingHandler->emitAttributes(dfTexEffect);
joshualittabb52a12015-01-13 15:02:10 -080038
Robert Phillips8296e752017-08-25 08:45:21 -040039 const char* atlasSizeInvName;
40 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040041 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -040042 kHigh_GrSLPrecision,
43 "AtlasSizeInv",
44 &atlasSizeInvName);
jvanverth21deace2015-04-01 12:43:48 -070045#ifdef SK_GAMMA_APPLY_TO_A8
46 // adjust based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -070047 const char* distanceAdjustUniName = nullptr;
jvanverth21deace2015-04-01 12:43:48 -070048 // width, height, 1/(3*width)
Ethan Nicholasf7b88202017-09-18 14:10:39 -040049 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -080050 "DistanceAdjust", &distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -070051#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000052
joshualitt9b989322014-12-15 14:16:27 -080053 // Setup pass through color
Brian Salomonbfd51832017-01-04 13:22:08 -050054 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000055
joshualittabb52a12015-01-13 15:02:10 -080056 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -040057 gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
joshualitt2dd1ae02014-12-03 06:24:10 -080058
joshualittabb52a12015-01-13 15:02:10 -080059 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -080060 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -080061 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -080062 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -040063 dfTexEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -050064 dfTexEffect.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -070065 args.fFPCoordTransformHandler);
joshualitt4973d9d2014-11-08 09:24:25 -080066
jvanverthbb4a1cf2015-04-07 09:06:00 -070067 // add varyings
Chris Dalton27372882017-12-08 13:34:21 -070068 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verthfc4f7682018-01-25 16:26:25 -050069 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
70 GrGLSLVarying texIdx(texIdxType);
Chris Dalton27372882017-12-08 13:34:21 -070071 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -040072 append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -040073 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -040074
jvanverthcf371bb2016-03-10 11:10:43 -080075 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
76 kUniformScale_DistanceFieldEffectMask;
jvanverthbb4a1cf2015-04-07 09:06:00 -070077 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -070078 bool isGammaCorrect =
79 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
Jim Van Verth90e89b32017-07-06 16:36:55 -040080 bool isAliased =
81 SkToBool(dfTexEffect.getFlags() & kAliased_DistanceFieldEffectFlag);
halcanary9d524f22016-03-29 09:03:52 -070082
jvanverthfdf7ccc2015-01-27 08:19:33 -080083 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -040084 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -040085 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040086 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
87 texIdx, "uv", "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -080088
Ethan Nicholasf7b88202017-09-18 14:10:39 -040089 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -040090 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverth21deace2015-04-01 12:43:48 -070091#ifdef SK_GAMMA_APPLY_TO_A8
92 // adjust width based on gamma
egdaniel4ca2e602015-11-18 08:01:26 -080093 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -070094#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000095
Ethan Nicholasf7b88202017-09-18 14:10:39 -040096 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -080097 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -070098 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -070099 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800100 // 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 +0000101
jvanverth354eba52015-03-16 11:32:49 -0700102 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700103#ifdef SK_VULKAN
104 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));",
105 st.fsIn());
106#else
107 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
egdaniel4ca2e602015-11-18 08:01:26 -0800108 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));",
109 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700110#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800111 } else if (isSimilarity) {
112 // For similarity transform, we adjust the effect of the transformation on the distance
113 // by using the length of the gradient of the texture coordinates. We use st coordinates
114 // to ensure we're mapping 1:1 from texel space to pixel space.
115 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
116
117 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700118#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400119 fragBuilder->codeAppendf("half st_grad_len = length(dFdx(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700120#else
121 // 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 -0400122 fragBuilder->codeAppendf("half st_grad_len = length(dFdy(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700123#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800124 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverth354eba52015-03-16 11:32:49 -0700125 } else {
126 // For general transforms, to determine the amount of correction we multiply a unit
127 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
128 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400129 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700130 // the length of the gradient may be 0, so we need to check for this
131 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400132 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800133 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400134 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800135 fragBuilder->codeAppend("} else {");
136 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
137 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700138
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400139 fragBuilder->codeAppendf("half2 Jdx = dFdx(%s);", st.fsIn());
140 fragBuilder->codeAppendf("half2 Jdy = dFdy(%s);", st.fsIn());
141 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800142 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000143
144 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800145 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000146 }
brianosman0586f5c2016-04-12 12:48:21 -0700147
Jim Van Verth90e89b32017-07-06 16:36:55 -0400148 if (isAliased) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400149 fragBuilder->codeAppend("half val = distance > 0 ? 1.0 : 0.0;");
Jim Van Verth90e89b32017-07-06 16:36:55 -0400150 } else if (isGammaCorrect) {
151 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
152 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want
153 // distance mapped linearly to coverage, so use a linear step:
brianosman0586f5c2016-04-12 12:48:21 -0700154 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400155 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0586f5c2016-04-12 12:48:21 -0700156 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400157 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0586f5c2016-04-12 12:48:21 -0700158 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000159
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400160 fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000161 }
162
bsalomona624bf32016-09-20 09:12:47 -0700163 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
164 FPCoordTransformIter&& transformIter) override {
Robert Phillips8296e752017-08-25 08:45:21 -0400165 const GrDistanceFieldA8TextGeoProc& dfa8gp = proc.cast<GrDistanceFieldA8TextGeoProc>();
166
jvanverth2d2a68c2014-06-10 06:42:56 -0700167#ifdef SK_GAMMA_APPLY_TO_A8
Robert Phillips8296e752017-08-25 08:45:21 -0400168 float distanceAdjust = dfa8gp.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700169 if (distanceAdjust != fDistanceAdjust) {
Robert Phillipse2538312017-08-24 17:47:23 +0000170 fDistanceAdjust = distanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400171 pdman.set1f(fDistanceAdjustUni, distanceAdjust);
jvanverth2d2a68c2014-06-10 06:42:56 -0700172 }
173#endif
joshualitt5559ca22015-05-21 15:50:36 -0700174
Brian Salomon7eae3e02018-08-07 14:02:38 +0000175 const SkISize& atlasSize = dfa8gp.atlasSize();
176 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
Robert Phillips8296e752017-08-25 08:45:21 -0400177
Brian Salomon7eae3e02018-08-07 14:02:38 +0000178 if (fAtlasSize != atlasSize) {
179 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
180 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400181 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500182 this->setTransformDataHelper(dfa8gp.localMatrix(), pdman, &transformIter);
commit-bot@chromium.org8fe2ee12014-03-26 18:03:05 +0000183 }
184
robertphillips46d36f02015-01-18 08:14:14 -0800185 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500186 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700187 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700188 const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800189 uint32_t key = dfTexEffect.getFlags();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800190 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400191 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000192 }
193
jvanverth@google.comd830d132013-11-11 20:54:09 +0000194private:
mtklein50282b42015-01-22 07:59:52 -0800195#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500196 float fDistanceAdjust = -1.f;
jvanverth21deace2015-04-01 12:43:48 -0700197 UniformHandle fDistanceAdjustUni;
mtklein50282b42015-01-22 07:59:52 -0800198#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500199 SkISize fAtlasSize = {0, 0};
Robert Phillips8296e752017-08-25 08:45:21 -0400200 UniformHandle fAtlasSizeInvUniform;
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000201
egdaniele659a582015-11-13 09:55:43 -0800202 typedef GrGLSLGeometryProcessor INHERITED;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000203};
204
205///////////////////////////////////////////////////////////////////////////////
206
Brian Salomon92be2f72018-06-19 14:33:47 -0400207constexpr GrPrimitiveProcessor::Attribute GrDistanceFieldA8TextGeoProc::kInColor;
208constexpr GrPrimitiveProcessor::Attribute GrDistanceFieldA8TextGeoProc::kInTextureCoords;
209
210GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(const sk_sp<GrTextureProxy>* proxies,
211 int numProxies,
212 const GrSamplerState& params,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500213#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon92be2f72018-06-19 14:33:47 -0400214 float distanceAdjust,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500215#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400216 uint32_t flags,
217 const SkMatrix& localMatrix)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400218 : INHERITED(kGrDistanceFieldA8TextGeoProc_ClassID)
Brian Salomon92be2f72018-06-19 14:33:47 -0400219 , fLocalMatrix(localMatrix)
220 , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500221#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400222 , fDistanceAdjust(distanceAdjust)
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500223#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400224{
Robert Phillips4bc70112018-03-01 10:24:02 -0500225 SkASSERT(numProxies <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500226 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400227
Brian Salomon5c6ac642017-12-19 11:09:32 -0500228 if (flags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400229 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500230 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400231 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500232 }
Brian Salomon92be2f72018-06-19 14:33:47 -0400233 this->setVertexAttributeCnt(3);
234
Brian Salomon7eae3e02018-08-07 14:02:38 +0000235 if (numProxies) {
236 fAtlasSize = proxies[0]->isize();
237 }
Robert Phillips4bc70112018-03-01 10:24:02 -0500238 for (int i = 0; i < numProxies; ++i) {
239 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000240 SkASSERT(proxies[i]->isize() == fAtlasSize);
241 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400242 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400243 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500244}
245
Robert Phillips4bc70112018-03-01 10:24:02 -0500246void GrDistanceFieldA8TextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
247 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400248 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500249 SkASSERT(numProxies <= kMaxTextures);
250
Brian Salomon7eae3e02018-08-07 14:02:38 +0000251 if (!fTextureSamplers[0].isInitialized()) {
252 fAtlasSize = proxies[0]->isize();
253 }
254
Robert Phillips4bc70112018-03-01 10:24:02 -0500255 for (int i = 0; i < numProxies; ++i) {
256 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000257 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500258 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000259 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400260 }
261 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400262 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400263}
264
Brian Salomon94efbf52016-11-29 13:43:05 -0500265void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800266 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700267 GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800268}
269
Brian Salomon94efbf52016-11-29 13:43:05 -0500270GrGLSLPrimitiveProcessor*
271GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700272 return new GrGLDistanceFieldA8TextGeoProc();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000273}
274
275///////////////////////////////////////////////////////////////////////////////
276
jvanverth502286d2015-04-08 12:37:51 -0700277GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000278
Hal Canary6f6961e2017-01-31 13:50:44 -0500279#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700280sk_sp<GrGeometryProcessor> GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) {
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500281 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
282 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400283 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
284 d->textureProxy(texIdx),
285 nullptr,
286 nullptr,
287 nullptr
288 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500289
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400290 GrSamplerState::WrapMode wrapModes[2];
291 GrTest::TestWrapModes(d->fRandom, wrapModes);
292 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
293 ? GrSamplerState::Filter::kBilerp
294 : GrSamplerState::Filter::kNearest);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000295
jvanverthcf371bb2016-03-10 11:10:43 -0800296 uint32_t flags = 0;
297 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
298 if (flags & kSimilarity_DistanceFieldEffectFlag) {
299 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
300 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500301 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Mike Reede1367b42017-12-15 14:28:56 -0500302#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500303 float lum = d->fRandom->nextF();
Mike Reede1367b42017-12-15 14:28:56 -0500304#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500305 return GrDistanceFieldA8TextGeoProc::Make(proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400306 samplerState,
jvanverth2d2a68c2014-06-10 06:42:56 -0700307#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500308 lum,
jvanverth2d2a68c2014-06-10 06:42:56 -0700309#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500310 flags, localMatrix);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000311}
Hal Canary6f6961e2017-01-31 13:50:44 -0500312#endif
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000313
314///////////////////////////////////////////////////////////////////////////////
315
egdaniele659a582015-11-13 09:55:43 -0800316class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700317public:
joshualitt465283c2015-09-11 08:19:35 -0700318 GrGLDistanceFieldPathGeoProc()
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400319 : fMatrix(SkMatrix::InvalidMatrix())
Robert Phillips8296e752017-08-25 08:45:21 -0400320 , fAtlasSize({0,0}) {
321 }
jvanverthfa38a302014-10-06 05:59:05 -0700322
mtklein36352bf2015-03-25 18:17:31 -0700323 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
Brian Salomon5c6ac642017-12-19 11:09:32 -0500324 const GrDistanceFieldPathGeoProc& dfPathEffect =
325 args.fGP.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700326
Chris Dalton60283612018-02-14 13:38:14 -0700327 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
jvanverthfa38a302014-10-06 05:59:05 -0700328
egdaniel4ca2e602015-11-18 08:01:26 -0800329 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800330 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800331 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800332
333 // emit attributes
Brian Salomon5c6ac642017-12-19 11:09:32 -0500334 varyingHandler->emitAttributes(dfPathEffect);
joshualittabb52a12015-01-13 15:02:10 -0800335
Robert Phillips8296e752017-08-25 08:45:21 -0400336 const char* atlasSizeInvName;
337 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400338 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400339 kHigh_GrSLPrecision,
340 "AtlasSizeInv",
341 &atlasSizeInvName);
342
Chris Dalton27372882017-12-08 13:34:21 -0700343 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500344 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
345 GrGLSLVarying texIdx(texIdxType);
Chris Dalton27372882017-12-08 13:34:21 -0700346 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -0400347 append_index_uv_varyings(args, dfPathEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
348 &texIdx, &st);
jvanverthfa38a302014-10-06 05:59:05 -0700349
joshualitt9b989322014-12-15 14:16:27 -0800350 // setup pass through color
Brian Salomon5c6ac642017-12-19 11:09:32 -0500351 varyingHandler->addPassThroughAttribute(dfPathEffect.inColor(), args.fOutputColor);
reede4ef1ca2015-02-17 18:38:38 -0800352
Brian Salomon5c6ac642017-12-19 11:09:32 -0500353 if (dfPathEffect.matrix().hasPerspective()) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400354 // Setup position
355 this->writeOutputPosition(vertBuilder,
356 uniformHandler,
357 gpArgs,
Brian Salomon92be2f72018-06-19 14:33:47 -0400358 dfPathEffect.inPosition().name(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500359 dfPathEffect.matrix(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400360 &fMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -0800361
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400362 // emit transforms
363 this->emitTransforms(vertBuilder,
364 varyingHandler,
365 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400366 dfPathEffect.inPosition().asShaderVar(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400367 args.fFPCoordTransformHandler);
368 } else {
369 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -0400370 this->writeOutputPosition(vertBuilder, gpArgs, dfPathEffect.inPosition().name());
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400371
372 // emit transforms
373 this->emitTransforms(vertBuilder,
374 varyingHandler,
375 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400376 dfPathEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500377 dfPathEffect.matrix(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400378 args.fFPCoordTransformHandler);
379 }
joshualitt4973d9d2014-11-08 09:24:25 -0800380
jvanverthfdf7ccc2015-01-27 08:19:33 -0800381 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400382 fragBuilder->codeAppendf("float2 uv = %s;", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400383 fragBuilder->codeAppend("half4 texColor;");
Brian Salomon5c6ac642017-12-19 11:09:32 -0500384 append_multitexture_lookup(args, dfPathEffect.numTextureSamplers(), texIdx, "uv",
385 "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -0800386
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400387 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400388 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverthfa38a302014-10-06 05:59:05 -0700389
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400390 fragBuilder->codeAppend("half afwidth;");
Brian Salomon5c6ac642017-12-19 11:09:32 -0500391 bool isUniformScale = (dfPathEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
392 kUniformScale_DistanceFieldEffectMask;
393 bool isSimilarity = SkToBool(dfPathEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosman0e3c5542016-04-13 13:56:21 -0700394 bool isGammaCorrect =
Brian Salomon5c6ac642017-12-19 11:09:32 -0500395 SkToBool(dfPathEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
jvanverthcf371bb2016-03-10 11:10:43 -0800396 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700397 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700398 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800399 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700400
jvanverthfa38a302014-10-06 05:59:05 -0700401 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700402#ifdef SK_VULKAN
Robert Phillips8296e752017-08-25 08:45:21 -0400403 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));",
404 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700405#else
406 // 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 -0400407 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));",
408 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700409#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800410 } else if (isSimilarity) {
411 // For similarity transform, we adjust the effect of the transformation on the distance
412 // by using the length of the gradient of the texture coordinates. We use st coordinates
413 // to ensure we're mapping 1:1 from texel space to pixel space.
jvanverthcf371bb2016-03-10 11:10:43 -0800414
415 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700416#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400417 fragBuilder->codeAppendf("half st_grad_len = length(dFdx(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700418#else
419 // 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 -0400420 fragBuilder->codeAppendf("half st_grad_len = length(dFdy(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700421#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800422 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverthfa38a302014-10-06 05:59:05 -0700423 } else {
jvanverth354eba52015-03-16 11:32:49 -0700424 // For general transforms, to determine the amount of correction we multiply a unit
425 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
426 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400427 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700428 // the length of the gradient may be 0, so we need to check for this
429 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400430 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800431 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400432 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800433 fragBuilder->codeAppend("} else {");
434 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
435 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700436
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400437 fragBuilder->codeAppendf("half2 Jdx = dFdx(%s);", st.fsIn());
438 fragBuilder->codeAppendf("half2 Jdy = dFdy(%s);", st.fsIn());
439 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
440 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
jvanverthfa38a302014-10-06 05:59:05 -0700441
442 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800443 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
jvanverthfa38a302014-10-06 05:59:05 -0700444 }
brianosman0e3c5542016-04-13 13:56:21 -0700445 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
446 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
447 // mapped linearly to coverage, so use a linear step:
448 if (isGammaCorrect) {
449 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400450 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0e3c5542016-04-13 13:56:21 -0700451 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400452 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0e3c5542016-04-13 13:56:21 -0700453 }
jvanverthfa38a302014-10-06 05:59:05 -0700454
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400455 fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
jvanverthfa38a302014-10-06 05:59:05 -0700456 }
457
bsalomona624bf32016-09-20 09:12:47 -0700458 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
459 FPCoordTransformIter&& transformIter) override {
joshualitt9b989322014-12-15 14:16:27 -0800460
joshualitte578a952015-05-14 10:09:13 -0700461 const GrDistanceFieldPathGeoProc& dfpgp = proc.cast<GrDistanceFieldPathGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700462
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400463 if (dfpgp.matrix().hasPerspective() && !fMatrix.cheapEqualTo(dfpgp.matrix())) {
464 fMatrix = dfpgp.matrix();
465 float matrix[3 * 3];
466 GrGLSLGetMatrix<3>(matrix, fMatrix);
467 pdman.setMatrix3f(fMatrixUniform, matrix);
joshualitt5559ca22015-05-21 15:50:36 -0700468 }
Robert Phillips8296e752017-08-25 08:45:21 -0400469
Brian Salomon7eae3e02018-08-07 14:02:38 +0000470 const SkISize& atlasSize = dfpgp.atlasSize();
471 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
472 if (fAtlasSize != atlasSize) {
473 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
474 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400475 }
476
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400477 if (dfpgp.matrix().hasPerspective()) {
478 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
479 } else {
480 this->setTransformDataHelper(dfpgp.matrix(), pdman, &transformIter);
481 }
jvanverthfa38a302014-10-06 05:59:05 -0700482 }
483
robertphillips46d36f02015-01-18 08:14:14 -0800484 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500485 const GrShaderCaps&,
jvanverthfa38a302014-10-06 05:59:05 -0700486 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700487 const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700488
joshualitt8fc6c2d2014-12-22 15:27:05 -0800489 uint32_t key = dfTexEffect.getFlags();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400490 key |= ComputePosKey(dfTexEffect.matrix()) << 16;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800491 b->add32(key);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400492 b->add32(dfTexEffect.matrix().hasPerspective());
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400493 b->add32(dfTexEffect.numTextureSamplers());
jvanverthfa38a302014-10-06 05:59:05 -0700494 }
495
496private:
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400497 SkMatrix fMatrix; // view matrix if perspective, local matrix otherwise
498 UniformHandle fMatrixUniform;
Robert Phillips8296e752017-08-25 08:45:21 -0400499
500 SkISize fAtlasSize;
501 UniformHandle fAtlasSizeInvUniform;
jvanverthfa38a302014-10-06 05:59:05 -0700502
egdaniele659a582015-11-13 09:55:43 -0800503 typedef GrGLSLGeometryProcessor INHERITED;
jvanverthfa38a302014-10-06 05:59:05 -0700504};
505
506///////////////////////////////////////////////////////////////////////////////
Brian Salomon92be2f72018-06-19 14:33:47 -0400507constexpr GrPrimitiveProcessor::Attribute GrDistanceFieldPathGeoProc::kInPosition;
508constexpr GrPrimitiveProcessor::Attribute GrDistanceFieldPathGeoProc::kInColor;
509constexpr GrPrimitiveProcessor::Attribute GrDistanceFieldPathGeoProc::kInTextureCoords;
510
511GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(const SkMatrix& matrix,
512 const sk_sp<GrTextureProxy>* proxies,
513 int numProxies,
514 const GrSamplerState& params,
515 uint32_t flags)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400516 : INHERITED(kGrDistanceFieldPathGeoProc_ClassID)
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400517 , fMatrix(matrix)
Brian Salomon92be2f72018-06-19 14:33:47 -0400518 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500519 SkASSERT(numProxies <= kMaxTextures);
Mike Klein5045e502018-06-19 01:40:57 +0000520 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400521
522 this->setVertexAttributeCnt(3);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000523
524 if (numProxies) {
525 fAtlasSize = proxies[0]->isize();
526 }
527
Robert Phillips4bc70112018-03-01 10:24:02 -0500528 for (int i = 0; i < numProxies; ++i) {
529 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000530 SkASSERT(proxies[i]->isize() == fAtlasSize);
531 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400532 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400533 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500534}
535
Robert Phillips4bc70112018-03-01 10:24:02 -0500536void GrDistanceFieldPathGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
537 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400538 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500539 SkASSERT(numProxies <= kMaxTextures);
540
Brian Salomon7eae3e02018-08-07 14:02:38 +0000541 if (!fTextureSamplers[0].isInitialized()) {
542 fAtlasSize = proxies[0]->isize();
543 }
544
Robert Phillips4bc70112018-03-01 10:24:02 -0500545 for (int i = 0; i < numProxies; ++i) {
546 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000547 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500548
549 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000550 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400551 }
552 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400553 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400554}
555
Brian Salomon94efbf52016-11-29 13:43:05 -0500556void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800557 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700558 GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800559}
560
Brian Salomon94efbf52016-11-29 13:43:05 -0500561GrGLSLPrimitiveProcessor*
562GrDistanceFieldPathGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700563 return new GrGLDistanceFieldPathGeoProc();
jvanverthfa38a302014-10-06 05:59:05 -0700564}
565
Brian Salomon92be2f72018-06-19 14:33:47 -0400566const GrPrimitiveProcessor::Attribute& GrDistanceFieldPathGeoProc::onVertexAttribute(int i) const {
567 return IthAttribute(i, kInPosition, kInColor, kInTextureCoords);
568}
569
jvanverthfa38a302014-10-06 05:59:05 -0700570///////////////////////////////////////////////////////////////////////////////
571
jvanverth502286d2015-04-08 12:37:51 -0700572GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc);
jvanverthfa38a302014-10-06 05:59:05 -0700573
Hal Canary6f6961e2017-01-31 13:50:44 -0500574#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700575sk_sp<GrGeometryProcessor> GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700576 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
577 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400578 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
579 d->textureProxy(texIdx),
580 nullptr,
581 nullptr,
582 nullptr
583 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500584
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400585 GrSamplerState::WrapMode wrapModes[2];
586 GrTest::TestWrapModes(d->fRandom, wrapModes);
587 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
588 ? GrSamplerState::Filter::kBilerp
589 : GrSamplerState::Filter::kNearest);
jvanverthfa38a302014-10-06 05:59:05 -0700590
jvanverthcf371bb2016-03-10 11:10:43 -0800591 uint32_t flags = 0;
592 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
593 if (flags & kSimilarity_DistanceFieldEffectFlag) {
594 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
595 }
596
Brian Osman09068252018-01-03 09:57:29 -0500597 return GrDistanceFieldPathGeoProc::Make(GrTest::TestMatrix(d->fRandom),
Robert Phillips4bc70112018-03-01 10:24:02 -0500598 proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400599 samplerState,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400600 flags);
jvanverthfa38a302014-10-06 05:59:05 -0700601}
Hal Canary6f6961e2017-01-31 13:50:44 -0500602#endif
jvanverthfa38a302014-10-06 05:59:05 -0700603
604///////////////////////////////////////////////////////////////////////////////
605
egdaniele659a582015-11-13 09:55:43 -0800606class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000607public:
Brian Salomon5c6ac642017-12-19 11:09:32 -0500608 GrGLDistanceFieldLCDTextGeoProc() : fAtlasSize({0, 0}) {
jvanverth502286d2015-04-08 12:37:51 -0700609 fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
jvanverth21deace2015-04-01 12:43:48 -0700610 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000611
mtklein36352bf2015-03-25 18:17:31 -0700612 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700613 const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
614 args.fGP.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000615
egdaniel4ca2e602015-11-18 08:01:26 -0800616 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800617 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800618 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800619
620 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800621 varyingHandler->emitAttributes(dfTexEffect);
egdaniel4ca2e602015-11-18 08:01:26 -0800622
Robert Phillips8296e752017-08-25 08:45:21 -0400623 const char* atlasSizeInvName;
624 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400625 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400626 kHigh_GrSLPrecision,
627 "AtlasSizeInv",
628 &atlasSizeInvName);
629
Chris Dalton60283612018-02-14 13:38:14 -0700630 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittabb52a12015-01-13 15:02:10 -0800631
joshualitt9b989322014-12-15 14:16:27 -0800632 // setup pass through color
Brian Salomonbfd51832017-01-04 13:22:08 -0500633 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualitt9b989322014-12-15 14:16:27 -0800634
joshualittabb52a12015-01-13 15:02:10 -0800635 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -0400636 gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
joshualitt4973d9d2014-11-08 09:24:25 -0800637
joshualittabb52a12015-01-13 15:02:10 -0800638 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800639 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800640 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800641 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400642 dfTexEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500643 dfTexEffect.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -0700644 args.fFPCoordTransformHandler);
joshualittabb52a12015-01-13 15:02:10 -0800645
jvanverthbb4a1cf2015-04-07 09:06:00 -0700646 // set up varyings
Chris Dalton27372882017-12-08 13:34:21 -0700647 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500648 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
649 GrGLSLVarying texIdx(texIdxType);
Chris Dalton27372882017-12-08 13:34:21 -0700650 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -0400651 append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -0400652 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -0400653
Chris Dalton27372882017-12-08 13:34:21 -0700654 GrGLSLVarying delta(kFloat_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600655 varyingHandler->addVarying("Delta", &delta);
Robert Phillips8296e752017-08-25 08:45:21 -0400656 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) {
657 vertBuilder->codeAppendf("%s = -%s.x/3.0;", delta.vsOut(), atlasSizeInvName);
658 } else {
659 vertBuilder->codeAppendf("%s = %s.x/3.0;", delta.vsOut(), atlasSizeInvName);
660 }
661
662 // add frag shader code
jvanverthcf371bb2016-03-10 11:10:43 -0800663 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
664 kUniformScale_DistanceFieldEffectMask;
665 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -0700666 bool isGammaCorrect =
667 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
joshualitt30ba4362014-08-21 20:18:45 -0700668
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000669 // create LCD offset adjusted by inverse of transform
jvanverthfdf7ccc2015-01-27 08:19:33 -0800670 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400671 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
joshualitt922c8b12015-08-07 09:55:23 -0700672
jvanverth78f07182014-07-30 06:17:59 -0700673 if (isUniformScale) {
jvanverthe499adf2016-07-20 12:22:14 -0700674#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400675 fragBuilder->codeAppendf("half st_grad_len = abs(dFdx(%s.x));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700676#else
677 // 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 -0400678 fragBuilder->codeAppendf("half st_grad_len = abs(dFdy(%s.y));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700679#endif
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400680 fragBuilder->codeAppendf("half2 offset = half2(st_grad_len*%s, 0.0);", delta.fsIn());
jvanverthcf371bb2016-03-10 11:10:43 -0800681 } else if (isSimilarity) {
682 // For a similarity matrix with rotation, the gradient will not be aligned
683 // with the texel coordinate axes, so we need to calculate it.
jvanverthe499adf2016-07-20 12:22:14 -0700684#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400685 fragBuilder->codeAppendf("half2 st_grad = dFdx(%s);", st.fsIn());
686 fragBuilder->codeAppendf("half2 offset = %s*st_grad;", delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700687#else
jvanverthcf371bb2016-03-10 11:10:43 -0800688 // We use dFdy because of a Mali 400 bug, and rotate -90 degrees to
689 // get the gradient in the x direction.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400690 fragBuilder->codeAppendf("half2 st_grad = dFdy(%s);", st.fsIn());
691 fragBuilder->codeAppendf("half2 offset = %s*half2(st_grad.y, -st_grad.x);",
Robert Phillips8296e752017-08-25 08:45:21 -0400692 delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700693#endif
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400694 fragBuilder->codeAppend("half st_grad_len = length(st_grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000695 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400696 fragBuilder->codeAppendf("half2 st = %s;\n", st.fsIn());
jvanverthbb4a1cf2015-04-07 09:06:00 -0700697
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400698 fragBuilder->codeAppend("half2 Jdx = dFdx(st);");
699 fragBuilder->codeAppend("half2 Jdy = dFdy(st);");
700 fragBuilder->codeAppendf("half2 offset = %s*Jdx;", delta.fsIn());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000701 }
702
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400703 // sample the texture by index
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400704 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400705 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
706 texIdx, "uv", "texColor");
Jim Van Verth6c251d22017-09-08 12:29:07 -0400707
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400708 // green is distance to uv center
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400709 fragBuilder->codeAppend("half3 distance;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400710 fragBuilder->codeAppend("distance.y = texColor.r;");
711 // red is distance to left offset
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400712 fragBuilder->codeAppend("half2 uv_adjusted = uv - offset;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400713 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
714 texIdx, "uv_adjusted", "texColor");
715 fragBuilder->codeAppend("distance.x = texColor.r;");
716 // blue is distance to right offset
717 fragBuilder->codeAppend("uv_adjusted = uv + offset;");
718 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
719 texIdx, "uv_adjusted", "texColor");
720 fragBuilder->codeAppend("distance.z = texColor.r;");
721
722 fragBuilder->codeAppend("distance = "
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400723 "half3(" SK_DistanceFieldMultiplier ")*(distance - half3(" SK_DistanceFieldThreshold"));");
jvanverth2d2a68c2014-06-10 06:42:56 -0700724
jvanverth21deace2015-04-01 12:43:48 -0700725 // adjust width based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -0700726 const char* distanceAdjustUniName = nullptr;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400727 fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -0800728 "DistanceAdjust", &distanceAdjustUniName);
egdaniel4ca2e602015-11-18 08:01:26 -0800729 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700730
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000731 // To be strictly correct, we should compute the anti-aliasing factor separately
732 // for each color component. However, this is only important when using perspective
733 // transformations, and even then using a single factor seems like a reasonable
734 // trade-off between quality and speed.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400735 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -0800736 if (isSimilarity) {
halcanary9d524f22016-03-29 09:03:52 -0700737 // For similarity transform (uniform scale-only is a subset of this), we adjust for the
738 // effect of the transformation on the distance by using the length of the gradient of
jvanverthcf371bb2016-03-10 11:10:43 -0800739 // the texture coordinates. We use st coordinates to ensure we're mapping 1:1 from texel
740 // space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700741
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000742 // this gives us a smooth step across approximately one fragment
jvanverthcf371bb2016-03-10 11:10:43 -0800743 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*st_grad_len;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000744 } else {
jvanverth354eba52015-03-16 11:32:49 -0700745 // For general transforms, to determine the amount of correction we multiply a unit
746 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
747 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400748 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance.r), dFdy(distance.r));");
jvanverthd68a5502015-03-16 12:58:43 -0700749 // the length of the gradient may be 0, so we need to check for this
750 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400751 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800752 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400753 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800754 fragBuilder->codeAppend("} else {");
755 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
756 fragBuilder->codeAppend("}");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400757 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800758 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000759
760 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800761 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000762 }
763
brianosman0586f5c2016-04-12 12:48:21 -0700764 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
765 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
766 // mapped linearly to coverage, so use a linear step:
brianosmanb461d342016-04-13 13:10:14 -0700767 if (isGammaCorrect) {
Greg Daniel55923822017-05-22 16:34:34 -0400768 fragBuilder->codeAppendf("%s = "
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400769 "half4(saturate((distance + half3(afwidth)) / half3(2.0 * afwidth)), 1.0);",
Greg Daniel55923822017-05-22 16:34:34 -0400770 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700771 } else {
Greg Daniel55923822017-05-22 16:34:34 -0400772 fragBuilder->codeAppendf(
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400773 "%s = half4(smoothstep(half3(-afwidth), half3(afwidth), distance), 1.0);",
Greg Daniel55923822017-05-22 16:34:34 -0400774 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700775 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000776 }
777
bsalomona624bf32016-09-20 09:12:47 -0700778 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& processor,
779 FPCoordTransformIter&& transformIter) override {
jvanverth21deace2015-04-01 12:43:48 -0700780 SkASSERT(fDistanceAdjustUni.isValid());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000781
joshualitt5559ca22015-05-21 15:50:36 -0700782 const GrDistanceFieldLCDTextGeoProc& dflcd = processor.cast<GrDistanceFieldLCDTextGeoProc>();
783 GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700784 if (wa != fDistanceAdjust) {
785 pdman.set3f(fDistanceAdjustUni,
786 wa.fR,
787 wa.fG,
788 wa.fB);
789 fDistanceAdjust = wa;
jvanverth2d2a68c2014-06-10 06:42:56 -0700790 }
joshualitt9b989322014-12-15 14:16:27 -0800791
Brian Salomon7eae3e02018-08-07 14:02:38 +0000792 const SkISize& atlasSize = dflcd.atlasSize();
793 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
794 if (fAtlasSize != atlasSize) {
795 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
796 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400797 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500798 this->setTransformDataHelper(dflcd.localMatrix(), pdman, &transformIter);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000799 }
800
robertphillips46d36f02015-01-18 08:14:14 -0800801 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500802 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700803 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700804 const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000805
joshualitt8fc6c2d2014-12-22 15:27:05 -0800806 uint32_t key = dfTexEffect.getFlags();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800807 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400808 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000809 }
810
811private:
jvanverth502286d2015-04-08 12:37:51 -0700812 GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400813 UniformHandle fDistanceAdjustUni;
814
815 SkISize fAtlasSize;
816 UniformHandle fAtlasSizeInvUniform;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000817
egdaniele659a582015-11-13 09:55:43 -0800818 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000819};
820
821///////////////////////////////////////////////////////////////////////////////
Mike Klein5045e502018-06-19 01:40:57 +0000822
Brian Salomon92be2f72018-06-19 14:33:47 -0400823constexpr GrPrimitiveProcessor::Attribute GrDistanceFieldLCDTextGeoProc::kInColor;
824constexpr GrPrimitiveProcessor::Attribute GrDistanceFieldLCDTextGeoProc::kInTextureCoords;
825
826GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(const sk_sp<GrTextureProxy>* proxies,
827 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 Salomon92be2f72018-06-19 14:33:47 -0400844 this->setVertexAttributeCnt(3);
845
Brian Salomon7eae3e02018-08-07 14:02:38 +0000846 if (numProxies) {
847 fAtlasSize = proxies[0]->isize();
848 }
849
Robert Phillips4bc70112018-03-01 10:24:02 -0500850 for (int i = 0; i < numProxies; ++i) {
851 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000852 SkASSERT(proxies[i]->isize() == fAtlasSize);
853 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400854 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400855 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500856}
857
Robert Phillips4bc70112018-03-01 10:24:02 -0500858void GrDistanceFieldLCDTextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
859 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400860 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500861 SkASSERT(numProxies <= kMaxTextures);
862
Brian Salomon7eae3e02018-08-07 14:02:38 +0000863 if (!fTextureSamplers[0].isInitialized()) {
864 fAtlasSize = proxies[0]->isize();
865 }
866
Robert Phillips4bc70112018-03-01 10:24:02 -0500867 for (int i = 0; i < numProxies; ++i) {
868 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000869 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500870
871 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000872 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400873 }
874 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400875 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400876}
877
Brian Salomon94efbf52016-11-29 13:43:05 -0500878void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800879 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700880 GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800881}
882
Brian Salomon94efbf52016-11-29 13:43:05 -0500883GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700884 return new GrGLDistanceFieldLCDTextGeoProc();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000885}
886
Brian Salomon92be2f72018-06-19 14:33:47 -0400887const GrPrimitiveProcessor::Attribute& GrDistanceFieldLCDTextGeoProc::onVertexAttribute(
888 int i) const {
889 return IthAttribute(i, fInPosition, kInColor, kInTextureCoords);
890}
891
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000892///////////////////////////////////////////////////////////////////////////////
893
jvanverth502286d2015-04-08 12:37:51 -0700894GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000895
Hal Canary6f6961e2017-01-31 13:50:44 -0500896#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700897sk_sp<GrGeometryProcessor> GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700898 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
899 GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400900 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
901 d->textureProxy(texIdx),
902 nullptr,
903 nullptr,
904 nullptr
905 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500906
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400907 GrSamplerState::WrapMode wrapModes[2];
908 GrTest::TestWrapModes(d->fRandom, wrapModes);
909 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
910 ? GrSamplerState::Filter::kBilerp
911 : GrSamplerState::Filter::kNearest);
jvanverth21deace2015-04-01 12:43:48 -0700912 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
jvanverth78f07182014-07-30 06:17:59 -0700913 uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
jvanverthcf371bb2016-03-10 11:10:43 -0800914 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
915 if (flags & kSimilarity_DistanceFieldEffectFlag) {
916 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
917 }
joshualitt0067ff52015-07-08 14:26:19 -0700918 flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500919 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Robert Phillips4bc70112018-03-01 10:24:02 -0500920 return GrDistanceFieldLCDTextGeoProc::Make(proxies, 1, samplerState, wa, flags, localMatrix);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000921}
Hal Canary6f6961e2017-01-31 13:50:44 -0500922#endif