jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
jvanverth | 8ed3b9a | 2015-04-09 08:00:49 -0700 | [diff] [blame] | 8 | #include "GrDistanceFieldGeoProc.h" |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 9 | #include "GrAtlasedShaderHelpers.h" |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 10 | #include "GrCaps.h" |
| 11 | #include "GrShaderCaps.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 12 | #include "GrTexture.h" |
| 13 | #include "SkDistanceFieldGen.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLGeometryProcessor.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 16 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 17 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 18 | #include "glsl/GrGLSLUtil.h" |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 19 | #include "glsl/GrGLSLVarying.h" |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 20 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 21 | |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 22 | // Assuming a radius of a little less than the diagonal of the fragment |
jvanverth | 24ba008 | 2015-03-19 11:34:13 -0700 | [diff] [blame] | 23 | #define SK_DistanceFieldAAFactor "0.65" |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 24 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 25 | class GrGLDistanceFieldA8TextGeoProc : public GrGLSLGeometryProcessor { |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 26 | public: |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 27 | GrGLDistanceFieldA8TextGeoProc() = default; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 28 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{ |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 30 | const GrDistanceFieldA8TextGeoProc& dfTexEffect = |
| 31 | args.fGP.cast<GrDistanceFieldA8TextGeoProc>(); |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 32 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 33 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 34 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 35 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 36 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 37 | |
| 38 | // emit attributes |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 39 | varyingHandler->emitAttributes(dfTexEffect); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 40 | |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 41 | const char* atlasSizeInvName; |
| 42 | fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 43 | kFloat2_GrSLType, |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 44 | kHigh_GrSLPrecision, |
| 45 | "AtlasSizeInv", |
| 46 | &atlasSizeInvName); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 47 | #ifdef SK_GAMMA_APPLY_TO_A8 |
| 48 | // adjust based on gamma |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 49 | const char* distanceAdjustUniName = nullptr; |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 50 | // width, height, 1/(3*width) |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 51 | fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 52 | "DistanceAdjust", &distanceAdjustUniName); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 53 | #endif |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 54 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 55 | // Setup pass through color |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 56 | varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor); |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 57 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 58 | // Setup position |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 59 | gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 60 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 61 | // emit transforms |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 62 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 63 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 64 | uniformHandler, |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 65 | dfTexEffect.inPosition().asShaderVar(), |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 66 | dfTexEffect.localMatrix(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 67 | args.fFPCoordTransformHandler); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 68 | |
jvanverth | bb4a1cf | 2015-04-07 09:06:00 -0700 | [diff] [blame] | 69 | // add varyings |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 70 | GrGLSLVarying uv(kFloat2_GrSLType); |
Jim Van Verth | dfc738b | 2018-11-19 17:15:27 +0000 | [diff] [blame^] | 71 | GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType; |
| 72 | GrGLSLVarying texIdx(texIdxType); |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 73 | GrGLSLVarying st(kFloat2_GrSLType); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 74 | append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv, |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 75 | &texIdx, &st); |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 76 | |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 77 | bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) == |
| 78 | kUniformScale_DistanceFieldEffectMask; |
jvanverth | bb4a1cf | 2015-04-07 09:06:00 -0700 | [diff] [blame] | 79 | bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag); |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 80 | bool isGammaCorrect = |
| 81 | SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag); |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 82 | bool isAliased = |
| 83 | SkToBool(dfTexEffect.getFlags() & kAliased_DistanceFieldEffectFlag); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 84 | |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame] | 85 | // Use highp to work around aliasing issues |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 86 | fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn()); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 87 | fragBuilder->codeAppend("half4 texColor;"); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 88 | append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(), |
| 89 | texIdx, "uv", "texColor"); |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame] | 90 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 91 | fragBuilder->codeAppend("half distance = " |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 92 | SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");"); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 93 | #ifdef SK_GAMMA_APPLY_TO_A8 |
| 94 | // adjust width based on gamma |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 95 | fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 96 | #endif |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 97 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 98 | fragBuilder->codeAppend("half afwidth;"); |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 99 | if (isUniformScale) { |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 100 | // For uniform scale, we adjust for the effect of the transformation on the distance |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 101 | // by using the length of the gradient of the t coordinate in the y direction. |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 102 | // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space. |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 103 | |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 104 | // this gives us a smooth step across approximately one fragment |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 105 | #ifdef SK_VULKAN |
| 106 | fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));", |
| 107 | st.fsIn()); |
| 108 | #else |
| 109 | // We use the y gradient because there is a bug in the Mali 400 in the x direction. |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 110 | fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));", |
| 111 | st.fsIn()); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 112 | #endif |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 113 | } else if (isSimilarity) { |
| 114 | // For similarity transform, we adjust the effect of the transformation on the distance |
| 115 | // by using the length of the gradient of the texture coordinates. We use st coordinates |
| 116 | // to ensure we're mapping 1:1 from texel space to pixel space. |
| 117 | // We use the y gradient because there is a bug in the Mali 400 in the x direction. |
| 118 | |
| 119 | // this gives us a smooth step across approximately one fragment |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 120 | #ifdef SK_VULKAN |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 121 | fragBuilder->codeAppendf("half st_grad_len = length(dFdx(%s));", st.fsIn()); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 122 | #else |
| 123 | // We use the y gradient because there is a bug in the Mali 400 in the x direction. |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 124 | fragBuilder->codeAppendf("half st_grad_len = length(dFdy(%s));", st.fsIn()); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 125 | #endif |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 126 | fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);"); |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 127 | } else { |
| 128 | // For general transforms, to determine the amount of correction we multiply a unit |
| 129 | // vector pointing along the SDF gradient direction by the Jacobian of the st coords |
| 130 | // (which is the inverse transform for this fragment) and take the length of the result. |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 131 | fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), dFdy(distance));"); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 132 | // the length of the gradient may be 0, so we need to check for this |
| 133 | // this also compensates for the Adreno, which likes to drop tiles on division by 0 |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 134 | fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 135 | fragBuilder->codeAppend("if (dg_len2 < 0.0001) {"); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 136 | fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 137 | fragBuilder->codeAppend("} else {"); |
| 138 | fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);"); |
| 139 | fragBuilder->codeAppend("}"); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 140 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 141 | fragBuilder->codeAppendf("half2 Jdx = dFdx(%s);", st.fsIn()); |
| 142 | fragBuilder->codeAppendf("half2 Jdy = dFdy(%s);", st.fsIn()); |
| 143 | fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 144 | fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);"); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 145 | |
| 146 | // this gives us a smooth step across approximately one fragment |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 147 | fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);"); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 148 | } |
brianosman | 0586f5c | 2016-04-12 12:48:21 -0700 | [diff] [blame] | 149 | |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 150 | if (isAliased) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 151 | fragBuilder->codeAppend("half val = distance > 0 ? 1.0 : 0.0;"); |
Jim Van Verth | 90e89b3 | 2017-07-06 16:36:55 -0400 | [diff] [blame] | 152 | } else if (isGammaCorrect) { |
| 153 | // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are |
| 154 | // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want |
| 155 | // distance mapped linearly to coverage, so use a linear step: |
brianosman | 0586f5c | 2016-04-12 12:48:21 -0700 | [diff] [blame] | 156 | fragBuilder->codeAppend( |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 157 | "half val = saturate((distance + afwidth) / (2.0 * afwidth));"); |
brianosman | 0586f5c | 2016-04-12 12:48:21 -0700 | [diff] [blame] | 158 | } else { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 159 | fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);"); |
brianosman | 0586f5c | 2016-04-12 12:48:21 -0700 | [diff] [blame] | 160 | } |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 161 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 162 | fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 163 | } |
| 164 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 165 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc, |
| 166 | FPCoordTransformIter&& transformIter) override { |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 167 | const GrDistanceFieldA8TextGeoProc& dfa8gp = proc.cast<GrDistanceFieldA8TextGeoProc>(); |
| 168 | |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 169 | #ifdef SK_GAMMA_APPLY_TO_A8 |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 170 | float distanceAdjust = dfa8gp.getDistanceAdjust(); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 171 | if (distanceAdjust != fDistanceAdjust) { |
Robert Phillips | e253831 | 2017-08-24 17:47:23 +0000 | [diff] [blame] | 172 | fDistanceAdjust = distanceAdjust; |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 173 | pdman.set1f(fDistanceAdjustUni, distanceAdjust); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 174 | } |
| 175 | #endif |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 176 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 177 | const SkISize& atlasSize = dfa8gp.atlasSize(); |
| 178 | SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight)); |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 179 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 180 | if (fAtlasSize != atlasSize) { |
| 181 | pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight); |
| 182 | fAtlasSize = atlasSize; |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 183 | } |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 184 | this->setTransformDataHelper(dfa8gp.localMatrix(), pdman, &transformIter); |
commit-bot@chromium.org | 8fe2ee1 | 2014-03-26 18:03:05 +0000 | [diff] [blame] | 185 | } |
| 186 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 187 | static inline void GenKey(const GrGeometryProcessor& gp, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 188 | const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 189 | GrProcessorKeyBuilder* b) { |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 190 | const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 191 | uint32_t key = dfTexEffect.getFlags(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 192 | b->add32(key); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 193 | b->add32(dfTexEffect.numTextureSamplers()); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 194 | } |
| 195 | |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 196 | private: |
mtklein | 50282b4 | 2015-01-22 07:59:52 -0800 | [diff] [blame] | 197 | #ifdef SK_GAMMA_APPLY_TO_A8 |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 198 | float fDistanceAdjust = -1.f; |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 199 | UniformHandle fDistanceAdjustUni; |
mtklein | 50282b4 | 2015-01-22 07:59:52 -0800 | [diff] [blame] | 200 | #endif |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 201 | SkISize fAtlasSize = {0, 0}; |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 202 | UniformHandle fAtlasSizeInvUniform; |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 203 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 204 | typedef GrGLSLGeometryProcessor INHERITED; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | /////////////////////////////////////////////////////////////////////////////// |
| 208 | |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 209 | GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(const GrShaderCaps& caps, |
| 210 | const sk_sp<GrTextureProxy>* proxies, |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 211 | int numProxies, |
| 212 | const GrSamplerState& params, |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 213 | #ifdef SK_GAMMA_APPLY_TO_A8 |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 214 | float distanceAdjust, |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 215 | #endif |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 216 | uint32_t flags, |
| 217 | const SkMatrix& localMatrix) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 218 | : INHERITED(kGrDistanceFieldA8TextGeoProc_ClassID) |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 219 | , fLocalMatrix(localMatrix) |
| 220 | , fFlags(flags & kNonLCD_DistanceFieldEffectMask) |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 221 | #ifdef SK_GAMMA_APPLY_TO_A8 |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 222 | , fDistanceAdjust(distanceAdjust) |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 223 | #endif |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 224 | { |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 225 | SkASSERT(numProxies <= kMaxTextures); |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 226 | SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask)); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 227 | |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 228 | if (flags & kPerspective_DistanceFieldEffectFlag) { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 229 | fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType}; |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 230 | } else { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 231 | fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 232 | } |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 233 | fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType }; |
Jim Van Verth | dfc738b | 2018-11-19 17:15:27 +0000 | [diff] [blame^] | 234 | fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType, |
| 235 | caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType}; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 236 | this->setVertexAttributes(&fInPosition, 3); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 237 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 238 | if (numProxies) { |
| 239 | fAtlasSize = proxies[0]->isize(); |
| 240 | } |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 241 | for (int i = 0; i < numProxies; ++i) { |
| 242 | SkASSERT(proxies[i]); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 243 | SkASSERT(proxies[i]->isize() == fAtlasSize); |
| 244 | fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 245 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 246 | this->setTextureSamplerCnt(numProxies); |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 247 | } |
| 248 | |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 249 | void GrDistanceFieldA8TextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies, |
| 250 | int numProxies, |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 251 | const GrSamplerState& params) { |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 252 | SkASSERT(numProxies <= kMaxTextures); |
| 253 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 254 | if (!fTextureSamplers[0].isInitialized()) { |
| 255 | fAtlasSize = proxies[0]->isize(); |
| 256 | } |
| 257 | |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 258 | for (int i = 0; i < numProxies; ++i) { |
| 259 | SkASSERT(proxies[i]); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 260 | SkASSERT(proxies[i]->isize() == fAtlasSize); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 261 | if (!fTextureSamplers[i].isInitialized()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 262 | fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 263 | } |
| 264 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 265 | this->setTextureSamplerCnt(numProxies); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 266 | } |
| 267 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 268 | void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 269 | GrProcessorKeyBuilder* b) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 270 | GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 273 | GrGLSLPrimitiveProcessor* |
| 274 | GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrShaderCaps&) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 275 | return new GrGLDistanceFieldA8TextGeoProc(); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /////////////////////////////////////////////////////////////////////////////// |
| 279 | |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 280 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 281 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 282 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 283 | sk_sp<GrGeometryProcessor> GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) { |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 284 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx |
| 285 | : GrProcessorUnitTest::kAlphaTextureIdx; |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 286 | sk_sp<GrTextureProxy> proxies[kMaxTextures] = { |
| 287 | d->textureProxy(texIdx), |
| 288 | nullptr, |
| 289 | nullptr, |
| 290 | nullptr |
| 291 | }; |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 292 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 293 | GrSamplerState::WrapMode wrapModes[2]; |
| 294 | GrTest::TestWrapModes(d->fRandom, wrapModes); |
| 295 | GrSamplerState samplerState(wrapModes, d->fRandom->nextBool() |
| 296 | ? GrSamplerState::Filter::kBilerp |
| 297 | : GrSamplerState::Filter::kNearest); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 298 | |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 299 | uint32_t flags = 0; |
| 300 | flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0; |
| 301 | if (flags & kSimilarity_DistanceFieldEffectFlag) { |
| 302 | flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0; |
| 303 | } |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 304 | SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom); |
Mike Reed | e1367b4 | 2017-12-15 14:28:56 -0500 | [diff] [blame] | 305 | #ifdef SK_GAMMA_APPLY_TO_A8 |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 306 | float lum = d->fRandom->nextF(); |
Mike Reed | e1367b4 | 2017-12-15 14:28:56 -0500 | [diff] [blame] | 307 | #endif |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 308 | return GrDistanceFieldA8TextGeoProc::Make(*d->caps()->shaderCaps(), |
| 309 | proxies, 1, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 310 | samplerState, |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 311 | #ifdef SK_GAMMA_APPLY_TO_A8 |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 312 | lum, |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 313 | #endif |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 314 | flags, localMatrix); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 315 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 316 | #endif |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 317 | |
| 318 | /////////////////////////////////////////////////////////////////////////////// |
| 319 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 320 | class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 321 | public: |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 322 | GrGLDistanceFieldPathGeoProc() |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 323 | : fMatrix(SkMatrix::InvalidMatrix()) |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 324 | , fAtlasSize({0,0}) { |
| 325 | } |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 326 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 327 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{ |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 328 | const GrDistanceFieldPathGeoProc& dfPathEffect = |
| 329 | args.fGP.cast<GrDistanceFieldPathGeoProc>(); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 330 | |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 331 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 332 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 333 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 334 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 335 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 336 | |
| 337 | // emit attributes |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 338 | varyingHandler->emitAttributes(dfPathEffect); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 339 | |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 340 | const char* atlasSizeInvName; |
| 341 | fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 342 | kFloat2_GrSLType, |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 343 | kHigh_GrSLPrecision, |
| 344 | "AtlasSizeInv", |
| 345 | &atlasSizeInvName); |
| 346 | |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 347 | GrGLSLVarying uv(kFloat2_GrSLType); |
Jim Van Verth | dfc738b | 2018-11-19 17:15:27 +0000 | [diff] [blame^] | 348 | GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType; |
| 349 | GrGLSLVarying texIdx(texIdxType); |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 350 | GrGLSLVarying st(kFloat2_GrSLType); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 351 | append_index_uv_varyings(args, dfPathEffect.inTextureCoords().name(), atlasSizeInvName, &uv, |
| 352 | &texIdx, &st); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 353 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 354 | // setup pass through color |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 355 | varyingHandler->addPassThroughAttribute(dfPathEffect.inColor(), args.fOutputColor); |
reed | e4ef1ca | 2015-02-17 18:38:38 -0800 | [diff] [blame] | 356 | |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 357 | if (dfPathEffect.matrix().hasPerspective()) { |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 358 | // Setup position |
| 359 | this->writeOutputPosition(vertBuilder, |
| 360 | uniformHandler, |
| 361 | gpArgs, |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 362 | dfPathEffect.inPosition().name(), |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 363 | dfPathEffect.matrix(), |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 364 | &fMatrixUniform); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 365 | |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 366 | // emit transforms |
| 367 | this->emitTransforms(vertBuilder, |
| 368 | varyingHandler, |
| 369 | uniformHandler, |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 370 | dfPathEffect.inPosition().asShaderVar(), |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 371 | args.fFPCoordTransformHandler); |
| 372 | } else { |
| 373 | // Setup position |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 374 | this->writeOutputPosition(vertBuilder, gpArgs, dfPathEffect.inPosition().name()); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 375 | |
| 376 | // emit transforms |
| 377 | this->emitTransforms(vertBuilder, |
| 378 | varyingHandler, |
| 379 | uniformHandler, |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 380 | dfPathEffect.inPosition().asShaderVar(), |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 381 | dfPathEffect.matrix(), |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 382 | args.fFPCoordTransformHandler); |
| 383 | } |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 384 | |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame] | 385 | // Use highp to work around aliasing issues |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 386 | fragBuilder->codeAppendf("float2 uv = %s;", uv.fsIn()); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 387 | fragBuilder->codeAppend("half4 texColor;"); |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 388 | append_multitexture_lookup(args, dfPathEffect.numTextureSamplers(), texIdx, "uv", |
| 389 | "texColor"); |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame] | 390 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 391 | fragBuilder->codeAppend("half distance = " |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 392 | SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");"); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 393 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 394 | fragBuilder->codeAppend("half afwidth;"); |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 395 | bool isUniformScale = (dfPathEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) == |
| 396 | kUniformScale_DistanceFieldEffectMask; |
| 397 | bool isSimilarity = SkToBool(dfPathEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag); |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 398 | bool isGammaCorrect = |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 399 | SkToBool(dfPathEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag); |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 400 | if (isUniformScale) { |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 401 | // For uniform scale, we adjust for the effect of the transformation on the distance |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 402 | // by using the length of the gradient of the t coordinate in the y direction. |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 403 | // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space. |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 404 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 405 | // this gives us a smooth step across approximately one fragment |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 406 | #ifdef SK_VULKAN |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 407 | fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));", |
| 408 | st.fsIn()); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 409 | #else |
| 410 | // We use the y gradient because there is a bug in the Mali 400 in the x direction. |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 411 | fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));", |
| 412 | st.fsIn()); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 413 | #endif |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 414 | } else if (isSimilarity) { |
| 415 | // For similarity transform, we adjust the effect of the transformation on the distance |
| 416 | // by using the length of the gradient of the texture coordinates. We use st coordinates |
| 417 | // to ensure we're mapping 1:1 from texel space to pixel space. |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 418 | |
| 419 | // this gives us a smooth step across approximately one fragment |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 420 | #ifdef SK_VULKAN |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 421 | fragBuilder->codeAppendf("half st_grad_len = length(dFdx(%s));", st.fsIn()); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 422 | #else |
| 423 | // We use the y gradient because there is a bug in the Mali 400 in the x direction. |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 424 | fragBuilder->codeAppendf("half st_grad_len = length(dFdy(%s));", st.fsIn()); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 425 | #endif |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 426 | fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);"); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 427 | } else { |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 428 | // For general transforms, to determine the amount of correction we multiply a unit |
| 429 | // vector pointing along the SDF gradient direction by the Jacobian of the st coords |
| 430 | // (which is the inverse transform for this fragment) and take the length of the result. |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 431 | fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), dFdy(distance));"); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 432 | // the length of the gradient may be 0, so we need to check for this |
| 433 | // this also compensates for the Adreno, which likes to drop tiles on division by 0 |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 434 | fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 435 | fragBuilder->codeAppend("if (dg_len2 < 0.0001) {"); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 436 | fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 437 | fragBuilder->codeAppend("} else {"); |
| 438 | fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);"); |
| 439 | fragBuilder->codeAppend("}"); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 440 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 441 | fragBuilder->codeAppendf("half2 Jdx = dFdx(%s);", st.fsIn()); |
| 442 | fragBuilder->codeAppendf("half2 Jdy = dFdy(%s);", st.fsIn()); |
| 443 | fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,"); |
| 444 | fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);"); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 445 | |
| 446 | // this gives us a smooth step across approximately one fragment |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 447 | fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);"); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 448 | } |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 449 | // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are |
| 450 | // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance |
| 451 | // mapped linearly to coverage, so use a linear step: |
| 452 | if (isGammaCorrect) { |
| 453 | fragBuilder->codeAppend( |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 454 | "half val = saturate((distance + afwidth) / (2.0 * afwidth));"); |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 455 | } else { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 456 | fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);"); |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 457 | } |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 458 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 459 | fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 460 | } |
| 461 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 462 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc, |
| 463 | FPCoordTransformIter&& transformIter) override { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 464 | |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 465 | const GrDistanceFieldPathGeoProc& dfpgp = proc.cast<GrDistanceFieldPathGeoProc>(); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 466 | |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 467 | if (dfpgp.matrix().hasPerspective() && !fMatrix.cheapEqualTo(dfpgp.matrix())) { |
| 468 | fMatrix = dfpgp.matrix(); |
| 469 | float matrix[3 * 3]; |
| 470 | GrGLSLGetMatrix<3>(matrix, fMatrix); |
| 471 | pdman.setMatrix3f(fMatrixUniform, matrix); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 472 | } |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 473 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 474 | const SkISize& atlasSize = dfpgp.atlasSize(); |
| 475 | SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight)); |
| 476 | if (fAtlasSize != atlasSize) { |
| 477 | pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight); |
| 478 | fAtlasSize = atlasSize; |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 479 | } |
| 480 | |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 481 | if (dfpgp.matrix().hasPerspective()) { |
| 482 | this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter); |
| 483 | } else { |
| 484 | this->setTransformDataHelper(dfpgp.matrix(), pdman, &transformIter); |
| 485 | } |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 486 | } |
| 487 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 488 | static inline void GenKey(const GrGeometryProcessor& gp, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 489 | const GrShaderCaps&, |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 490 | GrProcessorKeyBuilder* b) { |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 491 | const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>(); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 492 | |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 493 | uint32_t key = dfTexEffect.getFlags(); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 494 | key |= ComputePosKey(dfTexEffect.matrix()) << 16; |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 495 | b->add32(key); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 496 | b->add32(dfTexEffect.matrix().hasPerspective()); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 497 | b->add32(dfTexEffect.numTextureSamplers()); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | private: |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 501 | SkMatrix fMatrix; // view matrix if perspective, local matrix otherwise |
| 502 | UniformHandle fMatrixUniform; |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 503 | |
| 504 | SkISize fAtlasSize; |
| 505 | UniformHandle fAtlasSizeInvUniform; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 506 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 507 | typedef GrGLSLGeometryProcessor INHERITED; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 508 | }; |
| 509 | |
| 510 | /////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 511 | |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 512 | GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(const GrShaderCaps& caps, |
| 513 | const SkMatrix& matrix, |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 514 | const sk_sp<GrTextureProxy>* proxies, |
| 515 | int numProxies, |
| 516 | const GrSamplerState& params, |
| 517 | uint32_t flags) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 518 | : INHERITED(kGrDistanceFieldPathGeoProc_ClassID) |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 519 | , fMatrix(matrix) |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 520 | , fFlags(flags & kNonLCD_DistanceFieldEffectMask) { |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 521 | SkASSERT(numProxies <= kMaxTextures); |
Mike Klein | 5045e50 | 2018-06-19 01:40:57 +0000 | [diff] [blame] | 522 | SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask)); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 523 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 524 | fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
| 525 | fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType}; |
Jim Van Verth | dfc738b | 2018-11-19 17:15:27 +0000 | [diff] [blame^] | 526 | fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType, |
| 527 | caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType}; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 528 | this->setVertexAttributes(&fInPosition, 3); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 529 | |
| 530 | if (numProxies) { |
| 531 | fAtlasSize = proxies[0]->isize(); |
| 532 | } |
| 533 | |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 534 | for (int i = 0; i < numProxies; ++i) { |
| 535 | SkASSERT(proxies[i]); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 536 | SkASSERT(proxies[i]->isize() == fAtlasSize); |
| 537 | fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 538 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 539 | this->setTextureSamplerCnt(numProxies); |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 540 | } |
| 541 | |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 542 | void GrDistanceFieldPathGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies, |
| 543 | int numProxies, |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 544 | const GrSamplerState& params) { |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 545 | SkASSERT(numProxies <= kMaxTextures); |
| 546 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 547 | if (!fTextureSamplers[0].isInitialized()) { |
| 548 | fAtlasSize = proxies[0]->isize(); |
| 549 | } |
| 550 | |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 551 | for (int i = 0; i < numProxies; ++i) { |
| 552 | SkASSERT(proxies[i]); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 553 | SkASSERT(proxies[i]->isize() == fAtlasSize); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 554 | |
| 555 | if (!fTextureSamplers[i].isInitialized()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 556 | fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 557 | } |
| 558 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 559 | this->setTextureSamplerCnt(numProxies); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 560 | } |
| 561 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 562 | void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 563 | GrProcessorKeyBuilder* b) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 564 | GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 567 | GrGLSLPrimitiveProcessor* |
| 568 | GrDistanceFieldPathGeoProc::createGLSLInstance(const GrShaderCaps&) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 569 | return new GrGLDistanceFieldPathGeoProc(); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /////////////////////////////////////////////////////////////////////////////// |
| 573 | |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 574 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 575 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 576 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 577 | sk_sp<GrGeometryProcessor> GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 578 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx |
| 579 | : GrProcessorUnitTest::kAlphaTextureIdx; |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 580 | sk_sp<GrTextureProxy> proxies[kMaxTextures] = { |
| 581 | d->textureProxy(texIdx), |
| 582 | nullptr, |
| 583 | nullptr, |
| 584 | nullptr |
| 585 | }; |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 586 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 587 | GrSamplerState::WrapMode wrapModes[2]; |
| 588 | GrTest::TestWrapModes(d->fRandom, wrapModes); |
| 589 | GrSamplerState samplerState(wrapModes, d->fRandom->nextBool() |
| 590 | ? GrSamplerState::Filter::kBilerp |
| 591 | : GrSamplerState::Filter::kNearest); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 592 | |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 593 | uint32_t flags = 0; |
| 594 | flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0; |
| 595 | if (flags & kSimilarity_DistanceFieldEffectFlag) { |
| 596 | flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0; |
| 597 | } |
| 598 | |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 599 | return GrDistanceFieldPathGeoProc::Make(*d->caps()->shaderCaps(), |
| 600 | GrTest::TestMatrix(d->fRandom), |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 601 | proxies, 1, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 602 | samplerState, |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 603 | flags); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 604 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 605 | #endif |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 606 | |
| 607 | /////////////////////////////////////////////////////////////////////////////// |
| 608 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 609 | class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor { |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 610 | public: |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 611 | GrGLDistanceFieldLCDTextGeoProc() : fAtlasSize({0, 0}) { |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 612 | fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 613 | } |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 614 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 615 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{ |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 616 | const GrDistanceFieldLCDTextGeoProc& dfTexEffect = |
| 617 | args.fGP.cast<GrDistanceFieldLCDTextGeoProc>(); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 618 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 619 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 620 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 621 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 622 | |
| 623 | // emit attributes |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 624 | varyingHandler->emitAttributes(dfTexEffect); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 625 | |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 626 | const char* atlasSizeInvName; |
| 627 | fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 628 | kFloat2_GrSLType, |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 629 | kHigh_GrSLPrecision, |
| 630 | "AtlasSizeInv", |
| 631 | &atlasSizeInvName); |
| 632 | |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 633 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 634 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 635 | // setup pass through color |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 636 | varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 637 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 638 | // Setup position |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 639 | gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar(); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 640 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 641 | // emit transforms |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 642 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 643 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 644 | uniformHandler, |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 645 | dfTexEffect.inPosition().asShaderVar(), |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 646 | dfTexEffect.localMatrix(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 647 | args.fFPCoordTransformHandler); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 648 | |
jvanverth | bb4a1cf | 2015-04-07 09:06:00 -0700 | [diff] [blame] | 649 | // set up varyings |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 650 | GrGLSLVarying uv(kFloat2_GrSLType); |
Jim Van Verth | dfc738b | 2018-11-19 17:15:27 +0000 | [diff] [blame^] | 651 | GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType; |
| 652 | GrGLSLVarying texIdx(texIdxType); |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 653 | GrGLSLVarying st(kFloat2_GrSLType); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 654 | append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv, |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 655 | &texIdx, &st); |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 656 | |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 657 | GrGLSLVarying delta(kFloat_GrSLType); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 658 | varyingHandler->addVarying("Delta", &delta); |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 659 | if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) { |
| 660 | vertBuilder->codeAppendf("%s = -%s.x/3.0;", delta.vsOut(), atlasSizeInvName); |
| 661 | } else { |
| 662 | vertBuilder->codeAppendf("%s = %s.x/3.0;", delta.vsOut(), atlasSizeInvName); |
| 663 | } |
| 664 | |
| 665 | // add frag shader code |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 666 | bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) == |
| 667 | kUniformScale_DistanceFieldEffectMask; |
| 668 | bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag); |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 669 | bool isGammaCorrect = |
| 670 | SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 671 | |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 672 | // create LCD offset adjusted by inverse of transform |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame] | 673 | // Use highp to work around aliasing issues |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 674 | fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn()); |
joshualitt | 922c8b1 | 2015-08-07 09:55:23 -0700 | [diff] [blame] | 675 | |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 676 | if (isUniformScale) { |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 677 | #ifdef SK_VULKAN |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 678 | fragBuilder->codeAppendf("half st_grad_len = abs(dFdx(%s.x));", st.fsIn()); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 679 | #else |
| 680 | // We use the y gradient because there is a bug in the Mali 400 in the x direction. |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 681 | fragBuilder->codeAppendf("half st_grad_len = abs(dFdy(%s.y));", st.fsIn()); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 682 | #endif |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 683 | fragBuilder->codeAppendf("half2 offset = half2(st_grad_len*%s, 0.0);", delta.fsIn()); |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 684 | } else if (isSimilarity) { |
| 685 | // For a similarity matrix with rotation, the gradient will not be aligned |
| 686 | // with the texel coordinate axes, so we need to calculate it. |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 687 | #ifdef SK_VULKAN |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 688 | fragBuilder->codeAppendf("half2 st_grad = dFdx(%s);", st.fsIn()); |
| 689 | fragBuilder->codeAppendf("half2 offset = %s*st_grad;", delta.fsIn()); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 690 | #else |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 691 | // We use dFdy because of a Mali 400 bug, and rotate -90 degrees to |
| 692 | // get the gradient in the x direction. |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 693 | fragBuilder->codeAppendf("half2 st_grad = dFdy(%s);", st.fsIn()); |
| 694 | fragBuilder->codeAppendf("half2 offset = %s*half2(st_grad.y, -st_grad.x);", |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 695 | delta.fsIn()); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 696 | #endif |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 697 | fragBuilder->codeAppend("half st_grad_len = length(st_grad);"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 698 | } else { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 699 | fragBuilder->codeAppendf("half2 st = %s;\n", st.fsIn()); |
jvanverth | bb4a1cf | 2015-04-07 09:06:00 -0700 | [diff] [blame] | 700 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 701 | fragBuilder->codeAppend("half2 Jdx = dFdx(st);"); |
| 702 | fragBuilder->codeAppend("half2 Jdy = dFdy(st);"); |
| 703 | fragBuilder->codeAppendf("half2 offset = %s*Jdx;", delta.fsIn()); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 706 | // sample the texture by index |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 707 | fragBuilder->codeAppend("half4 texColor;"); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 708 | append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(), |
| 709 | texIdx, "uv", "texColor"); |
Jim Van Verth | 6c251d2 | 2017-09-08 12:29:07 -0400 | [diff] [blame] | 710 | |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 711 | // green is distance to uv center |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 712 | fragBuilder->codeAppend("half3 distance;"); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 713 | fragBuilder->codeAppend("distance.y = texColor.r;"); |
| 714 | // red is distance to left offset |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 715 | fragBuilder->codeAppend("half2 uv_adjusted = uv - offset;"); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 716 | append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(), |
| 717 | texIdx, "uv_adjusted", "texColor"); |
| 718 | fragBuilder->codeAppend("distance.x = texColor.r;"); |
| 719 | // blue is distance to right offset |
| 720 | fragBuilder->codeAppend("uv_adjusted = uv + offset;"); |
| 721 | append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(), |
| 722 | texIdx, "uv_adjusted", "texColor"); |
| 723 | fragBuilder->codeAppend("distance.z = texColor.r;"); |
| 724 | |
| 725 | fragBuilder->codeAppend("distance = " |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 726 | "half3(" SK_DistanceFieldMultiplier ")*(distance - half3(" SK_DistanceFieldThreshold"));"); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 727 | |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 728 | // adjust width based on gamma |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 729 | const char* distanceAdjustUniName = nullptr; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 730 | fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 731 | "DistanceAdjust", &distanceAdjustUniName); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 732 | fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 733 | |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 734 | // To be strictly correct, we should compute the anti-aliasing factor separately |
| 735 | // for each color component. However, this is only important when using perspective |
| 736 | // transformations, and even then using a single factor seems like a reasonable |
| 737 | // trade-off between quality and speed. |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 738 | fragBuilder->codeAppend("half afwidth;"); |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 739 | if (isSimilarity) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 740 | // For similarity transform (uniform scale-only is a subset of this), we adjust for the |
| 741 | // effect of the transformation on the distance by using the length of the gradient of |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 742 | // the texture coordinates. We use st coordinates to ensure we're mapping 1:1 from texel |
| 743 | // space to pixel space. |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 744 | |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 745 | // this gives us a smooth step across approximately one fragment |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 746 | fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*st_grad_len;"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 747 | } else { |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 748 | // For general transforms, to determine the amount of correction we multiply a unit |
| 749 | // vector pointing along the SDF gradient direction by the Jacobian of the st coords |
| 750 | // (which is the inverse transform for this fragment) and take the length of the result. |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 751 | fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance.r), dFdy(distance.r));"); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 752 | // the length of the gradient may be 0, so we need to check for this |
| 753 | // this also compensates for the Adreno, which likes to drop tiles on division by 0 |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 754 | fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 755 | fragBuilder->codeAppend("if (dg_len2 < 0.0001) {"); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 756 | fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 757 | fragBuilder->codeAppend("} else {"); |
| 758 | fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);"); |
| 759 | fragBuilder->codeAppend("}"); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 760 | fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 761 | fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 762 | |
| 763 | // this gives us a smooth step across approximately one fragment |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 764 | fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 765 | } |
| 766 | |
brianosman | 0586f5c | 2016-04-12 12:48:21 -0700 | [diff] [blame] | 767 | // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are |
| 768 | // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance |
| 769 | // mapped linearly to coverage, so use a linear step: |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 770 | if (isGammaCorrect) { |
Greg Daniel | 5592382 | 2017-05-22 16:34:34 -0400 | [diff] [blame] | 771 | fragBuilder->codeAppendf("%s = " |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 772 | "half4(saturate((distance + half3(afwidth)) / half3(2.0 * afwidth)), 1.0);", |
Greg Daniel | 5592382 | 2017-05-22 16:34:34 -0400 | [diff] [blame] | 773 | args.fOutputCoverage); |
brianosman | 0586f5c | 2016-04-12 12:48:21 -0700 | [diff] [blame] | 774 | } else { |
Greg Daniel | 5592382 | 2017-05-22 16:34:34 -0400 | [diff] [blame] | 775 | fragBuilder->codeAppendf( |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 776 | "%s = half4(smoothstep(half3(-afwidth), half3(afwidth), distance), 1.0);", |
Greg Daniel | 5592382 | 2017-05-22 16:34:34 -0400 | [diff] [blame] | 777 | args.fOutputCoverage); |
brianosman | 0586f5c | 2016-04-12 12:48:21 -0700 | [diff] [blame] | 778 | } |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 779 | } |
| 780 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 781 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& processor, |
| 782 | FPCoordTransformIter&& transformIter) override { |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 783 | SkASSERT(fDistanceAdjustUni.isValid()); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 784 | |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 785 | const GrDistanceFieldLCDTextGeoProc& dflcd = processor.cast<GrDistanceFieldLCDTextGeoProc>(); |
| 786 | GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust(); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 787 | if (wa != fDistanceAdjust) { |
| 788 | pdman.set3f(fDistanceAdjustUni, |
| 789 | wa.fR, |
| 790 | wa.fG, |
| 791 | wa.fB); |
| 792 | fDistanceAdjust = wa; |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 793 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 794 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 795 | const SkISize& atlasSize = dflcd.atlasSize(); |
| 796 | SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight)); |
| 797 | if (fAtlasSize != atlasSize) { |
| 798 | pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight); |
| 799 | fAtlasSize = atlasSize; |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 800 | } |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 801 | this->setTransformDataHelper(dflcd.localMatrix(), pdman, &transformIter); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 802 | } |
| 803 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 804 | static inline void GenKey(const GrGeometryProcessor& gp, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 805 | const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 806 | GrProcessorKeyBuilder* b) { |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 807 | const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>(); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 808 | |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 809 | uint32_t key = dfTexEffect.getFlags(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 810 | b->add32(key); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 811 | b->add32(dfTexEffect.numTextureSamplers()); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | private: |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 815 | GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust; |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 816 | UniformHandle fDistanceAdjustUni; |
| 817 | |
| 818 | SkISize fAtlasSize; |
| 819 | UniformHandle fAtlasSizeInvUniform; |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 820 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 821 | typedef GrGLSLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 822 | }; |
| 823 | |
| 824 | /////////////////////////////////////////////////////////////////////////////// |
Mike Klein | 5045e50 | 2018-06-19 01:40:57 +0000 | [diff] [blame] | 825 | |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 826 | GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(const GrShaderCaps& caps, |
| 827 | const sk_sp<GrTextureProxy>* proxies, |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 828 | int numProxies, |
| 829 | const GrSamplerState& params, |
| 830 | DistanceAdjust distanceAdjust, |
| 831 | uint32_t flags, |
| 832 | const SkMatrix& localMatrix) |
| 833 | : INHERITED(kGrDistanceFieldLCDTextGeoProc_ClassID) |
| 834 | , fLocalMatrix(localMatrix) |
| 835 | , fDistanceAdjust(distanceAdjust) |
| 836 | , fFlags(flags & kLCD_DistanceFieldEffectMask) { |
| 837 | SkASSERT(numProxies <= kMaxTextures); |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 838 | SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag)); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 839 | |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 840 | if (fFlags & kPerspective_DistanceFieldEffectFlag) { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 841 | fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType}; |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 842 | } else { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 843 | fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 844 | } |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 845 | fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType}; |
Jim Van Verth | dfc738b | 2018-11-19 17:15:27 +0000 | [diff] [blame^] | 846 | fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType, |
| 847 | caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType}; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 848 | this->setVertexAttributes(&fInPosition, 3); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 849 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 850 | if (numProxies) { |
| 851 | fAtlasSize = proxies[0]->isize(); |
| 852 | } |
| 853 | |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 854 | for (int i = 0; i < numProxies; ++i) { |
| 855 | SkASSERT(proxies[i]); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 856 | SkASSERT(proxies[i]->isize() == fAtlasSize); |
| 857 | fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 858 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 859 | this->setTextureSamplerCnt(numProxies); |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 860 | } |
| 861 | |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 862 | void GrDistanceFieldLCDTextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies, |
| 863 | int numProxies, |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 864 | const GrSamplerState& params) { |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 865 | SkASSERT(numProxies <= kMaxTextures); |
| 866 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 867 | if (!fTextureSamplers[0].isInitialized()) { |
| 868 | fAtlasSize = proxies[0]->isize(); |
| 869 | } |
| 870 | |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 871 | for (int i = 0; i < numProxies; ++i) { |
| 872 | SkASSERT(proxies[i]); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 873 | SkASSERT(proxies[i]->isize() == fAtlasSize); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 874 | |
| 875 | if (!fTextureSamplers[i].isInitialized()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 876 | fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 877 | } |
| 878 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 879 | this->setTextureSamplerCnt(numProxies); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 880 | } |
| 881 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 882 | void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 883 | GrProcessorKeyBuilder* b) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 884 | GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 885 | } |
| 886 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 887 | GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrShaderCaps&) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 888 | return new GrGLDistanceFieldLCDTextGeoProc(); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | /////////////////////////////////////////////////////////////////////////////// |
| 892 | |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 893 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 894 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 895 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 896 | sk_sp<GrGeometryProcessor> GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 897 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 898 | GrProcessorUnitTest::kAlphaTextureIdx; |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 899 | sk_sp<GrTextureProxy> proxies[kMaxTextures] = { |
| 900 | d->textureProxy(texIdx), |
| 901 | nullptr, |
| 902 | nullptr, |
| 903 | nullptr |
| 904 | }; |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 905 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 906 | GrSamplerState::WrapMode wrapModes[2]; |
| 907 | GrTest::TestWrapModes(d->fRandom, wrapModes); |
| 908 | GrSamplerState samplerState(wrapModes, d->fRandom->nextBool() |
| 909 | ? GrSamplerState::Filter::kBilerp |
| 910 | : GrSamplerState::Filter::kNearest); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 911 | DistanceAdjust wa = { 0.0f, 0.1f, -0.1f }; |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 912 | uint32_t flags = kUseLCD_DistanceFieldEffectFlag; |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 913 | flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0; |
| 914 | if (flags & kSimilarity_DistanceFieldEffectFlag) { |
| 915 | flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0; |
| 916 | } |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 917 | flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 918 | SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom); |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 919 | return GrDistanceFieldLCDTextGeoProc::Make(*d->caps()->shaderCaps(), proxies, 1, samplerState, |
| 920 | wa, flags, localMatrix); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 921 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 922 | #endif |