blob: b04e8a284fccfa25e415fc2f7c7779efe2476617 [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],
Brian Salomonb9b13732020-06-25 11:13:49 -040034 const GrCaps&,
35 SkVector bilerpInset = {0.5f, 0.5f});
Brian Salomond71548a2020-02-29 19:43:30 -050036 inline bool hasBorderAlpha() const;
37};
38
Brian Salomonca6b2f42020-01-24 11:31:21 -050039GrTextureEffect::Sampling::Sampling(const GrSurfaceProxy& proxy,
40 GrSamplerState sampler,
41 const SkRect& subset,
Brian Salomonca6b2f42020-01-24 11:31:21 -050042 const SkRect* domain,
Brian Salomond71548a2020-02-29 19:43:30 -050043 const float border[4],
Brian Salomonb9b13732020-06-25 11:13:49 -040044 const GrCaps& caps,
45 SkVector bilerpInset) {
Brian Salomon53c909e2020-02-13 13:54:24 -050046 struct Span {
47 float fA = 0.f, fB = 0.f;
Brian Salomonca6b2f42020-01-24 11:31:21 -050048
Brian Salomon53c909e2020-02-13 13:54:24 -050049 Span makeInset(float o) const {
50 Span r = {fA + o, fB - o};
51 if (r.fA > r.fB) {
52 r.fA = r.fB = (r.fA + r.fB) / 2;
53 }
Brian Salomonca6b2f42020-01-24 11:31:21 -050054 return r;
55 }
56
Brian Salomon53c909e2020-02-13 13:54:24 -050057 bool contains(Span r) const { return fA <= r.fA && fB >= r.fB; }
58 };
59 struct Result1D {
60 ShaderMode fShaderMode;
61 Span fShaderSubset;
62 Span fShaderClamp;
63 Mode fHWMode;
64 };
65
66 auto type = proxy.asTextureProxy()->textureType();
67 auto filter = sampler.filter();
68
Brian Salomonb9b13732020-06-25 11:13:49 -040069 auto resolve = [&](int size, Mode mode, Span subset, Span domain, float bilerpInset) {
Brian Salomon53c909e2020-02-13 13:54:24 -050070 Result1D r;
Brian Salomond71548a2020-02-29 19:43:30 -050071 bool canDoModeInHW = true;
72 // TODO: Use HW border color when available.
73 if (mode == Mode::kClampToBorder &&
74 (!caps.clampToBorderSupport() || border[0] || border[1] || border[2] || border[3])) {
75 canDoModeInHW = false;
76 } else if (mode != Mode::kClamp && !caps.npotTextureTileSupport() && !SkIsPow2(size)) {
77 canDoModeInHW = false;
78 } else if (type != GrTextureType::k2D &&
79 !(mode == Mode::kClamp || mode == Mode::kClampToBorder)) {
80 canDoModeInHW = false;
81 }
82 if (canDoModeInHW && size > 0 && subset.fA <= 0 && subset.fB >= size) {
Brian Salomon53c909e2020-02-13 13:54:24 -050083 r.fShaderMode = ShaderMode::kNone;
84 r.fHWMode = mode;
85 r.fShaderSubset = r.fShaderClamp = {0, 0};
86 return r;
87 }
88
89 r.fShaderSubset = subset;
Brian Salomoned729f92020-02-05 12:15:18 -050090 bool domainIsSafe = false;
Brian Salomoned729f92020-02-05 12:15:18 -050091 if (filter == Filter::kNearest) {
92 Span isubset{sk_float_floor(subset.fA), sk_float_ceil(subset.fB)};
93 if (domain.fA > isubset.fA && domain.fB < isubset.fB) {
94 domainIsSafe = true;
95 }
Brian Salomon53c909e2020-02-13 13:54:24 -050096 // This inset prevents sampling neighboring texels that could occur when
97 // texture coords fall exactly at texel boundaries (depending on precision
98 // and GPU-specific snapping at the boundary).
99 r.fShaderClamp = isubset.makeInset(0.5f);
100 } else {
Brian Salomonb9b13732020-06-25 11:13:49 -0400101 r.fShaderClamp = subset.makeInset(bilerpInset);
Brian Salomon53c909e2020-02-13 13:54:24 -0500102 if (r.fShaderClamp.contains(domain)) {
103 domainIsSafe = true;
Brian Salomoned729f92020-02-05 12:15:18 -0500104 }
Brian Salomoned729f92020-02-05 12:15:18 -0500105 }
Brian Salomon53c909e2020-02-13 13:54:24 -0500106 if (domainIsSafe) {
107 // The domain of coords that will be used won't access texels outside of the subset.
108 // So the wrap mode effectively doesn't matter. We use kClamp since it is always
109 // supported.
Brian Salomonca6b2f42020-01-24 11:31:21 -0500110 r.fShaderMode = ShaderMode::kNone;
Brian Salomon53c909e2020-02-13 13:54:24 -0500111 r.fHWMode = Mode::kClamp;
112 r.fShaderSubset = r.fShaderClamp = {0, 0};
Brian Salomonca6b2f42020-01-24 11:31:21 -0500113 return r;
114 }
Brian Salomon91dea512020-06-18 11:05:33 -0400115 r.fShaderMode = GetShaderMode(mode, filter);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500116 r.fHWMode = Mode::kClamp;
117 return r;
118 };
119
120 SkISize dim = proxy.isFullyLazy() ? SkISize{-1, -1} : proxy.backingStoreDimensions();
121
122 Span subsetX{subset.fLeft, subset.fRight};
123 auto domainX = domain ? Span{domain->fLeft, domain->fRight}
124 : Span{SK_FloatNegativeInfinity, SK_FloatInfinity};
Brian Salomonb9b13732020-06-25 11:13:49 -0400125 auto x = resolve(dim.width(), sampler.wrapModeX(), subsetX, domainX, bilerpInset.fX);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500126
127 Span subsetY{subset.fTop, subset.fBottom};
128 auto domainY = domain ? Span{domain->fTop, domain->fBottom}
129 : Span{SK_FloatNegativeInfinity, SK_FloatInfinity};
Brian Salomonb9b13732020-06-25 11:13:49 -0400130 auto y = resolve(dim.height(), sampler.wrapModeY(), subsetY, domainY, bilerpInset.fY);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500131
Brian Salomon53c909e2020-02-13 13:54:24 -0500132 fHWSampler = {x.fHWMode, y.fHWMode, filter};
Brian Salomonca6b2f42020-01-24 11:31:21 -0500133 fShaderModes[0] = x.fShaderMode;
134 fShaderModes[1] = y.fShaderMode;
135 fShaderSubset = {x.fShaderSubset.fA, y.fShaderSubset.fA,
136 x.fShaderSubset.fB, y.fShaderSubset.fB};
Brian Salomon53c909e2020-02-13 13:54:24 -0500137 fShaderClamp = {x.fShaderClamp.fA, y.fShaderClamp.fA,
138 x.fShaderClamp.fB, y.fShaderClamp.fB};
Brian Salomond71548a2020-02-29 19:43:30 -0500139 std::copy_n(border, 4, fBorder);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500140}
141
Brian Salomond71548a2020-02-29 19:43:30 -0500142bool GrTextureEffect::Sampling::hasBorderAlpha() const {
143 if (fHWSampler.wrapModeX() == GrSamplerState::WrapMode::kClampToBorder ||
144 fHWSampler.wrapModeY() == GrSamplerState::WrapMode::kClampToBorder) {
145 return true;
146 }
Brian Salomon91dea512020-06-18 11:05:33 -0400147 if (ShaderModeIsClampToBorder(fShaderModes[0]) || ShaderModeIsClampToBorder(fShaderModes[1])) {
Brian Salomond71548a2020-02-29 19:43:30 -0500148 return fBorder[3] < 1.f;
149 }
150 return false;
Brian Salomonca6b2f42020-01-24 11:31:21 -0500151}
152
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400153static void get_matrix(const SkMatrix& preMatrix, const GrSurfaceProxyView& view,
154 SkMatrix* outMatrix, bool* outLazyProxyNormalization) {
155 SkMatrix combined = preMatrix;
156 bool normalize = view.proxy()->backendFormat().textureType() != GrTextureType::kRectangle;
157 if (normalize) {
158 if (view.proxy()->isFullyLazy()) {
159 *outLazyProxyNormalization = true;
160 } else {
161 SkMatrixPriv::PostIDiv(&combined, view.proxy()->backingStoreDimensions().fWidth,
162 view.proxy()->backingStoreDimensions().fHeight);
163 *outLazyProxyNormalization = false;
164 }
165 } else {
166 *outLazyProxyNormalization = false;
167 }
168 if (view.origin() == kBottomLeft_GrSurfaceOrigin) {
169 if (normalize) {
170 // combined.postScale(1,-1);
171 // combined.postTranslate(0,1);
172 combined.set(SkMatrix::kMSkewY,
173 combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]);
174 combined.set(SkMatrix::kMScaleY,
175 combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
176 combined.set(SkMatrix::kMTransY,
177 combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
178 } else {
179 // combined.postScale(1, -1);
180 // combined.postTranslate(0,1);
181 SkScalar h = view.proxy()->backingStoreDimensions().fHeight;
182 combined.set(SkMatrix::kMSkewY,
183 h * combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]);
184 combined.set(SkMatrix::kMScaleY,
185 h * combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
186 combined.set(SkMatrix::kMTransY,
187 h * combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
188 }
189 }
190 *outMatrix = combined;
191}
192
Greg Danield2ccbb52020-02-05 10:45:39 -0500193std::unique_ptr<GrFragmentProcessor> GrTextureEffect::Make(GrSurfaceProxyView view,
Brian Salomonb8f098d2020-01-07 11:15:44 -0500194 SkAlphaType alphaType,
195 const SkMatrix& matrix,
Brian Salomon53c909e2020-02-13 13:54:24 -0500196 Filter filter) {
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400197 SkMatrix final;
198 bool lazyProxyNormalization;
199 get_matrix(matrix, view, &final, &lazyProxyNormalization);
Brian Osman273944a2020-05-29 16:44:26 -0400200 return GrMatrixEffect::Make(final, std::unique_ptr<GrFragmentProcessor>(
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400201 new GrTextureEffect(std::move(view),
202 alphaType,
203 Sampling(filter),
204 lazyProxyNormalization)));
Brian Salomonca6b2f42020-01-24 11:31:21 -0500205}
206
Greg Danield2ccbb52020-02-05 10:45:39 -0500207std::unique_ptr<GrFragmentProcessor> GrTextureEffect::Make(GrSurfaceProxyView view,
Brian Salomonca6b2f42020-01-24 11:31:21 -0500208 SkAlphaType alphaType,
209 const SkMatrix& matrix,
210 GrSamplerState sampler,
Brian Salomond71548a2020-02-29 19:43:30 -0500211 const GrCaps& caps,
212 const float border[4]) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500213 Sampling sampling(*view.proxy(), sampler, SkRect::Make(view.proxy()->dimensions()), nullptr,
Brian Salomond71548a2020-02-29 19:43:30 -0500214 border, caps);
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400215 SkMatrix final;
216 bool lazyProxyNormalization;
217 get_matrix(matrix, view, &final, &lazyProxyNormalization);
Brian Osman273944a2020-05-29 16:44:26 -0400218 return GrMatrixEffect::Make(final, std::unique_ptr<GrFragmentProcessor>(
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400219 new GrTextureEffect(std::move(view),
220 alphaType,
221 sampling,
222 lazyProxyNormalization)));
Brian Salomonca6b2f42020-01-24 11:31:21 -0500223}
224
Greg Danield2ccbb52020-02-05 10:45:39 -0500225std::unique_ptr<GrFragmentProcessor> GrTextureEffect::MakeSubset(GrSurfaceProxyView view,
Brian Salomonca6b2f42020-01-24 11:31:21 -0500226 SkAlphaType alphaType,
227 const SkMatrix& matrix,
228 GrSamplerState sampler,
229 const SkRect& subset,
Brian Salomond71548a2020-02-29 19:43:30 -0500230 const GrCaps& caps,
231 const float border[4]) {
232 Sampling sampling(*view.proxy(), sampler, subset, nullptr, border, caps);
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400233 SkMatrix final;
234 bool lazyProxyNormalization;
235 get_matrix(matrix, view, &final, &lazyProxyNormalization);
Brian Osman273944a2020-05-29 16:44:26 -0400236 return GrMatrixEffect::Make(final, std::unique_ptr<GrFragmentProcessor>(
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400237 new GrTextureEffect(std::move(view),
238 alphaType,
239 sampling,
240 lazyProxyNormalization)));
Brian Salomonca6b2f42020-01-24 11:31:21 -0500241}
242
Greg Danield2ccbb52020-02-05 10:45:39 -0500243std::unique_ptr<GrFragmentProcessor> GrTextureEffect::MakeSubset(GrSurfaceProxyView view,
Brian Salomonca6b2f42020-01-24 11:31:21 -0500244 SkAlphaType alphaType,
245 const SkMatrix& matrix,
246 GrSamplerState sampler,
247 const SkRect& subset,
248 const SkRect& domain,
Brian Salomond71548a2020-02-29 19:43:30 -0500249 const GrCaps& caps,
250 const float border[4]) {
251 Sampling sampling(*view.proxy(), sampler, subset, &domain, border, caps);
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400252 SkMatrix final;
253 bool lazyProxyNormalization;
254 get_matrix(matrix, view, &final, &lazyProxyNormalization);
Brian Osman273944a2020-05-29 16:44:26 -0400255 return GrMatrixEffect::Make(final, std::unique_ptr<GrFragmentProcessor>(
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400256 new GrTextureEffect(std::move(view),
257 alphaType,
258 sampling,
259 lazyProxyNormalization)));
Brian Salomonb8f098d2020-01-07 11:15:44 -0500260}
261
Brian Salomonb9b13732020-06-25 11:13:49 -0400262std::unique_ptr<GrFragmentProcessor> GrTextureEffect::MakeBilerpWithInset(
263 GrSurfaceProxyView view,
264 SkAlphaType alphaType,
265 const SkMatrix& matrix,
266 GrSamplerState::WrapMode wx,
267 GrSamplerState::WrapMode wy,
268 const SkRect& subset,
269 SkVector inset,
270 const GrCaps& caps,
271 const float border[4]) {
272 GrSamplerState sampler(wx, wy, GrSamplerState::Filter::kBilerp);
273 Sampling sampling(*view.proxy(), sampler, subset, nullptr, border, caps, inset);
274 SkMatrix final;
275 bool lazyProxyNormalization;
276 get_matrix(matrix, view, &final, &lazyProxyNormalization);
277 return GrMatrixEffect::Make(
278 final, std::unique_ptr<GrFragmentProcessor>(new GrTextureEffect(
279 std::move(view), alphaType, sampling, lazyProxyNormalization)));
280}
281
Brian Salomon91dea512020-06-18 11:05:33 -0400282GrTextureEffect::ShaderMode GrTextureEffect::GetShaderMode(GrSamplerState::WrapMode mode,
283 GrSamplerState::Filter filter) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500284 switch (mode) {
Brian Salomon91dea512020-06-18 11:05:33 -0400285 case GrSamplerState::WrapMode::kMirrorRepeat:
286 return ShaderMode::kMirrorRepeat;
287 case GrSamplerState::WrapMode::kClamp:
288 return ShaderMode::kClamp;
289 case GrSamplerState::WrapMode::kRepeat:
Brian Salomon53c909e2020-02-13 13:54:24 -0500290 switch (filter) {
291 case GrSamplerState::Filter::kNearest:
Brian Salomon91dea512020-06-18 11:05:33 -0400292 return ShaderMode::kRepeatNearest;
Brian Salomon53c909e2020-02-13 13:54:24 -0500293 case GrSamplerState::Filter::kBilerp:
Brian Salomon91dea512020-06-18 11:05:33 -0400294 return ShaderMode::kRepeatBilerp;
Brian Salomon53c909e2020-02-13 13:54:24 -0500295 case GrSamplerState::Filter::kMipMap:
Brian Salomon91dea512020-06-18 11:05:33 -0400296 return ShaderMode::kRepeatMipMap;
Brian Salomon53c909e2020-02-13 13:54:24 -0500297 }
298 SkUNREACHABLE;
Brian Salomon91dea512020-06-18 11:05:33 -0400299 case GrSamplerState::WrapMode::kClampToBorder:
300 return filter == GrSamplerState::Filter::kNearest ? ShaderMode::kClampToBorderNearest
301 : ShaderMode::kClampToBorderFilter;
Brian Salomon53c909e2020-02-13 13:54:24 -0500302 }
303 SkUNREACHABLE;
304}
305
Brian Salomon91dea512020-06-18 11:05:33 -0400306inline bool GrTextureEffect::ShaderModeIsClampToBorder(ShaderMode m) {
307 return m == ShaderMode::kClampToBorderNearest || m == ShaderMode::kClampToBorderFilter;
308}
309
Brian Salomonb8f098d2020-01-07 11:15:44 -0500310GrGLSLFragmentProcessor* GrTextureEffect::onCreateGLSLInstance() const {
311 class Impl : public GrGLSLFragmentProcessor {
Brian Salomonca6b2f42020-01-24 11:31:21 -0500312 UniformHandle fSubsetUni;
Brian Salomon53c909e2020-02-13 13:54:24 -0500313 UniformHandle fClampUni;
314 UniformHandle fNormUni;
Brian Salomond71548a2020-02-29 19:43:30 -0500315 UniformHandle fBorderUni;
Brian Salomonca6b2f42020-01-24 11:31:21 -0500316
Brian Salomonb8f098d2020-01-07 11:15:44 -0500317 public:
318 void emitCode(EmitArgs& args) override {
Ethan Nicholas94996eb2020-04-01 13:46:10 -0400319 auto& te = args.fFp.cast<GrTextureEffect>();
Brian Salomonb8f098d2020-01-07 11:15:44 -0500320 auto* fb = args.fFragBuilder;
Michael Ludwig89dd4e72020-06-03 15:39:06 -0400321
Brian Salomonca6b2f42020-01-24 11:31:21 -0500322 if (te.fShaderModes[0] == ShaderMode::kNone &&
323 te.fShaderModes[1] == ShaderMode::kNone) {
Ethan Nicholas36a3e012020-04-17 19:00:28 +0000324 fb->codeAppendf("%s = ", args.fOutputColor);
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400325 if (te.fLazyProxyNormalization) {
326 const char* norm = nullptr;
327 fNormUni = args.fUniformHandler->addUniform(&te, kFragment_GrShaderFlag,
328 kFloat4_GrSLType, "norm", &norm);
329 fb->appendTextureLookupAndBlend(args.fInputColor, SkBlendMode::kModulate,
330 args.fTexSamplers[0],
Michael Ludwige88320b2020-06-24 09:04:56 -0400331 SkStringPrintf("%s * %s.zw", args.fSampleCoord,
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400332 norm).c_str());
333 } else {
334 fb->appendTextureLookupAndBlend(args.fInputColor, SkBlendMode::kModulate,
Michael Ludwige88320b2020-06-24 09:04:56 -0400335 args.fTexSamplers[0], args.fSampleCoord);
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400336 }
Brian Salomonca6b2f42020-01-24 11:31:21 -0500337 fb->codeAppendf(";");
338 } else {
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400339 // Tripping this assert means we have a normalized fully lazy proxy with a
340 // non-default ShaderMode. There's nothing fundamentally wrong with doing that, but
341 // it hasn't been tested and this code path probably won't handle normalization
342 // properly in that case.
343 SkASSERT(!te.fLazyProxyNormalization);
Brian Salomon53c909e2020-02-13 13:54:24 -0500344 // Here is the basic flow of the various ShaderModes are implemented in a series of
345 // steps. Not all the steps apply to all the modes. We try to emit only the steps
346 // that are necessary for the given x/y shader modes.
347 //
348 // 0) Start with interpolated coordinates (unnormalize if doing anything
349 // complicated).
350 // 1) Map the coordinates into the subset range [Repeat and MirrorRepeat], or pass
351 // through output of 0).
352 // 2) Clamp the coordinates to a 0.5 inset of the subset rect [Clamp, Repeat, and
Brian Salomond71548a2020-02-29 19:43:30 -0500353 // MirrorRepeat always or ClampToBorder only when filtering] or pass through
354 // output of 1). The clamp rect collapses to a line or point it if the subset
355 // rect is less than one pixel wide/tall.
Brian Salomon53c909e2020-02-13 13:54:24 -0500356 // 3) Look up texture with output of 2) [All]
357 // 3) Use the difference between 1) and 2) to apply filtering at edge [Repeat or
Brian Salomond71548a2020-02-29 19:43:30 -0500358 // ClampToBorder]. In the Repeat case this requires extra texture lookups on the
359 // other side of the subset (up to 3 more reads). Or if ClampToBorder and not
360 // filtering do a hard less than/greater than test with the subset rect.
Brian Salomonca6b2f42020-01-24 11:31:21 -0500361
Brian Salomon53c909e2020-02-13 13:54:24 -0500362 // Convert possible projective texture coordinates into non-homogeneous half2.
Michael Ludwige88320b2020-06-24 09:04:56 -0400363 fb->codeAppendf("float2 inCoord = %s;", args.fSampleCoord);
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
Michael Ludwig724701f2020-06-18 17:52:49 -0400674 // subset boundaries. Snap the input coordinates to nearest neighbor (with an
675 // epsilon) before comparing to the subset rect to avoid GPU interpolation errors
Brian Salomon91dea512020-06-18 11:05:33 -0400676 if (m[0] == ShaderMode::kClampToBorderNearest) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500677 fb->codeAppendf(
Michael Ludwig724701f2020-06-18 17:52:49 -0400678 "float snappedX = floor(inCoord.x + 0.001) + 0.5;"
679 "if (snappedX < %s.x || snappedX > %s.z) {"
Brian Salomond71548a2020-02-29 19:43:30 -0500680 " textureColor = %s;"
Brian Salomon53c909e2020-02-13 13:54:24 -0500681 "}",
Brian Salomond71548a2020-02-29 19:43:30 -0500682 subsetName, subsetName, borderName);
Brian Salomon53c909e2020-02-13 13:54:24 -0500683 }
Brian Salomon91dea512020-06-18 11:05:33 -0400684 if (m[1] == ShaderMode::kClampToBorderNearest) {
Brian Salomon53c909e2020-02-13 13:54:24 -0500685 fb->codeAppendf(
Michael Ludwig724701f2020-06-18 17:52:49 -0400686 "float snappedY = floor(inCoord.y + 0.001) + 0.5;"
687 "if (snappedY < %s.y || snappedY > %s.w) {"
Brian Salomond71548a2020-02-29 19:43:30 -0500688 " textureColor = %s;"
Brian Salomon53c909e2020-02-13 13:54:24 -0500689 "}",
Brian Salomond71548a2020-02-29 19:43:30 -0500690 subsetName, subsetName, borderName);
Brian Salomon53c909e2020-02-13 13:54:24 -0500691 }
692 fb->codeAppendf("%s = %s * textureColor;", args.fOutputColor, args.fInputColor);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500693 }
694 }
695
696 protected:
697 void onSetData(const GrGLSLProgramDataManager& pdm,
698 const GrFragmentProcessor& fp) override {
699 const auto& te = fp.cast<GrTextureEffect>();
Brian Salomonca6b2f42020-01-24 11:31:21 -0500700
Brian Salomon53c909e2020-02-13 13:54:24 -0500701 const float w = te.fSampler.peekTexture()->width();
702 const float h = te.fSampler.peekTexture()->height();
703 const auto& s = te.fSubset;
704 const auto& c = te.fClamp;
Brian Salomonca6b2f42020-01-24 11:31:21 -0500705
Brian Salomon53c909e2020-02-13 13:54:24 -0500706 auto type = te.fSampler.peekTexture()->texturePriv().textureType();
707
708 float norm[4] = {w, h, 1.f/w, 1.f/h};
709
710 if (fNormUni.isValid()) {
711 pdm.set4fv(fNormUni, 1, norm);
712 SkASSERT(type != GrTextureType::kRectangle);
713 }
714
715 auto pushRect = [&](float rect[4], UniformHandle uni) {
Brian Salomonca6b2f42020-01-24 11:31:21 -0500716 if (te.fSampler.view().origin() == kBottomLeft_GrSurfaceOrigin) {
717 rect[1] = h - rect[1];
718 rect[3] = h - rect[3];
719 std::swap(rect[1], rect[3]);
720 }
Brian Salomon53c909e2020-02-13 13:54:24 -0500721 if (!fNormUni.isValid() && type != GrTextureType::kRectangle) {
722 rect[0] *= norm[2];
723 rect[2] *= norm[2];
724 rect[1] *= norm[3];
725 rect[3] *= norm[3];
Brian Salomonca6b2f42020-01-24 11:31:21 -0500726 }
Brian Salomon53c909e2020-02-13 13:54:24 -0500727 pdm.set4fv(uni, 1, rect);
728 };
Brian Salomonca6b2f42020-01-24 11:31:21 -0500729
Brian Salomon53c909e2020-02-13 13:54:24 -0500730 if (fSubsetUni.isValid()) {
731 float subset[] = {s.fLeft, s.fTop, s.fRight, s.fBottom};
732 pushRect(subset, fSubsetUni);
733 }
734 if (fClampUni.isValid()) {
735 float subset[] = {c.fLeft, c.fTop, c.fRight, c.fBottom};
736 pushRect(subset, fClampUni);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500737 }
Brian Salomond71548a2020-02-29 19:43:30 -0500738 if (fBorderUni.isValid()) {
739 pdm.set4fv(fBorderUni, 1, te.fBorder);
740 }
Brian Salomonb8f098d2020-01-07 11:15:44 -0500741 }
742 };
743 return new Impl;
744}
745
Brian Salomonca6b2f42020-01-24 11:31:21 -0500746void GrTextureEffect::onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const {
Brian Salomonca6b2f42020-01-24 11:31:21 -0500747 auto m0 = static_cast<uint32_t>(fShaderModes[0]);
748 auto m1 = static_cast<uint32_t>(fShaderModes[1]);
Brian Salomon91dea512020-06-18 11:05:33 -0400749 b->add32((m0 << 16) | m1);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500750}
Brian Salomonb8f098d2020-01-07 11:15:44 -0500751
Brian Salomonca6b2f42020-01-24 11:31:21 -0500752bool GrTextureEffect::onIsEqual(const GrFragmentProcessor& other) const {
Ethan Nicholas94996eb2020-04-01 13:46:10 -0400753 auto& that = other.cast<GrTextureEffect>();
Brian Salomond71548a2020-02-29 19:43:30 -0500754 if (fShaderModes[0] != that.fShaderModes[0] || fShaderModes[1] != that.fShaderModes[1]) {
755 return false;
756 }
757 if (fSubset != that.fSubset) {
758 return false;
759 }
Brian Salomon91dea512020-06-18 11:05:33 -0400760 if (this->hasClampToBorderShaderMode() && !std::equal(fBorder, fBorder + 4, that.fBorder)) {
Brian Salomond71548a2020-02-29 19:43:30 -0500761 return false;
762 }
763 return true;
Brian Salomonb8f098d2020-01-07 11:15:44 -0500764}
765
Greg Danield2ccbb52020-02-05 10:45:39 -0500766GrTextureEffect::GrTextureEffect(GrSurfaceProxyView view, SkAlphaType alphaType,
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400767 const Sampling& sampling, bool lazyProxyNormalization)
Brian Salomonb8f098d2020-01-07 11:15:44 -0500768 : GrFragmentProcessor(kGrTextureEffect_ClassID,
Brian Salomond71548a2020-02-29 19:43:30 -0500769 ModulateForSamplerOptFlags(alphaType, sampling.hasBorderAlpha()))
Greg Danield2ccbb52020-02-05 10:45:39 -0500770 , fSampler(std::move(view), sampling.fHWSampler)
Brian Salomonca6b2f42020-01-24 11:31:21 -0500771 , fSubset(sampling.fShaderSubset)
Brian Salomon53c909e2020-02-13 13:54:24 -0500772 , fClamp(sampling.fShaderClamp)
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400773 , fShaderModes{sampling.fShaderModes[0], sampling.fShaderModes[1]}
774 , fLazyProxyNormalization(lazyProxyNormalization) {
Brian Salomonca6b2f42020-01-24 11:31:21 -0500775 // We always compare the range even when it isn't used so assert we have canonical don't care
776 // values.
777 SkASSERT(fShaderModes[0] != ShaderMode::kNone || (fSubset.fLeft == 0 && fSubset.fRight == 0));
778 SkASSERT(fShaderModes[1] != ShaderMode::kNone || (fSubset.fTop == 0 && fSubset.fBottom == 0));
Brian Salomonb8f098d2020-01-07 11:15:44 -0500779 this->setTextureSamplerCnt(1);
Michael Ludwige88320b2020-06-24 09:04:56 -0400780 this->setUsesSampleCoordsDirectly();
Brian Salomond71548a2020-02-29 19:43:30 -0500781 std::copy_n(sampling.fBorder, 4, fBorder);
Brian Salomonb8f098d2020-01-07 11:15:44 -0500782}
783
784GrTextureEffect::GrTextureEffect(const GrTextureEffect& src)
785 : INHERITED(kGrTextureEffect_ClassID, src.optimizationFlags())
Brian Salomonca6b2f42020-01-24 11:31:21 -0500786 , fSampler(src.fSampler)
787 , fSubset(src.fSubset)
Brian Salomon922496f2020-02-14 12:20:14 -0500788 , fClamp(src.fClamp)
Ethan Nicholasafe2c902020-04-28 13:55:02 -0400789 , fShaderModes{src.fShaderModes[0], src.fShaderModes[1]}
790 , fLazyProxyNormalization(src.fLazyProxyNormalization) {
Brian Salomonba90ce92020-03-04 12:38:04 -0500791 std::copy_n(src.fBorder, 4, fBorder);
Brian Salomonb8f098d2020-01-07 11:15:44 -0500792 this->setTextureSamplerCnt(1);
Michael Ludwige88320b2020-06-24 09:04:56 -0400793 this->setUsesSampleCoordsDirectly();
Brian Salomonb8f098d2020-01-07 11:15:44 -0500794}
795
796std::unique_ptr<GrFragmentProcessor> GrTextureEffect::clone() const {
797 return std::unique_ptr<GrFragmentProcessor>(new GrTextureEffect(*this));
798}
799
800const GrFragmentProcessor::TextureSampler& GrTextureEffect::onTextureSampler(int) const {
801 return fSampler;
802}
803
804GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTextureEffect);
805#if GR_TEST_UTILS
806std::unique_ptr<GrFragmentProcessor> GrTextureEffect::TestCreate(GrProcessorTestData* testData) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500807 auto [view, ct, at] = testData->randomView();
Brian Salomon53c909e2020-02-13 13:54:24 -0500808 Mode wrapModes[2];
Brian Salomonb8f098d2020-01-07 11:15:44 -0500809 GrTest::TestWrapModes(testData->fRandom, wrapModes);
Brian Salomonb8f098d2020-01-07 11:15:44 -0500810
Brian Salomon53c909e2020-02-13 13:54:24 -0500811 Filter filter;
812 if (view.asTextureProxy()->mipMapped() == GrMipMapped::kYes) {
813 switch (testData->fRandom->nextULessThan(3)) {
814 case 0:
815 filter = Filter::kNearest;
816 break;
817 case 1:
818 filter = Filter::kBilerp;
819 break;
820 default:
821 filter = Filter::kMipMap;
822 break;
823 }
824 } else {
825 filter = testData->fRandom->nextBool() ? Filter::kBilerp : Filter::kNearest;
826 }
827 GrSamplerState params(wrapModes, filter);
Brian Salomonb8f098d2020-01-07 11:15:44 -0500828
829 const SkMatrix& matrix = GrTest::TestMatrix(testData->fRandom);
Greg Danield2ccbb52020-02-05 10:45:39 -0500830 return GrTextureEffect::Make(std::move(view), at, matrix, params, *testData->caps());
Brian Salomonb8f098d2020-01-07 11:15:44 -0500831}
832#endif