OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrEllipseEdgeEffect.h" | 8 #include "GrEllipseEdgeEffect.h" |
9 #include "gl/GrGLEffect.h" | 9 #include "gl/GrGLEffect.h" |
10 #include "gl/GrGLEffectMatrix.h" | 10 #include "gl/GrGLEffectMatrix.h" |
11 #include "gl/GrGLSL.h" | 11 #include "gl/GrGLSL.h" |
12 #include "gl/GrGLTexture.h" | 12 #include "gl/GrGLTexture.h" |
13 #include "GrTBackendEffectFactory.h" | 13 #include "GrTBackendEffectFactory.h" |
14 #include "GrTexture.h" | 14 #include "GrTexture.h" |
15 | 15 |
16 class GrGLEllipseEdgeEffect : public GrGLEffect { | 16 class GrGLEllipseEdgeEffect : public GrGLEffect { |
17 public: | 17 public: |
18 GrGLEllipseEdgeEffect(const GrBackendEffectFactory& factory, const GrEffectR
ef&) | 18 GrGLEllipseEdgeEffect(const GrBackendEffectFactory& factory, const GrDrawEff
ect&) |
19 : INHERITED (factory) {} | 19 : INHERITED (factory) {} |
20 | 20 |
21 virtual void emitCode(GrGLShaderBuilder* builder, | 21 virtual void emitCode(GrGLShaderBuilder* builder, |
22 const GrEffectStage& stage, | 22 const GrDrawEffect& drawEffect, |
23 EffectKey key, | 23 EffectKey key, |
24 const char* vertexCoords, | |
25 const char* outputColor, | 24 const char* outputColor, |
26 const char* inputColor, | 25 const char* inputColor, |
27 const TextureSamplerArray& samplers) SK_OVERRIDE { | 26 const TextureSamplerArray& samplers) SK_OVERRIDE { |
28 const GrEllipseEdgeEffect& effect = GetEffectFromStage<GrEllipseEdgeEffe
ct>(stage); | 27 const GrEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<GrEllip
seEdgeEffect>(); |
29 | 28 |
30 const char *vsCenterName, *fsCenterName; | 29 const char *vsCenterName, *fsCenterName; |
31 const char *vsEdgeName, *fsEdgeName; | 30 const char *vsEdgeName, *fsEdgeName; |
32 | 31 |
33 builder->addVarying(kVec2f_GrSLType, "EllipseCenter", &vsCenterName, &fs
CenterName); | 32 builder->addVarying(kVec2f_GrSLType, "EllipseCenter", &vsCenterName, &fs
CenterName); |
34 const SkString* attr0Name = | 33 const SkString* attr0Name = |
35 builder->getEffectAttributeName(stage.getVertexAttribIndices()[0]); | 34 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[
0]); |
36 builder->vsCodeAppendf("\t%s = %s;\n", vsCenterName, attr0Name->c_str())
; | 35 builder->vsCodeAppendf("\t%s = %s;\n", vsCenterName, attr0Name->c_str())
; |
37 | 36 |
38 builder->addVarying(kVec4f_GrSLType, "EllipseEdge", &vsEdgeName, &fsEdge
Name); | 37 builder->addVarying(kVec4f_GrSLType, "EllipseEdge", &vsEdgeName, &fsEdge
Name); |
39 const SkString* attr1Name = | 38 const SkString* attr1Name = |
40 builder->getEffectAttributeName(stage.getVertexAttribIndices()[1]); | 39 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[
1]); |
41 builder->vsCodeAppendf("\t%s = %s;\n", vsEdgeName, attr1Name->c_str()); | 40 builder->vsCodeAppendf("\t%s = %s;\n", vsEdgeName, attr1Name->c_str()); |
42 | 41 |
43 // translate to origin | 42 // translate to origin |
44 builder->fsCodeAppendf("\tvec2 outerOffset = (%s.xy - %s.xy);\n", | 43 builder->fsCodeAppendf("\tvec2 outerOffset = (%s.xy - %s.xy);\n", |
45 builder->fragmentPosition(), fsCenterName); | 44 builder->fragmentPosition(), fsCenterName); |
46 builder->fsCodeAppend("\tvec2 innerOffset = outerOffset;\n"); | 45 builder->fsCodeAppend("\tvec2 innerOffset = outerOffset;\n"); |
47 // scale y by xRadius/yRadius | 46 // scale y by xRadius/yRadius |
48 builder->fsCodeAppendf("\touterOffset.y *= %s.y;\n", fsEdgeName); | 47 builder->fsCodeAppendf("\touterOffset.y *= %s.y;\n", fsEdgeName); |
49 builder->fsCodeAppend("\tfloat dOuter = length(outerOffset);\n"); | 48 builder->fsCodeAppend("\tfloat dOuter = length(outerOffset);\n"); |
50 // compare outer lengths against xOuterRadius | 49 // compare outer lengths against xOuterRadius |
51 builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.x-dOuter, 0.0, 1.0)
;\n", fsEdgeName); | 50 builder->fsCodeAppendf("\tfloat edgeAlpha = clamp(%s.x-dOuter, 0.0, 1.0)
;\n", fsEdgeName); |
52 | 51 |
53 if (effect.isStroked()) { | 52 if (ellipseEffect.isStroked()) { |
54 builder->fsCodeAppendf("\tinnerOffset.y *= %s.w;\n", fsEdgeName); | 53 builder->fsCodeAppendf("\tinnerOffset.y *= %s.w;\n", fsEdgeName); |
55 builder->fsCodeAppend("\tfloat dInner = length(innerOffset);\n"); | 54 builder->fsCodeAppend("\tfloat dInner = length(innerOffset);\n"); |
56 | 55 |
57 // compare inner lengths against xInnerRadius | 56 // compare inner lengths against xInnerRadius |
58 builder->fsCodeAppendf("\tfloat innerAlpha = clamp(dInner-%s.z, 0.0,
1.0);\n", fsEdgeName); | 57 builder->fsCodeAppendf("\tfloat innerAlpha = clamp(dInner-%s.z, 0.0,
1.0);\n", fsEdgeName); |
59 builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n"); | 58 builder->fsCodeAppend("\tedgeAlpha *= innerAlpha;\n"); |
60 } | 59 } |
61 | 60 |
62 SkString modulate; | 61 SkString modulate; |
63 GrGLSLModulate4f(&modulate, inputColor, "edgeAlpha"); | 62 GrGLSLModulate4f(&modulate, inputColor, "edgeAlpha"); |
64 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); | 63 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
65 } | 64 } |
66 | 65 |
67 static inline EffectKey GenKey(const GrEffectStage& stage, const GrGLCaps&)
{ | 66 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCap
s&) { |
68 const GrEllipseEdgeEffect& effect = GetEffectFromStage<GrEllipseEdgeEffe
ct>(stage); | 67 const GrEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<GrEllip
seEdgeEffect>(); |
69 | 68 |
70 return effect.isStroked() ? 0x1 : 0x0; | 69 return ellipseEffect.isStroked() ? 0x1 : 0x0; |
71 } | 70 } |
72 | 71 |
73 virtual void setData(const GrGLUniformManager& uman, const GrEffectStage& st
age) SK_OVERRIDE { | 72 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE { |
74 } | 73 } |
75 | 74 |
76 private: | 75 private: |
77 typedef GrGLEffect INHERITED; | 76 typedef GrGLEffect INHERITED; |
78 }; | 77 }; |
79 | 78 |
80 /////////////////////////////////////////////////////////////////////////////// | 79 /////////////////////////////////////////////////////////////////////////////// |
81 | 80 |
82 GrEllipseEdgeEffect::GrEllipseEdgeEffect(bool stroke) : GrEffect() { | 81 GrEllipseEdgeEffect::GrEllipseEdgeEffect(bool stroke) : GrEffect() { |
83 this->addVertexAttrib(kVec2f_GrSLType); | 82 this->addVertexAttrib(kVec2f_GrSLType); |
(...skipping 12 matching lines...) Expand all Loading... |
96 | 95 |
97 /////////////////////////////////////////////////////////////////////////////// | 96 /////////////////////////////////////////////////////////////////////////////// |
98 | 97 |
99 GR_DEFINE_EFFECT_TEST(GrEllipseEdgeEffect); | 98 GR_DEFINE_EFFECT_TEST(GrEllipseEdgeEffect); |
100 | 99 |
101 GrEffectRef* GrEllipseEdgeEffect::TestCreate(SkMWCRandom* random, | 100 GrEffectRef* GrEllipseEdgeEffect::TestCreate(SkMWCRandom* random, |
102 GrContext* context, | 101 GrContext* context, |
103 GrTexture* textures[]) { | 102 GrTexture* textures[]) { |
104 return GrEllipseEdgeEffect::Create(random->nextBool()); | 103 return GrEllipseEdgeEffect::Create(random->nextBool()); |
105 } | 104 } |
OLD | NEW |