| 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 "GrEdgeEffect.h" |
| 9 #include "gl/GrGLEffect.h" |
| 10 #include "gl/GrGLSL.h" |
| 11 #include "GrTBackendEffectFactory.h" |
| 12 |
| 13 |
| 14 class GrGLEdgeEffect : public GrGLEffect { |
| 15 public: |
| 16 GrGLEdgeEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 17 : INHERITED (factory) {} |
| 18 |
| 19 virtual void emitCode(GrGLShaderBuilder* builder, |
| 20 const GrDrawEffect& drawEffect, |
| 21 EffectKey key, |
| 22 const char* outputColor, |
| 23 const char* inputColor, |
| 24 const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 25 const GrEdgeEffect& edgeEffect = drawEffect.castEffect<GrEdgeEffect>(); |
| 26 GrEdgeEffect::EdgeType type = edgeEffect.edgeType(); |
| 27 |
| 28 const char *vsName, *fsName; |
| 29 const SkString* attrName = |
| 30 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[
0]); |
| 31 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n"); |
| 32 |
| 33 switch (type) { |
| 34 case GrEdgeEffect::kHairLine_EdgeType: |
| 35 builder->addVarying(kVec4f_GrSLType, "HairEdge", &vsName, &fsName); |
| 36 |
| 37 builder->fsCodeAppendf("\t\tedgeAlpha = abs(dot(vec3(%s.xy,1), %s.xy
z));\n", |
| 38 builder->fragmentPosition(), fsName); |
| 39 builder->fsCodeAppendf("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n
"); |
| 40 break; |
| 41 case GrEdgeEffect::kQuad_EdgeType: |
| 42 builder->addVarying(kVec4f_GrSLType, "QuadEdge", &vsName, &fsName); |
| 43 |
| 44 // keep the derivative instructions outside the conditional |
| 45 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 46 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
| 47 builder->fsCodeAppendf("\t\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsNa
me, fsName); |
| 48 // today we know z and w are in device space. We could use derivativ
es |
| 49 builder->fsCodeAppendf("\t\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5,
1.0);\n", fsName, |
| 50 fsName); |
| 51 builder->fsCodeAppendf ("\t\t} else {\n"); |
| 52 builder->fsCodeAppendf("\t\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvd
x.y,\n" |
| 53 "\t\t\t 2.0*%s.x*duvdy.x - duvd
y.y);\n", |
| 54 fsName, fsName); |
| 55 builder->fsCodeAppendf("\t\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fs
Name, fsName, |
| 56 fsName); |
| 57 builder->fsCodeAppendf("\t\t\tedgeAlpha = " |
| 58 "clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0
);\n\t\t}\n"); |
| 59 if (kES2_GrGLBinding == builder->ctxInfo().binding()) { |
| 60 builder->fHeader.append("#extension GL_OES_standard_derivatives:
enable\n"); |
| 61 } |
| 62 break; |
| 63 case GrEdgeEffect::kHairQuad_EdgeType: |
| 64 builder->addVarying(kVec4f_GrSLType, "HairQuadEdge", &vsName, &fsNam
e); |
| 65 |
| 66 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 67 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
| 68 builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.
y,\n" |
| 69 "\t\t 2.0*%s.x*duvdy.x - duvdy.
y);\n", |
| 70 fsName, fsName); |
| 71 builder->fsCodeAppendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsNa
me, fsName, |
| 72 fsName); |
| 73 builder->fsCodeAppend("\t\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / do
t(gF, gF));\n"); |
| 74 builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"
); |
| 75 if (kES2_GrGLBinding == builder->ctxInfo().binding()) { |
| 76 builder->fHeader.append("#extension GL_OES_standard_derivatives:
enable\n"); |
| 77 } |
| 78 break; |
| 79 }; |
| 80 |
| 81 SkString modulate; |
| 82 GrGLSLModulate4f(&modulate, inputColor, "edgeAlpha"); |
| 83 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
| 84 |
| 85 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); |
| 86 } |
| 87 |
| 88 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCap
s&) { |
| 89 const GrEdgeEffect& QuadEffect = drawEffect.castEffect<GrEdgeEffect>(); |
| 90 |
| 91 return QuadEffect.edgeType(); |
| 92 } |
| 93 |
| 94 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE { |
| 95 } |
| 96 |
| 97 private: |
| 98 typedef GrGLEffect INHERITED; |
| 99 }; |
| 100 |
| 101 /////////////////////////////////////////////////////////////////////////////// |
| 102 |
| 103 GrEdgeEffect::GrEdgeEffect(EdgeType edgeType) : GrEffect() { |
| 104 if (edgeType == kQuad_EdgeType) { |
| 105 this->addVertexAttrib(kVec4f_GrSLType); |
| 106 } else { |
| 107 this->addVertexAttrib(kVec4f_GrSLType); // someday this will matter |
| 108 } |
| 109 fEdgeType = edgeType; |
| 110 } |
| 111 |
| 112 void GrEdgeEffect::getConstantColorComponents(GrColor* color, uint32_t* validFla
gs) const { |
| 113 *validFlags = 0; |
| 114 } |
| 115 |
| 116 const GrBackendEffectFactory& GrEdgeEffect::getFactory() const { |
| 117 return GrTBackendEffectFactory<GrEdgeEffect>::getInstance(); |
| 118 } |
| 119 |
| 120 /////////////////////////////////////////////////////////////////////////////// |
| 121 |
| 122 GR_DEFINE_EFFECT_TEST(GrEdgeEffect); |
| 123 |
| 124 GrEffectRef* GrEdgeEffect::TestCreate(SkMWCRandom* random, |
| 125 GrContext* context, |
| 126 GrTexture* textures[]) { |
| 127 return GrEdgeEffect::Create(static_cast<EdgeType>(random->nextULessThan(kEdg
eTypeCount))); |
| 128 } |
| OLD | NEW |