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