blob: 0f1b0fb5ea940d63febb32bae69eaf50968068a0 [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 Salomon91dea512020-06-18 11:05:33 -0400113 r.fShaderMode = GetShaderMode(mode, filter);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500114 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 }
Brian Salomon91dea512020-06-18 11:05:33 -0400145 if (ShaderModeIsClampToBorder(fShaderModes[0]) || ShaderModeIsClampToBorder(fShaderModes[1])) {
Brian Salomond71548a2020-02-29 19:43:30 -0500146 return fBorder[3] < 1.f;
147 }
148 return false;
Brian Salomonca6b2f42020-01-24 11:31:21 -0500149}
150
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400151static void get_matrix(const SkMatrix& preMatrix, const GrSurfaceProxyView& view,
152 SkMatrix* outMatrix, bool* outLazyProxyNormalization) {
153 SkMatrix combined = preMatrix;
154 bool normalize = view.proxy()->backendFormat().textureType() != GrTextureType::kRectangle;
155 if (normalize) {
156 if (view.proxy()->isFullyLazy()) {
157 *outLazyProxyNormalization = true;
158 } else {
159 SkMatrixPriv::PostIDiv(&combined, view.proxy()->backingStoreDimensions().fWidth,
160 view.proxy()->backingStoreDimensions().fHeight);
161 *outLazyProxyNormalization = false;
162 }
163 } else {
164 *outLazyProxyNormalization = false;
165 }
166 if (view.origin() == kBottomLeft_GrSurfaceOrigin) {
167 if (normalize) {
168 // combined.postScale(1,-1);
169 // combined.postTranslate(0,1);
170 combined.set(SkMatrix::kMSkewY,
171 combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]);
172 combined.set(SkMatrix::kMScaleY,
173 combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
174 combined.set(SkMatrix::kMTransY,
175 combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
176 } else {
177 // combined.postScale(1, -1);
178 // combined.postTranslate(0,1);
179 SkScalar h = view.proxy()->backingStoreDimensions().fHeight;
180 combined.set(SkMatrix::kMSkewY,
181 h * combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]);
182 combined.set(SkMatrix::kMScaleY,
183 h * combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
184 combined.set(SkMatrix::kMTransY,
185 h * combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
186 }
187 }
188 *outMatrix = combined;
189}
190
Greg Danield2ccbb52020-02-05 10:45:39 -0500191std::unique_ptr<GrFragmentProcessor> GrTextureEffect::Make(GrSurfaceProxyView view,
Brian Salomonb8f098d2020-01-07 11:15:44 -0500192 SkAlphaType alphaType,
193 const SkMatrix& matrix,
Brian Salomon53c909e2020-02-13 13:54:24 -0500194 Filter filter) {
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400195 SkMatrix final;
196 bool lazyProxyNormalization;
197 get_matrix(matrix, view, &final, &lazyProxyNormalization);
Brian Osman273944a2020-05-29 16:44:26 -0400198 return GrMatrixEffect::Make(final, std::unique_ptr<GrFragmentProcessor>(
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400199 new GrTextureEffect(std::move(view),
200 alphaType,
201 Sampling(filter),
202 lazyProxyNormalization)));
Brian Salomonca6b2f42020-01-24 11:31:21 -0500203}
204
Greg Danield2ccbb52020-02-05 10:45:39 -0500205std::unique_ptr<GrFragmentProcessor> GrTextureEffect::Make(GrSurfaceProxyView view,
Brian Salomonca6b2f42020-01-24 11:31:21 -0500206 SkAlphaType alphaType,
207 const SkMatrix& matrix,
208 GrSamplerState sampler,
Brian Salomond71548a2020-02-29 19:43:30 -0500209 const GrCaps& caps,
210 const float border[4]) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500211 Sampling sampling(*view.proxy(), sampler, SkRect::Make(view.proxy()->dimensions()), nullptr,
Brian Salomond71548a2020-02-29 19:43:30 -0500212 border, caps);
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400213 SkMatrix final;
214 bool lazyProxyNormalization;
215 get_matrix(matrix, view, &final, &lazyProxyNormalization);
Brian Osman273944a2020-05-29 16:44:26 -0400216 return GrMatrixEffect::Make(final, std::unique_ptr<GrFragmentProcessor>(
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400217 new GrTextureEffect(std::move(view),
218 alphaType,
219 sampling,
220 lazyProxyNormalization)));
Brian Salomonca6b2f42020-01-24 11:31:21 -0500221}
222
Greg Danield2ccbb52020-02-05 10:45:39 -0500223std::unique_ptr<GrFragmentProcessor> GrTextureEffect::MakeSubset(GrSurfaceProxyView view,
Brian Salomonca6b2f42020-01-24 11:31:21 -0500224 SkAlphaType alphaType,
225 const SkMatrix& matrix,
226 GrSamplerState sampler,
227 const SkRect& subset,
Brian Salomond71548a2020-02-29 19:43:30 -0500228 const GrCaps& caps,
229 const float border[4]) {
230 Sampling sampling(*view.proxy(), sampler, subset, nullptr, border, caps);
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400231 SkMatrix final;
232 bool lazyProxyNormalization;
233 get_matrix(matrix, view, &final, &lazyProxyNormalization);
Brian Osman273944a2020-05-29 16:44:26 -0400234 return GrMatrixEffect::Make(final, std::unique_ptr<GrFragmentProcessor>(
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400235 new GrTextureEffect(std::move(view),
236 alphaType,
237 sampling,
238 lazyProxyNormalization)));
Brian Salomonca6b2f42020-01-24 11:31:21 -0500239}
240
Greg Danield2ccbb52020-02-05 10:45:39 -0500241std::unique_ptr<GrFragmentProcessor> GrTextureEffect::MakeSubset(GrSurfaceProxyView view,
Brian Salomonca6b2f42020-01-24 11:31:21 -0500242 SkAlphaType alphaType,
243 const SkMatrix& matrix,
244 GrSamplerState sampler,
245 const SkRect& subset,
246 const SkRect& domain,
Brian Salomond71548a2020-02-29 19:43:30 -0500247 const GrCaps& caps,
248 const float border[4]) {
249 Sampling sampling(*view.proxy(), sampler, subset, &domain, border, caps);
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400250 SkMatrix final;
251 bool lazyProxyNormalization;
252 get_matrix(matrix, view, &final, &lazyProxyNormalization);
Brian Osman273944a2020-05-29 16:44:26 -0400253 return GrMatrixEffect::Make(final, std::unique_ptr<GrFragmentProcessor>(
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400254 new GrTextureEffect(std::move(view),
255 alphaType,
256 sampling,
257 lazyProxyNormalization)));
Brian Salomonb8f098d2020-01-07 11:15:44 -0500258}
259
Brian Salomon91dea512020-06-18 11:05:33 -0400260GrTextureEffect::ShaderMode GrTextureEffect::GetShaderMode(GrSamplerState::WrapMode mode,
261 GrSamplerState::Filter filter) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500262 switch (mode) {
Brian Salomon91dea512020-06-18 11:05:33 -0400263 case GrSamplerState::WrapMode::kMirrorRepeat:
264 return ShaderMode::kMirrorRepeat;
265 case GrSamplerState::WrapMode::kClamp:
266 return ShaderMode::kClamp;
267 case GrSamplerState::WrapMode::kRepeat:
Brian Salomon53c909e2020-02-13 13:54:24 -0500268 switch (filter) {
269 case GrSamplerState::Filter::kNearest:
Brian Salomon91dea512020-06-18 11:05:33 -0400270 return ShaderMode::kRepeatNearest;
Brian Salomon53c909e2020-02-13 13:54:24 -0500271 case GrSamplerState::Filter::kBilerp:
Brian Salomon91dea512020-06-18 11:05:33 -0400272 return ShaderMode::kRepeatBilerp;
Brian Salomon53c909e2020-02-13 13:54:24 -0500273 case GrSamplerState::Filter::kMipMap:
Brian Salomon91dea512020-06-18 11:05:33 -0400274 return ShaderMode::kRepeatMipMap;
Brian Salomon53c909e2020-02-13 13:54:24 -0500275 }
276 SkUNREACHABLE;
Brian Salomon91dea512020-06-18 11:05:33 -0400277 case GrSamplerState::WrapMode::kClampToBorder:
278 return filter == GrSamplerState::Filter::kNearest ? ShaderMode::kClampToBorderNearest
279 : ShaderMode::kClampToBorderFilter;
Brian Salomon53c909e2020-02-13 13:54:24 -0500280 }
281 SkUNREACHABLE;
282}
283
Brian Salomon91dea512020-06-18 11:05:33 -0400284inline bool GrTextureEffect::ShaderModeIsClampToBorder(ShaderMode m) {
285 return m == ShaderMode::kClampToBorderNearest || m == ShaderMode::kClampToBorderFilter;
286}
287
Brian Salomonb8f098d2020-01-07 11:15:44 -0500288GrGLSLFragmentProcessor* GrTextureEffect::onCreateGLSLInstance() const {
289 class Impl : public GrGLSLFragmentProcessor {
Brian Salomonca6b2f42020-01-24 11:31:21 -0500290 UniformHandle fSubsetUni;
Brian Salomon53c909e2020-02-13 13:54:24 -0500291 UniformHandle fClampUni;
292 UniformHandle fNormUni;
Brian Salomond71548a2020-02-29 19:43:30 -0500293 UniformHandle fBorderUni;
Brian Salomonca6b2f42020-01-24 11:31:21 -0500294
Brian Salomonb8f098d2020-01-07 11:15:44 -0500295 public:
296 void emitCode(EmitArgs& args) override {
Ethan Nicholas94996eb2020-04-01 13:46:10 -0400297 auto& te = args.fFp.cast<GrTextureEffect>();
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400298 SkString coords;
299 if (args.fFp.isSampledWithExplicitCoords()) {
300 coords = "_coords";
301 } else {
302 coords = args.fTransformedCoords[0].fVaryingPoint.c_str();
303 }
Brian Salomonb8f098d2020-01-07 11:15:44 -0500304 auto* fb = args.fFragBuilder;
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400305 if (te.sampleMatrix().fKind == SkSL::SampleMatrix::Kind::kMixed) {
Michael Ludwig89dd4e72020-06-03 15:39:06 -0400306 // FIXME this is very similar to the extra logic in
307 // GrGLSLFragmentShaderBuilder::ensureCoords2D
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400308 args.fUniformHandler->writeUniformMappings(te.sampleMatrix().fOwner, fb);
Michael Ludwig89dd4e72020-06-03 15:39:06 -0400309 SkString coords2D;
310 coords2D.printf("%s_teSample", coords.c_str());
311
312 fb->codeAppendf("float3 %s_3d = %s * _matrix * %s.xy1;\n",
313 coords2D.c_str(), te.sampleMatrix().fExpression.c_str(),
314 coords.c_str());
315 fb->codeAppendf("float2 %s = %s_3d.xy / %s_3d.z;\n",
316 coords2D.c_str(), coords2D.c_str(), coords2D.c_str());
317 coords = coords2D;
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400318 }
Brian Salomonca6b2f42020-01-24 11:31:21 -0500319 if (te.fShaderModes[0] == ShaderMode::kNone &&
320 te.fShaderModes[1] == ShaderMode::kNone) {
Ethan Nicholas36a3e012020-04-17 19:00:28 +0000321 fb->codeAppendf("%s = ", args.fOutputColor);
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400322 if (te.fLazyProxyNormalization) {
323 const char* norm = nullptr;
324 fNormUni = args.fUniformHandler->addUniform(&te, kFragment_GrShaderFlag,
325 kFloat4_GrSLType, "norm", &norm);
326 fb->appendTextureLookupAndBlend(args.fInputColor, SkBlendMode::kModulate,
327 args.fTexSamplers[0],
328 SkStringPrintf("%s * %s.zw", coords.c_str(),
329 norm).c_str());
330 } else {
331 fb->appendTextureLookupAndBlend(args.fInputColor, SkBlendMode::kModulate,
332 args.fTexSamplers[0], coords.c_str());
333 }
Brian Salomonca6b2f42020-01-24 11:31:21 -0500334 fb->codeAppendf(";");
335 } else {
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400336 // Tripping this assert means we have a normalized fully lazy proxy with a
337 // non-default ShaderMode. There's nothing fundamentally wrong with doing that, but
338 // it hasn't been tested and this code path probably won't handle normalization
339 // properly in that case.
340 SkASSERT(!te.fLazyProxyNormalization);
Brian Salomon53c909e2020-02-13 13:54:24 -0500341 // Here is the basic flow of the various ShaderModes are implemented in a series of
342 // steps. Not all the steps apply to all the modes. We try to emit only the steps
343 // that are necessary for the given x/y shader modes.
344 //
345 // 0) Start with interpolated coordinates (unnormalize if doing anything
346 // complicated).
347 // 1) Map the coordinates into the subset range [Repeat and MirrorRepeat], or pass
348 // through output of 0).
349 // 2) Clamp the coordinates to a 0.5 inset of the subset rect [Clamp, Repeat, and
Brian Salomond71548a2020-02-29 19:43:30 -0500350 // MirrorRepeat always or ClampToBorder only when filtering] or pass through
351 // output of 1). The clamp rect collapses to a line or point it if the subset
352 // rect is less than one pixel wide/tall.
Brian Salomon53c909e2020-02-13 13:54:24 -0500353 // 3) Look up texture with output of 2) [All]
354 // 3) Use the difference between 1) and 2) to apply filtering at edge [Repeat or
Brian Salomond71548a2020-02-29 19:43:30 -0500355 // ClampToBorder]. In the Repeat case this requires extra texture lookups on the
356 // other side of the subset (up to 3 more reads). Or if ClampToBorder and not
357 // filtering do a hard less than/greater than test with the subset rect.
Brian Salomonca6b2f42020-01-24 11:31:21 -0500358
Brian Salomon53c909e2020-02-13 13:54:24 -0500359 // Convert possible projective texture coordinates into non-homogeneous half2.
Brian Salomon66356c22020-02-11 15:22:18 -0500360 fb->codeAppendf(
361 "float2 inCoord = %s;",
Ethan Nicholas58430122020-04-14 09:54:02 -0400362 fb->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint,
363 te.sampleMatrix()).c_str());
Brian Salomon66356c22020-02-11 15:22:18 -0500364
Brian Salomon53c909e2020-02-13 13:54:24 -0500365 const auto& m = te.fShaderModes;
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500366 GrTextureType textureType = te.fSampler.proxy()->backendFormat().textureType();
367 bool normCoords = textureType != GrTextureType::kRectangle;
Brian Salomon53c909e2020-02-13 13:54:24 -0500368
Brian Salomond71548a2020-02-29 19:43:30 -0500369 const char* borderName = nullptr;
Brian Salomon91dea512020-06-18 11:05:33 -0400370 if (te.hasClampToBorderShaderMode()) {
Brian Salomond71548a2020-02-29 19:43:30 -0500371 fBorderUni = args.fUniformHandler->addUniform(
Ethan Nicholas16464c32020-04-06 13:53:05 -0400372 &te, kFragment_GrShaderFlag, kHalf4_GrSLType, "border", &borderName);
Brian Salomond71548a2020-02-29 19:43:30 -0500373 }
Brian Salomon53c909e2020-02-13 13:54:24 -0500374 auto modeUsesSubset = [](ShaderMode m) {
Brian Salomon91dea512020-06-18 11:05:33 -0400375 switch (m) {
376 case ShaderMode::kNone: return false;
377 case ShaderMode::kClamp: return false;
378 case ShaderMode::kRepeatNearest: return true;
379 case ShaderMode::kRepeatBilerp: return true;
380 case ShaderMode::kRepeatMipMap: return true;
381 case ShaderMode::kMirrorRepeat: return true;
382 case ShaderMode::kClampToBorderNearest: return true;
383 case ShaderMode::kClampToBorderFilter: return true;
384 }
385 SkUNREACHABLE;
Brian Salomon53c909e2020-02-13 13:54:24 -0500386 };
387
Brian Salomon91dea512020-06-18 11:05:33 -0400388 auto modeUsesClamp = [](ShaderMode m) {
389 switch (m) {
390 case ShaderMode::kNone: return false;
391 case ShaderMode::kClamp: return true;
392 case ShaderMode::kRepeatNearest: return true;
393 case ShaderMode::kRepeatBilerp: return true;
394 case ShaderMode::kRepeatMipMap: return true;
395 case ShaderMode::kMirrorRepeat: return true;
396 case ShaderMode::kClampToBorderNearest: return false;
397 case ShaderMode::kClampToBorderFilter: return true;
398 }
399 SkUNREACHABLE;
400 };
401
402 // To keep things a little simpler, when we have filtering logic in the shader we
403 // operate on unnormalized texture coordinates. We will add a uniform that stores
404 // {w, h, 1/w, 1/h} in a float4 below.
405 auto modeRequiresUnormCoords = [](ShaderMode m) {
406 switch (m) {
407 case ShaderMode::kNone: return false;
408 case ShaderMode::kClamp: return false;
Brian Salomond610c752020-06-18 14:41:53 -0400409 case ShaderMode::kRepeatNearest: return false;
Brian Salomon91dea512020-06-18 11:05:33 -0400410 case ShaderMode::kRepeatBilerp: return true;
411 case ShaderMode::kRepeatMipMap: return true;
412 case ShaderMode::kMirrorRepeat: return false;
413 case ShaderMode::kClampToBorderNearest: return true;
414 case ShaderMode::kClampToBorderFilter: return true;
415 }
416 SkUNREACHABLE;
Brian Salomon53c909e2020-02-13 13:54:24 -0500417 };
418
419 bool useSubset[2] = {modeUsesSubset(m[0]), modeUsesSubset(m[1])};
420 bool useClamp [2] = {modeUsesClamp (m[0]), modeUsesClamp (m[1])};
421
422 const char* subsetName = nullptr;
423 if (useSubset[0] || useSubset[1]) {
424 fSubsetUni = args.fUniformHandler->addUniform(
Ethan Nicholas16464c32020-04-06 13:53:05 -0400425 &te, kFragment_GrShaderFlag, kFloat4_GrSLType, "subset", &subsetName);
Brian Salomon53c909e2020-02-13 13:54:24 -0500426 }
427
428 const char* clampName = nullptr;
429 if (useClamp[0] || useClamp[1]) {
430 fClampUni = args.fUniformHandler->addUniform(
Ethan Nicholas16464c32020-04-06 13:53:05 -0400431 &te, kFragment_GrShaderFlag, kFloat4_GrSLType, "clamp", &clampName);
Brian Salomon53c909e2020-02-13 13:54:24 -0500432 }
433
Brian Salomon53c909e2020-02-13 13:54:24 -0500434 const char* norm = nullptr;
Brian Salomon91dea512020-06-18 11:05:33 -0400435 if (normCoords && (modeRequiresUnormCoords(m[0]) ||
436 modeRequiresUnormCoords(m[1]))) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500437 // TODO: Detect support for textureSize() or polyfill textureSize() in SkSL and
438 // always use?
Ethan Nicholas16464c32020-04-06 13:53:05 -0400439 fNormUni = args.fUniformHandler->addUniform(&te, kFragment_GrShaderFlag,
Brian Salomon53c909e2020-02-13 13:54:24 -0500440 kFloat4_GrSLType, "norm", &norm);
441 // TODO: Remove the normalization from the CoordTransform to skip unnormalizing
442 // step here.
443 fb->codeAppendf("inCoord *= %s.xy;", norm);
444 }
445
446 // Generates a string to read at a coordinate, normalizing coords if necessary.
Brian Salomonf46f19b2020-02-13 14:11:28 -0500447 auto read = [&](const char* coord) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500448 SkString result;
449 SkString normCoord;
450 if (norm) {
451 normCoord.printf("(%s) * %s.zw", coord, norm);
452 } else {
453 normCoord = coord;
454 }
Brian Salomonf46f19b2020-02-13 14:11:28 -0500455 fb->appendTextureLookup(&result, args.fTexSamplers[0], normCoord.c_str());
Brian Salomon53c909e2020-02-13 13:54:24 -0500456 return result;
457 };
458
459 // Implements coord wrapping for kRepeat and kMirrorRepeat
Brian Salomonf46f19b2020-02-13 14:11:28 -0500460 auto subsetCoord = [&](ShaderMode mode,
461 const char* coordSwizzle,
462 const char* subsetStartSwizzle,
463 const char* subsetStopSwizzle,
464 const char* extraCoord,
465 const char* coordWeight) {
Brian Salomon66356c22020-02-11 15:22:18 -0500466 switch (mode) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500467 // These modes either don't use the subset rect or don't need to map the
468 // coords to be within the subset.
Brian Salomon66356c22020-02-11 15:22:18 -0500469 case ShaderMode::kNone:
Brian Salomon91dea512020-06-18 11:05:33 -0400470 case ShaderMode::kClampToBorderNearest:
471 case ShaderMode::kClampToBorderFilter:
Brian Salomon66356c22020-02-11 15:22:18 -0500472 case ShaderMode::kClamp:
Brian Salomon53c909e2020-02-13 13:54:24 -0500473 fb->codeAppendf("subsetCoord.%s = inCoord.%s;", coordSwizzle,
474 coordSwizzle);
Brian Salomon66356c22020-02-11 15:22:18 -0500475 break;
Brian Salomon91dea512020-06-18 11:05:33 -0400476 case ShaderMode::kRepeatNearest:
477 case ShaderMode::kRepeatBilerp:
478 fb->codeAppendf(
479 "subsetCoord.%s = mod(inCoord.%s - %s.%s, %s.%s - %s.%s) + "
480 "%s.%s;",
481 coordSwizzle, coordSwizzle, subsetName, subsetStartSwizzle,
482 subsetName, subsetStopSwizzle, subsetName, subsetStartSwizzle,
483 subsetName, subsetStartSwizzle);
Brian Salomon66356c22020-02-11 15:22:18 -0500484 break;
Brian Salomon91dea512020-06-18 11:05:33 -0400485 case ShaderMode::kRepeatMipMap:
486 // The approach here is to generate two sets of texture coords that
487 // are both "moving" at the same speed (if not direction) as
488 // inCoords. We accomplish that by using two out of phase mirror
489 // repeat coords. We will always sample using both coords but the
490 // read from the upward sloping one is selected using a weight
491 // that transitions from one set to the other near the reflection
492 // point. Like the coords, the weight is a saw-tooth function,
493 // phase-shifted, vertically translated, and then clamped to 0..1.
494 // TODO: Skip this and use textureGrad() when available.
495 SkASSERT(extraCoord);
496 SkASSERT(coordWeight);
497 fb->codeAppend("{");
498 fb->codeAppendf("float w = %s.%s - %s.%s;", subsetName,
499 subsetStopSwizzle, subsetName, subsetStartSwizzle);
500 fb->codeAppendf("float w2 = 2 * w;");
501 fb->codeAppendf("float d = inCoord.%s - %s.%s;", coordSwizzle,
502 subsetName, subsetStartSwizzle);
503 fb->codeAppend("float m = mod(d, w2);");
504 fb->codeAppend("float o = mix(m, w2 - m, step(w, m));");
505 fb->codeAppendf("subsetCoord.%s = o + %s.%s;", coordSwizzle, subsetName,
506 subsetStartSwizzle);
507 fb->codeAppendf("%s = w - o + %s.%s;", extraCoord, subsetName,
508 subsetStartSwizzle);
509 // coordWeight is used as the third param of mix() to blend between a
510 // sample taken using subsetCoord and a sample at extraCoord.
511 fb->codeAppend("float hw = w/2;");
512 fb->codeAppend("float n = mod(d - hw, w2);");
513 fb->codeAppendf(
514 "%s = saturate(half(mix(n, w2 - n, step(w, n)) - hw + "
515 "0.5));",
516 coordWeight);
517 fb->codeAppend("}");
518 break;
519 case ShaderMode::kMirrorRepeat:
Brian Salomon66356c22020-02-11 15:22:18 -0500520 fb->codeAppend("{");
521 fb->codeAppendf("float w = %s.%s - %s.%s;", subsetName,
522 subsetStopSwizzle, subsetName, subsetStartSwizzle);
523 fb->codeAppendf("float w2 = 2 * w;");
524 fb->codeAppendf("float m = mod(inCoord.%s - %s.%s, w2);", coordSwizzle,
525 subsetName, subsetStartSwizzle);
Brian Salomon53c909e2020-02-13 13:54:24 -0500526 fb->codeAppendf("subsetCoord.%s = mix(m, w2 - m, step(w, m)) + %s.%s;",
Brian Salomon66356c22020-02-11 15:22:18 -0500527 coordSwizzle, subsetName, subsetStartSwizzle);
528 fb->codeAppend("}");
529 break;
Brian Salomon66356c22020-02-11 15:22:18 -0500530 }
531 };
532
Brian Salomonf46f19b2020-02-13 14:11:28 -0500533 auto clampCoord = [&](bool clamp,
534 const char* coordSwizzle,
535 const char* clampStartSwizzle,
536 const char* clampStopSwizzle) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500537 if (clamp) {
538 fb->codeAppendf("clampedCoord.%s = clamp(subsetCoord.%s, %s.%s, %s.%s);",
539 coordSwizzle, coordSwizzle, clampName, clampStartSwizzle,
540 clampName, clampStopSwizzle);
541 } else {
542 fb->codeAppendf("clampedCoord.%s = subsetCoord.%s;", coordSwizzle,
543 coordSwizzle);
544 }
545 };
546
Brian Salomonf46f19b2020-02-13 14:11:28 -0500547 // Insert vars for extra coords and blending weights for kRepeatMipMap.
548 const char* extraRepeatCoordX = nullptr;
549 const char* repeatCoordWeightX = nullptr;
550 const char* extraRepeatCoordY = nullptr;
551 const char* repeatCoordWeightY = nullptr;
Brian Salomon91dea512020-06-18 11:05:33 -0400552 if (m[0] == ShaderMode::kRepeatMipMap) {
Brian Salomonf46f19b2020-02-13 14:11:28 -0500553 fb->codeAppend("float extraRepeatCoordX; half repeatCoordWeightX;");
554 extraRepeatCoordX = "extraRepeatCoordX";
555 repeatCoordWeightX = "repeatCoordWeightX";
556 }
Brian Salomon91dea512020-06-18 11:05:33 -0400557 if (m[1] == ShaderMode::kRepeatMipMap) {
Brian Salomonf46f19b2020-02-13 14:11:28 -0500558 fb->codeAppend("float extraRepeatCoordY; half repeatCoordWeightY;");
559 extraRepeatCoordY = "extraRepeatCoordY";
560 repeatCoordWeightY = "repeatCoordWeightY";
561 }
562
563 // Apply subset rect and clamp rect to coords.
Brian Salomon53c909e2020-02-13 13:54:24 -0500564 fb->codeAppend("float2 subsetCoord;");
Brian Salomonf46f19b2020-02-13 14:11:28 -0500565 subsetCoord(te.fShaderModes[0], "x", "x", "z", extraRepeatCoordX,
566 repeatCoordWeightX);
567 subsetCoord(te.fShaderModes[1], "y", "y", "w", extraRepeatCoordY,
568 repeatCoordWeightY);
Brian Salomon53c909e2020-02-13 13:54:24 -0500569 fb->codeAppend("float2 clampedCoord;");
570 clampCoord(useClamp[0], "x", "x", "z");
571 clampCoord(useClamp[1], "y", "y", "w");
Brian Salomonca6b2f42020-01-24 11:31:21 -0500572
Brian Salomonf46f19b2020-02-13 14:11:28 -0500573 // Additional clamping for the extra coords for kRepeatMipMap.
Brian Salomon91dea512020-06-18 11:05:33 -0400574 if (m[0] == ShaderMode::kRepeatMipMap) {
Brian Salomonf46f19b2020-02-13 14:11:28 -0500575 fb->codeAppendf("extraRepeatCoordX = clamp(extraRepeatCoordX, %s.x, %s.z);",
576 clampName, clampName);
577 }
Brian Salomon91dea512020-06-18 11:05:33 -0400578 if (m[1] == ShaderMode::kRepeatMipMap) {
Brian Salomonf46f19b2020-02-13 14:11:28 -0500579 fb->codeAppendf("extraRepeatCoordY = clamp(extraRepeatCoordY, %s.y, %s.w);",
580 clampName, clampName);
581 }
582
583 // Do the 2 or 4 texture reads for kRepeatMipMap and then apply the weight(s)
584 // to blend between them. If neither direction is kRepeatMipMap do a single
585 // read at clampedCoord.
Brian Salomon91dea512020-06-18 11:05:33 -0400586 if (m[0] == ShaderMode::kRepeatMipMap && m[1] == ShaderMode::kRepeatMipMap) {
Brian Salomonf46f19b2020-02-13 14:11:28 -0500587 fb->codeAppendf(
588 "half4 textureColor ="
589 " mix(mix(%s, %s, repeatCoordWeightX),"
590 " mix(%s, %s, repeatCoordWeightX),"
591 " repeatCoordWeightY);",
592 read("clampedCoord").c_str(),
593 read("float2(extraRepeatCoordX, clampedCoord.y)").c_str(),
594 read("float2(clampedCoord.x, extraRepeatCoordY)").c_str(),
595 read("float2(extraRepeatCoordX, extraRepeatCoordY)").c_str());
596
Brian Salomon91dea512020-06-18 11:05:33 -0400597 } else if (m[0] == ShaderMode::kRepeatMipMap) {
Brian Salomonf46f19b2020-02-13 14:11:28 -0500598 fb->codeAppendf("half4 textureColor = mix(%s, %s, repeatCoordWeightX);",
599 read("clampedCoord").c_str(),
600 read("float2(extraRepeatCoordX, clampedCoord.y)").c_str());
Brian Salomon91dea512020-06-18 11:05:33 -0400601 } else if (m[1] == ShaderMode::kRepeatMipMap) {
Brian Salomonf46f19b2020-02-13 14:11:28 -0500602 fb->codeAppendf("half4 textureColor = mix(%s, %s, repeatCoordWeightY);",
603 read("clampedCoord").c_str(),
604 read("float2(clampedCoord.x, extraRepeatCoordY)").c_str());
605 } else {
606 fb->codeAppendf("half4 textureColor = %s;", read("clampedCoord").c_str());
607 }
Brian Salomonca6b2f42020-01-24 11:31:21 -0500608
Brian Salomon53c909e2020-02-13 13:54:24 -0500609 // Strings for extra texture reads used only in kRepeatBilerp
610 SkString repeatBilerpReadX;
611 SkString repeatBilerpReadY;
612
613 // Calculate the amount the coord moved for clamping. This will be used
Brian Salomond71548a2020-02-29 19:43:30 -0500614 // to implement shader-based filtering for kClampToBorder and kRepeat.
Brian Salomon53c909e2020-02-13 13:54:24 -0500615
Brian Salomon91dea512020-06-18 11:05:33 -0400616 if (m[0] == ShaderMode::kRepeatBilerp || m[0] == ShaderMode::kClampToBorderFilter) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500617 fb->codeAppend("half errX = half(subsetCoord.x - clampedCoord.x);");
618 fb->codeAppendf("float repeatCoordX = errX > 0 ? %s.x : %s.z;", clampName,
619 clampName);
620 repeatBilerpReadX = read("float2(repeatCoordX, clampedCoord.y)");
Brian Salomonca6b2f42020-01-24 11:31:21 -0500621 }
Brian Salomon91dea512020-06-18 11:05:33 -0400622 if (m[1] == ShaderMode::kRepeatBilerp || m[1] == ShaderMode::kClampToBorderFilter) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500623 fb->codeAppend("half errY = half(subsetCoord.y - clampedCoord.y);");
624 fb->codeAppendf("float repeatCoordY = errY > 0 ? %s.y : %s.w;", clampName,
625 clampName);
626 repeatBilerpReadY = read("float2(clampedCoord.x, repeatCoordY)");
627 }
628
629 // Add logic for kRepeatBilerp. Do 1 or 3 more texture reads depending
630 // on whether both modes are kRepeat and whether we're near a single subset edge
631 // or a corner. Then blend the multiple reads using the err values calculated
632 // above.
633 const char* ifStr = "if";
Brian Salomon91dea512020-06-18 11:05:33 -0400634 if (m[0] == ShaderMode::kRepeatBilerp && m[1] == ShaderMode::kRepeatBilerp) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500635 auto repeatBilerpReadXY = read("float2(repeatCoordX, repeatCoordY)");
636 fb->codeAppendf(
637 "if (errX != 0 && errY != 0) {"
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500638 " errX = abs(errX);"
Brian Salomon53c909e2020-02-13 13:54:24 -0500639 " textureColor = mix(mix(textureColor, %s, errX),"
640 " mix(%s, %s, errX),"
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500641 " abs(errY));"
Brian Salomon53c909e2020-02-13 13:54:24 -0500642 "}",
643 repeatBilerpReadX.c_str(), repeatBilerpReadY.c_str(),
644 repeatBilerpReadXY.c_str());
645 ifStr = "else if";
646 }
Brian Salomon91dea512020-06-18 11:05:33 -0400647 if (m[0] == ShaderMode::kRepeatBilerp) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500648 fb->codeAppendf(
649 "%s (errX != 0) {"
650 " textureColor = mix(textureColor, %s, abs(errX));"
651 "}",
652 ifStr, repeatBilerpReadX.c_str());
653 }
Brian Salomon91dea512020-06-18 11:05:33 -0400654 if (m[1] == ShaderMode::kRepeatBilerp) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500655 fb->codeAppendf(
656 "%s (errY != 0) {"
657 " textureColor = mix(textureColor, %s, abs(errY));"
658 "}",
659 ifStr, repeatBilerpReadY.c_str());
660 }
661
Brian Salomond71548a2020-02-29 19:43:30 -0500662 // Do soft edge shader filtering against border color for kClampToBorderFilter using
Brian Salomon53c909e2020-02-13 13:54:24 -0500663 // the err values calculated above.
Brian Salomon91dea512020-06-18 11:05:33 -0400664 if (m[0] == ShaderMode::kClampToBorderFilter) {
Brian Salomond71548a2020-02-29 19:43:30 -0500665 fb->codeAppendf("textureColor = mix(textureColor, %s, min(abs(errX), 1));",
666 borderName);
Brian Salomon53c909e2020-02-13 13:54:24 -0500667 }
Brian Salomon91dea512020-06-18 11:05:33 -0400668 if (m[1] == ShaderMode::kClampToBorderFilter) {
Brian Salomond71548a2020-02-29 19:43:30 -0500669 fb->codeAppendf("textureColor = mix(textureColor, %s, min(abs(errY), 1));",
670 borderName);
Brian Salomon53c909e2020-02-13 13:54:24 -0500671 }
672
Brian Salomond71548a2020-02-29 19:43:30 -0500673 // Do hard-edge shader transition to border color for kClampToBorderNearest at the
Brian Salomon53c909e2020-02-13 13:54:24 -0500674 // subset boundaries.
Brian Salomon91dea512020-06-18 11:05:33 -0400675 if (m[0] == ShaderMode::kClampToBorderNearest) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500676 fb->codeAppendf(
677 "if (inCoord.x < %s.x || inCoord.x > %s.z) {"
Brian Salomond71548a2020-02-29 19:43:30 -0500678 " textureColor = %s;"
Brian Salomon53c909e2020-02-13 13:54:24 -0500679 "}",
Brian Salomond71548a2020-02-29 19:43:30 -0500680 subsetName, subsetName, borderName);
Brian Salomon53c909e2020-02-13 13:54:24 -0500681 }
Brian Salomon91dea512020-06-18 11:05:33 -0400682 if (m[1] == ShaderMode::kClampToBorderNearest) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500683 fb->codeAppendf(
684 "if (inCoord.y < %s.y || inCoord.y > %s.w) {"
Brian Salomond71548a2020-02-29 19:43:30 -0500685 " textureColor = %s;"
Brian Salomon53c909e2020-02-13 13:54:24 -0500686 "}",
Brian Salomond71548a2020-02-29 19:43:30 -0500687 subsetName, subsetName, borderName);
Brian Salomon53c909e2020-02-13 13:54:24 -0500688 }
689 fb->codeAppendf("%s = %s * textureColor;", args.fOutputColor, args.fInputColor);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500690 }
691 }
692
693 protected:
694 void onSetData(const GrGLSLProgramDataManager& pdm,
695 const GrFragmentProcessor& fp) override {
696 const auto& te = fp.cast<GrTextureEffect>();
Brian Salomonca6b2f42020-01-24 11:31:21 -0500697
Brian Salomon53c909e2020-02-13 13:54:24 -0500698 const float w = te.fSampler.peekTexture()->width();
699 const float h = te.fSampler.peekTexture()->height();
700 const auto& s = te.fSubset;
701 const auto& c = te.fClamp;
Brian Salomonca6b2f42020-01-24 11:31:21 -0500702
Brian Salomon53c909e2020-02-13 13:54:24 -0500703 auto type = te.fSampler.peekTexture()->texturePriv().textureType();
704
705 float norm[4] = {w, h, 1.f/w, 1.f/h};
706
707 if (fNormUni.isValid()) {
708 pdm.set4fv(fNormUni, 1, norm);
709 SkASSERT(type != GrTextureType::kRectangle);
710 }
711
712 auto pushRect = [&](float rect[4], UniformHandle uni) {
Brian Salomonca6b2f42020-01-24 11:31:21 -0500713 if (te.fSampler.view().origin() == kBottomLeft_GrSurfaceOrigin) {
714 rect[1] = h - rect[1];
715 rect[3] = h - rect[3];
716 std::swap(rect[1], rect[3]);
717 }
Brian Salomon53c909e2020-02-13 13:54:24 -0500718 if (!fNormUni.isValid() && type != GrTextureType::kRectangle) {
719 rect[0] *= norm[2];
720 rect[2] *= norm[2];
721 rect[1] *= norm[3];
722 rect[3] *= norm[3];
Brian Salomonca6b2f42020-01-24 11:31:21 -0500723 }
Brian Salomon53c909e2020-02-13 13:54:24 -0500724 pdm.set4fv(uni, 1, rect);
725 };
Brian Salomonca6b2f42020-01-24 11:31:21 -0500726
Brian Salomon53c909e2020-02-13 13:54:24 -0500727 if (fSubsetUni.isValid()) {
728 float subset[] = {s.fLeft, s.fTop, s.fRight, s.fBottom};
729 pushRect(subset, fSubsetUni);
730 }
731 if (fClampUni.isValid()) {
732 float subset[] = {c.fLeft, c.fTop, c.fRight, c.fBottom};
733 pushRect(subset, fClampUni);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500734 }
Brian Salomond71548a2020-02-29 19:43:30 -0500735 if (fBorderUni.isValid()) {
736 pdm.set4fv(fBorderUni, 1, te.fBorder);
737 }
Brian Salomonb8f098d2020-01-07 11:15:44 -0500738 }
739 };
740 return new Impl;
741}
742
Brian Salomonca6b2f42020-01-24 11:31:21 -0500743void GrTextureEffect::onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const {
Brian Salomonca6b2f42020-01-24 11:31:21 -0500744 auto m0 = static_cast<uint32_t>(fShaderModes[0]);
745 auto m1 = static_cast<uint32_t>(fShaderModes[1]);
Brian Salomon91dea512020-06-18 11:05:33 -0400746 b->add32((m0 << 16) | m1);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500747}
Brian Salomonb8f098d2020-01-07 11:15:44 -0500748
Brian Salomonca6b2f42020-01-24 11:31:21 -0500749bool GrTextureEffect::onIsEqual(const GrFragmentProcessor& other) const {
Ethan Nicholas94996eb2020-04-01 13:46:10 -0400750 auto& that = other.cast<GrTextureEffect>();
Brian Salomond71548a2020-02-29 19:43:30 -0500751 if (fShaderModes[0] != that.fShaderModes[0] || fShaderModes[1] != that.fShaderModes[1]) {
752 return false;
753 }
754 if (fSubset != that.fSubset) {
755 return false;
756 }
Brian Salomon91dea512020-06-18 11:05:33 -0400757 if (this->hasClampToBorderShaderMode() && !std::equal(fBorder, fBorder + 4, that.fBorder)) {
Brian Salomond71548a2020-02-29 19:43:30 -0500758 return false;
759 }
760 return true;
Brian Salomonb8f098d2020-01-07 11:15:44 -0500761}
762
Greg Danield2ccbb52020-02-05 10:45:39 -0500763GrTextureEffect::GrTextureEffect(GrSurfaceProxyView view, SkAlphaType alphaType,
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400764 const Sampling& sampling, bool lazyProxyNormalization)
Brian Salomonb8f098d2020-01-07 11:15:44 -0500765 : GrFragmentProcessor(kGrTextureEffect_ClassID,
Brian Salomond71548a2020-02-29 19:43:30 -0500766 ModulateForSamplerOptFlags(alphaType, sampling.hasBorderAlpha()))
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400767 , fCoordTransform(SkMatrix::I())
Greg Danield2ccbb52020-02-05 10:45:39 -0500768 , fSampler(std::move(view), sampling.fHWSampler)
Brian Salomonca6b2f42020-01-24 11:31:21 -0500769 , fSubset(sampling.fShaderSubset)
Brian Salomon53c909e2020-02-13 13:54:24 -0500770 , fClamp(sampling.fShaderClamp)
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400771 , fShaderModes{sampling.fShaderModes[0], sampling.fShaderModes[1]}
772 , fLazyProxyNormalization(lazyProxyNormalization) {
Brian Salomonca6b2f42020-01-24 11:31:21 -0500773 // We always compare the range even when it isn't used so assert we have canonical don't care
774 // values.
775 SkASSERT(fShaderModes[0] != ShaderMode::kNone || (fSubset.fLeft == 0 && fSubset.fRight == 0));
776 SkASSERT(fShaderModes[1] != ShaderMode::kNone || (fSubset.fTop == 0 && fSubset.fBottom == 0));
Brian Salomonb8f098d2020-01-07 11:15:44 -0500777 this->setTextureSamplerCnt(1);
778 this->addCoordTransform(&fCoordTransform);
Brian Salomond71548a2020-02-29 19:43:30 -0500779 std::copy_n(sampling.fBorder, 4, fBorder);
Brian Salomonb8f098d2020-01-07 11:15:44 -0500780}
781
782GrTextureEffect::GrTextureEffect(const GrTextureEffect& src)
783 : INHERITED(kGrTextureEffect_ClassID, src.optimizationFlags())
784 , fCoordTransform(src.fCoordTransform)
Brian Salomonca6b2f42020-01-24 11:31:21 -0500785 , fSampler(src.fSampler)
786 , fSubset(src.fSubset)
Brian Salomon922496f2020-02-14 12:20:14 -0500787 , fClamp(src.fClamp)
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400788 , fShaderModes{src.fShaderModes[0], src.fShaderModes[1]}
789 , fLazyProxyNormalization(src.fLazyProxyNormalization) {
Brian Salomonba90ce92020-03-04 12:38:04 -0500790 std::copy_n(src.fBorder, 4, fBorder);
Brian Salomonb8f098d2020-01-07 11:15:44 -0500791 this->setTextureSamplerCnt(1);
792 this->addCoordTransform(&fCoordTransform);
793}
794
795std::unique_ptr<GrFragmentProcessor> GrTextureEffect::clone() const {
796 return std::unique_ptr<GrFragmentProcessor>(new GrTextureEffect(*this));
797}
798
799const GrFragmentProcessor::TextureSampler& GrTextureEffect::onTextureSampler(int) const {
800 return fSampler;
801}
802
803GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTextureEffect);
804#if GR_TEST_UTILS
805std::unique_ptr<GrFragmentProcessor> GrTextureEffect::TestCreate(GrProcessorTestData* testData) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500806 auto [view, ct, at] = testData->randomView();
Brian Salomon53c909e2020-02-13 13:54:24 -0500807 Mode wrapModes[2];
Brian Salomonb8f098d2020-01-07 11:15:44 -0500808 GrTest::TestWrapModes(testData->fRandom, wrapModes);
Brian Salomonb8f098d2020-01-07 11:15:44 -0500809
Brian Salomon53c909e2020-02-13 13:54:24 -0500810 Filter filter;
811 if (view.asTextureProxy()->mipMapped() == GrMipMapped::kYes) {
812 switch (testData->fRandom->nextULessThan(3)) {
813 case 0:
814 filter = Filter::kNearest;
815 break;
816 case 1:
817 filter = Filter::kBilerp;
818 break;
819 default:
820 filter = Filter::kMipMap;
821 break;
822 }
823 } else {
824 filter = testData->fRandom->nextBool() ? Filter::kBilerp : Filter::kNearest;
825 }
826 GrSamplerState params(wrapModes, filter);
Brian Salomonb8f098d2020-01-07 11:15:44 -0500827
828 const SkMatrix& matrix = GrTest::TestMatrix(testData->fRandom);
Greg Danield2ccbb52020-02-05 10:45:39 -0500829 return GrTextureEffect::Make(std::move(view), at, matrix, params, *testData->caps());
Brian Salomonb8f098d2020-01-07 11:15:44 -0500830}
831#endif