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