blob: e54af25ca05ce23718ccdf10116436121c65d411 [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.com6340a412013-01-22 19:55:59 +000017 GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrEffectRef&);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000018
bsalomon@google.com22a800a2012-10-26 19:16:46 +000019 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000020 const GrEffectStage&,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000021 EffectKey,
22 const char* vertexCoords,
23 const char* outputColor,
24 const char* inputColor,
25 const TextureSamplerArray&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000026
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000027 virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000028
bsalomon@google.com92b6a942012-11-02 19:50:26 +000029 static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000030
31private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000032 GrGLUniformManager::UniformHandle fNameUni;
bsalomon@google.com92b6a942012-11-02 19:50:26 +000033 GrGLEffectMatrix fEffectMatrix;
34 GrGLfloat fPrevDomain[4];
tomhudson@google.com2f68e762012-07-17 18:43:21 +000035
bsalomon@google.com22a800a2012-10-26 19:16:46 +000036 typedef GrGLEffect INHERITED;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000037};
38
bsalomon@google.com396e61f2012-10-25 19:00:29 +000039GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& factory,
bsalomon@google.com6340a412013-01-22 19:55:59 +000040 const GrEffectRef&)
bsalomon@google.com374e7592012-10-23 17:30:45 +000041 : INHERITED(factory)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000042 , fNameUni(GrGLUniformManager::kInvalidUniformHandle) {
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.com7b7cdd12012-11-07 16:17:24 +000047 const GrEffectStage& stage,
bsalomon@google.com92b6a942012-11-02 19:50:26 +000048 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000049 const char* vertexCoords,
50 const char* outputColor,
51 const char* inputColor,
52 const TextureSamplerArray& samplers) {
bsalomon@google.com6340a412013-01-22 19:55:59 +000053 const GrTextureDomainEffect& effect = GetEffectFromStage<GrTextureDomainEffect>(stage);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000054
bsalomon@google.com92b6a942012-11-02 19:50:26 +000055 const char* coords;
56 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000057 const char* domain;
tomhudson@google.com57143a22012-07-17 19:07:35 +000058 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000059 kVec4f_GrSLType, "TexDom", &domain);
60 if (GrTextureDomainEffect::kClamp_WrapMode == effect.wrapMode()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000061
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000062 builder->fsCodeAppendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n",
63 coords, domain, domain);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000064
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000065 builder->fsCodeAppendf("\t%s = ", outputColor);
66 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000067 inputColor,
68 samplers[0],
69 "clampCoord");
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000070 builder->fsCodeAppend(";\n");
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000071 } else {
72 GrAssert(GrTextureDomainEffect::kDecal_WrapMode == effect.wrapMode());
robertphillips@google.com13f181f2013-03-02 12:02:08 +000073
74 if (kImagination_GrGLVendor == builder->ctxInfo().vendor()) {
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +000075 // On the NexusS and GalaxyNexus, the other path (with the 'any'
76 // call) causes the compilation error "Calls to any function that
77 // may require a gradient calculation inside a conditional block
robertphillips@google.com13f181f2013-03-02 12:02:08 +000078 // may return undefined results". This appears to be an issue with
79 // the 'any' call since even the simple "result=black; if (any())
80 // result=white;" code fails to compile.
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000081 builder->fsCodeAppend("\tvec4 outside = vec4(0.0, 0.0, 0.0, 0.0);\n");
82 builder->fsCodeAppend("\tvec4 inside = ");
83 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_ShaderType,
84 inputColor,
85 samplers[0],
86 coords);
87 builder->fsCodeAppend(";\n");
robertphillips@google.com13f181f2013-03-02 12:02:08 +000088
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000089 builder->fsCodeAppendf("\tfloat x = abs(2.0*(%s.x - %s.x)/(%s.z - %s.x) - 1.0);\n",
90 coords, domain, domain, domain);
91 builder->fsCodeAppendf("\tfloat y = abs(2.0*(%s.y - %s.y)/(%s.w - %s.y) - 1.0);\n",
92 coords, domain, domain, domain);
93 builder->fsCodeAppend("\tfloat blend = step(1.0, max(x, y));\n");
94 builder->fsCodeAppendf("\t%s = mix(inside, outside, blend);\n", outputColor);
robertphillips@google.com13f181f2013-03-02 12:02:08 +000095 } else {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000096 builder->fsCodeAppend("\tbvec4 outside;\n");
97 builder->fsCodeAppendf("\toutside.xy = lessThan(%s, %s.xy);\n", coords, domain);
98 builder->fsCodeAppendf("\toutside.zw = greaterThan(%s, %s.zw);\n", coords, domain);
99 builder->fsCodeAppendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ", outputColor);
100 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_ShaderType,
101 inputColor,
102 samplers[0],
103 coords);
104 builder->fsCodeAppend(";\n");
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000105 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000106 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000107}
108
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000109void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000110 const GrTextureDomainEffect& effect = GetEffectFromStage<GrTextureDomainEffect>(stage);
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000111 const GrRect& domain = effect.domain();
112
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
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000120 if (kBottomLeft_GrSurfaceOrigin == effect.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,
131 effect.getMatrix(),
132 stage.getCoordChangeMatrix(),
133 effect.texture(0));
skia.committer@gmail.com1aa90cf2012-11-06 13:18:25 +0000134}
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000135
136GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrEffectStage& stage, const GrGLCaps&) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000137 const GrTextureDomainEffect& effect = GetEffectFromStage<GrTextureDomainEffect>(stage);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000138 EffectKey key = effect.wrapMode();
139 key <<= GrGLEffectMatrix::kKeyBits;
140 EffectKey matrixKey = GrGLEffectMatrix::GenKey(effect.getMatrix(),
141 stage.getCoordChangeMatrix(),
142 effect.texture(0));
143 return key | matrixKey;
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000144}
145
146
147///////////////////////////////////////////////////////////////////////////////
148
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000149GrEffectRef* GrTextureDomainEffect::Create(GrTexture* texture,
150 const SkMatrix& matrix,
151 const GrRect& domain,
152 WrapMode wrapMode,
153 bool bilerp) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000154 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
155 if (kClamp_WrapMode == wrapMode && domain.contains(kFullRect)) {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000156 return GrSimpleTextureEffect::Create(texture, matrix, bilerp);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000157 } else {
158 SkRect clippedDomain;
159 // We don't currently handle domains that are empty or don't intersect the texture.
bsalomon@google.comfe4e4912012-11-08 20:32:13 +0000160 // It is OK if the domain rect is a line or point, but it should not be inverted. We do not
161 // handle rects that do not intersect the [0..1]x[0..1] rect.
162 GrAssert(domain.fLeft <= domain.fRight);
163 GrAssert(domain.fTop <= domain.fBottom);
164 clippedDomain.fLeft = SkMaxScalar(domain.fLeft, kFullRect.fLeft);
165 clippedDomain.fRight = SkMinScalar(domain.fRight, kFullRect.fRight);
166 clippedDomain.fTop = SkMaxScalar(domain.fTop, kFullRect.fTop);
167 clippedDomain.fBottom = SkMinScalar(domain.fBottom, kFullRect.fBottom);
168 GrAssert(clippedDomain.fLeft <= clippedDomain.fRight);
169 GrAssert(clippedDomain.fTop <= clippedDomain.fBottom);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000170
bsalomon@google.com6340a412013-01-22 19:55:59 +0000171 AutoEffectUnref effect(SkNEW_ARGS(GrTextureDomainEffect, (texture,
172 matrix,
173 clippedDomain,
174 wrapMode,
175 bilerp)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000176 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000177
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000178 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000179}
180
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000181GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000182 const SkMatrix& matrix,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000183 const GrRect& domain,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000184 WrapMode wrapMode,
185 bool bilerp)
186 : GrSingleTextureEffect(texture, matrix, bilerp)
187 , fWrapMode(wrapMode)
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000188 , fTextureDomain(domain) {
189}
190
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000191GrTextureDomainEffect::~GrTextureDomainEffect() {
192
193}
194
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000195const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const {
196 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000197}
198
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000199bool GrTextureDomainEffect::onIsEqual(const GrEffect& sBase) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000200 const GrTextureDomainEffect& s = CastEffect<GrTextureDomainEffect>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000201 return this->hasSameTextureParamsAndMatrix(s) && this->fTextureDomain == s.fTextureDomain;
202}
203
204void GrTextureDomainEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
205 if (kDecal_WrapMode == fWrapMode) {
206 *validFlags = 0;
207 } else {
208 this->updateConstantColorComponentsForModulation(color, validFlags);
209 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000210}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000211
212///////////////////////////////////////////////////////////////////////////////
213
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000214GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000215
bsalomon@google.com73a96942013-02-13 16:31:19 +0000216GrEffectRef* GrTextureDomainEffect::TestCreate(SkMWCRandom* random,
sugoi@google.come0e385c2013-03-11 18:50:03 +0000217 GrContext*,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000218 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000219 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
220 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000221 GrRect domain;
222 domain.fLeft = random->nextUScalar1();
223 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1);
224 domain.fTop = random->nextUScalar1();
225 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000226 WrapMode wrapMode = random->nextBool() ? kClamp_WrapMode : kDecal_WrapMode;
227 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
228 return GrTextureDomainEffect::Create(textures[texIdx], matrix, domain, wrapMode);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000229}