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 "GrDistanceFieldTextureEffect.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" |
(...skipping 17 matching lines...) Expand all Loading... |
28 virtual void emitCode(GrGLFullShaderBuilder* builder, | 28 virtual void emitCode(GrGLFullShaderBuilder* builder, |
29 const GrDrawEffect& drawEffect, | 29 const GrDrawEffect& drawEffect, |
30 EffectKey key, | 30 EffectKey key, |
31 const char* outputColor, | 31 const char* outputColor, |
32 const char* inputColor, | 32 const char* inputColor, |
33 const TransformedCoordsArray&, | 33 const TransformedCoordsArray&, |
34 const TextureSamplerArray& samplers) SK_OVERRIDE { | 34 const TextureSamplerArray& samplers) SK_OVERRIDE { |
35 SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldTextureEffect>().numV
ertexAttribs()); | 35 SkASSERT(1 == drawEffect.castEffect<GrDistanceFieldTextureEffect>().numV
ertexAttribs()); |
36 | 36 |
37 SkAssertResult(builder->enableFeature(GrGLShaderBuilder::kStandardDeriva
tives_GLSLFeature)); | 37 SkAssertResult(builder->enableFeature(GrGLShaderBuilder::kStandardDeriva
tives_GLSLFeature)); |
38 const GrDistanceFieldTextureEffect& dfTexEffect = | |
39 drawEffect.castEffect<GrDistanceFi
eldTextureEffect>(); | |
40 | 38 |
41 SkString fsCoordName; | 39 SkString fsCoordName; |
42 const char* vsCoordName; | 40 const char* vsCoordName; |
43 const char* fsCoordNamePtr; | 41 const char* fsCoordNamePtr; |
44 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsC
oordNamePtr); | 42 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsCoordName, &fsC
oordNamePtr); |
45 fsCoordName = fsCoordNamePtr; | 43 fsCoordName = fsCoordNamePtr; |
46 | 44 |
47 const char* attrName0 = | 45 const char* attrName0 = |
48 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[
0])->c_str(); | 46 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[
0])->c_str(); |
49 builder->vsCodeAppendf("\t%s = %s;\n", vsCoordName, attrName0); | 47 builder->vsCodeAppendf("\t%s = %s;\n", vsCoordName, attrName0); |
50 | 48 |
51 const char* textureSizeUniName = NULL; | 49 const char* textureSizeUniName = NULL; |
52 fTextureSizeUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visib
ility, | 50 fTextureSizeUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visib
ility, |
53 kVec2f_GrSLType, "TextureSize", | 51 kVec2f_GrSLType, "TextureSize", |
54 &textureSizeUniName); | 52 &textureSizeUniName); |
55 | 53 |
56 builder->fsCodeAppend("\tvec4 texColor = "); | 54 builder->fsCodeAppend("\tvec4 texColor = "); |
57 builder->fsAppendTextureLookup(samplers[0], | 55 builder->fsAppendTextureLookup(samplers[0], |
58 fsCoordName.c_str(), | 56 fsCoordName.c_str(), |
59 kVec2f_GrSLType); | 57 kVec2f_GrSLType); |
60 builder->fsCodeAppend(";\n"); | 58 builder->fsCodeAppend(";\n"); |
61 builder->fsCodeAppend("\tfloat distance = " MULTIPLIER "*(texColor.r - "
THRESHOLD ");\n"); | 59 builder->fsCodeAppend("\tfloat distance = " MULTIPLIER "*(texColor.r - "
THRESHOLD ");\n"); |
62 | 60 |
63 // we adjust for the effect of the transformation on the distance by usi
ng | 61 // we adjust for the effect of the transformation on the distance by usi
ng |
64 // the length of the gradient of the texture coordinates. We use st coor
dinates | 62 // the length of the gradient of the texture coordinates. We use st coor
dinates |
65 // to ensure we're mapping 1:1 from texel space to pixel space. | 63 // to ensure we're mapping 1:1 from texel space to pixel space. |
66 builder->fsCodeAppendf("\tvec2 uv = %s;\n", fsCoordName.c_str()); | 64 builder->fsCodeAppendf("\tvec2 st = %s*%s;\n", fsCoordName.c_str(), text
ureSizeUniName); |
67 builder->fsCodeAppendf("\tvec2 st = uv*%s;\n", textureSizeUniName); | 65 builder->fsCodeAppend("\tvec2 Jdx = dFdx(st);\n"); |
68 builder->fsCodeAppend("\tfloat afwidth;\n"); | 66 builder->fsCodeAppend("\tvec2 Jdy = dFdy(st);\n"); |
69 if (dfTexEffect.isUniformScale()) { | 67 builder->fsCodeAppend("\tvec2 st_grad = normalize(st);\n"); |
70 // this gives us a smooth step across approximately one fragment | 68 builder->fsCodeAppend("\tvec2 grad = vec2(st_grad.x*Jdx.x + st_grad.y*Jd
y.x,\n"); |
71 // (assuming a radius of the diagonal of the fragment, hence a facto
r of sqrt(2)/2) | 69 builder->fsCodeAppend("\t st_grad.x*Jdx.y + st_grad.y*Jd
y.y);\n"); |
72 builder->fsCodeAppend("\tafwidth = 0.7071*dFdx(st.x);\n"); | |
73 } else { | |
74 builder->fsCodeAppend("\tvec2 Jdx = dFdx(st);\n"); | |
75 builder->fsCodeAppend("\tvec2 Jdy = dFdy(st);\n"); | |
76 | 70 |
77 builder->fsCodeAppend("\tvec2 uv_grad;\n"); | 71 // this gives us a smooth step across approximately one fragment |
78 if (builder->ctxInfo().caps()->dropsTileOnZeroDivide()) { | 72 // (assuming a radius of the diagonal of the fragment, hence a factor of
sqrt(2)/2) |
79 // this is to compensate for the Adreno, which likes to drop til
es on division by 0 | 73 builder->fsCodeAppend("\tfloat afwidth = 0.7071*length(grad);\n"); |
80 builder->fsCodeAppend("\tfloat uv_len2 = dot(uv, uv);\n"); | |
81 builder->fsCodeAppend("\tif (uv_len2 < 0.0001) {\n"); | |
82 builder->fsCodeAppend("\t\tuv_grad = vec2(0.7071, 0.7071);\n"); | |
83 builder->fsCodeAppend("\t} else {\n"); | |
84 builder->fsCodeAppend("\t\tuv_grad = uv*inversesqrt(uv_len2);\n"
); | |
85 builder->fsCodeAppend("\t}\n"); | |
86 } else { | |
87 builder->fsCodeAppend("\tuv_grad = normalize(uv);\n"); | |
88 } | |
89 builder->fsCodeAppend("\tvec2 grad = vec2(uv_grad.x*Jdx.x + uv_grad.
y*Jdy.x,\n"); | |
90 builder->fsCodeAppend("\t uv_grad.x*Jdx.y + uv_grad.
y*Jdy.y);\n"); | |
91 | |
92 // this gives us a smooth step across approximately one fragment | |
93 // (assuming a radius of the diagonal of the fragment, hence a facto
r of sqrt(2)/2) | |
94 builder->fsCodeAppend("\tafwidth = 0.7071*length(grad);\n"); | |
95 } | |
96 | |
97 builder->fsCodeAppend("\tfloat val = smoothstep(-afwidth, afwidth, dista
nce);\n"); | 74 builder->fsCodeAppend("\tfloat val = smoothstep(-afwidth, afwidth, dista
nce);\n"); |
98 | 75 |
99 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, | 76 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
100 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("val")
).c_str()); | 77 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("val")
).c_str()); |
101 } | 78 } |
102 | 79 |
103 virtual void setData(const GrGLUniformManager& uman, | 80 virtual void setData(const GrGLUniformManager& uman, |
104 const GrDrawEffect& drawEffect) SK_OVERRIDE { | 81 const GrDrawEffect& drawEffect) SK_OVERRIDE { |
105 SkASSERT(fTextureSizeUni.isValid()); | 82 SkASSERT(fTextureSizeUni.isValid()); |
106 | 83 const GrDistanceFieldTextureEffect& distanceFieldEffect = |
107 GrTexture* texture = drawEffect.effect()->get()->texture(0); | 84 drawEffect.castEffect<GrDistanceFi
eldTextureEffect>(); |
108 if (texture->width() != fTextureSize.width() || | 85 if (distanceFieldEffect.getSize().width() != fTextureSize.width() || |
109 texture->height() != fTextureSize.height()) { | 86 distanceFieldEffect.getSize().height() != fTextureSize.height()) { |
110 fTextureSize = SkSize::Make(texture->width(), texture->height()); | 87 fTextureSize = distanceFieldEffect.getSize(); |
111 uman.set2f(fTextureSizeUni, | 88 uman.set2f(fTextureSizeUni, |
112 fTextureSize.width(), | 89 distanceFieldEffect.getSize().width(), |
113 fTextureSize.height()); | 90 distanceFieldEffect.getSize().height()); |
114 } | 91 } |
115 } | 92 } |
116 | 93 |
117 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCap
s&) { | |
118 const GrDistanceFieldTextureEffect& dfTexEffect = | |
119 drawEffect.castEffect<GrDistanceFi
eldTextureEffect>(); | |
120 | |
121 return dfTexEffect.isUniformScale() ? 0x1 : 0x0; | |
122 } | |
123 | |
124 private: | 94 private: |
125 GrGLUniformManager::UniformHandle fTextureSizeUni; | 95 GrGLUniformManager::UniformHandle fTextureSizeUni; |
126 SkSize fTextureSize; | 96 SkSize fTextureSize; |
127 | 97 |
128 typedef GrGLVertexEffect INHERITED; | 98 typedef GrGLVertexEffect INHERITED; |
129 }; | 99 }; |
130 | 100 |
131 /////////////////////////////////////////////////////////////////////////////// | 101 /////////////////////////////////////////////////////////////////////////////// |
132 | 102 |
133 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture, | 103 GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrTexture* texture, |
134 const GrTextureParams
& params, | 104 const GrTextureParams
& params, |
135 bool uniformScale) | 105 const SkISize& size) |
136 : fTextureAccess(texture, params) | 106 : fTextureAccess(texture, params) |
137 , fUniformScale(uniformScale) { | 107 , fSize(SkSize::Make(SkIntToScalar(size.width()), SkIntToScalar(size.height(
)))) { |
138 this->addTextureAccess(&fTextureAccess); | 108 this->addTextureAccess(&fTextureAccess); |
139 this->addVertexAttrib(kVec2f_GrSLType); | 109 this->addVertexAttrib(kVec2f_GrSLType); |
140 } | 110 } |
141 | 111 |
142 bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const { | 112 bool GrDistanceFieldTextureEffect::onIsEqual(const GrEffect& other) const { |
143 const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureE
ffect>(other); | 113 const GrDistanceFieldTextureEffect& cte = CastEffect<GrDistanceFieldTextureE
ffect>(other); |
144 return fTextureAccess == cte.fTextureAccess; | 114 return fTextureAccess == cte.fTextureAccess; |
145 } | 115 } |
146 | 116 |
147 void GrDistanceFieldTextureEffect::getConstantColorComponents(GrColor* color, | 117 void GrDistanceFieldTextureEffect::getConstantColorComponents(GrColor* color, |
(...skipping 24 matching lines...) Expand all Loading... |
172 SkShader::kClamp_TileMode, | 142 SkShader::kClamp_TileMode, |
173 SkShader::kRepeat_TileMode, | 143 SkShader::kRepeat_TileMode, |
174 SkShader::kMirror_TileMode, | 144 SkShader::kMirror_TileMode, |
175 }; | 145 }; |
176 SkShader::TileMode tileModes[] = { | 146 SkShader::TileMode tileModes[] = { |
177 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], | 147 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
178 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], | 148 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
179 }; | 149 }; |
180 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil
erp_FilterMode : | 150 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil
erp_FilterMode : |
181 GrTextureParams::kNon
e_FilterMode); | 151 GrTextureParams::kNon
e_FilterMode); |
| 152 SkISize size = SkISize::Make(1024, 2048); |
182 | 153 |
183 return GrDistanceFieldTextureEffect::Create(textures[texIdx], params, random
->nextBool()); | 154 return GrDistanceFieldTextureEffect::Create(textures[texIdx], params, size); |
184 } | 155 } |
OLD | NEW |