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