blob: d090cbdddafbe84104091513851d28203ee52a28 [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,
Brian Salomon095d2462019-12-09 15:22:33 -050060 bool is2D, 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: {
Brian Salomon095d2462019-12-09 15:22:33 -050076 const char* type = is2D ? "float2" : "float";
Brian Salomon7caa1372019-12-09 10:40:56 -050077 builder->codeAppend("{");
Brian Salomon095d2462019-12-09 15:22:33 -050078 builder->codeAppendf("%s w = %s - %s;", type, domainEnd, domainStart);
79 builder->codeAppendf("%s w2 = 2 * w;", type);
80 builder->codeAppendf("%s m = mod(%s - %s, w2);", type, inCoord, domainStart);
Brian Salomon7caa1372019-12-09 10:40:56 -050081 builder->codeAppendf("%s = mix(m, w2 - m, step(w, m)) + %s;", out, domainStart);
82 builder->codeAppend("}");
Michael Ludwigbe315a22018-12-17 09:50:51 -050083 break;
Brian Salomon7caa1372019-12-09 10:40:56 -050084 }
Michael Ludwigbe315a22018-12-17 09:50:51 -050085 }
Michael Ludwigbe315a22018-12-17 09:50:51 -050086}
87
Brian Salomon7eabfe82019-12-02 14:20:20 -050088void GrTextureDomain::GLDomain::sampleProcessor(const GrTextureDomain& textureDomain,
89 const char* inColor,
90 const char* outColor,
91 const SkString& inCoords,
92 GrGLSLFragmentProcessor* parent,
93 GrGLSLFragmentProcessor::EmitArgs& args,
94 int childIndex) {
95 auto appendProcessorSample = [parent, &args, childIndex, inColor](const char* coord) {
96 SkString outColor("childColor");
97 parent->invokeChild(childIndex, inColor, &outColor, args, coord);
98 return outColor;
99 };
100 this->sample(args.fFragBuilder, args.fUniformHandler, textureDomain, outColor, inCoords,
101 appendProcessorSample);
102}
103
egdaniel2d721d32015-11-11 13:06:05 -0800104void GrTextureDomain::GLDomain::sampleTexture(GrGLSLShaderBuilder* builder,
egdaniel7ea439b2015-12-03 09:20:44 -0800105 GrGLSLUniformHandler* uniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500106 const GrShaderCaps* shaderCaps,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000107 const GrTextureDomain& textureDomain,
108 const char* outColor,
109 const SkString& inCoords,
egdaniel09aa1fc2016-04-20 07:09:46 -0700110 GrGLSLFragmentProcessor::SamplerHandle sampler,
Brian Osman2240be92017-10-18 13:15:13 -0400111 const char* inModulateColor) {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500112 auto appendTextureSample = [&sampler, inModulateColor, builder](const char* coord) {
113 builder->codeAppend("half4 textureColor = ");
114 builder->appendTextureLookupAndModulate(inModulateColor, sampler, coord);
115 builder->codeAppend(";");
116 return SkString("textureColor");
117 };
118 this->sample(builder, uniformHandler, textureDomain, outColor, inCoords, appendTextureSample);
119}
120
121void GrTextureDomain::GLDomain::sample(GrGLSLShaderBuilder* builder,
122 GrGLSLUniformHandler* uniformHandler,
123 const GrTextureDomain& textureDomain,
124 const char* outColor,
125 const SkString& inCoords,
126 const std::function<AppendSample>& appendSample) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500127 SkASSERT(!fHasMode || (textureDomain.modeX() == fModeX && textureDomain.modeY() == fModeY));
128 SkDEBUGCODE(fModeX = textureDomain.modeX();)
129 SkDEBUGCODE(fModeY = textureDomain.modeY();)
Hans Wennborgc63ec5c2017-12-08 18:56:23 -0800130 SkDEBUGCODE(fHasMode = true;)
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000131
Michael Ludwigbe315a22018-12-17 09:50:51 -0500132 if ((textureDomain.modeX() != kIgnore_Mode || textureDomain.modeY() != kIgnore_Mode) &&
133 !fDomainUni.isValid()) {
134 // Must include the domain uniform since at least one axis uses it
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000135 const char* name;
136 SkString uniName("TexDom");
137 if (textureDomain.fIndex >= 0) {
138 uniName.appendS32(textureDomain.fIndex);
139 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400140 fDomainUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -0800141 uniName.c_str(), &name);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000142 fDomainName = name;
143 }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000144
Michael Ludwigbe315a22018-12-17 09:50:51 -0500145 bool decalX = textureDomain.modeX() == kDecal_Mode;
146 bool decalY = textureDomain.modeY() == kDecal_Mode;
147 if ((decalX || decalY) && !fDecalUni.isValid()) {
148 const char* name;
149 SkString uniName("DecalParams");
150 if (textureDomain.fIndex >= 0) {
151 uniName.appendS32(textureDomain.fIndex);
joshualitt5ae5fc52014-07-29 12:59:27 -0700152 }
Michael Ludwigbe315a22018-12-17 09:50:51 -0500153 // Half3 since this will hold texture width, height, and then a step function control param
154 fDecalUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType,
155 uniName.c_str(), &name);
156 fDecalName = name;
157 }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000158
Michael Ludwigbe315a22018-12-17 09:50:51 -0500159 // Add a block so that we can declare variables
160 GrGLSLShaderBuilder::ShaderBlock block(builder);
161 // Always use a local variable for the input coordinates; often callers pass in an expression
162 // and we want to cache it across all of its references in the code below
163 builder->codeAppendf("float2 origCoord = %s;", inCoords.c_str());
Brian Salomon7caa1372019-12-09 10:40:56 -0500164 builder->codeAppend("float2 clampedCoord;");
165 SkString start;
166 SkString end;
Brian Salomon095d2462019-12-09 15:22:33 -0500167 bool is2D = textureDomain.modeX() == textureDomain.modeY();
168 if (is2D) {
169 // Doing the domain setup using vectors seems to avoid shader compilation issues on
170 // Chromecast, possibly due to reducing shader length.
171 start.printf("%s.xy", fDomainName.c_str());
172 end.printf("%s.zw", fDomainName.c_str());
173 append_wrap(builder, textureDomain.modeX(), "origCoord", start.c_str(), end.c_str(),
174 true, "clampedCoord");
175 } else {
176 // Apply x mode to the x coordinate using the left and right edges of the domain rect
177 // (stored as the x and z components of the domain uniform).
178 start.printf("%s.x", fDomainName.c_str());
179 end.printf("%s.z", fDomainName.c_str());
180 append_wrap(builder, textureDomain.modeX(), "origCoord.x", start.c_str(), end.c_str(),
181 false, "clampedCoord.x");
182 // Repeat the same logic for y.
183 start.printf("%s.y", fDomainName.c_str());
184 end.printf("%s.w", fDomainName.c_str());
185 append_wrap(builder, textureDomain.modeY(), "origCoord.y", start.c_str(), end.c_str(),
186 false, "clampedCoord.y");
187 }
Brian Salomon7eabfe82019-12-02 14:20:20 -0500188 // Sample 'appendSample' at the clamped coordinate location.
189 SkString color = appendSample("clampedCoord");
Michael Ludwigbe315a22018-12-17 09:50:51 -0500190
191 // Apply decal mode's transparency interpolation if needed
192 if (decalX || decalY) {
193 // The decal err is the max absoluate value between the clamped coordinate and the original
194 // pixel coordinate. This will then be clamped to 1.f if it's greater than the control
195 // parameter, which simulates kNearest and kBilerp behavior depending on if it's 0 or 1.
196 if (decalX && decalY) {
Ethan Nicholase1f55022019-02-05 17:17:40 -0500197 builder->codeAppendf("half err = max(half(abs(clampedCoord.x - origCoord.x) * %s.x), "
198 "half(abs(clampedCoord.y - origCoord.y) * %s.y));",
Michael Ludwigbe315a22018-12-17 09:50:51 -0500199 fDecalName.c_str(), fDecalName.c_str());
200 } else if (decalX) {
Ethan Nicholase1f55022019-02-05 17:17:40 -0500201 builder->codeAppendf("half err = half(abs(clampedCoord.x - origCoord.x) * %s.x);",
Michael Ludwigbe315a22018-12-17 09:50:51 -0500202 fDecalName.c_str());
203 } else {
204 SkASSERT(decalY);
Ethan Nicholase1f55022019-02-05 17:17:40 -0500205 builder->codeAppendf("half err = half(abs(clampedCoord.y - origCoord.y) * %s.y);",
Michael Ludwigbe315a22018-12-17 09:50:51 -0500206 fDecalName.c_str());
joshualitt5ae5fc52014-07-29 12:59:27 -0700207 }
joshualitt5ae5fc52014-07-29 12:59:27 -0700208
Michael Ludwigbe315a22018-12-17 09:50:51 -0500209 // Apply a transform to the error rate, which let's us simulate nearest or bilerp filtering
210 // in the same shader. When the texture is nearest filtered, fSizeName.z is set to 1/2 so
211 // this becomes a step function centered at .5 away from the clamped coordinate (but the
212 // domain for decal is inset by .5 so the edge lines up properly). When bilerp, fSizeName.z
213 // is set to 1 and it becomes a simple linear blend between texture and transparent.
214 builder->codeAppendf("if (err > %s.z) { err = 1.0; } else if (%s.z < 1) { err = 0.0; }",
215 fDecalName.c_str(), fDecalName.c_str());
Brian Salomon7eabfe82019-12-02 14:20:20 -0500216 builder->codeAppendf("%s = mix(%s, half4(0, 0, 0, 0), err);", outColor, color.c_str());
Michael Ludwigbe315a22018-12-17 09:50:51 -0500217 } else {
218 // A simple look up
Brian Salomon7eabfe82019-12-02 14:20:20 -0500219 builder->codeAppendf("%s = %s;", outColor, color.c_str());
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000220 }
221}
222
egdaniel018fb622015-10-28 07:26:40 -0700223void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000224 const GrTextureDomain& textureDomain,
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000225 const GrSurfaceProxyView& view,
Brian Salomon7eabfe82019-12-02 14:20:20 -0500226 const GrSamplerState& state) {
227 // We want a hard transition from texture content to trans-black in nearest mode.
228 bool filterDecal = state.filter() != GrSamplerState::Filter::kNearest;
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000229 this->setData(pdman, textureDomain, view.proxy(), view.origin(), filterDecal);
Brian Salomon7eabfe82019-12-02 14:20:20 -0500230}
Michael Ludwig170de012019-11-15 21:55:18 +0000231
Brian Salomon7eabfe82019-12-02 14:20:20 -0500232void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman,
233 const GrTextureDomain& textureDomain,
234 bool filterIfDecal) {
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000235 // The origin we pass here doesn't matter
236 this->setData(pdman, textureDomain, nullptr, kTopLeft_GrSurfaceOrigin, filterIfDecal);
Brian Salomon7eabfe82019-12-02 14:20:20 -0500237}
238
239void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman,
240 const GrTextureDomain& textureDomain,
241 const GrSurfaceProxy* proxy,
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000242 GrSurfaceOrigin origin,
Brian Salomon7eabfe82019-12-02 14:20:20 -0500243 bool filterIfDecal) {
244 SkASSERT(fHasMode && textureDomain.modeX() == fModeX && textureDomain.modeY() == fModeY);
245 if (kIgnore_Mode == textureDomain.modeX() && kIgnore_Mode == textureDomain.modeY()) {
246 return;
247 }
248 // If the texture is using nearest filtering, then the decal filter weight should step from
249 // 0 (texture) to 1 (transparent) one half pixel away from the domain. When doing any other
250 // form of filtering, the weight should be 1.0 so that it smoothly interpolates between the
251 // texture and transparent.
252 // Start off assuming we're in pixel units and later adjust if we have to deal with normalized
253 // texture coords.
254 float decalFilterWeights[3] = {1.f, 1.f, filterIfDecal ? 1.f : 0.5f};
255 bool sendDecalData = textureDomain.modeX() == kDecal_Mode ||
256 textureDomain.modeY() == kDecal_Mode;
257 float tempDomainValues[4];
258 const float* values;
259 if (proxy) {
Brian Salomon246bc3d2018-12-06 15:33:02 -0500260 SkScalar wInv, hInv, h;
Brian Salomon7eabfe82019-12-02 14:20:20 -0500261 GrTexture* tex = proxy->peekTexture();
Michael Ludwig8fa469d2019-11-25 16:08:44 -0500262 if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) {
Brian Salomon246bc3d2018-12-06 15:33:02 -0500263 wInv = hInv = 1.f;
264 h = tex->height();
Brian Salomon7eabfe82019-12-02 14:20:20 -0500265 // Don't do any scaling by texture size for decal filter rate, it's already in
266 // pixels
Brian Salomon246bc3d2018-12-06 15:33:02 -0500267 } else {
268 wInv = SK_Scalar1 / tex->width();
269 hInv = SK_Scalar1 / tex->height();
270 h = 1.f;
Michael Ludwigbe315a22018-12-17 09:50:51 -0500271
Brian Salomon7eabfe82019-12-02 14:20:20 -0500272 // Account for texture coord normalization in decal filter weights.
273 decalFilterWeights[0] = tex->width();
274 decalFilterWeights[1] = tex->height();
Brian Salomon246bc3d2018-12-06 15:33:02 -0500275 }
Robert Phillipse98234f2017-01-09 14:23:59 -0500276
Brian Salomon7eabfe82019-12-02 14:20:20 -0500277 tempDomainValues[0] = SkScalarToFloat(textureDomain.domain().fLeft * wInv);
278 tempDomainValues[1] = SkScalarToFloat(textureDomain.domain().fTop * hInv);
279 tempDomainValues[2] = SkScalarToFloat(textureDomain.domain().fRight * wInv);
280 tempDomainValues[3] = SkScalarToFloat(textureDomain.domain().fBottom * hInv);
Robert Phillipse98234f2017-01-09 14:23:59 -0500281
Michael Ludwig8fa469d2019-11-25 16:08:44 -0500282 if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500283 SkASSERT(tempDomainValues[0] >= 0.0f && tempDomainValues[0] <= proxy->width());
284 SkASSERT(tempDomainValues[1] >= 0.0f && tempDomainValues[1] <= proxy->height());
285 SkASSERT(tempDomainValues[2] >= 0.0f && tempDomainValues[2] <= proxy->width());
286 SkASSERT(tempDomainValues[3] >= 0.0f && tempDomainValues[3] <= proxy->height());
Brian Salomon246bc3d2018-12-06 15:33:02 -0500287 } else {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500288 SkASSERT(tempDomainValues[0] >= 0.0f && tempDomainValues[0] <= 1.0f);
289 SkASSERT(tempDomainValues[1] >= 0.0f && tempDomainValues[1] <= 1.0f);
290 SkASSERT(tempDomainValues[2] >= 0.0f && tempDomainValues[2] <= 1.0f);
291 SkASSERT(tempDomainValues[3] >= 0.0f && tempDomainValues[3] <= 1.0f);
Brian Salomon246bc3d2018-12-06 15:33:02 -0500292 }
Robert Phillipse98234f2017-01-09 14:23:59 -0500293
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000294 // vertical flip if necessary
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000295 if (kBottomLeft_GrSurfaceOrigin == origin) {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500296 tempDomainValues[1] = h - tempDomainValues[1];
297 tempDomainValues[3] = h - tempDomainValues[3];
Brian Salomon246bc3d2018-12-06 15:33:02 -0500298
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000299 // The top and bottom were just flipped, so correct the ordering
300 // of elements so that values = (l, t, r, b).
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400301 using std::swap;
Brian Salomon7eabfe82019-12-02 14:20:20 -0500302 swap(tempDomainValues[1], tempDomainValues[3]);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000303 }
Brian Salomon7eabfe82019-12-02 14:20:20 -0500304 values = tempDomainValues;
305 } else {
306 values = textureDomain.domain().asScalars();
307 }
308 if (!std::equal(values, values + 4, fPrevDomain)) {
309 pdman.set4fv(fDomainUni, 1, values);
310 std::copy_n(values, 4, fPrevDomain);
311 }
312 if (sendDecalData &&
313 !std::equal(decalFilterWeights, decalFilterWeights + 3, fPrevDeclFilterWeights)) {
314 pdman.set3fv(fDecalUni, 1, decalFilterWeights);
315 std::copy_n(decalFilterWeights, 3, fPrevDeclFilterWeights);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000316 }
317}
318
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000319///////////////////////////////////////////////////////////////////////////////
Brian Salomon587e08f2017-01-27 10:59:27 -0500320
Brian Salomon7eabfe82019-12-02 14:20:20 -0500321std::unique_ptr<GrFragmentProcessor> GrDomainEffect::Make(std::unique_ptr<GrFragmentProcessor> fp,
322 const SkRect& domain,
323 GrTextureDomain::Mode mode,
324 bool decalIsFiltered) {
325 return Make(std::move(fp), domain, mode, mode, decalIsFiltered);
Michael Ludwigbe315a22018-12-17 09:50:51 -0500326}
327
Brian Salomon7eabfe82019-12-02 14:20:20 -0500328std::unique_ptr<GrFragmentProcessor> GrDomainEffect::Make(std::unique_ptr<GrFragmentProcessor> fp,
329 const SkRect& domain,
330 GrTextureDomain::Mode modeX,
331 GrTextureDomain::Mode modeY,
332 bool decalIsFiltered) {
333 if (modeX == GrTextureDomain::kIgnore_Mode && modeY == GrTextureDomain::kIgnore_Mode) {
334 return fp;
335 }
336 int count = 0;
337 GrCoordTransform* coordTransform = nullptr;
338 for (auto [transform, ignored] : GrFragmentProcessor::FPCoordTransformRange(*fp)) {
339 ++count;
340 coordTransform = &transform;
341 }
342 // If there are no coord transforms on the passed FP or it's children then there's no need to
343 // enforce a domain.
344 // We have a limitation that only one coord transform is support when overriding local coords.
345 // If that limit were relaxed we would need to add a coord transform for each descendent FP
346 // transform and possibly have multiple domain rects to account for different proxy
347 // normalization and y-reversals.
348 if (count != 1) {
349 return fp;
350 }
351 GrCoordTransform transformCopy = *coordTransform;
352 // Reset the child FP's coord transform.
353 *coordTransform = {};
Michael Ludwigbe315a22018-12-17 09:50:51 -0500354 // 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 -0500355 // entirely and return the original FP. We'd need a GrMatrixProcessor if the matrix is not
356 // identity, though.
357 return std::unique_ptr<GrFragmentProcessor>(new GrDomainEffect(
358 std::move(fp), transformCopy, domain, modeX, modeY, decalIsFiltered));
Robert Phillips40fd7c92017-01-30 08:06:27 -0500359}
360
Brian Salomon7eabfe82019-12-02 14:20:20 -0500361std::unique_ptr<GrFragmentProcessor> GrDomainEffect::Make(std::unique_ptr<GrFragmentProcessor> fp,
362 const SkRect& domain,
363 GrTextureDomain::Mode mode,
364 GrSamplerState::Filter filter) {
365 bool filterIfDecal = filter != GrSamplerState::Filter::kNearest;
366 return Make(std::move(fp), domain, mode, filterIfDecal);
367}
368
369std::unique_ptr<GrFragmentProcessor> GrDomainEffect::Make(std::unique_ptr<GrFragmentProcessor> fp,
370 const SkRect& domain,
371 GrTextureDomain::Mode modeX,
372 GrTextureDomain::Mode modeY,
373 GrSamplerState::Filter filter) {
374 bool filterIfDecal = filter != GrSamplerState::Filter::kNearest;
375 return Make(std::move(fp), domain, modeX, modeY, filterIfDecal);
376}
377GrFragmentProcessor::OptimizationFlags GrDomainEffect::Flags(GrFragmentProcessor* fp,
378 GrTextureDomain::Mode modeX,
379 GrTextureDomain::Mode modeY) {
380 auto fpFlags = GrFragmentProcessor::ProcessorOptimizationFlags(fp);
381 if (modeX == GrTextureDomain::kDecal_Mode || modeY == GrTextureDomain::kDecal_Mode) {
382 return fpFlags & ~kPreservesOpaqueInput_OptimizationFlag;
383 }
384 return fpFlags;
385}
386
387GrDomainEffect::GrDomainEffect(std::unique_ptr<GrFragmentProcessor> fp,
388 const GrCoordTransform& coordTransform,
389 const SkRect& domain,
390 GrTextureDomain::Mode modeX,
391 GrTextureDomain::Mode modeY,
392 bool decalIsFiltered)
393 : INHERITED(kGrDomainEffect_ClassID, Flags(fp.get(), modeX, modeY))
394 , fCoordTransform(coordTransform)
395 , fDomain(domain, modeX, modeY)
396 , fDecalIsFiltered(decalIsFiltered) {
397 SkASSERT(fp);
398 fp->setSampledWithExplicitCoords(true);
399 this->registerChildProcessor(std::move(fp));
Brian Salomon6cd51b52017-07-26 19:07:15 -0400400 this->addCoordTransform(&fCoordTransform);
Brian Salomon7eabfe82019-12-02 14:20:20 -0500401 if (fDomain.modeX() != GrTextureDomain::kDecal_Mode &&
402 fDomain.modeY() != GrTextureDomain::kDecal_Mode) {
403 // Canonicalize this don't care value so we don't have to worry about it elsewhere.
404 fDecalIsFiltered = false;
405 }
Robert Phillips40fd7c92017-01-30 08:06:27 -0500406}
407
Brian Salomon7eabfe82019-12-02 14:20:20 -0500408GrDomainEffect::GrDomainEffect(const GrDomainEffect& that)
409 : INHERITED(kGrDomainEffect_ClassID, that.optimizationFlags())
Brian Salomon3f6f9652017-07-28 07:34:05 -0400410 , fCoordTransform(that.fCoordTransform)
Brian Salomon7eabfe82019-12-02 14:20:20 -0500411 , fDomain(that.fDomain)
412 , fDecalIsFiltered(that.fDecalIsFiltered) {
413 auto child = that.childProcessor(0).clone();
414 child->setSampledWithExplicitCoords(true);
415 this->registerChildProcessor(std::move(child));
Brian Salomon3f6f9652017-07-28 07:34:05 -0400416 this->addCoordTransform(&fCoordTransform);
Brian Salomon3f6f9652017-07-28 07:34:05 -0400417}
418
Brian Salomon7eabfe82019-12-02 14:20:20 -0500419void GrDomainEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
420 GrProcessorKeyBuilder* b) const {
421 b->add32(GrTextureDomain::GLDomain::DomainKey(fDomain));
joshualitteb2a6762014-12-04 11:35:33 -0800422}
423
Brian Salomon7eabfe82019-12-02 14:20:20 -0500424GrGLSLFragmentProcessor* GrDomainEffect::onCreateGLSLInstance() const {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400425 class GLSLProcessor : public GrGLSLFragmentProcessor {
426 public:
427 void emitCode(EmitArgs& args) override {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500428 const GrDomainEffect& de = args.fFp.cast<GrDomainEffect>();
429 const GrTextureDomain& domain = de.fDomain;
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400430
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400431 SkString coords2D =
Brian Salomon7eabfe82019-12-02 14:20:20 -0500432 args.fFragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint);
Brian Osmanc4689632016-12-19 17:04:59 -0500433
Brian Salomon7eabfe82019-12-02 14:20:20 -0500434 fGLDomain.sampleProcessor(domain, args.fInputColor, args.fOutputColor, coords2D, this,
435 args, 0);
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400436 }
437
438 protected:
Brian Salomonab015ef2017-04-04 10:15:51 -0400439 void onSetData(const GrGLSLProgramDataManager& pdman,
440 const GrFragmentProcessor& fp) override {
Brian Salomon7eabfe82019-12-02 14:20:20 -0500441 const GrDomainEffect& de = fp.cast<GrDomainEffect>();
442 const GrTextureDomain& domain = de.fDomain;
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000443 // TODO: Update GrCoordTransform to return a view instead of proxy
444 const GrSurfaceProxy* proxy = de.fCoordTransform.proxy();
445 // If we don't have a proxy the value of the origin doesn't matter
446 GrSurfaceOrigin origin = proxy ? proxy->origin() : kTopLeft_GrSurfaceOrigin;
447 fGLDomain.setData(pdman, domain, proxy, origin, de.fDecalIsFiltered);
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400448 }
449
450 private:
451 GrTextureDomain::GLDomain fGLDomain;
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400452 };
453
454 return new GLSLProcessor;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000455}
456
Brian Salomon7eabfe82019-12-02 14:20:20 -0500457bool GrDomainEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
458 auto& td = sBase.cast<GrDomainEffect>();
459 return fDomain == td.fDomain && fDecalIsFiltered == td.fDecalIsFiltered;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000460}
461
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000462///////////////////////////////////////////////////////////////////////////////
463
Brian Salomon7eabfe82019-12-02 14:20:20 -0500464GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDomainEffect);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000465
Hal Canary6f6961e2017-01-31 13:50:44 -0500466#if GR_TEST_UTILS
Brian Salomon7eabfe82019-12-02 14:20:20 -0500467std::unique_ptr<GrFragmentProcessor> GrDomainEffect::TestCreate(GrProcessorTestData* d) {
468 do {
469 GrTextureDomain::Mode modeX =
470 (GrTextureDomain::Mode)d->fRandom->nextULessThan(GrTextureDomain::kModeCount);
471 GrTextureDomain::Mode modeY =
472 (GrTextureDomain::Mode)d->fRandom->nextULessThan(GrTextureDomain::kModeCount);
473 auto child = GrProcessorUnitTest::MakeChildFP(d);
474 const auto* childPtr = child.get();
475 SkRect domain;
476 // We assert if the child's coord transform has a proxy and the domain rect is outside its
477 // bounds.
478 GrFragmentProcessor::CoordTransformIter ctIter(*child);
479 if (!ctIter) {
480 continue;
481 }
482 auto [transform, fp] = *ctIter;
483 if (auto proxy = transform.proxy()) {
484 auto [w, h] = proxy->backingStoreDimensions();
485 domain.fLeft = d->fRandom->nextRangeScalar(0, w);
486 domain.fRight = d->fRandom->nextRangeScalar(0, w);
487 domain.fTop = d->fRandom->nextRangeScalar(0, h);
488 domain.fBottom = d->fRandom->nextRangeScalar(0, h);
489 } else {
490 domain.fLeft = d->fRandom->nextRangeScalar(-100.f, 100.f);
491 domain.fRight = d->fRandom->nextRangeScalar(-100.f, 100.f);
492 domain.fTop = d->fRandom->nextRangeScalar(-100.f, 100.f);
493 domain.fBottom = d->fRandom->nextRangeScalar(-100.f, 100.f);
494 }
495 domain.sort();
496 bool filterIfDecal = d->fRandom->nextBool();
497 auto result = GrDomainEffect::Make(std::move(child), domain, modeX, modeY, filterIfDecal);
498 if (result && result.get() != childPtr) {
499 return result;
500 }
501 } while (true);
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400502}
Hal Canary6f6961e2017-01-31 13:50:44 -0500503#endif
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400504
505///////////////////////////////////////////////////////////////////////////////
Brian Salomonaff329b2017-08-11 09:40:37 -0400506std::unique_ptr<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::Make(
Michael Ludwig8fa469d2019-11-25 16:08:44 -0500507 sk_sp<GrSurfaceProxy> proxy, const SkIRect& subset, const SkIPoint& deviceSpaceOffset) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400508 return std::unique_ptr<GrFragmentProcessor>(new GrDeviceSpaceTextureDecalFragmentProcessor(
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400509 std::move(proxy), subset, deviceSpaceOffset));
Robert Phillips40fd7c92017-01-30 08:06:27 -0500510}
511
512GrDeviceSpaceTextureDecalFragmentProcessor::GrDeviceSpaceTextureDecalFragmentProcessor(
Michael Ludwig8fa469d2019-11-25 16:08:44 -0500513 sk_sp<GrSurfaceProxy> proxy, const SkIRect& subset, const SkIPoint& deviceSpaceOffset)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400514 : INHERITED(kGrDeviceSpaceTextureDecalFragmentProcessor_ClassID,
515 kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400516 , fTextureSampler(proxy, GrSamplerState::ClampNearest())
Michael Ludwigbe315a22018-12-17 09:50:51 -0500517 , fTextureDomain(proxy.get(),
518 GrTextureDomain::MakeTexelDomain(subset, GrTextureDomain::kDecal_Mode),
519 GrTextureDomain::kDecal_Mode, GrTextureDomain::kDecal_Mode) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400520 this->setTextureSamplerCnt(1);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500521 fDeviceSpaceOffset.fX = deviceSpaceOffset.fX - subset.fLeft;
522 fDeviceSpaceOffset.fY = deviceSpaceOffset.fY - subset.fTop;
Robert Phillips40fd7c92017-01-30 08:06:27 -0500523}
524
Brian Salomon1a2a7ab2017-07-26 13:11:51 -0400525GrDeviceSpaceTextureDecalFragmentProcessor::GrDeviceSpaceTextureDecalFragmentProcessor(
526 const GrDeviceSpaceTextureDecalFragmentProcessor& that)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400527 : INHERITED(kGrDeviceSpaceTextureDecalFragmentProcessor_ClassID,
528 kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Brian Salomon1a2a7ab2017-07-26 13:11:51 -0400529 , fTextureSampler(that.fTextureSampler)
530 , fTextureDomain(that.fTextureDomain)
531 , fDeviceSpaceOffset(that.fDeviceSpaceOffset) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400532 this->setTextureSamplerCnt(1);
Brian Salomon1a2a7ab2017-07-26 13:11:51 -0400533}
534
Brian Salomonaff329b2017-08-11 09:40:37 -0400535std::unique_ptr<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::clone() const {
536 return std::unique_ptr<GrFragmentProcessor>(
537 new GrDeviceSpaceTextureDecalFragmentProcessor(*this));
Brian Salomon1a2a7ab2017-07-26 13:11:51 -0400538}
539
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400540GrGLSLFragmentProcessor* GrDeviceSpaceTextureDecalFragmentProcessor::onCreateGLSLInstance() const {
541 class GLSLProcessor : public GrGLSLFragmentProcessor {
542 public:
543 void emitCode(EmitArgs& args) override {
544 const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp =
545 args.fFp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>();
546 const char* scaleAndTranslateName;
547 fScaleAndTranslateUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400548 kHalf4_GrSLType,
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400549 "scaleAndTranslate",
550 &scaleAndTranslateName);
Ethan Nicholase1f55022019-02-05 17:17:40 -0500551 args.fFragBuilder->codeAppendf("half2 coords = half2(sk_FragCoord.xy * %s.xy + %s.zw);",
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400552 scaleAndTranslateName, scaleAndTranslateName);
553 fGLDomain.sampleTexture(args.fFragBuilder,
554 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500555 args.fShaderCaps,
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400556 dstdfp.fTextureDomain,
557 args.fOutputColor,
558 SkString("coords"),
559 args.fTexSamplers[0],
560 args.fInputColor);
561 }
562
563 protected:
Brian Salomonab015ef2017-04-04 10:15:51 -0400564 void onSetData(const GrGLSLProgramDataManager& pdman,
565 const GrFragmentProcessor& fp) override {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400566 const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp =
567 fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>();
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000568 const auto& view = dstdfp.textureSampler(0).view();
569 SkISize textureDims = view.proxy()->backingStoreDimensions();
Robert Phillips9bee2e52017-05-29 12:37:20 -0400570
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000571 fGLDomain.setData(pdman, dstdfp.fTextureDomain, view,
Michael Ludwigbe315a22018-12-17 09:50:51 -0500572 dstdfp.textureSampler(0).samplerState());
Michael Ludwig8fa469d2019-11-25 16:08:44 -0500573 float iw = 1.f / textureDims.width();
574 float ih = 1.f / textureDims.height();
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400575 float scaleAndTransData[4] = {
576 iw, ih,
577 -dstdfp.fDeviceSpaceOffset.fX * iw, -dstdfp.fDeviceSpaceOffset.fY * ih
578 };
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000579 if (view.origin() == kBottomLeft_GrSurfaceOrigin) {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400580 scaleAndTransData[1] = -scaleAndTransData[1];
581 scaleAndTransData[3] = 1 - scaleAndTransData[3];
582 }
583 pdman.set4fv(fScaleAndTranslateUni, 1, scaleAndTransData);
584 }
585
586 private:
587 GrTextureDomain::GLDomain fGLDomain;
588 UniformHandle fScaleAndTranslateUni;
589 };
590
591 return new GLSLProcessor;
592}
593
594bool GrDeviceSpaceTextureDecalFragmentProcessor::onIsEqual(const GrFragmentProcessor& fp) const {
595 const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp =
596 fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>();
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000597 return dstdfp.fTextureSampler.view().proxy()->underlyingUniqueID() ==
598 fTextureSampler.view().proxy()->underlyingUniqueID() &&
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400599 dstdfp.fDeviceSpaceOffset == fDeviceSpaceOffset &&
600 dstdfp.fTextureDomain == fTextureDomain;
601}
602
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400603///////////////////////////////////////////////////////////////////////////////
604
605GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDeviceSpaceTextureDecalFragmentProcessor);
606
Hal Canary6f6961e2017-01-31 13:50:44 -0500607#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400608std::unique_ptr<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::TestCreate(
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400609 GrProcessorTestData* d) {
610 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
611 : GrProcessorUnitTest::kAlphaTextureIdx;
Robert Phillips40fd7c92017-01-30 08:06:27 -0500612 sk_sp<GrTextureProxy> proxy = d->textureProxy(texIdx);
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400613 SkIRect subset;
Robert Phillips40fd7c92017-01-30 08:06:27 -0500614 subset.fLeft = d->fRandom->nextULessThan(proxy->width() - 1);
615 subset.fRight = d->fRandom->nextRangeU(subset.fLeft, proxy->width());
616 subset.fTop = d->fRandom->nextULessThan(proxy->height() - 1);
617 subset.fBottom = d->fRandom->nextRangeU(subset.fTop, proxy->height());
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400618 SkIPoint pt;
619 pt.fX = d->fRandom->nextULessThan(2048);
620 pt.fY = d->fRandom->nextULessThan(2048);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400621 return GrDeviceSpaceTextureDecalFragmentProcessor::Make(std::move(proxy), subset, pt);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000622}
Hal Canary6f6961e2017-01-31 13:50:44 -0500623#endif