blob: f09857a3890bb157a8d691fd73f06ba58368c66f [file] [log] [blame]
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/effects/GrTextureDomain.h"
Robert Phillips40fd7c92017-01-30 08:06:27 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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.org907fbd52013-12-09 17:03:02 +000021
Ben Wagnerf08d1d02018-06-18 15:11:00 -040022#include <utility>
23
Michael Ludwig8fa469d2019-11-25 16:08:44 -050024GrTextureDomain::GrTextureDomain(GrSurfaceProxy* proxy, const SkRect& domain, Mode modeX,
Michael Ludwigbe315a22018-12-17 09:50:51 -050025 Mode modeY, int index)
26 : fModeX(modeX)
27 , fModeY(modeY)
Robert Phillips40fd7c92017-01-30 08:06:27 -050028 , fIndex(index) {
29
Michael Ludwigbe315a22018-12-17 09:50:51 -050030 if (!proxy) {
31 SkASSERT(modeX == kIgnore_Mode && modeY == kIgnore_Mode);
Robert Phillips40fd7c92017-01-30 08:06:27 -050032 return;
33 }
34
Brian Salomon9f2b86c2019-10-22 10:37:46 -040035 const SkRect kFullRect = proxy->getBoundsRect();
Robert Phillips40fd7c92017-01-30 08:06:27 -050036
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 Salomon7eabfe82019-12-02 14:20:20 -050040 SkASSERT(domain.isSorted());
Robert Phillips40fd7c92017-01-30 08:06:27 -050041 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 Salomon7eabfe82019-12-02 14:20:20 -050049GrTextureDomain::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.org907fbd52013-12-09 17:03:02 +000056//////////////////////////////////////////////////////////////////////////////
57
Brian Salomon7caa1372019-12-09 10:40:56 -050058static void append_wrap(GrGLSLShaderBuilder* builder, GrTextureDomain::Mode mode,
59 const char* inCoord, const char* domainStart, const char* domainEnd,
60 const char* out) {
Michael Ludwigbe315a22018-12-17 09:50:51 -050061 switch(mode) {
62 case GrTextureDomain::kIgnore_Mode:
Brian Salomon7caa1372019-12-09 10:40:56 -050063 builder->codeAppendf("%s = %s;\n", out, inCoord);
Michael Ludwigbe315a22018-12-17 09:50:51 -050064 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 Salomon7caa1372019-12-09 10:40:56 -050069 builder->codeAppendf("%s = clamp(%s, %s, %s);", out, inCoord, domainStart, domainEnd);
Michael Ludwigbe315a22018-12-17 09:50:51 -050070 break;
71 case GrTextureDomain::kRepeat_Mode:
Brian Salomon7caa1372019-12-09 10:40:56 -050072 builder->codeAppendf("%s = mod(%s - %s, %s - %s) + %s;", out, inCoord, domainStart,
73 domainEnd, domainStart, domainStart);
Michael Ludwigbe315a22018-12-17 09:50:51 -050074 break;
Brian Salomon7caa1372019-12-09 10:40:56 -050075 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 Ludwigbe315a22018-12-17 09:50:51 -050082 break;
Brian Salomon7caa1372019-12-09 10:40:56 -050083 }
Michael Ludwigbe315a22018-12-17 09:50:51 -050084 }
Michael Ludwigbe315a22018-12-17 09:50:51 -050085}
86
Brian Salomon7eabfe82019-12-02 14:20:20 -050087void 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
egdaniel2d721d32015-11-11 13:06:05 -0800103void GrTextureDomain::GLDomain::sampleTexture(GrGLSLShaderBuilder* builder,
egdaniel7ea439b2015-12-03 09:20:44 -0800104 GrGLSLUniformHandler* uniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500105 const GrShaderCaps* shaderCaps,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000106 const GrTextureDomain& textureDomain,
107 const char* outColor,
108 const SkString& inCoords,
egdaniel09aa1fc2016-04-20 07:09:46 -0700109 GrGLSLFragmentProcessor::SamplerHandle sampler,
Brian Osman2240be92017-10-18 13:15:13 -0400110 const char* inModulateColor) {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500111 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
120void 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 Ludwigbe315a22018-12-17 09:50:51 -0500126 SkASSERT(!fHasMode || (textureDomain.modeX() == fModeX && textureDomain.modeY() == fModeY));
127 SkDEBUGCODE(fModeX = textureDomain.modeX();)
128 SkDEBUGCODE(fModeY = textureDomain.modeY();)
Hans Wennborgc63ec5c2017-12-08 18:56:23 -0800129 SkDEBUGCODE(fHasMode = true;)
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000130
Michael Ludwigbe315a22018-12-17 09:50:51 -0500131 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.org907fbd52013-12-09 17:03:02 +0000134 const char* name;
135 SkString uniName("TexDom");
136 if (textureDomain.fIndex >= 0) {
137 uniName.appendS32(textureDomain.fIndex);
138 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400139 fDomainUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -0800140 uniName.c_str(), &name);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000141 fDomainName = name;
142 }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000143
Michael Ludwigbe315a22018-12-17 09:50:51 -0500144 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);
joshualitt5ae5fc52014-07-29 12:59:27 -0700151 }
Michael Ludwigbe315a22018-12-17 09:50:51 -0500152 // 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.org907fbd52013-12-09 17:03:02 +0000157
Michael Ludwigbe315a22018-12-17 09:50:51 -0500158 // 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 Salomon7caa1372019-12-09 10:40:56 -0500163 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 Ludwigbe315a22018-12-17 09:50:51 -0500177
Brian Salomon7eabfe82019-12-02 14:20:20 -0500178 // Sample 'appendSample' at the clamped coordinate location.
179 SkString color = appendSample("clampedCoord");
Michael Ludwigbe315a22018-12-17 09:50:51 -0500180
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 Nicholase1f55022019-02-05 17:17:40 -0500187 builder->codeAppendf("half err = max(half(abs(clampedCoord.x - origCoord.x) * %s.x), "
188 "half(abs(clampedCoord.y - origCoord.y) * %s.y));",
Michael Ludwigbe315a22018-12-17 09:50:51 -0500189 fDecalName.c_str(), fDecalName.c_str());
190 } else if (decalX) {
Ethan Nicholase1f55022019-02-05 17:17:40 -0500191 builder->codeAppendf("half err = half(abs(clampedCoord.x - origCoord.x) * %s.x);",
Michael Ludwigbe315a22018-12-17 09:50:51 -0500192 fDecalName.c_str());
193 } else {
194 SkASSERT(decalY);
Ethan Nicholase1f55022019-02-05 17:17:40 -0500195 builder->codeAppendf("half err = half(abs(clampedCoord.y - origCoord.y) * %s.y);",
Michael Ludwigbe315a22018-12-17 09:50:51 -0500196 fDecalName.c_str());
joshualitt5ae5fc52014-07-29 12:59:27 -0700197 }
joshualitt5ae5fc52014-07-29 12:59:27 -0700198
Michael Ludwigbe315a22018-12-17 09:50:51 -0500199 // 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 Salomon7eabfe82019-12-02 14:20:20 -0500206 builder->codeAppendf("%s = mix(%s, half4(0, 0, 0, 0), err);", outColor, color.c_str());
Michael Ludwigbe315a22018-12-17 09:50:51 -0500207 } else {
208 // A simple look up
Brian Salomon7eabfe82019-12-02 14:20:20 -0500209 builder->codeAppendf("%s = %s;", outColor, color.c_str());
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000210 }
211}
212
egdaniel018fb622015-10-28 07:26:40 -0700213void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000214 const GrTextureDomain& textureDomain,
Brian Salomon7eabfe82019-12-02 14:20:20 -0500215 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 Ludwig170de012019-11-15 21:55:18 +0000221
Brian Salomon7eabfe82019-12-02 14:20:20 -0500222void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman,
223 const GrTextureDomain& textureDomain,
224 bool filterIfDecal) {
225 this->setData(pdman, textureDomain, nullptr, filterIfDecal);
226}
227
228void 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 Salomon246bc3d2018-12-06 15:33:02 -0500248 SkScalar wInv, hInv, h;
Brian Salomon7eabfe82019-12-02 14:20:20 -0500249 GrTexture* tex = proxy->peekTexture();
Michael Ludwig8fa469d2019-11-25 16:08:44 -0500250 if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) {
Brian Salomon246bc3d2018-12-06 15:33:02 -0500251 wInv = hInv = 1.f;
252 h = tex->height();
Brian Salomon7eabfe82019-12-02 14:20:20 -0500253 // Don't do any scaling by texture size for decal filter rate, it's already in
254 // pixels
Brian Salomon246bc3d2018-12-06 15:33:02 -0500255 } else {
256 wInv = SK_Scalar1 / tex->width();
257 hInv = SK_Scalar1 / tex->height();
258 h = 1.f;
Michael Ludwigbe315a22018-12-17 09:50:51 -0500259
Brian Salomon7eabfe82019-12-02 14:20:20 -0500260 // Account for texture coord normalization in decal filter weights.
261 decalFilterWeights[0] = tex->width();
262 decalFilterWeights[1] = tex->height();
Brian Salomon246bc3d2018-12-06 15:33:02 -0500263 }
Robert Phillipse98234f2017-01-09 14:23:59 -0500264
Brian Salomon7eabfe82019-12-02 14:20:20 -0500265 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 Phillipse98234f2017-01-09 14:23:59 -0500269
Michael Ludwig8fa469d2019-11-25 16:08:44 -0500270 if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500271 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 Salomon246bc3d2018-12-06 15:33:02 -0500275 } else {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500276 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 Salomon246bc3d2018-12-06 15:33:02 -0500280 }
Robert Phillipse98234f2017-01-09 14:23:59 -0500281
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000282 // vertical flip if necessary
Robert Phillipsc686ce32017-07-21 14:12:29 -0400283 if (kBottomLeft_GrSurfaceOrigin == proxy->origin()) {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500284 tempDomainValues[1] = h - tempDomainValues[1];
285 tempDomainValues[3] = h - tempDomainValues[3];
Brian Salomon246bc3d2018-12-06 15:33:02 -0500286
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000287 // The top and bottom were just flipped, so correct the ordering
288 // of elements so that values = (l, t, r, b).
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400289 using std::swap;
Brian Salomon7eabfe82019-12-02 14:20:20 -0500290 swap(tempDomainValues[1], tempDomainValues[3]);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000291 }
Brian Salomon7eabfe82019-12-02 14:20:20 -0500292 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.org907fbd52013-12-09 17:03:02 +0000304 }
305}
306
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000307///////////////////////////////////////////////////////////////////////////////
Brian Salomon587e08f2017-01-27 10:59:27 -0500308
Brian Salomon7eabfe82019-12-02 14:20:20 -0500309std::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 Ludwigbe315a22018-12-17 09:50:51 -0500314}
315
Brian Salomon7eabfe82019-12-02 14:20:20 -0500316std::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 Ludwigbe315a22018-12-17 09:50:51 -0500342 // If both domain modes happen to be ignore, it would be faster to just drop the domain logic
Brian Salomon7eabfe82019-12-02 14:20:20 -0500343 // 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 Phillips40fd7c92017-01-30 08:06:27 -0500347}
348
Brian Salomon7eabfe82019-12-02 14:20:20 -0500349std::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
357std::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}
365GrFragmentProcessor::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
375GrDomainEffect::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 Salomon6cd51b52017-07-26 19:07:15 -0400388 this->addCoordTransform(&fCoordTransform);
Brian Salomon7eabfe82019-12-02 14:20:20 -0500389 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 Phillips40fd7c92017-01-30 08:06:27 -0500394}
395
Brian Salomon7eabfe82019-12-02 14:20:20 -0500396GrDomainEffect::GrDomainEffect(const GrDomainEffect& that)
397 : INHERITED(kGrDomainEffect_ClassID, that.optimizationFlags())
Brian Salomon3f6f9652017-07-28 07:34:05 -0400398 , fCoordTransform(that.fCoordTransform)
Brian Salomon7eabfe82019-12-02 14:20:20 -0500399 , 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 Salomon3f6f9652017-07-28 07:34:05 -0400404 this->addCoordTransform(&fCoordTransform);
Brian Salomon3f6f9652017-07-28 07:34:05 -0400405}
406
Brian Salomon7eabfe82019-12-02 14:20:20 -0500407void GrDomainEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
408 GrProcessorKeyBuilder* b) const {
409 b->add32(GrTextureDomain::GLDomain::DomainKey(fDomain));
joshualitteb2a6762014-12-04 11:35:33 -0800410}
411
Brian Salomon7eabfe82019-12-02 14:20:20 -0500412GrGLSLFragmentProcessor* GrDomainEffect::onCreateGLSLInstance() const {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400413 class GLSLProcessor : public GrGLSLFragmentProcessor {
414 public:
415 void emitCode(EmitArgs& args) override {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500416 const GrDomainEffect& de = args.fFp.cast<GrDomainEffect>();
417 const GrTextureDomain& domain = de.fDomain;
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400418
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400419 SkString coords2D =
Brian Salomon7eabfe82019-12-02 14:20:20 -0500420 args.fFragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint);
Brian Osmanc4689632016-12-19 17:04:59 -0500421
Brian Salomon7eabfe82019-12-02 14:20:20 -0500422 fGLDomain.sampleProcessor(domain, args.fInputColor, args.fOutputColor, coords2D, this,
423 args, 0);
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400424 }
425
426 protected:
Brian Salomonab015ef2017-04-04 10:15:51 -0400427 void onSetData(const GrGLSLProgramDataManager& pdman,
428 const GrFragmentProcessor& fp) override {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500429 const GrDomainEffect& de = fp.cast<GrDomainEffect>();
430 const GrTextureDomain& domain = de.fDomain;
431 fGLDomain.setData(pdman, domain, de.fCoordTransform.proxy(), de.fDecalIsFiltered);
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400432 }
433
434 private:
435 GrTextureDomain::GLDomain fGLDomain;
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400436 };
437
438 return new GLSLProcessor;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000439}
440
Brian Salomon7eabfe82019-12-02 14:20:20 -0500441bool GrDomainEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
442 auto& td = sBase.cast<GrDomainEffect>();
443 return fDomain == td.fDomain && fDecalIsFiltered == td.fDecalIsFiltered;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000444}
445
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000446///////////////////////////////////////////////////////////////////////////////
447
Brian Salomon7eabfe82019-12-02 14:20:20 -0500448GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDomainEffect);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000449
Hal Canary6f6961e2017-01-31 13:50:44 -0500450#if GR_TEST_UTILS
Brian Salomon7eabfe82019-12-02 14:20:20 -0500451std::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 Salomon2ebd0c82016-10-03 17:15:28 -0400486}
Hal Canary6f6961e2017-01-31 13:50:44 -0500487#endif
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400488
489///////////////////////////////////////////////////////////////////////////////
Brian Salomonaff329b2017-08-11 09:40:37 -0400490std::unique_ptr<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::Make(
Michael Ludwig8fa469d2019-11-25 16:08:44 -0500491 sk_sp<GrSurfaceProxy> proxy, const SkIRect& subset, const SkIPoint& deviceSpaceOffset) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400492 return std::unique_ptr<GrFragmentProcessor>(new GrDeviceSpaceTextureDecalFragmentProcessor(
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400493 std::move(proxy), subset, deviceSpaceOffset));
Robert Phillips40fd7c92017-01-30 08:06:27 -0500494}
495
496GrDeviceSpaceTextureDecalFragmentProcessor::GrDeviceSpaceTextureDecalFragmentProcessor(
Michael Ludwig8fa469d2019-11-25 16:08:44 -0500497 sk_sp<GrSurfaceProxy> proxy, const SkIRect& subset, const SkIPoint& deviceSpaceOffset)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400498 : INHERITED(kGrDeviceSpaceTextureDecalFragmentProcessor_ClassID,
499 kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400500 , fTextureSampler(proxy, GrSamplerState::ClampNearest())
Michael Ludwigbe315a22018-12-17 09:50:51 -0500501 , fTextureDomain(proxy.get(),
502 GrTextureDomain::MakeTexelDomain(subset, GrTextureDomain::kDecal_Mode),
503 GrTextureDomain::kDecal_Mode, GrTextureDomain::kDecal_Mode) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400504 this->setTextureSamplerCnt(1);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500505 fDeviceSpaceOffset.fX = deviceSpaceOffset.fX - subset.fLeft;
506 fDeviceSpaceOffset.fY = deviceSpaceOffset.fY - subset.fTop;
Robert Phillips40fd7c92017-01-30 08:06:27 -0500507}
508
Brian Salomon1a2a7ab2017-07-26 13:11:51 -0400509GrDeviceSpaceTextureDecalFragmentProcessor::GrDeviceSpaceTextureDecalFragmentProcessor(
510 const GrDeviceSpaceTextureDecalFragmentProcessor& that)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400511 : INHERITED(kGrDeviceSpaceTextureDecalFragmentProcessor_ClassID,
512 kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Brian Salomon1a2a7ab2017-07-26 13:11:51 -0400513 , fTextureSampler(that.fTextureSampler)
514 , fTextureDomain(that.fTextureDomain)
515 , fDeviceSpaceOffset(that.fDeviceSpaceOffset) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400516 this->setTextureSamplerCnt(1);
Brian Salomon1a2a7ab2017-07-26 13:11:51 -0400517}
518
Brian Salomonaff329b2017-08-11 09:40:37 -0400519std::unique_ptr<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::clone() const {
520 return std::unique_ptr<GrFragmentProcessor>(
521 new GrDeviceSpaceTextureDecalFragmentProcessor(*this));
Brian Salomon1a2a7ab2017-07-26 13:11:51 -0400522}
523
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400524GrGLSLFragmentProcessor* 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 Nicholasf7b88202017-09-18 14:10:39 -0400532 kHalf4_GrSLType,
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400533 "scaleAndTranslate",
534 &scaleAndTranslateName);
Ethan Nicholase1f55022019-02-05 17:17:40 -0500535 args.fFragBuilder->codeAppendf("half2 coords = half2(sk_FragCoord.xy * %s.xy + %s.zw);",
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400536 scaleAndTranslateName, scaleAndTranslateName);
537 fGLDomain.sampleTexture(args.fFragBuilder,
538 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500539 args.fShaderCaps,
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400540 dstdfp.fTextureDomain,
541 args.fOutputColor,
542 SkString("coords"),
543 args.fTexSamplers[0],
544 args.fInputColor);
545 }
546
547 protected:
Brian Salomonab015ef2017-04-04 10:15:51 -0400548 void onSetData(const GrGLSLProgramDataManager& pdman,
549 const GrFragmentProcessor& fp) override {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400550 const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp =
551 fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>();
Michael Ludwig8fa469d2019-11-25 16:08:44 -0500552 GrSurfaceProxy* proxy = dstdfp.textureSampler(0).proxy();
553 SkISize textureDims = proxy->backingStoreDimensions();
Robert Phillips9bee2e52017-05-29 12:37:20 -0400554
Michael Ludwigbe315a22018-12-17 09:50:51 -0500555 fGLDomain.setData(pdman, dstdfp.fTextureDomain, proxy,
556 dstdfp.textureSampler(0).samplerState());
Michael Ludwig8fa469d2019-11-25 16:08:44 -0500557 float iw = 1.f / textureDims.width();
558 float ih = 1.f / textureDims.height();
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400559 float scaleAndTransData[4] = {
560 iw, ih,
561 -dstdfp.fDeviceSpaceOffset.fX * iw, -dstdfp.fDeviceSpaceOffset.fY * ih
562 };
Robert Phillipsc686ce32017-07-21 14:12:29 -0400563 if (proxy->origin() == kBottomLeft_GrSurfaceOrigin) {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400564 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
578bool GrDeviceSpaceTextureDecalFragmentProcessor::onIsEqual(const GrFragmentProcessor& fp) const {
579 const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp =
580 fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>();
Brian Salomon1a2a7ab2017-07-26 13:11:51 -0400581 return dstdfp.fTextureSampler.proxy()->underlyingUniqueID() ==
582 fTextureSampler.proxy()->underlyingUniqueID() &&
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400583 dstdfp.fDeviceSpaceOffset == fDeviceSpaceOffset &&
584 dstdfp.fTextureDomain == fTextureDomain;
585}
586
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400587///////////////////////////////////////////////////////////////////////////////
588
589GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDeviceSpaceTextureDecalFragmentProcessor);
590
Hal Canary6f6961e2017-01-31 13:50:44 -0500591#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400592std::unique_ptr<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::TestCreate(
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400593 GrProcessorTestData* d) {
594 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
595 : GrProcessorUnitTest::kAlphaTextureIdx;
Robert Phillips40fd7c92017-01-30 08:06:27 -0500596 sk_sp<GrTextureProxy> proxy = d->textureProxy(texIdx);
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400597 SkIRect subset;
Robert Phillips40fd7c92017-01-30 08:06:27 -0500598 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 Salomon2ebd0c82016-10-03 17:15:28 -0400602 SkIPoint pt;
603 pt.fX = d->fRandom->nextULessThan(2048);
604 pt.fY = d->fRandom->nextULessThan(2048);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400605 return GrDeviceSpaceTextureDecalFragmentProcessor::Make(std::move(proxy), subset, pt);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000606}
Hal Canary6f6961e2017-01-31 13:50:44 -0500607#endif