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" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 9 | #include "GrInvariantOutput.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 10 | #include "GrTexture.h" |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 11 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 12 | #include "SkDistanceFieldGen.h" |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 13 | |
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" |
| 20 | #include "glsl/GrGLSLVertexShaderBuilder.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: |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 27 | GrGLDistanceFieldA8TextGeoProc() |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 28 | : fViewMatrix(SkMatrix::InvalidMatrix()) |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 29 | #ifdef SK_GAMMA_APPLY_TO_A8 |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 30 | , fDistanceAdjust(-1.0f) |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 31 | #endif |
| 32 | {} |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{ |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 35 | const GrDistanceFieldA8TextGeoProc& dfTexEffect = |
| 36 | args.fGP.cast<GrDistanceFieldA8TextGeoProc>(); |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 37 | GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 38 | SkAssertResult(fragBuilder->enableFeature( |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 39 | GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 40 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 41 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 42 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 43 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 44 | |
| 45 | // emit attributes |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 46 | varyingHandler->emitAttributes(dfTexEffect); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 47 | |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 48 | #ifdef SK_GAMMA_APPLY_TO_A8 |
| 49 | // adjust based on gamma |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 50 | const char* distanceAdjustUniName = nullptr; |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 51 | // width, height, 1/(3*width) |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 52 | fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 53 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 54 | "DistanceAdjust", &distanceAdjustUniName); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 55 | #endif |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 56 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 57 | // Setup pass through color |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 58 | if (!dfTexEffect.colorIgnored()) { |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 59 | varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 60 | } |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 61 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 62 | // Setup position |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 63 | this->setupPosition(vertBuilder, |
| 64 | uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 65 | gpArgs, |
| 66 | dfTexEffect.inPosition()->fName, |
| 67 | dfTexEffect.viewMatrix(), |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 68 | &fViewMatrixUniform); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 69 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 70 | // emit transforms |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 71 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 72 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 73 | uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 74 | gpArgs->fPositionVar, |
| 75 | dfTexEffect.inPosition()->fName, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame^] | 76 | args.fFPCoordTransformHandler); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 77 | |
jvanverth | bb4a1cf | 2015-04-07 09:06:00 -0700 | [diff] [blame] | 78 | // add varyings |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 79 | GrGLSLVertToFrag recipScale(kFloat_GrSLType); |
jvanverth | 7023a00 | 2016-02-22 11:25:32 -0800 | [diff] [blame] | 80 | GrGLSLVertToFrag uv(kVec2f_GrSLType); |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 81 | bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) == |
| 82 | kUniformScale_DistanceFieldEffectMask; |
jvanverth | bb4a1cf | 2015-04-07 09:06:00 -0700 | [diff] [blame] | 83 | bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag); |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 84 | bool isGammaCorrect = |
| 85 | SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag); |
jvanverth | 7023a00 | 2016-02-22 11:25:32 -0800 | [diff] [blame] | 86 | varyingHandler->addVarying("TextureCoords", &uv, kHigh_GrSLPrecision); |
| 87 | vertBuilder->codeAppendf("%s = %s;", uv.vsOut(), dfTexEffect.inTextureCoords()->fName); |
jvanverth | bb4a1cf | 2015-04-07 09:06:00 -0700 | [diff] [blame] | 88 | |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 89 | // compute numbers to be hardcoded to convert texture coordinates from float to int |
joshualitt | 922c8b1 | 2015-08-07 09:55:23 -0700 | [diff] [blame] | 90 | SkASSERT(dfTexEffect.numTextures() == 1); |
| 91 | GrTexture* atlas = dfTexEffect.textureAccess(0).getTexture(); |
joshualitt | 7375d6b | 2015-08-07 13:36:44 -0700 | [diff] [blame] | 92 | SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height())); |
joshualitt | 922c8b1 | 2015-08-07 09:55:23 -0700 | [diff] [blame] | 93 | |
jvanverth | 7023a00 | 2016-02-22 11:25:32 -0800 | [diff] [blame] | 94 | GrGLSLVertToFrag st(kVec2f_GrSLType); |
| 95 | varyingHandler->addVarying("IntTextureCoords", &st, kHigh_GrSLPrecision); |
| 96 | vertBuilder->codeAppendf("%s = vec2(%d, %d) * %s;", st.vsOut(), |
| 97 | atlas->width(), atlas->height(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 98 | dfTexEffect.inTextureCoords()->fName); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 99 | |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame] | 100 | // Use highp to work around aliasing issues |
cdalton | 1f50acf | 2016-04-11 11:30:50 -0700 | [diff] [blame] | 101 | fragBuilder->appendPrecisionModifier(kHigh_GrSLPrecision); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 102 | fragBuilder->codeAppendf("vec2 uv = %s;\n", uv.fsIn()); |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame] | 103 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 104 | fragBuilder->codeAppend("\tfloat texColor = "); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 105 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 106 | "uv", |
| 107 | kVec2f_GrSLType); |
| 108 | fragBuilder->codeAppend(".r;\n"); |
| 109 | fragBuilder->codeAppend("\tfloat distance = " |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame] | 110 | SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFieldThreshold ");"); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 111 | #ifdef SK_GAMMA_APPLY_TO_A8 |
| 112 | // adjust width based on gamma |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 113 | fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 114 | #endif |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 115 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 116 | fragBuilder->codeAppend("float afwidth;"); |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 117 | if (isUniformScale) { |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 118 | // 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] | 119 | // 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] | 120 | // 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] | 121 | |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 122 | // this gives us a smooth step across approximately one fragment |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 123 | #ifdef SK_VULKAN |
| 124 | fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(%s.x));", |
| 125 | st.fsIn()); |
| 126 | #else |
| 127 | // 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] | 128 | fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(%s.y));", |
| 129 | st.fsIn()); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 130 | #endif |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 131 | } else if (isSimilarity) { |
| 132 | // For similarity transform, we adjust the effect of the transformation on the distance |
| 133 | // by using the length of the gradient of the texture coordinates. We use st coordinates |
| 134 | // to ensure we're mapping 1:1 from texel space to pixel space. |
| 135 | // We use the y gradient because there is a bug in the Mali 400 in the x direction. |
| 136 | |
| 137 | // this gives us a smooth step across approximately one fragment |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 138 | #ifdef SK_VULKAN |
| 139 | fragBuilder->codeAppendf("float st_grad_len = length(dFdx(%s));", st.fsIn()); |
| 140 | #else |
| 141 | // We use the y gradient because there is a bug in the Mali 400 in the x direction. |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 142 | fragBuilder->codeAppendf("float st_grad_len = length(dFdy(%s));", st.fsIn()); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 143 | #endif |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 144 | fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);"); |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 145 | } else { |
| 146 | // For general transforms, to determine the amount of correction we multiply a unit |
| 147 | // vector pointing along the SDF gradient direction by the Jacobian of the st coords |
| 148 | // (which is the inverse transform for this fragment) and take the length of the result. |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 149 | fragBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance), dFdy(distance));"); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 150 | // the length of the gradient may be 0, so we need to check for this |
| 151 | // this also compensates for the Adreno, which likes to drop tiles on division by 0 |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 152 | fragBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);"); |
| 153 | fragBuilder->codeAppend("if (dg_len2 < 0.0001) {"); |
| 154 | fragBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);"); |
| 155 | fragBuilder->codeAppend("} else {"); |
| 156 | fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);"); |
| 157 | fragBuilder->codeAppend("}"); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 158 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 159 | fragBuilder->codeAppendf("vec2 Jdx = dFdx(%s);", st.fsIn()); |
| 160 | fragBuilder->codeAppendf("vec2 Jdy = dFdy(%s);", st.fsIn()); |
| 161 | fragBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,"); |
| 162 | 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] | 163 | |
| 164 | // this gives us a smooth step across approximately one fragment |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 165 | fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);"); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 166 | } |
brianosman | 0586f5c | 2016-04-12 12:48:21 -0700 | [diff] [blame] | 167 | |
| 168 | // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are |
| 169 | // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance |
| 170 | // mapped linearly to coverage, so use a linear step: |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 171 | if (isGammaCorrect) { |
brianosman | 0586f5c | 2016-04-12 12:48:21 -0700 | [diff] [blame] | 172 | fragBuilder->codeAppend( |
| 173 | "float val = clamp(distance + afwidth / (2.0 * afwidth), 0.0, 1.0);"); |
| 174 | } else { |
| 175 | fragBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distance);"); |
| 176 | } |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 177 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 178 | fragBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 179 | } |
| 180 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame^] | 181 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc, |
| 182 | FPCoordTransformIter&& transformIter) override { |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 183 | #ifdef SK_GAMMA_APPLY_TO_A8 |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 184 | const GrDistanceFieldA8TextGeoProc& dfTexEffect = proc.cast<GrDistanceFieldA8TextGeoProc>(); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 185 | float distanceAdjust = dfTexEffect.getDistanceAdjust(); |
| 186 | if (distanceAdjust != fDistanceAdjust) { |
| 187 | pdman.set1f(fDistanceAdjustUni, distanceAdjust); |
| 188 | fDistanceAdjust = distanceAdjust; |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 189 | } |
| 190 | #endif |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 191 | const GrDistanceFieldA8TextGeoProc& dfa8gp = proc.cast<GrDistanceFieldA8TextGeoProc>(); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 192 | |
| 193 | if (!dfa8gp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dfa8gp.viewMatrix())) { |
| 194 | fViewMatrix = dfa8gp.viewMatrix(); |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 195 | float viewMatrix[3 * 3]; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 196 | GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 197 | pdman.setMatrix3f(fViewMatrixUniform, viewMatrix); |
| 198 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame^] | 199 | this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter); |
commit-bot@chromium.org | 8fe2ee1 | 2014-03-26 18:03:05 +0000 | [diff] [blame] | 200 | } |
| 201 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 202 | static inline void GenKey(const GrGeometryProcessor& gp, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 203 | const GrGLSLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 204 | GrProcessorKeyBuilder* b) { |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 205 | const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 206 | uint32_t key = dfTexEffect.getFlags(); |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 207 | key |= dfTexEffect.colorIgnored() << 16; |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 208 | key |= ComputePosKey(dfTexEffect.viewMatrix()) << 25; |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 209 | b->add32(key); |
joshualitt | 922c8b1 | 2015-08-07 09:55:23 -0700 | [diff] [blame] | 210 | |
| 211 | // Currently we hardcode numbers to convert atlas coordinates to normalized floating point |
| 212 | SkASSERT(gp.numTextures() == 1); |
| 213 | GrTexture* atlas = gp.textureAccess(0).getTexture(); |
| 214 | SkASSERT(atlas); |
| 215 | b->add32(atlas->width()); |
| 216 | b->add32(atlas->height()); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 217 | } |
| 218 | |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 219 | private: |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 220 | SkMatrix fViewMatrix; |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 221 | UniformHandle fViewMatrixUniform; |
mtklein | 50282b4 | 2015-01-22 07:59:52 -0800 | [diff] [blame] | 222 | #ifdef SK_GAMMA_APPLY_TO_A8 |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 223 | float fDistanceAdjust; |
| 224 | UniformHandle fDistanceAdjustUni; |
mtklein | 50282b4 | 2015-01-22 07:59:52 -0800 | [diff] [blame] | 225 | #endif |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 226 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 227 | typedef GrGLSLGeometryProcessor INHERITED; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | /////////////////////////////////////////////////////////////////////////////// |
| 231 | |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 232 | GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 233 | const SkMatrix& viewMatrix, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 234 | GrTexture* texture, |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 235 | const GrTextureParams& params, |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 236 | #ifdef SK_GAMMA_APPLY_TO_A8 |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 237 | float distanceAdjust, |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 238 | #endif |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 239 | uint32_t flags, |
| 240 | bool usesLocalCoords) |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 241 | : fColor(color) |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 242 | , fViewMatrix(viewMatrix) |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 243 | , fTextureAccess(texture, params) |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 244 | #ifdef SK_GAMMA_APPLY_TO_A8 |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 245 | , fDistanceAdjust(distanceAdjust) |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 246 | #endif |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 247 | , fFlags(flags & kNonLCD_DistanceFieldEffectMask) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 248 | , fInColor(nullptr) |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 249 | , fUsesLocalCoords(usesLocalCoords) { |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 250 | SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask)); |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 251 | this->initClassID<GrDistanceFieldA8TextGeoProc>(); |
bsalomon | 6cb807b | 2016-08-17 11:33:39 -0700 | [diff] [blame] | 252 | fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType, |
| 253 | kHigh_GrSLPrecision); |
| 254 | fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType); |
| 255 | fInTextureCoords = &this->addVertexAttrib("inTextureCoords", kVec2us_GrVertexAttribType, |
| 256 | kHigh_GrSLPrecision); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 257 | this->addTextureAccess(&fTextureAccess); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 258 | } |
| 259 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 260 | void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps, |
| 261 | GrProcessorKeyBuilder* b) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 262 | GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 263 | } |
| 264 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 265 | GrGLSLPrimitiveProcessor* GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrGLSLCaps&) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 266 | return new GrGLDistanceFieldA8TextGeoProc(); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /////////////////////////////////////////////////////////////////////////////// |
| 270 | |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 271 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 272 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 273 | sk_sp<GrGeometryProcessor> GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 274 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 275 | GrProcessorUnitTest::kAlphaTextureIdx; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 276 | static const SkShader::TileMode kTileModes[] = { |
| 277 | SkShader::kClamp_TileMode, |
| 278 | SkShader::kRepeat_TileMode, |
| 279 | SkShader::kMirror_TileMode, |
| 280 | }; |
| 281 | SkShader::TileMode tileModes[] = { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 282 | kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 283 | kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 284 | }; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 285 | GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 286 | GrTextureParams::kNone_FilterMode); |
| 287 | |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 288 | uint32_t flags = 0; |
| 289 | flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0; |
| 290 | if (flags & kSimilarity_DistanceFieldEffectFlag) { |
| 291 | flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0; |
| 292 | } |
| 293 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 294 | return GrDistanceFieldA8TextGeoProc::Make(GrRandomColor(d->fRandom), |
| 295 | GrTest::TestMatrix(d->fRandom), |
| 296 | d->fTextures[texIdx], params, |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 297 | #ifdef SK_GAMMA_APPLY_TO_A8 |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 298 | d->fRandom->nextF(), |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 299 | #endif |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 300 | flags, |
| 301 | d->fRandom->nextBool()); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /////////////////////////////////////////////////////////////////////////////// |
| 305 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 306 | class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 307 | public: |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 308 | GrGLDistanceFieldPathGeoProc() |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 309 | : fViewMatrix(SkMatrix::InvalidMatrix()) |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 310 | , fTextureSize(SkISize::Make(-1, -1)) {} |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 311 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 312 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{ |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 313 | const GrDistanceFieldPathGeoProc& dfTexEffect = args.fGP.cast<GrDistanceFieldPathGeoProc>(); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 314 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 315 | GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 316 | SkAssertResult(fragBuilder->enableFeature( |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 317 | GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 318 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 319 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 320 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 321 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 322 | |
| 323 | // emit attributes |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 324 | varyingHandler->emitAttributes(dfTexEffect); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 325 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 326 | GrGLSLVertToFrag v(kVec2f_GrSLType); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 327 | varyingHandler->addVarying("TextureCoords", &v, kHigh_GrSLPrecision); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 328 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 329 | // setup pass through color |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 330 | if (!dfTexEffect.colorIgnored()) { |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 331 | varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 332 | } |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 333 | vertBuilder->codeAppendf("%s = %s;", v.vsOut(), dfTexEffect.inTextureCoords()->fName); |
reed | e4ef1ca | 2015-02-17 18:38:38 -0800 | [diff] [blame] | 334 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 335 | // Setup position |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 336 | this->setupPosition(vertBuilder, |
| 337 | uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 338 | gpArgs, |
| 339 | dfTexEffect.inPosition()->fName, |
| 340 | dfTexEffect.viewMatrix(), |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 341 | &fViewMatrixUniform); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 342 | |
| 343 | // emit transforms |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 344 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 345 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 346 | uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 347 | gpArgs->fPositionVar, |
| 348 | dfTexEffect.inPosition()->fName, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame^] | 349 | args.fFPCoordTransformHandler); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 350 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 351 | const char* textureSizeUniName = nullptr; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 352 | fTextureSizeUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 353 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 354 | "TextureSize", &textureSizeUniName); |
reed | e4ef1ca | 2015-02-17 18:38:38 -0800 | [diff] [blame] | 355 | |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame] | 356 | // Use highp to work around aliasing issues |
cdalton | 1f50acf | 2016-04-11 11:30:50 -0700 | [diff] [blame] | 357 | fragBuilder->appendPrecisionModifier(kHigh_GrSLPrecision); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 358 | fragBuilder->codeAppendf("vec2 uv = %s;", v.fsIn()); |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame] | 359 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 360 | fragBuilder->codeAppend("float texColor = "); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 361 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 362 | "uv", |
| 363 | kVec2f_GrSLType); |
| 364 | fragBuilder->codeAppend(".r;"); |
| 365 | fragBuilder->codeAppend("float distance = " |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame] | 366 | SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFieldThreshold ");"); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 367 | |
cdalton | 1f50acf | 2016-04-11 11:30:50 -0700 | [diff] [blame] | 368 | fragBuilder->appendPrecisionModifier(kHigh_GrSLPrecision); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 369 | fragBuilder->codeAppendf("vec2 st = uv*%s;", textureSizeUniName); |
| 370 | fragBuilder->codeAppend("float afwidth;"); |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 371 | bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) == |
| 372 | kUniformScale_DistanceFieldEffectMask; |
| 373 | bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag); |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 374 | bool isGammaCorrect = |
| 375 | SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag); |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 376 | if (isUniformScale) { |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 377 | // 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] | 378 | // 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] | 379 | // 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] | 380 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 381 | // this gives us a smooth step across approximately one fragment |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 382 | #ifdef SK_VULKAN |
| 383 | fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(st.x));"); |
| 384 | #else |
| 385 | // 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] | 386 | fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdy(st.y));"); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 387 | #endif |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 388 | } else if (isSimilarity) { |
| 389 | // For similarity transform, we adjust the effect of the transformation on the distance |
| 390 | // by using the length of the gradient of the texture coordinates. We use st coordinates |
| 391 | // to ensure we're mapping 1:1 from texel space to pixel space. |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 392 | |
| 393 | // this gives us a smooth step across approximately one fragment |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 394 | #ifdef SK_VULKAN |
| 395 | fragBuilder->codeAppend("float st_grad_len = length(dFdx(st));"); |
| 396 | #else |
| 397 | // We use the y gradient because there is a bug in the Mali 400 in the x direction. |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 398 | fragBuilder->codeAppend("float st_grad_len = length(dFdy(st));"); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 399 | #endif |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 400 | fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);"); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 401 | } else { |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 402 | // For general transforms, to determine the amount of correction we multiply a unit |
| 403 | // vector pointing along the SDF gradient direction by the Jacobian of the st coords |
| 404 | // (which is the inverse transform for this fragment) and take the length of the result. |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 405 | fragBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance), dFdy(distance));"); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 406 | // the length of the gradient may be 0, so we need to check for this |
| 407 | // this also compensates for the Adreno, which likes to drop tiles on division by 0 |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 408 | fragBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);"); |
| 409 | fragBuilder->codeAppend("if (dg_len2 < 0.0001) {"); |
| 410 | fragBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);"); |
| 411 | fragBuilder->codeAppend("} else {"); |
| 412 | fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);"); |
| 413 | fragBuilder->codeAppend("}"); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 414 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 415 | fragBuilder->codeAppend("vec2 Jdx = dFdx(st);"); |
| 416 | fragBuilder->codeAppend("vec2 Jdy = dFdy(st);"); |
| 417 | fragBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,"); |
| 418 | fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);"); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 419 | |
| 420 | // this gives us a smooth step across approximately one fragment |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 421 | fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);"); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 422 | } |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 423 | // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are |
| 424 | // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance |
| 425 | // mapped linearly to coverage, so use a linear step: |
| 426 | if (isGammaCorrect) { |
| 427 | fragBuilder->codeAppend( |
| 428 | "float val = clamp(distance + afwidth / (2.0 * afwidth), 0.0, 1.0);"); |
| 429 | } else { |
| 430 | fragBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distance);"); |
| 431 | } |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 432 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 433 | fragBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 434 | } |
| 435 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame^] | 436 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc, |
| 437 | FPCoordTransformIter&& transformIter) override { |
reed | e4ef1ca | 2015-02-17 18:38:38 -0800 | [diff] [blame] | 438 | SkASSERT(fTextureSizeUni.isValid()); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 439 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 440 | GrTexture* texture = proc.texture(0); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 441 | if (texture->width() != fTextureSize.width() || |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 442 | texture->height() != fTextureSize.height()) { |
| 443 | fTextureSize = SkISize::Make(texture->width(), texture->height()); |
reed | e4ef1ca | 2015-02-17 18:38:38 -0800 | [diff] [blame] | 444 | pdman.set2f(fTextureSizeUni, |
| 445 | SkIntToScalar(fTextureSize.width()), |
| 446 | SkIntToScalar(fTextureSize.height())); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 447 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 448 | |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 449 | const GrDistanceFieldPathGeoProc& dfpgp = proc.cast<GrDistanceFieldPathGeoProc>(); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 450 | |
| 451 | if (!dfpgp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dfpgp.viewMatrix())) { |
| 452 | fViewMatrix = dfpgp.viewMatrix(); |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 453 | float viewMatrix[3 * 3]; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 454 | GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 455 | pdman.setMatrix3f(fViewMatrixUniform, viewMatrix); |
| 456 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame^] | 457 | this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 458 | } |
| 459 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 460 | static inline void GenKey(const GrGeometryProcessor& gp, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 461 | const GrGLSLCaps&, |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 462 | GrProcessorKeyBuilder* b) { |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 463 | const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>(); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 464 | |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 465 | uint32_t key = dfTexEffect.getFlags(); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 466 | key |= dfTexEffect.colorIgnored() << 16; |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 467 | key |= ComputePosKey(dfTexEffect.viewMatrix()) << 25; |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 468 | b->add32(key); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | private: |
reed | e4ef1ca | 2015-02-17 18:38:38 -0800 | [diff] [blame] | 472 | UniformHandle fTextureSizeUni; |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 473 | UniformHandle fViewMatrixUniform; |
| 474 | SkMatrix fViewMatrix; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 475 | SkISize fTextureSize; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 476 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 477 | typedef GrGLSLGeometryProcessor INHERITED; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 478 | }; |
| 479 | |
| 480 | /////////////////////////////////////////////////////////////////////////////// |
| 481 | |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 482 | GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc( |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 483 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 484 | const SkMatrix& viewMatrix, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 485 | GrTexture* texture, |
| 486 | const GrTextureParams& params, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 487 | uint32_t flags, |
| 488 | bool usesLocalCoords) |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 489 | : fColor(color) |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 490 | , fViewMatrix(viewMatrix) |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 491 | , fTextureAccess(texture, params) |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 492 | , fFlags(flags & kNonLCD_DistanceFieldEffectMask) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 493 | , fInColor(nullptr) |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 494 | , fUsesLocalCoords(usesLocalCoords) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 495 | SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask)); |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 496 | this->initClassID<GrDistanceFieldPathGeoProc>(); |
bsalomon | 6cb807b | 2016-08-17 11:33:39 -0700 | [diff] [blame] | 497 | fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType, |
| 498 | kHigh_GrSLPrecision); |
| 499 | fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType); |
| 500 | fInTextureCoords = &this->addVertexAttrib("inTextureCoords", kVec2f_GrVertexAttribType); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 501 | this->addTextureAccess(&fTextureAccess); |
| 502 | } |
| 503 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 504 | void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps, |
| 505 | GrProcessorKeyBuilder* b) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 506 | GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 507 | } |
| 508 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 509 | GrGLSLPrimitiveProcessor* GrDistanceFieldPathGeoProc::createGLSLInstance(const GrGLSLCaps&) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 510 | return new GrGLDistanceFieldPathGeoProc(); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /////////////////////////////////////////////////////////////////////////////// |
| 514 | |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 515 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 516 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 517 | sk_sp<GrGeometryProcessor> GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 518 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx |
| 519 | : GrProcessorUnitTest::kAlphaTextureIdx; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 520 | static const SkShader::TileMode kTileModes[] = { |
| 521 | SkShader::kClamp_TileMode, |
| 522 | SkShader::kRepeat_TileMode, |
| 523 | SkShader::kMirror_TileMode, |
| 524 | }; |
| 525 | SkShader::TileMode tileModes[] = { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 526 | kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 527 | kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 528 | }; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 529 | GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode |
| 530 | : GrTextureParams::kNone_FilterMode); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 531 | |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 532 | uint32_t flags = 0; |
| 533 | flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0; |
| 534 | if (flags & kSimilarity_DistanceFieldEffectFlag) { |
| 535 | flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0; |
| 536 | } |
| 537 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 538 | return GrDistanceFieldPathGeoProc::Make(GrRandomColor(d->fRandom), |
| 539 | GrTest::TestMatrix(d->fRandom), |
| 540 | d->fTextures[texIdx], |
| 541 | params, |
| 542 | flags, |
| 543 | d->fRandom->nextBool()); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /////////////////////////////////////////////////////////////////////////////// |
| 547 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 548 | class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor { |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 549 | public: |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 550 | GrGLDistanceFieldLCDTextGeoProc() |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 551 | : fViewMatrix(SkMatrix::InvalidMatrix()) { |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 552 | fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 553 | } |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 554 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 555 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{ |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 556 | const GrDistanceFieldLCDTextGeoProc& dfTexEffect = |
| 557 | args.fGP.cast<GrDistanceFieldLCDTextGeoProc>(); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 558 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 559 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 560 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 561 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 562 | |
| 563 | // emit attributes |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 564 | varyingHandler->emitAttributes(dfTexEffect); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 565 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 566 | GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder; |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 567 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 568 | // setup pass through color |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 569 | if (!dfTexEffect.colorIgnored()) { |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 570 | varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 571 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 572 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 573 | // Setup position |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 574 | this->setupPosition(vertBuilder, |
| 575 | uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 576 | gpArgs, |
| 577 | dfTexEffect.inPosition()->fName, |
| 578 | dfTexEffect.viewMatrix(), |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 579 | &fViewMatrixUniform); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 580 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 581 | // emit transforms |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 582 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 583 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 584 | uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 585 | gpArgs->fPositionVar, |
| 586 | dfTexEffect.inPosition()->fName, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame^] | 587 | args.fFPCoordTransformHandler); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 588 | |
jvanverth | bb4a1cf | 2015-04-07 09:06:00 -0700 | [diff] [blame] | 589 | // set up varyings |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 590 | bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) == |
| 591 | kUniformScale_DistanceFieldEffectMask; |
| 592 | bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag); |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 593 | bool isGammaCorrect = |
| 594 | SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 595 | GrGLSLVertToFrag recipScale(kFloat_GrSLType); |
jvanverth | 7023a00 | 2016-02-22 11:25:32 -0800 | [diff] [blame] | 596 | GrGLSLVertToFrag uv(kVec2f_GrSLType); |
| 597 | varyingHandler->addVarying("TextureCoords", &uv, kHigh_GrSLPrecision); |
| 598 | vertBuilder->codeAppendf("%s = %s;", uv.vsOut(), dfTexEffect.inTextureCoords()->fName); |
jvanverth | bb4a1cf | 2015-04-07 09:06:00 -0700 | [diff] [blame] | 599 | |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 600 | // compute numbers to be hardcoded to convert texture coordinates from float to int |
joshualitt | 922c8b1 | 2015-08-07 09:55:23 -0700 | [diff] [blame] | 601 | SkASSERT(dfTexEffect.numTextures() == 1); |
| 602 | GrTexture* atlas = dfTexEffect.textureAccess(0).getTexture(); |
joshualitt | 7375d6b | 2015-08-07 13:36:44 -0700 | [diff] [blame] | 603 | SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height())); |
joshualitt | 922c8b1 | 2015-08-07 09:55:23 -0700 | [diff] [blame] | 604 | |
jvanverth | 7023a00 | 2016-02-22 11:25:32 -0800 | [diff] [blame] | 605 | GrGLSLVertToFrag st(kVec2f_GrSLType); |
| 606 | varyingHandler->addVarying("IntTextureCoords", &st, kHigh_GrSLPrecision); |
| 607 | vertBuilder->codeAppendf("%s = vec2(%d, %d) * %s;", st.vsOut(), |
| 608 | atlas->width(), atlas->height(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 609 | dfTexEffect.inTextureCoords()->fName); |
jvanverth | bb4a1cf | 2015-04-07 09:06:00 -0700 | [diff] [blame] | 610 | |
| 611 | // add frag shader code |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 612 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 613 | SkAssertResult(fragBuilder->enableFeature( |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 614 | GrGLSLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 615 | |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 616 | // create LCD offset adjusted by inverse of transform |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame] | 617 | // Use highp to work around aliasing issues |
cdalton | 1f50acf | 2016-04-11 11:30:50 -0700 | [diff] [blame] | 618 | fragBuilder->appendPrecisionModifier(kHigh_GrSLPrecision); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 619 | fragBuilder->codeAppendf("vec2 uv = %s;\n", uv.fsIn()); |
cdalton | 1f50acf | 2016-04-11 11:30:50 -0700 | [diff] [blame] | 620 | fragBuilder->appendPrecisionModifier(kHigh_GrSLPrecision); |
joshualitt | 922c8b1 | 2015-08-07 09:55:23 -0700 | [diff] [blame] | 621 | |
| 622 | SkScalar lcdDelta = 1.0f / (3.0f * atlas->width()); |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 623 | if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 624 | fragBuilder->codeAppendf("float delta = -%.*f;\n", SK_FLT_DECIMAL_DIG, lcdDelta); |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 625 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 626 | fragBuilder->codeAppendf("float delta = %.*f;\n", SK_FLT_DECIMAL_DIG, lcdDelta); |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 627 | } |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 628 | if (isUniformScale) { |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 629 | #ifdef SK_VULKAN |
| 630 | fragBuilder->codeAppendf("float st_grad_len = abs(dFdx(%s.x));", st.fsIn()); |
| 631 | #else |
| 632 | // We use the y gradient because there is a bug in the Mali 400 in the x direction. |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 633 | fragBuilder->codeAppendf("float st_grad_len = abs(dFdy(%s.y));", st.fsIn()); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 634 | #endif |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 635 | fragBuilder->codeAppend("vec2 offset = vec2(st_grad_len*delta, 0.0);"); |
| 636 | } else if (isSimilarity) { |
| 637 | // For a similarity matrix with rotation, the gradient will not be aligned |
| 638 | // with the texel coordinate axes, so we need to calculate it. |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 639 | #ifdef SK_VULKAN |
| 640 | fragBuilder->codeAppendf("vec2 st_grad = dFdx(%s);", st.fsIn()); |
| 641 | fragBuilder->codeAppend("vec2 offset = delta*st_grad;"); |
| 642 | #else |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 643 | // We use dFdy because of a Mali 400 bug, and rotate -90 degrees to |
| 644 | // get the gradient in the x direction. |
| 645 | fragBuilder->codeAppendf("vec2 st_grad = dFdy(%s);", st.fsIn()); |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 646 | fragBuilder->codeAppend("vec2 offset = delta*vec2(st_grad.y, -st_grad.x);"); |
jvanverth | e499adf | 2016-07-20 12:22:14 -0700 | [diff] [blame] | 647 | #endif |
| 648 | fragBuilder->codeAppend("float st_grad_len = length(st_grad);"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 649 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 650 | fragBuilder->codeAppendf("vec2 st = %s;\n", st.fsIn()); |
jvanverth | bb4a1cf | 2015-04-07 09:06:00 -0700 | [diff] [blame] | 651 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 652 | fragBuilder->codeAppend("vec2 Jdx = dFdx(st);"); |
| 653 | fragBuilder->codeAppend("vec2 Jdy = dFdy(st);"); |
| 654 | fragBuilder->codeAppend("vec2 offset = delta*Jdx;"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | // green is distance to uv center |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 658 | fragBuilder->codeAppend("\tvec4 texColor = "); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 659 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], "uv", kVec2f_GrSLType); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 660 | fragBuilder->codeAppend(";\n"); |
| 661 | fragBuilder->codeAppend("\tvec3 distance;\n"); |
| 662 | fragBuilder->codeAppend("\tdistance.y = texColor.r;\n"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 663 | // red is distance to left offset |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 664 | fragBuilder->codeAppend("\tvec2 uv_adjusted = uv - offset;\n"); |
| 665 | fragBuilder->codeAppend("\ttexColor = "); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 666 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], "uv_adjusted", kVec2f_GrSLType); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 667 | fragBuilder->codeAppend(";\n"); |
| 668 | fragBuilder->codeAppend("\tdistance.x = texColor.r;\n"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 669 | // blue is distance to right offset |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 670 | fragBuilder->codeAppend("\tuv_adjusted = uv + offset;\n"); |
| 671 | fragBuilder->codeAppend("\ttexColor = "); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 672 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], "uv_adjusted", kVec2f_GrSLType); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 673 | fragBuilder->codeAppend(";\n"); |
| 674 | fragBuilder->codeAppend("\tdistance.z = texColor.r;\n"); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 675 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 676 | fragBuilder->codeAppend("\tdistance = " |
jvanverth | ada68ef | 2014-11-03 14:00:24 -0800 | [diff] [blame] | 677 | "vec3(" SK_DistanceFieldMultiplier ")*(distance - vec3(" SK_DistanceFieldThreshold"));"); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 678 | |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 679 | // adjust width based on gamma |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 680 | const char* distanceAdjustUniName = nullptr; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 681 | fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 682 | kVec3f_GrSLType, kDefault_GrSLPrecision, |
| 683 | "DistanceAdjust", &distanceAdjustUniName); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 684 | fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 685 | |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 686 | // To be strictly correct, we should compute the anti-aliasing factor separately |
| 687 | // for each color component. However, this is only important when using perspective |
| 688 | // transformations, and even then using a single factor seems like a reasonable |
| 689 | // trade-off between quality and speed. |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 690 | fragBuilder->codeAppend("float afwidth;"); |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 691 | if (isSimilarity) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 692 | // For similarity transform (uniform scale-only is a subset of this), we adjust for the |
| 693 | // 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] | 694 | // the texture coordinates. We use st coordinates to ensure we're mapping 1:1 from texel |
| 695 | // space to pixel space. |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 696 | |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 697 | // this gives us a smooth step across approximately one fragment |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 698 | fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*st_grad_len;"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 699 | } else { |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 700 | // For general transforms, to determine the amount of correction we multiply a unit |
| 701 | // vector pointing along the SDF gradient direction by the Jacobian of the st coords |
| 702 | // (which is the inverse transform for this fragment) and take the length of the result. |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 703 | fragBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance.r), dFdy(distance.r));"); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 704 | // the length of the gradient may be 0, so we need to check for this |
| 705 | // this also compensates for the Adreno, which likes to drop tiles on division by 0 |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 706 | fragBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);"); |
| 707 | fragBuilder->codeAppend("if (dg_len2 < 0.0001) {"); |
| 708 | fragBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);"); |
| 709 | fragBuilder->codeAppend("} else {"); |
| 710 | fragBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);"); |
| 711 | fragBuilder->codeAppend("}"); |
| 712 | fragBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,"); |
| 713 | 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] | 714 | |
| 715 | // this gives us a smooth step across approximately one fragment |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 716 | fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 717 | } |
| 718 | |
brianosman | 0586f5c | 2016-04-12 12:48:21 -0700 | [diff] [blame] | 719 | // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are |
| 720 | // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance |
| 721 | // mapped linearly to coverage, so use a linear step: |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 722 | if (isGammaCorrect) { |
brianosman | 0586f5c | 2016-04-12 12:48:21 -0700 | [diff] [blame] | 723 | fragBuilder->codeAppend("vec4 val = " |
bsalomon | 32d1e95 | 2016-09-15 07:29:52 -0700 | [diff] [blame] | 724 | "vec4(clamp(distance + vec3(afwidth) / vec3(2.0 * afwidth), 0.0, 1.0), 1.0);"); |
brianosman | 0586f5c | 2016-04-12 12:48:21 -0700 | [diff] [blame] | 725 | } else { |
| 726 | fragBuilder->codeAppend( |
| 727 | "vec4 val = vec4(smoothstep(vec3(-afwidth), vec3(afwidth), distance), 1.0);"); |
| 728 | } |
| 729 | |
egdaniel | 27b6335 | 2015-09-15 13:13:50 -0700 | [diff] [blame] | 730 | // set alpha to be max of rgb coverage |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 731 | fragBuilder->codeAppend("val.a = max(max(val.r, val.g), val.b);"); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 732 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 733 | fragBuilder->codeAppendf("%s = val;", args.fOutputCoverage); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 734 | } |
| 735 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame^] | 736 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& processor, |
| 737 | FPCoordTransformIter&& transformIter) override { |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 738 | SkASSERT(fDistanceAdjustUni.isValid()); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 739 | |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 740 | const GrDistanceFieldLCDTextGeoProc& dflcd = processor.cast<GrDistanceFieldLCDTextGeoProc>(); |
| 741 | GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust(); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 742 | if (wa != fDistanceAdjust) { |
| 743 | pdman.set3f(fDistanceAdjustUni, |
| 744 | wa.fR, |
| 745 | wa.fG, |
| 746 | wa.fB); |
| 747 | fDistanceAdjust = wa; |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 748 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 749 | |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 750 | if (!dflcd.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dflcd.viewMatrix())) { |
| 751 | fViewMatrix = dflcd.viewMatrix(); |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 752 | float viewMatrix[3 * 3]; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 753 | GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 754 | pdman.setMatrix3f(fViewMatrixUniform, viewMatrix); |
| 755 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame^] | 756 | this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 757 | } |
| 758 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 759 | static inline void GenKey(const GrGeometryProcessor& gp, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 760 | const GrGLSLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 761 | GrProcessorKeyBuilder* b) { |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 762 | const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>(); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 763 | |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 764 | uint32_t key = dfTexEffect.getFlags(); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 765 | key |= dfTexEffect.colorIgnored() << 16; |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 766 | key |= ComputePosKey(dfTexEffect.viewMatrix()) << 25; |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 767 | b->add32(key); |
joshualitt | 922c8b1 | 2015-08-07 09:55:23 -0700 | [diff] [blame] | 768 | |
| 769 | // Currently we hardcode numbers to convert atlas coordinates to normalized floating point |
| 770 | SkASSERT(gp.numTextures() == 1); |
| 771 | GrTexture* atlas = gp.textureAccess(0).getTexture(); |
| 772 | SkASSERT(atlas); |
| 773 | b->add32(atlas->width()); |
| 774 | b->add32(atlas->height()); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | private: |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 778 | SkMatrix fViewMatrix; |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 779 | UniformHandle fViewMatrixUniform; |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 780 | UniformHandle fColorUniform; |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 781 | GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust; |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 782 | UniformHandle fDistanceAdjustUni; |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 783 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 784 | typedef GrGLSLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 785 | }; |
| 786 | |
| 787 | /////////////////////////////////////////////////////////////////////////////// |
| 788 | |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 789 | GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc( |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 790 | GrColor color, const SkMatrix& viewMatrix, |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 791 | GrTexture* texture, const GrTextureParams& params, |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 792 | DistanceAdjust distanceAdjust, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 793 | uint32_t flags, bool usesLocalCoords) |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 794 | : fColor(color) |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 795 | , fViewMatrix(viewMatrix) |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 796 | , fTextureAccess(texture, params) |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 797 | , fDistanceAdjust(distanceAdjust) |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 798 | , fFlags(flags & kLCD_DistanceFieldEffectMask) |
| 799 | , fUsesLocalCoords(usesLocalCoords) { |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 800 | SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag)); |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 801 | this->initClassID<GrDistanceFieldLCDTextGeoProc>(); |
bsalomon | 6cb807b | 2016-08-17 11:33:39 -0700 | [diff] [blame] | 802 | fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType, |
| 803 | kHigh_GrSLPrecision); |
| 804 | fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType); |
| 805 | fInTextureCoords = &this->addVertexAttrib("inTextureCoords", kVec2us_GrVertexAttribType, |
| 806 | kHigh_GrSLPrecision); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 807 | this->addTextureAccess(&fTextureAccess); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 808 | } |
| 809 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 810 | void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps, |
| 811 | GrProcessorKeyBuilder* b) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 812 | GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 813 | } |
| 814 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 815 | GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrGLSLCaps&) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 816 | return new GrGLDistanceFieldLCDTextGeoProc(); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | /////////////////////////////////////////////////////////////////////////////// |
| 820 | |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 821 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 822 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 823 | sk_sp<GrGeometryProcessor> GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 824 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 825 | GrProcessorUnitTest::kAlphaTextureIdx; |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 826 | static const SkShader::TileMode kTileModes[] = { |
| 827 | SkShader::kClamp_TileMode, |
| 828 | SkShader::kRepeat_TileMode, |
| 829 | SkShader::kMirror_TileMode, |
| 830 | }; |
| 831 | SkShader::TileMode tileModes[] = { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 832 | kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 833 | kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 834 | }; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 835 | GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 836 | GrTextureParams::kNone_FilterMode); |
jvanverth | 21deace | 2015-04-01 12:43:48 -0700 | [diff] [blame] | 837 | DistanceAdjust wa = { 0.0f, 0.1f, -0.1f }; |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 838 | uint32_t flags = kUseLCD_DistanceFieldEffectFlag; |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 839 | flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0; |
| 840 | if (flags & kSimilarity_DistanceFieldEffectFlag) { |
| 841 | flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0; |
| 842 | } |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 843 | flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 844 | return GrDistanceFieldLCDTextGeoProc::Make(GrRandomColor(d->fRandom), |
| 845 | GrTest::TestMatrix(d->fRandom), |
| 846 | d->fTextures[texIdx], params, |
| 847 | wa, |
| 848 | flags, |
| 849 | d->fRandom->nextBool()); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 850 | } |