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 | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 17 | GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrEffect&); |
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 | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 40 | const GrEffect&) |
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 | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 53 | const GrTextureDomainEffect& effect = |
| 54 | static_cast<const GrTextureDomainEffect&>(*stage.getEffect()); |
| 55 | |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 56 | const char* coords; |
| 57 | fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 58 | const char* domain; |
tomhudson@google.com | 57143a2 | 2012-07-17 19:07:35 +0000 | [diff] [blame] | 59 | fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 60 | kVec4f_GrSLType, "TexDom", &domain); |
| 61 | if (GrTextureDomainEffect::kClamp_WrapMode == effect.wrapMode()) { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 62 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 63 | builder->fFSCode.appendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n", |
| 64 | coords, domain, domain); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 65 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 66 | 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.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 81 | } |
| 82 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 83 | void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) { |
| 84 | const GrTextureDomainEffect& effect = |
| 85 | static_cast<const GrTextureDomainEffect&>(*stage.getEffect()); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 86 | const GrRect& domain = effect.domain(); |
| 87 | |
| 88 | float values[4] = { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 89 | SkScalarToFloat(domain.left()), |
| 90 | SkScalarToFloat(domain.top()), |
| 91 | SkScalarToFloat(domain.right()), |
| 92 | SkScalarToFloat(domain.bottom()) |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 93 | }; |
| 94 | // vertical flip if necessary |
bsalomon@google.com | 2d0bade | 2012-10-26 19:01:17 +0000 | [diff] [blame] | 95 | if (GrSurface::kBottomLeft_Origin == effect.texture(0)->origin()) { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 96 | 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.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 102 | 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.com | 1aa90cf | 2012-11-06 13:18:25 +0000 | [diff] [blame] | 109 | } |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 110 | |
| 111 | GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrEffectStage& stage, const GrGLCaps&) { |
| 112 | const GrTextureDomainEffect& effect = |
| 113 | static_cast<const GrTextureDomainEffect&>(*stage.getEffect()); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 114 | 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.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
| 123 | /////////////////////////////////////////////////////////////////////////////// |
| 124 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 125 | GrEffectRef* GrTextureDomainEffect::Create(GrTexture* texture, |
| 126 | const SkMatrix& matrix, |
| 127 | const GrRect& domain, |
| 128 | WrapMode wrapMode, |
| 129 | bool bilerp) { |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 130 | static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1}; |
| 131 | if (kClamp_WrapMode == wrapMode && domain.contains(kFullRect)) { |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 132 | return GrSimpleTextureEffect::Create(texture, matrix, bilerp); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 133 | } else { |
| 134 | SkRect clippedDomain; |
| 135 | // 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] | 136 | // 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.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 146 | |
| 147 | SkAutoTUnref<GrEffect> effect(SkNEW_ARGS(GrTextureDomainEffect, (texture, |
| 148 | matrix, |
| 149 | clippedDomain, |
| 150 | wrapMode, |
| 151 | bilerp))); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 152 | return CreateEffectRef(effect); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 153 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 154 | } |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 155 | } |
| 156 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 157 | GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 158 | const SkMatrix& matrix, |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 159 | const GrRect& domain, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 160 | WrapMode wrapMode, |
| 161 | bool bilerp) |
| 162 | : GrSingleTextureEffect(texture, matrix, bilerp) |
| 163 | , fWrapMode(wrapMode) |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 164 | , fTextureDomain(domain) { |
| 165 | } |
| 166 | |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 167 | GrTextureDomainEffect::~GrTextureDomainEffect() { |
| 168 | |
| 169 | } |
| 170 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 171 | const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const { |
| 172 | return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance(); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 173 | } |
| 174 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 175 | bool GrTextureDomainEffect::onIsEqual(const GrEffect& sBase) const { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 176 | const GrTextureDomainEffect& s = static_cast<const GrTextureDomainEffect&>(sBase); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 177 | return this->hasSameTextureParamsAndMatrix(s) && this->fTextureDomain == s.fTextureDomain; |
| 178 | } |
| 179 | |
| 180 | void 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.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 186 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 187 | |
| 188 | /////////////////////////////////////////////////////////////////////////////// |
| 189 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 190 | GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 191 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 192 | GrEffectRef* GrTextureDomainEffect::TestCreate(SkRandom* random, |
| 193 | GrContext* context, |
| 194 | GrTexture* textures[]) { |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 195 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 196 | GrEffectUnitTest::kAlphaTextureIdx; |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 197 | 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.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 202 | 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.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 205 | } |