blob: b1fd3ed6d5445dff484960492f4f68fd53228e7f [file] [log] [blame]
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001/*
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"
bsalomon@google.com68b58c92013-01-17 16:50:08 +00009#include "GrSimpleTextureEffect.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000010#include "GrTBackendEffectFactory.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +000011#include "gl/GrGLEffect.h"
bsalomon@google.com92b6a942012-11-02 19:50:26 +000012#include "gl/GrGLEffectMatrix.h"
13#include "SkFloatingPoint.h"
tomhudson@google.com2f68e762012-07-17 18:43:21 +000014
bsalomon@google.com22a800a2012-10-26 19:16:46 +000015class GrGLTextureDomainEffect : public GrGLEffect {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000016public:
bsalomon@google.comc7818882013-03-20 19:19:53 +000017 GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000018
bsalomon@google.com22a800a2012-10-26 19:16:46 +000019 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.comc7818882013-03-20 19:19:53 +000020 const GrDrawEffect&,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000021 EffectKey,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000022 const char* outputColor,
23 const char* inputColor,
24 const TextureSamplerArray&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000025
bsalomon@google.comc7818882013-03-20 19:19:53 +000026 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000027
bsalomon@google.comc7818882013-03-20 19:19:53 +000028 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000029
30private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000031 GrGLUniformManager::UniformHandle fNameUni;
bsalomon@google.com92b6a942012-11-02 19:50:26 +000032 GrGLEffectMatrix fEffectMatrix;
33 GrGLfloat fPrevDomain[4];
tomhudson@google.com2f68e762012-07-17 18:43:21 +000034
bsalomon@google.com22a800a2012-10-26 19:16:46 +000035 typedef GrGLEffect INHERITED;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000036};
37
bsalomon@google.com396e61f2012-10-25 19:00:29 +000038GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& factory,
bsalomon@google.comc7818882013-03-20 19:19:53 +000039 const GrDrawEffect& drawEffect)
bsalomon@google.com374e7592012-10-23 17:30:45 +000040 : INHERITED(factory)
bsalomon@google.comc7818882013-03-20 19:19:53 +000041 , fNameUni(GrGLUniformManager::kInvalidUniformHandle)
42 , fEffectMatrix(drawEffect.castEffect<GrTextureDomainEffect>().coordsType()) {
bsalomon@google.com92b6a942012-11-02 19:50:26 +000043 fPrevDomain[0] = SK_FloatNaN;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000044}
45
bsalomon@google.com22a800a2012-10-26 19:16:46 +000046void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +000047 const GrDrawEffect& drawEffect,
bsalomon@google.com92b6a942012-11-02 19:50:26 +000048 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000049 const char* outputColor,
50 const char* inputColor,
51 const TextureSamplerArray& samplers) {
bsalomon@google.comc7818882013-03-20 19:19:53 +000052 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainEffect>();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000053
bsalomon@google.com92b6a942012-11-02 19:50:26 +000054 const char* coords;
bsalomon@google.comc7818882013-03-20 19:19:53 +000055 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000056 const char* domain;
tomhudson@google.com57143a22012-07-17 19:07:35 +000057 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000058 kVec4f_GrSLType, "TexDom", &domain);
bsalomon@google.comc7818882013-03-20 19:19:53 +000059 if (GrTextureDomainEffect::kClamp_WrapMode == texDom.wrapMode()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000060
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000061 builder->fsCodeAppendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n",
62 coords, domain, domain);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000063
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000064 builder->fsCodeAppendf("\t%s = ", outputColor);
65 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000066 inputColor,
67 samplers[0],
68 "clampCoord");
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000069 builder->fsCodeAppend(";\n");
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000070 } else {
bsalomon@google.comc7818882013-03-20 19:19:53 +000071 GrAssert(GrTextureDomainEffect::kDecal_WrapMode == texDom.wrapMode());
robertphillips@google.com13f181f2013-03-02 12:02:08 +000072
73 if (kImagination_GrGLVendor == builder->ctxInfo().vendor()) {
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +000074 // On the NexusS and GalaxyNexus, the other path (with the 'any'
75 // call) causes the compilation error "Calls to any function that
76 // may require a gradient calculation inside a conditional block
robertphillips@google.com13f181f2013-03-02 12:02:08 +000077 // may return undefined results". This appears to be an issue with
78 // the 'any' call since even the simple "result=black; if (any())
79 // result=white;" code fails to compile.
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000080 builder->fsCodeAppend("\tvec4 outside = vec4(0.0, 0.0, 0.0, 0.0);\n");
81 builder->fsCodeAppend("\tvec4 inside = ");
82 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_ShaderType,
83 inputColor,
84 samplers[0],
85 coords);
86 builder->fsCodeAppend(";\n");
robertphillips@google.com13f181f2013-03-02 12:02:08 +000087
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000088 builder->fsCodeAppendf("\tfloat x = abs(2.0*(%s.x - %s.x)/(%s.z - %s.x) - 1.0);\n",
89 coords, domain, domain, domain);
90 builder->fsCodeAppendf("\tfloat y = abs(2.0*(%s.y - %s.y)/(%s.w - %s.y) - 1.0);\n",
91 coords, domain, domain, domain);
92 builder->fsCodeAppend("\tfloat blend = step(1.0, max(x, y));\n");
93 builder->fsCodeAppendf("\t%s = mix(inside, outside, blend);\n", outputColor);
robertphillips@google.com13f181f2013-03-02 12:02:08 +000094 } else {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000095 builder->fsCodeAppend("\tbvec4 outside;\n");
96 builder->fsCodeAppendf("\toutside.xy = lessThan(%s, %s.xy);\n", coords, domain);
97 builder->fsCodeAppendf("\toutside.zw = greaterThan(%s, %s.zw);\n", coords, domain);
98 builder->fsCodeAppendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ", outputColor);
99 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_ShaderType,
100 inputColor,
101 samplers[0],
102 coords);
103 builder->fsCodeAppend(";\n");
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000104 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000105 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000106}
107
bsalomon@google.comc7818882013-03-20 19:19:53 +0000108void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman,
109 const GrDrawEffect& drawEffect) {
110 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainEffect>();
111 const GrRect& domain = texDom.domain();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000112
113 float values[4] = {
bsalomon@google.com81712882012-11-01 17:12:34 +0000114 SkScalarToFloat(domain.left()),
115 SkScalarToFloat(domain.top()),
116 SkScalarToFloat(domain.right()),
117 SkScalarToFloat(domain.bottom())
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000118 };
119 // vertical flip if necessary
bsalomon@google.comc7818882013-03-20 19:19:53 +0000120 if (kBottomLeft_GrSurfaceOrigin == texDom.texture(0)->origin()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000121 values[1] = 1.0f - values[1];
122 values[3] = 1.0f - values[3];
123 // The top and bottom were just flipped, so correct the ordering
124 // of elements so that values = (l, t, r, b).
125 SkTSwap(values[1], values[3]);
126 }
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000127 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) {
128 uman.set4fv(fNameUni, 0, 1, values);
129 }
130 fEffectMatrix.setData(uman,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000131 texDom.getMatrix(),
132 drawEffect,
133 texDom.texture(0));
skia.committer@gmail.com1aa90cf2012-11-06 13:18:25 +0000134}
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000135
bsalomon@google.comc7818882013-03-20 19:19:53 +0000136GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrDrawEffect& drawEffect,
137 const GrGLCaps&) {
138 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainEffect>();
139 EffectKey key = texDom.wrapMode();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000140 key <<= GrGLEffectMatrix::kKeyBits;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000141 EffectKey matrixKey = GrGLEffectMatrix::GenKey(texDom.getMatrix(),
142 drawEffect,
143 texDom.coordsType(),
144 texDom.texture(0));
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000145 return key | matrixKey;
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000146}
147
148
149///////////////////////////////////////////////////////////////////////////////
150
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000151GrEffectRef* GrTextureDomainEffect::Create(GrTexture* texture,
152 const SkMatrix& matrix,
153 const GrRect& domain,
154 WrapMode wrapMode,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000155 bool bilerp,
156 CoordsType coordsType) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000157 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
158 if (kClamp_WrapMode == wrapMode && domain.contains(kFullRect)) {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000159 return GrSimpleTextureEffect::Create(texture, matrix, bilerp);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000160 } else {
161 SkRect clippedDomain;
162 // We don't currently handle domains that are empty or don't intersect the texture.
bsalomon@google.comfe4e4912012-11-08 20:32:13 +0000163 // It is OK if the domain rect is a line or point, but it should not be inverted. We do not
164 // handle rects that do not intersect the [0..1]x[0..1] rect.
165 GrAssert(domain.fLeft <= domain.fRight);
166 GrAssert(domain.fTop <= domain.fBottom);
167 clippedDomain.fLeft = SkMaxScalar(domain.fLeft, kFullRect.fLeft);
168 clippedDomain.fRight = SkMinScalar(domain.fRight, kFullRect.fRight);
169 clippedDomain.fTop = SkMaxScalar(domain.fTop, kFullRect.fTop);
170 clippedDomain.fBottom = SkMinScalar(domain.fBottom, kFullRect.fBottom);
171 GrAssert(clippedDomain.fLeft <= clippedDomain.fRight);
172 GrAssert(clippedDomain.fTop <= clippedDomain.fBottom);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000173
bsalomon@google.com6340a412013-01-22 19:55:59 +0000174 AutoEffectUnref effect(SkNEW_ARGS(GrTextureDomainEffect, (texture,
175 matrix,
176 clippedDomain,
177 wrapMode,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000178 bilerp,
179 coordsType)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000180 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000181
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000182 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000183}
184
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000185GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000186 const SkMatrix& matrix,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000187 const GrRect& domain,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000188 WrapMode wrapMode,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000189 bool bilerp,
190 CoordsType coordsType)
191 : GrSingleTextureEffect(texture, matrix, bilerp, coordsType)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000192 , fWrapMode(wrapMode)
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000193 , fTextureDomain(domain) {
194}
195
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000196GrTextureDomainEffect::~GrTextureDomainEffect() {
197
198}
199
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000200const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const {
201 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000202}
203
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000204bool GrTextureDomainEffect::onIsEqual(const GrEffect& sBase) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000205 const GrTextureDomainEffect& s = CastEffect<GrTextureDomainEffect>(sBase);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000206 return this->hasSameTextureParamsMatrixAndCoordsType(s) &&
207 this->fTextureDomain == s.fTextureDomain;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000208}
209
210void GrTextureDomainEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
211 if (kDecal_WrapMode == fWrapMode) {
212 *validFlags = 0;
213 } else {
214 this->updateConstantColorComponentsForModulation(color, validFlags);
215 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000216}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000217
218///////////////////////////////////////////////////////////////////////////////
219
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000220GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000221
bsalomon@google.com73a96942013-02-13 16:31:19 +0000222GrEffectRef* GrTextureDomainEffect::TestCreate(SkMWCRandom* random,
sugoi@google.come0e385c2013-03-11 18:50:03 +0000223 GrContext*,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000224 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000225 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
226 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000227 GrRect domain;
228 domain.fLeft = random->nextUScalar1();
229 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1);
230 domain.fTop = random->nextUScalar1();
231 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000232 WrapMode wrapMode = random->nextBool() ? kClamp_WrapMode : kDecal_WrapMode;
233 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000234 bool bilerp = random->nextBool();
235 CoordsType coords = random->nextBool() ? kLocal_CoordsType : kPosition_CoordsType;
236 return GrTextureDomainEffect::Create(textures[texIdx],
237 matrix,
238 domain,
239 wrapMode,
240 bilerp,
241 coords);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000242}