blob: e44802977999cc5173a8601c70ca44c3415eb678 [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 Verth3ae2cd52018-11-16 14:44:13 -050071 GrGLSLVarying texIdx(kFloat_GrSLType);
Chris Dalton27372882017-12-08 13:34:21 -070072 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -040073 append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -040074 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -040075
jvanverthcf371bb2016-03-10 11:10:43 -080076 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
77 kUniformScale_DistanceFieldEffectMask;
jvanverthbb4a1cf2015-04-07 09:06:00 -070078 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -070079 bool isGammaCorrect =
80 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
Jim Van Verth90e89b32017-07-06 16:36:55 -040081 bool isAliased =
82 SkToBool(dfTexEffect.getFlags() & kAliased_DistanceFieldEffectFlag);
halcanary9d524f22016-03-29 09:03:52 -070083
jvanverthfdf7ccc2015-01-27 08:19:33 -080084 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -040085 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -040086 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040087 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
88 texIdx, "uv", "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -080089
Ethan Nicholasf7b88202017-09-18 14:10:39 -040090 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -040091 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverth21deace2015-04-01 12:43:48 -070092#ifdef SK_GAMMA_APPLY_TO_A8
93 // adjust width based on gamma
egdaniel4ca2e602015-11-18 08:01:26 -080094 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -070095#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000096
Ethan Nicholasf7b88202017-09-18 14:10:39 -040097 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -080098 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -070099 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700100 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800101 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000102
jvanverth354eba52015-03-16 11:32:49 -0700103 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700104#ifdef SK_VULKAN
105 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));",
106 st.fsIn());
107#else
108 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
egdaniel4ca2e602015-11-18 08:01:26 -0800109 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));",
110 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700111#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800112 } else if (isSimilarity) {
113 // For similarity transform, we adjust the effect of the transformation on the distance
114 // by using the length of the gradient of the texture coordinates. We use st coordinates
115 // to ensure we're mapping 1:1 from texel space to pixel space.
116 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
117
118 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700119#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400120 fragBuilder->codeAppendf("half st_grad_len = length(dFdx(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700121#else
122 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400123 fragBuilder->codeAppendf("half st_grad_len = length(dFdy(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700124#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800125 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverth354eba52015-03-16 11:32:49 -0700126 } else {
127 // For general transforms, to determine the amount of correction we multiply a unit
128 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
129 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400130 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700131 // the length of the gradient may be 0, so we need to check for this
132 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400133 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800134 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400135 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800136 fragBuilder->codeAppend("} else {");
137 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
138 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700139
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400140 fragBuilder->codeAppendf("half2 Jdx = dFdx(%s);", st.fsIn());
141 fragBuilder->codeAppendf("half2 Jdy = dFdy(%s);", st.fsIn());
142 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800143 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000144
145 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800146 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000147 }
brianosman0586f5c2016-04-12 12:48:21 -0700148
Jim Van Verth90e89b32017-07-06 16:36:55 -0400149 if (isAliased) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400150 fragBuilder->codeAppend("half val = distance > 0 ? 1.0 : 0.0;");
Jim Van Verth90e89b32017-07-06 16:36:55 -0400151 } else if (isGammaCorrect) {
152 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
153 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want
154 // distance mapped linearly to coverage, so use a linear step:
brianosman0586f5c2016-04-12 12:48:21 -0700155 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400156 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0586f5c2016-04-12 12:48:21 -0700157 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400158 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0586f5c2016-04-12 12:48:21 -0700159 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000160
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400161 fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000162 }
163
bsalomona624bf32016-09-20 09:12:47 -0700164 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
165 FPCoordTransformIter&& transformIter) override {
Robert Phillips8296e752017-08-25 08:45:21 -0400166 const GrDistanceFieldA8TextGeoProc& dfa8gp = proc.cast<GrDistanceFieldA8TextGeoProc>();
167
jvanverth2d2a68c2014-06-10 06:42:56 -0700168#ifdef SK_GAMMA_APPLY_TO_A8
Robert Phillips8296e752017-08-25 08:45:21 -0400169 float distanceAdjust = dfa8gp.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700170 if (distanceAdjust != fDistanceAdjust) {
Robert Phillipse2538312017-08-24 17:47:23 +0000171 fDistanceAdjust = distanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400172 pdman.set1f(fDistanceAdjustUni, distanceAdjust);
jvanverth2d2a68c2014-06-10 06:42:56 -0700173 }
174#endif
joshualitt5559ca22015-05-21 15:50:36 -0700175
Brian Salomon7eae3e02018-08-07 14:02:38 +0000176 const SkISize& atlasSize = dfa8gp.atlasSize();
177 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
Robert Phillips8296e752017-08-25 08:45:21 -0400178
Brian Salomon7eae3e02018-08-07 14:02:38 +0000179 if (fAtlasSize != atlasSize) {
180 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
181 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400182 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500183 this->setTransformDataHelper(dfa8gp.localMatrix(), pdman, &transformIter);
commit-bot@chromium.org8fe2ee12014-03-26 18:03:05 +0000184 }
185
robertphillips46d36f02015-01-18 08:14:14 -0800186 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500187 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700188 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700189 const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800190 uint32_t key = dfTexEffect.getFlags();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800191 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400192 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000193 }
194
jvanverth@google.comd830d132013-11-11 20:54:09 +0000195private:
mtklein50282b42015-01-22 07:59:52 -0800196#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500197 float fDistanceAdjust = -1.f;
jvanverth21deace2015-04-01 12:43:48 -0700198 UniformHandle fDistanceAdjustUni;
mtklein50282b42015-01-22 07:59:52 -0800199#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500200 SkISize fAtlasSize = {0, 0};
Robert Phillips8296e752017-08-25 08:45:21 -0400201 UniformHandle fAtlasSizeInvUniform;
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000202
egdaniele659a582015-11-13 09:55:43 -0800203 typedef GrGLSLGeometryProcessor INHERITED;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000204};
205
206///////////////////////////////////////////////////////////////////////////////
207
Brian Osman4a3f5c82018-09-18 16:16:38 -0400208GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(const GrShaderCaps& caps,
209 const sk_sp<GrTextureProxy>* proxies,
Brian Salomon92be2f72018-06-19 14:33:47 -0400210 int numProxies,
211 const GrSamplerState& params,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500212#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon92be2f72018-06-19 14:33:47 -0400213 float distanceAdjust,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500214#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400215 uint32_t flags,
216 const SkMatrix& localMatrix)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400217 : INHERITED(kGrDistanceFieldA8TextGeoProc_ClassID)
Brian Salomon92be2f72018-06-19 14:33:47 -0400218 , fLocalMatrix(localMatrix)
219 , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500220#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400221 , fDistanceAdjust(distanceAdjust)
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500222#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400223{
Robert Phillips4bc70112018-03-01 10:24:02 -0500224 SkASSERT(numProxies <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500225 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400226
Brian Salomon5c6ac642017-12-19 11:09:32 -0500227 if (flags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400228 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500229 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400230 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500231 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500232 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType };
Jim Van Verth3ae2cd52018-11-16 14:44:13 -0500233 fInTextureCoords = { "inTextureCoords", kUShort2_GrVertexAttribType,
234 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType };
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500235 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400236
Brian Salomon7eae3e02018-08-07 14:02:38 +0000237 if (numProxies) {
238 fAtlasSize = proxies[0]->isize();
239 }
Robert Phillips4bc70112018-03-01 10:24:02 -0500240 for (int i = 0; i < numProxies; ++i) {
241 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000242 SkASSERT(proxies[i]->isize() == fAtlasSize);
243 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400244 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400245 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500246}
247
Robert Phillips4bc70112018-03-01 10:24:02 -0500248void GrDistanceFieldA8TextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
249 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400250 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500251 SkASSERT(numProxies <= kMaxTextures);
252
Brian Salomon7eae3e02018-08-07 14:02:38 +0000253 if (!fTextureSamplers[0].isInitialized()) {
254 fAtlasSize = proxies[0]->isize();
255 }
256
Robert Phillips4bc70112018-03-01 10:24:02 -0500257 for (int i = 0; i < numProxies; ++i) {
258 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000259 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500260 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000261 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400262 }
263 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400264 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400265}
266
Brian Salomon94efbf52016-11-29 13:43:05 -0500267void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800268 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700269 GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800270}
271
Brian Salomon94efbf52016-11-29 13:43:05 -0500272GrGLSLPrimitiveProcessor*
273GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700274 return new GrGLDistanceFieldA8TextGeoProc();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000275}
276
277///////////////////////////////////////////////////////////////////////////////
278
jvanverth502286d2015-04-08 12:37:51 -0700279GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000280
Hal Canary6f6961e2017-01-31 13:50:44 -0500281#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700282sk_sp<GrGeometryProcessor> GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) {
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500283 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
284 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400285 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
286 d->textureProxy(texIdx),
287 nullptr,
288 nullptr,
289 nullptr
290 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500291
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400292 GrSamplerState::WrapMode wrapModes[2];
293 GrTest::TestWrapModes(d->fRandom, wrapModes);
294 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
295 ? GrSamplerState::Filter::kBilerp
296 : GrSamplerState::Filter::kNearest);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000297
jvanverthcf371bb2016-03-10 11:10:43 -0800298 uint32_t flags = 0;
299 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
300 if (flags & kSimilarity_DistanceFieldEffectFlag) {
301 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
302 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500303 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Mike Reede1367b42017-12-15 14:28:56 -0500304#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500305 float lum = d->fRandom->nextF();
Mike Reede1367b42017-12-15 14:28:56 -0500306#endif
Brian Osman4a3f5c82018-09-18 16:16:38 -0400307 return GrDistanceFieldA8TextGeoProc::Make(*d->caps()->shaderCaps(),
308 proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400309 samplerState,
jvanverth2d2a68c2014-06-10 06:42:56 -0700310#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500311 lum,
jvanverth2d2a68c2014-06-10 06:42:56 -0700312#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500313 flags, localMatrix);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000314}
Hal Canary6f6961e2017-01-31 13:50:44 -0500315#endif
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000316
317///////////////////////////////////////////////////////////////////////////////
318
egdaniele659a582015-11-13 09:55:43 -0800319class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700320public:
joshualitt465283c2015-09-11 08:19:35 -0700321 GrGLDistanceFieldPathGeoProc()
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400322 : fMatrix(SkMatrix::InvalidMatrix())
Robert Phillips8296e752017-08-25 08:45:21 -0400323 , fAtlasSize({0,0}) {
324 }
jvanverthfa38a302014-10-06 05:59:05 -0700325
mtklein36352bf2015-03-25 18:17:31 -0700326 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
Brian Salomon5c6ac642017-12-19 11:09:32 -0500327 const GrDistanceFieldPathGeoProc& dfPathEffect =
328 args.fGP.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700329
Chris Dalton60283612018-02-14 13:38:14 -0700330 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
jvanverthfa38a302014-10-06 05:59:05 -0700331
egdaniel4ca2e602015-11-18 08:01:26 -0800332 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800333 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800334 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800335
336 // emit attributes
Brian Salomon5c6ac642017-12-19 11:09:32 -0500337 varyingHandler->emitAttributes(dfPathEffect);
joshualittabb52a12015-01-13 15:02:10 -0800338
Robert Phillips8296e752017-08-25 08:45:21 -0400339 const char* atlasSizeInvName;
340 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400341 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400342 kHigh_GrSLPrecision,
343 "AtlasSizeInv",
344 &atlasSizeInvName);
345
Chris Dalton27372882017-12-08 13:34:21 -0700346 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verth3ae2cd52018-11-16 14:44:13 -0500347 GrGLSLVarying texIdx(kFloat_GrSLType);
Chris Dalton27372882017-12-08 13:34:21 -0700348 GrGLSLVarying st(kFloat2_GrSLType);
Brian Salomon92be2f72018-06-19 14:33:47 -0400349 append_index_uv_varyings(args, dfPathEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
350 &texIdx, &st);
jvanverthfa38a302014-10-06 05:59:05 -0700351
joshualitt9b989322014-12-15 14:16:27 -0800352 // setup pass through color
Brian Salomon5c6ac642017-12-19 11:09:32 -0500353 varyingHandler->addPassThroughAttribute(dfPathEffect.inColor(), args.fOutputColor);
reede4ef1ca2015-02-17 18:38:38 -0800354
Brian Salomon5c6ac642017-12-19 11:09:32 -0500355 if (dfPathEffect.matrix().hasPerspective()) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400356 // Setup position
357 this->writeOutputPosition(vertBuilder,
358 uniformHandler,
359 gpArgs,
Brian Salomon92be2f72018-06-19 14:33:47 -0400360 dfPathEffect.inPosition().name(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500361 dfPathEffect.matrix(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400362 &fMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -0800363
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400364 // emit transforms
365 this->emitTransforms(vertBuilder,
366 varyingHandler,
367 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400368 dfPathEffect.inPosition().asShaderVar(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400369 args.fFPCoordTransformHandler);
370 } else {
371 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -0400372 this->writeOutputPosition(vertBuilder, gpArgs, dfPathEffect.inPosition().name());
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400373
374 // emit transforms
375 this->emitTransforms(vertBuilder,
376 varyingHandler,
377 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400378 dfPathEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500379 dfPathEffect.matrix(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400380 args.fFPCoordTransformHandler);
381 }
joshualitt4973d9d2014-11-08 09:24:25 -0800382
jvanverthfdf7ccc2015-01-27 08:19:33 -0800383 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400384 fragBuilder->codeAppendf("float2 uv = %s;", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400385 fragBuilder->codeAppend("half4 texColor;");
Brian Salomon5c6ac642017-12-19 11:09:32 -0500386 append_multitexture_lookup(args, dfPathEffect.numTextureSamplers(), texIdx, "uv",
387 "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -0800388
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400389 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400390 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverthfa38a302014-10-06 05:59:05 -0700391
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400392 fragBuilder->codeAppend("half afwidth;");
Brian Salomon5c6ac642017-12-19 11:09:32 -0500393 bool isUniformScale = (dfPathEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
394 kUniformScale_DistanceFieldEffectMask;
395 bool isSimilarity = SkToBool(dfPathEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosman0e3c5542016-04-13 13:56:21 -0700396 bool isGammaCorrect =
Brian Salomon5c6ac642017-12-19 11:09:32 -0500397 SkToBool(dfPathEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
jvanverthcf371bb2016-03-10 11:10:43 -0800398 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700399 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700400 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800401 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700402
jvanverthfa38a302014-10-06 05:59:05 -0700403 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700404#ifdef SK_VULKAN
Robert Phillips8296e752017-08-25 08:45:21 -0400405 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));",
406 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700407#else
408 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Robert Phillips8296e752017-08-25 08:45:21 -0400409 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));",
410 st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700411#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800412 } else if (isSimilarity) {
413 // For similarity transform, we adjust the effect of the transformation on the distance
414 // by using the length of the gradient of the texture coordinates. We use st coordinates
415 // to ensure we're mapping 1:1 from texel space to pixel space.
jvanverthcf371bb2016-03-10 11:10:43 -0800416
417 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700418#ifdef SK_VULKAN
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400419 fragBuilder->codeAppendf("half st_grad_len = length(dFdx(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700420#else
421 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400422 fragBuilder->codeAppendf("half st_grad_len = length(dFdy(%s));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700423#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800424 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverthfa38a302014-10-06 05:59:05 -0700425 } else {
jvanverth354eba52015-03-16 11:32:49 -0700426 // For general transforms, to determine the amount of correction we multiply a unit
427 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
428 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400429 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700430 // the length of the gradient may be 0, so we need to check for this
431 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400432 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800433 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400434 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800435 fragBuilder->codeAppend("} else {");
436 fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);");
437 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700438
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400439 fragBuilder->codeAppendf("half2 Jdx = dFdx(%s);", st.fsIn());
440 fragBuilder->codeAppendf("half2 Jdy = dFdy(%s);", st.fsIn());
441 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
442 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
jvanverthfa38a302014-10-06 05:59:05 -0700443
444 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800445 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
jvanverthfa38a302014-10-06 05:59:05 -0700446 }
brianosman0e3c5542016-04-13 13:56:21 -0700447 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
448 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
449 // mapped linearly to coverage, so use a linear step:
450 if (isGammaCorrect) {
451 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400452 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0e3c5542016-04-13 13:56:21 -0700453 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400454 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0e3c5542016-04-13 13:56:21 -0700455 }
jvanverthfa38a302014-10-06 05:59:05 -0700456
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400457 fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
jvanverthfa38a302014-10-06 05:59:05 -0700458 }
459
bsalomona624bf32016-09-20 09:12:47 -0700460 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
461 FPCoordTransformIter&& transformIter) override {
joshualitt9b989322014-12-15 14:16:27 -0800462
joshualitte578a952015-05-14 10:09:13 -0700463 const GrDistanceFieldPathGeoProc& dfpgp = proc.cast<GrDistanceFieldPathGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700464
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400465 if (dfpgp.matrix().hasPerspective() && !fMatrix.cheapEqualTo(dfpgp.matrix())) {
466 fMatrix = dfpgp.matrix();
467 float matrix[3 * 3];
468 GrGLSLGetMatrix<3>(matrix, fMatrix);
469 pdman.setMatrix3f(fMatrixUniform, matrix);
joshualitt5559ca22015-05-21 15:50:36 -0700470 }
Robert Phillips8296e752017-08-25 08:45:21 -0400471
Brian Salomon7eae3e02018-08-07 14:02:38 +0000472 const SkISize& atlasSize = dfpgp.atlasSize();
473 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
474 if (fAtlasSize != atlasSize) {
475 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
476 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -0400477 }
478
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400479 if (dfpgp.matrix().hasPerspective()) {
480 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
481 } else {
482 this->setTransformDataHelper(dfpgp.matrix(), pdman, &transformIter);
483 }
jvanverthfa38a302014-10-06 05:59:05 -0700484 }
485
robertphillips46d36f02015-01-18 08:14:14 -0800486 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500487 const GrShaderCaps&,
jvanverthfa38a302014-10-06 05:59:05 -0700488 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700489 const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700490
joshualitt8fc6c2d2014-12-22 15:27:05 -0800491 uint32_t key = dfTexEffect.getFlags();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400492 key |= ComputePosKey(dfTexEffect.matrix()) << 16;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800493 b->add32(key);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400494 b->add32(dfTexEffect.matrix().hasPerspective());
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400495 b->add32(dfTexEffect.numTextureSamplers());
jvanverthfa38a302014-10-06 05:59:05 -0700496 }
497
498private:
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400499 SkMatrix fMatrix; // view matrix if perspective, local matrix otherwise
500 UniformHandle fMatrixUniform;
Robert Phillips8296e752017-08-25 08:45:21 -0400501
502 SkISize fAtlasSize;
503 UniformHandle fAtlasSizeInvUniform;
jvanverthfa38a302014-10-06 05:59:05 -0700504
egdaniele659a582015-11-13 09:55:43 -0800505 typedef GrGLSLGeometryProcessor INHERITED;
jvanverthfa38a302014-10-06 05:59:05 -0700506};
507
508///////////////////////////////////////////////////////////////////////////////
Brian Salomon92be2f72018-06-19 14:33:47 -0400509
Brian Osman4a3f5c82018-09-18 16:16:38 -0400510GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(const GrShaderCaps& caps,
511 const SkMatrix& matrix,
Brian Salomon92be2f72018-06-19 14:33:47 -0400512 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
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500522 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
523 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
Jim Van Verth3ae2cd52018-11-16 14:44:13 -0500524 fInTextureCoords = { "inTextureCoords", kUShort2_GrVertexAttribType,
525 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType };
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500526 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000527
528 if (numProxies) {
529 fAtlasSize = proxies[0]->isize();
530 }
531
Robert Phillips4bc70112018-03-01 10:24:02 -0500532 for (int i = 0; i < numProxies; ++i) {
533 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000534 SkASSERT(proxies[i]->isize() == fAtlasSize);
535 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400536 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400537 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500538}
539
Robert Phillips4bc70112018-03-01 10:24:02 -0500540void GrDistanceFieldPathGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
541 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400542 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500543 SkASSERT(numProxies <= kMaxTextures);
544
Brian Salomon7eae3e02018-08-07 14:02:38 +0000545 if (!fTextureSamplers[0].isInitialized()) {
546 fAtlasSize = proxies[0]->isize();
547 }
548
Robert Phillips4bc70112018-03-01 10:24:02 -0500549 for (int i = 0; i < numProxies; ++i) {
550 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000551 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500552
553 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000554 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400555 }
556 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400557 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400558}
559
Brian Salomon94efbf52016-11-29 13:43:05 -0500560void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800561 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700562 GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800563}
564
Brian Salomon94efbf52016-11-29 13:43:05 -0500565GrGLSLPrimitiveProcessor*
566GrDistanceFieldPathGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700567 return new GrGLDistanceFieldPathGeoProc();
jvanverthfa38a302014-10-06 05:59:05 -0700568}
569
570///////////////////////////////////////////////////////////////////////////////
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 Osman4a3f5c82018-09-18 16:16:38 -0400597 return GrDistanceFieldPathGeoProc::Make(*d->caps()->shaderCaps(),
598 GrTest::TestMatrix(d->fRandom),
Robert Phillips4bc70112018-03-01 10:24:02 -0500599 proxies, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400600 samplerState,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400601 flags);
jvanverthfa38a302014-10-06 05:59:05 -0700602}
Hal Canary6f6961e2017-01-31 13:50:44 -0500603#endif
jvanverthfa38a302014-10-06 05:59:05 -0700604
605///////////////////////////////////////////////////////////////////////////////
606
egdaniele659a582015-11-13 09:55:43 -0800607class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000608public:
Brian Salomon5c6ac642017-12-19 11:09:32 -0500609 GrGLDistanceFieldLCDTextGeoProc() : fAtlasSize({0, 0}) {
jvanverth502286d2015-04-08 12:37:51 -0700610 fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
jvanverth21deace2015-04-01 12:43:48 -0700611 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000612
mtklein36352bf2015-03-25 18:17:31 -0700613 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700614 const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
615 args.fGP.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000616
egdaniel4ca2e602015-11-18 08:01:26 -0800617 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800618 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800619 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800620
621 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800622 varyingHandler->emitAttributes(dfTexEffect);
egdaniel4ca2e602015-11-18 08:01:26 -0800623
Robert Phillips8296e752017-08-25 08:45:21 -0400624 const char* atlasSizeInvName;
625 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400626 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -0400627 kHigh_GrSLPrecision,
628 "AtlasSizeInv",
629 &atlasSizeInvName);
630
Chris Dalton60283612018-02-14 13:38:14 -0700631 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittabb52a12015-01-13 15:02:10 -0800632
joshualitt9b989322014-12-15 14:16:27 -0800633 // setup pass through color
Brian Salomonbfd51832017-01-04 13:22:08 -0500634 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualitt9b989322014-12-15 14:16:27 -0800635
joshualittabb52a12015-01-13 15:02:10 -0800636 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -0400637 gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
joshualitt4973d9d2014-11-08 09:24:25 -0800638
joshualittabb52a12015-01-13 15:02:10 -0800639 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800640 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800641 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800642 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -0400643 dfTexEffect.inPosition().asShaderVar(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500644 dfTexEffect.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -0700645 args.fFPCoordTransformHandler);
joshualittabb52a12015-01-13 15:02:10 -0800646
jvanverthbb4a1cf2015-04-07 09:06:00 -0700647 // set up varyings
Chris Dalton27372882017-12-08 13:34:21 -0700648 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verth3ae2cd52018-11-16 14:44:13 -0500649 GrGLSLVarying texIdx(kFloat_GrSLType);
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 Osman4a3f5c82018-09-18 16:16:38 -0400823GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(const GrShaderCaps& caps,
824 const sk_sp<GrTextureProxy>* proxies,
Brian Salomon92be2f72018-06-19 14:33:47 -0400825 int numProxies,
826 const GrSamplerState& params,
827 DistanceAdjust distanceAdjust,
828 uint32_t flags,
829 const SkMatrix& localMatrix)
830 : INHERITED(kGrDistanceFieldLCDTextGeoProc_ClassID)
831 , fLocalMatrix(localMatrix)
832 , fDistanceAdjust(distanceAdjust)
833 , fFlags(flags & kLCD_DistanceFieldEffectMask) {
834 SkASSERT(numProxies <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500835 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag));
Brian Salomon92be2f72018-06-19 14:33:47 -0400836
Brian Salomon5c6ac642017-12-19 11:09:32 -0500837 if (fFlags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400838 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500839 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400840 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500841 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500842 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
Jim Van Verth3ae2cd52018-11-16 14:44:13 -0500843 fInTextureCoords = { "inTextureCoords", kUShort2_GrVertexAttribType,
844 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType };
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500845 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400846
Brian Salomon7eae3e02018-08-07 14:02:38 +0000847 if (numProxies) {
848 fAtlasSize = proxies[0]->isize();
849 }
850
Robert Phillips4bc70112018-03-01 10:24:02 -0500851 for (int i = 0; i < numProxies; ++i) {
852 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000853 SkASSERT(proxies[i]->isize() == fAtlasSize);
854 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400855 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400856 this->setTextureSamplerCnt(numProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500857}
858
Robert Phillips4bc70112018-03-01 10:24:02 -0500859void GrDistanceFieldLCDTextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
860 int numProxies,
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400861 const GrSamplerState& params) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500862 SkASSERT(numProxies <= kMaxTextures);
863
Brian Salomon7eae3e02018-08-07 14:02:38 +0000864 if (!fTextureSamplers[0].isInitialized()) {
865 fAtlasSize = proxies[0]->isize();
866 }
867
Robert Phillips4bc70112018-03-01 10:24:02 -0500868 for (int i = 0; i < numProxies; ++i) {
869 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000870 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500871
872 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000873 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400874 }
875 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400876 this->setTextureSamplerCnt(numProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400877}
878
Brian Salomon94efbf52016-11-29 13:43:05 -0500879void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800880 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700881 GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800882}
883
Brian Salomon94efbf52016-11-29 13:43:05 -0500884GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700885 return new GrGLDistanceFieldLCDTextGeoProc();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000886}
887
888///////////////////////////////////////////////////////////////////////////////
889
jvanverth502286d2015-04-08 12:37:51 -0700890GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000891
Hal Canary6f6961e2017-01-31 13:50:44 -0500892#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700893sk_sp<GrGeometryProcessor> GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700894 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
895 GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400896 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
897 d->textureProxy(texIdx),
898 nullptr,
899 nullptr,
900 nullptr
901 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500902
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400903 GrSamplerState::WrapMode wrapModes[2];
904 GrTest::TestWrapModes(d->fRandom, wrapModes);
905 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
906 ? GrSamplerState::Filter::kBilerp
907 : GrSamplerState::Filter::kNearest);
jvanverth21deace2015-04-01 12:43:48 -0700908 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
jvanverth78f07182014-07-30 06:17:59 -0700909 uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
jvanverthcf371bb2016-03-10 11:10:43 -0800910 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
911 if (flags & kSimilarity_DistanceFieldEffectFlag) {
912 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
913 }
joshualitt0067ff52015-07-08 14:26:19 -0700914 flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500915 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Brian Osman4a3f5c82018-09-18 16:16:38 -0400916 return GrDistanceFieldLCDTextGeoProc::Make(*d->caps()->shaderCaps(), proxies, 1, samplerState,
917 wa, flags, localMatrix);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000918}
Hal Canary6f6961e2017-01-31 13:50:44 -0500919#endif