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