blob: c4a59d8daec44f95943292227f4c80614597f967 [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.com396e61f2012-10-25 19:00:29 +000017 GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrEffect&);
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.com021fc732012-10-25 12:47:42 +000040 const GrEffect&)
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.com7b7cdd12012-11-07 16:17:24 +000053 const GrTextureDomainEffect& effect =
54 static_cast<const GrTextureDomainEffect&>(*stage.getEffect());
55
bsalomon@google.com92b6a942012-11-02 19:50:26 +000056 const char* coords;
57 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000058 const char* domain;
tomhudson@google.com57143a22012-07-17 19:07:35 +000059 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000060 kVec4f_GrSLType, "TexDom", &domain);
61 if (GrTextureDomainEffect::kClamp_WrapMode == effect.wrapMode()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000062
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000063 builder->fFSCode.appendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n",
64 coords, domain, domain);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000065
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000066 builder->fFSCode.appendf("\t%s = ", outputColor);
67 builder->appendTextureLookupAndModulate(&builder->fFSCode,
68 inputColor,
69 samplers[0],
70 "clampCoord");
71 builder->fFSCode.append(";\n");
72 } else {
73 GrAssert(GrTextureDomainEffect::kDecal_WrapMode == effect.wrapMode());
74 builder->fFSCode.append("\tbvec4 outside;\n");
75 builder->fFSCode.appendf("\toutside.xy = lessThan(%s, %s.xy);\n", coords, domain);
76 builder->fFSCode.appendf("\toutside.zw = greaterThan(%s, %s.zw);\n", coords, domain);
77 builder->fFSCode.appendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ", outputColor);
78 builder->appendTextureLookupAndModulate(&builder->fFSCode, inputColor, samplers[0], coords);
79 builder->fFSCode.append(";\n");
80 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +000081}
82
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000083void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
84 const GrTextureDomainEffect& effect =
85 static_cast<const GrTextureDomainEffect&>(*stage.getEffect());
tomhudson@google.com2f68e762012-07-17 18:43:21 +000086 const GrRect& domain = effect.domain();
87
88 float values[4] = {
bsalomon@google.com81712882012-11-01 17:12:34 +000089 SkScalarToFloat(domain.left()),
90 SkScalarToFloat(domain.top()),
91 SkScalarToFloat(domain.right()),
92 SkScalarToFloat(domain.bottom())
tomhudson@google.com2f68e762012-07-17 18:43:21 +000093 };
94 // vertical flip if necessary
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000095 if (GrSurface::kBottomLeft_Origin == effect.texture(0)->origin()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000096 values[1] = 1.0f - values[1];
97 values[3] = 1.0f - values[3];
98 // The top and bottom were just flipped, so correct the ordering
99 // of elements so that values = (l, t, r, b).
100 SkTSwap(values[1], values[3]);
101 }
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000102 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) {
103 uman.set4fv(fNameUni, 0, 1, values);
104 }
105 fEffectMatrix.setData(uman,
106 effect.getMatrix(),
107 stage.getCoordChangeMatrix(),
108 effect.texture(0));
skia.committer@gmail.com1aa90cf2012-11-06 13:18:25 +0000109}
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000110
111GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrEffectStage& stage, const GrGLCaps&) {
112 const GrTextureDomainEffect& effect =
113 static_cast<const GrTextureDomainEffect&>(*stage.getEffect());
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000114 EffectKey key = effect.wrapMode();
115 key <<= GrGLEffectMatrix::kKeyBits;
116 EffectKey matrixKey = GrGLEffectMatrix::GenKey(effect.getMatrix(),
117 stage.getCoordChangeMatrix(),
118 effect.texture(0));
119 return key | matrixKey;
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000120}
121
122
123///////////////////////////////////////////////////////////////////////////////
124
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000125GrEffectRef* GrTextureDomainEffect::Create(GrTexture* texture,
126 const SkMatrix& matrix,
127 const GrRect& domain,
128 WrapMode wrapMode,
129 bool bilerp) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000130 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
131 if (kClamp_WrapMode == wrapMode && domain.contains(kFullRect)) {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000132 return GrSimpleTextureEffect::Create(texture, matrix, bilerp);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000133 } else {
134 SkRect clippedDomain;
135 // We don't currently handle domains that are empty or don't intersect the texture.
bsalomon@google.comfe4e4912012-11-08 20:32:13 +0000136 // It is OK if the domain rect is a line or point, but it should not be inverted. We do not
137 // handle rects that do not intersect the [0..1]x[0..1] rect.
138 GrAssert(domain.fLeft <= domain.fRight);
139 GrAssert(domain.fTop <= domain.fBottom);
140 clippedDomain.fLeft = SkMaxScalar(domain.fLeft, kFullRect.fLeft);
141 clippedDomain.fRight = SkMinScalar(domain.fRight, kFullRect.fRight);
142 clippedDomain.fTop = SkMaxScalar(domain.fTop, kFullRect.fTop);
143 clippedDomain.fBottom = SkMinScalar(domain.fBottom, kFullRect.fBottom);
144 GrAssert(clippedDomain.fLeft <= clippedDomain.fRight);
145 GrAssert(clippedDomain.fTop <= clippedDomain.fBottom);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000146
147 SkAutoTUnref<GrEffect> effect(SkNEW_ARGS(GrTextureDomainEffect, (texture,
148 matrix,
149 clippedDomain,
150 wrapMode,
151 bilerp)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000152 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000153
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000154 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000155}
156
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000157GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000158 const SkMatrix& matrix,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000159 const GrRect& domain,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000160 WrapMode wrapMode,
161 bool bilerp)
162 : GrSingleTextureEffect(texture, matrix, bilerp)
163 , fWrapMode(wrapMode)
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000164 , fTextureDomain(domain) {
165}
166
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000167GrTextureDomainEffect::~GrTextureDomainEffect() {
168
169}
170
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000171const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const {
172 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000173}
174
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000175bool GrTextureDomainEffect::onIsEqual(const GrEffect& sBase) const {
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000176 const GrTextureDomainEffect& s = static_cast<const GrTextureDomainEffect&>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000177 return this->hasSameTextureParamsAndMatrix(s) && this->fTextureDomain == s.fTextureDomain;
178}
179
180void GrTextureDomainEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
181 if (kDecal_WrapMode == fWrapMode) {
182 *validFlags = 0;
183 } else {
184 this->updateConstantColorComponentsForModulation(color, validFlags);
185 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000186}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000187
188///////////////////////////////////////////////////////////////////////////////
189
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000190GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000191
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000192GrEffectRef* GrTextureDomainEffect::TestCreate(SkRandom* random,
193 GrContext* context,
194 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000195 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
196 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000197 GrRect domain;
198 domain.fLeft = random->nextUScalar1();
199 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1);
200 domain.fTop = random->nextUScalar1();
201 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000202 WrapMode wrapMode = random->nextBool() ? kClamp_WrapMode : kDecal_WrapMode;
203 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
204 return GrTextureDomainEffect::Create(textures[texIdx], matrix, domain, wrapMode);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000205}