|
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 "GrDistanceFieldTextureEffect.h" |
|
9 #include "gl/GrGLEffect.h" |
|
10 #include "gl/GrGLSL.h" |
|
11 #include "gl/GrGLTexture.h" |
|
12 #include "gl/GrGLVertexEffect.h" |
|
13 #include "GrTBackendEffectFactory.h" |
|
14 #include "GrTexture.h" |
|
15 |
|
16 // The distance field is constructed as unsigned char values, so that the zero value is at 128, |
|
17 // and the range is [-4, 4 - 1/255). Hence our multiplier is 8 - 1/32 and zero threshold is 128/255. |
|
18 #define MULTIPLIER "7.96875" |
|
19 #define THRESHOLD "0.50196078431" |
|
20 |
|
21 class GrGLDistanceFieldTextureEffect : public GrGLVertexEffect { |
|
22 public: |
|
23 GrGLDistanceFieldTextureEffect(const GrBackendEffectFactory& factory, |
|
24 const GrDrawEffect& drawEffect) |
|
25 : INHERITED (factory) |
|
26 , fTextureSize(SkSize::Make(-1.f,-1.f)) {} |
|
27 |
|
28 virtual void emitCode(GrGLFullShaderBuilder* builder, |
|
29 const GrDrawEffect& drawEffect, |
|
30 EffectKey key, |
|
31 const char* outputColor, |
|
32 const char* inputColor, |
|
33 const TransformedCoordsArray&, |
|
34 const TextureSamplerArray& samplers) SK_OVERRIDE { |
|
35 SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldTextureEffect>().numVertexAttribs()); |
|
36 |
|
37 SkAssertResult(builder->enableFeature(GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
|
38 |
|
39 SkString fsCoordName; |
|
40 const char* vsCoordName; |
|
41 const char* fsCoordNamePtr; |
|
42 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsCoordNamePtr); |
|
43 fsCoordName = fsCoordNamePtr; |
|
44 |
|
45 const char* attrName0 = |
|
46 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0])->c_str(); |
|
47 builder->vsCodeAppendf("\t%s = %s;\n", vsCoordName, attrName0); |
|
48 |
|
49 const char* textureSizeUniName = NULL; |
|
50 fTextureSizeUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
|
51 kVec2f_GrSLType, "TextureSize", |
|
52 &textureSizeUniName); |
|
53 |
|
54 builder->fsCodeAppend("\tvec4 texColor = "); |
|
55 builder->fsAppendTextureLookup(samplers[0], |
|
56 fsCoordName.c_str(), |
|
57 kVec2f_GrSLType); |
|
58 builder->fsCodeAppend(";\n"); |
|
59 builder->fsCodeAppend("\tfloat distance = " MULTIPLIER "*(texColor.r - " THRESHOLD ");\n"); |
|
60 |
|
61 // we adjust for the effect of the transformation on the distance by using |
|
62 // the length of the gradient of the texture coordinates. We use st coordinates |
|
63 // to ensure we're mapping 1:1 from texel space to pixel space. |
|
64 builder->fsCodeAppendf("\tvec2 st = %s*%s;\n", fsCoordName.c_str(), textureSizeUniName); |
|
65 builder->fsCodeAppend("\tvec2 Jdx = dFdx(st);\n"); |
|
66 builder->fsCodeAppend("\tvec2 Jdy = dFdy(st);\n"); |
|
67 builder->fsCodeAppend("\tvec2 st_grad = normalize(st);\n"); |
|
68 builder->fsCodeAppend("\tvec2 grad = vec2(st_grad.x*Jdx.x + st_grad.y*Jdy.x,\n"); |
|
69 builder->fsCodeAppend("\t st_grad.x*Jdx.y + st_grad.y*Jdy.y);\n"); |
|
70 |
|
71 // this gives us a smooth step across approximately one fragment |
|
72 // (assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/2) |
|
73 builder->fsCodeAppend("\tfloat afwidth = 0.7071*length(grad);\n"); |
|
74 builder->fsCodeAppend("\tfloat val = smoothstep(-afwidth, afwidth, distance);\n"); |
|
75 |
|
76 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
|
77 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("val")).c_str()); |
|
78 } |
|
79 |
|
80 virtual void setData(const GrGLUniformManager& uman, |
|
81 const GrDrawEffect& drawEffect) SK_OVERRIDE { |
|
82 SkASSERT(fTextureSizeUni.isValid()); |
|
83 const GrDistanceFieldTextureEffect& distanceFieldEffect = |
|
84 drawEffect.castEffect<GrDistanceFieldTextureEffect>(); |
|
85 if (distanceFieldEffect.getSize().width() != fTextureSize.width() || |
|
86 distanceFieldEffect.getSize().height() != fTextureSize.height()) { |
|
87 fTextureSize = distanceFieldEffect.getSize(); |
|
88 uman.set2f(fTextureSizeUni, |
|
89 distanceFieldEffect.getSize().width(), |
|
90 distanceFieldEffect.getSize().height()); |
|
91 } |
|
92 } |
|
93 |
|
94 private: |
|
95 GrGLUniformManager::UniformHandle fTextureSizeUni; |
|
96 SkSize fTextureSize; |
|
97 |
|
98 typedef GrGLVertexEffect INHERITED; |
|
99 }; |
|
100 |
|
101 /////////////////////////////////////////////////////////////////////////////// |
|
102 |
|
103 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture, |
|
104 const GrTextureParams& params, |
|
105 const SkISize& size) |
|
106 : fTextureAccess(texture, params) |
|
107 , fSize(SkSize::Make(SkIntToScalar(size.width()), SkIntToScalar(size.height()))) { |
|
108 this->addTextureAccess(&fTextureAccess); |
|
109 this->addVertexAttrib(kVec2f_GrSLType); |
|
110 } |
|
111 |
|
112 bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const { |
|
113 const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureEffect>(other); |
|
114 return fTextureAccess == cte.fTextureAccess; |
|
115 } |
|
116 |
|
117 void GrDistanceFieldTextureEffect::getConstantColorComponents(GrColor* color, |
|
118 uint32_t* validFlags) const { |
|
119 if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color) && |
|
120 GrPixelConfigIsOpaque(this->texture(0)->config())) { |
|
121 *validFlags = kA_GrColorComponentFlag; |
|
122 } else { |
|
123 *validFlags = 0; |
|
124 } |
|
125 } |
|
126 |
|
127 const GrBackendEffectFactory& GrDistanceFieldTextureEffect::getFactory() const { |
|
128 return GrTBackendEffectFactory<GrDistanceFieldTextureEffect>::getInstance(); |
|
129 } |
|
130 |
|
131 /////////////////////////////////////////////////////////////////////////////// |
|
132 |
|
133 GR_DEFINE_EFFECT_TEST(GrDistanceFieldTextureEffect); |
|
134 |
|
135 GrEffectRef* GrDistanceFieldTextureEffect::TestCreate(SkRandom* random, |
|
136 GrContext*, |
|
137 const GrDrawTargetCaps&, |
|
138 GrTexture* textures[]) { |
|
139 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
|
140 GrEffectUnitTest::kAlphaTextureIdx; |
|
141 static const SkShader::TileMode kTileModes[] = { |
|
142 SkShader::kClamp_TileMode, |
|
143 SkShader::kRepeat_TileMode, |
|
144 SkShader::kMirror_TileMode, |
|
145 }; |
|
146 SkShader::TileMode tileModes[] = { |
|
147 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
|
148 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
|
149 }; |
|
150 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
|
151 GrTextureParams::kNone_FilterMode); |
|
152 SkISize size = SkISize::Make(1024, 2048); |
|
153 |
|
154 return GrDistanceFieldTextureEffect::Create(textures[texIdx], params, size); |
|
155 } |