blob: eca99d73a86b7f64d02028e9505592c2f905e17d [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 "SkFloatingPoint.h"
tomhudson@google.com2f68e762012-07-17 18:43:21 +000013
bsalomon@google.com22a800a2012-10-26 19:16:46 +000014class GrGLTextureDomainEffect : public GrGLEffect {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000015public:
bsalomon@google.comc7818882013-03-20 19:19:53 +000016 GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000017
bsalomon@google.com22a800a2012-10-26 19:16:46 +000018 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.comc7818882013-03-20 19:19:53 +000019 const GrDrawEffect&,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000020 EffectKey,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000021 const char* outputColor,
22 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000023 const TransformedCoordsArray&,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000024 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 GrGLfloat fPrevDomain[4];
tomhudson@google.com2f68e762012-07-17 18:43:21 +000033
bsalomon@google.com22a800a2012-10-26 19:16:46 +000034 typedef GrGLEffect INHERITED;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000035};
36
bsalomon@google.com396e61f2012-10-25 19:00:29 +000037GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& factory,
bsalomon@google.com77af6802013-10-02 13:04:56 +000038 const GrDrawEffect&)
39 : INHERITED(factory) {
bsalomon@google.com92b6a942012-11-02 19:50:26 +000040 fPrevDomain[0] = SK_FloatNaN;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000041}
42
bsalomon@google.com22a800a2012-10-26 19:16:46 +000043void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +000044 const GrDrawEffect& drawEffect,
bsalomon@google.com92b6a942012-11-02 19:50:26 +000045 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000046 const char* outputColor,
47 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000048 const TransformedCoordsArray& coords,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000049 const TextureSamplerArray& samplers) {
bsalomon@google.comc7818882013-03-20 19:19:53 +000050 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainEffect>();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000051
bsalomon@google.com77af6802013-10-02 13:04:56 +000052 SkString coords2D = builder->ensureFSCoords2D(coords, 0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000053 const char* domain;
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000054 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000055 kVec4f_GrSLType, "TexDom", &domain);
bsalomon@google.comc7818882013-03-20 19:19:53 +000056 if (GrTextureDomainEffect::kClamp_WrapMode == texDom.wrapMode()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000057
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000058 builder->fsCodeAppendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n",
bsalomon@google.com77af6802013-10-02 13:04:56 +000059 coords2D.c_str(), domain, domain);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000060
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000061 builder->fsCodeAppendf("\t%s = ", outputColor);
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000062 builder->fsAppendTextureLookupAndModulate(inputColor, samplers[0], "clampCoord");
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000063 builder->fsCodeAppend(";\n");
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000064 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000065 SkASSERT(GrTextureDomainEffect::kDecal_WrapMode == texDom.wrapMode());
robertphillips@google.com13f181f2013-03-02 12:02:08 +000066
67 if (kImagination_GrGLVendor == builder->ctxInfo().vendor()) {
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +000068 // 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
robertphillips@google.com13f181f2013-03-02 12:02:08 +000071 // 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.
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000074 builder->fsCodeAppend("\tvec4 outside = vec4(0.0, 0.0, 0.0, 0.0);\n");
75 builder->fsCodeAppend("\tvec4 inside = ");
bsalomon@google.com77af6802013-10-02 13:04:56 +000076 builder->fsAppendTextureLookupAndModulate(inputColor, samplers[0], coords2D.c_str());
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000077 builder->fsCodeAppend(";\n");
robertphillips@google.com13f181f2013-03-02 12:02:08 +000078
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000079 builder->fsCodeAppendf("\tfloat x = abs(2.0*(%s.x - %s.x)/(%s.z - %s.x) - 1.0);\n",
bsalomon@google.com77af6802013-10-02 13:04:56 +000080 coords2D.c_str(), domain, domain, domain);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000081 builder->fsCodeAppendf("\tfloat y = abs(2.0*(%s.y - %s.y)/(%s.w - %s.y) - 1.0);\n",
bsalomon@google.com77af6802013-10-02 13:04:56 +000082 coords2D.c_str(), domain, domain, domain);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000083 builder->fsCodeAppend("\tfloat blend = step(1.0, max(x, y));\n");
84 builder->fsCodeAppendf("\t%s = mix(inside, outside, blend);\n", outputColor);
robertphillips@google.com13f181f2013-03-02 12:02:08 +000085 } else {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000086 builder->fsCodeAppend("\tbvec4 outside;\n");
bsalomon@google.com77af6802013-10-02 13:04:56 +000087 builder->fsCodeAppendf("\toutside.xy = lessThan(%s, %s.xy);\n", coords2D.c_str(), domain);
88 builder->fsCodeAppendf("\toutside.zw = greaterThan(%s, %s.zw);\n", coords2D.c_str(), domain);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000089 builder->fsCodeAppendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ", outputColor);
bsalomon@google.com77af6802013-10-02 13:04:56 +000090 builder->fsAppendTextureLookupAndModulate(inputColor, samplers[0], coords2D.c_str());
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000091 builder->fsCodeAppend(";\n");
robertphillips@google.com13f181f2013-03-02 12:02:08 +000092 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000093 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +000094}
95
bsalomon@google.comc7818882013-03-20 19:19:53 +000096void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman,
97 const GrDrawEffect& drawEffect) {
98 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainEffect>();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000099 const SkRect& domain = texDom.domain();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000100
101 float values[4] = {
bsalomon@google.com81712882012-11-01 17:12:34 +0000102 SkScalarToFloat(domain.left()),
103 SkScalarToFloat(domain.top()),
104 SkScalarToFloat(domain.right()),
105 SkScalarToFloat(domain.bottom())
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000106 };
107 // vertical flip if necessary
bsalomon@google.comc7818882013-03-20 19:19:53 +0000108 if (kBottomLeft_GrSurfaceOrigin == texDom.texture(0)->origin()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000109 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 }
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000115 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) {
116 uman.set4fv(fNameUni, 0, 1, values);
robertphillips@google.com6b96d362013-04-15 21:51:52 +0000117 memcpy(fPrevDomain, values, 4 * sizeof(GrGLfloat));
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000118 }
skia.committer@gmail.com1aa90cf2012-11-06 13:18:25 +0000119}
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000120
bsalomon@google.comc7818882013-03-20 19:19:53 +0000121GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrDrawEffect& drawEffect,
122 const GrGLCaps&) {
bsalomon@google.com77af6802013-10-02 13:04:56 +0000123 return drawEffect.castEffect<GrTextureDomainEffect>().wrapMode();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000124}
125
126
127///////////////////////////////////////////////////////////////////////////////
128
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000129GrEffectRef* GrTextureDomainEffect::Create(GrTexture* texture,
130 const SkMatrix& matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000131 const SkRect& domain,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000132 WrapMode wrapMode,
humper@google.comb86add12013-07-25 18:49:07 +0000133 GrTextureParams::FilterMode filterMode,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000134 GrCoordSet coordSet) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000135 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
136 if (kClamp_WrapMode == wrapMode && domain.contains(kFullRect)) {
humper@google.comb86add12013-07-25 18:49:07 +0000137 return GrSimpleTextureEffect::Create(texture, matrix, filterMode);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000138 } else {
139 SkRect clippedDomain;
140 // We don't currently handle domains that are empty or don't intersect the texture.
bsalomon@google.comfe4e4912012-11-08 20:32:13 +0000141 // 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.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000143 SkASSERT(domain.fLeft <= domain.fRight);
144 SkASSERT(domain.fTop <= domain.fBottom);
bsalomon@google.comfe4e4912012-11-08 20:32:13 +0000145 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);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000149 SkASSERT(clippedDomain.fLeft <= clippedDomain.fRight);
150 SkASSERT(clippedDomain.fTop <= clippedDomain.fBottom);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000151
bsalomon@google.com6340a412013-01-22 19:55:59 +0000152 AutoEffectUnref effect(SkNEW_ARGS(GrTextureDomainEffect, (texture,
153 matrix,
154 clippedDomain,
155 wrapMode,
humper@google.comb86add12013-07-25 18:49:07 +0000156 filterMode,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000157 coordSet)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000158 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000159
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000160 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000161}
162
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000163GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000164 const SkMatrix& matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000165 const SkRect& domain,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000166 WrapMode wrapMode,
humper@google.comb86add12013-07-25 18:49:07 +0000167 GrTextureParams::FilterMode filterMode,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000168 GrCoordSet coordSet)
169 : GrSingleTextureEffect(texture, matrix, filterMode, coordSet)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000170 , fWrapMode(wrapMode)
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000171 , fTextureDomain(domain) {
172}
173
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000174GrTextureDomainEffect::~GrTextureDomainEffect() {
175
176}
177
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000178const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const {
179 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000180}
181
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000182bool GrTextureDomainEffect::onIsEqual(const GrEffect& sBase) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000183 const GrTextureDomainEffect& s = CastEffect<GrTextureDomainEffect>(sBase);
bsalomon@google.com77af6802013-10-02 13:04:56 +0000184 return this->hasSameTextureParamsMatrixAndSourceCoords(s) &&
bsalomon@google.comc7818882013-03-20 19:19:53 +0000185 this->fTextureDomain == s.fTextureDomain;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000186}
187
188void GrTextureDomainEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
189 if (kDecal_WrapMode == fWrapMode) {
190 *validFlags = 0;
191 } else {
192 this->updateConstantColorComponentsForModulation(color, validFlags);
193 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000194}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000195
196///////////////////////////////////////////////////////////////////////////////
197
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000198GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000199
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000200GrEffectRef* GrTextureDomainEffect::TestCreate(SkRandom* random,
sugoi@google.come0e385c2013-03-11 18:50:03 +0000201 GrContext*,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000202 const GrDrawTargetCaps&,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000203 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000204 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
205 GrEffectUnitTest::kAlphaTextureIdx;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000206 SkRect domain;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000207 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);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000211 WrapMode wrapMode = random->nextBool() ? kClamp_WrapMode : kDecal_WrapMode;
212 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000213 bool bilerp = random->nextBool();
bsalomon@google.com77af6802013-10-02 13:04:56 +0000214 GrCoordSet coords = random->nextBool() ? kLocal_GrCoordSet : kPosition_GrCoordSet;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000215 return GrTextureDomainEffect::Create(textures[texIdx],
216 matrix,
217 domain,
218 wrapMode,
humper@google.comb86add12013-07-25 18:49:07 +0000219 bilerp ? GrTextureParams::kBilerp_FilterMode : GrTextureParams::kNone_FilterMode,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000220 coords);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000221}