blob: c03be6fc24e27e89eb1a11e3220dec300f970502 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/core/SkDistanceFieldGen.h"
9#include "src/gpu/GrCaps.h"
10#include "src/gpu/GrShaderCaps.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000011#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/effects/GrAtlasedShaderHelpers.h"
13#include "src/gpu/effects/GrDistanceFieldGeoProc.h"
14#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
15#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
16#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
17#include "src/gpu/glsl/GrGLSLUniformHandler.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/glsl/GrGLSLVarying.h"
19#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000020
jvanverth21deace2015-04-01 12:43:48 -070021// Assuming a radius of a little less than the diagonal of the fragment
jvanverth24ba0082015-03-19 11:34:13 -070022#define SK_DistanceFieldAAFactor "0.65"
jvanverth2d2a68c2014-06-10 06:42:56 -070023
egdaniele659a582015-11-13 09:55:43 -080024class GrGLDistanceFieldA8TextGeoProc : public GrGLSLGeometryProcessor {
jvanverth@google.comd830d132013-11-11 20:54:09 +000025public:
Brian Salomon5c6ac642017-12-19 11:09:32 -050026 GrGLDistanceFieldA8TextGeoProc() = default;
jvanverth@google.comd830d132013-11-11 20:54:09 +000027
mtklein36352bf2015-03-25 18:17:31 -070028 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -070029 const GrDistanceFieldA8TextGeoProc& dfTexEffect =
Robert Phillips787fd9d2021-03-22 14:48:09 -040030 args.fGeomProc.cast<GrDistanceFieldA8TextGeoProc>();
Chris Dalton60283612018-02-14 13:38:14 -070031 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
jvanverth@google.comd830d132013-11-11 20:54:09 +000032
egdaniel4ca2e602015-11-18 08:01:26 -080033 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -080034 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -080035 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -080036
37 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -080038 varyingHandler->emitAttributes(dfTexEffect);
joshualittabb52a12015-01-13 15:02:10 -080039
Brian Salomon2638f3d2019-10-22 12:20:37 -040040 const char* atlasDimensionsInvName;
Ethan Nicholas16464c32020-04-06 13:53:05 -040041 fAtlasDimensionsInvUniform = uniformHandler->addUniform(nullptr,
42 kVertex_GrShaderFlag,
Brian Salomon2638f3d2019-10-22 12:20:37 -040043 kFloat2_GrSLType,
44 "AtlasDimensionsInv",
45 &atlasDimensionsInvName);
jvanverth21deace2015-04-01 12:43:48 -070046#ifdef SK_GAMMA_APPLY_TO_A8
47 // adjust based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -070048 const char* distanceAdjustUniName = nullptr;
jvanverth21deace2015-04-01 12:43:48 -070049 // width, height, 1/(3*width)
Ethan Nicholas16464c32020-04-06 13:53:05 -040050 fDistanceAdjustUni = uniformHandler->addUniform(nullptr, kFragment_GrShaderFlag,
51 kHalf_GrSLType, "DistanceAdjust",
52 &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
John Stiles4d7ac492021-03-09 20:16:43 -050056 fragBuilder->codeAppendf("half4 %s;\n", args.fOutputColor);
Brian Salomonbfd51832017-01-04 13:22:08 -050057 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000058
joshualittabb52a12015-01-13 15:02:10 -080059 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -040060 gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
Michael Ludwig553db622020-06-19 10:47:30 -040061 this->writeLocalCoord(vertBuilder, uniformHandler, gpArgs, gpArgs->fPositionVar,
62 dfTexEffect.localMatrix(), &fLocalMatrixUniform);
joshualitt4973d9d2014-11-08 09:24:25 -080063
jvanverthbb4a1cf2015-04-07 09:06:00 -070064 // add varyings
Brian Salomonde3d4412020-09-10 16:00:16 -040065 GrGLSLVarying uv, texIdx, st;
Jim Van Verthfb395102020-02-03 10:11:19 -050066 append_index_uv_varyings(args, dfTexEffect.numTextureSamplers(),
67 dfTexEffect.inTextureCoords().name(), atlasDimensionsInvName, &uv,
68 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -040069
jvanverthcf371bb2016-03-10 11:10:43 -080070 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
71 kUniformScale_DistanceFieldEffectMask;
jvanverthbb4a1cf2015-04-07 09:06:00 -070072 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -070073 bool isGammaCorrect =
74 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
Jim Van Verth90e89b32017-07-06 16:36:55 -040075 bool isAliased =
76 SkToBool(dfTexEffect.getFlags() & kAliased_DistanceFieldEffectFlag);
halcanary9d524f22016-03-29 09:03:52 -070077
jvanverthfdf7ccc2015-01-27 08:19:33 -080078 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -040079 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -040080 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040081 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
82 texIdx, "uv", "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -080083
Ethan Nicholasf7b88202017-09-18 14:10:39 -040084 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -040085 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverth21deace2015-04-01 12:43:48 -070086#ifdef SK_GAMMA_APPLY_TO_A8
87 // adjust width based on gamma
egdaniel4ca2e602015-11-18 08:01:26 -080088 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -070089#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000090
Ethan Nicholasf7b88202017-09-18 14:10:39 -040091 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -080092 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -070093 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -070094 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -080095 // 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 +000096
jvanverth354eba52015-03-16 11:32:49 -070097 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -070098#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -050099 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
100 "*half(dFdx(%s.x)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700101#else
102 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500103 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
104 "*half(dFdy(%s.y)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700105#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800106 } else if (isSimilarity) {
107 // For similarity transform, we adjust the effect of the transformation on the distance
108 // by using the length of the gradient of the texture coordinates. We use st coordinates
109 // to ensure we're mapping 1:1 from texel space to pixel space.
110 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
111
112 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700113#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500114 fragBuilder->codeAppendf("half st_grad_len = length(half2(dFdx(%s)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700115#else
116 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500117 fragBuilder->codeAppendf("half st_grad_len = length(half2(dFdy(%s)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700118#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800119 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverth354eba52015-03-16 11:32:49 -0700120 } else {
121 // For general transforms, to determine the amount of correction we multiply a unit
122 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
123 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500124 fragBuilder->codeAppend("half2 dist_grad = half2(float2(dFdx(distance), "
125 "dFdy(distance)));");
jvanverthd68a5502015-03-16 12:58:43 -0700126 // the length of the gradient may be 0, so we need to check for this
127 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400128 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800129 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400130 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800131 fragBuilder->codeAppend("} else {");
Ethan Nicholase1f55022019-02-05 17:17:40 -0500132 fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
egdaniel4ca2e602015-11-18 08:01:26 -0800133 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700134
Ethan Nicholase1f55022019-02-05 17:17:40 -0500135 fragBuilder->codeAppendf("half2 Jdx = half2(dFdx(%s));", st.fsIn());
136 fragBuilder->codeAppendf("half2 Jdy = half2(dFdy(%s));", st.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400137 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800138 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000139
140 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800141 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000142 }
brianosman0586f5c2016-04-12 12:48:21 -0700143
Jim Van Verth90e89b32017-07-06 16:36:55 -0400144 if (isAliased) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400145 fragBuilder->codeAppend("half val = distance > 0 ? 1.0 : 0.0;");
Jim Van Verth90e89b32017-07-06 16:36:55 -0400146 } else if (isGammaCorrect) {
147 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
148 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want
149 // distance mapped linearly to coverage, so use a linear step:
brianosman0586f5c2016-04-12 12:48:21 -0700150 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400151 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0586f5c2016-04-12 12:48:21 -0700152 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400153 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0586f5c2016-04-12 12:48:21 -0700154 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000155
John Stiles4d7ac492021-03-09 20:16:43 -0500156 fragBuilder->codeAppendf("half4 %s = half4(val);", args.fOutputCoverage);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000157 }
158
Robert Phillips787fd9d2021-03-22 14:48:09 -0400159 void setData(const GrGLSLProgramDataManager& pdman,
160 const GrGeometryProcessor& geomProc) override {
161 const GrDistanceFieldA8TextGeoProc& dfa8gp = geomProc.cast<GrDistanceFieldA8TextGeoProc>();
Robert Phillips8296e752017-08-25 08:45:21 -0400162
jvanverth2d2a68c2014-06-10 06:42:56 -0700163#ifdef SK_GAMMA_APPLY_TO_A8
Robert Phillips8296e752017-08-25 08:45:21 -0400164 float distanceAdjust = dfa8gp.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700165 if (distanceAdjust != fDistanceAdjust) {
Robert Phillipse2538312017-08-24 17:47:23 +0000166 fDistanceAdjust = distanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400167 pdman.set1f(fDistanceAdjustUni, distanceAdjust);
jvanverth2d2a68c2014-06-10 06:42:56 -0700168 }
169#endif
joshualitt5559ca22015-05-21 15:50:36 -0700170
Brian Salomon2638f3d2019-10-22 12:20:37 -0400171 const SkISize& atlasDimensions = dfa8gp.atlasDimensions();
172 SkASSERT(SkIsPow2(atlasDimensions.fWidth) && SkIsPow2(atlasDimensions.fHeight));
Robert Phillips8296e752017-08-25 08:45:21 -0400173
Brian Salomon2638f3d2019-10-22 12:20:37 -0400174 if (fAtlasDimensions != atlasDimensions) {
175 pdman.set2f(fAtlasDimensionsInvUniform,
176 1.0f / atlasDimensions.fWidth,
177 1.0f / atlasDimensions.fHeight);
178 fAtlasDimensions = atlasDimensions;
Robert Phillips8296e752017-08-25 08:45:21 -0400179 }
Michael Ludwig553db622020-06-19 10:47:30 -0400180 this->setTransform(pdman, fLocalMatrixUniform, dfa8gp.localMatrix(), &fLocalMatrix);
commit-bot@chromium.org8fe2ee12014-03-26 18:03:05 +0000181 }
182
robertphillips46d36f02015-01-18 08:14:14 -0800183 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500184 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700185 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700186 const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800187 uint32_t key = dfTexEffect.getFlags();
Michael Ludwig553db622020-06-19 10:47:30 -0400188 key |= ComputeMatrixKey(dfTexEffect.localMatrix()) << 16;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800189 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400190 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000191 }
192
jvanverth@google.comd830d132013-11-11 20:54:09 +0000193private:
mtklein50282b42015-01-22 07:59:52 -0800194#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500195 float fDistanceAdjust = -1.f;
jvanverth21deace2015-04-01 12:43:48 -0700196 UniformHandle fDistanceAdjustUni;
mtklein50282b42015-01-22 07:59:52 -0800197#endif
Michael Ludwig553db622020-06-19 10:47:30 -0400198 SkISize fAtlasDimensions = {0, 0};
Brian Salomon2638f3d2019-10-22 12:20:37 -0400199 UniformHandle fAtlasDimensionsInvUniform;
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000200
Michael Ludwig553db622020-06-19 10:47:30 -0400201 SkMatrix fLocalMatrix = SkMatrix::InvalidMatrix();
202 UniformHandle fLocalMatrixUniform;
203
John Stiles7571f9e2020-09-02 22:42:33 -0400204 using INHERITED = GrGLSLGeometryProcessor;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000205};
206
207///////////////////////////////////////////////////////////////////////////////
208
Brian Osman4a3f5c82018-09-18 16:16:38 -0400209GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(const GrShaderCaps& caps,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500210 const GrSurfaceProxyView* views,
211 int numViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500212 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{
Greg Daniel9715b6c2019-12-10 15:03:10 -0500225 SkASSERT(numViews <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500226 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400227
Brian Salomon5c6ac642017-12-19 11:09:32 -0500228 if (flags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400229 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500230 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400231 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500232 }
Jim Van Verth1694a862018-12-17 19:48:42 +0000233 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType };
Jim Van Verth2f2c77a2020-01-24 15:48:23 +0000234 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
235 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500236 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400237
Greg Daniel9715b6c2019-12-10 15:03:10 -0500238 if (numViews) {
239 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000240 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500241 for (int i = 0; i < numViews; ++i) {
242 const GrSurfaceProxy* proxy = views[i].proxy();
243 SkASSERT(proxy);
244 SkASSERT(proxy->dimensions() == fAtlasDimensions);
245 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertha950b632017-09-12 11:54:11 -0400246 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500247 this->setTextureSamplerCnt(numViews);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500248}
249
Greg Daniel9715b6c2019-12-10 15:03:10 -0500250void GrDistanceFieldA8TextGeoProc::addNewViews(const GrSurfaceProxyView* views,
251 int numViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500252 GrSamplerState params) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500253 SkASSERT(numViews <= kMaxTextures);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500254 // Just to make sure we don't try to add too many proxies
Brian Osman788b9162020-02-07 10:36:46 -0500255 numViews = std::min(numViews, kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500256
Brian Salomon7eae3e02018-08-07 14:02:38 +0000257 if (!fTextureSamplers[0].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500258 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000259 }
260
Greg Daniel9715b6c2019-12-10 15:03:10 -0500261 for (int i = 0; i < numViews; ++i) {
262 const GrSurfaceProxy* proxy = views[i].proxy();
263 SkASSERT(proxy);
264 SkASSERT(proxy->dimensions() == fAtlasDimensions);
Robert Phillips4bc70112018-03-01 10:24:02 -0500265 if (!fTextureSamplers[i].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500266 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400267 }
268 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500269 this->setTextureSamplerCnt(numViews);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400270}
271
Brian Salomon94efbf52016-11-29 13:43:05 -0500272void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800273 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700274 GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800275}
276
Brian Salomon94efbf52016-11-29 13:43:05 -0500277GrGLSLPrimitiveProcessor*
278GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700279 return new GrGLDistanceFieldA8TextGeoProc();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000280}
281
282///////////////////////////////////////////////////////////////////////////////
283
jvanverth502286d2015-04-08 12:37:51 -0700284GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000285
Hal Canary6f6961e2017-01-31 13:50:44 -0500286#if GR_TEST_UTILS
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500287GrGeometryProcessor* GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500288 auto [view, ct, at] = d->randomAlphaOnlyView();
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()
Brian Salomona3b02f52020-07-15 16:02:01 -0400293 ? GrSamplerState::Filter::kLinear
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400294 : 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 Phillips7cd0bfe2019-11-20 16:08:10 -0500305 return GrDistanceFieldA8TextGeoProc::Make(d->allocator(), *d->caps()->shaderCaps(),
Greg Daniel9715b6c2019-12-10 15:03:10 -0500306 &view, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400307 samplerState,
jvanverth2d2a68c2014-06-10 06:42:56 -0700308#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500309 lum,
jvanverth2d2a68c2014-06-10 06:42:56 -0700310#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500311 flags, localMatrix);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000312}
Hal Canary6f6961e2017-01-31 13:50:44 -0500313#endif
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000314
315///////////////////////////////////////////////////////////////////////////////
316
egdaniele659a582015-11-13 09:55:43 -0800317class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700318public:
Brian Salomon2638f3d2019-10-22 12:20:37 -0400319 GrGLDistanceFieldPathGeoProc() : fMatrix(SkMatrix::InvalidMatrix()), fAtlasDimensions{0,0} {}
jvanverthfa38a302014-10-06 05:59:05 -0700320
mtklein36352bf2015-03-25 18:17:31 -0700321 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
Brian Salomon5c6ac642017-12-19 11:09:32 -0500322 const GrDistanceFieldPathGeoProc& dfPathEffect =
Robert Phillips787fd9d2021-03-22 14:48:09 -0400323 args.fGeomProc.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700324
Chris Dalton60283612018-02-14 13:38:14 -0700325 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
jvanverthfa38a302014-10-06 05:59:05 -0700326
egdaniel4ca2e602015-11-18 08:01:26 -0800327 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800328 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800329 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800330
331 // emit attributes
Brian Salomon5c6ac642017-12-19 11:09:32 -0500332 varyingHandler->emitAttributes(dfPathEffect);
joshualittabb52a12015-01-13 15:02:10 -0800333
Brian Salomon2638f3d2019-10-22 12:20:37 -0400334 const char* atlasDimensionsInvName;
Ethan Nicholas16464c32020-04-06 13:53:05 -0400335 fAtlasDimensionsInvUniform = uniformHandler->addUniform(nullptr,
336 kVertex_GrShaderFlag,
Brian Salomon2638f3d2019-10-22 12:20:37 -0400337 kFloat2_GrSLType,
338 "AtlasDimensionsInv",
339 &atlasDimensionsInvName);
Robert Phillips8296e752017-08-25 08:45:21 -0400340
Brian Salomonde3d4412020-09-10 16:00:16 -0400341 GrGLSLVarying uv, texIdx, st;
Jim Van Verthfb395102020-02-03 10:11:19 -0500342 append_index_uv_varyings(args, dfPathEffect.numTextureSamplers(),
343 dfPathEffect.inTextureCoords().name(), atlasDimensionsInvName, &uv,
344 &texIdx, &st);
jvanverthfa38a302014-10-06 05:59:05 -0700345
joshualitt9b989322014-12-15 14:16:27 -0800346 // setup pass through color
John Stiles4d7ac492021-03-09 20:16:43 -0500347 fragBuilder->codeAppendf("half4 %s;", args.fOutputColor);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500348 varyingHandler->addPassThroughAttribute(dfPathEffect.inColor(), args.fOutputColor);
reede4ef1ca2015-02-17 18:38:38 -0800349
Brian Salomon5c6ac642017-12-19 11:09:32 -0500350 if (dfPathEffect.matrix().hasPerspective()) {
Michael Ludwig553db622020-06-19 10:47:30 -0400351 // Setup position (output position is transformed, local coords are pass through)
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400352 this->writeOutputPosition(vertBuilder,
353 uniformHandler,
354 gpArgs,
Brian Salomon92be2f72018-06-19 14:33:47 -0400355 dfPathEffect.inPosition().name(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500356 dfPathEffect.matrix(),
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400357 &fMatrixUniform);
Michael Ludwig553db622020-06-19 10:47:30 -0400358 gpArgs->fLocalCoordVar = dfPathEffect.inPosition().asShaderVar();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400359 } else {
Michael Ludwig553db622020-06-19 10:47:30 -0400360 // Setup position (output position is pass through, local coords are transformed)
Brian Salomon92be2f72018-06-19 14:33:47 -0400361 this->writeOutputPosition(vertBuilder, gpArgs, dfPathEffect.inPosition().name());
Michael Ludwig553db622020-06-19 10:47:30 -0400362 this->writeLocalCoord(vertBuilder, uniformHandler, gpArgs,
363 dfPathEffect.inPosition().asShaderVar(), dfPathEffect.matrix(),
364 &fMatrixUniform);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400365 }
joshualitt4973d9d2014-11-08 09:24:25 -0800366
jvanverthfdf7ccc2015-01-27 08:19:33 -0800367 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400368 fragBuilder->codeAppendf("float2 uv = %s;", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400369 fragBuilder->codeAppend("half4 texColor;");
Brian Salomon5c6ac642017-12-19 11:09:32 -0500370 append_multitexture_lookup(args, dfPathEffect.numTextureSamplers(), texIdx, "uv",
371 "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -0800372
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400373 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400374 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverthfa38a302014-10-06 05:59:05 -0700375
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400376 fragBuilder->codeAppend("half afwidth;");
Brian Salomon5c6ac642017-12-19 11:09:32 -0500377 bool isUniformScale = (dfPathEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
378 kUniformScale_DistanceFieldEffectMask;
379 bool isSimilarity = SkToBool(dfPathEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosman0e3c5542016-04-13 13:56:21 -0700380 bool isGammaCorrect =
Brian Salomon5c6ac642017-12-19 11:09:32 -0500381 SkToBool(dfPathEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
jvanverthcf371bb2016-03-10 11:10:43 -0800382 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700383 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700384 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800385 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700386
jvanverthfa38a302014-10-06 05:59:05 -0700387 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700388#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500389 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
390 "*half(dFdx(%s.x)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700391#else
392 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500393 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
394 "*half(dFdy(%s.y)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700395#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800396 } else if (isSimilarity) {
397 // For similarity transform, we adjust the effect of the transformation on the distance
398 // by using the length of the gradient of the texture coordinates. We use st coordinates
399 // to ensure we're mapping 1:1 from texel space to pixel space.
jvanverthcf371bb2016-03-10 11:10:43 -0800400
401 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700402#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500403 fragBuilder->codeAppendf("half st_grad_len = half(length(dFdx(%s)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700404#else
405 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500406 fragBuilder->codeAppendf("half st_grad_len = half(length(dFdy(%s)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700407#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800408 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverthfa38a302014-10-06 05:59:05 -0700409 } else {
jvanverth354eba52015-03-16 11:32:49 -0700410 // For general transforms, to determine the amount of correction we multiply a unit
411 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
412 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500413 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), "
414 "dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700415 // the length of the gradient may be 0, so we need to check for this
416 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400417 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800418 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400419 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800420 fragBuilder->codeAppend("} else {");
Ethan Nicholase1f55022019-02-05 17:17:40 -0500421 fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
egdaniel4ca2e602015-11-18 08:01:26 -0800422 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700423
Ethan Nicholase1f55022019-02-05 17:17:40 -0500424 fragBuilder->codeAppendf("half2 Jdx = half2(dFdx(%s));", st.fsIn());
425 fragBuilder->codeAppendf("half2 Jdy = half2(dFdy(%s));", st.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400426 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
427 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
jvanverthfa38a302014-10-06 05:59:05 -0700428
429 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800430 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
jvanverthfa38a302014-10-06 05:59:05 -0700431 }
brianosman0e3c5542016-04-13 13:56:21 -0700432 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
433 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
434 // mapped linearly to coverage, so use a linear step:
435 if (isGammaCorrect) {
436 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400437 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0e3c5542016-04-13 13:56:21 -0700438 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400439 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0e3c5542016-04-13 13:56:21 -0700440 }
jvanverthfa38a302014-10-06 05:59:05 -0700441
John Stiles4d7ac492021-03-09 20:16:43 -0500442 fragBuilder->codeAppendf("half4 %s = half4(val);", args.fOutputCoverage);
jvanverthfa38a302014-10-06 05:59:05 -0700443 }
444
Robert Phillips787fd9d2021-03-22 14:48:09 -0400445 void setData(const GrGLSLProgramDataManager& pdman,
446 const GrGeometryProcessor& geomProc) override {
447 const GrDistanceFieldPathGeoProc& dfpgp = geomProc.cast<GrDistanceFieldPathGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700448
Michael Ludwig553db622020-06-19 10:47:30 -0400449 // We always set the matrix uniform; it's either used to transform from local to device
450 // for the output position, or from device to local for the local coord variable.
451 this->setTransform(pdman, fMatrixUniform, dfpgp.matrix(), &fMatrix);
Robert Phillips8296e752017-08-25 08:45:21 -0400452
Brian Salomon2638f3d2019-10-22 12:20:37 -0400453 const SkISize& atlasDimensions = dfpgp.atlasDimensions();
454 SkASSERT(SkIsPow2(atlasDimensions.fWidth) && SkIsPow2(atlasDimensions.fHeight));
455 if (fAtlasDimensions != atlasDimensions) {
456 pdman.set2f(fAtlasDimensionsInvUniform,
457 1.0f / atlasDimensions.fWidth,
458 1.0f / atlasDimensions.fHeight);
459 fAtlasDimensions = atlasDimensions;
Robert Phillips8296e752017-08-25 08:45:21 -0400460 }
jvanverthfa38a302014-10-06 05:59:05 -0700461 }
462
robertphillips46d36f02015-01-18 08:14:14 -0800463 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500464 const GrShaderCaps&,
jvanverthfa38a302014-10-06 05:59:05 -0700465 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700466 const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700467
joshualitt8fc6c2d2014-12-22 15:27:05 -0800468 uint32_t key = dfTexEffect.getFlags();
Michael Ludwig553db622020-06-19 10:47:30 -0400469 key |= ComputeMatrixKey(dfTexEffect.matrix()) << 16;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800470 b->add32(key);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400471 b->add32(dfTexEffect.matrix().hasPerspective());
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400472 b->add32(dfTexEffect.numTextureSamplers());
jvanverthfa38a302014-10-06 05:59:05 -0700473 }
474
475private:
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400476 SkMatrix fMatrix; // view matrix if perspective, local matrix otherwise
477 UniformHandle fMatrixUniform;
Robert Phillips8296e752017-08-25 08:45:21 -0400478
Brian Salomon2638f3d2019-10-22 12:20:37 -0400479 SkISize fAtlasDimensions;
480 UniformHandle fAtlasDimensionsInvUniform;
jvanverthfa38a302014-10-06 05:59:05 -0700481
John Stiles7571f9e2020-09-02 22:42:33 -0400482 using INHERITED = GrGLSLGeometryProcessor;
jvanverthfa38a302014-10-06 05:59:05 -0700483};
484
485///////////////////////////////////////////////////////////////////////////////
Brian Salomon92be2f72018-06-19 14:33:47 -0400486
Brian Osman4a3f5c82018-09-18 16:16:38 -0400487GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(const GrShaderCaps& caps,
488 const SkMatrix& matrix,
Brian Osmanc906d252018-12-04 11:17:46 -0500489 bool wideColor,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500490 const GrSurfaceProxyView* views,
491 int numViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500492 GrSamplerState params,
Brian Salomon92be2f72018-06-19 14:33:47 -0400493 uint32_t flags)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400494 : INHERITED(kGrDistanceFieldPathGeoProc_ClassID)
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400495 , fMatrix(matrix)
Brian Salomon92be2f72018-06-19 14:33:47 -0400496 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500497 SkASSERT(numViews <= kMaxTextures);
Mike Klein5045e502018-06-19 01:40:57 +0000498 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400499
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500500 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Osmanc906d252018-12-04 11:17:46 -0500501 fInColor = MakeColorAttribute("inColor", wideColor);
Jim Van Verth2f2c77a2020-01-24 15:48:23 +0000502 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
503 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500504 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000505
Greg Daniel9715b6c2019-12-10 15:03:10 -0500506 if (numViews) {
507 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000508 }
509
Greg Daniel9715b6c2019-12-10 15:03:10 -0500510 for (int i = 0; i < numViews; ++i) {
511 const GrSurfaceProxy* proxy = views[i].proxy();
512 SkASSERT(proxy);
513 SkASSERT(proxy->dimensions() == fAtlasDimensions);
514 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertha950b632017-09-12 11:54:11 -0400515 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500516 this->setTextureSamplerCnt(numViews);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500517}
518
Greg Daniel9715b6c2019-12-10 15:03:10 -0500519void GrDistanceFieldPathGeoProc::addNewViews(const GrSurfaceProxyView* views,
520 int numViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500521 GrSamplerState params) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500522 SkASSERT(numViews <= kMaxTextures);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500523 // Just to make sure we don't try to add too many proxies
Brian Osman788b9162020-02-07 10:36:46 -0500524 numViews = std::min(numViews, kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500525
Brian Salomon7eae3e02018-08-07 14:02:38 +0000526 if (!fTextureSamplers[0].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500527 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000528 }
529
Greg Daniel9715b6c2019-12-10 15:03:10 -0500530 for (int i = 0; i < numViews; ++i) {
531 const GrSurfaceProxy* proxy = views[i].proxy();
532 SkASSERT(proxy);
533 SkASSERT(proxy->dimensions() == fAtlasDimensions);
Robert Phillips4bc70112018-03-01 10:24:02 -0500534 if (!fTextureSamplers[i].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500535 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400536 }
537 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500538 this->setTextureSamplerCnt(numViews);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400539}
540
Brian Salomon94efbf52016-11-29 13:43:05 -0500541void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800542 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700543 GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800544}
545
Brian Salomon94efbf52016-11-29 13:43:05 -0500546GrGLSLPrimitiveProcessor*
547GrDistanceFieldPathGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700548 return new GrGLDistanceFieldPathGeoProc();
jvanverthfa38a302014-10-06 05:59:05 -0700549}
550
551///////////////////////////////////////////////////////////////////////////////
552
jvanverth502286d2015-04-08 12:37:51 -0700553GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc);
jvanverthfa38a302014-10-06 05:59:05 -0700554
Hal Canary6f6961e2017-01-31 13:50:44 -0500555#if GR_TEST_UTILS
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500556GrGeometryProcessor* GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500557 auto [view, ct, at] = d->randomAlphaOnlyView();
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500558
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400559 GrSamplerState::WrapMode wrapModes[2];
560 GrTest::TestWrapModes(d->fRandom, wrapModes);
561 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
Brian Salomona3b02f52020-07-15 16:02:01 -0400562 ? GrSamplerState::Filter::kLinear
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400563 : GrSamplerState::Filter::kNearest);
jvanverthfa38a302014-10-06 05:59:05 -0700564
jvanverthcf371bb2016-03-10 11:10:43 -0800565 uint32_t flags = 0;
566 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
567 if (flags & kSimilarity_DistanceFieldEffectFlag) {
568 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
569 }
570
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500571 return GrDistanceFieldPathGeoProc::Make(d->allocator(), *d->caps()->shaderCaps(),
Brian Osman4a3f5c82018-09-18 16:16:38 -0400572 GrTest::TestMatrix(d->fRandom),
Brian Osmanc906d252018-12-04 11:17:46 -0500573 d->fRandom->nextBool(),
Greg Daniel9715b6c2019-12-10 15:03:10 -0500574 &view, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400575 samplerState,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400576 flags);
jvanverthfa38a302014-10-06 05:59:05 -0700577}
Hal Canary6f6961e2017-01-31 13:50:44 -0500578#endif
jvanverthfa38a302014-10-06 05:59:05 -0700579
580///////////////////////////////////////////////////////////////////////////////
581
egdaniele659a582015-11-13 09:55:43 -0800582class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000583public:
Michael Ludwig553db622020-06-19 10:47:30 -0400584 GrGLDistanceFieldLCDTextGeoProc()
585 : fAtlasDimensions({0, 0})
586 , fLocalMatrix(SkMatrix::InvalidMatrix()) {
jvanverth502286d2015-04-08 12:37:51 -0700587 fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
jvanverth21deace2015-04-01 12:43:48 -0700588 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000589
mtklein36352bf2015-03-25 18:17:31 -0700590 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700591 const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
Robert Phillips787fd9d2021-03-22 14:48:09 -0400592 args.fGeomProc.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000593
egdaniel4ca2e602015-11-18 08:01:26 -0800594 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800595 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800596 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800597
598 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800599 varyingHandler->emitAttributes(dfTexEffect);
egdaniel4ca2e602015-11-18 08:01:26 -0800600
Brian Salomon2638f3d2019-10-22 12:20:37 -0400601 const char* atlasDimensionsInvName;
Ethan Nicholas16464c32020-04-06 13:53:05 -0400602 fAtlasDimensionsInvUniform = uniformHandler->addUniform(nullptr,
603 kVertex_GrShaderFlag,
Brian Salomon2638f3d2019-10-22 12:20:37 -0400604 kFloat2_GrSLType,
605 "AtlasDimensionsInv",
606 &atlasDimensionsInvName);
Robert Phillips8296e752017-08-25 08:45:21 -0400607
Chris Dalton60283612018-02-14 13:38:14 -0700608 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittabb52a12015-01-13 15:02:10 -0800609
joshualitt9b989322014-12-15 14:16:27 -0800610 // setup pass through color
John Stiles4d7ac492021-03-09 20:16:43 -0500611 fragBuilder->codeAppendf("half4 %s;\n", args.fOutputColor);
Brian Salomonbfd51832017-01-04 13:22:08 -0500612 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualitt9b989322014-12-15 14:16:27 -0800613
joshualittabb52a12015-01-13 15:02:10 -0800614 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -0400615 gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
Michael Ludwig553db622020-06-19 10:47:30 -0400616 this->writeLocalCoord(vertBuilder, uniformHandler, gpArgs,
617 dfTexEffect.inPosition().asShaderVar(), dfTexEffect.localMatrix(),
618 &fLocalMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -0800619
jvanverthbb4a1cf2015-04-07 09:06:00 -0700620 // set up varyings
Brian Salomonde3d4412020-09-10 16:00:16 -0400621 GrGLSLVarying uv, texIdx, st;
Jim Van Verthfb395102020-02-03 10:11:19 -0500622 append_index_uv_varyings(args, dfTexEffect.numTextureSamplers(),
623 dfTexEffect.inTextureCoords().name(), atlasDimensionsInvName, &uv,
624 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -0400625
Chris Dalton27372882017-12-08 13:34:21 -0700626 GrGLSLVarying delta(kFloat_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600627 varyingHandler->addVarying("Delta", &delta);
Robert Phillips8296e752017-08-25 08:45:21 -0400628 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) {
Brian Salomon2638f3d2019-10-22 12:20:37 -0400629 vertBuilder->codeAppendf("%s = -%s.x/3.0;", delta.vsOut(), atlasDimensionsInvName);
Robert Phillips8296e752017-08-25 08:45:21 -0400630 } else {
Brian Salomon2638f3d2019-10-22 12:20:37 -0400631 vertBuilder->codeAppendf("%s = %s.x/3.0;", delta.vsOut(), atlasDimensionsInvName);
Robert Phillips8296e752017-08-25 08:45:21 -0400632 }
633
634 // add frag shader code
jvanverthcf371bb2016-03-10 11:10:43 -0800635 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
636 kUniformScale_DistanceFieldEffectMask;
637 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -0700638 bool isGammaCorrect =
639 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
joshualitt30ba4362014-08-21 20:18:45 -0700640
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000641 // create LCD offset adjusted by inverse of transform
jvanverthfdf7ccc2015-01-27 08:19:33 -0800642 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400643 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
joshualitt922c8b12015-08-07 09:55:23 -0700644
jvanverth78f07182014-07-30 06:17:59 -0700645 if (isUniformScale) {
jvanverthe499adf2016-07-20 12:22:14 -0700646#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500647 fragBuilder->codeAppendf("half st_grad_len = half(abs(dFdx(%s.x)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700648#else
649 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500650 fragBuilder->codeAppendf("half st_grad_len = half(abs(dFdy(%s.y)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700651#endif
Ethan Nicholase1f55022019-02-05 17:17:40 -0500652 fragBuilder->codeAppendf("half2 offset = half2(half(st_grad_len*%s), 0.0);",
653 delta.fsIn());
jvanverthcf371bb2016-03-10 11:10:43 -0800654 } else if (isSimilarity) {
655 // For a similarity matrix with rotation, the gradient will not be aligned
656 // with the texel coordinate axes, so we need to calculate it.
jvanverthe499adf2016-07-20 12:22:14 -0700657#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500658 fragBuilder->codeAppendf("half2 st_grad = half2(dFdx(%s));", st.fsIn());
659 fragBuilder->codeAppendf("half2 offset = half(%s)*st_grad;", delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700660#else
jvanverthcf371bb2016-03-10 11:10:43 -0800661 // We use dFdy because of a Mali 400 bug, and rotate -90 degrees to
662 // get the gradient in the x direction.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500663 fragBuilder->codeAppendf("half2 st_grad = half2(dFdy(%s));", st.fsIn());
664 fragBuilder->codeAppendf("half2 offset = half2(%s*float2(st_grad.y, -st_grad.x));",
Robert Phillips8296e752017-08-25 08:45:21 -0400665 delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700666#endif
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400667 fragBuilder->codeAppend("half st_grad_len = length(st_grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000668 } else {
Ethan Nicholase1f55022019-02-05 17:17:40 -0500669 fragBuilder->codeAppendf("half2 st = half2(%s);\n", st.fsIn());
jvanverthbb4a1cf2015-04-07 09:06:00 -0700670
Ethan Nicholase1f55022019-02-05 17:17:40 -0500671 fragBuilder->codeAppend("half2 Jdx = half2(dFdx(st));");
672 fragBuilder->codeAppend("half2 Jdy = half2(dFdy(st));");
673 fragBuilder->codeAppendf("half2 offset = half2(half(%s))*Jdx;", delta.fsIn());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000674 }
675
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400676 // sample the texture by index
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400677 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400678 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
679 texIdx, "uv", "texColor");
Jim Van Verth6c251d22017-09-08 12:29:07 -0400680
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400681 // green is distance to uv center
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400682 fragBuilder->codeAppend("half3 distance;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400683 fragBuilder->codeAppend("distance.y = texColor.r;");
684 // red is distance to left offset
Ethan Nicholase1f55022019-02-05 17:17:40 -0500685 fragBuilder->codeAppend("half2 uv_adjusted = half2(uv) - offset;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400686 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
687 texIdx, "uv_adjusted", "texColor");
688 fragBuilder->codeAppend("distance.x = texColor.r;");
689 // blue is distance to right offset
Ethan Nicholase1f55022019-02-05 17:17:40 -0500690 fragBuilder->codeAppend("uv_adjusted = half2(uv) + offset;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400691 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
692 texIdx, "uv_adjusted", "texColor");
693 fragBuilder->codeAppend("distance.z = texColor.r;");
694
695 fragBuilder->codeAppend("distance = "
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400696 "half3(" SK_DistanceFieldMultiplier ")*(distance - half3(" SK_DistanceFieldThreshold"));");
jvanverth2d2a68c2014-06-10 06:42:56 -0700697
jvanverth21deace2015-04-01 12:43:48 -0700698 // adjust width based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -0700699 const char* distanceAdjustUniName = nullptr;
Ethan Nicholas16464c32020-04-06 13:53:05 -0400700 fDistanceAdjustUni = uniformHandler->addUniform(nullptr, kFragment_GrShaderFlag,
701 kHalf3_GrSLType, "DistanceAdjust",
702 &distanceAdjustUniName);
egdaniel4ca2e602015-11-18 08:01:26 -0800703 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700704
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000705 // To be strictly correct, we should compute the anti-aliasing factor separately
706 // for each color component. However, this is only important when using perspective
707 // transformations, and even then using a single factor seems like a reasonable
708 // trade-off between quality and speed.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400709 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -0800710 if (isSimilarity) {
halcanary9d524f22016-03-29 09:03:52 -0700711 // For similarity transform (uniform scale-only is a subset of this), we adjust for the
712 // effect of the transformation on the distance by using the length of the gradient of
jvanverthcf371bb2016-03-10 11:10:43 -0800713 // the texture coordinates. We use st coordinates to ensure we're mapping 1:1 from texel
714 // space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700715
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000716 // this gives us a smooth step across approximately one fragment
jvanverthcf371bb2016-03-10 11:10:43 -0800717 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*st_grad_len;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000718 } else {
jvanverth354eba52015-03-16 11:32:49 -0700719 // For general transforms, to determine the amount of correction we multiply a unit
720 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
721 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500722 fragBuilder->codeAppend("half2 dist_grad = half2(half(dFdx(distance.r)), "
723 "half(dFdy(distance.r)));");
jvanverthd68a5502015-03-16 12:58:43 -0700724 // the length of the gradient may be 0, so we need to check for this
725 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400726 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800727 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400728 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800729 fragBuilder->codeAppend("} else {");
Ethan Nicholase1f55022019-02-05 17:17:40 -0500730 fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
egdaniel4ca2e602015-11-18 08:01:26 -0800731 fragBuilder->codeAppend("}");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400732 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800733 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000734
735 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800736 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000737 }
738
brianosman0586f5c2016-04-12 12:48:21 -0700739 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
740 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
741 // mapped linearly to coverage, so use a linear step:
brianosmanb461d342016-04-13 13:10:14 -0700742 if (isGammaCorrect) {
John Stiles4d7ac492021-03-09 20:16:43 -0500743 fragBuilder->codeAppendf("half4 %s = "
744 "half4(saturate((distance + half3(afwidth)) / half3(2.0 * afwidth)), 1.0);",
745 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700746 } else {
Greg Daniel55923822017-05-22 16:34:34 -0400747 fragBuilder->codeAppendf(
John Stiles4d7ac492021-03-09 20:16:43 -0500748 "half4 %s = half4(smoothstep(half3(-afwidth), half3(afwidth), distance), 1.0);",
749 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700750 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000751 }
752
Brian Osman609f1592020-07-01 15:14:39 -0400753 void setData(const GrGLSLProgramDataManager& pdman,
Robert Phillips787fd9d2021-03-22 14:48:09 -0400754 const GrGeometryProcessor& geomProc) override {
jvanverth21deace2015-04-01 12:43:48 -0700755 SkASSERT(fDistanceAdjustUni.isValid());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000756
Robert Phillips787fd9d2021-03-22 14:48:09 -0400757 const GrDistanceFieldLCDTextGeoProc& dflcd = geomProc.cast<GrDistanceFieldLCDTextGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700758 GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700759 if (wa != fDistanceAdjust) {
760 pdman.set3f(fDistanceAdjustUni,
761 wa.fR,
762 wa.fG,
763 wa.fB);
764 fDistanceAdjust = wa;
jvanverth2d2a68c2014-06-10 06:42:56 -0700765 }
joshualitt9b989322014-12-15 14:16:27 -0800766
Brian Salomon2638f3d2019-10-22 12:20:37 -0400767 const SkISize& atlasDimensions = dflcd.atlasDimensions();
768 SkASSERT(SkIsPow2(atlasDimensions.fWidth) && SkIsPow2(atlasDimensions.fHeight));
769 if (fAtlasDimensions != atlasDimensions) {
770 pdman.set2f(fAtlasDimensionsInvUniform,
771 1.0f / atlasDimensions.fWidth,
772 1.0f / atlasDimensions.fHeight);
773 fAtlasDimensions = atlasDimensions;
Robert Phillips8296e752017-08-25 08:45:21 -0400774 }
Michael Ludwig553db622020-06-19 10:47:30 -0400775 this->setTransform(pdman, fLocalMatrixUniform, dflcd.localMatrix(), &fLocalMatrix);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000776 }
777
robertphillips46d36f02015-01-18 08:14:14 -0800778 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500779 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700780 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700781 const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000782
Michael Ludwig553db622020-06-19 10:47:30 -0400783 uint32_t key = (dfTexEffect.getFlags() << 16) |
784 ComputeMatrixKey(dfTexEffect.localMatrix());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800785 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400786 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000787 }
788
789private:
jvanverth502286d2015-04-08 12:37:51 -0700790 GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400791 UniformHandle fDistanceAdjustUni;
792
Brian Salomon2638f3d2019-10-22 12:20:37 -0400793 SkISize fAtlasDimensions;
794 UniformHandle fAtlasDimensionsInvUniform;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000795
Michael Ludwig553db622020-06-19 10:47:30 -0400796 SkMatrix fLocalMatrix;
797 UniformHandle fLocalMatrixUniform;
798
John Stiles7571f9e2020-09-02 22:42:33 -0400799 using INHERITED = GrGLSLGeometryProcessor;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000800};
801
802///////////////////////////////////////////////////////////////////////////////
Mike Klein5045e502018-06-19 01:40:57 +0000803
Brian Osman4a3f5c82018-09-18 16:16:38 -0400804GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(const GrShaderCaps& caps,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500805 const GrSurfaceProxyView* views,
806 int numViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500807 GrSamplerState params,
Brian Salomon92be2f72018-06-19 14:33:47 -0400808 DistanceAdjust distanceAdjust,
809 uint32_t flags,
810 const SkMatrix& localMatrix)
811 : INHERITED(kGrDistanceFieldLCDTextGeoProc_ClassID)
812 , fLocalMatrix(localMatrix)
813 , fDistanceAdjust(distanceAdjust)
814 , fFlags(flags & kLCD_DistanceFieldEffectMask) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500815 SkASSERT(numViews <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500816 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag));
Brian Salomon92be2f72018-06-19 14:33:47 -0400817
Brian Salomon5c6ac642017-12-19 11:09:32 -0500818 if (fFlags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400819 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500820 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400821 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500822 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500823 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
Jim Van Verth2f2c77a2020-01-24 15:48:23 +0000824 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
825 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500826 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400827
Greg Daniel9715b6c2019-12-10 15:03:10 -0500828 if (numViews) {
829 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000830 }
831
Greg Daniel9715b6c2019-12-10 15:03:10 -0500832 for (int i = 0; i < numViews; ++i) {
833 const GrSurfaceProxy* proxy = views[i].proxy();
834 SkASSERT(proxy);
835 SkASSERT(proxy->dimensions() == fAtlasDimensions);
836 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertha950b632017-09-12 11:54:11 -0400837 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500838 this->setTextureSamplerCnt(numViews);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500839}
840
Greg Daniel9715b6c2019-12-10 15:03:10 -0500841void GrDistanceFieldLCDTextGeoProc::addNewViews(const GrSurfaceProxyView* views,
842 int numViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500843 GrSamplerState params) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500844 SkASSERT(numViews <= kMaxTextures);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500845 // Just to make sure we don't try to add too many proxies
Brian Osman788b9162020-02-07 10:36:46 -0500846 numViews = std::min(numViews, kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500847
Brian Salomon7eae3e02018-08-07 14:02:38 +0000848 if (!fTextureSamplers[0].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500849 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000850 }
851
Greg Daniel9715b6c2019-12-10 15:03:10 -0500852 for (int i = 0; i < numViews; ++i) {
853 const GrSurfaceProxy* proxy = views[i].proxy();
854 SkASSERT(proxy);
855 SkASSERT(proxy->dimensions() == fAtlasDimensions);
Robert Phillips4bc70112018-03-01 10:24:02 -0500856 if (!fTextureSamplers[i].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500857 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400858 }
859 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500860 this->setTextureSamplerCnt(numViews);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400861}
862
Brian Salomon94efbf52016-11-29 13:43:05 -0500863void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800864 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700865 GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800866}
867
Brian Salomon94efbf52016-11-29 13:43:05 -0500868GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700869 return new GrGLDistanceFieldLCDTextGeoProc();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000870}
871
872///////////////////////////////////////////////////////////////////////////////
873
jvanverth502286d2015-04-08 12:37:51 -0700874GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000875
Hal Canary6f6961e2017-01-31 13:50:44 -0500876#if GR_TEST_UTILS
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500877GrGeometryProcessor* GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500878 auto [view, ct, at] = d->randomView();
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500879
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400880 GrSamplerState::WrapMode wrapModes[2];
881 GrTest::TestWrapModes(d->fRandom, wrapModes);
882 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
Brian Salomona3b02f52020-07-15 16:02:01 -0400883 ? GrSamplerState::Filter::kLinear
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400884 : GrSamplerState::Filter::kNearest);
jvanverth21deace2015-04-01 12:43:48 -0700885 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
jvanverth78f07182014-07-30 06:17:59 -0700886 uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
jvanverthcf371bb2016-03-10 11:10:43 -0800887 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
888 if (flags & kSimilarity_DistanceFieldEffectFlag) {
889 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
890 }
joshualitt0067ff52015-07-08 14:26:19 -0700891 flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500892 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500893
Greg Daniel9715b6c2019-12-10 15:03:10 -0500894 return GrDistanceFieldLCDTextGeoProc::Make(d->allocator(), *d->caps()->shaderCaps(), &view,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500895 1, samplerState, wa, flags, localMatrix);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000896}
Hal Canary6f6961e2017-01-31 13:50:44 -0500897#endif