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" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 9 | #include "GrInvariantOutput.h" |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 10 | #include "GrSimpleTextureEffect.h" |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 11 | #include "SkFloatingPoint.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 12 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 14 | #include "glsl/GrGLSLProgramDataManager.h" |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 15 | #include "glsl/GrGLSLSampler.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 16 | #include "glsl/GrGLSLShaderBuilder.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 17 | #include "glsl/GrGLSLUniformHandler.h" |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 18 | |
| 19 | GrTextureDomain::GrTextureDomain(const SkRect& domain, Mode mode, int index) |
| 20 | : fIndex(index) { |
| 21 | |
| 22 | static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1}; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 23 | if (domain.contains(kFullRect) && kClamp_Mode == mode) { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 24 | fMode = kIgnore_Mode; |
| 25 | } else { |
| 26 | fMode = mode; |
| 27 | } |
| 28 | |
| 29 | if (fMode != kIgnore_Mode) { |
| 30 | // We don't currently handle domains that are empty or don't intersect the texture. |
| 31 | // It is OK if the domain rect is a line or point, but it should not be inverted. We do not |
| 32 | // handle rects that do not intersect the [0..1]x[0..1] rect. |
| 33 | SkASSERT(domain.fLeft <= domain.fRight); |
| 34 | SkASSERT(domain.fTop <= domain.fBottom); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 35 | fDomain.fLeft = SkScalarPin(domain.fLeft, kFullRect.fLeft, kFullRect.fRight); |
| 36 | fDomain.fRight = SkScalarPin(domain.fRight, kFullRect.fLeft, kFullRect.fRight); |
| 37 | fDomain.fTop = SkScalarPin(domain.fTop, kFullRect.fTop, kFullRect.fBottom); |
| 38 | fDomain.fBottom = SkScalarPin(domain.fBottom, kFullRect.fTop, kFullRect.fBottom); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 39 | SkASSERT(fDomain.fLeft <= fDomain.fRight); |
| 40 | SkASSERT(fDomain.fTop <= fDomain.fBottom); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | ////////////////////////////////////////////////////////////////////////////// |
| 45 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 46 | void GrTextureDomain::GLDomain::sampleTexture(GrGLSLShaderBuilder* builder, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 47 | GrGLSLUniformHandler* uniformHandler, |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 48 | const GrGLSLCaps* glslCaps, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 49 | const GrTextureDomain& textureDomain, |
| 50 | const char* outColor, |
| 51 | const SkString& inCoords, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 52 | GrGLSLFragmentProcessor::SamplerHandle sampler, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 53 | const char* inModulateColor) { |
reed@google.com | d7b1af6 | 2013-12-09 20:31:50 +0000 | [diff] [blame] | 54 | SkASSERT((Mode)-1 == fMode || textureDomain.mode() == fMode); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 55 | SkDEBUGCODE(fMode = textureDomain.mode();) |
| 56 | |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 57 | if (textureDomain.mode() != kIgnore_Mode && !fDomainUni.isValid()) { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 58 | const char* name; |
| 59 | SkString uniName("TexDom"); |
| 60 | if (textureDomain.fIndex >= 0) { |
| 61 | uniName.appendS32(textureDomain.fIndex); |
| 62 | } |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 63 | fDomainUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 64 | kVec4f_GrSLType, kDefault_GrSLPrecision, |
| 65 | uniName.c_str(), &name); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 66 | fDomainName = name; |
| 67 | } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 68 | |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 69 | switch (textureDomain.mode()) { |
| 70 | case kIgnore_Mode: { |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 71 | builder->codeAppendf("%s = ", outColor); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 72 | builder->appendTextureLookupAndModulate(inModulateColor, sampler, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 73 | inCoords.c_str()); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 74 | builder->codeAppend(";"); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 75 | break; |
| 76 | } |
| 77 | case kClamp_Mode: { |
| 78 | SkString clampedCoords; |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 79 | clampedCoords.appendf("clamp(%s, %s.xy, %s.zw)", |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 80 | inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str()); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 81 | |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 82 | builder->codeAppendf("%s = ", outColor); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 83 | builder->appendTextureLookupAndModulate(inModulateColor, sampler, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 84 | clampedCoords.c_str()); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 85 | builder->codeAppend(";"); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 86 | break; |
| 87 | } |
| 88 | case kDecal_Mode: { |
| 89 | // Add a block since we're going to declare variables. |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 90 | GrGLSLShaderBuilder::ShaderBlock block(builder); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 91 | |
| 92 | const char* domain = fDomainName.c_str(); |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 93 | if (!glslCaps->canUseAnyFunctionInShader()) { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 94 | // On the NexusS and GalaxyNexus, the other path (with the 'any' |
| 95 | // call) causes the compilation error "Calls to any function that |
| 96 | // may require a gradient calculation inside a conditional block |
| 97 | // may return undefined results". This appears to be an issue with |
| 98 | // the 'any' call since even the simple "result=black; if (any()) |
| 99 | // result=white;" code fails to compile. |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 100 | builder->codeAppend("vec4 outside = vec4(0.0, 0.0, 0.0, 0.0);"); |
| 101 | builder->codeAppend("vec4 inside = "); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 102 | builder->appendTextureLookupAndModulate(inModulateColor, sampler, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 103 | inCoords.c_str()); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 104 | builder->codeAppend(";"); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 105 | |
cdalton | 1f50acf | 2016-04-11 11:30:50 -0700 | [diff] [blame] | 106 | builder->appendPrecisionModifier(kHigh_GrSLPrecision); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 107 | builder->codeAppendf("float x = (%s).x;", inCoords.c_str()); |
cdalton | 1f50acf | 2016-04-11 11:30:50 -0700 | [diff] [blame] | 108 | builder->appendPrecisionModifier(kHigh_GrSLPrecision); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 109 | builder->codeAppendf("float y = (%s).y;", inCoords.c_str()); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 110 | |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 111 | builder->codeAppendf("x = abs(2.0*(x - %s.x)/(%s.z - %s.x) - 1.0);", |
| 112 | domain, domain, domain); |
| 113 | builder->codeAppendf("y = abs(2.0*(y - %s.y)/(%s.w - %s.y) - 1.0);", |
| 114 | domain, domain, domain); |
| 115 | builder->codeAppend("float blend = step(1.0, max(x, y));"); |
| 116 | builder->codeAppendf("%s = mix(inside, outside, blend);", outColor); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 117 | } else { |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 118 | builder->codeAppend("bvec4 outside;\n"); |
| 119 | builder->codeAppendf("outside.xy = lessThan(%s, %s.xy);", inCoords.c_str(), |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 120 | domain); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 121 | builder->codeAppendf("outside.zw = greaterThan(%s, %s.zw);", inCoords.c_str(), |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 122 | domain); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 123 | builder->codeAppendf("%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ", |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 124 | outColor); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 125 | builder->appendTextureLookupAndModulate(inModulateColor, sampler, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 126 | inCoords.c_str()); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 127 | builder->codeAppend(";"); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 128 | } |
| 129 | break; |
| 130 | } |
| 131 | case kRepeat_Mode: { |
| 132 | SkString clampedCoords; |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 133 | clampedCoords.printf("mod(%s - %s.xy, %s.zw - %s.xy) + %s.xy", |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 134 | inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str(), |
| 135 | fDomainName.c_str(), fDomainName.c_str()); |
| 136 | |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 137 | builder->codeAppendf("%s = ", outColor); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 138 | builder->appendTextureLookupAndModulate(inModulateColor, sampler, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 139 | clampedCoords.c_str()); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 140 | builder->codeAppend(";"); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 141 | break; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 146 | void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 147 | const GrTextureDomain& textureDomain, |
| 148 | GrSurfaceOrigin textureOrigin) { |
| 149 | SkASSERT(textureDomain.mode() == fMode); |
| 150 | if (kIgnore_Mode != textureDomain.mode()) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 151 | float values[kPrevDomainCount] = { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 152 | SkScalarToFloat(textureDomain.domain().left()), |
| 153 | SkScalarToFloat(textureDomain.domain().top()), |
| 154 | SkScalarToFloat(textureDomain.domain().right()), |
| 155 | SkScalarToFloat(textureDomain.domain().bottom()) |
| 156 | }; |
| 157 | // vertical flip if necessary |
| 158 | if (kBottomLeft_GrSurfaceOrigin == textureOrigin) { |
| 159 | values[1] = 1.0f - values[1]; |
| 160 | values[3] = 1.0f - values[3]; |
| 161 | // The top and bottom were just flipped, so correct the ordering |
| 162 | // of elements so that values = (l, t, r, b). |
| 163 | SkTSwap(values[1], values[3]); |
| 164 | } |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 165 | if (0 != memcmp(values, fPrevDomain, kPrevDomainCount * sizeof(float))) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 166 | pdman.set4fv(fDomainUni, 1, values); |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 167 | memcpy(fPrevDomain, values, kPrevDomainCount * sizeof(float)); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 172 | /////////////////////////////////////////////////////////////////////////////// |
| 173 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 174 | sk_sp<GrFragmentProcessor> GrTextureDomainEffect::Make(GrTexture* texture, |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 175 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 176 | const SkMatrix& matrix, |
| 177 | const SkRect& domain, |
| 178 | GrTextureDomain::Mode mode, |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 179 | GrTextureParams::FilterMode filterMode) { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 180 | static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1}; |
| 181 | if (GrTextureDomain::kIgnore_Mode == mode || |
| 182 | (GrTextureDomain::kClamp_Mode == mode && domain.contains(kFullRect))) { |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 183 | return GrSimpleTextureEffect::Make(texture, std::move(colorSpaceXform), matrix, filterMode); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 184 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 185 | return sk_sp<GrFragmentProcessor>( |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 186 | new GrTextureDomainEffect(texture, std::move(colorSpaceXform), matrix, domain, mode, |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 187 | filterMode)); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 191 | GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 192 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 193 | const SkMatrix& matrix, |
| 194 | const SkRect& domain, |
| 195 | GrTextureDomain::Mode mode, |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 196 | GrTextureParams::FilterMode filterMode) |
| 197 | : GrSingleTextureEffect(texture, std::move(colorSpaceXform), matrix, filterMode) |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 198 | , fTextureDomain(domain, mode) { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 199 | SkASSERT(mode != GrTextureDomain::kRepeat_Mode || |
| 200 | filterMode == GrTextureParams::kNone_FilterMode); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 201 | this->initClassID<GrTextureDomainEffect>(); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 202 | } |
| 203 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 204 | void GrTextureDomainEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 205 | GrProcessorKeyBuilder* b) const { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 206 | b->add32(GrTextureDomain::GLDomain::DomainKey(fTextureDomain)); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 207 | } |
| 208 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 209 | GrGLSLFragmentProcessor* GrTextureDomainEffect::onCreateGLSLInstance() const { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 210 | class GLSLProcessor : public GrGLSLFragmentProcessor { |
| 211 | public: |
| 212 | void emitCode(EmitArgs& args) override { |
| 213 | const GrTextureDomainEffect& tde = args.fFp.cast<GrTextureDomainEffect>(); |
| 214 | const GrTextureDomain& domain = tde.fTextureDomain; |
| 215 | |
| 216 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 217 | SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
| 218 | fGLDomain.sampleTexture(fragBuilder, |
| 219 | args.fUniformHandler, |
| 220 | args.fGLSLCaps, |
| 221 | domain, |
| 222 | args.fOutputColor, |
| 223 | coords2D, |
| 224 | args.fTexSamplers[0], |
| 225 | args.fInputColor); |
| 226 | } |
| 227 | |
| 228 | protected: |
| 229 | void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& fp) override { |
| 230 | const GrTextureDomainEffect& tde = fp.cast<GrTextureDomainEffect>(); |
| 231 | const GrTextureDomain& domain = tde.fTextureDomain; |
Brian Salomon | db4183d | 2016-11-17 12:48:40 -0500 | [diff] [blame] | 232 | fGLDomain.setData(pdman, domain, tde.textureSampler(0).texture()->origin()); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | private: |
| 236 | GrTextureDomain::GLDomain fGLDomain; |
| 237 | |
| 238 | }; |
| 239 | |
| 240 | return new GLSLProcessor; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 241 | } |
| 242 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 243 | bool GrTextureDomainEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 244 | const GrTextureDomainEffect& s = sBase.cast<GrTextureDomainEffect>(); |
Brian Salomon | a7c4c29 | 2016-11-17 12:47:06 -0500 | [diff] [blame] | 245 | return this->fTextureDomain == s.fTextureDomain; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 246 | } |
| 247 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 248 | void GrTextureDomainEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 249 | if (GrTextureDomain::kDecal_Mode == fTextureDomain.mode()) { |
Brian Salomon | db4183d | 2016-11-17 12:48:40 -0500 | [diff] [blame] | 250 | if (GrPixelConfigIsAlphaOnly(this->textureSampler(0).texture()->config())) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 251 | inout->mulByUnknownSingleComponent(); |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 252 | } else { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 253 | inout->mulByUnknownFourComponents(); |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 254 | } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 255 | } else { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 256 | this->updateInvariantOutputForModulation(inout); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | /////////////////////////////////////////////////////////////////////////////// |
| 261 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 262 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTextureDomainEffect); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 263 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 264 | sk_sp<GrFragmentProcessor> GrTextureDomainEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 265 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 266 | GrProcessorUnitTest::kAlphaTextureIdx; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 267 | SkRect domain; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 268 | domain.fLeft = d->fRandom->nextUScalar1(); |
| 269 | domain.fRight = d->fRandom->nextRangeScalar(domain.fLeft, SK_Scalar1); |
| 270 | domain.fTop = d->fRandom->nextUScalar1(); |
| 271 | domain.fBottom = d->fRandom->nextRangeScalar(domain.fTop, SK_Scalar1); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 272 | GrTextureDomain::Mode mode = |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 273 | (GrTextureDomain::Mode) d->fRandom->nextULessThan(GrTextureDomain::kModeCount); |
| 274 | const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom); |
| 275 | bool bilerp = mode != GrTextureDomain::kRepeat_Mode ? d->fRandom->nextBool() : false; |
Brian Osman | e2f732f | 2016-10-03 14:23:50 -0400 | [diff] [blame] | 276 | auto colorSpaceXform = GrTest::TestColorXform(d->fRandom); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 277 | return GrTextureDomainEffect::Make( |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 278 | d->fTextures[texIdx], |
Brian Osman | e2f732f | 2016-10-03 14:23:50 -0400 | [diff] [blame] | 279 | colorSpaceXform, |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 280 | matrix, |
| 281 | domain, |
| 282 | mode, |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 283 | bilerp ? GrTextureParams::kBilerp_FilterMode : GrTextureParams::kNone_FilterMode); |
| 284 | } |
| 285 | |
| 286 | /////////////////////////////////////////////////////////////////////////////// |
| 287 | |
| 288 | sk_sp<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::Make(GrTexture* texture, |
| 289 | const SkIRect& subset, const SkIPoint& deviceSpaceOffset) { |
| 290 | return sk_sp<GrFragmentProcessor>(new GrDeviceSpaceTextureDecalFragmentProcessor( |
| 291 | texture, subset, deviceSpaceOffset)); |
| 292 | } |
| 293 | |
| 294 | GrDeviceSpaceTextureDecalFragmentProcessor::GrDeviceSpaceTextureDecalFragmentProcessor( |
| 295 | GrTexture* texture, const SkIRect& subset, const SkIPoint& deviceSpaceOffset) |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 296 | : fTextureSampler(texture, GrTextureParams::ClampNoFilter()) |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 297 | , fTextureDomain(GrTextureDomain::MakeTexelDomain(texture, subset), |
| 298 | GrTextureDomain::kDecal_Mode) { |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 299 | this->addTextureSampler(&fTextureSampler); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 300 | fDeviceSpaceOffset.fX = deviceSpaceOffset.fX - subset.fLeft; |
| 301 | fDeviceSpaceOffset.fY = deviceSpaceOffset.fY - subset.fTop; |
| 302 | this->initClassID<GrDeviceSpaceTextureDecalFragmentProcessor>(); |
| 303 | this->setWillReadFragmentPosition(); |
| 304 | } |
| 305 | |
| 306 | GrGLSLFragmentProcessor* GrDeviceSpaceTextureDecalFragmentProcessor::onCreateGLSLInstance() const { |
| 307 | class GLSLProcessor : public GrGLSLFragmentProcessor { |
| 308 | public: |
| 309 | void emitCode(EmitArgs& args) override { |
| 310 | const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp = |
| 311 | args.fFp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>(); |
| 312 | const char* scaleAndTranslateName; |
| 313 | fScaleAndTranslateUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, |
| 314 | kVec4f_GrSLType, |
| 315 | kDefault_GrSLPrecision, |
| 316 | "scaleAndTranslate", |
| 317 | &scaleAndTranslateName); |
| 318 | args.fFragBuilder->codeAppendf("vec2 coords = %s.xy * %s.xy + %s.zw;", |
| 319 | args.fFragBuilder->fragmentPosition(), |
| 320 | scaleAndTranslateName, scaleAndTranslateName); |
| 321 | fGLDomain.sampleTexture(args.fFragBuilder, |
| 322 | args.fUniformHandler, |
| 323 | args.fGLSLCaps, |
| 324 | dstdfp.fTextureDomain, |
| 325 | args.fOutputColor, |
| 326 | SkString("coords"), |
| 327 | args.fTexSamplers[0], |
| 328 | args.fInputColor); |
| 329 | } |
| 330 | |
| 331 | protected: |
| 332 | void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& fp) override { |
| 333 | const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp = |
| 334 | fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>(); |
Brian Salomon | db4183d | 2016-11-17 12:48:40 -0500 | [diff] [blame] | 335 | GrTexture* texture = dstdfp.textureSampler(0).texture(); |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 336 | fGLDomain.setData(pdman, dstdfp.fTextureDomain, texture->origin()); |
| 337 | float iw = 1.f / texture->width(); |
| 338 | float ih = 1.f / texture->height(); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 339 | float scaleAndTransData[4] = { |
| 340 | iw, ih, |
| 341 | -dstdfp.fDeviceSpaceOffset.fX * iw, -dstdfp.fDeviceSpaceOffset.fY * ih |
| 342 | }; |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 343 | if (texture->origin() == kBottomLeft_GrSurfaceOrigin) { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 344 | scaleAndTransData[1] = -scaleAndTransData[1]; |
| 345 | scaleAndTransData[3] = 1 - scaleAndTransData[3]; |
| 346 | } |
| 347 | pdman.set4fv(fScaleAndTranslateUni, 1, scaleAndTransData); |
| 348 | } |
| 349 | |
| 350 | private: |
| 351 | GrTextureDomain::GLDomain fGLDomain; |
| 352 | UniformHandle fScaleAndTranslateUni; |
| 353 | }; |
| 354 | |
| 355 | return new GLSLProcessor; |
| 356 | } |
| 357 | |
| 358 | bool GrDeviceSpaceTextureDecalFragmentProcessor::onIsEqual(const GrFragmentProcessor& fp) const { |
| 359 | const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp = |
| 360 | fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>(); |
Brian Salomon | db4183d | 2016-11-17 12:48:40 -0500 | [diff] [blame] | 361 | return dstdfp.fTextureSampler.texture() == fTextureSampler.texture() && |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 362 | dstdfp.fDeviceSpaceOffset == fDeviceSpaceOffset && |
| 363 | dstdfp.fTextureDomain == fTextureDomain; |
| 364 | } |
| 365 | |
| 366 | void GrDeviceSpaceTextureDecalFragmentProcessor::onComputeInvariantOutput( |
| 367 | GrInvariantOutput* inout) const { |
Brian Salomon | db4183d | 2016-11-17 12:48:40 -0500 | [diff] [blame] | 368 | if (GrPixelConfigIsAlphaOnly(this->textureSampler(0).texture()->config())) { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 369 | inout->mulByUnknownSingleComponent(); |
| 370 | } else { |
| 371 | inout->mulByUnknownFourComponents(); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /////////////////////////////////////////////////////////////////////////////// |
| 376 | |
| 377 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDeviceSpaceTextureDecalFragmentProcessor); |
| 378 | |
| 379 | sk_sp<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::TestCreate( |
| 380 | GrProcessorTestData* d) { |
| 381 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx |
| 382 | : GrProcessorUnitTest::kAlphaTextureIdx; |
| 383 | SkIRect subset; |
| 384 | subset.fLeft = d->fRandom->nextULessThan(d->fTextures[texIdx]->width() - 1); |
| 385 | subset.fRight = d->fRandom->nextRangeU(subset.fLeft, d->fTextures[texIdx]->width()); |
| 386 | subset.fTop = d->fRandom->nextULessThan(d->fTextures[texIdx]->height() - 1); |
| 387 | subset.fBottom = d->fRandom->nextRangeU(subset.fTop, d->fTextures[texIdx]->height()); |
| 388 | SkIPoint pt; |
| 389 | pt.fX = d->fRandom->nextULessThan(2048); |
| 390 | pt.fY = d->fRandom->nextULessThan(2048); |
| 391 | return GrDeviceSpaceTextureDecalFragmentProcessor::Make(d->fTextures[texIdx], subset, pt); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 392 | } |