blob: e500fad4a80b49acc766203c41606abc327789ed [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.com2eaaefd2012-10-29 19:51:22 +00009#include "GrTBackendEffectFactory.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +000010#include "gl/GrGLEffect.h"
bsalomon@google.com92b6a942012-11-02 19:50:26 +000011#include "gl/GrGLEffectMatrix.h"
12#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.com396e61f2012-10-25 19:00:29 +000016 GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrEffect&);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000017
bsalomon@google.com22a800a2012-10-26 19:16:46 +000018 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000019 const GrEffectStage&,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000020 EffectKey,
21 const char* vertexCoords,
22 const char* outputColor,
23 const char* inputColor,
24 const TextureSamplerArray&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000025
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000026 virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000027
bsalomon@google.com92b6a942012-11-02 19:50:26 +000028 static inline EffectKey GenKey(const GrEffectStage&, 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.com021fc732012-10-25 12:47:42 +000039 const GrEffect&)
bsalomon@google.com374e7592012-10-23 17:30:45 +000040 : INHERITED(factory)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000041 , fNameUni(GrGLUniformManager::kInvalidUniformHandle) {
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.com7b7cdd12012-11-07 16:17:24 +000046 const GrEffectStage& stage,
bsalomon@google.com92b6a942012-11-02 19:50:26 +000047 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000048 const char* vertexCoords,
49 const char* outputColor,
50 const char* inputColor,
51 const TextureSamplerArray& samplers) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000052 const GrTextureDomainEffect& effect =
53 static_cast<const GrTextureDomainEffect&>(*stage.getEffect());
54
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());
73 builder->fFSCode.append("\tbvec4 outside;\n");
74 builder->fFSCode.appendf("\toutside.xy = lessThan(%s, %s.xy);\n", coords, domain);
75 builder->fFSCode.appendf("\toutside.zw = greaterThan(%s, %s.zw);\n", coords, domain);
76 builder->fFSCode.appendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ", outputColor);
77 builder->appendTextureLookupAndModulate(&builder->fFSCode, inputColor, samplers[0], coords);
78 builder->fFSCode.append(";\n");
79 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +000080}
81
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000082void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
83 const GrTextureDomainEffect& effect =
84 static_cast<const GrTextureDomainEffect&>(*stage.getEffect());
tomhudson@google.com2f68e762012-07-17 18:43:21 +000085 const GrRect& domain = effect.domain();
86
87 float values[4] = {
bsalomon@google.com81712882012-11-01 17:12:34 +000088 SkScalarToFloat(domain.left()),
89 SkScalarToFloat(domain.top()),
90 SkScalarToFloat(domain.right()),
91 SkScalarToFloat(domain.bottom())
tomhudson@google.com2f68e762012-07-17 18:43:21 +000092 };
93 // vertical flip if necessary
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000094 if (GrSurface::kBottomLeft_Origin == effect.texture(0)->origin()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000095 values[1] = 1.0f - values[1];
96 values[3] = 1.0f - values[3];
97 // The top and bottom were just flipped, so correct the ordering
98 // of elements so that values = (l, t, r, b).
99 SkTSwap(values[1], values[3]);
100 }
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000101 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) {
102 uman.set4fv(fNameUni, 0, 1, values);
103 }
104 fEffectMatrix.setData(uman,
105 effect.getMatrix(),
106 stage.getCoordChangeMatrix(),
107 effect.texture(0));
skia.committer@gmail.com1aa90cf2012-11-06 13:18:25 +0000108}
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000109
110GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrEffectStage& stage, const GrGLCaps&) {
111 const GrTextureDomainEffect& effect =
112 static_cast<const GrTextureDomainEffect&>(*stage.getEffect());
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000113 EffectKey key = effect.wrapMode();
114 key <<= GrGLEffectMatrix::kKeyBits;
115 EffectKey matrixKey = GrGLEffectMatrix::GenKey(effect.getMatrix(),
116 stage.getCoordChangeMatrix(),
117 effect.texture(0));
118 return key | matrixKey;
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000119}
120
121
122///////////////////////////////////////////////////////////////////////////////
123
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000124GrEffect* GrTextureDomainEffect::Create(GrTexture* texture,
125 const SkMatrix& matrix,
126 const GrRect& domain,
127 WrapMode wrapMode,
128 bool bilerp) {
129 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
130 if (kClamp_WrapMode == wrapMode && domain.contains(kFullRect)) {
131 return SkNEW_ARGS(GrSingleTextureEffect, (texture, matrix, bilerp));
132 } else {
133 SkRect clippedDomain;
134 // We don't currently handle domains that are empty or don't intersect the texture.
135 SkAssertResult(clippedDomain.intersect(kFullRect, domain));
136 return SkNEW_ARGS(GrTextureDomainEffect,
137 (texture, matrix, clippedDomain, wrapMode, bilerp));
138 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000139}
140
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000141GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000142 const SkMatrix& matrix,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000143 const GrRect& domain,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000144 WrapMode wrapMode,
145 bool bilerp)
146 : GrSingleTextureEffect(texture, matrix, bilerp)
147 , fWrapMode(wrapMode)
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000148 , fTextureDomain(domain) {
149}
150
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000151GrTextureDomainEffect::~GrTextureDomainEffect() {
152
153}
154
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000155const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const {
156 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000157}
158
bsalomon@google.coma469c282012-10-24 18:28:34 +0000159bool GrTextureDomainEffect::isEqual(const GrEffect& sBase) const {
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000160 const GrTextureDomainEffect& s = static_cast<const GrTextureDomainEffect&>(sBase);
161 return (INHERITED::isEqual(sBase) && this->fTextureDomain == s.fTextureDomain);
162}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000163
164///////////////////////////////////////////////////////////////////////////////
165
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000166GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000167
bsalomon@google.coma469c282012-10-24 18:28:34 +0000168GrEffect* GrTextureDomainEffect::TestCreate(SkRandom* random,
169 GrContext* context,
170 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000171 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
172 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000173 GrRect domain;
174 domain.fLeft = random->nextUScalar1();
175 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1);
176 domain.fTop = random->nextUScalar1();
177 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000178 WrapMode wrapMode = random->nextBool() ? kClamp_WrapMode : kDecal_WrapMode;
179 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
180 return GrTextureDomainEffect::Create(textures[texIdx], matrix, domain, wrapMode);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000181}