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