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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/effects/GrTextureDomain.h" |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/gpu/GrTexture.h" |
| 11 | #include "include/private/SkFloatingPoint.h" |
| 12 | #include "src/gpu/GrProxyProvider.h" |
| 13 | #include "src/gpu/GrShaderCaps.h" |
| 14 | #include "src/gpu/GrSurfaceProxyPriv.h" |
Brian Salomon | b8f098d | 2020-01-07 11:15:44 -0500 | [diff] [blame] | 15 | #include "src/gpu/effects/GrTextureEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 17 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 18 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
| 19 | #include "src/gpu/glsl/GrGLSLShaderBuilder.h" |
| 20 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 21 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 22 | #include <utility> |
| 23 | |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 24 | GrTextureDomain::GrTextureDomain(GrSurfaceProxy* proxy, const SkRect& domain, Mode modeX, |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 25 | Mode modeY, int index) |
| 26 | : fModeX(modeX) |
| 27 | , fModeY(modeY) |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 28 | , fIndex(index) { |
| 29 | |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 30 | if (!proxy) { |
| 31 | SkASSERT(modeX == kIgnore_Mode && modeY == kIgnore_Mode); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 32 | return; |
| 33 | } |
| 34 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 35 | const SkRect kFullRect = proxy->getBoundsRect(); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 36 | |
| 37 | // We don't currently handle domains that are empty or don't intersect the texture. |
| 38 | // It is OK if the domain rect is a line or point, but it should not be inverted. We do not |
| 39 | // handle rects that do not intersect the [0..1]x[0..1] rect. |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 40 | SkASSERT(domain.isSorted()); |
Brian Osman | aba642c | 2020-02-06 12:52:25 -0500 | [diff] [blame^] | 41 | fDomain.fLeft = SkTPin(domain.fLeft, 0.0f, kFullRect.fRight); |
| 42 | fDomain.fRight = SkTPin(domain.fRight, fDomain.fLeft, kFullRect.fRight); |
| 43 | fDomain.fTop = SkTPin(domain.fTop, 0.0f, kFullRect.fBottom); |
| 44 | fDomain.fBottom = SkTPin(domain.fBottom, fDomain.fTop, kFullRect.fBottom); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 45 | SkASSERT(fDomain.fLeft <= fDomain.fRight); |
| 46 | SkASSERT(fDomain.fTop <= fDomain.fBottom); |
| 47 | } |
| 48 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 49 | GrTextureDomain::GrTextureDomain(const SkRect& domain, Mode modeX, Mode modeY, int index) |
| 50 | : fDomain(domain), fModeX(modeX), fModeY(modeY), fIndex(index) { |
| 51 | // We don't currently handle domains that are empty or don't intersect the texture. |
| 52 | // It is OK if the domain rect is a line or point, but it should not be inverted. |
| 53 | SkASSERT(domain.isSorted()); |
| 54 | } |
| 55 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 56 | ////////////////////////////////////////////////////////////////////////////// |
| 57 | |
Brian Salomon | 7caa137 | 2019-12-09 10:40:56 -0500 | [diff] [blame] | 58 | static void append_wrap(GrGLSLShaderBuilder* builder, GrTextureDomain::Mode mode, |
| 59 | const char* inCoord, const char* domainStart, const char* domainEnd, |
Brian Salomon | 095d246 | 2019-12-09 15:22:33 -0500 | [diff] [blame] | 60 | bool is2D, const char* out) { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 61 | switch(mode) { |
| 62 | case GrTextureDomain::kIgnore_Mode: |
Brian Salomon | 7caa137 | 2019-12-09 10:40:56 -0500 | [diff] [blame] | 63 | builder->codeAppendf("%s = %s;\n", out, inCoord); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 64 | break; |
| 65 | case GrTextureDomain::kDecal_Mode: |
| 66 | // The lookup coordinate to use for decal will be clamped just like kClamp_Mode, |
| 67 | // it's just that the post-processing will be different, so fall through |
| 68 | case GrTextureDomain::kClamp_Mode: |
Brian Salomon | 7caa137 | 2019-12-09 10:40:56 -0500 | [diff] [blame] | 69 | builder->codeAppendf("%s = clamp(%s, %s, %s);", out, inCoord, domainStart, domainEnd); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 70 | break; |
| 71 | case GrTextureDomain::kRepeat_Mode: |
Brian Salomon | 7caa137 | 2019-12-09 10:40:56 -0500 | [diff] [blame] | 72 | builder->codeAppendf("%s = mod(%s - %s, %s - %s) + %s;", out, inCoord, domainStart, |
| 73 | domainEnd, domainStart, domainStart); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 74 | break; |
Brian Salomon | 7caa137 | 2019-12-09 10:40:56 -0500 | [diff] [blame] | 75 | case GrTextureDomain::kMirrorRepeat_Mode: { |
Brian Salomon | 095d246 | 2019-12-09 15:22:33 -0500 | [diff] [blame] | 76 | const char* type = is2D ? "float2" : "float"; |
Brian Salomon | 7caa137 | 2019-12-09 10:40:56 -0500 | [diff] [blame] | 77 | builder->codeAppend("{"); |
Brian Salomon | 095d246 | 2019-12-09 15:22:33 -0500 | [diff] [blame] | 78 | builder->codeAppendf("%s w = %s - %s;", type, domainEnd, domainStart); |
| 79 | builder->codeAppendf("%s w2 = 2 * w;", type); |
| 80 | builder->codeAppendf("%s m = mod(%s - %s, w2);", type, inCoord, domainStart); |
Brian Salomon | 7caa137 | 2019-12-09 10:40:56 -0500 | [diff] [blame] | 81 | builder->codeAppendf("%s = mix(m, w2 - m, step(w, m)) + %s;", out, domainStart); |
| 82 | builder->codeAppend("}"); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 83 | break; |
Brian Salomon | 7caa137 | 2019-12-09 10:40:56 -0500 | [diff] [blame] | 84 | } |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 85 | } |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 86 | } |
| 87 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 88 | void GrTextureDomain::GLDomain::sampleProcessor(const GrTextureDomain& textureDomain, |
| 89 | const char* inColor, |
| 90 | const char* outColor, |
| 91 | const SkString& inCoords, |
| 92 | GrGLSLFragmentProcessor* parent, |
| 93 | GrGLSLFragmentProcessor::EmitArgs& args, |
| 94 | int childIndex) { |
| 95 | auto appendProcessorSample = [parent, &args, childIndex, inColor](const char* coord) { |
Brian Osman | 978693c | 2020-01-24 14:52:10 -0500 | [diff] [blame] | 96 | return parent->invokeChild(childIndex, inColor, args, coord); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 97 | }; |
| 98 | this->sample(args.fFragBuilder, args.fUniformHandler, textureDomain, outColor, inCoords, |
| 99 | appendProcessorSample); |
| 100 | } |
| 101 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 102 | void GrTextureDomain::GLDomain::sampleTexture(GrGLSLShaderBuilder* builder, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 103 | GrGLSLUniformHandler* uniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 104 | const GrShaderCaps* shaderCaps, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 105 | const GrTextureDomain& textureDomain, |
| 106 | const char* outColor, |
| 107 | const SkString& inCoords, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 108 | GrGLSLFragmentProcessor::SamplerHandle sampler, |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 109 | const char* inModulateColor) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 110 | auto appendTextureSample = [&sampler, inModulateColor, builder](const char* coord) { |
| 111 | builder->codeAppend("half4 textureColor = "); |
Brian Salomon | 87e9ddb | 2019-12-19 14:50:22 -0500 | [diff] [blame] | 112 | builder->appendTextureLookupAndBlend(inModulateColor, SkBlendMode::kModulate, sampler, |
| 113 | coord); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 114 | builder->codeAppend(";"); |
| 115 | return SkString("textureColor"); |
| 116 | }; |
| 117 | this->sample(builder, uniformHandler, textureDomain, outColor, inCoords, appendTextureSample); |
| 118 | } |
| 119 | |
| 120 | void GrTextureDomain::GLDomain::sample(GrGLSLShaderBuilder* builder, |
| 121 | GrGLSLUniformHandler* uniformHandler, |
| 122 | const GrTextureDomain& textureDomain, |
| 123 | const char* outColor, |
| 124 | const SkString& inCoords, |
| 125 | const std::function<AppendSample>& appendSample) { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 126 | SkASSERT(!fHasMode || (textureDomain.modeX() == fModeX && textureDomain.modeY() == fModeY)); |
| 127 | SkDEBUGCODE(fModeX = textureDomain.modeX();) |
| 128 | SkDEBUGCODE(fModeY = textureDomain.modeY();) |
Hans Wennborg | c63ec5c | 2017-12-08 18:56:23 -0800 | [diff] [blame] | 129 | SkDEBUGCODE(fHasMode = true;) |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 130 | |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 131 | if ((textureDomain.modeX() != kIgnore_Mode || textureDomain.modeY() != kIgnore_Mode) && |
| 132 | !fDomainUni.isValid()) { |
| 133 | // Must include the domain uniform since at least one axis uses it |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 134 | const char* name; |
| 135 | SkString uniName("TexDom"); |
| 136 | if (textureDomain.fIndex >= 0) { |
| 137 | uniName.appendS32(textureDomain.fIndex); |
| 138 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 139 | fDomainUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 140 | uniName.c_str(), &name); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 141 | fDomainName = name; |
| 142 | } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 143 | |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 144 | bool decalX = textureDomain.modeX() == kDecal_Mode; |
| 145 | bool decalY = textureDomain.modeY() == kDecal_Mode; |
| 146 | if ((decalX || decalY) && !fDecalUni.isValid()) { |
| 147 | const char* name; |
| 148 | SkString uniName("DecalParams"); |
| 149 | if (textureDomain.fIndex >= 0) { |
| 150 | uniName.appendS32(textureDomain.fIndex); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 151 | } |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 152 | // Half3 since this will hold texture width, height, and then a step function control param |
| 153 | fDecalUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType, |
| 154 | uniName.c_str(), &name); |
| 155 | fDecalName = name; |
| 156 | } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 157 | |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 158 | // Add a block so that we can declare variables |
| 159 | GrGLSLShaderBuilder::ShaderBlock block(builder); |
| 160 | // Always use a local variable for the input coordinates; often callers pass in an expression |
| 161 | // and we want to cache it across all of its references in the code below |
| 162 | builder->codeAppendf("float2 origCoord = %s;", inCoords.c_str()); |
Brian Salomon | 7caa137 | 2019-12-09 10:40:56 -0500 | [diff] [blame] | 163 | builder->codeAppend("float2 clampedCoord;"); |
| 164 | SkString start; |
| 165 | SkString end; |
Brian Salomon | 095d246 | 2019-12-09 15:22:33 -0500 | [diff] [blame] | 166 | bool is2D = textureDomain.modeX() == textureDomain.modeY(); |
| 167 | if (is2D) { |
| 168 | // Doing the domain setup using vectors seems to avoid shader compilation issues on |
| 169 | // Chromecast, possibly due to reducing shader length. |
| 170 | start.printf("%s.xy", fDomainName.c_str()); |
| 171 | end.printf("%s.zw", fDomainName.c_str()); |
| 172 | append_wrap(builder, textureDomain.modeX(), "origCoord", start.c_str(), end.c_str(), |
| 173 | true, "clampedCoord"); |
| 174 | } else { |
| 175 | // Apply x mode to the x coordinate using the left and right edges of the domain rect |
| 176 | // (stored as the x and z components of the domain uniform). |
| 177 | start.printf("%s.x", fDomainName.c_str()); |
| 178 | end.printf("%s.z", fDomainName.c_str()); |
| 179 | append_wrap(builder, textureDomain.modeX(), "origCoord.x", start.c_str(), end.c_str(), |
| 180 | false, "clampedCoord.x"); |
| 181 | // Repeat the same logic for y. |
| 182 | start.printf("%s.y", fDomainName.c_str()); |
| 183 | end.printf("%s.w", fDomainName.c_str()); |
| 184 | append_wrap(builder, textureDomain.modeY(), "origCoord.y", start.c_str(), end.c_str(), |
| 185 | false, "clampedCoord.y"); |
| 186 | } |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 187 | // Sample 'appendSample' at the clamped coordinate location. |
| 188 | SkString color = appendSample("clampedCoord"); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 189 | |
| 190 | // Apply decal mode's transparency interpolation if needed |
| 191 | if (decalX || decalY) { |
| 192 | // The decal err is the max absoluate value between the clamped coordinate and the original |
| 193 | // pixel coordinate. This will then be clamped to 1.f if it's greater than the control |
| 194 | // parameter, which simulates kNearest and kBilerp behavior depending on if it's 0 or 1. |
| 195 | if (decalX && decalY) { |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 196 | builder->codeAppendf("half err = max(half(abs(clampedCoord.x - origCoord.x) * %s.x), " |
| 197 | "half(abs(clampedCoord.y - origCoord.y) * %s.y));", |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 198 | fDecalName.c_str(), fDecalName.c_str()); |
| 199 | } else if (decalX) { |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 200 | builder->codeAppendf("half err = half(abs(clampedCoord.x - origCoord.x) * %s.x);", |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 201 | fDecalName.c_str()); |
| 202 | } else { |
| 203 | SkASSERT(decalY); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 204 | builder->codeAppendf("half err = half(abs(clampedCoord.y - origCoord.y) * %s.y);", |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 205 | fDecalName.c_str()); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 206 | } |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 207 | |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 208 | // Apply a transform to the error rate, which let's us simulate nearest or bilerp filtering |
| 209 | // in the same shader. When the texture is nearest filtered, fSizeName.z is set to 1/2 so |
| 210 | // this becomes a step function centered at .5 away from the clamped coordinate (but the |
| 211 | // domain for decal is inset by .5 so the edge lines up properly). When bilerp, fSizeName.z |
| 212 | // is set to 1 and it becomes a simple linear blend between texture and transparent. |
| 213 | builder->codeAppendf("if (err > %s.z) { err = 1.0; } else if (%s.z < 1) { err = 0.0; }", |
| 214 | fDecalName.c_str(), fDecalName.c_str()); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 215 | builder->codeAppendf("%s = mix(%s, half4(0, 0, 0, 0), err);", outColor, color.c_str()); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 216 | } else { |
| 217 | // A simple look up |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 218 | builder->codeAppendf("%s = %s;", outColor, color.c_str()); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 222 | void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 223 | const GrTextureDomain& textureDomain, |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 224 | const GrSurfaceProxyView& view, |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 225 | GrSamplerState state) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 226 | // We want a hard transition from texture content to trans-black in nearest mode. |
| 227 | bool filterDecal = state.filter() != GrSamplerState::Filter::kNearest; |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 228 | this->setData(pdman, textureDomain, view.proxy(), view.origin(), filterDecal); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 229 | } |
Michael Ludwig | 170de01 | 2019-11-15 21:55:18 +0000 | [diff] [blame] | 230 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 231 | void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman, |
| 232 | const GrTextureDomain& textureDomain, |
| 233 | bool filterIfDecal) { |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 234 | // The origin we pass here doesn't matter |
| 235 | this->setData(pdman, textureDomain, nullptr, kTopLeft_GrSurfaceOrigin, filterIfDecal); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman, |
| 239 | const GrTextureDomain& textureDomain, |
| 240 | const GrSurfaceProxy* proxy, |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 241 | GrSurfaceOrigin origin, |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 242 | bool filterIfDecal) { |
| 243 | SkASSERT(fHasMode && textureDomain.modeX() == fModeX && textureDomain.modeY() == fModeY); |
| 244 | if (kIgnore_Mode == textureDomain.modeX() && kIgnore_Mode == textureDomain.modeY()) { |
| 245 | return; |
| 246 | } |
| 247 | // If the texture is using nearest filtering, then the decal filter weight should step from |
| 248 | // 0 (texture) to 1 (transparent) one half pixel away from the domain. When doing any other |
| 249 | // form of filtering, the weight should be 1.0 so that it smoothly interpolates between the |
| 250 | // texture and transparent. |
| 251 | // Start off assuming we're in pixel units and later adjust if we have to deal with normalized |
| 252 | // texture coords. |
| 253 | float decalFilterWeights[3] = {1.f, 1.f, filterIfDecal ? 1.f : 0.5f}; |
| 254 | bool sendDecalData = textureDomain.modeX() == kDecal_Mode || |
| 255 | textureDomain.modeY() == kDecal_Mode; |
| 256 | float tempDomainValues[4]; |
| 257 | const float* values; |
| 258 | if (proxy) { |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 259 | SkScalar wInv, hInv, h; |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 260 | GrTexture* tex = proxy->peekTexture(); |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 261 | if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) { |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 262 | wInv = hInv = 1.f; |
| 263 | h = tex->height(); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 264 | // Don't do any scaling by texture size for decal filter rate, it's already in |
| 265 | // pixels |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 266 | } else { |
| 267 | wInv = SK_Scalar1 / tex->width(); |
| 268 | hInv = SK_Scalar1 / tex->height(); |
| 269 | h = 1.f; |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 270 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 271 | // Account for texture coord normalization in decal filter weights. |
| 272 | decalFilterWeights[0] = tex->width(); |
| 273 | decalFilterWeights[1] = tex->height(); |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 274 | } |
Robert Phillips | e98234f | 2017-01-09 14:23:59 -0500 | [diff] [blame] | 275 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 276 | tempDomainValues[0] = SkScalarToFloat(textureDomain.domain().fLeft * wInv); |
| 277 | tempDomainValues[1] = SkScalarToFloat(textureDomain.domain().fTop * hInv); |
| 278 | tempDomainValues[2] = SkScalarToFloat(textureDomain.domain().fRight * wInv); |
| 279 | tempDomainValues[3] = SkScalarToFloat(textureDomain.domain().fBottom * hInv); |
Robert Phillips | e98234f | 2017-01-09 14:23:59 -0500 | [diff] [blame] | 280 | |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 281 | if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 282 | SkASSERT(tempDomainValues[0] >= 0.0f && tempDomainValues[0] <= proxy->width()); |
| 283 | SkASSERT(tempDomainValues[1] >= 0.0f && tempDomainValues[1] <= proxy->height()); |
| 284 | SkASSERT(tempDomainValues[2] >= 0.0f && tempDomainValues[2] <= proxy->width()); |
| 285 | SkASSERT(tempDomainValues[3] >= 0.0f && tempDomainValues[3] <= proxy->height()); |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 286 | } else { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 287 | SkASSERT(tempDomainValues[0] >= 0.0f && tempDomainValues[0] <= 1.0f); |
| 288 | SkASSERT(tempDomainValues[1] >= 0.0f && tempDomainValues[1] <= 1.0f); |
| 289 | SkASSERT(tempDomainValues[2] >= 0.0f && tempDomainValues[2] <= 1.0f); |
| 290 | SkASSERT(tempDomainValues[3] >= 0.0f && tempDomainValues[3] <= 1.0f); |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 291 | } |
Robert Phillips | e98234f | 2017-01-09 14:23:59 -0500 | [diff] [blame] | 292 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 293 | // vertical flip if necessary |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 294 | if (kBottomLeft_GrSurfaceOrigin == origin) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 295 | tempDomainValues[1] = h - tempDomainValues[1]; |
| 296 | tempDomainValues[3] = h - tempDomainValues[3]; |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 297 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 298 | // The top and bottom were just flipped, so correct the ordering |
| 299 | // of elements so that values = (l, t, r, b). |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 300 | using std::swap; |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 301 | swap(tempDomainValues[1], tempDomainValues[3]); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 302 | } |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 303 | values = tempDomainValues; |
| 304 | } else { |
| 305 | values = textureDomain.domain().asScalars(); |
| 306 | } |
| 307 | if (!std::equal(values, values + 4, fPrevDomain)) { |
| 308 | pdman.set4fv(fDomainUni, 1, values); |
| 309 | std::copy_n(values, 4, fPrevDomain); |
| 310 | } |
| 311 | if (sendDecalData && |
| 312 | !std::equal(decalFilterWeights, decalFilterWeights + 3, fPrevDeclFilterWeights)) { |
| 313 | pdman.set3fv(fDecalUni, 1, decalFilterWeights); |
| 314 | std::copy_n(decalFilterWeights, 3, fPrevDeclFilterWeights); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 318 | /////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 319 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 320 | std::unique_ptr<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::Make( |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 321 | sk_sp<GrSurfaceProxy> proxy, const SkIRect& subset, const SkIPoint& deviceSpaceOffset) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 322 | return std::unique_ptr<GrFragmentProcessor>(new GrDeviceSpaceTextureDecalFragmentProcessor( |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 323 | std::move(proxy), subset, deviceSpaceOffset)); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | GrDeviceSpaceTextureDecalFragmentProcessor::GrDeviceSpaceTextureDecalFragmentProcessor( |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 327 | sk_sp<GrSurfaceProxy> proxy, const SkIRect& subset, const SkIPoint& deviceSpaceOffset) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 328 | : INHERITED(kGrDeviceSpaceTextureDecalFragmentProcessor_ClassID, |
| 329 | kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 330 | , fTextureSampler(proxy, GrSamplerState::Filter::kNearest) |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 331 | , fTextureDomain(proxy.get(), |
| 332 | GrTextureDomain::MakeTexelDomain(subset, GrTextureDomain::kDecal_Mode), |
| 333 | GrTextureDomain::kDecal_Mode, GrTextureDomain::kDecal_Mode) { |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 334 | this->setTextureSamplerCnt(1); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 335 | fDeviceSpaceOffset.fX = deviceSpaceOffset.fX - subset.fLeft; |
| 336 | fDeviceSpaceOffset.fY = deviceSpaceOffset.fY - subset.fTop; |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 337 | } |
| 338 | |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 339 | GrDeviceSpaceTextureDecalFragmentProcessor::GrDeviceSpaceTextureDecalFragmentProcessor( |
| 340 | const GrDeviceSpaceTextureDecalFragmentProcessor& that) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 341 | : INHERITED(kGrDeviceSpaceTextureDecalFragmentProcessor_ClassID, |
| 342 | kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 343 | , fTextureSampler(that.fTextureSampler) |
| 344 | , fTextureDomain(that.fTextureDomain) |
| 345 | , fDeviceSpaceOffset(that.fDeviceSpaceOffset) { |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 346 | this->setTextureSamplerCnt(1); |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 347 | } |
| 348 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 349 | std::unique_ptr<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::clone() const { |
| 350 | return std::unique_ptr<GrFragmentProcessor>( |
| 351 | new GrDeviceSpaceTextureDecalFragmentProcessor(*this)); |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 352 | } |
| 353 | |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 354 | GrGLSLFragmentProcessor* GrDeviceSpaceTextureDecalFragmentProcessor::onCreateGLSLInstance() const { |
| 355 | class GLSLProcessor : public GrGLSLFragmentProcessor { |
| 356 | public: |
| 357 | void emitCode(EmitArgs& args) override { |
| 358 | const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp = |
| 359 | args.fFp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>(); |
| 360 | const char* scaleAndTranslateName; |
| 361 | fScaleAndTranslateUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 362 | kHalf4_GrSLType, |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 363 | "scaleAndTranslate", |
| 364 | &scaleAndTranslateName); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 365 | args.fFragBuilder->codeAppendf("half2 coords = half2(sk_FragCoord.xy * %s.xy + %s.zw);", |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 366 | scaleAndTranslateName, scaleAndTranslateName); |
| 367 | fGLDomain.sampleTexture(args.fFragBuilder, |
| 368 | args.fUniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 369 | args.fShaderCaps, |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 370 | dstdfp.fTextureDomain, |
| 371 | args.fOutputColor, |
| 372 | SkString("coords"), |
| 373 | args.fTexSamplers[0], |
| 374 | args.fInputColor); |
| 375 | } |
| 376 | |
| 377 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 378 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 379 | const GrFragmentProcessor& fp) override { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 380 | const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp = |
| 381 | fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>(); |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 382 | const auto& view = dstdfp.textureSampler(0).view(); |
| 383 | SkISize textureDims = view.proxy()->backingStoreDimensions(); |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 384 | |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 385 | fGLDomain.setData(pdman, dstdfp.fTextureDomain, view, |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 386 | dstdfp.textureSampler(0).samplerState()); |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 387 | float iw = 1.f / textureDims.width(); |
| 388 | float ih = 1.f / textureDims.height(); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 389 | float scaleAndTransData[4] = { |
| 390 | iw, ih, |
| 391 | -dstdfp.fDeviceSpaceOffset.fX * iw, -dstdfp.fDeviceSpaceOffset.fY * ih |
| 392 | }; |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 393 | if (view.origin() == kBottomLeft_GrSurfaceOrigin) { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 394 | scaleAndTransData[1] = -scaleAndTransData[1]; |
| 395 | scaleAndTransData[3] = 1 - scaleAndTransData[3]; |
| 396 | } |
| 397 | pdman.set4fv(fScaleAndTranslateUni, 1, scaleAndTransData); |
| 398 | } |
| 399 | |
| 400 | private: |
| 401 | GrTextureDomain::GLDomain fGLDomain; |
| 402 | UniformHandle fScaleAndTranslateUni; |
| 403 | }; |
| 404 | |
| 405 | return new GLSLProcessor; |
| 406 | } |
| 407 | |
| 408 | bool GrDeviceSpaceTextureDecalFragmentProcessor::onIsEqual(const GrFragmentProcessor& fp) const { |
| 409 | const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp = |
| 410 | fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>(); |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 411 | return dstdfp.fTextureSampler.view().proxy()->underlyingUniqueID() == |
| 412 | fTextureSampler.view().proxy()->underlyingUniqueID() && |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 413 | dstdfp.fDeviceSpaceOffset == fDeviceSpaceOffset && |
| 414 | dstdfp.fTextureDomain == fTextureDomain; |
| 415 | } |
| 416 | |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 417 | /////////////////////////////////////////////////////////////////////////////// |
| 418 | |
| 419 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDeviceSpaceTextureDecalFragmentProcessor); |
| 420 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 421 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 422 | std::unique_ptr<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::TestCreate( |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 423 | GrProcessorTestData* d) { |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 424 | auto [proxy, at, ct] = d->randomProxy(); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 425 | SkIRect subset; |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 426 | subset.fLeft = d->fRandom->nextULessThan(proxy->width() - 1); |
| 427 | subset.fRight = d->fRandom->nextRangeU(subset.fLeft, proxy->width()); |
| 428 | subset.fTop = d->fRandom->nextULessThan(proxy->height() - 1); |
| 429 | subset.fBottom = d->fRandom->nextRangeU(subset.fTop, proxy->height()); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 430 | SkIPoint pt; |
| 431 | pt.fX = d->fRandom->nextULessThan(2048); |
| 432 | pt.fY = d->fRandom->nextULessThan(2048); |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 433 | return GrDeviceSpaceTextureDecalFragmentProcessor::Make(std::move(proxy), subset, pt); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 434 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 435 | #endif |