| 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" |
| 11 #include "gl/GrGLSL.h" | 11 #include "gl/GrGLSL.h" |
| 12 #include "GrTBackendEffectFactory.h" | 12 #include "GrTBackendEffectFactory.h" |
| 13 | 13 |
| 14 class GrGLConicEffect : public GrGLEffect { | 14 class GrGLConicEffect : public GrGLVertexEffect { |
| 15 public: | 15 public: |
| 16 GrGLConicEffect(const GrBackendEffectFactory&, const GrDrawEffect&); | 16 GrGLConicEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 17 | 17 |
| 18 virtual void emitCode(GrGLShaderBuilder* builder, | 18 virtual void emitCode(GrGLFullShaderBuilder* builder, |
| 19 const GrDrawEffect& drawEffect, | 19 const GrDrawEffect& drawEffect, |
| 20 EffectKey key, | 20 EffectKey key, |
| 21 const char* outputColor, | 21 const char* outputColor, |
| 22 const char* inputColor, | 22 const char* inputColor, |
| 23 const TransformedCoordsArray&, | 23 const TransformedCoordsArray&, |
| 24 const TextureSamplerArray&) SK_OVERRIDE; | 24 const TextureSamplerArray&) SK_OVERRIDE; |
| 25 | 25 |
| 26 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); | 26 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 27 | 27 |
| 28 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE {} | 28 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE {} |
| 29 | 29 |
| 30 private: | 30 private: |
| 31 GrBezierEdgeType fEdgeType; | 31 GrBezierEdgeType fEdgeType; |
| 32 | 32 |
| 33 typedef GrGLEffect INHERITED; | 33 typedef GrGLVertexEffect INHERITED; |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 GrGLConicEffect::GrGLConicEffect(const GrBackendEffectFactory& factory, | 36 GrGLConicEffect::GrGLConicEffect(const GrBackendEffectFactory& factory, |
| 37 const GrDrawEffect& drawEffect) | 37 const GrDrawEffect& drawEffect) |
| 38 : INHERITED (factory) { | 38 : INHERITED (factory) { |
| 39 const GrConicEffect& ce = drawEffect.castEffect<GrConicEffect>(); | 39 const GrConicEffect& ce = drawEffect.castEffect<GrConicEffect>(); |
| 40 fEdgeType = ce.getEdgeType(); | 40 fEdgeType = ce.getEdgeType(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void GrGLConicEffect::emitCode(GrGLShaderBuilder* builder, | 43 void GrGLConicEffect::emitCode(GrGLFullShaderBuilder* builder, |
| 44 const GrDrawEffect& drawEffect, | 44 const GrDrawEffect& drawEffect, |
| 45 EffectKey key, | 45 EffectKey key, |
| 46 const char* outputColor, | 46 const char* outputColor, |
| 47 const char* inputColor, | 47 const char* inputColor, |
| 48 const TransformedCoordsArray&, | 48 const TransformedCoordsArray&, |
| 49 const TextureSamplerArray& samplers) { | 49 const TextureSamplerArray& samplers) { |
| 50 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder(
); | |
| 51 SkASSERT(NULL != vertexBuilder); | |
| 52 | |
| 53 const char *vsName, *fsName; | 50 const char *vsName, *fsName; |
| 54 | 51 |
| 55 vertexBuilder->addVarying(kVec4f_GrSLType, "ConicCoeffs", | 52 builder->addVarying(kVec4f_GrSLType, "ConicCoeffs", |
| 56 &vsName, &fsName); | 53 &vsName, &fsName); |
| 57 const SkString* attr0Name = | 54 const SkString* attr0Name = |
| 58 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices(
)[0]); | 55 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 59 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str()); | 56 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str()); |
| 60 | 57 |
| 61 builder->fsCodeAppend("\t\tfloat edgeAlpha;\n"); | 58 builder->fsCodeAppend("\t\tfloat edgeAlpha;\n"); |
| 62 | 59 |
| 63 switch (fEdgeType) { | 60 switch (fEdgeType) { |
| 64 case kHairAA_GrBezierEdgeType: { | 61 case kHairAA_GrBezierEdgeType: { |
| 65 SkAssertResult(builder->enableFeature( | 62 SkAssertResult(builder->enableFeature( |
| 66 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); | 63 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 67 builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName); | 64 builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName); |
| 68 builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName); | 65 builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName); |
| 69 builder->fsCodeAppendf("\t\tfloat dfdx =\n" | 66 builder->fsCodeAppendf("\t\tfloat dfdx =\n" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 const GrDrawTargetCaps& caps, | 146 const GrDrawTargetCaps& caps, |
| 150 GrTexture*[]) { | 147 GrTexture*[]) { |
| 151 const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->next
ULessThan(3)); | 148 const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->next
ULessThan(3)); |
| 152 return GrConicEffect::Create(edgeType, caps); | 149 return GrConicEffect::Create(edgeType, caps); |
| 153 } | 150 } |
| 154 | 151 |
| 155 ////////////////////////////////////////////////////////////////////////////// | 152 ////////////////////////////////////////////////////////////////////////////// |
| 156 // Quad | 153 // Quad |
| 157 ////////////////////////////////////////////////////////////////////////////// | 154 ////////////////////////////////////////////////////////////////////////////// |
| 158 | 155 |
| 159 class GrGLQuadEffect : public GrGLEffect { | 156 class GrGLQuadEffect : public GrGLVertexEffect { |
| 160 public: | 157 public: |
| 161 GrGLQuadEffect(const GrBackendEffectFactory&, const GrDrawEffect&); | 158 GrGLQuadEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 162 | 159 |
| 163 virtual void emitCode(GrGLShaderBuilder* builder, | 160 virtual void emitCode(GrGLFullShaderBuilder* builder, |
| 164 const GrDrawEffect& drawEffect, | 161 const GrDrawEffect& drawEffect, |
| 165 EffectKey key, | 162 EffectKey key, |
| 166 const char* outputColor, | 163 const char* outputColor, |
| 167 const char* inputColor, | 164 const char* inputColor, |
| 168 const TransformedCoordsArray&, | 165 const TransformedCoordsArray&, |
| 169 const TextureSamplerArray&) SK_OVERRIDE; | 166 const TextureSamplerArray&) SK_OVERRIDE; |
| 170 | 167 |
| 171 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); | 168 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 172 | 169 |
| 173 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE {} | 170 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE {} |
| 174 | 171 |
| 175 private: | 172 private: |
| 176 GrBezierEdgeType fEdgeType; | 173 GrBezierEdgeType fEdgeType; |
| 177 | 174 |
| 178 typedef GrGLEffect INHERITED; | 175 typedef GrGLVertexEffect INHERITED; |
| 179 }; | 176 }; |
| 180 | 177 |
| 181 GrGLQuadEffect::GrGLQuadEffect(const GrBackendEffectFactory& factory, | 178 GrGLQuadEffect::GrGLQuadEffect(const GrBackendEffectFactory& factory, |
| 182 const GrDrawEffect& drawEffect) | 179 const GrDrawEffect& drawEffect) |
| 183 : INHERITED (factory) { | 180 : INHERITED (factory) { |
| 184 const GrQuadEffect& ce = drawEffect.castEffect<GrQuadEffect>(); | 181 const GrQuadEffect& ce = drawEffect.castEffect<GrQuadEffect>(); |
| 185 fEdgeType = ce.getEdgeType(); | 182 fEdgeType = ce.getEdgeType(); |
| 186 } | 183 } |
| 187 | 184 |
| 188 void GrGLQuadEffect::emitCode(GrGLShaderBuilder* builder, | 185 void GrGLQuadEffect::emitCode(GrGLFullShaderBuilder* builder, |
| 189 const GrDrawEffect& drawEffect, | 186 const GrDrawEffect& drawEffect, |
| 190 EffectKey key, | 187 EffectKey key, |
| 191 const char* outputColor, | 188 const char* outputColor, |
| 192 const char* inputColor, | 189 const char* inputColor, |
| 193 const TransformedCoordsArray&, | 190 const TransformedCoordsArray&, |
| 194 const TextureSamplerArray& samplers) { | 191 const TextureSamplerArray& samplers) { |
| 195 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder(
); | |
| 196 SkASSERT(NULL != vertexBuilder); | |
| 197 | |
| 198 const char *vsName, *fsName; | 192 const char *vsName, *fsName; |
| 199 | 193 |
| 200 const SkString* attrName = | 194 const SkString* attrName = |
| 201 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices(
)[0]); | 195 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 202 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n"); | 196 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n"); |
| 203 | 197 |
| 204 vertexBuilder->addVarying(kVec4f_GrSLType, "HairQuadEdge", &vsName, &fsName)
; | 198 builder->addVarying(kVec4f_GrSLType, "HairQuadEdge", &vsName, &fsName); |
| 205 | 199 |
| 206 switch (fEdgeType) { | 200 switch (fEdgeType) { |
| 207 case kHairAA_GrBezierEdgeType: { | 201 case kHairAA_GrBezierEdgeType: { |
| 208 SkAssertResult(builder->enableFeature( | 202 SkAssertResult(builder->enableFeature( |
| 209 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); | 203 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 210 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); | 204 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 211 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); | 205 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
| 212 builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.
y,\n" | 206 builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.
y,\n" |
| 213 "\t\t 2.0*%s.x*duvdy.x - duvdy.
y);\n", | 207 "\t\t 2.0*%s.x*duvdy.x - duvdy.
y);\n", |
| 214 fsName, fsName); | 208 fsName, fsName); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 241 fsName); | 235 fsName); |
| 242 builder->fsCodeAppend("\t\tedgeAlpha = float(edgeAlpha < 0.0);\n"); | 236 builder->fsCodeAppend("\t\tedgeAlpha = float(edgeAlpha < 0.0);\n"); |
| 243 break; | 237 break; |
| 244 } | 238 } |
| 245 } | 239 } |
| 246 | 240 |
| 247 SkString modulate; | 241 SkString modulate; |
| 248 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); | 242 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); |
| 249 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); | 243 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
| 250 | 244 |
| 251 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); | 245 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); |
| 252 } | 246 } |
| 253 | 247 |
| 254 GrGLEffect::EffectKey GrGLQuadEffect::GenKey(const GrDrawEffect& drawEffect, con
st GrGLCaps&) { | 248 GrGLEffect::EffectKey GrGLQuadEffect::GenKey(const GrDrawEffect& drawEffect, con
st GrGLCaps&) { |
| 255 const GrQuadEffect& ce = drawEffect.castEffect<GrQuadEffect>(); | 249 const GrQuadEffect& ce = drawEffect.castEffect<GrQuadEffect>(); |
| 256 return ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2; | 250 return ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2; |
| 257 } | 251 } |
| 258 | 252 |
| 259 ////////////////////////////////////////////////////////////////////////////// | 253 ////////////////////////////////////////////////////////////////////////////// |
| 260 | 254 |
| 261 GrQuadEffect::~GrQuadEffect() {} | 255 GrQuadEffect::~GrQuadEffect() {} |
| (...skipping 21 matching lines...) Expand all Loading... |
| 283 const GrDrawTargetCaps& caps, | 277 const GrDrawTargetCaps& caps, |
| 284 GrTexture*[]) { | 278 GrTexture*[]) { |
| 285 const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->next
ULessThan(3)); | 279 const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->next
ULessThan(3)); |
| 286 return GrQuadEffect::Create(edgeType, caps); | 280 return GrQuadEffect::Create(edgeType, caps); |
| 287 } | 281 } |
| 288 | 282 |
| 289 ////////////////////////////////////////////////////////////////////////////// | 283 ////////////////////////////////////////////////////////////////////////////// |
| 290 // Cubic | 284 // Cubic |
| 291 ////////////////////////////////////////////////////////////////////////////// | 285 ////////////////////////////////////////////////////////////////////////////// |
| 292 | 286 |
| 293 class GrGLCubicEffect : public GrGLEffect { | 287 class GrGLCubicEffect : public GrGLVertexEffect { |
| 294 public: | 288 public: |
| 295 GrGLCubicEffect(const GrBackendEffectFactory&, const GrDrawEffect&); | 289 GrGLCubicEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 296 | 290 |
| 297 virtual void emitCode(GrGLShaderBuilder* builder, | 291 virtual void emitCode(GrGLFullShaderBuilder* builder, |
| 298 const GrDrawEffect& drawEffect, | 292 const GrDrawEffect& drawEffect, |
| 299 EffectKey key, | 293 EffectKey key, |
| 300 const char* outputColor, | 294 const char* outputColor, |
| 301 const char* inputColor, | 295 const char* inputColor, |
| 302 const TransformedCoordsArray&, | 296 const TransformedCoordsArray&, |
| 303 const TextureSamplerArray&) SK_OVERRIDE; | 297 const TextureSamplerArray&) SK_OVERRIDE; |
| 304 | 298 |
| 305 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); | 299 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 306 | 300 |
| 307 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE {} | 301 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE {} |
| 308 | 302 |
| 309 private: | 303 private: |
| 310 GrBezierEdgeType fEdgeType; | 304 GrBezierEdgeType fEdgeType; |
| 311 | 305 |
| 312 typedef GrGLEffect INHERITED; | 306 typedef GrGLVertexEffect INHERITED; |
| 313 }; | 307 }; |
| 314 | 308 |
| 315 GrGLCubicEffect::GrGLCubicEffect(const GrBackendEffectFactory& factory, | 309 GrGLCubicEffect::GrGLCubicEffect(const GrBackendEffectFactory& factory, |
| 316 const GrDrawEffect& drawEffect) | 310 const GrDrawEffect& drawEffect) |
| 317 : INHERITED (factory) { | 311 : INHERITED (factory) { |
| 318 const GrCubicEffect& ce = drawEffect.castEffect<GrCubicEffect>(); | 312 const GrCubicEffect& ce = drawEffect.castEffect<GrCubicEffect>(); |
| 319 fEdgeType = ce.getEdgeType(); | 313 fEdgeType = ce.getEdgeType(); |
| 320 } | 314 } |
| 321 | 315 |
| 322 void GrGLCubicEffect::emitCode(GrGLShaderBuilder* builder, | 316 void GrGLCubicEffect::emitCode(GrGLFullShaderBuilder* builder, |
| 323 const GrDrawEffect& drawEffect, | 317 const GrDrawEffect& drawEffect, |
| 324 EffectKey key, | 318 EffectKey key, |
| 325 const char* outputColor, | 319 const char* outputColor, |
| 326 const char* inputColor, | 320 const char* inputColor, |
| 327 const TransformedCoordsArray&, | 321 const TransformedCoordsArray&, |
| 328 const TextureSamplerArray& samplers) { | 322 const TextureSamplerArray& samplers) { |
| 329 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder(
); | |
| 330 SkASSERT(NULL != vertexBuilder); | |
| 331 | |
| 332 const char *vsName, *fsName; | 323 const char *vsName, *fsName; |
| 333 | 324 |
| 334 vertexBuilder->addVarying(kVec4f_GrSLType, "CubicCoeffs", | 325 builder->addVarying(kVec4f_GrSLType, "CubicCoeffs", |
| 335 &vsName, &fsName); | 326 &vsName, &fsName); |
| 336 const SkString* attr0Name = | 327 const SkString* attr0Name = |
| 337 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices(
)[0]); | 328 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 338 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str()); | 329 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str()); |
| 339 | 330 |
| 340 builder->fsCodeAppend("\t\tfloat edgeAlpha;\n"); | 331 builder->fsCodeAppend("\t\tfloat edgeAlpha;\n"); |
| 341 | 332 |
| 342 switch (fEdgeType) { | 333 switch (fEdgeType) { |
| 343 case kHairAA_GrBezierEdgeType: { | 334 case kHairAA_GrBezierEdgeType: { |
| 344 SkAssertResult(builder->enableFeature( | 335 SkAssertResult(builder->enableFeature( |
| 345 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); | 336 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 346 builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName); | 337 builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName); |
| 347 builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName); | 338 builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName); |
| 348 builder->fsCodeAppendf("\t\tfloat dfdx =\n" | 339 builder->fsCodeAppendf("\t\tfloat dfdx =\n" |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 | 414 |
| 424 GR_DEFINE_EFFECT_TEST(GrCubicEffect); | 415 GR_DEFINE_EFFECT_TEST(GrCubicEffect); |
| 425 | 416 |
| 426 GrEffectRef* GrCubicEffect::TestCreate(SkRandom* random, | 417 GrEffectRef* GrCubicEffect::TestCreate(SkRandom* random, |
| 427 GrContext*, | 418 GrContext*, |
| 428 const GrDrawTargetCaps& caps, | 419 const GrDrawTargetCaps& caps, |
| 429 GrTexture*[]) { | 420 GrTexture*[]) { |
| 430 const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->next
ULessThan(3)); | 421 const GrBezierEdgeType edgeType = static_cast<GrBezierEdgeType>(random->next
ULessThan(3)); |
| 431 return GrCubicEffect::Create(edgeType, caps); | 422 return GrCubicEffect::Create(edgeType, caps); |
| 432 } | 423 } |
| OLD | NEW |