commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +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 "GrTextureDomain.h" |
| 9 | #include "GrSimpleTextureEffect.h" |
| 10 | #include "GrTBackendEffectFactory.h" |
| 11 | #include "gl/GrGLEffect.h" |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 12 | #include "gl/GrGLShaderBuilder.h" |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 13 | #include "SkFloatingPoint.h" |
| 14 | |
| 15 | |
| 16 | GrTextureDomain::GrTextureDomain(const SkRect& domain, Mode mode, int index) |
| 17 | : fIndex(index) { |
| 18 | |
| 19 | static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1}; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 20 | if (domain.contains(kFullRect) && kClamp_Mode == mode) { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 21 | fMode = kIgnore_Mode; |
| 22 | } else { |
| 23 | fMode = mode; |
| 24 | } |
| 25 | |
| 26 | if (fMode != kIgnore_Mode) { |
| 27 | // We don't currently handle domains that are empty or don't intersect the texture. |
| 28 | // It is OK if the domain rect is a line or point, but it should not be inverted. We do not |
| 29 | // handle rects that do not intersect the [0..1]x[0..1] rect. |
| 30 | SkASSERT(domain.fLeft <= domain.fRight); |
| 31 | SkASSERT(domain.fTop <= domain.fBottom); |
| 32 | fDomain.fLeft = SkMaxScalar(domain.fLeft, kFullRect.fLeft); |
| 33 | fDomain.fRight = SkMinScalar(domain.fRight, kFullRect.fRight); |
| 34 | fDomain.fTop = SkMaxScalar(domain.fTop, kFullRect.fTop); |
| 35 | fDomain.fBottom = SkMinScalar(domain.fBottom, kFullRect.fBottom); |
| 36 | SkASSERT(fDomain.fLeft <= fDomain.fRight); |
| 37 | SkASSERT(fDomain.fTop <= fDomain.fBottom); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | ////////////////////////////////////////////////////////////////////////////// |
| 42 | |
| 43 | void GrTextureDomain::GLDomain::sampleTexture(GrGLShaderBuilder* builder, |
| 44 | const GrTextureDomain& textureDomain, |
| 45 | const char* outColor, |
| 46 | const SkString& inCoords, |
| 47 | const GrGLEffect::TextureSampler sampler, |
| 48 | const char* inModulateColor) { |
reed@google.com | d7b1af6 | 2013-12-09 20:31:50 +0000 | [diff] [blame] | 49 | SkASSERT((Mode)-1 == fMode || textureDomain.mode() == fMode); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 50 | SkDEBUGCODE(fMode = textureDomain.mode();) |
| 51 | |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame^] | 52 | if (textureDomain.mode() != kIgnore_Mode && !fDomainUni.isValid()) { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 53 | const char* name; |
| 54 | SkString uniName("TexDom"); |
| 55 | if (textureDomain.fIndex >= 0) { |
| 56 | uniName.appendS32(textureDomain.fIndex); |
| 57 | } |
| 58 | fDomainUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame^] | 59 | kVec4f_GrSLType, uniName.c_str(), &name); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 60 | fDomainName = name; |
| 61 | } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 62 | |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame^] | 63 | switch (textureDomain.mode()) { |
| 64 | case kIgnore_Mode: { |
| 65 | builder->fsCodeAppendf("\t%s = ", outColor); |
| 66 | builder->fsAppendTextureLookupAndModulate(inModulateColor, sampler, |
| 67 | inCoords.c_str()); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 68 | builder->fsCodeAppend(";\n"); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame^] | 69 | break; |
| 70 | } |
| 71 | case kClamp_Mode: { |
| 72 | SkString clampedCoords; |
| 73 | clampedCoords.appendf("\tclamp(%s, %s.xy, %s.zw)", |
| 74 | inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str()); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 75 | |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame^] | 76 | builder->fsCodeAppendf("\t%s = ", outColor); |
| 77 | builder->fsAppendTextureLookupAndModulate(inModulateColor, sampler, |
| 78 | clampedCoords.c_str()); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 79 | builder->fsCodeAppend(";\n"); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame^] | 80 | break; |
| 81 | } |
| 82 | case kDecal_Mode: { |
| 83 | // Add a block since we're going to declare variables. |
| 84 | GrGLShaderBuilder::FSBlock block(builder); |
| 85 | |
| 86 | const char* domain = fDomainName.c_str(); |
| 87 | if (kImagination_GrGLVendor == builder->ctxInfo().vendor()) { |
| 88 | // On the NexusS and GalaxyNexus, the other path (with the 'any' |
| 89 | // call) causes the compilation error "Calls to any function that |
| 90 | // may require a gradient calculation inside a conditional block |
| 91 | // may return undefined results". This appears to be an issue with |
| 92 | // the 'any' call since even the simple "result=black; if (any()) |
| 93 | // result=white;" code fails to compile. |
| 94 | builder->fsCodeAppend("\tvec4 outside = vec4(0.0, 0.0, 0.0, 0.0);\n"); |
| 95 | builder->fsCodeAppend("\tvec4 inside = "); |
| 96 | builder->fsAppendTextureLookupAndModulate(inModulateColor, sampler, |
| 97 | inCoords.c_str()); |
| 98 | builder->fsCodeAppend(";\n"); |
| 99 | |
| 100 | builder->fsCodeAppendf("\tfloat x = abs(2.0*(%s.x - %s.x)/(%s.z - %s.x) - 1.0);\n", |
| 101 | inCoords.c_str(), domain, domain, domain); |
| 102 | builder->fsCodeAppendf("\tfloat y = abs(2.0*(%s.y - %s.y)/(%s.w - %s.y) - 1.0);\n", |
| 103 | inCoords.c_str(), domain, domain, domain); |
| 104 | builder->fsCodeAppend("\tfloat blend = step(1.0, max(x, y));\n"); |
| 105 | builder->fsCodeAppendf("\t%s = mix(inside, outside, blend);\n", outColor); |
| 106 | } else { |
| 107 | builder->fsCodeAppend("\tbvec4 outside;\n"); |
| 108 | builder->fsCodeAppendf("\toutside.xy = lessThan(%s, %s.xy);\n", inCoords.c_str(), |
| 109 | domain); |
| 110 | builder->fsCodeAppendf("\toutside.zw = greaterThan(%s, %s.zw);\n", inCoords.c_str(), |
| 111 | domain); |
| 112 | builder->fsCodeAppendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ", |
| 113 | outColor); |
| 114 | builder->fsAppendTextureLookupAndModulate(inModulateColor, sampler, |
| 115 | inCoords.c_str()); |
| 116 | builder->fsCodeAppend(";\n"); |
| 117 | } |
| 118 | break; |
| 119 | } |
| 120 | case kRepeat_Mode: { |
| 121 | SkString clampedCoords; |
| 122 | clampedCoords.printf("\tmod(%s - %s.xy, %s.zw - %s.xy) + %s.xy", |
| 123 | inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str(), |
| 124 | fDomainName.c_str(), fDomainName.c_str()); |
| 125 | |
| 126 | builder->fsCodeAppendf("\t%s = ", outColor); |
| 127 | builder->fsAppendTextureLookupAndModulate(inModulateColor, sampler, |
| 128 | clampedCoords.c_str()); |
| 129 | builder->fsCodeAppend(";\n"); |
| 130 | break; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void GrTextureDomain::GLDomain::setData(const GrGLUniformManager& uman, |
| 136 | const GrTextureDomain& textureDomain, |
| 137 | GrSurfaceOrigin textureOrigin) { |
| 138 | SkASSERT(textureDomain.mode() == fMode); |
| 139 | if (kIgnore_Mode != textureDomain.mode()) { |
| 140 | GrGLfloat values[4] = { |
| 141 | SkScalarToFloat(textureDomain.domain().left()), |
| 142 | SkScalarToFloat(textureDomain.domain().top()), |
| 143 | SkScalarToFloat(textureDomain.domain().right()), |
| 144 | SkScalarToFloat(textureDomain.domain().bottom()) |
| 145 | }; |
| 146 | // vertical flip if necessary |
| 147 | if (kBottomLeft_GrSurfaceOrigin == textureOrigin) { |
| 148 | values[1] = 1.0f - values[1]; |
| 149 | values[3] = 1.0f - values[3]; |
| 150 | // The top and bottom were just flipped, so correct the ordering |
| 151 | // of elements so that values = (l, t, r, b). |
| 152 | SkTSwap(values[1], values[3]); |
| 153 | } |
| 154 | if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) { |
| 155 | uman.set4fv(fDomainUni, 1, values); |
| 156 | memcpy(fPrevDomain, values, 4 * sizeof(GrGLfloat)); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | |
| 162 | ////////////////////////////////////////////////////////////////////////////// |
| 163 | |
| 164 | class GrGLTextureDomainEffect : public GrGLEffect { |
| 165 | public: |
| 166 | GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 167 | |
| 168 | virtual void emitCode(GrGLShaderBuilder*, |
| 169 | const GrDrawEffect&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 170 | const GrEffectKey&, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 171 | const char* outputColor, |
| 172 | const char* inputColor, |
| 173 | const TransformedCoordsArray&, |
| 174 | const TextureSamplerArray&) SK_OVERRIDE; |
| 175 | |
| 176 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
| 177 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 178 | static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder*); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 179 | |
| 180 | private: |
| 181 | GrTextureDomain::GLDomain fGLDomain; |
| 182 | typedef GrGLEffect INHERITED; |
| 183 | }; |
| 184 | |
| 185 | GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& factory, |
| 186 | const GrDrawEffect&) |
| 187 | : INHERITED(factory) { |
| 188 | } |
| 189 | |
| 190 | void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder, |
| 191 | const GrDrawEffect& drawEffect, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 192 | const GrEffectKey& key, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 193 | const char* outputColor, |
| 194 | const char* inputColor, |
| 195 | const TransformedCoordsArray& coords, |
| 196 | const TextureSamplerArray& samplers) { |
| 197 | const GrTextureDomainEffect& effect = drawEffect.castEffect<GrTextureDomainEffect>(); |
| 198 | const GrTextureDomain& domain = effect.textureDomain(); |
| 199 | |
| 200 | SkString coords2D = builder->ensureFSCoords2D(coords, 0); |
| 201 | fGLDomain.sampleTexture(builder, domain, outputColor, coords2D, samplers[0], inputColor); |
| 202 | } |
| 203 | |
| 204 | void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, |
| 205 | const GrDrawEffect& drawEffect) { |
| 206 | const GrTextureDomainEffect& effect = drawEffect.castEffect<GrTextureDomainEffect>(); |
| 207 | const GrTextureDomain& domain = effect.textureDomain(); |
| 208 | fGLDomain.setData(uman, domain, effect.texture(0)->origin()); |
| 209 | } |
| 210 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 211 | void GrGLTextureDomainEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&, |
| 212 | GrEffectKeyBuilder* b) { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 213 | const GrTextureDomain& domain = drawEffect.castEffect<GrTextureDomainEffect>().textureDomain(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 214 | b->add32(GrTextureDomain::GLDomain::DomainKey(domain)); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | |
| 218 | /////////////////////////////////////////////////////////////////////////////// |
| 219 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 220 | GrEffect* GrTextureDomainEffect::Create(GrTexture* texture, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 221 | const SkMatrix& matrix, |
| 222 | const SkRect& domain, |
| 223 | GrTextureDomain::Mode mode, |
| 224 | GrTextureParams::FilterMode filterMode, |
| 225 | GrCoordSet coordSet) { |
| 226 | static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1}; |
| 227 | if (GrTextureDomain::kIgnore_Mode == mode || |
| 228 | (GrTextureDomain::kClamp_Mode == mode && domain.contains(kFullRect))) { |
| 229 | return GrSimpleTextureEffect::Create(texture, matrix, filterMode); |
| 230 | } else { |
| 231 | |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 232 | return SkNEW_ARGS(GrTextureDomainEffect, (texture, |
| 233 | matrix, |
| 234 | domain, |
| 235 | mode, |
| 236 | filterMode, |
| 237 | coordSet)); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, |
| 242 | const SkMatrix& matrix, |
| 243 | const SkRect& domain, |
| 244 | GrTextureDomain::Mode mode, |
| 245 | GrTextureParams::FilterMode filterMode, |
| 246 | GrCoordSet coordSet) |
| 247 | : GrSingleTextureEffect(texture, matrix, filterMode, coordSet) |
| 248 | , fTextureDomain(domain, mode) { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame^] | 249 | SkASSERT(mode != GrTextureDomain::kRepeat_Mode || |
| 250 | filterMode == GrTextureParams::kNone_FilterMode); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | GrTextureDomainEffect::~GrTextureDomainEffect() { |
| 254 | |
| 255 | } |
| 256 | |
| 257 | const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const { |
| 258 | return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance(); |
| 259 | } |
| 260 | |
| 261 | bool GrTextureDomainEffect::onIsEqual(const GrEffect& sBase) const { |
| 262 | const GrTextureDomainEffect& s = CastEffect<GrTextureDomainEffect>(sBase); |
| 263 | return this->hasSameTextureParamsMatrixAndSourceCoords(s) && |
| 264 | this->fTextureDomain == s.fTextureDomain; |
| 265 | } |
| 266 | |
| 267 | void GrTextureDomainEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 268 | if (GrTextureDomain::kDecal_Mode == fTextureDomain.mode()) { // TODO: helper |
| 269 | *validFlags = 0; |
| 270 | } else { |
| 271 | this->updateConstantColorComponentsForModulation(color, validFlags); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /////////////////////////////////////////////////////////////////////////////// |
| 276 | |
| 277 | GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect); |
| 278 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 279 | GrEffect* GrTextureDomainEffect::TestCreate(SkRandom* random, |
| 280 | GrContext*, |
| 281 | const GrDrawTargetCaps&, |
| 282 | GrTexture* textures[]) { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 283 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 284 | GrEffectUnitTest::kAlphaTextureIdx; |
| 285 | SkRect domain; |
| 286 | domain.fLeft = random->nextUScalar1(); |
| 287 | domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1); |
| 288 | domain.fTop = random->nextUScalar1(); |
| 289 | domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1); |
| 290 | GrTextureDomain::Mode mode = |
| 291 | (GrTextureDomain::Mode) random->nextULessThan(GrTextureDomain::kModeCount); |
| 292 | const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame^] | 293 | bool bilerp = mode != GrTextureDomain::kRepeat_Mode ? random->nextBool() : false; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 294 | GrCoordSet coords = random->nextBool() ? kLocal_GrCoordSet : kPosition_GrCoordSet; |
| 295 | return GrTextureDomainEffect::Create(textures[texIdx], |
| 296 | matrix, |
| 297 | domain, |
| 298 | mode, |
| 299 | bilerp ? GrTextureParams::kBilerp_FilterMode : GrTextureParams::kNone_FilterMode, |
| 300 | coords); |
| 301 | } |