OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 "GrTextureDomainEffect.h" | 8 #include "GrTextureDomainEffect.h" |
9 #include "GrSimpleTextureEffect.h" | 9 #include "GrSimpleTextureEffect.h" |
10 #include "GrTBackendEffectFactory.h" | 10 #include "GrTBackendEffectFactory.h" |
11 #include "gl/GrGLEffect.h" | 11 #include "gl/GrGLEffect.h" |
12 #include "gl/GrGLEffectMatrix.h" | |
13 #include "SkFloatingPoint.h" | 12 #include "SkFloatingPoint.h" |
14 | 13 |
15 class GrGLTextureDomainEffect : public GrGLEffect { | 14 class GrGLTextureDomainEffect : public GrGLEffect { |
16 public: | 15 public: |
17 GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrDrawEffect&); | 16 GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
18 | 17 |
19 virtual void emitCode(GrGLShaderBuilder*, | 18 virtual void emitCode(GrGLShaderBuilder*, |
20 const GrDrawEffect&, | 19 const GrDrawEffect&, |
21 EffectKey, | 20 EffectKey, |
22 const char* outputColor, | 21 const char* outputColor, |
23 const char* inputColor, | 22 const char* inputColor, |
| 23 const TransformedCoordsArray&, |
24 const TextureSamplerArray&) SK_OVERRIDE; | 24 const TextureSamplerArray&) SK_OVERRIDE; |
25 | 25 |
26 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; | 26 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; |
27 | 27 |
28 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); | 28 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
29 | 29 |
30 private: | 30 private: |
31 GrGLUniformManager::UniformHandle fNameUni; | 31 GrGLUniformManager::UniformHandle fNameUni; |
32 GrGLEffectMatrix fEffectMatrix; | |
33 GrGLfloat fPrevDomain[4]; | 32 GrGLfloat fPrevDomain[4]; |
34 | 33 |
35 typedef GrGLEffect INHERITED; | 34 typedef GrGLEffect INHERITED; |
36 }; | 35 }; |
37 | 36 |
38 GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& f
actory, | 37 GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& f
actory, |
39 const GrDrawEffect& drawEffect) | 38 const GrDrawEffect&) |
40 : INHERITED(factory) | 39 : INHERITED(factory) { |
41 , fEffectMatrix(drawEffect.castEffect<GrTextureDomainEffect>().coordsType())
{ | |
42 fPrevDomain[0] = SK_FloatNaN; | 40 fPrevDomain[0] = SK_FloatNaN; |
43 } | 41 } |
44 | 42 |
45 void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder, | 43 void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder, |
46 const GrDrawEffect& drawEffect, | 44 const GrDrawEffect& drawEffect, |
47 EffectKey key, | 45 EffectKey key, |
48 const char* outputColor, | 46 const char* outputColor, |
49 const char* inputColor, | 47 const char* inputColor, |
| 48 const TransformedCoordsArray& coords, |
50 const TextureSamplerArray& samplers) { | 49 const TextureSamplerArray& samplers) { |
51 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainE
ffect>(); | 50 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainE
ffect>(); |
52 | 51 |
53 SkString coords; | 52 SkString coords2D = builder->ensureFSCoords2D(coords, 0); |
54 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords); | |
55 const char* domain; | 53 const char* domain; |
56 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, | 54 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
57 kVec4f_GrSLType, "TexDom", &domain); | 55 kVec4f_GrSLType, "TexDom", &domain); |
58 if (GrTextureDomainEffect::kClamp_WrapMode == texDom.wrapMode()) { | 56 if (GrTextureDomainEffect::kClamp_WrapMode == texDom.wrapMode()) { |
59 | 57 |
60 builder->fsCodeAppendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n", | 58 builder->fsCodeAppendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n", |
61 coords.c_str(), domain, domain); | 59 coords2D.c_str(), domain, domain); |
62 | 60 |
63 builder->fsCodeAppendf("\t%s = ", outputColor); | 61 builder->fsCodeAppendf("\t%s = ", outputColor); |
64 builder->fsAppendTextureLookupAndModulate(inputColor, samplers[0], "clam
pCoord"); | 62 builder->fsAppendTextureLookupAndModulate(inputColor, samplers[0], "clam
pCoord"); |
65 builder->fsCodeAppend(";\n"); | 63 builder->fsCodeAppend(";\n"); |
66 } else { | 64 } else { |
67 SkASSERT(GrTextureDomainEffect::kDecal_WrapMode == texDom.wrapMode()); | 65 SkASSERT(GrTextureDomainEffect::kDecal_WrapMode == texDom.wrapMode()); |
68 | 66 |
69 if (kImagination_GrGLVendor == builder->ctxInfo().vendor()) { | 67 if (kImagination_GrGLVendor == builder->ctxInfo().vendor()) { |
70 // On the NexusS and GalaxyNexus, the other path (with the 'any' | 68 // On the NexusS and GalaxyNexus, the other path (with the 'any' |
71 // call) causes the compilation error "Calls to any function that | 69 // call) causes the compilation error "Calls to any function that |
72 // may require a gradient calculation inside a conditional block | 70 // may require a gradient calculation inside a conditional block |
73 // may return undefined results". This appears to be an issue with | 71 // may return undefined results". This appears to be an issue with |
74 // the 'any' call since even the simple "result=black; if (any()) | 72 // the 'any' call since even the simple "result=black; if (any()) |
75 // result=white;" code fails to compile. | 73 // result=white;" code fails to compile. |
76 builder->fsCodeAppend("\tvec4 outside = vec4(0.0, 0.0, 0.0, 0.0);\n"
); | 74 builder->fsCodeAppend("\tvec4 outside = vec4(0.0, 0.0, 0.0, 0.0);\n"
); |
77 builder->fsCodeAppend("\tvec4 inside = "); | 75 builder->fsCodeAppend("\tvec4 inside = "); |
78 builder->fsAppendTextureLookupAndModulate(inputColor, samplers[0], c
oords.c_str()); | 76 builder->fsAppendTextureLookupAndModulate(inputColor, samplers[0], c
oords2D.c_str()); |
79 builder->fsCodeAppend(";\n"); | 77 builder->fsCodeAppend(";\n"); |
80 | 78 |
81 builder->fsCodeAppendf("\tfloat x = abs(2.0*(%s.x - %s.x)/(%s.z - %s
.x) - 1.0);\n", | 79 builder->fsCodeAppendf("\tfloat x = abs(2.0*(%s.x - %s.x)/(%s.z - %s
.x) - 1.0);\n", |
82 coords.c_str(), domain, domain, domain); | 80 coords2D.c_str(), domain, domain, domain); |
83 builder->fsCodeAppendf("\tfloat y = abs(2.0*(%s.y - %s.y)/(%s.w - %s
.y) - 1.0);\n", | 81 builder->fsCodeAppendf("\tfloat y = abs(2.0*(%s.y - %s.y)/(%s.w - %s
.y) - 1.0);\n", |
84 coords.c_str(), domain, domain, domain); | 82 coords2D.c_str(), domain, domain, domain); |
85 builder->fsCodeAppend("\tfloat blend = step(1.0, max(x, y));\n"); | 83 builder->fsCodeAppend("\tfloat blend = step(1.0, max(x, y));\n"); |
86 builder->fsCodeAppendf("\t%s = mix(inside, outside, blend);\n", outp
utColor); | 84 builder->fsCodeAppendf("\t%s = mix(inside, outside, blend);\n", outp
utColor); |
87 } else { | 85 } else { |
88 builder->fsCodeAppend("\tbvec4 outside;\n"); | 86 builder->fsCodeAppend("\tbvec4 outside;\n"); |
89 builder->fsCodeAppendf("\toutside.xy = lessThan(%s, %s.xy);\n", coor
ds.c_str(), domain); | 87 builder->fsCodeAppendf("\toutside.xy = lessThan(%s, %s.xy);\n", coor
ds2D.c_str(), domain); |
90 builder->fsCodeAppendf("\toutside.zw = greaterThan(%s, %s.zw);\n", c
oords.c_str(), domain); | 88 builder->fsCodeAppendf("\toutside.zw = greaterThan(%s, %s.zw);\n", c
oords2D.c_str(), domain); |
91 builder->fsCodeAppendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.
0) : ", outputColor); | 89 builder->fsCodeAppendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.
0) : ", outputColor); |
92 builder->fsAppendTextureLookupAndModulate(inputColor, samplers[0], c
oords.c_str()); | 90 builder->fsAppendTextureLookupAndModulate(inputColor, samplers[0], c
oords2D.c_str()); |
93 builder->fsCodeAppend(";\n"); | 91 builder->fsCodeAppend(";\n"); |
94 } | 92 } |
95 } | 93 } |
96 } | 94 } |
97 | 95 |
98 void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, | 96 void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, |
99 const GrDrawEffect& drawEffect) { | 97 const GrDrawEffect& drawEffect) { |
100 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainE
ffect>(); | 98 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainE
ffect>(); |
101 const SkRect& domain = texDom.domain(); | 99 const SkRect& domain = texDom.domain(); |
102 | 100 |
103 float values[4] = { | 101 float values[4] = { |
104 SkScalarToFloat(domain.left()), | 102 SkScalarToFloat(domain.left()), |
105 SkScalarToFloat(domain.top()), | 103 SkScalarToFloat(domain.top()), |
106 SkScalarToFloat(domain.right()), | 104 SkScalarToFloat(domain.right()), |
107 SkScalarToFloat(domain.bottom()) | 105 SkScalarToFloat(domain.bottom()) |
108 }; | 106 }; |
109 // vertical flip if necessary | 107 // vertical flip if necessary |
110 if (kBottomLeft_GrSurfaceOrigin == texDom.texture(0)->origin()) { | 108 if (kBottomLeft_GrSurfaceOrigin == texDom.texture(0)->origin()) { |
111 values[1] = 1.0f - values[1]; | 109 values[1] = 1.0f - values[1]; |
112 values[3] = 1.0f - values[3]; | 110 values[3] = 1.0f - values[3]; |
113 // The top and bottom were just flipped, so correct the ordering | 111 // The top and bottom were just flipped, so correct the ordering |
114 // of elements so that values = (l, t, r, b). | 112 // of elements so that values = (l, t, r, b). |
115 SkTSwap(values[1], values[3]); | 113 SkTSwap(values[1], values[3]); |
116 } | 114 } |
117 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) { | 115 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) { |
118 uman.set4fv(fNameUni, 0, 1, values); | 116 uman.set4fv(fNameUni, 0, 1, values); |
119 memcpy(fPrevDomain, values, 4 * sizeof(GrGLfloat)); | 117 memcpy(fPrevDomain, values, 4 * sizeof(GrGLfloat)); |
120 } | 118 } |
121 fEffectMatrix.setData(uman, | |
122 texDom.getMatrix(), | |
123 drawEffect, | |
124 texDom.texture(0)); | |
125 } | 119 } |
126 | 120 |
127 GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrDrawEffect& drawEf
fect, | 121 GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrDrawEffect& drawEf
fect, |
128 const GrGLCaps&) { | 122 const GrGLCaps&) { |
129 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainE
ffect>(); | 123 return drawEffect.castEffect<GrTextureDomainEffect>().wrapMode(); |
130 EffectKey key = texDom.wrapMode(); | |
131 key <<= GrGLEffectMatrix::kKeyBits; | |
132 EffectKey matrixKey = GrGLEffectMatrix::GenKey(texDom.getMatrix(), | |
133 drawEffect, | |
134 texDom.coordsType(), | |
135 texDom.texture(0)); | |
136 return key | matrixKey; | |
137 } | 124 } |
138 | 125 |
139 | 126 |
140 /////////////////////////////////////////////////////////////////////////////// | 127 /////////////////////////////////////////////////////////////////////////////// |
141 | 128 |
142 GrEffectRef* GrTextureDomainEffect::Create(GrTexture* texture, | 129 GrEffectRef* GrTextureDomainEffect::Create(GrTexture* texture, |
143 const SkMatrix& matrix, | 130 const SkMatrix& matrix, |
144 const SkRect& domain, | 131 const SkRect& domain, |
145 WrapMode wrapMode, | 132 WrapMode wrapMode, |
146 GrTextureParams::FilterMode filterMod
e, | 133 GrTextureParams::FilterMode filterMod
e, |
147 CoordsType coordsType) { | 134 GrCoordSet coordSet) { |
148 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1}; | 135 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1}; |
149 if (kClamp_WrapMode == wrapMode && domain.contains(kFullRect)) { | 136 if (kClamp_WrapMode == wrapMode && domain.contains(kFullRect)) { |
150 return GrSimpleTextureEffect::Create(texture, matrix, filterMode); | 137 return GrSimpleTextureEffect::Create(texture, matrix, filterMode); |
151 } else { | 138 } else { |
152 SkRect clippedDomain; | 139 SkRect clippedDomain; |
153 // We don't currently handle domains that are empty or don't intersect t
he texture. | 140 // We don't currently handle domains that are empty or don't intersect t
he texture. |
154 // It is OK if the domain rect is a line or point, but it should not be
inverted. We do not | 141 // It is OK if the domain rect is a line or point, but it should not be
inverted. We do not |
155 // handle rects that do not intersect the [0..1]x[0..1] rect. | 142 // handle rects that do not intersect the [0..1]x[0..1] rect. |
156 SkASSERT(domain.fLeft <= domain.fRight); | 143 SkASSERT(domain.fLeft <= domain.fRight); |
157 SkASSERT(domain.fTop <= domain.fBottom); | 144 SkASSERT(domain.fTop <= domain.fBottom); |
158 clippedDomain.fLeft = SkMaxScalar(domain.fLeft, kFullRect.fLeft); | 145 clippedDomain.fLeft = SkMaxScalar(domain.fLeft, kFullRect.fLeft); |
159 clippedDomain.fRight = SkMinScalar(domain.fRight, kFullRect.fRight); | 146 clippedDomain.fRight = SkMinScalar(domain.fRight, kFullRect.fRight); |
160 clippedDomain.fTop = SkMaxScalar(domain.fTop, kFullRect.fTop); | 147 clippedDomain.fTop = SkMaxScalar(domain.fTop, kFullRect.fTop); |
161 clippedDomain.fBottom = SkMinScalar(domain.fBottom, kFullRect.fBottom); | 148 clippedDomain.fBottom = SkMinScalar(domain.fBottom, kFullRect.fBottom); |
162 SkASSERT(clippedDomain.fLeft <= clippedDomain.fRight); | 149 SkASSERT(clippedDomain.fLeft <= clippedDomain.fRight); |
163 SkASSERT(clippedDomain.fTop <= clippedDomain.fBottom); | 150 SkASSERT(clippedDomain.fTop <= clippedDomain.fBottom); |
164 | 151 |
165 AutoEffectUnref effect(SkNEW_ARGS(GrTextureDomainEffect, (texture, | 152 AutoEffectUnref effect(SkNEW_ARGS(GrTextureDomainEffect, (texture, |
166 matrix, | 153 matrix, |
167 clippedDomain, | 154 clippedDomain, |
168 wrapMode, | 155 wrapMode, |
169 filterMode, | 156 filterMode, |
170 coordsType))); | 157 coordSet))); |
171 return CreateEffectRef(effect); | 158 return CreateEffectRef(effect); |
172 | 159 |
173 } | 160 } |
174 } | 161 } |
175 | 162 |
176 GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, | 163 GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, |
177 const SkMatrix& matrix, | 164 const SkMatrix& matrix, |
178 const SkRect& domain, | 165 const SkRect& domain, |
179 WrapMode wrapMode, | 166 WrapMode wrapMode, |
180 GrTextureParams::FilterMode filterM
ode, | 167 GrTextureParams::FilterMode filterM
ode, |
181 CoordsType coordsType) | 168 GrCoordSet coordSet) |
182 : GrSingleTextureEffect(texture, matrix, filterMode, coordsType) | 169 : GrSingleTextureEffect(texture, matrix, filterMode, coordSet) |
183 , fWrapMode(wrapMode) | 170 , fWrapMode(wrapMode) |
184 , fTextureDomain(domain) { | 171 , fTextureDomain(domain) { |
185 } | 172 } |
186 | 173 |
187 GrTextureDomainEffect::~GrTextureDomainEffect() { | 174 GrTextureDomainEffect::~GrTextureDomainEffect() { |
188 | 175 |
189 } | 176 } |
190 | 177 |
191 const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const { | 178 const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const { |
192 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance(); | 179 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance(); |
193 } | 180 } |
194 | 181 |
195 bool GrTextureDomainEffect::onIsEqual(const GrEffect& sBase) const { | 182 bool GrTextureDomainEffect::onIsEqual(const GrEffect& sBase) const { |
196 const GrTextureDomainEffect& s = CastEffect<GrTextureDomainEffect>(sBase); | 183 const GrTextureDomainEffect& s = CastEffect<GrTextureDomainEffect>(sBase); |
197 return this->hasSameTextureParamsMatrixAndCoordsType(s) && | 184 return this->hasSameTextureParamsMatrixAndSourceCoords(s) && |
198 this->fTextureDomain == s.fTextureDomain; | 185 this->fTextureDomain == s.fTextureDomain; |
199 } | 186 } |
200 | 187 |
201 void GrTextureDomainEffect::getConstantColorComponents(GrColor* color, uint32_t*
validFlags) const { | 188 void GrTextureDomainEffect::getConstantColorComponents(GrColor* color, uint32_t*
validFlags) const { |
202 if (kDecal_WrapMode == fWrapMode) { | 189 if (kDecal_WrapMode == fWrapMode) { |
203 *validFlags = 0; | 190 *validFlags = 0; |
204 } else { | 191 } else { |
205 this->updateConstantColorComponentsForModulation(color, validFlags); | 192 this->updateConstantColorComponentsForModulation(color, validFlags); |
206 } | 193 } |
207 } | 194 } |
208 | 195 |
209 /////////////////////////////////////////////////////////////////////////////// | 196 /////////////////////////////////////////////////////////////////////////////// |
210 | 197 |
211 GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect); | 198 GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect); |
212 | 199 |
213 GrEffectRef* GrTextureDomainEffect::TestCreate(SkRandom* random, | 200 GrEffectRef* GrTextureDomainEffect::TestCreate(SkRandom* random, |
214 GrContext*, | 201 GrContext*, |
215 const GrDrawTargetCaps&, | 202 const GrDrawTargetCaps&, |
216 GrTexture* textures[]) { | 203 GrTexture* textures[]) { |
217 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : | 204 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
218 GrEffectUnitTest::kAlphaTextureIdx; | 205 GrEffectUnitTest::kAlphaTextureIdx; |
219 SkRect domain; | 206 SkRect domain; |
220 domain.fLeft = random->nextUScalar1(); | 207 domain.fLeft = random->nextUScalar1(); |
221 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1); | 208 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1); |
222 domain.fTop = random->nextUScalar1(); | 209 domain.fTop = random->nextUScalar1(); |
223 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1); | 210 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1); |
224 WrapMode wrapMode = random->nextBool() ? kClamp_WrapMode : kDecal_WrapMode; | 211 WrapMode wrapMode = random->nextBool() ? kClamp_WrapMode : kDecal_WrapMode; |
225 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random); | 212 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random); |
226 bool bilerp = random->nextBool(); | 213 bool bilerp = random->nextBool(); |
227 CoordsType coords = random->nextBool() ? kLocal_CoordsType : kPosition_Coord
sType; | 214 GrCoordSet coords = random->nextBool() ? kLocal_GrCoordSet : kPosition_GrCoo
rdSet; |
228 return GrTextureDomainEffect::Create(textures[texIdx], | 215 return GrTextureDomainEffect::Create(textures[texIdx], |
229 matrix, | 216 matrix, |
230 domain, | 217 domain, |
231 wrapMode, | 218 wrapMode, |
232 bilerp ? GrTextureParams::kBilerp_Filte
rMode : GrTextureParams::kNone_FilterMode, | 219 bilerp ? GrTextureParams::kBilerp_Filte
rMode : GrTextureParams::kNone_FilterMode, |
233 coords); | 220 coords); |
234 } | 221 } |
OLD | NEW |