tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 9 | #include "GrSimpleTextureEffect.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 10 | #include "GrTBackendEffectFactory.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 11 | #include "gl/GrGLEffect.h" |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 12 | #include "gl/GrGLEffectMatrix.h" |
| 13 | #include "SkFloatingPoint.h" |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 14 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 15 | class GrGLTextureDomainEffect : public GrGLEffect { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 16 | public: |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame^] | 17 | GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrEffectRef&); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 18 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 19 | virtual void emitCode(GrGLShaderBuilder*, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 20 | const GrEffectStage&, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 21 | EffectKey, |
| 22 | const char* vertexCoords, |
| 23 | const char* outputColor, |
| 24 | const char* inputColor, |
| 25 | const TextureSamplerArray&) SK_OVERRIDE; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 26 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 27 | virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 28 | |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 29 | static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 30 | |
| 31 | private: |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 32 | GrGLUniformManager::UniformHandle fNameUni; |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 33 | GrGLEffectMatrix fEffectMatrix; |
| 34 | GrGLfloat fPrevDomain[4]; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 35 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 36 | typedef GrGLEffect INHERITED; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 39 | GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame^] | 40 | const GrEffectRef&) |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 41 | : INHERITED(factory) |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 42 | , fNameUni(GrGLUniformManager::kInvalidUniformHandle) { |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 43 | fPrevDomain[0] = SK_FloatNaN; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 44 | } |
| 45 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 46 | void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 47 | const GrEffectStage& stage, |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 48 | EffectKey key, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 49 | const char* vertexCoords, |
| 50 | const char* outputColor, |
| 51 | const char* inputColor, |
| 52 | const TextureSamplerArray& samplers) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame^] | 53 | const GrTextureDomainEffect& effect = GetEffectFromStage<GrTextureDomainEffect>(stage); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 54 | |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 55 | const char* coords; |
| 56 | fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 57 | const char* domain; |
tomhudson@google.com | 57143a2 | 2012-07-17 19:07:35 +0000 | [diff] [blame] | 58 | fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 59 | kVec4f_GrSLType, "TexDom", &domain); |
| 60 | if (GrTextureDomainEffect::kClamp_WrapMode == effect.wrapMode()) { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 61 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 62 | builder->fFSCode.appendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n", |
| 63 | coords, domain, domain); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 64 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 65 | 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.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 80 | } |
| 81 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 82 | void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame^] | 83 | const GrTextureDomainEffect& effect = GetEffectFromStage<GrTextureDomainEffect>(stage); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 84 | const GrRect& domain = effect.domain(); |
| 85 | |
| 86 | float values[4] = { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 87 | SkScalarToFloat(domain.left()), |
| 88 | SkScalarToFloat(domain.top()), |
| 89 | SkScalarToFloat(domain.right()), |
| 90 | SkScalarToFloat(domain.bottom()) |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 91 | }; |
| 92 | // vertical flip if necessary |
bsalomon@google.com | 2d0bade | 2012-10-26 19:01:17 +0000 | [diff] [blame] | 93 | if (GrSurface::kBottomLeft_Origin == effect.texture(0)->origin()) { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 94 | values[1] = 1.0f - values[1]; |
| 95 | values[3] = 1.0f - values[3]; |
| 96 | // The top and bottom were just flipped, so correct the ordering |
| 97 | // of elements so that values = (l, t, r, b). |
| 98 | SkTSwap(values[1], values[3]); |
| 99 | } |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 100 | if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) { |
| 101 | uman.set4fv(fNameUni, 0, 1, values); |
| 102 | } |
| 103 | fEffectMatrix.setData(uman, |
| 104 | effect.getMatrix(), |
| 105 | stage.getCoordChangeMatrix(), |
| 106 | effect.texture(0)); |
skia.committer@gmail.com | 1aa90cf | 2012-11-06 13:18:25 +0000 | [diff] [blame] | 107 | } |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 108 | |
| 109 | GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrEffectStage& stage, const GrGLCaps&) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame^] | 110 | const GrTextureDomainEffect& effect = GetEffectFromStage<GrTextureDomainEffect>(stage); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 111 | EffectKey key = effect.wrapMode(); |
| 112 | key <<= GrGLEffectMatrix::kKeyBits; |
| 113 | EffectKey matrixKey = GrGLEffectMatrix::GenKey(effect.getMatrix(), |
| 114 | stage.getCoordChangeMatrix(), |
| 115 | effect.texture(0)); |
| 116 | return key | matrixKey; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
| 120 | /////////////////////////////////////////////////////////////////////////////// |
| 121 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 122 | GrEffectRef* GrTextureDomainEffect::Create(GrTexture* texture, |
| 123 | const SkMatrix& matrix, |
| 124 | const GrRect& domain, |
| 125 | WrapMode wrapMode, |
| 126 | bool bilerp) { |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 127 | static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1}; |
| 128 | if (kClamp_WrapMode == wrapMode && domain.contains(kFullRect)) { |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 129 | return GrSimpleTextureEffect::Create(texture, matrix, bilerp); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 130 | } else { |
| 131 | SkRect clippedDomain; |
| 132 | // We don't currently handle domains that are empty or don't intersect the texture. |
bsalomon@google.com | fe4e491 | 2012-11-08 20:32:13 +0000 | [diff] [blame] | 133 | // It is OK if the domain rect is a line or point, but it should not be inverted. We do not |
| 134 | // handle rects that do not intersect the [0..1]x[0..1] rect. |
| 135 | GrAssert(domain.fLeft <= domain.fRight); |
| 136 | GrAssert(domain.fTop <= domain.fBottom); |
| 137 | clippedDomain.fLeft = SkMaxScalar(domain.fLeft, kFullRect.fLeft); |
| 138 | clippedDomain.fRight = SkMinScalar(domain.fRight, kFullRect.fRight); |
| 139 | clippedDomain.fTop = SkMaxScalar(domain.fTop, kFullRect.fTop); |
| 140 | clippedDomain.fBottom = SkMinScalar(domain.fBottom, kFullRect.fBottom); |
| 141 | GrAssert(clippedDomain.fLeft <= clippedDomain.fRight); |
| 142 | GrAssert(clippedDomain.fTop <= clippedDomain.fBottom); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 143 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame^] | 144 | AutoEffectUnref effect(SkNEW_ARGS(GrTextureDomainEffect, (texture, |
| 145 | matrix, |
| 146 | clippedDomain, |
| 147 | wrapMode, |
| 148 | bilerp))); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 149 | return CreateEffectRef(effect); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 150 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 151 | } |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 152 | } |
| 153 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 154 | GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 155 | const SkMatrix& matrix, |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 156 | const GrRect& domain, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 157 | WrapMode wrapMode, |
| 158 | bool bilerp) |
| 159 | : GrSingleTextureEffect(texture, matrix, bilerp) |
| 160 | , fWrapMode(wrapMode) |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 161 | , fTextureDomain(domain) { |
| 162 | } |
| 163 | |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 164 | GrTextureDomainEffect::~GrTextureDomainEffect() { |
| 165 | |
| 166 | } |
| 167 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 168 | const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const { |
| 169 | return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance(); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 170 | } |
| 171 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame^] | 172 | bool GrTextureDomainEffect::onIsEqual(const GrEffectRef& sBase) const { |
| 173 | const GrTextureDomainEffect& s = CastEffect<GrTextureDomainEffect>(sBase); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 174 | return this->hasSameTextureParamsAndMatrix(s) && this->fTextureDomain == s.fTextureDomain; |
| 175 | } |
| 176 | |
| 177 | void GrTextureDomainEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 178 | if (kDecal_WrapMode == fWrapMode) { |
| 179 | *validFlags = 0; |
| 180 | } else { |
| 181 | this->updateConstantColorComponentsForModulation(color, validFlags); |
| 182 | } |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 183 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 184 | |
| 185 | /////////////////////////////////////////////////////////////////////////////// |
| 186 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 187 | GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 188 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 189 | GrEffectRef* GrTextureDomainEffect::TestCreate(SkRandom* random, |
| 190 | GrContext* context, |
| 191 | GrTexture* textures[]) { |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 192 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 193 | GrEffectUnitTest::kAlphaTextureIdx; |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 194 | GrRect domain; |
| 195 | domain.fLeft = random->nextUScalar1(); |
| 196 | domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1); |
| 197 | domain.fTop = random->nextUScalar1(); |
| 198 | domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 199 | WrapMode wrapMode = random->nextBool() ? kClamp_WrapMode : kDecal_WrapMode; |
| 200 | const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random); |
| 201 | return GrTextureDomainEffect::Create(textures[texIdx], matrix, domain, wrapMode); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 202 | } |