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