| 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 "GrCustomCoordsTextureEffect.h" | 8 #include "GrDistanceFieldTextureEffect.h" |
| 9 #include "gl/GrGLEffect.h" | 9 #include "gl/GrGLEffect.h" |
| 10 #include "gl/GrGLSL.h" | 10 #include "gl/GrGLSL.h" |
| 11 #include "gl/GrGLTexture.h" | 11 #include "gl/GrGLTexture.h" |
| 12 #include "gl/GrGLVertexEffect.h" | 12 #include "gl/GrGLVertexEffect.h" |
| 13 #include "GrTBackendEffectFactory.h" | 13 #include "GrTBackendEffectFactory.h" |
| 14 #include "GrTexture.h" | 14 #include "GrTexture.h" |
| 15 | 15 |
| 16 class GrGLCustomCoordsTextureEffect : public GrGLVertexEffect { | 16 // The distance field is constructed as unsigned char values, so that the zero v
alue is at 128. |
| 17 // Hence our zero threshold is 128/255. |
| 18 #define THRESHOLD "0.50196078431" |
| 19 |
| 20 class GrGLDistanceFieldTextureEffect : public GrGLVertexEffect { |
| 17 public: | 21 public: |
| 18 GrGLCustomCoordsTextureEffect(const GrBackendEffectFactory& factory, const G
rDrawEffect& drawEffect) | 22 GrGLDistanceFieldTextureEffect(const GrBackendEffectFactory& factory, const
GrDrawEffect& drawEffect) |
| 19 : INHERITED (factory) {} | 23 : INHERITED (factory) {} |
| 20 | 24 |
| 21 virtual void emitCode(GrGLFullShaderBuilder* builder, | 25 virtual void emitCode(GrGLFullShaderBuilder* builder, |
| 22 const GrDrawEffect& drawEffect, | 26 const GrDrawEffect& drawEffect, |
| 23 EffectKey key, | 27 EffectKey key, |
| 24 const char* outputColor, | 28 const char* outputColor, |
| 25 const char* inputColor, | 29 const char* inputColor, |
| 26 const TransformedCoordsArray&, | 30 const TransformedCoordsArray&, |
| 27 const TextureSamplerArray& samplers) SK_OVERRIDE { | 31 const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 28 SkASSERT(1 == drawEffect.castEffect<GrCustomCoordsTextureEffect>().numVe
rtexAttribs()); | 32 SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldTextureEffect>().numV
ertexAttribs()); |
| 29 | 33 |
| 30 SkString fsCoordName; | 34 SkString fsCoordName; |
| 31 const char* vsVaryingName; | 35 const char* vsVaryingName; |
| 32 const char* fsVaryingNamePtr; | 36 const char* fsVaryingNamePtr; |
| 33 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsVaryingName, &f
sVaryingNamePtr); | 37 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsVaryingName, &f
sVaryingNamePtr); |
| 34 fsCoordName = fsVaryingNamePtr; | 38 fsCoordName = fsVaryingNamePtr; |
| 35 | 39 |
| 36 const char* attrName = | 40 const char* attrName = |
| 37 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[
0])->c_str(); | 41 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[
0])->c_str(); |
| 38 builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, attrName); | 42 builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, attrName); |
| 39 | 43 |
| 40 builder->fsCodeAppendf("\t%s = ", outputColor); | 44 builder->fsCodeAppend("\tvec4 texColor = "); |
| 41 builder->fsAppendTextureLookupAndModulate(inputColor, | 45 builder->fsAppendTextureLookup(samplers[0], |
| 42 samplers[0], | 46 fsCoordName.c_str(), |
| 43 fsCoordName.c_str(), | 47 kVec2f_GrSLType); |
| 44 kVec2f_GrSLType); | |
| 45 builder->fsCodeAppend(";\n"); | 48 builder->fsCodeAppend(";\n"); |
| 49 builder->fsCodeAppend("\tfloat distance = texColor.r;\n"); |
| 50 // this gives us a smooth step across approximately one fragment |
| 51 // (assuming a radius of the diagonal of the fragment, hence a factor of
sqrt(2)/2) |
| 52 builder->fsCodeAppend("\tfloat afwidth = 0.7071*length(vec2(dFdx(distanc
e), dFdy(distance)));\n"); |
| 53 builder->fsCodeAppend("\tfloat val = smoothstep("THRESHOLD"-afwidth, "TH
RESHOLD"+afwidth, distance);\n"); |
| 54 |
| 55 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
| 56 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("val")
).c_str()); |
| 46 } | 57 } |
| 47 | 58 |
| 48 virtual void setData(const GrGLUniformManager& uman, | 59 virtual void setData(const GrGLUniformManager& uman, |
| 49 const GrDrawEffect& drawEffect) SK_OVERRIDE {} | 60 const GrDrawEffect& drawEffect) SK_OVERRIDE {} |
| 50 | 61 |
| 51 private: | 62 private: |
| 52 typedef GrGLVertexEffect INHERITED; | 63 typedef GrGLVertexEffect INHERITED; |
| 53 }; | 64 }; |
| 54 | 65 |
| 55 /////////////////////////////////////////////////////////////////////////////// | 66 /////////////////////////////////////////////////////////////////////////////// |
| 56 | 67 |
| 57 GrCustomCoordsTextureEffect::GrCustomCoordsTextureEffect(GrTexture* texture, | 68 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture, |
| 58 const GrTextureParams&
params) | 69 const GrTextureParams&
params) |
| 59 : fTextureAccess(texture, params) { | 70 : fTextureAccess(texture, params) { |
| 60 this->addTextureAccess(&fTextureAccess); | 71 this->addTextureAccess(&fTextureAccess); |
| 61 this->addVertexAttrib(kVec2f_GrSLType); | 72 this->addVertexAttrib(kVec2f_GrSLType); |
| 62 } | 73 } |
| 63 | 74 |
| 64 bool GrCustomCoordsTextureEffect::onIsEqual(const GrEffect& other) const { | 75 bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const { |
| 65 const GrCustomCoordsTextureEffect& cte = CastEffect<GrCustomCoordsTextureEff
ect>(other); | 76 const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureE
ffect>(other); |
| 66 return fTextureAccess == cte.fTextureAccess; | 77 return fTextureAccess == cte.fTextureAccess; |
| 67 } | 78 } |
| 68 | 79 |
| 69 void GrCustomCoordsTextureEffect::getConstantColorComponents(GrColor* color, | 80 void GrDistanceFieldTextureEffect::getConstantColorComponents(GrColor* color, |
| 70 uint32_t* validFlag
s) const { | 81 uint32_t* validFlag
s) const { |
| 71 if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color
) && | 82 if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color
) && |
| 72 GrPixelConfigIsOpaque(this->texture(0)->config())) { | 83 GrPixelConfigIsOpaque(this->texture(0)->config())) { |
| 73 *validFlags = kA_GrColorComponentFlag; | 84 *validFlags = kA_GrColorComponentFlag; |
| 74 } else { | 85 } else { |
| 75 *validFlags = 0; | 86 *validFlags = 0; |
| 76 } | 87 } |
| 77 } | 88 } |
| 78 | 89 |
| 79 const GrBackendEffectFactory& GrCustomCoordsTextureEffect::getFactory() const { | 90 const GrBackendEffectFactory& GrDistanceFieldTextureEffect::getFactory() const { |
| 80 return GrTBackendEffectFactory<GrCustomCoordsTextureEffect>::getInstance(); | 91 return GrTBackendEffectFactory<GrDistanceFieldTextureEffect>::getInstance(); |
| 81 } | 92 } |
| 82 | 93 |
| 83 /////////////////////////////////////////////////////////////////////////////// | 94 /////////////////////////////////////////////////////////////////////////////// |
| 84 | 95 |
| 85 GR_DEFINE_EFFECT_TEST(GrCustomCoordsTextureEffect); | 96 GR_DEFINE_EFFECT_TEST(GrDistanceFieldTextureEffect); |
| 86 | 97 |
| 87 GrEffectRef* GrCustomCoordsTextureEffect::TestCreate(SkRandom* random, | 98 GrEffectRef* GrDistanceFieldTextureEffect::TestCreate(SkRandom* random, |
| 88 GrContext*, | 99 GrContext*, |
| 89 const GrDrawTargetCaps&, | 100 const GrDrawTargetCaps&, |
| 90 GrTexture* textures[]) { | 101 GrTexture* textures[]) { |
| 91 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : | 102 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 92 GrEffectUnitTest::kAlphaTextureIdx; | 103 GrEffectUnitTest::kAlphaTextureIdx; |
| 93 static const SkShader::TileMode kTileModes[] = { | 104 static const SkShader::TileMode kTileModes[] = { |
| 94 SkShader::kClamp_TileMode, | 105 SkShader::kClamp_TileMode, |
| 95 SkShader::kRepeat_TileMode, | 106 SkShader::kRepeat_TileMode, |
| 96 SkShader::kMirror_TileMode, | 107 SkShader::kMirror_TileMode, |
| 97 }; | 108 }; |
| 98 SkShader::TileMode tileModes[] = { | 109 SkShader::TileMode tileModes[] = { |
| 99 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], | 110 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 100 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], | 111 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 101 }; | 112 }; |
| 102 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil
erp_FilterMode : | 113 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil
erp_FilterMode : |
| 103 GrTextureParams::kNon
e_FilterMode); | 114 GrTextureParams::kNon
e_FilterMode); |
| 104 | 115 |
| 105 return GrCustomCoordsTextureEffect::Create(textures[texIdx], params); | 116 return GrDistanceFieldTextureEffect::Create(textures[texIdx], params); |
| 106 } | 117 } |
| OLD | NEW |