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 | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 9 | #include "GrTBackendEffectFactory.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 10 | #include "gl/GrGLEffect.h" |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 11 | #include "gl/GrGLEffectMatrix.h" |
| 12 | #include "SkFloatingPoint.h" |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 13 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 14 | class GrGLTextureDomainEffect : public GrGLEffect { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 15 | public: |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 16 | GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrEffect&); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 18 | virtual void emitCode(GrGLShaderBuilder*, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 19 | const GrEffectStage&, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 20 | EffectKey, |
| 21 | const char* vertexCoords, |
| 22 | const char* outputColor, |
| 23 | const char* inputColor, |
| 24 | const TextureSamplerArray&) SK_OVERRIDE; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 25 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 26 | virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 27 | |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 28 | static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 29 | |
| 30 | private: |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 31 | GrGLUniformManager::UniformHandle fNameUni; |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 32 | GrGLEffectMatrix fEffectMatrix; |
| 33 | GrGLfloat fPrevDomain[4]; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 34 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 35 | typedef GrGLEffect INHERITED; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 38 | GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 39 | const GrEffect&) |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 40 | : INHERITED(factory) |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 41 | , fNameUni(GrGLUniformManager::kInvalidUniformHandle) { |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 42 | fPrevDomain[0] = SK_FloatNaN; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 43 | } |
| 44 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 45 | void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame^] | 46 | const GrEffectStage& stage, |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 47 | EffectKey key, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 48 | const char* vertexCoords, |
| 49 | const char* outputColor, |
| 50 | const char* inputColor, |
| 51 | const TextureSamplerArray& samplers) { |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame^] | 52 | const GrTextureDomainEffect& effect = |
| 53 | static_cast<const GrTextureDomainEffect&>(*stage.getEffect()); |
| 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) { |
| 83 | const GrTextureDomainEffect& effect = |
| 84 | static_cast<const GrTextureDomainEffect&>(*stage.getEffect()); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 85 | const GrRect& domain = effect.domain(); |
| 86 | |
| 87 | float values[4] = { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 88 | SkScalarToFloat(domain.left()), |
| 89 | SkScalarToFloat(domain.top()), |
| 90 | SkScalarToFloat(domain.right()), |
| 91 | SkScalarToFloat(domain.bottom()) |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 92 | }; |
| 93 | // vertical flip if necessary |
bsalomon@google.com | 2d0bade | 2012-10-26 19:01:17 +0000 | [diff] [blame] | 94 | if (GrSurface::kBottomLeft_Origin == effect.texture(0)->origin()) { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 95 | 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.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 101 | 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.com | 1aa90cf | 2012-11-06 13:18:25 +0000 | [diff] [blame] | 108 | } |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 109 | |
| 110 | GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrEffectStage& stage, const GrGLCaps&) { |
| 111 | const GrTextureDomainEffect& effect = |
| 112 | static_cast<const GrTextureDomainEffect&>(*stage.getEffect()); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame^] | 113 | 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.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
| 122 | /////////////////////////////////////////////////////////////////////////////// |
| 123 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame^] | 124 | GrEffect* 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.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 139 | } |
| 140 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 141 | GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame^] | 142 | const SkMatrix& matrix, |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 143 | const GrRect& domain, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame^] | 144 | WrapMode wrapMode, |
| 145 | bool bilerp) |
| 146 | : GrSingleTextureEffect(texture, matrix, bilerp) |
| 147 | , fWrapMode(wrapMode) |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 148 | , fTextureDomain(domain) { |
| 149 | } |
| 150 | |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 151 | GrTextureDomainEffect::~GrTextureDomainEffect() { |
| 152 | |
| 153 | } |
| 154 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 155 | const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const { |
| 156 | return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance(); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 157 | } |
| 158 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 159 | bool GrTextureDomainEffect::isEqual(const GrEffect& sBase) const { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 160 | const GrTextureDomainEffect& s = static_cast<const GrTextureDomainEffect&>(sBase); |
| 161 | return (INHERITED::isEqual(sBase) && this->fTextureDomain == s.fTextureDomain); |
| 162 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 163 | |
| 164 | /////////////////////////////////////////////////////////////////////////////// |
| 165 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 166 | GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 167 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 168 | GrEffect* GrTextureDomainEffect::TestCreate(SkRandom* random, |
| 169 | GrContext* context, |
| 170 | GrTexture* textures[]) { |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 171 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 172 | GrEffectUnitTest::kAlphaTextureIdx; |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 173 | 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.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame^] | 178 | 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.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 181 | } |