OLD | NEW |
(Empty) | |
| 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, |
| 23 const TextureSamplerArray&) SK_OVERRIDE; |
| 24 |
| 25 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 26 |
| 27 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE {} |
| 28 |
| 29 private: |
| 30 bool fAntiAlias; |
| 31 bool fFill; |
| 32 |
| 33 typedef GrGLEffect INHERITED; |
| 34 }; |
| 35 |
| 36 GrGLConicEffect::GrGLConicEffect(const GrBackendEffectFactory& factory, |
| 37 const GrDrawEffect& drawEffect) |
| 38 : INHERITED (factory) { |
| 39 const GrConicEffect& ce = drawEffect.castEffect<GrConicEffect>(); |
| 40 fAntiAlias = ce.isAntiAliased(); |
| 41 fFill = ce.isFilled(); |
| 42 } |
| 43 |
| 44 void GrGLConicEffect::emitCode(GrGLShaderBuilder* builder, |
| 45 const GrDrawEffect& drawEffect, |
| 46 EffectKey key, |
| 47 const char* outputColor, |
| 48 const char* inputColor, |
| 49 const TextureSamplerArray& samplers) SK_OVERRIDE
{ |
| 50 const char *vsName, *fsName; |
| 51 |
| 52 if (fAntiAlias) { |
| 53 SkAssertResult(builder->enableFeature( |
| 54 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 55 } |
| 56 builder->addVarying(kVec4f_GrSLType, "ConicCoeffs", |
| 57 &vsName, &fsName); |
| 58 const SkString* attr0Name = |
| 59 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 60 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str()); |
| 61 |
| 62 builder->fsCodeAppend("\t\tfloat edgeAlpha;\n"); |
| 63 |
| 64 if (fAntiAlias) { |
| 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*d
klmdx.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*d
klmdy.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 } |
| 76 builder->fsCodeAppendf("\t\tfloat func = %s.x*%s.x - %s.y*%s.z;\n", fsName,
fsName, |
| 77 fsName, fsName); |
| 78 if (!fFill) { |
| 79 builder->fsCodeAppend("\t\tfunc = abs(func);\n"); |
| 80 } |
| 81 if (fAntiAlias) { |
| 82 builder->fsCodeAppend("\t\tedgeAlpha = func / gFM;\n"); |
| 83 } |
| 84 if (fFill) { |
| 85 if (fAntiAlias) { |
| 86 builder->fsCodeAppend("\t\tedgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1
.0);\n"); |
| 87 // Add line below for smooth cubic ramp |
| 88 // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2
.0*edgeAlpha);\n"); |
| 89 } else { |
| 90 builder->fsCodeAppend("\t\tedgeAlpha = float(func < 0.0);\n"); |
| 91 } |
| 92 } else { |
| 93 builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
| 94 // Add line below for smooth cubic ramp |
| 95 // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*e
dgeAlpha);\n"); |
| 96 } |
| 97 |
| 98 SkString modulate; |
| 99 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); |
| 100 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
| 101 } |
| 102 |
| 103 GrGLEffect::EffectKey GrGLConicEffect::GenKey(const GrDrawEffect& drawEffect, co
nst GrGLCaps&) { |
| 104 const GrConicEffect& ce = drawEffect.castEffect<GrConicEffect>(); |
| 105 return ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2; |
| 106 } |
| 107 |
| 108 ////////////////////////////////////////////////////////////////////////////// |
| 109 |
| 110 GrConicEffect::~GrConicEffect() {} |
| 111 |
| 112 const GrBackendEffectFactory& GrConicEffect::getFactory() const SK_OVERRIDE { |
| 113 return GrTBackendEffectFactory<GrConicEffect>::getInstance(); |
| 114 } |
| 115 |
| 116 GrConicEffect::GrConicEffect(const GrBezierEdgeType edgeType) : GrEffect() { |
| 117 this->addVertexAttrib(kVec4f_GrSLType); |
| 118 fAntiAlias = GrBezierEdgeTypeIsAA(edgeType) ; |
| 119 fFill = GrBezierEdgeTypeIsFill(edgeType); |
| 120 } |
| 121 |
| 122 bool GrConicEffect::onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 123 const GrConicEffect& ce = CastEffect<GrConicEffect>(other); |
| 124 return (ce.fAntiAlias == fAntiAlias && ce.fFill == fFill); |
| 125 } |
| 126 |
| 127 ////////////////////////////////////////////////////////////////////////////// |
| 128 |
| 129 GR_DEFINE_EFFECT_TEST(GrConicEffect); |
| 130 |
| 131 GrEffectRef* GrConicEffect::TestCreate(SkMWCRandom* random, |
| 132 GrContext*, |
| 133 const GrDrawTargetCaps& caps, |
| 134 GrTexture*[]) { |
| 135 const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->next
ULessThan(3)); |
| 136 return GrConicEffect::Create(edgeType, caps); |
| 137 } |
| 138 |
| 139 ////////////////////////////////////////////////////////////////////////////// |
| 140 // Quad |
| 141 ////////////////////////////////////////////////////////////////////////////// |
| 142 |
| 143 class GrGLQuadEffect : public GrGLEffect { |
| 144 public: |
| 145 GrGLQuadEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 146 |
| 147 virtual void emitCode(GrGLShaderBuilder* builder, |
| 148 const GrDrawEffect& drawEffect, |
| 149 EffectKey key, |
| 150 const char* outputColor, |
| 151 const char* inputColor, |
| 152 const TextureSamplerArray&) SK_OVERRIDE; |
| 153 |
| 154 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 155 |
| 156 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE {} |
| 157 |
| 158 private: |
| 159 bool fAntiAlias; |
| 160 bool fFill; |
| 161 |
| 162 typedef GrGLEffect INHERITED; |
| 163 }; |
| 164 |
| 165 GrGLQuadEffect::GrGLQuadEffect(const GrBackendEffectFactory& factory, |
| 166 const GrDrawEffect& drawEffect) |
| 167 : INHERITED (factory) { |
| 168 const GrQuadEffect& ce = drawEffect.castEffect<GrQuadEffect>(); |
| 169 fAntiAlias = ce.isAntiAliased(); |
| 170 fFill = ce.isFilled(); |
| 171 } |
| 172 |
| 173 void GrGLQuadEffect::emitCode(GrGLShaderBuilder* builder, |
| 174 const GrDrawEffect& drawEffect, |
| 175 EffectKey key, |
| 176 const char* outputColor, |
| 177 const char* inputColor, |
| 178 const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 179 const char *vsName, *fsName; |
| 180 const SkString* attrName = |
| 181 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 182 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n"); |
| 183 |
| 184 if (fAntiAlias) { |
| 185 SkAssertResult(builder->enableFeature( |
| 186 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 187 } |
| 188 builder->addVarying(kVec4f_GrSLType, "HairQuadEdge", &vsName, &fsName); |
| 189 |
| 190 if (fAntiAlias) { |
| 191 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 192 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
| 193 builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n
" |
| 194 "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\
n", |
| 195 fsName, fsName); |
| 196 } |
| 197 builder->fsCodeAppendf("\t\tfloat func = (%s.x*%s.x - %s.y);\n", fsName, fsN
ame, |
| 198 fsName); |
| 199 if (!fFill) { |
| 200 builder->fsCodeAppend("\t\tfunc = abs(func);\n"); |
| 201 } |
| 202 if (fAntiAlias) { |
| 203 builder->fsCodeAppend("\t\tedgeAlpha = func / sqrt(dot(gF, gF));\n"); |
| 204 } |
| 205 if (fFill) { |
| 206 if (fAntiAlias) { |
| 207 builder->fsCodeAppend("\t\tedgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1
.0);\n"); |
| 208 // Add line below for smooth cubic ramp |
| 209 // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2
.0*edgeAlpha);\n"); |
| 210 } else { |
| 211 builder->fsCodeAppend("\t\tedgeAlpha = float(func < 0.0);\n"); |
| 212 } |
| 213 } else { |
| 214 builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
| 215 // Add line below for smooth cubic ramp |
| 216 // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*e
dgeAlpha);\n"); |
| 217 } |
| 218 |
| 219 SkString modulate; |
| 220 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); |
| 221 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
| 222 |
| 223 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); |
| 224 } |
| 225 |
| 226 GrGLEffect::EffectKey GrGLQuadEffect::GenKey(const GrDrawEffect& drawEffect, con
st GrGLCaps&) { |
| 227 const GrQuadEffect& ce = drawEffect.castEffect<GrQuadEffect>(); |
| 228 return ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2; |
| 229 } |
| 230 |
| 231 ////////////////////////////////////////////////////////////////////////////// |
| 232 |
| 233 GrQuadEffect::~GrQuadEffect() {} |
| 234 |
| 235 const GrBackendEffectFactory& GrQuadEffect::getFactory() const SK_OVERRIDE { |
| 236 return GrTBackendEffectFactory<GrQuadEffect>::getInstance(); |
| 237 } |
| 238 |
| 239 GrQuadEffect::GrQuadEffect(const GrBezierEdgeType edgeType) : GrEffect() { |
| 240 this->addVertexAttrib(kVec4f_GrSLType); |
| 241 fAntiAlias = GrBezierEdgeTypeIsAA(edgeType) ; |
| 242 fFill = GrBezierEdgeTypeIsFill(edgeType); |
| 243 } |
| 244 |
| 245 bool GrQuadEffect::onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 246 const GrQuadEffect& ce = CastEffect<GrQuadEffect>(other); |
| 247 return (ce.fAntiAlias == fAntiAlias && ce.fFill == fFill); |
| 248 } |
| 249 |
| 250 ////////////////////////////////////////////////////////////////////////////// |
| 251 |
| 252 GR_DEFINE_EFFECT_TEST(GrQuadEffect); |
| 253 |
| 254 GrEffectRef* GrQuadEffect::TestCreate(SkMWCRandom* random, |
| 255 GrContext*, |
| 256 const GrDrawTargetCaps& caps, |
| 257 GrTexture*[]) { |
| 258 const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->next
ULessThan(3)); |
| 259 return GrQuadEffect::Create(edgeType, caps); |
| 260 } |
| 261 |
| 262 ////////////////////////////////////////////////////////////////////////////// |
| 263 // Cubic |
| 264 ////////////////////////////////////////////////////////////////////////////// |
| 265 |
| 266 class GrGLCubicEffect : public GrGLEffect { |
| 267 public: |
| 268 GrGLCubicEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 269 |
| 270 virtual void emitCode(GrGLShaderBuilder* builder, |
| 271 const GrDrawEffect& drawEffect, |
| 272 EffectKey key, |
| 273 const char* outputColor, |
| 274 const char* inputColor, |
| 275 const TextureSamplerArray&) SK_OVERRIDE; |
| 276 |
| 277 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 278 |
| 279 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE {} |
| 280 |
| 281 private: |
| 282 bool fAntiAlias; |
| 283 bool fFill; |
| 284 |
| 285 typedef GrGLEffect INHERITED; |
| 286 }; |
| 287 |
| 288 GrGLCubicEffect::GrGLCubicEffect(const GrBackendEffectFactory& factory, |
| 289 const GrDrawEffect& drawEffect) |
| 290 : INHERITED (factory) { |
| 291 const GrCubicEffect& ce = drawEffect.castEffect<GrCubicEffect>(); |
| 292 fAntiAlias = ce.isAntiAliased(); |
| 293 fFill = ce.isFilled(); |
| 294 } |
| 295 |
| 296 void GrGLCubicEffect::emitCode(GrGLShaderBuilder* builder, |
| 297 const GrDrawEffect& drawEffect, |
| 298 EffectKey key, |
| 299 const char* outputColor, |
| 300 const char* inputColor, |
| 301 const TextureSamplerArray& samplers) SK_OVERRIDE
{ |
| 302 const char *vsName, *fsName; |
| 303 |
| 304 if (fAntiAlias) { |
| 305 SkAssertResult(builder->enableFeature( |
| 306 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 307 } |
| 308 builder->addVarying(kVec4f_GrSLType, "CubicCoeffs", |
| 309 &vsName, &fsName); |
| 310 const SkString* attr0Name = |
| 311 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 312 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str()); |
| 313 |
| 314 builder->fsCodeAppend("\t\tfloat edgeAlpha;\n"); |
| 315 |
| 316 if (fAntiAlias) { |
| 317 builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName); |
| 318 builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName); |
| 319 builder->fsCodeAppendf("\t\tfloat dfdx =\n" |
| 320 "\t\t3.0*%s.x*%s.x*dklmdx.x - %s.y*dklmdx.z - %s.z*dk
lmdx.y;\n", |
| 321 fsName, fsName, fsName, fsName); |
| 322 builder->fsCodeAppendf("\t\tfloat dfdy =\n" |
| 323 "\t\t3.0*%s.x*%s.x*dklmdy.x - %s.y*dklmdy.z - %s.z*dk
lmdy.y;\n", |
| 324 fsName, fsName, fsName, fsName); |
| 325 builder->fsCodeAppend("\t\tvec2 gF = vec2(dfdx, dfdy);\n"); |
| 326 builder->fsCodeAppend("\t\tfloat gFM = sqrt(dot(gF, gF));\n"); |
| 327 } |
| 328 builder->fsCodeAppendf("\t\tfloat func = %s.x*%s.x*%s.x - %s.y*%s.z;\n", |
| 329 fsName, fsName, fsName, fsName, fsName); |
| 330 if (!fFill) { |
| 331 builder->fsCodeAppend("\t\tfunc = abs(func);\n"); |
| 332 } |
| 333 if (fAntiAlias) { |
| 334 builder->fsCodeAppend("\t\tedgeAlpha = func / gFM;\n"); |
| 335 } |
| 336 if (fFill) { |
| 337 if (fAntiAlias) { |
| 338 builder->fsCodeAppend("\t\tedgeAlpha = clamp(1.0 - edgeAlpha, 0.0, 1
.0);\n"); |
| 339 // Add line below for smooth cubic ramp |
| 340 // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2
.0*edgeAlpha);\n"); |
| 341 } else { |
| 342 builder->fsCodeAppend("\t\tedgeAlpha = float(func < 0.0);\n"); |
| 343 } |
| 344 } else { |
| 345 builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
| 346 // Add line below for smooth cubic ramp |
| 347 // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*e
dgeAlpha);\n"); |
| 348 } |
| 349 |
| 350 SkString modulate; |
| 351 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); |
| 352 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
| 353 } |
| 354 |
| 355 GrGLEffect::EffectKey GrGLCubicEffect::GenKey(const GrDrawEffect& drawEffect, co
nst GrGLCaps&) { |
| 356 const GrCubicEffect& ce = drawEffect.castEffect<GrCubicEffect>(); |
| 357 return ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2; |
| 358 } |
| 359 |
| 360 ////////////////////////////////////////////////////////////////////////////// |
| 361 |
| 362 GrCubicEffect::~GrCubicEffect() {} |
| 363 |
| 364 const GrBackendEffectFactory& GrCubicEffect::getFactory() const SK_OVERRIDE { |
| 365 return GrTBackendEffectFactory<GrCubicEffect>::getInstance(); |
| 366 } |
| 367 |
| 368 GrCubicEffect::GrCubicEffect(const GrBezierEdgeType edgeType) : GrEffect() { |
| 369 this->addVertexAttrib(kVec4f_GrSLType); |
| 370 fAntiAlias = GrBezierEdgeTypeIsAA(edgeType) ; |
| 371 fFill = GrBezierEdgeTypeIsFill(edgeType); |
| 372 } |
| 373 |
| 374 bool GrCubicEffect::onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 375 const GrCubicEffect& ce = CastEffect<GrCubicEffect>(other); |
| 376 return (ce.fAntiAlias == fAntiAlias && ce.fFill == fFill); |
| 377 } |
| 378 |
| 379 ////////////////////////////////////////////////////////////////////////////// |
| 380 |
| 381 GR_DEFINE_EFFECT_TEST(GrCubicEffect); |
| 382 |
| 383 GrEffectRef* GrCubicEffect::TestCreate(SkMWCRandom* random, |
| 384 GrContext*, |
| 385 const GrDrawTargetCaps& caps, |
| 386 GrTexture*[]) { |
| 387 const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->next
ULessThan(3)); |
| 388 return GrCubicEffect::Create(edgeType, caps); |
| 389 } |
OLD | NEW |