blob: 68196771738fae6f0957c4da407d8b52c13b6c75 [file] [log] [blame]
Brian Salomonb8f098d2020-01-07 11:15:44 -05001/*
2 * Copyright 2017 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
8#include "src/gpu/effects/GrTextureEffect.h"
9
Greg Daniel456f9b52020-03-05 19:14:18 +000010#include "src/gpu/GrTexture.h"
Brian Salomonca6b2f42020-01-24 11:31:21 -050011#include "src/gpu/GrTexturePriv.h"
Brian Osman273944a2020-05-29 16:44:26 -040012#include "src/gpu/effects/GrMatrixEffect.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -050013#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
14#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
15#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
16#include "src/sksl/SkSLCPP.h"
17#include "src/sksl/SkSLUtil.h"
18
Brian Salomon53c909e2020-02-13 13:54:24 -050019using Mode = GrSamplerState::WrapMode;
20using Filter = GrSamplerState::Filter;
Brian Salomonca6b2f42020-01-24 11:31:21 -050021
Brian Salomond71548a2020-02-29 19:43:30 -050022struct GrTextureEffect::Sampling {
23 GrSamplerState fHWSampler;
24 ShaderMode fShaderModes[2] = {ShaderMode::kNone, ShaderMode::kNone};
25 SkRect fShaderSubset = {0, 0, 0, 0};
26 SkRect fShaderClamp = {0, 0, 0, 0};
27 float fBorder[4] = {0, 0, 0, 0};
28 Sampling(GrSamplerState::Filter filter) : fHWSampler(filter) {}
29 Sampling(const GrSurfaceProxy& proxy,
30 GrSamplerState sampler,
31 const SkRect&,
32 const SkRect*,
33 const float border[4],
34 const GrCaps&);
35 inline bool hasBorderAlpha() const;
36};
37
Brian Salomonca6b2f42020-01-24 11:31:21 -050038GrTextureEffect::Sampling::Sampling(const GrSurfaceProxy& proxy,
39 GrSamplerState sampler,
40 const SkRect& subset,
Brian Salomonca6b2f42020-01-24 11:31:21 -050041 const SkRect* domain,
Brian Salomond71548a2020-02-29 19:43:30 -050042 const float border[4],
Brian Salomonca6b2f42020-01-24 11:31:21 -050043 const GrCaps& caps) {
Brian Salomon53c909e2020-02-13 13:54:24 -050044 struct Span {
45 float fA = 0.f, fB = 0.f;
Brian Salomonca6b2f42020-01-24 11:31:21 -050046
Brian Salomon53c909e2020-02-13 13:54:24 -050047 Span makeInset(float o) const {
48 Span r = {fA + o, fB - o};
49 if (r.fA > r.fB) {
50 r.fA = r.fB = (r.fA + r.fB) / 2;
51 }
Brian Salomonca6b2f42020-01-24 11:31:21 -050052 return r;
53 }
54
Brian Salomon53c909e2020-02-13 13:54:24 -050055 bool contains(Span r) const { return fA <= r.fA && fB >= r.fB; }
56 };
57 struct Result1D {
58 ShaderMode fShaderMode;
59 Span fShaderSubset;
60 Span fShaderClamp;
61 Mode fHWMode;
62 };
63
64 auto type = proxy.asTextureProxy()->textureType();
65 auto filter = sampler.filter();
66
Brian Salomond71548a2020-02-29 19:43:30 -050067 auto resolve = [type, &caps, filter, &border](int size, Mode mode, Span subset, Span domain) {
Brian Salomon53c909e2020-02-13 13:54:24 -050068 Result1D r;
Brian Salomond71548a2020-02-29 19:43:30 -050069 bool canDoModeInHW = true;
70 // TODO: Use HW border color when available.
71 if (mode == Mode::kClampToBorder &&
72 (!caps.clampToBorderSupport() || border[0] || border[1] || border[2] || border[3])) {
73 canDoModeInHW = false;
74 } else if (mode != Mode::kClamp && !caps.npotTextureTileSupport() && !SkIsPow2(size)) {
75 canDoModeInHW = false;
76 } else if (type != GrTextureType::k2D &&
77 !(mode == Mode::kClamp || mode == Mode::kClampToBorder)) {
78 canDoModeInHW = false;
79 }
80 if (canDoModeInHW && size > 0 && subset.fA <= 0 && subset.fB >= size) {
Brian Salomon53c909e2020-02-13 13:54:24 -050081 r.fShaderMode = ShaderMode::kNone;
82 r.fHWMode = mode;
83 r.fShaderSubset = r.fShaderClamp = {0, 0};
84 return r;
85 }
86
87 r.fShaderSubset = subset;
Brian Salomoned729f92020-02-05 12:15:18 -050088 bool domainIsSafe = false;
Brian Salomoned729f92020-02-05 12:15:18 -050089 if (filter == Filter::kNearest) {
90 Span isubset{sk_float_floor(subset.fA), sk_float_ceil(subset.fB)};
91 if (domain.fA > isubset.fA && domain.fB < isubset.fB) {
92 domainIsSafe = true;
93 }
Brian Salomon53c909e2020-02-13 13:54:24 -050094 // This inset prevents sampling neighboring texels that could occur when
95 // texture coords fall exactly at texel boundaries (depending on precision
96 // and GPU-specific snapping at the boundary).
97 r.fShaderClamp = isubset.makeInset(0.5f);
98 } else {
99 r.fShaderClamp = subset.makeInset(0.5f);
100 if (r.fShaderClamp.contains(domain)) {
101 domainIsSafe = true;
Brian Salomoned729f92020-02-05 12:15:18 -0500102 }
Brian Salomoned729f92020-02-05 12:15:18 -0500103 }
Brian Salomon53c909e2020-02-13 13:54:24 -0500104 if (domainIsSafe) {
105 // The domain of coords that will be used won't access texels outside of the subset.
106 // So the wrap mode effectively doesn't matter. We use kClamp since it is always
107 // supported.
Brian Salomonca6b2f42020-01-24 11:31:21 -0500108 r.fShaderMode = ShaderMode::kNone;
Brian Salomon53c909e2020-02-13 13:54:24 -0500109 r.fHWMode = Mode::kClamp;
110 r.fShaderSubset = r.fShaderClamp = {0, 0};
Brian Salomonca6b2f42020-01-24 11:31:21 -0500111 return r;
112 }
Brian Salomonca6b2f42020-01-24 11:31:21 -0500113 r.fShaderMode = static_cast<ShaderMode>(mode);
114 r.fHWMode = Mode::kClamp;
115 return r;
116 };
117
118 SkISize dim = proxy.isFullyLazy() ? SkISize{-1, -1} : proxy.backingStoreDimensions();
119
120 Span subsetX{subset.fLeft, subset.fRight};
121 auto domainX = domain ? Span{domain->fLeft, domain->fRight}
122 : Span{SK_FloatNegativeInfinity, SK_FloatInfinity};
123 auto x = resolve(dim.width(), sampler.wrapModeX(), subsetX, domainX);
124
125 Span subsetY{subset.fTop, subset.fBottom};
126 auto domainY = domain ? Span{domain->fTop, domain->fBottom}
127 : Span{SK_FloatNegativeInfinity, SK_FloatInfinity};
128 auto y = resolve(dim.height(), sampler.wrapModeY(), subsetY, domainY);
129
Brian Salomon53c909e2020-02-13 13:54:24 -0500130 fHWSampler = {x.fHWMode, y.fHWMode, filter};
Brian Salomonca6b2f42020-01-24 11:31:21 -0500131 fShaderModes[0] = x.fShaderMode;
132 fShaderModes[1] = y.fShaderMode;
133 fShaderSubset = {x.fShaderSubset.fA, y.fShaderSubset.fA,
134 x.fShaderSubset.fB, y.fShaderSubset.fB};
Brian Salomon53c909e2020-02-13 13:54:24 -0500135 fShaderClamp = {x.fShaderClamp.fA, y.fShaderClamp.fA,
136 x.fShaderClamp.fB, y.fShaderClamp.fB};
Brian Salomond71548a2020-02-29 19:43:30 -0500137 std::copy_n(border, 4, fBorder);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500138}
139
Brian Salomond71548a2020-02-29 19:43:30 -0500140bool GrTextureEffect::Sampling::hasBorderAlpha() const {
141 if (fHWSampler.wrapModeX() == GrSamplerState::WrapMode::kClampToBorder ||
142 fHWSampler.wrapModeY() == GrSamplerState::WrapMode::kClampToBorder) {
143 return true;
144 }
145 if (fShaderModes[0] == ShaderMode::kClampToBorder ||
146 fShaderModes[1] == ShaderMode::kClampToBorder) {
147 return fBorder[3] < 1.f;
148 }
149 return false;
Brian Salomonca6b2f42020-01-24 11:31:21 -0500150}
151
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400152static void get_matrix(const SkMatrix& preMatrix, const GrSurfaceProxyView& view,
153 SkMatrix* outMatrix, bool* outLazyProxyNormalization) {
154 SkMatrix combined = preMatrix;
155 bool normalize = view.proxy()->backendFormat().textureType() != GrTextureType::kRectangle;
156 if (normalize) {
157 if (view.proxy()->isFullyLazy()) {
158 *outLazyProxyNormalization = true;
159 } else {
160 SkMatrixPriv::PostIDiv(&combined, view.proxy()->backingStoreDimensions().fWidth,
161 view.proxy()->backingStoreDimensions().fHeight);
162 *outLazyProxyNormalization = false;
163 }
164 } else {
165 *outLazyProxyNormalization = false;
166 }
167 if (view.origin() == kBottomLeft_GrSurfaceOrigin) {
168 if (normalize) {
169 // combined.postScale(1,-1);
170 // combined.postTranslate(0,1);
171 combined.set(SkMatrix::kMSkewY,
172 combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]);
173 combined.set(SkMatrix::kMScaleY,
174 combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
175 combined.set(SkMatrix::kMTransY,
176 combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
177 } else {
178 // combined.postScale(1, -1);
179 // combined.postTranslate(0,1);
180 SkScalar h = view.proxy()->backingStoreDimensions().fHeight;
181 combined.set(SkMatrix::kMSkewY,
182 h * combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]);
183 combined.set(SkMatrix::kMScaleY,
184 h * combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
185 combined.set(SkMatrix::kMTransY,
186 h * combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
187 }
188 }
189 *outMatrix = combined;
190}
191
Greg Danield2ccbb52020-02-05 10:45:39 -0500192std::unique_ptr<GrFragmentProcessor> GrTextureEffect::Make(GrSurfaceProxyView view,
Brian Salomonb8f098d2020-01-07 11:15:44 -0500193 SkAlphaType alphaType,
194 const SkMatrix& matrix,
Brian Salomon53c909e2020-02-13 13:54:24 -0500195 Filter filter) {
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400196 SkMatrix final;
197 bool lazyProxyNormalization;
198 get_matrix(matrix, view, &final, &lazyProxyNormalization);
Brian Osman273944a2020-05-29 16:44:26 -0400199 return GrMatrixEffect::Make(final, std::unique_ptr<GrFragmentProcessor>(
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400200 new GrTextureEffect(std::move(view),
201 alphaType,
202 Sampling(filter),
203 lazyProxyNormalization)));
Brian Salomonca6b2f42020-01-24 11:31:21 -0500204}
205
Greg Danield2ccbb52020-02-05 10:45:39 -0500206std::unique_ptr<GrFragmentProcessor> GrTextureEffect::Make(GrSurfaceProxyView view,
Brian Salomonca6b2f42020-01-24 11:31:21 -0500207 SkAlphaType alphaType,
208 const SkMatrix& matrix,
209 GrSamplerState sampler,
Brian Salomond71548a2020-02-29 19:43:30 -0500210 const GrCaps& caps,
211 const float border[4]) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500212 Sampling sampling(*view.proxy(), sampler, SkRect::Make(view.proxy()->dimensions()), nullptr,
Brian Salomond71548a2020-02-29 19:43:30 -0500213 border, caps);
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400214 SkMatrix final;
215 bool lazyProxyNormalization;
216 get_matrix(matrix, view, &final, &lazyProxyNormalization);
Brian Osman273944a2020-05-29 16:44:26 -0400217 return GrMatrixEffect::Make(final, std::unique_ptr<GrFragmentProcessor>(
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400218 new GrTextureEffect(std::move(view),
219 alphaType,
220 sampling,
221 lazyProxyNormalization)));
Brian Salomonca6b2f42020-01-24 11:31:21 -0500222}
223
Greg Danield2ccbb52020-02-05 10:45:39 -0500224std::unique_ptr<GrFragmentProcessor> GrTextureEffect::MakeSubset(GrSurfaceProxyView view,
Brian Salomonca6b2f42020-01-24 11:31:21 -0500225 SkAlphaType alphaType,
226 const SkMatrix& matrix,
227 GrSamplerState sampler,
228 const SkRect& subset,
Brian Salomond71548a2020-02-29 19:43:30 -0500229 const GrCaps& caps,
230 const float border[4]) {
231 Sampling sampling(*view.proxy(), sampler, subset, nullptr, border, caps);
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400232 SkMatrix final;
233 bool lazyProxyNormalization;
234 get_matrix(matrix, view, &final, &lazyProxyNormalization);
Brian Osman273944a2020-05-29 16:44:26 -0400235 return GrMatrixEffect::Make(final, std::unique_ptr<GrFragmentProcessor>(
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400236 new GrTextureEffect(std::move(view),
237 alphaType,
238 sampling,
239 lazyProxyNormalization)));
Brian Salomonca6b2f42020-01-24 11:31:21 -0500240}
241
Greg Danield2ccbb52020-02-05 10:45:39 -0500242std::unique_ptr<GrFragmentProcessor> GrTextureEffect::MakeSubset(GrSurfaceProxyView view,
Brian Salomonca6b2f42020-01-24 11:31:21 -0500243 SkAlphaType alphaType,
244 const SkMatrix& matrix,
245 GrSamplerState sampler,
246 const SkRect& subset,
247 const SkRect& domain,
Brian Salomond71548a2020-02-29 19:43:30 -0500248 const GrCaps& caps,
249 const float border[4]) {
250 Sampling sampling(*view.proxy(), sampler, subset, &domain, border, caps);
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400251 SkMatrix final;
252 bool lazyProxyNormalization;
253 get_matrix(matrix, view, &final, &lazyProxyNormalization);
Brian Osman273944a2020-05-29 16:44:26 -0400254 return GrMatrixEffect::Make(final, std::unique_ptr<GrFragmentProcessor>(
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400255 new GrTextureEffect(std::move(view),
256 alphaType,
257 sampling,
258 lazyProxyNormalization)));
Brian Salomonb8f098d2020-01-07 11:15:44 -0500259}
260
Brian Salomon53c909e2020-02-13 13:54:24 -0500261GrTextureEffect::FilterLogic GrTextureEffect::GetFilterLogic(ShaderMode mode,
262 GrSamplerState::Filter filter) {
263 switch (mode) {
264 case ShaderMode::kMirrorRepeat:
265 case ShaderMode::kNone:
266 case ShaderMode::kClamp:
267 return FilterLogic::kNone;
268 case ShaderMode::kRepeat:
269 switch (filter) {
270 case GrSamplerState::Filter::kNearest:
271 return FilterLogic::kNone;
272 case GrSamplerState::Filter::kBilerp:
273 return FilterLogic::kRepeatBilerp;
274 case GrSamplerState::Filter::kMipMap:
Brian Salomonf46f19b2020-02-13 14:11:28 -0500275 return FilterLogic::kRepeatMipMap;
Brian Salomon53c909e2020-02-13 13:54:24 -0500276 }
277 SkUNREACHABLE;
Brian Salomond71548a2020-02-29 19:43:30 -0500278 case ShaderMode::kClampToBorder:
279 return filter > GrSamplerState::Filter::kNearest ? FilterLogic::kClampToBorderFilter
280 : FilterLogic::kClampToBorderNearest;
Brian Salomon53c909e2020-02-13 13:54:24 -0500281 }
282 SkUNREACHABLE;
283}
284
Brian Salomonb8f098d2020-01-07 11:15:44 -0500285GrGLSLFragmentProcessor* GrTextureEffect::onCreateGLSLInstance() const {
286 class Impl : public GrGLSLFragmentProcessor {
Brian Salomonca6b2f42020-01-24 11:31:21 -0500287 UniformHandle fSubsetUni;
Brian Salomon53c909e2020-02-13 13:54:24 -0500288 UniformHandle fClampUni;
289 UniformHandle fNormUni;
Brian Salomond71548a2020-02-29 19:43:30 -0500290 UniformHandle fBorderUni;
Brian Salomonca6b2f42020-01-24 11:31:21 -0500291
Brian Salomonb8f098d2020-01-07 11:15:44 -0500292 public:
293 void emitCode(EmitArgs& args) override {
Ethan Nicholas94996eb2020-04-01 13:46:10 -0400294 auto& te = args.fFp.cast<GrTextureEffect>();
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400295 SkString coords;
296 if (args.fFp.isSampledWithExplicitCoords()) {
297 coords = "_coords";
298 } else {
299 coords = args.fTransformedCoords[0].fVaryingPoint.c_str();
300 }
Brian Salomonb8f098d2020-01-07 11:15:44 -0500301 auto* fb = args.fFragBuilder;
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400302 if (te.sampleMatrix().fKind == SkSL::SampleMatrix::Kind::kMixed) {
Michael Ludwig89dd4e72020-06-03 15:39:06 -0400303 // FIXME this is very similar to the extra logic in
304 // GrGLSLFragmentShaderBuilder::ensureCoords2D
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400305 args.fUniformHandler->writeUniformMappings(te.sampleMatrix().fOwner, fb);
Michael Ludwig89dd4e72020-06-03 15:39:06 -0400306 SkString coords2D;
307 coords2D.printf("%s_teSample", coords.c_str());
308
309 fb->codeAppendf("float3 %s_3d = %s * _matrix * %s.xy1;\n",
310 coords2D.c_str(), te.sampleMatrix().fExpression.c_str(),
311 coords.c_str());
312 fb->codeAppendf("float2 %s = %s_3d.xy / %s_3d.z;\n",
313 coords2D.c_str(), coords2D.c_str(), coords2D.c_str());
314 coords = coords2D;
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400315 }
Brian Salomonca6b2f42020-01-24 11:31:21 -0500316 if (te.fShaderModes[0] == ShaderMode::kNone &&
317 te.fShaderModes[1] == ShaderMode::kNone) {
Ethan Nicholas36a3e012020-04-17 19:00:28 +0000318 fb->codeAppendf("%s = ", args.fOutputColor);
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400319 if (te.fLazyProxyNormalization) {
320 const char* norm = nullptr;
321 fNormUni = args.fUniformHandler->addUniform(&te, kFragment_GrShaderFlag,
322 kFloat4_GrSLType, "norm", &norm);
323 fb->appendTextureLookupAndBlend(args.fInputColor, SkBlendMode::kModulate,
324 args.fTexSamplers[0],
325 SkStringPrintf("%s * %s.zw", coords.c_str(),
326 norm).c_str());
327 } else {
328 fb->appendTextureLookupAndBlend(args.fInputColor, SkBlendMode::kModulate,
329 args.fTexSamplers[0], coords.c_str());
330 }
Brian Salomonca6b2f42020-01-24 11:31:21 -0500331 fb->codeAppendf(";");
332 } else {
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400333 // Tripping this assert means we have a normalized fully lazy proxy with a
334 // non-default ShaderMode. There's nothing fundamentally wrong with doing that, but
335 // it hasn't been tested and this code path probably won't handle normalization
336 // properly in that case.
337 SkASSERT(!te.fLazyProxyNormalization);
Brian Salomon53c909e2020-02-13 13:54:24 -0500338 // Here is the basic flow of the various ShaderModes are implemented in a series of
339 // steps. Not all the steps apply to all the modes. We try to emit only the steps
340 // that are necessary for the given x/y shader modes.
341 //
342 // 0) Start with interpolated coordinates (unnormalize if doing anything
343 // complicated).
344 // 1) Map the coordinates into the subset range [Repeat and MirrorRepeat], or pass
345 // through output of 0).
346 // 2) Clamp the coordinates to a 0.5 inset of the subset rect [Clamp, Repeat, and
Brian Salomond71548a2020-02-29 19:43:30 -0500347 // MirrorRepeat always or ClampToBorder only when filtering] or pass through
348 // output of 1). The clamp rect collapses to a line or point it if the subset
349 // rect is less than one pixel wide/tall.
Brian Salomon53c909e2020-02-13 13:54:24 -0500350 // 3) Look up texture with output of 2) [All]
351 // 3) Use the difference between 1) and 2) to apply filtering at edge [Repeat or
Brian Salomond71548a2020-02-29 19:43:30 -0500352 // ClampToBorder]. In the Repeat case this requires extra texture lookups on the
353 // other side of the subset (up to 3 more reads). Or if ClampToBorder and not
354 // filtering do a hard less than/greater than test with the subset rect.
Brian Salomonca6b2f42020-01-24 11:31:21 -0500355
Brian Salomon53c909e2020-02-13 13:54:24 -0500356 // Convert possible projective texture coordinates into non-homogeneous half2.
Brian Salomon66356c22020-02-11 15:22:18 -0500357 fb->codeAppendf(
358 "float2 inCoord = %s;",
Ethan Nicholas58430122020-04-14 09:54:02 -0400359 fb->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint,
360 te.sampleMatrix()).c_str());
Brian Salomon66356c22020-02-11 15:22:18 -0500361
Brian Salomon53c909e2020-02-13 13:54:24 -0500362 const auto& m = te.fShaderModes;
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500363 GrTextureType textureType = te.fSampler.proxy()->backendFormat().textureType();
364 bool normCoords = textureType != GrTextureType::kRectangle;
Brian Salomon53c909e2020-02-13 13:54:24 -0500365 auto filter = te.fSampler.samplerState().filter();
366 FilterLogic filterLogic[2] = {GetFilterLogic(m[0], filter),
367 GetFilterLogic(m[1], filter)};
368
Brian Salomond71548a2020-02-29 19:43:30 -0500369 const char* borderName = nullptr;
370 if (te.fShaderModes[0] == ShaderMode::kClampToBorder ||
371 te.fShaderModes[1] == ShaderMode::kClampToBorder) {
372 fBorderUni = args.fUniformHandler->addUniform(
Ethan Nicholas16464c32020-04-06 13:53:05 -0400373 &te, kFragment_GrShaderFlag, kHalf4_GrSLType, "border", &borderName);
Brian Salomond71548a2020-02-29 19:43:30 -0500374 }
Brian Salomon53c909e2020-02-13 13:54:24 -0500375 auto modeUsesSubset = [](ShaderMode m) {
376 return m == ShaderMode::kRepeat || m == ShaderMode::kMirrorRepeat ||
Brian Salomond71548a2020-02-29 19:43:30 -0500377 m == ShaderMode::kClampToBorder;
Brian Salomon53c909e2020-02-13 13:54:24 -0500378 };
379
380 auto modeUsesClamp = [filter](ShaderMode m) {
381 return m != ShaderMode::kNone &&
Brian Salomond71548a2020-02-29 19:43:30 -0500382 (m != ShaderMode::kClampToBorder || filter > Filter::kNearest);
Brian Salomon53c909e2020-02-13 13:54:24 -0500383 };
384
385 bool useSubset[2] = {modeUsesSubset(m[0]), modeUsesSubset(m[1])};
386 bool useClamp [2] = {modeUsesClamp (m[0]), modeUsesClamp (m[1])};
387
388 const char* subsetName = nullptr;
389 if (useSubset[0] || useSubset[1]) {
390 fSubsetUni = args.fUniformHandler->addUniform(
Ethan Nicholas16464c32020-04-06 13:53:05 -0400391 &te, kFragment_GrShaderFlag, kFloat4_GrSLType, "subset", &subsetName);
Brian Salomon53c909e2020-02-13 13:54:24 -0500392 }
393
394 const char* clampName = nullptr;
395 if (useClamp[0] || useClamp[1]) {
396 fClampUni = args.fUniformHandler->addUniform(
Ethan Nicholas16464c32020-04-06 13:53:05 -0400397 &te, kFragment_GrShaderFlag, kFloat4_GrSLType, "clamp", &clampName);
Brian Salomon53c909e2020-02-13 13:54:24 -0500398 }
399
400 // To keep things a little simpler, when we have filtering logic in the shader we
401 // operate on unnormalized texture coordinates. We add a uniform that stores
402 // {w, h, 1/w, 1/h} in a float4.
403 const char* norm = nullptr;
404 if (normCoords && (filterLogic[0] != FilterLogic::kNone ||
405 filterLogic[1] != FilterLogic::kNone)) {
406 // TODO: Detect support for textureSize() or polyfill textureSize() in SkSL and
407 // always use?
Ethan Nicholas16464c32020-04-06 13:53:05 -0400408 fNormUni = args.fUniformHandler->addUniform(&te, kFragment_GrShaderFlag,
Brian Salomon53c909e2020-02-13 13:54:24 -0500409 kFloat4_GrSLType, "norm", &norm);
410 // TODO: Remove the normalization from the CoordTransform to skip unnormalizing
411 // step here.
412 fb->codeAppendf("inCoord *= %s.xy;", norm);
413 }
414
415 // Generates a string to read at a coordinate, normalizing coords if necessary.
Brian Salomonf46f19b2020-02-13 14:11:28 -0500416 auto read = [&](const char* coord) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500417 SkString result;
418 SkString normCoord;
419 if (norm) {
420 normCoord.printf("(%s) * %s.zw", coord, norm);
421 } else {
422 normCoord = coord;
423 }
Brian Salomonf46f19b2020-02-13 14:11:28 -0500424 fb->appendTextureLookup(&result, args.fTexSamplers[0], normCoord.c_str());
Brian Salomon53c909e2020-02-13 13:54:24 -0500425 return result;
426 };
427
428 // Implements coord wrapping for kRepeat and kMirrorRepeat
Brian Salomonf46f19b2020-02-13 14:11:28 -0500429 auto subsetCoord = [&](ShaderMode mode,
430 const char* coordSwizzle,
431 const char* subsetStartSwizzle,
432 const char* subsetStopSwizzle,
433 const char* extraCoord,
434 const char* coordWeight) {
Brian Salomon66356c22020-02-11 15:22:18 -0500435 switch (mode) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500436 // These modes either don't use the subset rect or don't need to map the
437 // coords to be within the subset.
Brian Salomon66356c22020-02-11 15:22:18 -0500438 case ShaderMode::kNone:
Brian Salomond71548a2020-02-29 19:43:30 -0500439 case ShaderMode::kClampToBorder:
Brian Salomon66356c22020-02-11 15:22:18 -0500440 case ShaderMode::kClamp:
Brian Salomon53c909e2020-02-13 13:54:24 -0500441 fb->codeAppendf("subsetCoord.%s = inCoord.%s;", coordSwizzle,
442 coordSwizzle);
Brian Salomon66356c22020-02-11 15:22:18 -0500443 break;
444 case ShaderMode::kRepeat:
Brian Salomonf46f19b2020-02-13 14:11:28 -0500445 if (filter == Filter::kMipMap) {
446 // The approach here is to generate two sets of texture coords that
447 // are both "moving" at the same speed (if not direction) as
448 // inCoords. We accomplish that by using two out of phase mirror
449 // repeat coords. We will always sample using both coords but the
450 // read from the upward sloping one is selected using a weight
451 // that transitions from one set to the other near the reflection
452 // point. Like the coords, the weight is a saw-tooth function,
453 // phase-shifted, vertically translated, and then clamped to 0..1.
454 // TODO: Skip this and use textureGrad() when available.
455 SkASSERT(extraCoord);
456 SkASSERT(coordWeight);
457 fb->codeAppend("{");
458 fb->codeAppendf("float w = %s.%s - %s.%s;", subsetName,
459 subsetStopSwizzle, subsetName, subsetStartSwizzle);
460 fb->codeAppendf("float w2 = 2 * w;");
461 fb->codeAppendf("float d = inCoord.%s - %s.%s;", coordSwizzle,
462 subsetName, subsetStartSwizzle);
463 fb->codeAppend("float m = mod(d, w2);");
464 fb->codeAppend("float o = mix(m, w2 - m, step(w, m));");
465 fb->codeAppendf("subsetCoord.%s = o + %s.%s;", coordSwizzle,
466 subsetName, subsetStartSwizzle);
467 fb->codeAppendf("%s = w - o + %s.%s;", extraCoord, subsetName,
468 subsetStartSwizzle);
469 // coordWeight is used as the third param of mix() to blend between a
470 // sample taken using subsetCoord and a sample at extraCoord.
471 fb->codeAppend("float hw = w/2;");
472 fb->codeAppend("float n = mod(d - hw, w2);");
473 fb->codeAppendf(
474 "%s = saturate(half(mix(n, w2 - n, step(w, n)) - hw + "
475 "0.5));",
476 coordWeight);
477 fb->codeAppend("}");
478 } else {
479 fb->codeAppendf(
480 "subsetCoord.%s = mod(inCoord.%s - %s.%s, %s.%s - %s.%s) + "
481 "%s.%s;",
482 coordSwizzle, coordSwizzle, subsetName, subsetStartSwizzle,
483 subsetName, subsetStopSwizzle, subsetName,
484 subsetStartSwizzle, subsetName, subsetStartSwizzle);
485 }
Brian Salomon66356c22020-02-11 15:22:18 -0500486 break;
487 case ShaderMode::kMirrorRepeat: {
488 fb->codeAppend("{");
489 fb->codeAppendf("float w = %s.%s - %s.%s;", subsetName,
490 subsetStopSwizzle, subsetName, subsetStartSwizzle);
491 fb->codeAppendf("float w2 = 2 * w;");
492 fb->codeAppendf("float m = mod(inCoord.%s - %s.%s, w2);", coordSwizzle,
493 subsetName, subsetStartSwizzle);
Brian Salomon53c909e2020-02-13 13:54:24 -0500494 fb->codeAppendf("subsetCoord.%s = mix(m, w2 - m, step(w, m)) + %s.%s;",
Brian Salomon66356c22020-02-11 15:22:18 -0500495 coordSwizzle, subsetName, subsetStartSwizzle);
496 fb->codeAppend("}");
497 break;
498 }
499 }
500 };
501
Brian Salomonf46f19b2020-02-13 14:11:28 -0500502 auto clampCoord = [&](bool clamp,
503 const char* coordSwizzle,
504 const char* clampStartSwizzle,
505 const char* clampStopSwizzle) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500506 if (clamp) {
507 fb->codeAppendf("clampedCoord.%s = clamp(subsetCoord.%s, %s.%s, %s.%s);",
508 coordSwizzle, coordSwizzle, clampName, clampStartSwizzle,
509 clampName, clampStopSwizzle);
510 } else {
511 fb->codeAppendf("clampedCoord.%s = subsetCoord.%s;", coordSwizzle,
512 coordSwizzle);
513 }
514 };
515
Brian Salomonf46f19b2020-02-13 14:11:28 -0500516 // Insert vars for extra coords and blending weights for kRepeatMipMap.
517 const char* extraRepeatCoordX = nullptr;
518 const char* repeatCoordWeightX = nullptr;
519 const char* extraRepeatCoordY = nullptr;
520 const char* repeatCoordWeightY = nullptr;
521 if (filterLogic[0] == FilterLogic::kRepeatMipMap) {
522 fb->codeAppend("float extraRepeatCoordX; half repeatCoordWeightX;");
523 extraRepeatCoordX = "extraRepeatCoordX";
524 repeatCoordWeightX = "repeatCoordWeightX";
525 }
526 if (filterLogic[1] == FilterLogic::kRepeatMipMap) {
527 fb->codeAppend("float extraRepeatCoordY; half repeatCoordWeightY;");
528 extraRepeatCoordY = "extraRepeatCoordY";
529 repeatCoordWeightY = "repeatCoordWeightY";
530 }
531
532 // Apply subset rect and clamp rect to coords.
Brian Salomon53c909e2020-02-13 13:54:24 -0500533 fb->codeAppend("float2 subsetCoord;");
Brian Salomonf46f19b2020-02-13 14:11:28 -0500534 subsetCoord(te.fShaderModes[0], "x", "x", "z", extraRepeatCoordX,
535 repeatCoordWeightX);
536 subsetCoord(te.fShaderModes[1], "y", "y", "w", extraRepeatCoordY,
537 repeatCoordWeightY);
Brian Salomon53c909e2020-02-13 13:54:24 -0500538 fb->codeAppend("float2 clampedCoord;");
539 clampCoord(useClamp[0], "x", "x", "z");
540 clampCoord(useClamp[1], "y", "y", "w");
Brian Salomonca6b2f42020-01-24 11:31:21 -0500541
Brian Salomonf46f19b2020-02-13 14:11:28 -0500542 // Additional clamping for the extra coords for kRepeatMipMap.
543 if (filterLogic[0] == FilterLogic::kRepeatMipMap) {
544 fb->codeAppendf("extraRepeatCoordX = clamp(extraRepeatCoordX, %s.x, %s.z);",
545 clampName, clampName);
546 }
547 if (filterLogic[1] == FilterLogic::kRepeatMipMap) {
548 fb->codeAppendf("extraRepeatCoordY = clamp(extraRepeatCoordY, %s.y, %s.w);",
549 clampName, clampName);
550 }
551
552 // Do the 2 or 4 texture reads for kRepeatMipMap and then apply the weight(s)
553 // to blend between them. If neither direction is kRepeatMipMap do a single
554 // read at clampedCoord.
555 if (filterLogic[0] == FilterLogic::kRepeatMipMap &&
556 filterLogic[1] == FilterLogic::kRepeatMipMap) {
557 fb->codeAppendf(
558 "half4 textureColor ="
559 " mix(mix(%s, %s, repeatCoordWeightX),"
560 " mix(%s, %s, repeatCoordWeightX),"
561 " repeatCoordWeightY);",
562 read("clampedCoord").c_str(),
563 read("float2(extraRepeatCoordX, clampedCoord.y)").c_str(),
564 read("float2(clampedCoord.x, extraRepeatCoordY)").c_str(),
565 read("float2(extraRepeatCoordX, extraRepeatCoordY)").c_str());
566
567 } else if (filterLogic[0] == FilterLogic::kRepeatMipMap) {
568 fb->codeAppendf("half4 textureColor = mix(%s, %s, repeatCoordWeightX);",
569 read("clampedCoord").c_str(),
570 read("float2(extraRepeatCoordX, clampedCoord.y)").c_str());
571 } else if (filterLogic[1] == FilterLogic::kRepeatMipMap) {
572 fb->codeAppendf("half4 textureColor = mix(%s, %s, repeatCoordWeightY);",
573 read("clampedCoord").c_str(),
574 read("float2(clampedCoord.x, extraRepeatCoordY)").c_str());
575 } else {
576 fb->codeAppendf("half4 textureColor = %s;", read("clampedCoord").c_str());
577 }
Brian Salomonca6b2f42020-01-24 11:31:21 -0500578
Brian Salomon53c909e2020-02-13 13:54:24 -0500579 // Strings for extra texture reads used only in kRepeatBilerp
580 SkString repeatBilerpReadX;
581 SkString repeatBilerpReadY;
582
583 // Calculate the amount the coord moved for clamping. This will be used
Brian Salomond71548a2020-02-29 19:43:30 -0500584 // to implement shader-based filtering for kClampToBorder and kRepeat.
Brian Salomon53c909e2020-02-13 13:54:24 -0500585
586 if (filterLogic[0] == FilterLogic::kRepeatBilerp ||
Brian Salomond71548a2020-02-29 19:43:30 -0500587 filterLogic[0] == FilterLogic::kClampToBorderFilter) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500588 fb->codeAppend("half errX = half(subsetCoord.x - clampedCoord.x);");
589 fb->codeAppendf("float repeatCoordX = errX > 0 ? %s.x : %s.z;", clampName,
590 clampName);
591 repeatBilerpReadX = read("float2(repeatCoordX, clampedCoord.y)");
Brian Salomonca6b2f42020-01-24 11:31:21 -0500592 }
Brian Salomon53c909e2020-02-13 13:54:24 -0500593 if (filterLogic[1] == FilterLogic::kRepeatBilerp ||
Brian Salomond71548a2020-02-29 19:43:30 -0500594 filterLogic[1] == FilterLogic::kClampToBorderFilter) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500595 fb->codeAppend("half errY = half(subsetCoord.y - clampedCoord.y);");
596 fb->codeAppendf("float repeatCoordY = errY > 0 ? %s.y : %s.w;", clampName,
597 clampName);
598 repeatBilerpReadY = read("float2(clampedCoord.x, repeatCoordY)");
599 }
600
601 // Add logic for kRepeatBilerp. Do 1 or 3 more texture reads depending
602 // on whether both modes are kRepeat and whether we're near a single subset edge
603 // or a corner. Then blend the multiple reads using the err values calculated
604 // above.
605 const char* ifStr = "if";
606 if (filterLogic[0] == FilterLogic::kRepeatBilerp &&
607 filterLogic[1] == FilterLogic::kRepeatBilerp) {
608 auto repeatBilerpReadXY = read("float2(repeatCoordX, repeatCoordY)");
609 fb->codeAppendf(
610 "if (errX != 0 && errY != 0) {"
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500611 " errX = abs(errX);"
Brian Salomon53c909e2020-02-13 13:54:24 -0500612 " textureColor = mix(mix(textureColor, %s, errX),"
613 " mix(%s, %s, errX),"
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500614 " abs(errY));"
Brian Salomon53c909e2020-02-13 13:54:24 -0500615 "}",
616 repeatBilerpReadX.c_str(), repeatBilerpReadY.c_str(),
617 repeatBilerpReadXY.c_str());
618 ifStr = "else if";
619 }
620 if (filterLogic[0] == FilterLogic::kRepeatBilerp) {
621 fb->codeAppendf(
622 "%s (errX != 0) {"
623 " textureColor = mix(textureColor, %s, abs(errX));"
624 "}",
625 ifStr, repeatBilerpReadX.c_str());
626 }
627 if (filterLogic[1] == FilterLogic::kRepeatBilerp) {
628 fb->codeAppendf(
629 "%s (errY != 0) {"
630 " textureColor = mix(textureColor, %s, abs(errY));"
631 "}",
632 ifStr, repeatBilerpReadY.c_str());
633 }
634
Brian Salomond71548a2020-02-29 19:43:30 -0500635 // Do soft edge shader filtering against border color for kClampToBorderFilter using
Brian Salomon53c909e2020-02-13 13:54:24 -0500636 // the err values calculated above.
Brian Salomond71548a2020-02-29 19:43:30 -0500637 if (filterLogic[0] == FilterLogic::kClampToBorderFilter) {
638 fb->codeAppendf("textureColor = mix(textureColor, %s, min(abs(errX), 1));",
639 borderName);
Brian Salomon53c909e2020-02-13 13:54:24 -0500640 }
Brian Salomond71548a2020-02-29 19:43:30 -0500641 if (filterLogic[1] == FilterLogic::kClampToBorderFilter) {
642 fb->codeAppendf("textureColor = mix(textureColor, %s, min(abs(errY), 1));",
643 borderName);
Brian Salomon53c909e2020-02-13 13:54:24 -0500644 }
645
Brian Salomond71548a2020-02-29 19:43:30 -0500646 // Do hard-edge shader transition to border color for kClampToBorderNearest at the
Brian Salomon53c909e2020-02-13 13:54:24 -0500647 // subset boundaries.
Brian Salomond71548a2020-02-29 19:43:30 -0500648 if (filterLogic[0] == FilterLogic::kClampToBorderNearest) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500649 fb->codeAppendf(
650 "if (inCoord.x < %s.x || inCoord.x > %s.z) {"
Brian Salomond71548a2020-02-29 19:43:30 -0500651 " textureColor = %s;"
Brian Salomon53c909e2020-02-13 13:54:24 -0500652 "}",
Brian Salomond71548a2020-02-29 19:43:30 -0500653 subsetName, subsetName, borderName);
Brian Salomon53c909e2020-02-13 13:54:24 -0500654 }
Brian Salomond71548a2020-02-29 19:43:30 -0500655 if (filterLogic[1] == FilterLogic::kClampToBorderNearest) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500656 fb->codeAppendf(
657 "if (inCoord.y < %s.y || inCoord.y > %s.w) {"
Brian Salomond71548a2020-02-29 19:43:30 -0500658 " textureColor = %s;"
Brian Salomon53c909e2020-02-13 13:54:24 -0500659 "}",
Brian Salomond71548a2020-02-29 19:43:30 -0500660 subsetName, subsetName, borderName);
Brian Salomon53c909e2020-02-13 13:54:24 -0500661 }
662 fb->codeAppendf("%s = %s * textureColor;", args.fOutputColor, args.fInputColor);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500663 }
664 }
665
666 protected:
667 void onSetData(const GrGLSLProgramDataManager& pdm,
668 const GrFragmentProcessor& fp) override {
669 const auto& te = fp.cast<GrTextureEffect>();
Brian Salomonca6b2f42020-01-24 11:31:21 -0500670
Brian Salomon53c909e2020-02-13 13:54:24 -0500671 const float w = te.fSampler.peekTexture()->width();
672 const float h = te.fSampler.peekTexture()->height();
673 const auto& s = te.fSubset;
674 const auto& c = te.fClamp;
Brian Salomonca6b2f42020-01-24 11:31:21 -0500675
Brian Salomon53c909e2020-02-13 13:54:24 -0500676 auto type = te.fSampler.peekTexture()->texturePriv().textureType();
677
678 float norm[4] = {w, h, 1.f/w, 1.f/h};
679
680 if (fNormUni.isValid()) {
681 pdm.set4fv(fNormUni, 1, norm);
682 SkASSERT(type != GrTextureType::kRectangle);
683 }
684
685 auto pushRect = [&](float rect[4], UniformHandle uni) {
Brian Salomonca6b2f42020-01-24 11:31:21 -0500686 if (te.fSampler.view().origin() == kBottomLeft_GrSurfaceOrigin) {
687 rect[1] = h - rect[1];
688 rect[3] = h - rect[3];
689 std::swap(rect[1], rect[3]);
690 }
Brian Salomon53c909e2020-02-13 13:54:24 -0500691 if (!fNormUni.isValid() && type != GrTextureType::kRectangle) {
692 rect[0] *= norm[2];
693 rect[2] *= norm[2];
694 rect[1] *= norm[3];
695 rect[3] *= norm[3];
Brian Salomonca6b2f42020-01-24 11:31:21 -0500696 }
Brian Salomon53c909e2020-02-13 13:54:24 -0500697 pdm.set4fv(uni, 1, rect);
698 };
Brian Salomonca6b2f42020-01-24 11:31:21 -0500699
Brian Salomon53c909e2020-02-13 13:54:24 -0500700 if (fSubsetUni.isValid()) {
701 float subset[] = {s.fLeft, s.fTop, s.fRight, s.fBottom};
702 pushRect(subset, fSubsetUni);
703 }
704 if (fClampUni.isValid()) {
705 float subset[] = {c.fLeft, c.fTop, c.fRight, c.fBottom};
706 pushRect(subset, fClampUni);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500707 }
Brian Salomond71548a2020-02-29 19:43:30 -0500708 if (fBorderUni.isValid()) {
709 pdm.set4fv(fBorderUni, 1, te.fBorder);
710 }
Brian Salomonb8f098d2020-01-07 11:15:44 -0500711 }
712 };
713 return new Impl;
714}
715
Brian Salomonca6b2f42020-01-24 11:31:21 -0500716void GrTextureEffect::onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const {
Brian Salomonca6b2f42020-01-24 11:31:21 -0500717 auto m0 = static_cast<uint32_t>(fShaderModes[0]);
718 auto m1 = static_cast<uint32_t>(fShaderModes[1]);
Brian Salomon53c909e2020-02-13 13:54:24 -0500719 auto filter = fSampler.samplerState().filter();
720 auto l0 = static_cast<uint32_t>(GetFilterLogic(fShaderModes[0], filter));
721 auto l1 = static_cast<uint32_t>(GetFilterLogic(fShaderModes[1], filter));
722 b->add32((l0 << 24) | (l1 << 16) | (m0 << 8) | m1);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500723}
Brian Salomonb8f098d2020-01-07 11:15:44 -0500724
Brian Salomonca6b2f42020-01-24 11:31:21 -0500725bool GrTextureEffect::onIsEqual(const GrFragmentProcessor& other) const {
Ethan Nicholas94996eb2020-04-01 13:46:10 -0400726 auto& that = other.cast<GrTextureEffect>();
Brian Salomond71548a2020-02-29 19:43:30 -0500727 if (fShaderModes[0] != that.fShaderModes[0] || fShaderModes[1] != that.fShaderModes[1]) {
728 return false;
729 }
730 if (fSubset != that.fSubset) {
731 return false;
732 }
733 if ((fShaderModes[0] == ShaderMode::kClampToBorder ||
734 fShaderModes[1] == ShaderMode::kClampToBorder) &&
735 !std::equal(fBorder, fBorder + 4, that.fBorder)) {
736 return false;
737 }
738 return true;
Brian Salomonb8f098d2020-01-07 11:15:44 -0500739}
740
Greg Danield2ccbb52020-02-05 10:45:39 -0500741GrTextureEffect::GrTextureEffect(GrSurfaceProxyView view, SkAlphaType alphaType,
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400742 const Sampling& sampling, bool lazyProxyNormalization)
Brian Salomonb8f098d2020-01-07 11:15:44 -0500743 : GrFragmentProcessor(kGrTextureEffect_ClassID,
Brian Salomond71548a2020-02-29 19:43:30 -0500744 ModulateForSamplerOptFlags(alphaType, sampling.hasBorderAlpha()))
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400745 , fCoordTransform(SkMatrix::I())
Greg Danield2ccbb52020-02-05 10:45:39 -0500746 , fSampler(std::move(view), sampling.fHWSampler)
Brian Salomonca6b2f42020-01-24 11:31:21 -0500747 , fSubset(sampling.fShaderSubset)
Brian Salomon53c909e2020-02-13 13:54:24 -0500748 , fClamp(sampling.fShaderClamp)
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400749 , fShaderModes{sampling.fShaderModes[0], sampling.fShaderModes[1]}
750 , fLazyProxyNormalization(lazyProxyNormalization) {
Brian Salomonca6b2f42020-01-24 11:31:21 -0500751 // We always compare the range even when it isn't used so assert we have canonical don't care
752 // values.
753 SkASSERT(fShaderModes[0] != ShaderMode::kNone || (fSubset.fLeft == 0 && fSubset.fRight == 0));
754 SkASSERT(fShaderModes[1] != ShaderMode::kNone || (fSubset.fTop == 0 && fSubset.fBottom == 0));
Brian Salomonb8f098d2020-01-07 11:15:44 -0500755 this->setTextureSamplerCnt(1);
756 this->addCoordTransform(&fCoordTransform);
Brian Salomond71548a2020-02-29 19:43:30 -0500757 std::copy_n(sampling.fBorder, 4, fBorder);
Brian Salomonb8f098d2020-01-07 11:15:44 -0500758}
759
760GrTextureEffect::GrTextureEffect(const GrTextureEffect& src)
761 : INHERITED(kGrTextureEffect_ClassID, src.optimizationFlags())
762 , fCoordTransform(src.fCoordTransform)
Brian Salomonca6b2f42020-01-24 11:31:21 -0500763 , fSampler(src.fSampler)
764 , fSubset(src.fSubset)
Brian Salomon922496f2020-02-14 12:20:14 -0500765 , fClamp(src.fClamp)
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400766 , fShaderModes{src.fShaderModes[0], src.fShaderModes[1]}
767 , fLazyProxyNormalization(src.fLazyProxyNormalization) {
Brian Salomonba90ce92020-03-04 12:38:04 -0500768 std::copy_n(src.fBorder, 4, fBorder);
Brian Salomonb8f098d2020-01-07 11:15:44 -0500769 this->setTextureSamplerCnt(1);
770 this->addCoordTransform(&fCoordTransform);
771}
772
773std::unique_ptr<GrFragmentProcessor> GrTextureEffect::clone() const {
774 return std::unique_ptr<GrFragmentProcessor>(new GrTextureEffect(*this));
775}
776
777const GrFragmentProcessor::TextureSampler& GrTextureEffect::onTextureSampler(int) const {
778 return fSampler;
779}
780
781GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTextureEffect);
782#if GR_TEST_UTILS
783std::unique_ptr<GrFragmentProcessor> GrTextureEffect::TestCreate(GrProcessorTestData* testData) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500784 auto [view, ct, at] = testData->randomView();
Brian Salomon53c909e2020-02-13 13:54:24 -0500785 Mode wrapModes[2];
Brian Salomonb8f098d2020-01-07 11:15:44 -0500786 GrTest::TestWrapModes(testData->fRandom, wrapModes);
Brian Salomonb8f098d2020-01-07 11:15:44 -0500787
Brian Salomon53c909e2020-02-13 13:54:24 -0500788 Filter filter;
789 if (view.asTextureProxy()->mipMapped() == GrMipMapped::kYes) {
790 switch (testData->fRandom->nextULessThan(3)) {
791 case 0:
792 filter = Filter::kNearest;
793 break;
794 case 1:
795 filter = Filter::kBilerp;
796 break;
797 default:
798 filter = Filter::kMipMap;
799 break;
800 }
801 } else {
802 filter = testData->fRandom->nextBool() ? Filter::kBilerp : Filter::kNearest;
803 }
804 GrSamplerState params(wrapModes, filter);
Brian Salomonb8f098d2020-01-07 11:15:44 -0500805
806 const SkMatrix& matrix = GrTest::TestMatrix(testData->fRandom);
Greg Danield2ccbb52020-02-05 10:45:39 -0500807 return GrTextureEffect::Make(std::move(view), at, matrix, params, *testData->caps());
Brian Salomonb8f098d2020-01-07 11:15:44 -0500808}
809#endif