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