blob: 6dd67802528e695401132fd6fc34064dd9d1dc9a [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();
Brian Salomon5a328282021-04-14 10:32:25 -040061 WriteLocalCoord(vertBuilder,
62 uniformHandler,
63 *args.fShaderCaps,
64 gpArgs,
65 gpArgs->fPositionVar,
66 dfTexEffect.localMatrix(),
67 &fLocalMatrixUniform);
joshualitt4973d9d2014-11-08 09:24:25 -080068
jvanverthbb4a1cf2015-04-07 09:06:00 -070069 // add varyings
Brian Salomonde3d4412020-09-10 16:00:16 -040070 GrGLSLVarying uv, texIdx, st;
Jim Van Verthfb395102020-02-03 10:11:19 -050071 append_index_uv_varyings(args, dfTexEffect.numTextureSamplers(),
72 dfTexEffect.inTextureCoords().name(), atlasDimensionsInvName, &uv,
73 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -040074
jvanverthcf371bb2016-03-10 11:10:43 -080075 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
76 kUniformScale_DistanceFieldEffectMask;
jvanverthbb4a1cf2015-04-07 09:06:00 -070077 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -070078 bool isGammaCorrect =
79 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
Jim Van Verth90e89b32017-07-06 16:36:55 -040080 bool isAliased =
81 SkToBool(dfTexEffect.getFlags() & kAliased_DistanceFieldEffectFlag);
halcanary9d524f22016-03-29 09:03:52 -070082
jvanverthfdf7ccc2015-01-27 08:19:33 -080083 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -040084 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -040085 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040086 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
87 texIdx, "uv", "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -080088
Ethan Nicholasf7b88202017-09-18 14:10:39 -040089 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -040090 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverth21deace2015-04-01 12:43:48 -070091#ifdef SK_GAMMA_APPLY_TO_A8
92 // adjust width based on gamma
egdaniel4ca2e602015-11-18 08:01:26 -080093 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -070094#endif
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000095
Ethan Nicholasf7b88202017-09-18 14:10:39 -040096 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -080097 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -070098 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -070099 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800100 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000101
jvanverth354eba52015-03-16 11:32:49 -0700102 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700103#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500104 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
105 "*half(dFdx(%s.x)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700106#else
107 // 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 -0500108 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
109 "*half(dFdy(%s.y)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700110#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800111 } else if (isSimilarity) {
112 // For similarity transform, we adjust the effect of the transformation on the distance
113 // by using the length of the gradient of the texture coordinates. We use st coordinates
114 // to ensure we're mapping 1:1 from texel space to pixel space.
115 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
116
117 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700118#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500119 fragBuilder->codeAppendf("half st_grad_len = length(half2(dFdx(%s)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700120#else
121 // We use the y gradient because there is a bug in the Mali 400 in the x direction.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500122 fragBuilder->codeAppendf("half st_grad_len = length(half2(dFdy(%s)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700123#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800124 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverth354eba52015-03-16 11:32:49 -0700125 } else {
126 // For general transforms, to determine the amount of correction we multiply a unit
127 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
128 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500129 fragBuilder->codeAppend("half2 dist_grad = half2(float2(dFdx(distance), "
130 "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 {");
Ethan Nicholase1f55022019-02-05 17:17:40 -0500137 fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
egdaniel4ca2e602015-11-18 08:01:26 -0800138 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700139
Ethan Nicholase1f55022019-02-05 17:17:40 -0500140 fragBuilder->codeAppendf("half2 Jdx = half2(dFdx(%s));", st.fsIn());
141 fragBuilder->codeAppendf("half2 Jdy = half2(dFdy(%s));", st.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400142 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
John Stiles4d7ac492021-03-09 20:16:43 -0500161 fragBuilder->codeAppendf("half4 %s = half4(val);", args.fOutputCoverage);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000162 }
163
Robert Phillips787fd9d2021-03-22 14:48:09 -0400164 void setData(const GrGLSLProgramDataManager& pdman,
Brian Salomon5a328282021-04-14 10:32:25 -0400165 const GrShaderCaps& shaderCaps,
Robert Phillips787fd9d2021-03-22 14:48:09 -0400166 const GrGeometryProcessor& geomProc) override {
167 const GrDistanceFieldA8TextGeoProc& dfa8gp = geomProc.cast<GrDistanceFieldA8TextGeoProc>();
Robert Phillips8296e752017-08-25 08:45:21 -0400168
jvanverth2d2a68c2014-06-10 06:42:56 -0700169#ifdef SK_GAMMA_APPLY_TO_A8
Robert Phillips8296e752017-08-25 08:45:21 -0400170 float distanceAdjust = dfa8gp.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700171 if (distanceAdjust != fDistanceAdjust) {
Robert Phillipse2538312017-08-24 17:47:23 +0000172 fDistanceAdjust = distanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400173 pdman.set1f(fDistanceAdjustUni, distanceAdjust);
jvanverth2d2a68c2014-06-10 06:42:56 -0700174 }
175#endif
joshualitt5559ca22015-05-21 15:50:36 -0700176
Brian Salomon2638f3d2019-10-22 12:20:37 -0400177 const SkISize& atlasDimensions = dfa8gp.atlasDimensions();
178 SkASSERT(SkIsPow2(atlasDimensions.fWidth) && SkIsPow2(atlasDimensions.fHeight));
Robert Phillips8296e752017-08-25 08:45:21 -0400179
Brian Salomon2638f3d2019-10-22 12:20:37 -0400180 if (fAtlasDimensions != atlasDimensions) {
181 pdman.set2f(fAtlasDimensionsInvUniform,
182 1.0f / atlasDimensions.fWidth,
183 1.0f / atlasDimensions.fHeight);
184 fAtlasDimensions = atlasDimensions;
Robert Phillips8296e752017-08-25 08:45:21 -0400185 }
Brian Salomon5a328282021-04-14 10:32:25 -0400186 SetTransform(pdman, shaderCaps, fLocalMatrixUniform, dfa8gp.localMatrix(), &fLocalMatrix);
commit-bot@chromium.org8fe2ee12014-03-26 18:03:05 +0000187 }
188
robertphillips46d36f02015-01-18 08:14:14 -0800189 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon5a328282021-04-14 10:32:25 -0400190 const GrShaderCaps& shaderCaps,
joshualittb0a8a372014-09-23 09:50:21 -0700191 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700192 const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800193 uint32_t key = dfTexEffect.getFlags();
Brian Salomon5a328282021-04-14 10:32:25 -0400194 key |= ComputeMatrixKey(shaderCaps, dfTexEffect.localMatrix()) << 16;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800195 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400196 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000197 }
198
jvanverth@google.comd830d132013-11-11 20:54:09 +0000199private:
mtklein50282b42015-01-22 07:59:52 -0800200#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500201 float fDistanceAdjust = -1.f;
jvanverth21deace2015-04-01 12:43:48 -0700202 UniformHandle fDistanceAdjustUni;
mtklein50282b42015-01-22 07:59:52 -0800203#endif
Michael Ludwig553db622020-06-19 10:47:30 -0400204 SkISize fAtlasDimensions = {0, 0};
Brian Salomon2638f3d2019-10-22 12:20:37 -0400205 UniformHandle fAtlasDimensionsInvUniform;
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000206
Michael Ludwig553db622020-06-19 10:47:30 -0400207 SkMatrix fLocalMatrix = SkMatrix::InvalidMatrix();
208 UniformHandle fLocalMatrixUniform;
209
John Stiles7571f9e2020-09-02 22:42:33 -0400210 using INHERITED = GrGLSLGeometryProcessor;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000211};
212
213///////////////////////////////////////////////////////////////////////////////
214
Brian Osman4a3f5c82018-09-18 16:16:38 -0400215GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(const GrShaderCaps& caps,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500216 const GrSurfaceProxyView* views,
217 int numViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500218 GrSamplerState params,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500219#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon92be2f72018-06-19 14:33:47 -0400220 float distanceAdjust,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500221#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400222 uint32_t flags,
223 const SkMatrix& localMatrix)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400224 : INHERITED(kGrDistanceFieldA8TextGeoProc_ClassID)
Brian Salomon92be2f72018-06-19 14:33:47 -0400225 , fLocalMatrix(localMatrix)
226 , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500227#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400228 , fDistanceAdjust(distanceAdjust)
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500229#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400230{
Greg Daniel9715b6c2019-12-10 15:03:10 -0500231 SkASSERT(numViews <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500232 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400233
Brian Salomon5c6ac642017-12-19 11:09:32 -0500234 if (flags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400235 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500236 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400237 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500238 }
Jim Van Verth1694a862018-12-17 19:48:42 +0000239 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType };
Jim Van Verth2f2c77a2020-01-24 15:48:23 +0000240 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
241 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500242 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400243
Greg Daniel9715b6c2019-12-10 15:03:10 -0500244 if (numViews) {
245 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000246 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500247 for (int i = 0; i < numViews; ++i) {
248 const GrSurfaceProxy* proxy = views[i].proxy();
249 SkASSERT(proxy);
250 SkASSERT(proxy->dimensions() == fAtlasDimensions);
251 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertha950b632017-09-12 11:54:11 -0400252 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500253 this->setTextureSamplerCnt(numViews);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500254}
255
Greg Daniel9715b6c2019-12-10 15:03:10 -0500256void GrDistanceFieldA8TextGeoProc::addNewViews(const GrSurfaceProxyView* views,
257 int numViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500258 GrSamplerState params) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500259 SkASSERT(numViews <= kMaxTextures);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500260 // Just to make sure we don't try to add too many proxies
Brian Osman788b9162020-02-07 10:36:46 -0500261 numViews = std::min(numViews, kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500262
Brian Salomon7eae3e02018-08-07 14:02:38 +0000263 if (!fTextureSamplers[0].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500264 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000265 }
266
Greg Daniel9715b6c2019-12-10 15:03:10 -0500267 for (int i = 0; i < numViews; ++i) {
268 const GrSurfaceProxy* proxy = views[i].proxy();
269 SkASSERT(proxy);
270 SkASSERT(proxy->dimensions() == fAtlasDimensions);
Robert Phillips4bc70112018-03-01 10:24:02 -0500271 if (!fTextureSamplers[i].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500272 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400273 }
274 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500275 this->setTextureSamplerCnt(numViews);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400276}
277
Brian Salomon94efbf52016-11-29 13:43:05 -0500278void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800279 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700280 GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800281}
282
Robert Phillipsf10535f2021-03-23 09:30:45 -0400283GrGLSLGeometryProcessor*
Brian Salomon94efbf52016-11-29 13:43:05 -0500284GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700285 return new GrGLDistanceFieldA8TextGeoProc();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000286}
287
288///////////////////////////////////////////////////////////////////////////////
289
jvanverth502286d2015-04-08 12:37:51 -0700290GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000291
Hal Canary6f6961e2017-01-31 13:50:44 -0500292#if GR_TEST_UTILS
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500293GrGeometryProcessor* GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500294 auto [view, ct, at] = d->randomAlphaOnlyView();
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500295
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400296 GrSamplerState::WrapMode wrapModes[2];
297 GrTest::TestWrapModes(d->fRandom, wrapModes);
298 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
Brian Salomona3b02f52020-07-15 16:02:01 -0400299 ? GrSamplerState::Filter::kLinear
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400300 : GrSamplerState::Filter::kNearest);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000301
jvanverthcf371bb2016-03-10 11:10:43 -0800302 uint32_t flags = 0;
303 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
304 if (flags & kSimilarity_DistanceFieldEffectFlag) {
305 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
306 }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500307 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Mike Reede1367b42017-12-15 14:28:56 -0500308#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500309 float lum = d->fRandom->nextF();
Mike Reede1367b42017-12-15 14:28:56 -0500310#endif
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500311 return GrDistanceFieldA8TextGeoProc::Make(d->allocator(), *d->caps()->shaderCaps(),
Greg Daniel9715b6c2019-12-10 15:03:10 -0500312 &view, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400313 samplerState,
jvanverth2d2a68c2014-06-10 06:42:56 -0700314#ifdef SK_GAMMA_APPLY_TO_A8
Brian Salomon5c6ac642017-12-19 11:09:32 -0500315 lum,
jvanverth2d2a68c2014-06-10 06:42:56 -0700316#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500317 flags, localMatrix);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000318}
Hal Canary6f6961e2017-01-31 13:50:44 -0500319#endif
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000320
321///////////////////////////////////////////////////////////////////////////////
322
egdaniele659a582015-11-13 09:55:43 -0800323class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700324public:
Brian Salomon2638f3d2019-10-22 12:20:37 -0400325 GrGLDistanceFieldPathGeoProc() : fMatrix(SkMatrix::InvalidMatrix()), fAtlasDimensions{0,0} {}
jvanverthfa38a302014-10-06 05:59:05 -0700326
mtklein36352bf2015-03-25 18:17:31 -0700327 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
Brian Salomon5c6ac642017-12-19 11:09:32 -0500328 const GrDistanceFieldPathGeoProc& dfPathEffect =
Robert Phillips787fd9d2021-03-22 14:48:09 -0400329 args.fGeomProc.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700330
Chris Dalton60283612018-02-14 13:38:14 -0700331 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
jvanverthfa38a302014-10-06 05:59:05 -0700332
egdaniel4ca2e602015-11-18 08:01:26 -0800333 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800334 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800335 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800336
337 // emit attributes
Brian Salomon5c6ac642017-12-19 11:09:32 -0500338 varyingHandler->emitAttributes(dfPathEffect);
joshualittabb52a12015-01-13 15:02:10 -0800339
Brian Salomon2638f3d2019-10-22 12:20:37 -0400340 const char* atlasDimensionsInvName;
Ethan Nicholas16464c32020-04-06 13:53:05 -0400341 fAtlasDimensionsInvUniform = uniformHandler->addUniform(nullptr,
342 kVertex_GrShaderFlag,
Brian Salomon2638f3d2019-10-22 12:20:37 -0400343 kFloat2_GrSLType,
344 "AtlasDimensionsInv",
345 &atlasDimensionsInvName);
Robert Phillips8296e752017-08-25 08:45:21 -0400346
Brian Salomonde3d4412020-09-10 16:00:16 -0400347 GrGLSLVarying uv, texIdx, st;
Jim Van Verthfb395102020-02-03 10:11:19 -0500348 append_index_uv_varyings(args, dfPathEffect.numTextureSamplers(),
349 dfPathEffect.inTextureCoords().name(), atlasDimensionsInvName, &uv,
350 &texIdx, &st);
jvanverthfa38a302014-10-06 05:59:05 -0700351
joshualitt9b989322014-12-15 14:16:27 -0800352 // setup pass through color
John Stiles4d7ac492021-03-09 20:16:43 -0500353 fragBuilder->codeAppendf("half4 %s;", args.fOutputColor);
Brian Salomon5c6ac642017-12-19 11:09:32 -0500354 varyingHandler->addPassThroughAttribute(dfPathEffect.inColor(), args.fOutputColor);
reede4ef1ca2015-02-17 18:38:38 -0800355
Brian Salomon5c6ac642017-12-19 11:09:32 -0500356 if (dfPathEffect.matrix().hasPerspective()) {
Michael Ludwig553db622020-06-19 10:47:30 -0400357 // Setup position (output position is transformed, local coords are pass through)
Brian Salomon5a328282021-04-14 10:32:25 -0400358 WriteOutputPosition(vertBuilder,
359 uniformHandler,
360 *args.fShaderCaps,
361 gpArgs,
362 dfPathEffect.inPosition().name(),
363 dfPathEffect.matrix(),
364 &fMatrixUniform);
Michael Ludwig553db622020-06-19 10:47:30 -0400365 gpArgs->fLocalCoordVar = dfPathEffect.inPosition().asShaderVar();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400366 } else {
Michael Ludwig553db622020-06-19 10:47:30 -0400367 // Setup position (output position is pass through, local coords are transformed)
Brian Salomon5a328282021-04-14 10:32:25 -0400368 WriteOutputPosition(vertBuilder, gpArgs, dfPathEffect.inPosition().name());
369 WriteLocalCoord(vertBuilder,
370 uniformHandler,
371 *args.fShaderCaps,
372 gpArgs,
373 dfPathEffect.inPosition().asShaderVar(),
374 dfPathEffect.matrix(),
375 &fMatrixUniform);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400376 }
joshualitt4973d9d2014-11-08 09:24:25 -0800377
jvanverthfdf7ccc2015-01-27 08:19:33 -0800378 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400379 fragBuilder->codeAppendf("float2 uv = %s;", uv.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400380 fragBuilder->codeAppend("half4 texColor;");
Brian Salomon5c6ac642017-12-19 11:09:32 -0500381 append_multitexture_lookup(args, dfPathEffect.numTextureSamplers(), texIdx, "uv",
382 "texColor");
jvanverthfdf7ccc2015-01-27 08:19:33 -0800383
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400384 fragBuilder->codeAppend("half distance = "
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400385 SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
jvanverthfa38a302014-10-06 05:59:05 -0700386
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400387 fragBuilder->codeAppend("half afwidth;");
Brian Salomon5c6ac642017-12-19 11:09:32 -0500388 bool isUniformScale = (dfPathEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
389 kUniformScale_DistanceFieldEffectMask;
390 bool isSimilarity = SkToBool(dfPathEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosman0e3c5542016-04-13 13:56:21 -0700391 bool isGammaCorrect =
Brian Salomon5c6ac642017-12-19 11:09:32 -0500392 SkToBool(dfPathEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
jvanverthcf371bb2016-03-10 11:10:43 -0800393 if (isUniformScale) {
jvanverth354eba52015-03-16 11:32:49 -0700394 // For uniform scale, we adjust for the effect of the transformation on the distance
halcanary9d524f22016-03-29 09:03:52 -0700395 // by using the length of the gradient of the t coordinate in the y direction.
jvanverthcf371bb2016-03-10 11:10:43 -0800396 // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700397
jvanverthfa38a302014-10-06 05:59:05 -0700398 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700399#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500400 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
401 "*half(dFdx(%s.x)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700402#else
403 // 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 -0500404 fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
405 "*half(dFdy(%s.y)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700406#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800407 } else if (isSimilarity) {
408 // For similarity transform, we adjust the effect of the transformation on the distance
409 // by using the length of the gradient of the texture coordinates. We use st coordinates
410 // to ensure we're mapping 1:1 from texel space to pixel space.
jvanverthcf371bb2016-03-10 11:10:43 -0800411
412 // this gives us a smooth step across approximately one fragment
jvanverthe499adf2016-07-20 12:22:14 -0700413#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500414 fragBuilder->codeAppendf("half st_grad_len = half(length(dFdx(%s)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700415#else
416 // 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 -0500417 fragBuilder->codeAppendf("half st_grad_len = half(length(dFdy(%s)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700418#endif
jvanverthcf371bb2016-03-10 11:10:43 -0800419 fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
jvanverthfa38a302014-10-06 05:59:05 -0700420 } else {
jvanverth354eba52015-03-16 11:32:49 -0700421 // For general transforms, to determine the amount of correction we multiply a unit
422 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
423 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500424 fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), "
425 "dFdy(distance));");
jvanverthd68a5502015-03-16 12:58:43 -0700426 // the length of the gradient may be 0, so we need to check for this
427 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400428 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800429 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400430 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800431 fragBuilder->codeAppend("} else {");
Ethan Nicholase1f55022019-02-05 17:17:40 -0500432 fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
egdaniel4ca2e602015-11-18 08:01:26 -0800433 fragBuilder->codeAppend("}");
jvanverthd68a5502015-03-16 12:58:43 -0700434
Ethan Nicholase1f55022019-02-05 17:17:40 -0500435 fragBuilder->codeAppendf("half2 Jdx = half2(dFdx(%s));", st.fsIn());
436 fragBuilder->codeAppendf("half2 Jdy = half2(dFdy(%s));", st.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400437 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
438 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
jvanverthfa38a302014-10-06 05:59:05 -0700439
440 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800441 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
jvanverthfa38a302014-10-06 05:59:05 -0700442 }
brianosman0e3c5542016-04-13 13:56:21 -0700443 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
444 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
445 // mapped linearly to coverage, so use a linear step:
446 if (isGammaCorrect) {
447 fragBuilder->codeAppend(
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400448 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
brianosman0e3c5542016-04-13 13:56:21 -0700449 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400450 fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
brianosman0e3c5542016-04-13 13:56:21 -0700451 }
jvanverthfa38a302014-10-06 05:59:05 -0700452
John Stiles4d7ac492021-03-09 20:16:43 -0500453 fragBuilder->codeAppendf("half4 %s = half4(val);", args.fOutputCoverage);
jvanverthfa38a302014-10-06 05:59:05 -0700454 }
455
Robert Phillips787fd9d2021-03-22 14:48:09 -0400456 void setData(const GrGLSLProgramDataManager& pdman,
Brian Salomon5a328282021-04-14 10:32:25 -0400457 const GrShaderCaps& shaderCaps,
Robert Phillips787fd9d2021-03-22 14:48:09 -0400458 const GrGeometryProcessor& geomProc) override {
459 const GrDistanceFieldPathGeoProc& dfpgp = geomProc.cast<GrDistanceFieldPathGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700460
Michael Ludwig553db622020-06-19 10:47:30 -0400461 // We always set the matrix uniform; it's either used to transform from local to device
462 // for the output position, or from device to local for the local coord variable.
Brian Salomon5a328282021-04-14 10:32:25 -0400463 SetTransform(pdman, shaderCaps, fMatrixUniform, dfpgp.matrix(), &fMatrix);
Robert Phillips8296e752017-08-25 08:45:21 -0400464
Brian Salomon2638f3d2019-10-22 12:20:37 -0400465 const SkISize& atlasDimensions = dfpgp.atlasDimensions();
466 SkASSERT(SkIsPow2(atlasDimensions.fWidth) && SkIsPow2(atlasDimensions.fHeight));
467 if (fAtlasDimensions != atlasDimensions) {
468 pdman.set2f(fAtlasDimensionsInvUniform,
469 1.0f / atlasDimensions.fWidth,
470 1.0f / atlasDimensions.fHeight);
471 fAtlasDimensions = atlasDimensions;
Robert Phillips8296e752017-08-25 08:45:21 -0400472 }
jvanverthfa38a302014-10-06 05:59:05 -0700473 }
474
robertphillips46d36f02015-01-18 08:14:14 -0800475 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon5a328282021-04-14 10:32:25 -0400476 const GrShaderCaps& shaderCaps,
jvanverthfa38a302014-10-06 05:59:05 -0700477 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700478 const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>();
jvanverthfa38a302014-10-06 05:59:05 -0700479
joshualitt8fc6c2d2014-12-22 15:27:05 -0800480 uint32_t key = dfTexEffect.getFlags();
Brian Salomon5a328282021-04-14 10:32:25 -0400481 key |= ComputeMatrixKey(shaderCaps, dfTexEffect.matrix()) << 16;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800482 b->add32(key);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400483 b->add32(dfTexEffect.matrix().hasPerspective());
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400484 b->add32(dfTexEffect.numTextureSamplers());
jvanverthfa38a302014-10-06 05:59:05 -0700485 }
486
487private:
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400488 SkMatrix fMatrix; // view matrix if perspective, local matrix otherwise
489 UniformHandle fMatrixUniform;
Robert Phillips8296e752017-08-25 08:45:21 -0400490
Brian Salomon2638f3d2019-10-22 12:20:37 -0400491 SkISize fAtlasDimensions;
492 UniformHandle fAtlasDimensionsInvUniform;
jvanverthfa38a302014-10-06 05:59:05 -0700493
John Stiles7571f9e2020-09-02 22:42:33 -0400494 using INHERITED = GrGLSLGeometryProcessor;
jvanverthfa38a302014-10-06 05:59:05 -0700495};
496
497///////////////////////////////////////////////////////////////////////////////
Brian Salomon92be2f72018-06-19 14:33:47 -0400498
Brian Osman4a3f5c82018-09-18 16:16:38 -0400499GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(const GrShaderCaps& caps,
500 const SkMatrix& matrix,
Brian Osmanc906d252018-12-04 11:17:46 -0500501 bool wideColor,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500502 const GrSurfaceProxyView* views,
503 int numViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500504 GrSamplerState params,
Brian Salomon92be2f72018-06-19 14:33:47 -0400505 uint32_t flags)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400506 : INHERITED(kGrDistanceFieldPathGeoProc_ClassID)
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400507 , fMatrix(matrix)
Brian Salomon92be2f72018-06-19 14:33:47 -0400508 , fFlags(flags & kNonLCD_DistanceFieldEffectMask) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500509 SkASSERT(numViews <= kMaxTextures);
Mike Klein5045e502018-06-19 01:40:57 +0000510 SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
Brian Salomon92be2f72018-06-19 14:33:47 -0400511
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500512 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Osmanc906d252018-12-04 11:17:46 -0500513 fInColor = MakeColorAttribute("inColor", wideColor);
Jim Van Verth2f2c77a2020-01-24 15:48:23 +0000514 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
515 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500516 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000517
Greg Daniel9715b6c2019-12-10 15:03:10 -0500518 if (numViews) {
519 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000520 }
521
Greg Daniel9715b6c2019-12-10 15:03:10 -0500522 for (int i = 0; i < numViews; ++i) {
523 const GrSurfaceProxy* proxy = views[i].proxy();
524 SkASSERT(proxy);
525 SkASSERT(proxy->dimensions() == fAtlasDimensions);
526 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertha950b632017-09-12 11:54:11 -0400527 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500528 this->setTextureSamplerCnt(numViews);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500529}
530
Greg Daniel9715b6c2019-12-10 15:03:10 -0500531void GrDistanceFieldPathGeoProc::addNewViews(const GrSurfaceProxyView* views,
532 int numViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500533 GrSamplerState params) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500534 SkASSERT(numViews <= kMaxTextures);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500535 // Just to make sure we don't try to add too many proxies
Brian Osman788b9162020-02-07 10:36:46 -0500536 numViews = std::min(numViews, kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500537
Brian Salomon7eae3e02018-08-07 14:02:38 +0000538 if (!fTextureSamplers[0].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500539 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000540 }
541
Greg Daniel9715b6c2019-12-10 15:03:10 -0500542 for (int i = 0; i < numViews; ++i) {
543 const GrSurfaceProxy* proxy = views[i].proxy();
544 SkASSERT(proxy);
545 SkASSERT(proxy->dimensions() == fAtlasDimensions);
Robert Phillips4bc70112018-03-01 10:24:02 -0500546 if (!fTextureSamplers[i].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500547 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400548 }
549 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500550 this->setTextureSamplerCnt(numViews);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400551}
552
Brian Salomon94efbf52016-11-29 13:43:05 -0500553void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800554 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700555 GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800556}
557
Robert Phillipsf10535f2021-03-23 09:30:45 -0400558GrGLSLGeometryProcessor*
Brian Salomon94efbf52016-11-29 13:43:05 -0500559GrDistanceFieldPathGeoProc::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700560 return new GrGLDistanceFieldPathGeoProc();
jvanverthfa38a302014-10-06 05:59:05 -0700561}
562
563///////////////////////////////////////////////////////////////////////////////
564
jvanverth502286d2015-04-08 12:37:51 -0700565GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc);
jvanverthfa38a302014-10-06 05:59:05 -0700566
Hal Canary6f6961e2017-01-31 13:50:44 -0500567#if GR_TEST_UTILS
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500568GrGeometryProcessor* GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500569 auto [view, ct, at] = d->randomAlphaOnlyView();
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500570
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400571 GrSamplerState::WrapMode wrapModes[2];
572 GrTest::TestWrapModes(d->fRandom, wrapModes);
573 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
Brian Salomona3b02f52020-07-15 16:02:01 -0400574 ? GrSamplerState::Filter::kLinear
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400575 : GrSamplerState::Filter::kNearest);
jvanverthfa38a302014-10-06 05:59:05 -0700576
jvanverthcf371bb2016-03-10 11:10:43 -0800577 uint32_t flags = 0;
578 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
579 if (flags & kSimilarity_DistanceFieldEffectFlag) {
580 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
581 }
John Stiles391a9f62021-04-29 23:17:41 -0400582 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
583 bool wideColor = d->fRandom->nextBool();
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500584 return GrDistanceFieldPathGeoProc::Make(d->allocator(), *d->caps()->shaderCaps(),
John Stiles391a9f62021-04-29 23:17:41 -0400585 localMatrix,
586 wideColor,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500587 &view, 1,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400588 samplerState,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400589 flags);
jvanverthfa38a302014-10-06 05:59:05 -0700590}
Hal Canary6f6961e2017-01-31 13:50:44 -0500591#endif
jvanverthfa38a302014-10-06 05:59:05 -0700592
593///////////////////////////////////////////////////////////////////////////////
594
egdaniele659a582015-11-13 09:55:43 -0800595class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000596public:
Michael Ludwig553db622020-06-19 10:47:30 -0400597 GrGLDistanceFieldLCDTextGeoProc()
598 : fAtlasDimensions({0, 0})
599 , fLocalMatrix(SkMatrix::InvalidMatrix()) {
jvanverth502286d2015-04-08 12:37:51 -0700600 fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
jvanverth21deace2015-04-01 12:43:48 -0700601 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000602
mtklein36352bf2015-03-25 18:17:31 -0700603 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
jvanverth502286d2015-04-08 12:37:51 -0700604 const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
Robert Phillips787fd9d2021-03-22 14:48:09 -0400605 args.fGeomProc.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000606
egdaniel4ca2e602015-11-18 08:01:26 -0800607 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800608 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800609 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualittabb52a12015-01-13 15:02:10 -0800610
611 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800612 varyingHandler->emitAttributes(dfTexEffect);
egdaniel4ca2e602015-11-18 08:01:26 -0800613
Brian Salomon2638f3d2019-10-22 12:20:37 -0400614 const char* atlasDimensionsInvName;
Ethan Nicholas16464c32020-04-06 13:53:05 -0400615 fAtlasDimensionsInvUniform = uniformHandler->addUniform(nullptr,
616 kVertex_GrShaderFlag,
Brian Salomon2638f3d2019-10-22 12:20:37 -0400617 kFloat2_GrSLType,
618 "AtlasDimensionsInv",
619 &atlasDimensionsInvName);
Robert Phillips8296e752017-08-25 08:45:21 -0400620
Chris Dalton60283612018-02-14 13:38:14 -0700621 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittabb52a12015-01-13 15:02:10 -0800622
joshualitt9b989322014-12-15 14:16:27 -0800623 // setup pass through color
John Stiles4d7ac492021-03-09 20:16:43 -0500624 fragBuilder->codeAppendf("half4 %s;\n", args.fOutputColor);
Brian Salomonbfd51832017-01-04 13:22:08 -0500625 varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
joshualitt9b989322014-12-15 14:16:27 -0800626
joshualittabb52a12015-01-13 15:02:10 -0800627 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -0400628 gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
Brian Salomon5a328282021-04-14 10:32:25 -0400629 WriteLocalCoord(vertBuilder,
630 uniformHandler,
631 *args.fShaderCaps,
632 gpArgs,
633 dfTexEffect.inPosition().asShaderVar(),
634 dfTexEffect.localMatrix(),
635 &fLocalMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -0800636
jvanverthbb4a1cf2015-04-07 09:06:00 -0700637 // set up varyings
Brian Salomonde3d4412020-09-10 16:00:16 -0400638 GrGLSLVarying uv, texIdx, st;
Jim Van Verthfb395102020-02-03 10:11:19 -0500639 append_index_uv_varyings(args, dfTexEffect.numTextureSamplers(),
640 dfTexEffect.inTextureCoords().name(), atlasDimensionsInvName, &uv,
641 &texIdx, &st);
Robert Phillips8296e752017-08-25 08:45:21 -0400642
Chris Dalton27372882017-12-08 13:34:21 -0700643 GrGLSLVarying delta(kFloat_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600644 varyingHandler->addVarying("Delta", &delta);
Robert Phillips8296e752017-08-25 08:45:21 -0400645 if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) {
Brian Salomon2638f3d2019-10-22 12:20:37 -0400646 vertBuilder->codeAppendf("%s = -%s.x/3.0;", delta.vsOut(), atlasDimensionsInvName);
Robert Phillips8296e752017-08-25 08:45:21 -0400647 } else {
Brian Salomon2638f3d2019-10-22 12:20:37 -0400648 vertBuilder->codeAppendf("%s = %s.x/3.0;", delta.vsOut(), atlasDimensionsInvName);
Robert Phillips8296e752017-08-25 08:45:21 -0400649 }
650
651 // add frag shader code
jvanverthcf371bb2016-03-10 11:10:43 -0800652 bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
653 kUniformScale_DistanceFieldEffectMask;
654 bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
brianosmanb461d342016-04-13 13:10:14 -0700655 bool isGammaCorrect =
656 SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
joshualitt30ba4362014-08-21 20:18:45 -0700657
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000658 // create LCD offset adjusted by inverse of transform
jvanverthfdf7ccc2015-01-27 08:19:33 -0800659 // Use highp to work around aliasing issues
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400660 fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
joshualitt922c8b12015-08-07 09:55:23 -0700661
jvanverth78f07182014-07-30 06:17:59 -0700662 if (isUniformScale) {
jvanverthe499adf2016-07-20 12:22:14 -0700663#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500664 fragBuilder->codeAppendf("half st_grad_len = half(abs(dFdx(%s.x)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700665#else
666 // 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 -0500667 fragBuilder->codeAppendf("half st_grad_len = half(abs(dFdy(%s.y)));", st.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700668#endif
Ethan Nicholase1f55022019-02-05 17:17:40 -0500669 fragBuilder->codeAppendf("half2 offset = half2(half(st_grad_len*%s), 0.0);",
670 delta.fsIn());
jvanverthcf371bb2016-03-10 11:10:43 -0800671 } else if (isSimilarity) {
672 // For a similarity matrix with rotation, the gradient will not be aligned
673 // with the texel coordinate axes, so we need to calculate it.
jvanverthe499adf2016-07-20 12:22:14 -0700674#ifdef SK_VULKAN
Ethan Nicholase1f55022019-02-05 17:17:40 -0500675 fragBuilder->codeAppendf("half2 st_grad = half2(dFdx(%s));", st.fsIn());
676 fragBuilder->codeAppendf("half2 offset = half(%s)*st_grad;", delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700677#else
jvanverthcf371bb2016-03-10 11:10:43 -0800678 // We use dFdy because of a Mali 400 bug, and rotate -90 degrees to
679 // get the gradient in the x direction.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500680 fragBuilder->codeAppendf("half2 st_grad = half2(dFdy(%s));", st.fsIn());
681 fragBuilder->codeAppendf("half2 offset = half2(%s*float2(st_grad.y, -st_grad.x));",
Robert Phillips8296e752017-08-25 08:45:21 -0400682 delta.fsIn());
jvanverthe499adf2016-07-20 12:22:14 -0700683#endif
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400684 fragBuilder->codeAppend("half st_grad_len = length(st_grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000685 } else {
Ethan Nicholase1f55022019-02-05 17:17:40 -0500686 fragBuilder->codeAppendf("half2 st = half2(%s);\n", st.fsIn());
jvanverthbb4a1cf2015-04-07 09:06:00 -0700687
Ethan Nicholase1f55022019-02-05 17:17:40 -0500688 fragBuilder->codeAppend("half2 Jdx = half2(dFdx(st));");
689 fragBuilder->codeAppend("half2 Jdy = half2(dFdy(st));");
690 fragBuilder->codeAppendf("half2 offset = half2(half(%s))*Jdx;", delta.fsIn());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000691 }
692
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400693 // sample the texture by index
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400694 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400695 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
696 texIdx, "uv", "texColor");
Jim Van Verth6c251d22017-09-08 12:29:07 -0400697
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400698 // green is distance to uv center
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400699 fragBuilder->codeAppend("half3 distance;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400700 fragBuilder->codeAppend("distance.y = texColor.r;");
701 // red is distance to left offset
Ethan Nicholase1f55022019-02-05 17:17:40 -0500702 fragBuilder->codeAppend("half2 uv_adjusted = half2(uv) - offset;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400703 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
704 texIdx, "uv_adjusted", "texColor");
705 fragBuilder->codeAppend("distance.x = texColor.r;");
706 // blue is distance to right offset
Ethan Nicholase1f55022019-02-05 17:17:40 -0500707 fragBuilder->codeAppend("uv_adjusted = half2(uv) + offset;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400708 append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
709 texIdx, "uv_adjusted", "texColor");
710 fragBuilder->codeAppend("distance.z = texColor.r;");
711
712 fragBuilder->codeAppend("distance = "
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400713 "half3(" SK_DistanceFieldMultiplier ")*(distance - half3(" SK_DistanceFieldThreshold"));");
jvanverth2d2a68c2014-06-10 06:42:56 -0700714
jvanverth21deace2015-04-01 12:43:48 -0700715 // adjust width based on gamma
halcanary96fcdcc2015-08-27 07:41:13 -0700716 const char* distanceAdjustUniName = nullptr;
Ethan Nicholas16464c32020-04-06 13:53:05 -0400717 fDistanceAdjustUni = uniformHandler->addUniform(nullptr, kFragment_GrShaderFlag,
718 kHalf3_GrSLType, "DistanceAdjust",
719 &distanceAdjustUniName);
egdaniel4ca2e602015-11-18 08:01:26 -0800720 fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
jvanverth21deace2015-04-01 12:43:48 -0700721
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000722 // To be strictly correct, we should compute the anti-aliasing factor separately
723 // for each color component. However, this is only important when using perspective
724 // transformations, and even then using a single factor seems like a reasonable
725 // trade-off between quality and speed.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400726 fragBuilder->codeAppend("half afwidth;");
jvanverthcf371bb2016-03-10 11:10:43 -0800727 if (isSimilarity) {
halcanary9d524f22016-03-29 09:03:52 -0700728 // For similarity transform (uniform scale-only is a subset of this), we adjust for the
729 // effect of the transformation on the distance by using the length of the gradient of
jvanverthcf371bb2016-03-10 11:10:43 -0800730 // the texture coordinates. We use st coordinates to ensure we're mapping 1:1 from texel
731 // space to pixel space.
jvanverth354eba52015-03-16 11:32:49 -0700732
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000733 // this gives us a smooth step across approximately one fragment
jvanverthcf371bb2016-03-10 11:10:43 -0800734 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*st_grad_len;");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000735 } else {
jvanverth354eba52015-03-16 11:32:49 -0700736 // For general transforms, to determine the amount of correction we multiply a unit
737 // vector pointing along the SDF gradient direction by the Jacobian of the st coords
738 // (which is the inverse transform for this fragment) and take the length of the result.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500739 fragBuilder->codeAppend("half2 dist_grad = half2(half(dFdx(distance.r)), "
740 "half(dFdy(distance.r)));");
jvanverthd68a5502015-03-16 12:58:43 -0700741 // the length of the gradient may be 0, so we need to check for this
742 // this also compensates for the Adreno, which likes to drop tiles on division by 0
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400743 fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
egdaniel4ca2e602015-11-18 08:01:26 -0800744 fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400745 fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
egdaniel4ca2e602015-11-18 08:01:26 -0800746 fragBuilder->codeAppend("} else {");
Ethan Nicholase1f55022019-02-05 17:17:40 -0500747 fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
egdaniel4ca2e602015-11-18 08:01:26 -0800748 fragBuilder->codeAppend("}");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400749 fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
egdaniel4ca2e602015-11-18 08:01:26 -0800750 fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000751
752 // this gives us a smooth step across approximately one fragment
egdaniel4ca2e602015-11-18 08:01:26 -0800753 fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000754 }
755
brianosman0586f5c2016-04-12 12:48:21 -0700756 // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
757 // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
758 // mapped linearly to coverage, so use a linear step:
brianosmanb461d342016-04-13 13:10:14 -0700759 if (isGammaCorrect) {
John Stiles4d7ac492021-03-09 20:16:43 -0500760 fragBuilder->codeAppendf("half4 %s = "
761 "half4(saturate((distance + half3(afwidth)) / half3(2.0 * afwidth)), 1.0);",
762 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700763 } else {
Greg Daniel55923822017-05-22 16:34:34 -0400764 fragBuilder->codeAppendf(
John Stiles4d7ac492021-03-09 20:16:43 -0500765 "half4 %s = half4(smoothstep(half3(-afwidth), half3(afwidth), distance), 1.0);",
766 args.fOutputCoverage);
brianosman0586f5c2016-04-12 12:48:21 -0700767 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000768 }
769
Brian Osman609f1592020-07-01 15:14:39 -0400770 void setData(const GrGLSLProgramDataManager& pdman,
Brian Salomon5a328282021-04-14 10:32:25 -0400771 const GrShaderCaps& shaderCaps,
Robert Phillips787fd9d2021-03-22 14:48:09 -0400772 const GrGeometryProcessor& geomProc) override {
jvanverth21deace2015-04-01 12:43:48 -0700773 SkASSERT(fDistanceAdjustUni.isValid());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000774
Robert Phillips787fd9d2021-03-22 14:48:09 -0400775 const GrDistanceFieldLCDTextGeoProc& dflcd = geomProc.cast<GrDistanceFieldLCDTextGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700776 GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust();
jvanverth21deace2015-04-01 12:43:48 -0700777 if (wa != fDistanceAdjust) {
778 pdman.set3f(fDistanceAdjustUni,
779 wa.fR,
780 wa.fG,
781 wa.fB);
782 fDistanceAdjust = wa;
jvanverth2d2a68c2014-06-10 06:42:56 -0700783 }
joshualitt9b989322014-12-15 14:16:27 -0800784
Brian Salomon2638f3d2019-10-22 12:20:37 -0400785 const SkISize& atlasDimensions = dflcd.atlasDimensions();
786 SkASSERT(SkIsPow2(atlasDimensions.fWidth) && SkIsPow2(atlasDimensions.fHeight));
787 if (fAtlasDimensions != atlasDimensions) {
788 pdman.set2f(fAtlasDimensionsInvUniform,
789 1.0f / atlasDimensions.fWidth,
790 1.0f / atlasDimensions.fHeight);
791 fAtlasDimensions = atlasDimensions;
Robert Phillips8296e752017-08-25 08:45:21 -0400792 }
Brian Salomon5a328282021-04-14 10:32:25 -0400793 SetTransform(pdman, shaderCaps, fLocalMatrixUniform, dflcd.localMatrix(), &fLocalMatrix);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000794 }
795
robertphillips46d36f02015-01-18 08:14:14 -0800796 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon5a328282021-04-14 10:32:25 -0400797 const GrShaderCaps& shaderCaps,
joshualittb0a8a372014-09-23 09:50:21 -0700798 GrProcessorKeyBuilder* b) {
jvanverth502286d2015-04-08 12:37:51 -0700799 const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000800
Michael Ludwig553db622020-06-19 10:47:30 -0400801 uint32_t key = (dfTexEffect.getFlags() << 16) |
Brian Salomon5a328282021-04-14 10:32:25 -0400802 ComputeMatrixKey(shaderCaps, dfTexEffect.localMatrix());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800803 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400804 b->add32(dfTexEffect.numTextureSamplers());
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000805 }
806
807private:
jvanverth502286d2015-04-08 12:37:51 -0700808 GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust;
Robert Phillips8296e752017-08-25 08:45:21 -0400809 UniformHandle fDistanceAdjustUni;
810
Brian Salomon2638f3d2019-10-22 12:20:37 -0400811 SkISize fAtlasDimensions;
812 UniformHandle fAtlasDimensionsInvUniform;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000813
Michael Ludwig553db622020-06-19 10:47:30 -0400814 SkMatrix fLocalMatrix;
815 UniformHandle fLocalMatrixUniform;
816
John Stiles7571f9e2020-09-02 22:42:33 -0400817 using INHERITED = GrGLSLGeometryProcessor;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000818};
819
820///////////////////////////////////////////////////////////////////////////////
Mike Klein5045e502018-06-19 01:40:57 +0000821
Brian Osman4a3f5c82018-09-18 16:16:38 -0400822GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(const GrShaderCaps& caps,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500823 const GrSurfaceProxyView* views,
824 int numViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500825 GrSamplerState params,
Brian Salomon92be2f72018-06-19 14:33:47 -0400826 DistanceAdjust distanceAdjust,
827 uint32_t flags,
828 const SkMatrix& localMatrix)
829 : INHERITED(kGrDistanceFieldLCDTextGeoProc_ClassID)
830 , fLocalMatrix(localMatrix)
831 , fDistanceAdjust(distanceAdjust)
832 , fFlags(flags & kLCD_DistanceFieldEffectMask) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500833 SkASSERT(numViews <= kMaxTextures);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500834 SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag));
Brian Salomon92be2f72018-06-19 14:33:47 -0400835
Brian Salomon5c6ac642017-12-19 11:09:32 -0500836 if (fFlags & kPerspective_DistanceFieldEffectFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400837 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500838 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400839 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon5c6ac642017-12-19 11:09:32 -0500840 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500841 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
Jim Van Verth2f2c77a2020-01-24 15:48:23 +0000842 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
843 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500844 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400845
Greg Daniel9715b6c2019-12-10 15:03:10 -0500846 if (numViews) {
847 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000848 }
849
Greg Daniel9715b6c2019-12-10 15:03:10 -0500850 for (int i = 0; i < numViews; ++i) {
851 const GrSurfaceProxy* proxy = views[i].proxy();
852 SkASSERT(proxy);
853 SkASSERT(proxy->dimensions() == fAtlasDimensions);
854 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertha950b632017-09-12 11:54:11 -0400855 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500856 this->setTextureSamplerCnt(numViews);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500857}
858
Greg Daniel9715b6c2019-12-10 15:03:10 -0500859void GrDistanceFieldLCDTextGeoProc::addNewViews(const GrSurfaceProxyView* views,
860 int numViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500861 GrSamplerState params) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500862 SkASSERT(numViews <= kMaxTextures);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500863 // Just to make sure we don't try to add too many proxies
Brian Osman788b9162020-02-07 10:36:46 -0500864 numViews = std::min(numViews, kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500865
Brian Salomon7eae3e02018-08-07 14:02:38 +0000866 if (!fTextureSamplers[0].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500867 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000868 }
869
Greg Daniel9715b6c2019-12-10 15:03:10 -0500870 for (int i = 0; i < numViews; ++i) {
871 const GrSurfaceProxy* proxy = views[i].proxy();
872 SkASSERT(proxy);
873 SkASSERT(proxy->dimensions() == fAtlasDimensions);
Robert Phillips4bc70112018-03-01 10:24:02 -0500874 if (!fTextureSamplers[i].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500875 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400876 }
877 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500878 this->setTextureSamplerCnt(numViews);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400879}
880
Brian Salomon94efbf52016-11-29 13:43:05 -0500881void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800882 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700883 GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800884}
885
Robert Phillipsf10535f2021-03-23 09:30:45 -0400886GrGLSLGeometryProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(
887 const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700888 return new GrGLDistanceFieldLCDTextGeoProc();
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000889}
890
891///////////////////////////////////////////////////////////////////////////////
892
jvanverth502286d2015-04-08 12:37:51 -0700893GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000894
Hal Canary6f6961e2017-01-31 13:50:44 -0500895#if GR_TEST_UTILS
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500896GrGeometryProcessor* GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500897 auto [view, ct, at] = d->randomView();
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500898
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400899 GrSamplerState::WrapMode wrapModes[2];
900 GrTest::TestWrapModes(d->fRandom, wrapModes);
901 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
Brian Salomona3b02f52020-07-15 16:02:01 -0400902 ? GrSamplerState::Filter::kLinear
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400903 : GrSamplerState::Filter::kNearest);
jvanverth21deace2015-04-01 12:43:48 -0700904 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
jvanverth78f07182014-07-30 06:17:59 -0700905 uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
jvanverthcf371bb2016-03-10 11:10:43 -0800906 flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
907 if (flags & kSimilarity_DistanceFieldEffectFlag) {
908 flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
909 }
joshualitt0067ff52015-07-08 14:26:19 -0700910 flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500911 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500912
Greg Daniel9715b6c2019-12-10 15:03:10 -0500913 return GrDistanceFieldLCDTextGeoProc::Make(d->allocator(), *d->caps()->shaderCaps(), &view,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500914 1, samplerState, wa, flags, localMatrix);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000915}
Hal Canary6f6961e2017-01-31 13:50:44 -0500916#endif