blob: 5b1c72f77a87a093aad9beba71deb60ad4a883f4 [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.com7b7cdd12012-11-07 16:17:24 +000062 builder->fFSCode.appendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n",
63 coords, domain, domain);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000064
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000065 builder->fFSCode.appendf("\t%s = ", outputColor);
66 builder->appendTextureLookupAndModulate(&builder->fFSCode,
67 inputColor,
68 samplers[0],
69 "clampCoord");
70 builder->fFSCode.append(";\n");
71 } else {
72 GrAssert(GrTextureDomainEffect::kDecal_WrapMode == effect.wrapMode());
robertphillips@google.com13f181f2013-03-02 12:02:08 +000073
74 if (kImagination_GrGLVendor == builder->ctxInfo().vendor()) {
75 // 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
78 // 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.
81 builder->fFSCode.appendf("\tvec4 outside = vec4(0.0, 0.0, 0.0, 0.0);\n");
82 builder->fFSCode.appendf("\tvec4 inside = ");
83 builder->appendTextureLookupAndModulate(&builder->fFSCode, inputColor, samplers[0], coords);
84 builder->fFSCode.appendf(";\n");
85
86 builder->fFSCode.appendf("\tfloat x = abs(2.0*(%s.x - %s.x)/(%s.z - %s.x) - 1.0);\n",
87 coords, domain, domain, domain);
88 builder->fFSCode.appendf("\tfloat y = abs(2.0*(%s.y - %s.y)/(%s.w - %s.y) - 1.0);\n",
89 coords, domain, domain, domain);
90 builder->fFSCode.appendf("\tfloat blend = step(1.0, max(x, y));\n");
91 builder->fFSCode.appendf("\t%s = mix(inside, outside, blend);\n", outputColor);
92 } else {
93 builder->fFSCode.append("\tbvec4 outside;\n");
94 builder->fFSCode.appendf("\toutside.xy = lessThan(%s, %s.xy);\n", coords, domain);
95 builder->fFSCode.appendf("\toutside.zw = greaterThan(%s, %s.zw);\n", coords, domain);
96 builder->fFSCode.appendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ", outputColor);
97 builder->appendTextureLookupAndModulate(&builder->fFSCode, inputColor, samplers[0], coords);
98 builder->fFSCode.append(";\n");
99 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000100 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000101}
102
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000103void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000104 const GrTextureDomainEffect& effect = GetEffectFromStage<GrTextureDomainEffect>(stage);
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000105 const GrRect& domain = effect.domain();
106
107 float values[4] = {
bsalomon@google.com81712882012-11-01 17:12:34 +0000108 SkScalarToFloat(domain.left()),
109 SkScalarToFloat(domain.top()),
110 SkScalarToFloat(domain.right()),
111 SkScalarToFloat(domain.bottom())
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000112 };
113 // vertical flip if necessary
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000114 if (kBottomLeft_GrSurfaceOrigin == effect.texture(0)->origin()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000115 values[1] = 1.0f - values[1];
116 values[3] = 1.0f - values[3];
117 // The top and bottom were just flipped, so correct the ordering
118 // of elements so that values = (l, t, r, b).
119 SkTSwap(values[1], values[3]);
120 }
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000121 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) {
122 uman.set4fv(fNameUni, 0, 1, values);
123 }
124 fEffectMatrix.setData(uman,
125 effect.getMatrix(),
126 stage.getCoordChangeMatrix(),
127 effect.texture(0));
skia.committer@gmail.com1aa90cf2012-11-06 13:18:25 +0000128}
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000129
130GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrEffectStage& stage, const GrGLCaps&) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000131 const GrTextureDomainEffect& effect = GetEffectFromStage<GrTextureDomainEffect>(stage);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000132 EffectKey key = effect.wrapMode();
133 key <<= GrGLEffectMatrix::kKeyBits;
134 EffectKey matrixKey = GrGLEffectMatrix::GenKey(effect.getMatrix(),
135 stage.getCoordChangeMatrix(),
136 effect.texture(0));
137 return key | matrixKey;
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000138}
139
140
141///////////////////////////////////////////////////////////////////////////////
142
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000143GrEffectRef* GrTextureDomainEffect::Create(GrTexture* texture,
144 const SkMatrix& matrix,
145 const GrRect& domain,
146 WrapMode wrapMode,
147 bool bilerp) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000148 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
149 if (kClamp_WrapMode == wrapMode && domain.contains(kFullRect)) {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000150 return GrSimpleTextureEffect::Create(texture, matrix, bilerp);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000151 } else {
152 SkRect clippedDomain;
153 // We don't currently handle domains that are empty or don't intersect the texture.
bsalomon@google.comfe4e4912012-11-08 20:32:13 +0000154 // 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.
156 GrAssert(domain.fLeft <= domain.fRight);
157 GrAssert(domain.fTop <= domain.fBottom);
158 clippedDomain.fLeft = SkMaxScalar(domain.fLeft, kFullRect.fLeft);
159 clippedDomain.fRight = SkMinScalar(domain.fRight, kFullRect.fRight);
160 clippedDomain.fTop = SkMaxScalar(domain.fTop, kFullRect.fTop);
161 clippedDomain.fBottom = SkMinScalar(domain.fBottom, kFullRect.fBottom);
162 GrAssert(clippedDomain.fLeft <= clippedDomain.fRight);
163 GrAssert(clippedDomain.fTop <= clippedDomain.fBottom);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000164
bsalomon@google.com6340a412013-01-22 19:55:59 +0000165 AutoEffectUnref effect(SkNEW_ARGS(GrTextureDomainEffect, (texture,
166 matrix,
167 clippedDomain,
168 wrapMode,
169 bilerp)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000170 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000171
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000172 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000173}
174
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000175GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000176 const SkMatrix& matrix,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000177 const GrRect& domain,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000178 WrapMode wrapMode,
179 bool bilerp)
180 : GrSingleTextureEffect(texture, matrix, bilerp)
181 , fWrapMode(wrapMode)
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000182 , fTextureDomain(domain) {
183}
184
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000185GrTextureDomainEffect::~GrTextureDomainEffect() {
186
187}
188
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000189const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const {
190 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000191}
192
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000193bool GrTextureDomainEffect::onIsEqual(const GrEffect& sBase) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000194 const GrTextureDomainEffect& s = CastEffect<GrTextureDomainEffect>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000195 return this->hasSameTextureParamsAndMatrix(s) && this->fTextureDomain == s.fTextureDomain;
196}
197
198void GrTextureDomainEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
199 if (kDecal_WrapMode == fWrapMode) {
200 *validFlags = 0;
201 } else {
202 this->updateConstantColorComponentsForModulation(color, validFlags);
203 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000204}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000205
206///////////////////////////////////////////////////////////////////////////////
207
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000208GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000209
bsalomon@google.com73a96942013-02-13 16:31:19 +0000210GrEffectRef* GrTextureDomainEffect::TestCreate(SkMWCRandom* random,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000211 GrContext* context,
212 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000213 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
214 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000215 GrRect domain;
216 domain.fLeft = random->nextUScalar1();
217 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1);
218 domain.fTop = random->nextUScalar1();
219 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000220 WrapMode wrapMode = random->nextBool() ? kClamp_WrapMode : kDecal_WrapMode;
221 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
222 return GrTextureDomainEffect::Create(textures[texIdx], matrix, domain, wrapMode);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000223}