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()); |
robertphillips@google.com | 13f181f | 2013-03-02 12:02:08 +0000 | [diff] [blame^] | 73 | |
| 74 | if (kImagination_GrGLVendor == builder->ctxInfo().vendor()) { |
| 75 | // On the NexusS and GalaxyNexus, the other path (with the 'any' |
| 76 | // call) causes the compilation error "Calls to any function that |
| 77 | // may require a gradient calculation inside a conditional block |
| 78 | // may return undefined results". This appears to be an issue with |
| 79 | // the 'any' call since even the simple "result=black; if (any()) |
| 80 | // result=white;" code fails to compile. |
| 81 | builder->fFSCode.appendf("\tvec4 outside = vec4(0.0, 0.0, 0.0, 0.0);\n"); |
| 82 | builder->fFSCode.appendf("\tvec4 inside = "); |
| 83 | builder->appendTextureLookupAndModulate(&builder->fFSCode, inputColor, samplers[0], coords); |
| 84 | builder->fFSCode.appendf(";\n"); |
| 85 | |
| 86 | builder->fFSCode.appendf("\tfloat x = abs(2.0*(%s.x - %s.x)/(%s.z - %s.x) - 1.0);\n", |
| 87 | coords, domain, domain, domain); |
| 88 | builder->fFSCode.appendf("\tfloat y = abs(2.0*(%s.y - %s.y)/(%s.w - %s.y) - 1.0);\n", |
| 89 | coords, domain, domain, domain); |
| 90 | builder->fFSCode.appendf("\tfloat blend = step(1.0, max(x, y));\n"); |
| 91 | builder->fFSCode.appendf("\t%s = mix(inside, outside, blend);\n", outputColor); |
| 92 | } else { |
| 93 | builder->fFSCode.append("\tbvec4 outside;\n"); |
| 94 | builder->fFSCode.appendf("\toutside.xy = lessThan(%s, %s.xy);\n", coords, domain); |
| 95 | builder->fFSCode.appendf("\toutside.zw = greaterThan(%s, %s.zw);\n", coords, domain); |
| 96 | builder->fFSCode.appendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ", outputColor); |
| 97 | builder->appendTextureLookupAndModulate(&builder->fFSCode, inputColor, samplers[0], coords); |
| 98 | builder->fFSCode.append(";\n"); |
| 99 | } |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 100 | } |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 101 | } |
| 102 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 103 | void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 104 | const GrTextureDomainEffect& effect = GetEffectFromStage<GrTextureDomainEffect>(stage); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 105 | const GrRect& domain = effect.domain(); |
| 106 | |
| 107 | float values[4] = { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 108 | SkScalarToFloat(domain.left()), |
| 109 | SkScalarToFloat(domain.top()), |
| 110 | SkScalarToFloat(domain.right()), |
| 111 | SkScalarToFloat(domain.bottom()) |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 112 | }; |
| 113 | // vertical flip if necessary |
senorblanco@chromium.org | ef5dbe1 | 2013-01-28 16:42:38 +0000 | [diff] [blame] | 114 | if (kBottomLeft_GrSurfaceOrigin == effect.texture(0)->origin()) { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 115 | values[1] = 1.0f - values[1]; |
| 116 | values[3] = 1.0f - values[3]; |
| 117 | // The top and bottom were just flipped, so correct the ordering |
| 118 | // of elements so that values = (l, t, r, b). |
| 119 | SkTSwap(values[1], values[3]); |
| 120 | } |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 121 | if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) { |
| 122 | uman.set4fv(fNameUni, 0, 1, values); |
| 123 | } |
| 124 | fEffectMatrix.setData(uman, |
| 125 | effect.getMatrix(), |
| 126 | stage.getCoordChangeMatrix(), |
| 127 | effect.texture(0)); |
skia.committer@gmail.com | 1aa90cf | 2012-11-06 13:18:25 +0000 | [diff] [blame] | 128 | } |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 129 | |
| 130 | GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrEffectStage& stage, const GrGLCaps&) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 131 | const GrTextureDomainEffect& effect = GetEffectFromStage<GrTextureDomainEffect>(stage); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 132 | EffectKey key = effect.wrapMode(); |
| 133 | key <<= GrGLEffectMatrix::kKeyBits; |
| 134 | EffectKey matrixKey = GrGLEffectMatrix::GenKey(effect.getMatrix(), |
| 135 | stage.getCoordChangeMatrix(), |
| 136 | effect.texture(0)); |
| 137 | return key | matrixKey; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
| 141 | /////////////////////////////////////////////////////////////////////////////// |
| 142 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 143 | GrEffectRef* GrTextureDomainEffect::Create(GrTexture* texture, |
| 144 | const SkMatrix& matrix, |
| 145 | const GrRect& domain, |
| 146 | WrapMode wrapMode, |
| 147 | bool bilerp) { |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 148 | static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1}; |
| 149 | if (kClamp_WrapMode == wrapMode && domain.contains(kFullRect)) { |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 150 | return GrSimpleTextureEffect::Create(texture, matrix, bilerp); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 151 | } else { |
| 152 | SkRect clippedDomain; |
| 153 | // 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] | 154 | // 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. |
| 156 | GrAssert(domain.fLeft <= domain.fRight); |
| 157 | GrAssert(domain.fTop <= domain.fBottom); |
| 158 | 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); |
| 162 | GrAssert(clippedDomain.fLeft <= clippedDomain.fRight); |
| 163 | GrAssert(clippedDomain.fTop <= clippedDomain.fBottom); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 164 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 165 | AutoEffectUnref effect(SkNEW_ARGS(GrTextureDomainEffect, (texture, |
| 166 | matrix, |
| 167 | clippedDomain, |
| 168 | wrapMode, |
| 169 | bilerp))); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 170 | return CreateEffectRef(effect); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 171 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 172 | } |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 173 | } |
| 174 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 175 | GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 176 | const SkMatrix& matrix, |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 177 | const GrRect& domain, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 178 | WrapMode wrapMode, |
| 179 | bool bilerp) |
| 180 | : GrSingleTextureEffect(texture, matrix, bilerp) |
| 181 | , fWrapMode(wrapMode) |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 182 | , fTextureDomain(domain) { |
| 183 | } |
| 184 | |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 185 | GrTextureDomainEffect::~GrTextureDomainEffect() { |
| 186 | |
| 187 | } |
| 188 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 189 | const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const { |
| 190 | return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance(); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 191 | } |
| 192 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 193 | bool GrTextureDomainEffect::onIsEqual(const GrEffect& sBase) const { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 194 | const GrTextureDomainEffect& s = CastEffect<GrTextureDomainEffect>(sBase); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 195 | return this->hasSameTextureParamsAndMatrix(s) && this->fTextureDomain == s.fTextureDomain; |
| 196 | } |
| 197 | |
| 198 | void GrTextureDomainEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 199 | if (kDecal_WrapMode == fWrapMode) { |
| 200 | *validFlags = 0; |
| 201 | } else { |
| 202 | this->updateConstantColorComponentsForModulation(color, validFlags); |
| 203 | } |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 204 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 205 | |
| 206 | /////////////////////////////////////////////////////////////////////////////// |
| 207 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 208 | GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 209 | |
bsalomon@google.com | 73a9694 | 2013-02-13 16:31:19 +0000 | [diff] [blame] | 210 | GrEffectRef* GrTextureDomainEffect::TestCreate(SkMWCRandom* random, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 211 | GrContext* context, |
| 212 | GrTexture* textures[]) { |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 213 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 214 | GrEffectUnitTest::kAlphaTextureIdx; |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 215 | GrRect domain; |
| 216 | domain.fLeft = random->nextUScalar1(); |
| 217 | domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1); |
| 218 | domain.fTop = random->nextUScalar1(); |
| 219 | domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 220 | WrapMode wrapMode = random->nextBool() ? kClamp_WrapMode : kDecal_WrapMode; |
| 221 | const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random); |
| 222 | return GrTextureDomainEffect::Create(textures[texIdx], matrix, domain, wrapMode); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 223 | } |