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" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 10 | #include "GrShaderCaps.h" |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 11 | #include "GrSimpleTextureEffect.h" |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 12 | #include "SkFloatingPoint.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 15 | #include "glsl/GrGLSLProgramDataManager.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, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame^] | 48 | const GrShaderCaps* shaderCaps, |
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(); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame^] | 93 | if (!shaderCaps->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 | |
Ethan Nicholas | 1fc83b1 | 2016-11-22 09:31:35 -0500 | [diff] [blame] | 106 | builder->codeAppendf("highp float x = (%s).x;", inCoords.c_str()); |
| 107 | builder->codeAppendf("highp float y = (%s).y;", inCoords.c_str()); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 108 | |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 109 | builder->codeAppendf("x = abs(2.0*(x - %s.x)/(%s.z - %s.x) - 1.0);", |
| 110 | domain, domain, domain); |
| 111 | builder->codeAppendf("y = abs(2.0*(y - %s.y)/(%s.w - %s.y) - 1.0);", |
| 112 | domain, domain, domain); |
| 113 | builder->codeAppend("float blend = step(1.0, max(x, y));"); |
| 114 | builder->codeAppendf("%s = mix(inside, outside, blend);", outColor); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 115 | } else { |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 116 | builder->codeAppend("bvec4 outside;\n"); |
| 117 | builder->codeAppendf("outside.xy = lessThan(%s, %s.xy);", inCoords.c_str(), |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 118 | domain); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 119 | builder->codeAppendf("outside.zw = greaterThan(%s, %s.zw);", 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("%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ", |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 122 | outColor); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 123 | builder->appendTextureLookupAndModulate(inModulateColor, sampler, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 124 | inCoords.c_str()); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 125 | builder->codeAppend(";"); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 126 | } |
| 127 | break; |
| 128 | } |
| 129 | case kRepeat_Mode: { |
| 130 | SkString clampedCoords; |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 131 | clampedCoords.printf("mod(%s - %s.xy, %s.zw - %s.xy) + %s.xy", |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 132 | inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str(), |
| 133 | fDomainName.c_str(), fDomainName.c_str()); |
| 134 | |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 135 | builder->codeAppendf("%s = ", outColor); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 136 | builder->appendTextureLookupAndModulate(inModulateColor, sampler, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 137 | clampedCoords.c_str()); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 138 | builder->codeAppend(";"); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 139 | break; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 144 | void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 145 | const GrTextureDomain& textureDomain, |
| 146 | GrSurfaceOrigin textureOrigin) { |
| 147 | SkASSERT(textureDomain.mode() == fMode); |
| 148 | if (kIgnore_Mode != textureDomain.mode()) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 149 | float values[kPrevDomainCount] = { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 150 | SkScalarToFloat(textureDomain.domain().left()), |
| 151 | SkScalarToFloat(textureDomain.domain().top()), |
| 152 | SkScalarToFloat(textureDomain.domain().right()), |
| 153 | SkScalarToFloat(textureDomain.domain().bottom()) |
| 154 | }; |
| 155 | // vertical flip if necessary |
| 156 | if (kBottomLeft_GrSurfaceOrigin == textureOrigin) { |
| 157 | values[1] = 1.0f - values[1]; |
| 158 | values[3] = 1.0f - values[3]; |
| 159 | // The top and bottom were just flipped, so correct the ordering |
| 160 | // of elements so that values = (l, t, r, b). |
| 161 | SkTSwap(values[1], values[3]); |
| 162 | } |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 163 | if (0 != memcmp(values, fPrevDomain, kPrevDomainCount * sizeof(float))) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 164 | pdman.set4fv(fDomainUni, 1, values); |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 165 | memcpy(fPrevDomain, values, kPrevDomainCount * sizeof(float)); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 170 | /////////////////////////////////////////////////////////////////////////////// |
| 171 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 172 | sk_sp<GrFragmentProcessor> GrTextureDomainEffect::Make(GrTexture* texture, |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 173 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 174 | const SkMatrix& matrix, |
| 175 | const SkRect& domain, |
| 176 | GrTextureDomain::Mode mode, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 177 | GrSamplerParams::FilterMode filterMode) { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 178 | static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1}; |
| 179 | if (GrTextureDomain::kIgnore_Mode == mode || |
| 180 | (GrTextureDomain::kClamp_Mode == mode && domain.contains(kFullRect))) { |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 181 | return GrSimpleTextureEffect::Make(texture, std::move(colorSpaceXform), matrix, filterMode); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 182 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 183 | return sk_sp<GrFragmentProcessor>( |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 184 | new GrTextureDomainEffect(texture, std::move(colorSpaceXform), matrix, domain, mode, |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 185 | filterMode)); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 189 | GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 190 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 191 | const SkMatrix& matrix, |
| 192 | const SkRect& domain, |
| 193 | GrTextureDomain::Mode mode, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 194 | GrSamplerParams::FilterMode filterMode) |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 195 | : GrSingleTextureEffect(texture, std::move(colorSpaceXform), matrix, filterMode) |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 196 | , fTextureDomain(domain, mode) { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 197 | SkASSERT(mode != GrTextureDomain::kRepeat_Mode || |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 198 | filterMode == GrSamplerParams::kNone_FilterMode); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 199 | this->initClassID<GrTextureDomainEffect>(); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 202 | void GrTextureDomainEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 203 | GrProcessorKeyBuilder* b) const { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 204 | b->add32(GrTextureDomain::GLDomain::DomainKey(fTextureDomain)); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 205 | } |
| 206 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 207 | GrGLSLFragmentProcessor* GrTextureDomainEffect::onCreateGLSLInstance() const { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 208 | class GLSLProcessor : public GrGLSLFragmentProcessor { |
| 209 | public: |
| 210 | void emitCode(EmitArgs& args) override { |
| 211 | const GrTextureDomainEffect& tde = args.fFp.cast<GrTextureDomainEffect>(); |
| 212 | const GrTextureDomain& domain = tde.fTextureDomain; |
| 213 | |
| 214 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 215 | SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
| 216 | fGLDomain.sampleTexture(fragBuilder, |
| 217 | args.fUniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame^] | 218 | args.fShaderCaps, |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 219 | domain, |
| 220 | args.fOutputColor, |
| 221 | coords2D, |
| 222 | args.fTexSamplers[0], |
| 223 | args.fInputColor); |
| 224 | } |
| 225 | |
| 226 | protected: |
| 227 | void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& fp) override { |
| 228 | const GrTextureDomainEffect& tde = fp.cast<GrTextureDomainEffect>(); |
| 229 | const GrTextureDomain& domain = tde.fTextureDomain; |
Brian Salomon | db4183d | 2016-11-17 12:48:40 -0500 | [diff] [blame] | 230 | fGLDomain.setData(pdman, domain, tde.textureSampler(0).texture()->origin()); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | private: |
| 234 | GrTextureDomain::GLDomain fGLDomain; |
| 235 | |
| 236 | }; |
| 237 | |
| 238 | return new GLSLProcessor; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 239 | } |
| 240 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 241 | bool GrTextureDomainEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 242 | const GrTextureDomainEffect& s = sBase.cast<GrTextureDomainEffect>(); |
Brian Salomon | a7c4c29 | 2016-11-17 12:47:06 -0500 | [diff] [blame] | 243 | return this->fTextureDomain == s.fTextureDomain; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 244 | } |
| 245 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 246 | void GrTextureDomainEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 247 | if (GrTextureDomain::kDecal_Mode == fTextureDomain.mode()) { |
Brian Salomon | db4183d | 2016-11-17 12:48:40 -0500 | [diff] [blame] | 248 | if (GrPixelConfigIsAlphaOnly(this->textureSampler(0).texture()->config())) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 249 | inout->mulByUnknownSingleComponent(); |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 250 | } else { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 251 | inout->mulByUnknownFourComponents(); |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 252 | } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 253 | } else { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 254 | this->updateInvariantOutputForModulation(inout); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | /////////////////////////////////////////////////////////////////////////////// |
| 259 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 260 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTextureDomainEffect); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 261 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 262 | sk_sp<GrFragmentProcessor> GrTextureDomainEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 263 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 264 | GrProcessorUnitTest::kAlphaTextureIdx; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 265 | SkRect domain; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 266 | domain.fLeft = d->fRandom->nextUScalar1(); |
| 267 | domain.fRight = d->fRandom->nextRangeScalar(domain.fLeft, SK_Scalar1); |
| 268 | domain.fTop = d->fRandom->nextUScalar1(); |
| 269 | domain.fBottom = d->fRandom->nextRangeScalar(domain.fTop, SK_Scalar1); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 270 | GrTextureDomain::Mode mode = |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 271 | (GrTextureDomain::Mode) d->fRandom->nextULessThan(GrTextureDomain::kModeCount); |
| 272 | const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom); |
| 273 | bool bilerp = mode != GrTextureDomain::kRepeat_Mode ? d->fRandom->nextBool() : false; |
Brian Osman | e2f732f | 2016-10-03 14:23:50 -0400 | [diff] [blame] | 274 | auto colorSpaceXform = GrTest::TestColorXform(d->fRandom); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 275 | return GrTextureDomainEffect::Make( |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 276 | d->fTextures[texIdx], |
Brian Osman | e2f732f | 2016-10-03 14:23:50 -0400 | [diff] [blame] | 277 | colorSpaceXform, |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 278 | matrix, |
| 279 | domain, |
| 280 | mode, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 281 | bilerp ? GrSamplerParams::kBilerp_FilterMode : GrSamplerParams::kNone_FilterMode); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /////////////////////////////////////////////////////////////////////////////// |
| 285 | |
| 286 | sk_sp<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::Make(GrTexture* texture, |
| 287 | const SkIRect& subset, const SkIPoint& deviceSpaceOffset) { |
| 288 | return sk_sp<GrFragmentProcessor>(new GrDeviceSpaceTextureDecalFragmentProcessor( |
| 289 | texture, subset, deviceSpaceOffset)); |
| 290 | } |
| 291 | |
| 292 | GrDeviceSpaceTextureDecalFragmentProcessor::GrDeviceSpaceTextureDecalFragmentProcessor( |
| 293 | GrTexture* texture, const SkIRect& subset, const SkIPoint& deviceSpaceOffset) |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 294 | : fTextureSampler(texture, GrSamplerParams::ClampNoFilter()) |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 295 | , fTextureDomain(GrTextureDomain::MakeTexelDomain(texture, subset), |
| 296 | GrTextureDomain::kDecal_Mode) { |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 297 | this->addTextureSampler(&fTextureSampler); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 298 | fDeviceSpaceOffset.fX = deviceSpaceOffset.fX - subset.fLeft; |
| 299 | fDeviceSpaceOffset.fY = deviceSpaceOffset.fY - subset.fTop; |
| 300 | this->initClassID<GrDeviceSpaceTextureDecalFragmentProcessor>(); |
| 301 | this->setWillReadFragmentPosition(); |
| 302 | } |
| 303 | |
| 304 | GrGLSLFragmentProcessor* GrDeviceSpaceTextureDecalFragmentProcessor::onCreateGLSLInstance() const { |
| 305 | class GLSLProcessor : public GrGLSLFragmentProcessor { |
| 306 | public: |
| 307 | void emitCode(EmitArgs& args) override { |
| 308 | const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp = |
| 309 | args.fFp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>(); |
| 310 | const char* scaleAndTranslateName; |
| 311 | fScaleAndTranslateUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, |
| 312 | kVec4f_GrSLType, |
| 313 | kDefault_GrSLPrecision, |
| 314 | "scaleAndTranslate", |
| 315 | &scaleAndTranslateName); |
| 316 | args.fFragBuilder->codeAppendf("vec2 coords = %s.xy * %s.xy + %s.zw;", |
| 317 | args.fFragBuilder->fragmentPosition(), |
| 318 | scaleAndTranslateName, scaleAndTranslateName); |
| 319 | fGLDomain.sampleTexture(args.fFragBuilder, |
| 320 | args.fUniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame^] | 321 | args.fShaderCaps, |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 322 | dstdfp.fTextureDomain, |
| 323 | args.fOutputColor, |
| 324 | SkString("coords"), |
| 325 | args.fTexSamplers[0], |
| 326 | args.fInputColor); |
| 327 | } |
| 328 | |
| 329 | protected: |
| 330 | void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& fp) override { |
| 331 | const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp = |
| 332 | fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>(); |
Brian Salomon | db4183d | 2016-11-17 12:48:40 -0500 | [diff] [blame] | 333 | GrTexture* texture = dstdfp.textureSampler(0).texture(); |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 334 | fGLDomain.setData(pdman, dstdfp.fTextureDomain, texture->origin()); |
| 335 | float iw = 1.f / texture->width(); |
| 336 | float ih = 1.f / texture->height(); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 337 | float scaleAndTransData[4] = { |
| 338 | iw, ih, |
| 339 | -dstdfp.fDeviceSpaceOffset.fX * iw, -dstdfp.fDeviceSpaceOffset.fY * ih |
| 340 | }; |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 341 | if (texture->origin() == kBottomLeft_GrSurfaceOrigin) { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 342 | scaleAndTransData[1] = -scaleAndTransData[1]; |
| 343 | scaleAndTransData[3] = 1 - scaleAndTransData[3]; |
| 344 | } |
| 345 | pdman.set4fv(fScaleAndTranslateUni, 1, scaleAndTransData); |
| 346 | } |
| 347 | |
| 348 | private: |
| 349 | GrTextureDomain::GLDomain fGLDomain; |
| 350 | UniformHandle fScaleAndTranslateUni; |
| 351 | }; |
| 352 | |
| 353 | return new GLSLProcessor; |
| 354 | } |
| 355 | |
| 356 | bool GrDeviceSpaceTextureDecalFragmentProcessor::onIsEqual(const GrFragmentProcessor& fp) const { |
| 357 | const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp = |
| 358 | fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>(); |
Brian Salomon | db4183d | 2016-11-17 12:48:40 -0500 | [diff] [blame] | 359 | return dstdfp.fTextureSampler.texture() == fTextureSampler.texture() && |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 360 | dstdfp.fDeviceSpaceOffset == fDeviceSpaceOffset && |
| 361 | dstdfp.fTextureDomain == fTextureDomain; |
| 362 | } |
| 363 | |
| 364 | void GrDeviceSpaceTextureDecalFragmentProcessor::onComputeInvariantOutput( |
| 365 | GrInvariantOutput* inout) const { |
Brian Salomon | db4183d | 2016-11-17 12:48:40 -0500 | [diff] [blame] | 366 | if (GrPixelConfigIsAlphaOnly(this->textureSampler(0).texture()->config())) { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 367 | inout->mulByUnknownSingleComponent(); |
| 368 | } else { |
| 369 | inout->mulByUnknownFourComponents(); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /////////////////////////////////////////////////////////////////////////////// |
| 374 | |
| 375 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDeviceSpaceTextureDecalFragmentProcessor); |
| 376 | |
| 377 | sk_sp<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::TestCreate( |
| 378 | GrProcessorTestData* d) { |
| 379 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx |
| 380 | : GrProcessorUnitTest::kAlphaTextureIdx; |
| 381 | SkIRect subset; |
| 382 | subset.fLeft = d->fRandom->nextULessThan(d->fTextures[texIdx]->width() - 1); |
| 383 | subset.fRight = d->fRandom->nextRangeU(subset.fLeft, d->fTextures[texIdx]->width()); |
| 384 | subset.fTop = d->fRandom->nextULessThan(d->fTextures[texIdx]->height() - 1); |
| 385 | subset.fBottom = d->fRandom->nextRangeU(subset.fTop, d->fTextures[texIdx]->height()); |
| 386 | SkIPoint pt; |
| 387 | pt.fX = d->fRandom->nextULessThan(2048); |
| 388 | pt.fY = d->fRandom->nextULessThan(2048); |
| 389 | return GrDeviceSpaceTextureDecalFragmentProcessor::Make(d->fTextures[texIdx], subset, pt); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 390 | } |