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