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" |
| 15 | #include "src/gpu/effects/generated/GrSimpleTextureEffect.h" |
| 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()); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 41 | fDomain.fLeft = SkScalarPin(domain.fLeft, 0.0f, kFullRect.fRight); |
| 42 | fDomain.fRight = SkScalarPin(domain.fRight, fDomain.fLeft, kFullRect.fRight); |
| 43 | fDomain.fTop = SkScalarPin(domain.fTop, 0.0f, kFullRect.fBottom); |
| 44 | fDomain.fBottom = SkScalarPin(domain.fBottom, fDomain.fTop, kFullRect.fBottom); |
| 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) { |
| 96 | SkString outColor("childColor"); |
| 97 | parent->invokeChild(childIndex, inColor, &outColor, args, coord); |
| 98 | return outColor; |
| 99 | }; |
| 100 | this->sample(args.fFragBuilder, args.fUniformHandler, textureDomain, outColor, inCoords, |
| 101 | appendProcessorSample); |
| 102 | } |
| 103 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 104 | void GrTextureDomain::GLDomain::sampleTexture(GrGLSLShaderBuilder* builder, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 105 | GrGLSLUniformHandler* uniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 106 | const GrShaderCaps* shaderCaps, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 107 | const GrTextureDomain& textureDomain, |
| 108 | const char* outColor, |
| 109 | const SkString& inCoords, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 110 | GrGLSLFragmentProcessor::SamplerHandle sampler, |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 111 | const char* inModulateColor) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 112 | auto appendTextureSample = [&sampler, inModulateColor, builder](const char* coord) { |
| 113 | builder->codeAppend("half4 textureColor = "); |
| 114 | builder->appendTextureLookupAndModulate(inModulateColor, sampler, coord); |
| 115 | builder->codeAppend(";"); |
| 116 | return SkString("textureColor"); |
| 117 | }; |
| 118 | this->sample(builder, uniformHandler, textureDomain, outColor, inCoords, appendTextureSample); |
| 119 | } |
| 120 | |
| 121 | void GrTextureDomain::GLDomain::sample(GrGLSLShaderBuilder* builder, |
| 122 | GrGLSLUniformHandler* uniformHandler, |
| 123 | const GrTextureDomain& textureDomain, |
| 124 | const char* outColor, |
| 125 | const SkString& inCoords, |
| 126 | const std::function<AppendSample>& appendSample) { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 127 | SkASSERT(!fHasMode || (textureDomain.modeX() == fModeX && textureDomain.modeY() == fModeY)); |
| 128 | SkDEBUGCODE(fModeX = textureDomain.modeX();) |
| 129 | SkDEBUGCODE(fModeY = textureDomain.modeY();) |
Hans Wennborg | c63ec5c | 2017-12-08 18:56:23 -0800 | [diff] [blame] | 130 | SkDEBUGCODE(fHasMode = true;) |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 131 | |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 132 | if ((textureDomain.modeX() != kIgnore_Mode || textureDomain.modeY() != kIgnore_Mode) && |
| 133 | !fDomainUni.isValid()) { |
| 134 | // 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] | 135 | const char* name; |
| 136 | SkString uniName("TexDom"); |
| 137 | if (textureDomain.fIndex >= 0) { |
| 138 | uniName.appendS32(textureDomain.fIndex); |
| 139 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 140 | fDomainUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 141 | uniName.c_str(), &name); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 142 | fDomainName = name; |
| 143 | } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 144 | |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 145 | bool decalX = textureDomain.modeX() == kDecal_Mode; |
| 146 | bool decalY = textureDomain.modeY() == kDecal_Mode; |
| 147 | if ((decalX || decalY) && !fDecalUni.isValid()) { |
| 148 | const char* name; |
| 149 | SkString uniName("DecalParams"); |
| 150 | if (textureDomain.fIndex >= 0) { |
| 151 | uniName.appendS32(textureDomain.fIndex); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 152 | } |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 153 | // Half3 since this will hold texture width, height, and then a step function control param |
| 154 | fDecalUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType, |
| 155 | uniName.c_str(), &name); |
| 156 | fDecalName = name; |
| 157 | } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 158 | |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 159 | // Add a block so that we can declare variables |
| 160 | GrGLSLShaderBuilder::ShaderBlock block(builder); |
| 161 | // Always use a local variable for the input coordinates; often callers pass in an expression |
| 162 | // and we want to cache it across all of its references in the code below |
| 163 | builder->codeAppendf("float2 origCoord = %s;", inCoords.c_str()); |
Brian Salomon | 7caa137 | 2019-12-09 10:40:56 -0500 | [diff] [blame] | 164 | builder->codeAppend("float2 clampedCoord;"); |
| 165 | SkString start; |
| 166 | SkString end; |
Brian Salomon | 095d246 | 2019-12-09 15:22:33 -0500 | [diff] [blame^] | 167 | bool is2D = textureDomain.modeX() == textureDomain.modeY(); |
| 168 | if (is2D) { |
| 169 | // Doing the domain setup using vectors seems to avoid shader compilation issues on |
| 170 | // Chromecast, possibly due to reducing shader length. |
| 171 | start.printf("%s.xy", fDomainName.c_str()); |
| 172 | end.printf("%s.zw", fDomainName.c_str()); |
| 173 | append_wrap(builder, textureDomain.modeX(), "origCoord", start.c_str(), end.c_str(), |
| 174 | true, "clampedCoord"); |
| 175 | } else { |
| 176 | // Apply x mode to the x coordinate using the left and right edges of the domain rect |
| 177 | // (stored as the x and z components of the domain uniform). |
| 178 | start.printf("%s.x", fDomainName.c_str()); |
| 179 | end.printf("%s.z", fDomainName.c_str()); |
| 180 | append_wrap(builder, textureDomain.modeX(), "origCoord.x", start.c_str(), end.c_str(), |
| 181 | false, "clampedCoord.x"); |
| 182 | // Repeat the same logic for y. |
| 183 | start.printf("%s.y", fDomainName.c_str()); |
| 184 | end.printf("%s.w", fDomainName.c_str()); |
| 185 | append_wrap(builder, textureDomain.modeY(), "origCoord.y", start.c_str(), end.c_str(), |
| 186 | false, "clampedCoord.y"); |
| 187 | } |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 188 | // Sample 'appendSample' at the clamped coordinate location. |
| 189 | SkString color = appendSample("clampedCoord"); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 190 | |
| 191 | // Apply decal mode's transparency interpolation if needed |
| 192 | if (decalX || decalY) { |
| 193 | // The decal err is the max absoluate value between the clamped coordinate and the original |
| 194 | // pixel coordinate. This will then be clamped to 1.f if it's greater than the control |
| 195 | // parameter, which simulates kNearest and kBilerp behavior depending on if it's 0 or 1. |
| 196 | if (decalX && decalY) { |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 197 | builder->codeAppendf("half err = max(half(abs(clampedCoord.x - origCoord.x) * %s.x), " |
| 198 | "half(abs(clampedCoord.y - origCoord.y) * %s.y));", |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 199 | fDecalName.c_str(), fDecalName.c_str()); |
| 200 | } else if (decalX) { |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 201 | builder->codeAppendf("half err = half(abs(clampedCoord.x - origCoord.x) * %s.x);", |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 202 | fDecalName.c_str()); |
| 203 | } else { |
| 204 | SkASSERT(decalY); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 205 | builder->codeAppendf("half err = half(abs(clampedCoord.y - origCoord.y) * %s.y);", |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 206 | fDecalName.c_str()); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 207 | } |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 208 | |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 209 | // Apply a transform to the error rate, which let's us simulate nearest or bilerp filtering |
| 210 | // in the same shader. When the texture is nearest filtered, fSizeName.z is set to 1/2 so |
| 211 | // this becomes a step function centered at .5 away from the clamped coordinate (but the |
| 212 | // domain for decal is inset by .5 so the edge lines up properly). When bilerp, fSizeName.z |
| 213 | // is set to 1 and it becomes a simple linear blend between texture and transparent. |
| 214 | builder->codeAppendf("if (err > %s.z) { err = 1.0; } else if (%s.z < 1) { err = 0.0; }", |
| 215 | fDecalName.c_str(), fDecalName.c_str()); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 216 | 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] | 217 | } else { |
| 218 | // A simple look up |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 219 | builder->codeAppendf("%s = %s;", outColor, color.c_str()); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 223 | void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 224 | const GrTextureDomain& textureDomain, |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 225 | const GrSurfaceProxy* proxy, |
| 226 | const GrSamplerState& state) { |
| 227 | // We want a hard transition from texture content to trans-black in nearest mode. |
| 228 | bool filterDecal = state.filter() != GrSamplerState::Filter::kNearest; |
| 229 | this->setData(pdman, textureDomain, proxy, filterDecal); |
| 230 | } |
Michael Ludwig | 170de01 | 2019-11-15 21:55:18 +0000 | [diff] [blame] | 231 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 232 | void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman, |
| 233 | const GrTextureDomain& textureDomain, |
| 234 | bool filterIfDecal) { |
| 235 | this->setData(pdman, textureDomain, nullptr, filterIfDecal); |
| 236 | } |
| 237 | |
| 238 | void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman, |
| 239 | const GrTextureDomain& textureDomain, |
| 240 | const GrSurfaceProxy* proxy, |
| 241 | bool filterIfDecal) { |
| 242 | SkASSERT(fHasMode && textureDomain.modeX() == fModeX && textureDomain.modeY() == fModeY); |
| 243 | if (kIgnore_Mode == textureDomain.modeX() && kIgnore_Mode == textureDomain.modeY()) { |
| 244 | return; |
| 245 | } |
| 246 | // If the texture is using nearest filtering, then the decal filter weight should step from |
| 247 | // 0 (texture) to 1 (transparent) one half pixel away from the domain. When doing any other |
| 248 | // form of filtering, the weight should be 1.0 so that it smoothly interpolates between the |
| 249 | // texture and transparent. |
| 250 | // Start off assuming we're in pixel units and later adjust if we have to deal with normalized |
| 251 | // texture coords. |
| 252 | float decalFilterWeights[3] = {1.f, 1.f, filterIfDecal ? 1.f : 0.5f}; |
| 253 | bool sendDecalData = textureDomain.modeX() == kDecal_Mode || |
| 254 | textureDomain.modeY() == kDecal_Mode; |
| 255 | float tempDomainValues[4]; |
| 256 | const float* values; |
| 257 | if (proxy) { |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 258 | SkScalar wInv, hInv, h; |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 259 | GrTexture* tex = proxy->peekTexture(); |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 260 | if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) { |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 261 | wInv = hInv = 1.f; |
| 262 | h = tex->height(); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 263 | // Don't do any scaling by texture size for decal filter rate, it's already in |
| 264 | // pixels |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 265 | } else { |
| 266 | wInv = SK_Scalar1 / tex->width(); |
| 267 | hInv = SK_Scalar1 / tex->height(); |
| 268 | h = 1.f; |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 269 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 270 | // Account for texture coord normalization in decal filter weights. |
| 271 | decalFilterWeights[0] = tex->width(); |
| 272 | decalFilterWeights[1] = tex->height(); |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 273 | } |
Robert Phillips | e98234f | 2017-01-09 14:23:59 -0500 | [diff] [blame] | 274 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 275 | tempDomainValues[0] = SkScalarToFloat(textureDomain.domain().fLeft * wInv); |
| 276 | tempDomainValues[1] = SkScalarToFloat(textureDomain.domain().fTop * hInv); |
| 277 | tempDomainValues[2] = SkScalarToFloat(textureDomain.domain().fRight * wInv); |
| 278 | tempDomainValues[3] = SkScalarToFloat(textureDomain.domain().fBottom * hInv); |
Robert Phillips | e98234f | 2017-01-09 14:23:59 -0500 | [diff] [blame] | 279 | |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 280 | if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 281 | SkASSERT(tempDomainValues[0] >= 0.0f && tempDomainValues[0] <= proxy->width()); |
| 282 | SkASSERT(tempDomainValues[1] >= 0.0f && tempDomainValues[1] <= proxy->height()); |
| 283 | SkASSERT(tempDomainValues[2] >= 0.0f && tempDomainValues[2] <= proxy->width()); |
| 284 | SkASSERT(tempDomainValues[3] >= 0.0f && tempDomainValues[3] <= proxy->height()); |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 285 | } else { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 286 | SkASSERT(tempDomainValues[0] >= 0.0f && tempDomainValues[0] <= 1.0f); |
| 287 | SkASSERT(tempDomainValues[1] >= 0.0f && tempDomainValues[1] <= 1.0f); |
| 288 | SkASSERT(tempDomainValues[2] >= 0.0f && tempDomainValues[2] <= 1.0f); |
| 289 | SkASSERT(tempDomainValues[3] >= 0.0f && tempDomainValues[3] <= 1.0f); |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 290 | } |
Robert Phillips | e98234f | 2017-01-09 14:23:59 -0500 | [diff] [blame] | 291 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 292 | // vertical flip if necessary |
Robert Phillips | c686ce3 | 2017-07-21 14:12:29 -0400 | [diff] [blame] | 293 | if (kBottomLeft_GrSurfaceOrigin == proxy->origin()) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 294 | tempDomainValues[1] = h - tempDomainValues[1]; |
| 295 | tempDomainValues[3] = h - tempDomainValues[3]; |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 296 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 297 | // The top and bottom were just flipped, so correct the ordering |
| 298 | // of elements so that values = (l, t, r, b). |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 299 | using std::swap; |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 300 | swap(tempDomainValues[1], tempDomainValues[3]); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 301 | } |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 302 | values = tempDomainValues; |
| 303 | } else { |
| 304 | values = textureDomain.domain().asScalars(); |
| 305 | } |
| 306 | if (!std::equal(values, values + 4, fPrevDomain)) { |
| 307 | pdman.set4fv(fDomainUni, 1, values); |
| 308 | std::copy_n(values, 4, fPrevDomain); |
| 309 | } |
| 310 | if (sendDecalData && |
| 311 | !std::equal(decalFilterWeights, decalFilterWeights + 3, fPrevDeclFilterWeights)) { |
| 312 | pdman.set3fv(fDecalUni, 1, decalFilterWeights); |
| 313 | std::copy_n(decalFilterWeights, 3, fPrevDeclFilterWeights); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 317 | /////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 318 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 319 | std::unique_ptr<GrFragmentProcessor> GrDomainEffect::Make(std::unique_ptr<GrFragmentProcessor> fp, |
| 320 | const SkRect& domain, |
| 321 | GrTextureDomain::Mode mode, |
| 322 | bool decalIsFiltered) { |
| 323 | return Make(std::move(fp), domain, mode, mode, decalIsFiltered); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 324 | } |
| 325 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 326 | std::unique_ptr<GrFragmentProcessor> GrDomainEffect::Make(std::unique_ptr<GrFragmentProcessor> fp, |
| 327 | const SkRect& domain, |
| 328 | GrTextureDomain::Mode modeX, |
| 329 | GrTextureDomain::Mode modeY, |
| 330 | bool decalIsFiltered) { |
| 331 | if (modeX == GrTextureDomain::kIgnore_Mode && modeY == GrTextureDomain::kIgnore_Mode) { |
| 332 | return fp; |
| 333 | } |
| 334 | int count = 0; |
| 335 | GrCoordTransform* coordTransform = nullptr; |
| 336 | for (auto [transform, ignored] : GrFragmentProcessor::FPCoordTransformRange(*fp)) { |
| 337 | ++count; |
| 338 | coordTransform = &transform; |
| 339 | } |
| 340 | // If there are no coord transforms on the passed FP or it's children then there's no need to |
| 341 | // enforce a domain. |
| 342 | // We have a limitation that only one coord transform is support when overriding local coords. |
| 343 | // If that limit were relaxed we would need to add a coord transform for each descendent FP |
| 344 | // transform and possibly have multiple domain rects to account for different proxy |
| 345 | // normalization and y-reversals. |
| 346 | if (count != 1) { |
| 347 | return fp; |
| 348 | } |
| 349 | GrCoordTransform transformCopy = *coordTransform; |
| 350 | // Reset the child FP's coord transform. |
| 351 | *coordTransform = {}; |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 352 | // If both domain modes happen to be ignore, it would be faster to just drop the domain logic |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 353 | // entirely and return the original FP. We'd need a GrMatrixProcessor if the matrix is not |
| 354 | // identity, though. |
| 355 | return std::unique_ptr<GrFragmentProcessor>(new GrDomainEffect( |
| 356 | std::move(fp), transformCopy, domain, modeX, modeY, decalIsFiltered)); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 357 | } |
| 358 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 359 | std::unique_ptr<GrFragmentProcessor> GrDomainEffect::Make(std::unique_ptr<GrFragmentProcessor> fp, |
| 360 | const SkRect& domain, |
| 361 | GrTextureDomain::Mode mode, |
| 362 | GrSamplerState::Filter filter) { |
| 363 | bool filterIfDecal = filter != GrSamplerState::Filter::kNearest; |
| 364 | return Make(std::move(fp), domain, mode, filterIfDecal); |
| 365 | } |
| 366 | |
| 367 | std::unique_ptr<GrFragmentProcessor> GrDomainEffect::Make(std::unique_ptr<GrFragmentProcessor> fp, |
| 368 | const SkRect& domain, |
| 369 | GrTextureDomain::Mode modeX, |
| 370 | GrTextureDomain::Mode modeY, |
| 371 | GrSamplerState::Filter filter) { |
| 372 | bool filterIfDecal = filter != GrSamplerState::Filter::kNearest; |
| 373 | return Make(std::move(fp), domain, modeX, modeY, filterIfDecal); |
| 374 | } |
| 375 | GrFragmentProcessor::OptimizationFlags GrDomainEffect::Flags(GrFragmentProcessor* fp, |
| 376 | GrTextureDomain::Mode modeX, |
| 377 | GrTextureDomain::Mode modeY) { |
| 378 | auto fpFlags = GrFragmentProcessor::ProcessorOptimizationFlags(fp); |
| 379 | if (modeX == GrTextureDomain::kDecal_Mode || modeY == GrTextureDomain::kDecal_Mode) { |
| 380 | return fpFlags & ~kPreservesOpaqueInput_OptimizationFlag; |
| 381 | } |
| 382 | return fpFlags; |
| 383 | } |
| 384 | |
| 385 | GrDomainEffect::GrDomainEffect(std::unique_ptr<GrFragmentProcessor> fp, |
| 386 | const GrCoordTransform& coordTransform, |
| 387 | const SkRect& domain, |
| 388 | GrTextureDomain::Mode modeX, |
| 389 | GrTextureDomain::Mode modeY, |
| 390 | bool decalIsFiltered) |
| 391 | : INHERITED(kGrDomainEffect_ClassID, Flags(fp.get(), modeX, modeY)) |
| 392 | , fCoordTransform(coordTransform) |
| 393 | , fDomain(domain, modeX, modeY) |
| 394 | , fDecalIsFiltered(decalIsFiltered) { |
| 395 | SkASSERT(fp); |
| 396 | fp->setSampledWithExplicitCoords(true); |
| 397 | this->registerChildProcessor(std::move(fp)); |
Brian Salomon | 6cd51b5 | 2017-07-26 19:07:15 -0400 | [diff] [blame] | 398 | this->addCoordTransform(&fCoordTransform); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 399 | if (fDomain.modeX() != GrTextureDomain::kDecal_Mode && |
| 400 | fDomain.modeY() != GrTextureDomain::kDecal_Mode) { |
| 401 | // Canonicalize this don't care value so we don't have to worry about it elsewhere. |
| 402 | fDecalIsFiltered = false; |
| 403 | } |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 404 | } |
| 405 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 406 | GrDomainEffect::GrDomainEffect(const GrDomainEffect& that) |
| 407 | : INHERITED(kGrDomainEffect_ClassID, that.optimizationFlags()) |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 408 | , fCoordTransform(that.fCoordTransform) |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 409 | , fDomain(that.fDomain) |
| 410 | , fDecalIsFiltered(that.fDecalIsFiltered) { |
| 411 | auto child = that.childProcessor(0).clone(); |
| 412 | child->setSampledWithExplicitCoords(true); |
| 413 | this->registerChildProcessor(std::move(child)); |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 414 | this->addCoordTransform(&fCoordTransform); |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 415 | } |
| 416 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 417 | void GrDomainEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 418 | GrProcessorKeyBuilder* b) const { |
| 419 | b->add32(GrTextureDomain::GLDomain::DomainKey(fDomain)); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 422 | GrGLSLFragmentProcessor* GrDomainEffect::onCreateGLSLInstance() const { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 423 | class GLSLProcessor : public GrGLSLFragmentProcessor { |
| 424 | public: |
| 425 | void emitCode(EmitArgs& args) override { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 426 | const GrDomainEffect& de = args.fFp.cast<GrDomainEffect>(); |
| 427 | const GrTextureDomain& domain = de.fDomain; |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 428 | |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 429 | SkString coords2D = |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 430 | args.fFragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint); |
Brian Osman | c468963 | 2016-12-19 17:04:59 -0500 | [diff] [blame] | 431 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 432 | fGLDomain.sampleProcessor(domain, args.fInputColor, args.fOutputColor, coords2D, this, |
| 433 | args, 0); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 437 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 438 | const GrFragmentProcessor& fp) override { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 439 | const GrDomainEffect& de = fp.cast<GrDomainEffect>(); |
| 440 | const GrTextureDomain& domain = de.fDomain; |
| 441 | fGLDomain.setData(pdman, domain, de.fCoordTransform.proxy(), de.fDecalIsFiltered); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | private: |
| 445 | GrTextureDomain::GLDomain fGLDomain; |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 446 | }; |
| 447 | |
| 448 | return new GLSLProcessor; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 451 | bool GrDomainEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
| 452 | auto& td = sBase.cast<GrDomainEffect>(); |
| 453 | return fDomain == td.fDomain && fDecalIsFiltered == td.fDecalIsFiltered; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 454 | } |
| 455 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 456 | /////////////////////////////////////////////////////////////////////////////// |
| 457 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 458 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDomainEffect); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 459 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 460 | #if GR_TEST_UTILS |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 461 | std::unique_ptr<GrFragmentProcessor> GrDomainEffect::TestCreate(GrProcessorTestData* d) { |
| 462 | do { |
| 463 | GrTextureDomain::Mode modeX = |
| 464 | (GrTextureDomain::Mode)d->fRandom->nextULessThan(GrTextureDomain::kModeCount); |
| 465 | GrTextureDomain::Mode modeY = |
| 466 | (GrTextureDomain::Mode)d->fRandom->nextULessThan(GrTextureDomain::kModeCount); |
| 467 | auto child = GrProcessorUnitTest::MakeChildFP(d); |
| 468 | const auto* childPtr = child.get(); |
| 469 | SkRect domain; |
| 470 | // We assert if the child's coord transform has a proxy and the domain rect is outside its |
| 471 | // bounds. |
| 472 | GrFragmentProcessor::CoordTransformIter ctIter(*child); |
| 473 | if (!ctIter) { |
| 474 | continue; |
| 475 | } |
| 476 | auto [transform, fp] = *ctIter; |
| 477 | if (auto proxy = transform.proxy()) { |
| 478 | auto [w, h] = proxy->backingStoreDimensions(); |
| 479 | domain.fLeft = d->fRandom->nextRangeScalar(0, w); |
| 480 | domain.fRight = d->fRandom->nextRangeScalar(0, w); |
| 481 | domain.fTop = d->fRandom->nextRangeScalar(0, h); |
| 482 | domain.fBottom = d->fRandom->nextRangeScalar(0, h); |
| 483 | } else { |
| 484 | domain.fLeft = d->fRandom->nextRangeScalar(-100.f, 100.f); |
| 485 | domain.fRight = d->fRandom->nextRangeScalar(-100.f, 100.f); |
| 486 | domain.fTop = d->fRandom->nextRangeScalar(-100.f, 100.f); |
| 487 | domain.fBottom = d->fRandom->nextRangeScalar(-100.f, 100.f); |
| 488 | } |
| 489 | domain.sort(); |
| 490 | bool filterIfDecal = d->fRandom->nextBool(); |
| 491 | auto result = GrDomainEffect::Make(std::move(child), domain, modeX, modeY, filterIfDecal); |
| 492 | if (result && result.get() != childPtr) { |
| 493 | return result; |
| 494 | } |
| 495 | } while (true); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 496 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 497 | #endif |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 498 | |
| 499 | /////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 500 | std::unique_ptr<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::Make( |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 501 | sk_sp<GrSurfaceProxy> proxy, const SkIRect& subset, const SkIPoint& deviceSpaceOffset) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 502 | return std::unique_ptr<GrFragmentProcessor>(new GrDeviceSpaceTextureDecalFragmentProcessor( |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 503 | std::move(proxy), subset, deviceSpaceOffset)); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | GrDeviceSpaceTextureDecalFragmentProcessor::GrDeviceSpaceTextureDecalFragmentProcessor( |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 507 | sk_sp<GrSurfaceProxy> proxy, const SkIRect& subset, const SkIPoint& deviceSpaceOffset) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 508 | : INHERITED(kGrDeviceSpaceTextureDecalFragmentProcessor_ClassID, |
| 509 | kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 510 | , fTextureSampler(proxy, GrSamplerState::ClampNearest()) |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 511 | , fTextureDomain(proxy.get(), |
| 512 | GrTextureDomain::MakeTexelDomain(subset, GrTextureDomain::kDecal_Mode), |
| 513 | GrTextureDomain::kDecal_Mode, GrTextureDomain::kDecal_Mode) { |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 514 | this->setTextureSamplerCnt(1); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 515 | fDeviceSpaceOffset.fX = deviceSpaceOffset.fX - subset.fLeft; |
| 516 | fDeviceSpaceOffset.fY = deviceSpaceOffset.fY - subset.fTop; |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 517 | } |
| 518 | |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 519 | GrDeviceSpaceTextureDecalFragmentProcessor::GrDeviceSpaceTextureDecalFragmentProcessor( |
| 520 | const GrDeviceSpaceTextureDecalFragmentProcessor& that) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 521 | : INHERITED(kGrDeviceSpaceTextureDecalFragmentProcessor_ClassID, |
| 522 | kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 523 | , fTextureSampler(that.fTextureSampler) |
| 524 | , fTextureDomain(that.fTextureDomain) |
| 525 | , fDeviceSpaceOffset(that.fDeviceSpaceOffset) { |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 526 | this->setTextureSamplerCnt(1); |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 527 | } |
| 528 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 529 | std::unique_ptr<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::clone() const { |
| 530 | return std::unique_ptr<GrFragmentProcessor>( |
| 531 | new GrDeviceSpaceTextureDecalFragmentProcessor(*this)); |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 532 | } |
| 533 | |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 534 | GrGLSLFragmentProcessor* GrDeviceSpaceTextureDecalFragmentProcessor::onCreateGLSLInstance() const { |
| 535 | class GLSLProcessor : public GrGLSLFragmentProcessor { |
| 536 | public: |
| 537 | void emitCode(EmitArgs& args) override { |
| 538 | const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp = |
| 539 | args.fFp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>(); |
| 540 | const char* scaleAndTranslateName; |
| 541 | fScaleAndTranslateUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 542 | kHalf4_GrSLType, |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 543 | "scaleAndTranslate", |
| 544 | &scaleAndTranslateName); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 545 | 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] | 546 | scaleAndTranslateName, scaleAndTranslateName); |
| 547 | fGLDomain.sampleTexture(args.fFragBuilder, |
| 548 | args.fUniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 549 | args.fShaderCaps, |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 550 | dstdfp.fTextureDomain, |
| 551 | args.fOutputColor, |
| 552 | SkString("coords"), |
| 553 | args.fTexSamplers[0], |
| 554 | args.fInputColor); |
| 555 | } |
| 556 | |
| 557 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 558 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 559 | const GrFragmentProcessor& fp) override { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 560 | const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp = |
| 561 | fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>(); |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 562 | GrSurfaceProxy* proxy = dstdfp.textureSampler(0).proxy(); |
| 563 | SkISize textureDims = proxy->backingStoreDimensions(); |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 564 | |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 565 | fGLDomain.setData(pdman, dstdfp.fTextureDomain, proxy, |
| 566 | dstdfp.textureSampler(0).samplerState()); |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 567 | float iw = 1.f / textureDims.width(); |
| 568 | float ih = 1.f / textureDims.height(); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 569 | float scaleAndTransData[4] = { |
| 570 | iw, ih, |
| 571 | -dstdfp.fDeviceSpaceOffset.fX * iw, -dstdfp.fDeviceSpaceOffset.fY * ih |
| 572 | }; |
Robert Phillips | c686ce3 | 2017-07-21 14:12:29 -0400 | [diff] [blame] | 573 | if (proxy->origin() == kBottomLeft_GrSurfaceOrigin) { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 574 | scaleAndTransData[1] = -scaleAndTransData[1]; |
| 575 | scaleAndTransData[3] = 1 - scaleAndTransData[3]; |
| 576 | } |
| 577 | pdman.set4fv(fScaleAndTranslateUni, 1, scaleAndTransData); |
| 578 | } |
| 579 | |
| 580 | private: |
| 581 | GrTextureDomain::GLDomain fGLDomain; |
| 582 | UniformHandle fScaleAndTranslateUni; |
| 583 | }; |
| 584 | |
| 585 | return new GLSLProcessor; |
| 586 | } |
| 587 | |
| 588 | bool GrDeviceSpaceTextureDecalFragmentProcessor::onIsEqual(const GrFragmentProcessor& fp) const { |
| 589 | const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp = |
| 590 | fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>(); |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 591 | return dstdfp.fTextureSampler.proxy()->underlyingUniqueID() == |
| 592 | fTextureSampler.proxy()->underlyingUniqueID() && |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 593 | dstdfp.fDeviceSpaceOffset == fDeviceSpaceOffset && |
| 594 | dstdfp.fTextureDomain == fTextureDomain; |
| 595 | } |
| 596 | |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 597 | /////////////////////////////////////////////////////////////////////////////// |
| 598 | |
| 599 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDeviceSpaceTextureDecalFragmentProcessor); |
| 600 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 601 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 602 | std::unique_ptr<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::TestCreate( |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 603 | GrProcessorTestData* d) { |
| 604 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx |
| 605 | : GrProcessorUnitTest::kAlphaTextureIdx; |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 606 | sk_sp<GrTextureProxy> proxy = d->textureProxy(texIdx); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 607 | SkIRect subset; |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 608 | subset.fLeft = d->fRandom->nextULessThan(proxy->width() - 1); |
| 609 | subset.fRight = d->fRandom->nextRangeU(subset.fLeft, proxy->width()); |
| 610 | subset.fTop = d->fRandom->nextULessThan(proxy->height() - 1); |
| 611 | subset.fBottom = d->fRandom->nextRangeU(subset.fTop, proxy->height()); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 612 | SkIPoint pt; |
| 613 | pt.fX = d->fRandom->nextULessThan(2048); |
| 614 | pt.fY = d->fRandom->nextULessThan(2048); |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 615 | return GrDeviceSpaceTextureDecalFragmentProcessor::Make(std::move(proxy), subset, pt); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 616 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 617 | #endif |