blob: c7eda889aa50aa3e08d4d62b5a9881bcb5b0c75d [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 , fEffectMatrix(drawEffect.castEffect<GrTextureDomainEffect>().coordsType()) {
bsalomon@google.com92b6a942012-11-02 19:50:26 +000042 fPrevDomain[0] = SK_FloatNaN;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000043}
44
bsalomon@google.com22a800a2012-10-26 19:16:46 +000045void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +000046 const GrDrawEffect& drawEffect,
bsalomon@google.com92b6a942012-11-02 19:50:26 +000047 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000048 const char* outputColor,
49 const char* inputColor,
50 const TextureSamplerArray& samplers) {
bsalomon@google.comc7818882013-03-20 19:19:53 +000051 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainEffect>();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000052
commit-bot@chromium.org7ab7ca42013-08-28 15:59:13 +000053 SkString coords;
bsalomon@google.comc7818882013-03-20 19:19:53 +000054 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000055 const char* domain;
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000056 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000057 kVec4f_GrSLType, "TexDom", &domain);
bsalomon@google.comc7818882013-03-20 19:19:53 +000058 if (GrTextureDomainEffect::kClamp_WrapMode == texDom.wrapMode()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000059
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000060 builder->fsCodeAppendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n",
commit-bot@chromium.org7ab7ca42013-08-28 15:59:13 +000061 coords.c_str(), domain, domain);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000062
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000063 builder->fsCodeAppendf("\t%s = ", outputColor);
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000064 builder->fsAppendTextureLookupAndModulate(inputColor, samplers[0], "clampCoord");
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000065 builder->fsCodeAppend(";\n");
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000066 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000067 SkASSERT(GrTextureDomainEffect::kDecal_WrapMode == texDom.wrapMode());
robertphillips@google.com13f181f2013-03-02 12:02:08 +000068
69 if (kImagination_GrGLVendor == builder->ctxInfo().vendor()) {
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +000070 // On the NexusS and GalaxyNexus, the other path (with the 'any'
71 // call) causes the compilation error "Calls to any function that
72 // may require a gradient calculation inside a conditional block
robertphillips@google.com13f181f2013-03-02 12:02:08 +000073 // may return undefined results". This appears to be an issue with
74 // the 'any' call since even the simple "result=black; if (any())
75 // result=white;" code fails to compile.
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000076 builder->fsCodeAppend("\tvec4 outside = vec4(0.0, 0.0, 0.0, 0.0);\n");
77 builder->fsCodeAppend("\tvec4 inside = ");
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000078 builder->fsAppendTextureLookupAndModulate(inputColor, samplers[0], coords.c_str());
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000079 builder->fsCodeAppend(";\n");
robertphillips@google.com13f181f2013-03-02 12:02:08 +000080
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000081 builder->fsCodeAppendf("\tfloat x = abs(2.0*(%s.x - %s.x)/(%s.z - %s.x) - 1.0);\n",
commit-bot@chromium.org7ab7ca42013-08-28 15:59:13 +000082 coords.c_str(), domain, domain, domain);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000083 builder->fsCodeAppendf("\tfloat y = abs(2.0*(%s.y - %s.y)/(%s.w - %s.y) - 1.0);\n",
commit-bot@chromium.org7ab7ca42013-08-28 15:59:13 +000084 coords.c_str(), domain, domain, domain);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000085 builder->fsCodeAppend("\tfloat blend = step(1.0, max(x, y));\n");
86 builder->fsCodeAppendf("\t%s = mix(inside, outside, blend);\n", outputColor);
robertphillips@google.com13f181f2013-03-02 12:02:08 +000087 } else {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000088 builder->fsCodeAppend("\tbvec4 outside;\n");
commit-bot@chromium.org7ab7ca42013-08-28 15:59:13 +000089 builder->fsCodeAppendf("\toutside.xy = lessThan(%s, %s.xy);\n", coords.c_str(), domain);
90 builder->fsCodeAppendf("\toutside.zw = greaterThan(%s, %s.zw);\n", coords.c_str(), domain);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000091 builder->fsCodeAppendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ", outputColor);
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000092 builder->fsAppendTextureLookupAndModulate(inputColor, samplers[0], coords.c_str());
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000093 builder->fsCodeAppend(";\n");
robertphillips@google.com13f181f2013-03-02 12:02:08 +000094 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000095 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +000096}
97
bsalomon@google.comc7818882013-03-20 19:19:53 +000098void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman,
99 const GrDrawEffect& drawEffect) {
100 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainEffect>();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000101 const SkRect& domain = texDom.domain();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000102
103 float values[4] = {
bsalomon@google.com81712882012-11-01 17:12:34 +0000104 SkScalarToFloat(domain.left()),
105 SkScalarToFloat(domain.top()),
106 SkScalarToFloat(domain.right()),
107 SkScalarToFloat(domain.bottom())
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000108 };
109 // vertical flip if necessary
bsalomon@google.comc7818882013-03-20 19:19:53 +0000110 if (kBottomLeft_GrSurfaceOrigin == texDom.texture(0)->origin()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000111 values[1] = 1.0f - values[1];
112 values[3] = 1.0f - values[3];
113 // The top and bottom were just flipped, so correct the ordering
114 // of elements so that values = (l, t, r, b).
115 SkTSwap(values[1], values[3]);
116 }
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000117 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) {
118 uman.set4fv(fNameUni, 0, 1, values);
robertphillips@google.com6b96d362013-04-15 21:51:52 +0000119 memcpy(fPrevDomain, values, 4 * sizeof(GrGLfloat));
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000120 }
121 fEffectMatrix.setData(uman,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000122 texDom.getMatrix(),
123 drawEffect,
124 texDom.texture(0));
skia.committer@gmail.com1aa90cf2012-11-06 13:18:25 +0000125}
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000126
bsalomon@google.comc7818882013-03-20 19:19:53 +0000127GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrDrawEffect& drawEffect,
128 const GrGLCaps&) {
129 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainEffect>();
130 EffectKey key = texDom.wrapMode();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000131 key <<= GrGLEffectMatrix::kKeyBits;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000132 EffectKey matrixKey = GrGLEffectMatrix::GenKey(texDom.getMatrix(),
133 drawEffect,
134 texDom.coordsType(),
135 texDom.texture(0));
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000136 return key | matrixKey;
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000137}
138
139
140///////////////////////////////////////////////////////////////////////////////
141
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000142GrEffectRef* GrTextureDomainEffect::Create(GrTexture* texture,
143 const SkMatrix& matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000144 const SkRect& domain,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000145 WrapMode wrapMode,
humper@google.comb86add12013-07-25 18:49:07 +0000146 GrTextureParams::FilterMode filterMode,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000147 CoordsType coordsType) {
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)) {
humper@google.comb86add12013-07-25 18:49:07 +0000150 return GrSimpleTextureEffect::Create(texture, matrix, filterMode);
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.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000156 SkASSERT(domain.fLeft <= domain.fRight);
157 SkASSERT(domain.fTop <= domain.fBottom);
bsalomon@google.comfe4e4912012-11-08 20:32:13 +0000158 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);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000162 SkASSERT(clippedDomain.fLeft <= clippedDomain.fRight);
163 SkASSERT(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,
humper@google.comb86add12013-07-25 18:49:07 +0000169 filterMode,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000170 coordsType)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000171 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000172
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000173 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000174}
175
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000176GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000177 const SkMatrix& matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000178 const SkRect& domain,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000179 WrapMode wrapMode,
humper@google.comb86add12013-07-25 18:49:07 +0000180 GrTextureParams::FilterMode filterMode,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000181 CoordsType coordsType)
humper@google.comb86add12013-07-25 18:49:07 +0000182 : GrSingleTextureEffect(texture, matrix, filterMode, coordsType)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000183 , fWrapMode(wrapMode)
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000184 , fTextureDomain(domain) {
185}
186
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000187GrTextureDomainEffect::~GrTextureDomainEffect() {
188
189}
190
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000191const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const {
192 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000193}
194
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000195bool GrTextureDomainEffect::onIsEqual(const GrEffect& sBase) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000196 const GrTextureDomainEffect& s = CastEffect<GrTextureDomainEffect>(sBase);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000197 return this->hasSameTextureParamsMatrixAndCoordsType(s) &&
198 this->fTextureDomain == s.fTextureDomain;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000199}
200
201void GrTextureDomainEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
202 if (kDecal_WrapMode == fWrapMode) {
203 *validFlags = 0;
204 } else {
205 this->updateConstantColorComponentsForModulation(color, validFlags);
206 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000207}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000208
209///////////////////////////////////////////////////////////////////////////////
210
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000211GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000212
bsalomon@google.com73a96942013-02-13 16:31:19 +0000213GrEffectRef* GrTextureDomainEffect::TestCreate(SkMWCRandom* random,
sugoi@google.come0e385c2013-03-11 18:50:03 +0000214 GrContext*,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000215 const GrDrawTargetCaps&,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000216 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000217 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
218 GrEffectUnitTest::kAlphaTextureIdx;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000219 SkRect domain;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000220 domain.fLeft = random->nextUScalar1();
221 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1);
222 domain.fTop = random->nextUScalar1();
223 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000224 WrapMode wrapMode = random->nextBool() ? kClamp_WrapMode : kDecal_WrapMode;
225 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000226 bool bilerp = random->nextBool();
227 CoordsType coords = random->nextBool() ? kLocal_CoordsType : kPosition_CoordsType;
228 return GrTextureDomainEffect::Create(textures[texIdx],
229 matrix,
230 domain,
231 wrapMode,
humper@google.comb86add12013-07-25 18:49:07 +0000232 bilerp ? GrTextureParams::kBilerp_FilterMode : GrTextureParams::kNone_FilterMode,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000233 coords);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000234}