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