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