commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +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 | |
| 8 | #include "GrBezierEffect.h" |
| 9 | |
| 10 | #include "gl/GrGLEffect.h" |
| 11 | #include "gl/GrGLSL.h" |
| 12 | #include "GrTBackendEffectFactory.h" |
| 13 | |
| 14 | class GrGLConicEffect : public GrGLEffect { |
| 15 | public: |
| 16 | GrGLConicEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 17 | |
| 18 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 19 | const GrDrawEffect& drawEffect, |
| 20 | EffectKey key, |
| 21 | const char* outputColor, |
| 22 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 23 | const TransformedCoordsArray&, |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 24 | const TextureSamplerArray&) SK_OVERRIDE; |
| 25 | |
| 26 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 27 | |
| 28 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} |
| 29 | |
| 30 | private: |
| 31 | GrBezierEdgeType fEdgeType; |
| 32 | |
| 33 | typedef GrGLEffect INHERITED; |
| 34 | }; |
skia.committer@gmail.com | 44a77c8 | 2013-08-23 07:01:29 +0000 | [diff] [blame] | 35 | |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 36 | GrGLConicEffect::GrGLConicEffect(const GrBackendEffectFactory& factory, |
| 37 | const GrDrawEffect& drawEffect) |
| 38 | : INHERITED (factory) { |
| 39 | const GrConicEffect& ce = drawEffect.castEffect<GrConicEffect>(); |
| 40 | fEdgeType = ce.getEdgeType(); |
| 41 | } |
| 42 | |
| 43 | void GrGLConicEffect::emitCode(GrGLShaderBuilder* builder, |
| 44 | const GrDrawEffect& drawEffect, |
| 45 | EffectKey key, |
| 46 | const char* outputColor, |
| 47 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 48 | const TransformedCoordsArray&, |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 49 | const TextureSamplerArray& samplers) { |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 50 | GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder(); |
| 51 | SkASSERT(NULL != vertexBuilder); |
| 52 | |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 53 | const char *vsName, *fsName; |
| 54 | |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 55 | vertexBuilder->addVarying(kVec4f_GrSLType, "ConicCoeffs", |
| 56 | &vsName, &fsName); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 57 | const SkString* attr0Name = |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 58 | vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 59 | vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str()); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 60 | |
| 61 | builder->fsCodeAppend("\t\tfloat edgeAlpha;\n"); |
| 62 | |
| 63 | switch (fEdgeType) { |
| 64 | case kHairAA_GrBezierEdgeType: { |
| 65 | SkAssertResult(builder->enableFeature( |
| 66 | GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 67 | builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName); |
| 68 | builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName); |
| 69 | builder->fsCodeAppendf("\t\tfloat dfdx =\n" |
| 70 | "\t\t\t2.0*%s.x*dklmdx.x - %s.y*dklmdx.z - %s.z*dklmdx.y;\n", |
| 71 | fsName, fsName, fsName); |
| 72 | builder->fsCodeAppendf("\t\tfloat dfdy =\n" |
| 73 | "\t\t\t2.0*%s.x*dklmdy.x - %s.y*dklmdy.z - %s.z*dklmdy.y;\n", |
| 74 | fsName, fsName, fsName); |
| 75 | builder->fsCodeAppend("\t\tvec2 gF = vec2(dfdx, dfdy);\n"); |
| 76 | builder->fsCodeAppend("\t\tfloat gFM = sqrt(dot(gF, gF));\n"); |
| 77 | builder->fsCodeAppendf("\t\tfloat func = %s.x*%s.x - %s.y*%s.z;\n", fsName, fsName, |
| 78 | fsName, fsName); |
| 79 | builder->fsCodeAppend("\t\tfunc = abs(func);\n"); |
| 80 | builder->fsCodeAppend("\t\tedgeAlpha = func / gFM;\n"); |
| 81 | builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
| 82 | // Add line below for smooth cubic ramp |
| 83 | // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n"); |
| 84 | break; |
| 85 | } |
| 86 | case kFillAA_GrBezierEdgeType: { |
| 87 | SkAssertResult(builder->enableFeature( |
| 88 | GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 89 | builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName); |
| 90 | builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName); |
| 91 | builder->fsCodeAppendf("\t\tfloat dfdx =\n" |
| 92 | "\t\t\t2.0*%s.x*dklmdx.x - %s.y*dklmdx.z - %s.z*dklmdx.y;\n", |
| 93 | fsName, fsName, fsName); |
| 94 | builder->fsCodeAppendf("\t\tfloat dfdy =\n" |
| 95 | "\t\t\t2.0*%s.x*dklmdy.x - %s.y*dklmdy.z - %s.z*dklmdy.y;\n", |
| 96 | fsName, fsName, fsName); |
| 97 | builder->fsCodeAppend("\t\tvec2 gF = vec2(dfdx, dfdy);\n"); |
| 98 | builder->fsCodeAppend("\t\tfloat gFM = sqrt(dot(gF, gF));\n"); |
| 99 | builder->fsCodeAppendf("\t\tfloat func = %s.x*%s.x - %s.y*%s.z;\n", fsName, fsName, |
| 100 | fsName, fsName); |
| 101 | builder->fsCodeAppend("\t\tedgeAlpha = func / gFM;\n"); |
| 102 | builder->fsCodeAppend("\t\tedgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1.0);\n"); |
| 103 | // Add line below for smooth cubic ramp |
| 104 | // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n"); |
| 105 | break; |
| 106 | } |
| 107 | case kFillNoAA_GrBezierEdgeType: { |
| 108 | builder->fsCodeAppendf("\t\tedgeAlpha = %s.x*%s.x - %s.y*%s.z;\n", fsName, fsName, |
| 109 | fsName, fsName); |
| 110 | builder->fsCodeAppend("\t\tedgeAlpha = float(edgeAlpha < 0.0);\n"); |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | SkString modulate; |
| 116 | GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); |
| 117 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
| 118 | } |
| 119 | |
| 120 | GrGLEffect::EffectKey GrGLConicEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 121 | const GrConicEffect& ce = drawEffect.castEffect<GrConicEffect>(); |
| 122 | return ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2; |
| 123 | } |
| 124 | |
| 125 | ////////////////////////////////////////////////////////////////////////////// |
| 126 | |
| 127 | GrConicEffect::~GrConicEffect() {} |
| 128 | |
| 129 | const GrBackendEffectFactory& GrConicEffect::getFactory() const { |
| 130 | return GrTBackendEffectFactory<GrConicEffect>::getInstance(); |
| 131 | } |
| 132 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 133 | GrConicEffect::GrConicEffect(GrBezierEdgeType edgeType) : GrVertexEffect() { |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 134 | this->addVertexAttrib(kVec4f_GrSLType); |
| 135 | fEdgeType = edgeType; |
| 136 | } |
| 137 | |
| 138 | bool GrConicEffect::onIsEqual(const GrEffect& other) const { |
| 139 | const GrConicEffect& ce = CastEffect<GrConicEffect>(other); |
| 140 | return (ce.fEdgeType == fEdgeType); |
| 141 | } |
| 142 | |
| 143 | ////////////////////////////////////////////////////////////////////////////// |
| 144 | |
| 145 | GR_DEFINE_EFFECT_TEST(GrConicEffect); |
| 146 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 147 | GrEffectRef* GrConicEffect::TestCreate(SkRandom* random, |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 148 | GrContext*, |
| 149 | const GrDrawTargetCaps& caps, |
| 150 | GrTexture*[]) { |
| 151 | const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->nextULessThan(3)); |
| 152 | return GrConicEffect::Create(edgeType, caps); |
| 153 | } |
| 154 | |
| 155 | ////////////////////////////////////////////////////////////////////////////// |
| 156 | // Quad |
| 157 | ////////////////////////////////////////////////////////////////////////////// |
| 158 | |
| 159 | class GrGLQuadEffect : public GrGLEffect { |
| 160 | public: |
| 161 | GrGLQuadEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 162 | |
| 163 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 164 | const GrDrawEffect& drawEffect, |
| 165 | EffectKey key, |
| 166 | const char* outputColor, |
| 167 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 168 | const TransformedCoordsArray&, |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 169 | const TextureSamplerArray&) SK_OVERRIDE; |
| 170 | |
| 171 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 172 | |
| 173 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} |
| 174 | |
| 175 | private: |
| 176 | GrBezierEdgeType fEdgeType; |
| 177 | |
| 178 | typedef GrGLEffect INHERITED; |
| 179 | }; |
skia.committer@gmail.com | 44a77c8 | 2013-08-23 07:01:29 +0000 | [diff] [blame] | 180 | |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 181 | GrGLQuadEffect::GrGLQuadEffect(const GrBackendEffectFactory& factory, |
| 182 | const GrDrawEffect& drawEffect) |
| 183 | : INHERITED (factory) { |
| 184 | const GrQuadEffect& ce = drawEffect.castEffect<GrQuadEffect>(); |
| 185 | fEdgeType = ce.getEdgeType(); |
| 186 | } |
| 187 | |
| 188 | void GrGLQuadEffect::emitCode(GrGLShaderBuilder* builder, |
| 189 | const GrDrawEffect& drawEffect, |
| 190 | EffectKey key, |
| 191 | const char* outputColor, |
| 192 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 193 | const TransformedCoordsArray&, |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 194 | const TextureSamplerArray& samplers) { |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 195 | GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder(); |
| 196 | SkASSERT(NULL != vertexBuilder); |
| 197 | |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 198 | const char *vsName, *fsName; |
| 199 | |
| 200 | const SkString* attrName = |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 201 | vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 202 | builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n"); |
| 203 | |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 204 | vertexBuilder->addVarying(kVec4f_GrSLType, "HairQuadEdge", &vsName, &fsName); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 205 | |
| 206 | switch (fEdgeType) { |
| 207 | case kHairAA_GrBezierEdgeType: { |
| 208 | SkAssertResult(builder->enableFeature( |
| 209 | GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 210 | builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 211 | builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
| 212 | builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n" |
| 213 | "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n", |
| 214 | fsName, fsName); |
| 215 | builder->fsCodeAppendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, |
| 216 | fsName); |
| 217 | builder->fsCodeAppend("\t\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n"); |
| 218 | builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
| 219 | // Add line below for smooth cubic ramp |
| 220 | // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n"); |
| 221 | break; |
| 222 | } |
| 223 | case kFillAA_GrBezierEdgeType: { |
| 224 | SkAssertResult(builder->enableFeature( |
| 225 | GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 226 | builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 227 | builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
| 228 | builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n" |
| 229 | "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n", |
| 230 | fsName, fsName); |
| 231 | builder->fsCodeAppendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, |
| 232 | fsName); |
| 233 | builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha / sqrt(dot(gF, gF));\n"); |
| 234 | builder->fsCodeAppend("\t\tedgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1.0);\n"); |
| 235 | // Add line below for smooth cubic ramp |
| 236 | // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n"); |
| 237 | break; |
| 238 | } |
| 239 | case kFillNoAA_GrBezierEdgeType: { |
| 240 | builder->fsCodeAppendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, |
| 241 | fsName); |
| 242 | builder->fsCodeAppend("\t\tedgeAlpha = float(edgeAlpha < 0.0);\n"); |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | SkString modulate; |
| 248 | GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); |
| 249 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
| 250 | |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 251 | vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | GrGLEffect::EffectKey GrGLQuadEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 255 | const GrQuadEffect& ce = drawEffect.castEffect<GrQuadEffect>(); |
| 256 | return ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2; |
| 257 | } |
| 258 | |
| 259 | ////////////////////////////////////////////////////////////////////////////// |
| 260 | |
| 261 | GrQuadEffect::~GrQuadEffect() {} |
| 262 | |
| 263 | const GrBackendEffectFactory& GrQuadEffect::getFactory() const { |
| 264 | return GrTBackendEffectFactory<GrQuadEffect>::getInstance(); |
| 265 | } |
| 266 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 267 | GrQuadEffect::GrQuadEffect(GrBezierEdgeType edgeType) : GrVertexEffect() { |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 268 | this->addVertexAttrib(kVec4f_GrSLType); |
| 269 | fEdgeType = edgeType; |
| 270 | } |
| 271 | |
| 272 | bool GrQuadEffect::onIsEqual(const GrEffect& other) const { |
| 273 | const GrQuadEffect& ce = CastEffect<GrQuadEffect>(other); |
| 274 | return (ce.fEdgeType == fEdgeType); |
| 275 | } |
| 276 | |
| 277 | ////////////////////////////////////////////////////////////////////////////// |
| 278 | |
| 279 | GR_DEFINE_EFFECT_TEST(GrQuadEffect); |
| 280 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 281 | GrEffectRef* GrQuadEffect::TestCreate(SkRandom* random, |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 282 | GrContext*, |
| 283 | const GrDrawTargetCaps& caps, |
| 284 | GrTexture*[]) { |
| 285 | const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->nextULessThan(3)); |
| 286 | return GrQuadEffect::Create(edgeType, caps); |
| 287 | } |
| 288 | |
| 289 | ////////////////////////////////////////////////////////////////////////////// |
| 290 | // Cubic |
| 291 | ////////////////////////////////////////////////////////////////////////////// |
| 292 | |
| 293 | class GrGLCubicEffect : public GrGLEffect { |
| 294 | public: |
| 295 | GrGLCubicEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 296 | |
| 297 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 298 | const GrDrawEffect& drawEffect, |
| 299 | EffectKey key, |
| 300 | const char* outputColor, |
| 301 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 302 | const TransformedCoordsArray&, |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 303 | const TextureSamplerArray&) SK_OVERRIDE; |
| 304 | |
| 305 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 306 | |
| 307 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} |
| 308 | |
| 309 | private: |
| 310 | GrBezierEdgeType fEdgeType; |
| 311 | |
| 312 | typedef GrGLEffect INHERITED; |
| 313 | }; |
skia.committer@gmail.com | 44a77c8 | 2013-08-23 07:01:29 +0000 | [diff] [blame] | 314 | |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 315 | GrGLCubicEffect::GrGLCubicEffect(const GrBackendEffectFactory& factory, |
| 316 | const GrDrawEffect& drawEffect) |
| 317 | : INHERITED (factory) { |
| 318 | const GrCubicEffect& ce = drawEffect.castEffect<GrCubicEffect>(); |
| 319 | fEdgeType = ce.getEdgeType(); |
| 320 | } |
| 321 | |
| 322 | void GrGLCubicEffect::emitCode(GrGLShaderBuilder* builder, |
| 323 | const GrDrawEffect& drawEffect, |
| 324 | EffectKey key, |
| 325 | const char* outputColor, |
| 326 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 327 | const TransformedCoordsArray&, |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 328 | const TextureSamplerArray& samplers) { |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 329 | GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder(); |
| 330 | SkASSERT(NULL != vertexBuilder); |
| 331 | |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 332 | const char *vsName, *fsName; |
| 333 | |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 334 | vertexBuilder->addVarying(kVec4f_GrSLType, "CubicCoeffs", |
| 335 | &vsName, &fsName); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 336 | const SkString* attr0Name = |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 337 | vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 338 | vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str()); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 339 | |
| 340 | builder->fsCodeAppend("\t\tfloat edgeAlpha;\n"); |
| 341 | |
| 342 | switch (fEdgeType) { |
| 343 | case kHairAA_GrBezierEdgeType: { |
| 344 | SkAssertResult(builder->enableFeature( |
| 345 | GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 346 | builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName); |
| 347 | builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName); |
| 348 | builder->fsCodeAppendf("\t\tfloat dfdx =\n" |
| 349 | "\t\t3.0*%s.x*%s.x*dklmdx.x - %s.y*dklmdx.z - %s.z*dklmdx.y;\n", |
| 350 | fsName, fsName, fsName, fsName); |
| 351 | builder->fsCodeAppendf("\t\tfloat dfdy =\n" |
| 352 | "\t\t3.0*%s.x*%s.x*dklmdy.x - %s.y*dklmdy.z - %s.z*dklmdy.y;\n", |
| 353 | fsName, fsName, fsName, fsName); |
| 354 | builder->fsCodeAppend("\t\tvec2 gF = vec2(dfdx, dfdy);\n"); |
| 355 | builder->fsCodeAppend("\t\tfloat gFM = sqrt(dot(gF, gF));\n"); |
| 356 | builder->fsCodeAppendf("\t\tfloat func = %s.x*%s.x*%s.x - %s.y*%s.z;\n", |
| 357 | fsName, fsName, fsName, fsName, fsName); |
| 358 | builder->fsCodeAppend("\t\tfunc = abs(func);\n"); |
| 359 | builder->fsCodeAppend("\t\tedgeAlpha = func / gFM;\n"); |
| 360 | builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
| 361 | // Add line below for smooth cubic ramp |
| 362 | // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n"); |
| 363 | break; |
| 364 | } |
| 365 | case kFillAA_GrBezierEdgeType: { |
| 366 | SkAssertResult(builder->enableFeature( |
| 367 | GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 368 | builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName); |
| 369 | builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName); |
| 370 | builder->fsCodeAppendf("\t\tfloat dfdx =\n" |
| 371 | "\t\t3.0*%s.x*%s.x*dklmdx.x - %s.y*dklmdx.z - %s.z*dklmdx.y;\n", |
| 372 | fsName, fsName, fsName, fsName); |
| 373 | builder->fsCodeAppendf("\t\tfloat dfdy =\n" |
| 374 | "\t\t3.0*%s.x*%s.x*dklmdy.x - %s.y*dklmdy.z - %s.z*dklmdy.y;\n", |
| 375 | fsName, fsName, fsName, fsName); |
| 376 | builder->fsCodeAppend("\t\tvec2 gF = vec2(dfdx, dfdy);\n"); |
| 377 | builder->fsCodeAppend("\t\tfloat gFM = sqrt(dot(gF, gF));\n"); |
| 378 | builder->fsCodeAppendf("\t\tfloat func = %s.x*%s.x*%s.x - %s.y*%s.z;\n", |
| 379 | fsName, fsName, fsName, fsName, fsName); |
| 380 | builder->fsCodeAppend("\t\tedgeAlpha = func / gFM;\n"); |
| 381 | builder->fsCodeAppend("\t\tedgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1.0);\n"); |
| 382 | // Add line below for smooth cubic ramp |
| 383 | // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n"); |
| 384 | break; |
| 385 | } |
| 386 | case kFillNoAA_GrBezierEdgeType: { |
| 387 | builder->fsCodeAppendf("\t\tedgeAlpha = %s.x*%s.x*%s.x - %s.y*%s.z;\n", |
| 388 | fsName, fsName, fsName, fsName, fsName); |
| 389 | builder->fsCodeAppend("\t\tedgeAlpha = float(edgeAlpha < 0.0);\n"); |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | SkString modulate; |
| 395 | GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); |
| 396 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
| 397 | } |
| 398 | |
| 399 | GrGLEffect::EffectKey GrGLCubicEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 400 | const GrCubicEffect& ce = drawEffect.castEffect<GrCubicEffect>(); |
| 401 | return ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2; |
| 402 | } |
| 403 | |
| 404 | ////////////////////////////////////////////////////////////////////////////// |
| 405 | |
| 406 | GrCubicEffect::~GrCubicEffect() {} |
| 407 | |
| 408 | const GrBackendEffectFactory& GrCubicEffect::getFactory() const { |
| 409 | return GrTBackendEffectFactory<GrCubicEffect>::getInstance(); |
| 410 | } |
| 411 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 412 | GrCubicEffect::GrCubicEffect(GrBezierEdgeType edgeType) : GrVertexEffect() { |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 413 | this->addVertexAttrib(kVec4f_GrSLType); |
| 414 | fEdgeType = edgeType; |
| 415 | } |
| 416 | |
| 417 | bool GrCubicEffect::onIsEqual(const GrEffect& other) const { |
| 418 | const GrCubicEffect& ce = CastEffect<GrCubicEffect>(other); |
| 419 | return (ce.fEdgeType == fEdgeType); |
| 420 | } |
| 421 | |
| 422 | ////////////////////////////////////////////////////////////////////////////// |
| 423 | |
| 424 | GR_DEFINE_EFFECT_TEST(GrCubicEffect); |
| 425 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 426 | GrEffectRef* GrCubicEffect::TestCreate(SkRandom* random, |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 427 | GrContext*, |
| 428 | const GrDrawTargetCaps& caps, |
| 429 | GrTexture*[]) { |
| 430 | const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->nextULessThan(3)); |
| 431 | return GrCubicEffect::Create(edgeType, caps); |
| 432 | } |