1 | /* |
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 | |
10 | #include "src/gpu/GrTexture.h" |
11 | #include "src/gpu/GrTexturePriv.h" |
12 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
13 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
14 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
15 | #include "src/sksl/SkSLCPP.h" |
16 | #include "src/sksl/SkSLUtil.h" |
17 | |
18 | using Mode = GrSamplerState::WrapMode; |
19 | using Filter = GrSamplerState::Filter; |
20 | |
21 | struct GrTextureEffect::Sampling { |
22 | GrSamplerState fHWSampler; |
23 | ShaderMode fShaderModes[2] = {ShaderMode::kNone, ShaderMode::kNone}; |
24 | SkRect fShaderSubset = {0, 0, 0, 0}; |
25 | SkRect fShaderClamp = {0, 0, 0, 0}; |
26 | float fBorder[4] = {0, 0, 0, 0}; |
27 | Sampling(GrSamplerState::Filter filter) : fHWSampler(filter) {} |
28 | Sampling(const GrSurfaceProxy& proxy, |
29 | GrSamplerState sampler, |
30 | const SkRect&, |
31 | const SkRect*, |
32 | const float border[4], |
33 | const GrCaps&); |
34 | inline bool hasBorderAlpha() const; |
35 | }; |
36 | |
37 | GrTextureEffect::Sampling::Sampling(const GrSurfaceProxy& proxy, |
38 | GrSamplerState sampler, |
39 | const SkRect& subset, |
40 | const SkRect* domain, |
41 | const float border[4], |
42 | const GrCaps& caps) { |
43 | struct Span { |
44 | float fA = 0.f, fB = 0.f; |
45 | |
46 | Span makeInset(float o) const { |
47 | Span r = {fA + o, fB - o}; |
48 | if (r.fA > r.fB) { |
49 | r.fA = r.fB = (r.fA + r.fB) / 2; |
50 | } |
51 | return r; |
52 | } |
53 | |
54 | bool contains(Span r) const { return fA <= r.fA && fB >= r.fB; } |
55 | }; |
56 | struct Result1D { |
57 | ShaderMode fShaderMode; |
58 | Span fShaderSubset; |
59 | Span fShaderClamp; |
60 | Mode fHWMode; |
61 | }; |
62 | |
63 | auto type = proxy.asTextureProxy()->textureType(); |
64 | auto filter = sampler.filter(); |
65 | |
66 | auto resolve = [type, &caps, filter, &border](int size, Mode mode, Span subset, Span domain) { |
67 | Result1D r; |
68 | bool canDoModeInHW = true; |
69 | // TODO: Use HW border color when available. |
70 | if (mode == Mode::kClampToBorder && |
71 | (!caps.clampToBorderSupport() || border[0] || border[1] || border[2] || border[3])) { |
72 | canDoModeInHW = false; |
73 | } else if (mode != Mode::kClamp && !caps.npotTextureTileSupport() && !SkIsPow2(size)) { |
74 | canDoModeInHW = false; |
75 | } else if (type != GrTextureType::k2D && |
76 | !(mode == Mode::kClamp || mode == Mode::kClampToBorder)) { |
77 | canDoModeInHW = false; |
78 | } |
79 | if (canDoModeInHW && size > 0 && subset.fA <= 0 && subset.fB >= size) { |
80 | r.fShaderMode = ShaderMode::kNone; |
81 | r.fHWMode = mode; |
82 | r.fShaderSubset = r.fShaderClamp = {0, 0}; |
83 | return r; |
84 | } |
85 | |
86 | r.fShaderSubset = subset; |
87 | bool domainIsSafe = false; |
88 | if (filter == Filter::kNearest) { |
89 | Span isubset{sk_float_floor(subset.fA), sk_float_ceil(subset.fB)}; |
90 | if (domain.fA > isubset.fA && domain.fB < isubset.fB) { |
91 | domainIsSafe = true; |
92 | } |
93 | // This inset prevents sampling neighboring texels that could occur when |
94 | // texture coords fall exactly at texel boundaries (depending on precision |
95 | // and GPU-specific snapping at the boundary). |
96 | r.fShaderClamp = isubset.makeInset(0.5f); |
97 | } else { |
98 | r.fShaderClamp = subset.makeInset(0.5f); |
99 | if (r.fShaderClamp.contains(domain)) { |
100 | domainIsSafe = true; |
101 | } |
102 | } |
103 | if (domainIsSafe) { |
104 | // The domain of coords that will be used won't access texels outside of the subset. |
105 | // So the wrap mode effectively doesn't matter. We use kClamp since it is always |
106 | // supported. |
107 | r.fShaderMode = ShaderMode::kNone; |
108 | r.fHWMode = Mode::kClamp; |
109 | r.fShaderSubset = r.fShaderClamp = {0, 0}; |
110 | return r; |
111 | } |
112 | r.fShaderMode = static_cast<ShaderMode>(mode); |
113 | r.fHWMode = Mode::kClamp; |
114 | return r; |
115 | }; |
116 | |
117 | SkISize dim = proxy.isFullyLazy() ? SkISize{-1, -1} : proxy.backingStoreDimensions(); |
118 | |
119 | Span subsetX{subset.fLeft, subset.fRight}; |
120 | auto domainX = domain ? Span{domain->fLeft, domain->fRight} |
121 | : Span{SK_FloatNegativeInfinity, SK_FloatInfinity}; |
122 | auto x = resolve(dim.width(), sampler.wrapModeX(), subsetX, domainX); |
123 | |
124 | Span subsetY{subset.fTop, subset.fBottom}; |
125 | auto domainY = domain ? Span{domain->fTop, domain->fBottom} |
126 | : Span{SK_FloatNegativeInfinity, SK_FloatInfinity}; |
127 | auto y = resolve(dim.height(), sampler.wrapModeY(), subsetY, domainY); |
128 | |
129 | fHWSampler = {x.fHWMode, y.fHWMode, filter}; |
130 | fShaderModes[0] = x.fShaderMode; |
131 | fShaderModes[1] = y.fShaderMode; |
132 | fShaderSubset = {x.fShaderSubset.fA, y.fShaderSubset.fA, |
133 | x.fShaderSubset.fB, y.fShaderSubset.fB}; |
134 | fShaderClamp = {x.fShaderClamp.fA, y.fShaderClamp.fA, |
135 | x.fShaderClamp.fB, y.fShaderClamp.fB}; |
136 | std::copy_n(border, 4, fBorder); |
137 | } |
138 | |
139 | bool GrTextureEffect::Sampling::hasBorderAlpha() const { |
140 | if (fHWSampler.wrapModeX() == GrSamplerState::WrapMode::kClampToBorder || |
141 | fHWSampler.wrapModeY() == GrSamplerState::WrapMode::kClampToBorder) { |
142 | return true; |
143 | } |
144 | if (fShaderModes[0] == ShaderMode::kClampToBorder || |
145 | fShaderModes[1] == ShaderMode::kClampToBorder) { |
146 | return fBorder[3] < 1.f; |
147 | } |
148 | return false; |
149 | } |
150 | |
151 | std::unique_ptr<GrFragmentProcessor> GrTextureEffect::Make(GrSurfaceProxyView view, |
152 | SkAlphaType alphaType, |
153 | const SkMatrix& matrix, |
154 | Filter filter) { |
155 | return std::unique_ptr<GrFragmentProcessor>( |
156 | new GrTextureEffect(std::move(view), alphaType, matrix, Sampling(filter))); |
157 | } |
158 | |
159 | std::unique_ptr<GrFragmentProcessor> GrTextureEffect::Make(GrSurfaceProxyView view, |
160 | SkAlphaType alphaType, |
161 | const SkMatrix& matrix, |
162 | GrSamplerState sampler, |
163 | const GrCaps& caps, |
164 | const float border[4]) { |
165 | Sampling sampling(*view.proxy(), sampler, SkRect::Make(view.proxy()->dimensions()), nullptr, |
166 | border, caps); |
167 | return std::unique_ptr<GrFragmentProcessor>( |
168 | new GrTextureEffect(std::move(view), alphaType, matrix, sampling)); |
169 | } |
170 | |
171 | std::unique_ptr<GrFragmentProcessor> GrTextureEffect::MakeSubset(GrSurfaceProxyView view, |
172 | SkAlphaType alphaType, |
173 | const SkMatrix& matrix, |
174 | GrSamplerState sampler, |
175 | const SkRect& subset, |
176 | const GrCaps& caps, |
177 | const float border[4]) { |
178 | Sampling sampling(*view.proxy(), sampler, subset, nullptr, border, caps); |
179 | return std::unique_ptr<GrFragmentProcessor>( |
180 | new GrTextureEffect(std::move(view), alphaType, matrix, sampling)); |
181 | } |
182 | |
183 | std::unique_ptr<GrFragmentProcessor> GrTextureEffect::MakeSubset(GrSurfaceProxyView view, |
184 | SkAlphaType alphaType, |
185 | const SkMatrix& matrix, |
186 | GrSamplerState sampler, |
187 | const SkRect& subset, |
188 | const SkRect& domain, |
189 | const GrCaps& caps, |
190 | const float border[4]) { |
191 | Sampling sampling(*view.proxy(), sampler, subset, &domain, border, caps); |
192 | return std::unique_ptr<GrFragmentProcessor>( |
193 | new GrTextureEffect(std::move(view), alphaType, matrix, sampling)); |
194 | } |
195 | |
196 | GrTextureEffect::FilterLogic GrTextureEffect::GetFilterLogic(ShaderMode mode, |
197 | GrSamplerState::Filter filter) { |
198 | switch (mode) { |
199 | case ShaderMode::kMirrorRepeat: |
200 | case ShaderMode::kNone: |
201 | case ShaderMode::kClamp: |
202 | return FilterLogic::kNone; |
203 | case ShaderMode::kRepeat: |
204 | switch (filter) { |
205 | case GrSamplerState::Filter::kNearest: |
206 | return FilterLogic::kNone; |
207 | case GrSamplerState::Filter::kBilerp: |
208 | return FilterLogic::kRepeatBilerp; |
209 | case GrSamplerState::Filter::kMipMap: |
210 | return FilterLogic::kRepeatMipMap; |
211 | } |
212 | SkUNREACHABLE; |
213 | case ShaderMode::kClampToBorder: |
214 | return filter > GrSamplerState::Filter::kNearest ? FilterLogic::kClampToBorderFilter |
215 | : FilterLogic::kClampToBorderNearest; |
216 | } |
217 | SkUNREACHABLE; |
218 | } |
219 | |
220 | GrGLSLFragmentProcessor* GrTextureEffect::onCreateGLSLInstance() const { |
221 | class Impl : public GrGLSLFragmentProcessor { |
222 | UniformHandle fSubsetUni; |
223 | UniformHandle fClampUni; |
224 | UniformHandle fNormUni; |
225 | UniformHandle fBorderUni; |
226 | |
227 | public: |
228 | void emitCode(EmitArgs& args) override { |
229 | auto& te = args.fFp.cast<GrTextureEffect>(); |
230 | const char* coords; |
231 | if (args.fFp.isSampledWithExplicitCoords()) { |
232 | coords = "_coords" ; |
233 | } else { |
234 | coords = args.fTransformedCoords[0].fVaryingPoint.c_str(); |
235 | } |
236 | auto* fb = args.fFragBuilder; |
237 | if (te.fShaderModes[0] == ShaderMode::kNone && |
238 | te.fShaderModes[1] == ShaderMode::kNone) { |
239 | fb->codeAppendf("%s = " , args.fOutputColor); |
240 | fb->appendTextureLookupAndBlend(args.fInputColor, SkBlendMode::kModulate, |
241 | args.fTexSamplers[0], coords); |
242 | fb->codeAppendf(";" ); |
243 | } else { |
244 | // Here is the basic flow of the various ShaderModes are implemented in a series of |
245 | // steps. Not all the steps apply to all the modes. We try to emit only the steps |
246 | // that are necessary for the given x/y shader modes. |
247 | // |
248 | // 0) Start with interpolated coordinates (unnormalize if doing anything |
249 | // complicated). |
250 | // 1) Map the coordinates into the subset range [Repeat and MirrorRepeat], or pass |
251 | // through output of 0). |
252 | // 2) Clamp the coordinates to a 0.5 inset of the subset rect [Clamp, Repeat, and |
253 | // MirrorRepeat always or ClampToBorder only when filtering] or pass through |
254 | // output of 1). The clamp rect collapses to a line or point it if the subset |
255 | // rect is less than one pixel wide/tall. |
256 | // 3) Look up texture with output of 2) [All] |
257 | // 3) Use the difference between 1) and 2) to apply filtering at edge [Repeat or |
258 | // ClampToBorder]. In the Repeat case this requires extra texture lookups on the |
259 | // other side of the subset (up to 3 more reads). Or if ClampToBorder and not |
260 | // filtering do a hard less than/greater than test with the subset rect. |
261 | |
262 | // Convert possible projective texture coordinates into non-homogeneous half2. |
263 | fb->codeAppendf( |
264 | "float2 inCoord = %s;" , |
265 | fb->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint).c_str()); |
266 | |
267 | const auto& m = te.fShaderModes; |
268 | GrTextureType textureType = te.fSampler.proxy()->backendFormat().textureType(); |
269 | bool normCoords = textureType != GrTextureType::kRectangle; |
270 | auto filter = te.fSampler.samplerState().filter(); |
271 | FilterLogic filterLogic[2] = {GetFilterLogic(m[0], filter), |
272 | GetFilterLogic(m[1], filter)}; |
273 | |
274 | const char* borderName = nullptr; |
275 | if (te.fShaderModes[0] == ShaderMode::kClampToBorder || |
276 | te.fShaderModes[1] == ShaderMode::kClampToBorder) { |
277 | fBorderUni = args.fUniformHandler->addUniform( |
278 | &te, kFragment_GrShaderFlag, kHalf4_GrSLType, "border" , &borderName); |
279 | } |
280 | auto modeUsesSubset = [](ShaderMode m) { |
281 | return m == ShaderMode::kRepeat || m == ShaderMode::kMirrorRepeat || |
282 | m == ShaderMode::kClampToBorder; |
283 | }; |
284 | |
285 | auto modeUsesClamp = [filter](ShaderMode m) { |
286 | return m != ShaderMode::kNone && |
287 | (m != ShaderMode::kClampToBorder || filter > Filter::kNearest); |
288 | }; |
289 | |
290 | bool useSubset[2] = {modeUsesSubset(m[0]), modeUsesSubset(m[1])}; |
291 | bool useClamp [2] = {modeUsesClamp (m[0]), modeUsesClamp (m[1])}; |
292 | |
293 | const char* subsetName = nullptr; |
294 | if (useSubset[0] || useSubset[1]) { |
295 | fSubsetUni = args.fUniformHandler->addUniform( |
296 | &te, kFragment_GrShaderFlag, kFloat4_GrSLType, "subset" , &subsetName); |
297 | } |
298 | |
299 | const char* clampName = nullptr; |
300 | if (useClamp[0] || useClamp[1]) { |
301 | fClampUni = args.fUniformHandler->addUniform( |
302 | &te, kFragment_GrShaderFlag, kFloat4_GrSLType, "clamp" , &clampName); |
303 | } |
304 | |
305 | // To keep things a little simpler, when we have filtering logic in the shader we |
306 | // operate on unnormalized texture coordinates. We add a uniform that stores |
307 | // {w, h, 1/w, 1/h} in a float4. |
308 | const char* norm = nullptr; |
309 | if (normCoords && (filterLogic[0] != FilterLogic::kNone || |
310 | filterLogic[1] != FilterLogic::kNone)) { |
311 | // TODO: Detect support for textureSize() or polyfill textureSize() in SkSL and |
312 | // always use? |
313 | fNormUni = args.fUniformHandler->addUniform(&te, kFragment_GrShaderFlag, |
314 | kFloat4_GrSLType, "norm" , &norm); |
315 | // TODO: Remove the normalization from the CoordTransform to skip unnormalizing |
316 | // step here. |
317 | fb->codeAppendf("inCoord *= %s.xy;" , norm); |
318 | } |
319 | |
320 | // Generates a string to read at a coordinate, normalizing coords if necessary. |
321 | auto read = [&](const char* coord) { |
322 | SkString result; |
323 | SkString normCoord; |
324 | if (norm) { |
325 | normCoord.printf("(%s) * %s.zw" , coord, norm); |
326 | } else { |
327 | normCoord = coord; |
328 | } |
329 | fb->appendTextureLookup(&result, args.fTexSamplers[0], normCoord.c_str()); |
330 | return result; |
331 | }; |
332 | |
333 | // Implements coord wrapping for kRepeat and kMirrorRepeat |
334 | auto subsetCoord = [&](ShaderMode mode, |
335 | const char* coordSwizzle, |
336 | const char* subsetStartSwizzle, |
337 | const char* subsetStopSwizzle, |
338 | const char* , |
339 | const char* coordWeight) { |
340 | switch (mode) { |
341 | // These modes either don't use the subset rect or don't need to map the |
342 | // coords to be within the subset. |
343 | case ShaderMode::kNone: |
344 | case ShaderMode::kClampToBorder: |
345 | case ShaderMode::kClamp: |
346 | fb->codeAppendf("subsetCoord.%s = inCoord.%s;" , coordSwizzle, |
347 | coordSwizzle); |
348 | break; |
349 | case ShaderMode::kRepeat: |
350 | if (filter == Filter::kMipMap) { |
351 | // The approach here is to generate two sets of texture coords that |
352 | // are both "moving" at the same speed (if not direction) as |
353 | // inCoords. We accomplish that by using two out of phase mirror |
354 | // repeat coords. We will always sample using both coords but the |
355 | // read from the upward sloping one is selected using a weight |
356 | // that transitions from one set to the other near the reflection |
357 | // point. Like the coords, the weight is a saw-tooth function, |
358 | // phase-shifted, vertically translated, and then clamped to 0..1. |
359 | // TODO: Skip this and use textureGrad() when available. |
360 | SkASSERT(extraCoord); |
361 | SkASSERT(coordWeight); |
362 | fb->codeAppend("{" ); |
363 | fb->codeAppendf("float w = %s.%s - %s.%s;" , subsetName, |
364 | subsetStopSwizzle, subsetName, subsetStartSwizzle); |
365 | fb->codeAppendf("float w2 = 2 * w;" ); |
366 | fb->codeAppendf("float d = inCoord.%s - %s.%s;" , coordSwizzle, |
367 | subsetName, subsetStartSwizzle); |
368 | fb->codeAppend("float m = mod(d, w2);" ); |
369 | fb->codeAppend("float o = mix(m, w2 - m, step(w, m));" ); |
370 | fb->codeAppendf("subsetCoord.%s = o + %s.%s;" , coordSwizzle, |
371 | subsetName, subsetStartSwizzle); |
372 | fb->codeAppendf("%s = w - o + %s.%s;" , extraCoord, subsetName, |
373 | subsetStartSwizzle); |
374 | // coordWeight is used as the third param of mix() to blend between a |
375 | // sample taken using subsetCoord and a sample at extraCoord. |
376 | fb->codeAppend("float hw = w/2;" ); |
377 | fb->codeAppend("float n = mod(d - hw, w2);" ); |
378 | fb->codeAppendf( |
379 | "%s = saturate(half(mix(n, w2 - n, step(w, n)) - hw + " |
380 | "0.5));" , |
381 | coordWeight); |
382 | fb->codeAppend("}" ); |
383 | } else { |
384 | fb->codeAppendf( |
385 | "subsetCoord.%s = mod(inCoord.%s - %s.%s, %s.%s - %s.%s) + " |
386 | "%s.%s;" , |
387 | coordSwizzle, coordSwizzle, subsetName, subsetStartSwizzle, |
388 | subsetName, subsetStopSwizzle, subsetName, |
389 | subsetStartSwizzle, subsetName, subsetStartSwizzle); |
390 | } |
391 | break; |
392 | case ShaderMode::kMirrorRepeat: { |
393 | fb->codeAppend("{" ); |
394 | fb->codeAppendf("float w = %s.%s - %s.%s;" , subsetName, |
395 | subsetStopSwizzle, subsetName, subsetStartSwizzle); |
396 | fb->codeAppendf("float w2 = 2 * w;" ); |
397 | fb->codeAppendf("float m = mod(inCoord.%s - %s.%s, w2);" , coordSwizzle, |
398 | subsetName, subsetStartSwizzle); |
399 | fb->codeAppendf("subsetCoord.%s = mix(m, w2 - m, step(w, m)) + %s.%s;" , |
400 | coordSwizzle, subsetName, subsetStartSwizzle); |
401 | fb->codeAppend("}" ); |
402 | break; |
403 | } |
404 | } |
405 | }; |
406 | |
407 | auto clampCoord = [&](bool clamp, |
408 | const char* coordSwizzle, |
409 | const char* clampStartSwizzle, |
410 | const char* clampStopSwizzle) { |
411 | if (clamp) { |
412 | fb->codeAppendf("clampedCoord.%s = clamp(subsetCoord.%s, %s.%s, %s.%s);" , |
413 | coordSwizzle, coordSwizzle, clampName, clampStartSwizzle, |
414 | clampName, clampStopSwizzle); |
415 | } else { |
416 | fb->codeAppendf("clampedCoord.%s = subsetCoord.%s;" , coordSwizzle, |
417 | coordSwizzle); |
418 | } |
419 | }; |
420 | |
421 | // Insert vars for extra coords and blending weights for kRepeatMipMap. |
422 | const char* = nullptr; |
423 | const char* repeatCoordWeightX = nullptr; |
424 | const char* = nullptr; |
425 | const char* repeatCoordWeightY = nullptr; |
426 | if (filterLogic[0] == FilterLogic::kRepeatMipMap) { |
427 | fb->codeAppend("float extraRepeatCoordX; half repeatCoordWeightX;" ); |
428 | extraRepeatCoordX = "extraRepeatCoordX" ; |
429 | repeatCoordWeightX = "repeatCoordWeightX" ; |
430 | } |
431 | if (filterLogic[1] == FilterLogic::kRepeatMipMap) { |
432 | fb->codeAppend("float extraRepeatCoordY; half repeatCoordWeightY;" ); |
433 | extraRepeatCoordY = "extraRepeatCoordY" ; |
434 | repeatCoordWeightY = "repeatCoordWeightY" ; |
435 | } |
436 | |
437 | // Apply subset rect and clamp rect to coords. |
438 | fb->codeAppend("float2 subsetCoord;" ); |
439 | subsetCoord(te.fShaderModes[0], "x" , "x" , "z" , extraRepeatCoordX, |
440 | repeatCoordWeightX); |
441 | subsetCoord(te.fShaderModes[1], "y" , "y" , "w" , extraRepeatCoordY, |
442 | repeatCoordWeightY); |
443 | fb->codeAppend("float2 clampedCoord;" ); |
444 | clampCoord(useClamp[0], "x" , "x" , "z" ); |
445 | clampCoord(useClamp[1], "y" , "y" , "w" ); |
446 | |
447 | // Additional clamping for the extra coords for kRepeatMipMap. |
448 | if (filterLogic[0] == FilterLogic::kRepeatMipMap) { |
449 | fb->codeAppendf("extraRepeatCoordX = clamp(extraRepeatCoordX, %s.x, %s.z);" , |
450 | clampName, clampName); |
451 | } |
452 | if (filterLogic[1] == FilterLogic::kRepeatMipMap) { |
453 | fb->codeAppendf("extraRepeatCoordY = clamp(extraRepeatCoordY, %s.y, %s.w);" , |
454 | clampName, clampName); |
455 | } |
456 | |
457 | // Do the 2 or 4 texture reads for kRepeatMipMap and then apply the weight(s) |
458 | // to blend between them. If neither direction is kRepeatMipMap do a single |
459 | // read at clampedCoord. |
460 | if (filterLogic[0] == FilterLogic::kRepeatMipMap && |
461 | filterLogic[1] == FilterLogic::kRepeatMipMap) { |
462 | fb->codeAppendf( |
463 | "half4 textureColor =" |
464 | " mix(mix(%s, %s, repeatCoordWeightX)," |
465 | " mix(%s, %s, repeatCoordWeightX)," |
466 | " repeatCoordWeightY);" , |
467 | read("clampedCoord" ).c_str(), |
468 | read("float2(extraRepeatCoordX, clampedCoord.y)" ).c_str(), |
469 | read("float2(clampedCoord.x, extraRepeatCoordY)" ).c_str(), |
470 | read("float2(extraRepeatCoordX, extraRepeatCoordY)" ).c_str()); |
471 | |
472 | } else if (filterLogic[0] == FilterLogic::kRepeatMipMap) { |
473 | fb->codeAppendf("half4 textureColor = mix(%s, %s, repeatCoordWeightX);" , |
474 | read("clampedCoord" ).c_str(), |
475 | read("float2(extraRepeatCoordX, clampedCoord.y)" ).c_str()); |
476 | } else if (filterLogic[1] == FilterLogic::kRepeatMipMap) { |
477 | fb->codeAppendf("half4 textureColor = mix(%s, %s, repeatCoordWeightY);" , |
478 | read("clampedCoord" ).c_str(), |
479 | read("float2(clampedCoord.x, extraRepeatCoordY)" ).c_str()); |
480 | } else { |
481 | fb->codeAppendf("half4 textureColor = %s;" , read("clampedCoord" ).c_str()); |
482 | } |
483 | |
484 | // Strings for extra texture reads used only in kRepeatBilerp |
485 | SkString repeatBilerpReadX; |
486 | SkString repeatBilerpReadY; |
487 | |
488 | // Calculate the amount the coord moved for clamping. This will be used |
489 | // to implement shader-based filtering for kClampToBorder and kRepeat. |
490 | |
491 | if (filterLogic[0] == FilterLogic::kRepeatBilerp || |
492 | filterLogic[0] == FilterLogic::kClampToBorderFilter) { |
493 | fb->codeAppend("half errX = half(subsetCoord.x - clampedCoord.x);" ); |
494 | fb->codeAppendf("float repeatCoordX = errX > 0 ? %s.x : %s.z;" , clampName, |
495 | clampName); |
496 | repeatBilerpReadX = read("float2(repeatCoordX, clampedCoord.y)" ); |
497 | } |
498 | if (filterLogic[1] == FilterLogic::kRepeatBilerp || |
499 | filterLogic[1] == FilterLogic::kClampToBorderFilter) { |
500 | fb->codeAppend("half errY = half(subsetCoord.y - clampedCoord.y);" ); |
501 | fb->codeAppendf("float repeatCoordY = errY > 0 ? %s.y : %s.w;" , clampName, |
502 | clampName); |
503 | repeatBilerpReadY = read("float2(clampedCoord.x, repeatCoordY)" ); |
504 | } |
505 | |
506 | // Add logic for kRepeatBilerp. Do 1 or 3 more texture reads depending |
507 | // on whether both modes are kRepeat and whether we're near a single subset edge |
508 | // or a corner. Then blend the multiple reads using the err values calculated |
509 | // above. |
510 | const char* ifStr = "if" ; |
511 | if (filterLogic[0] == FilterLogic::kRepeatBilerp && |
512 | filterLogic[1] == FilterLogic::kRepeatBilerp) { |
513 | auto repeatBilerpReadXY = read("float2(repeatCoordX, repeatCoordY)" ); |
514 | fb->codeAppendf( |
515 | "if (errX != 0 && errY != 0) {" |
516 | " errX = abs(errX);" |
517 | " textureColor = mix(mix(textureColor, %s, errX)," |
518 | " mix(%s, %s, errX)," |
519 | " abs(errY));" |
520 | "}" , |
521 | repeatBilerpReadX.c_str(), repeatBilerpReadY.c_str(), |
522 | repeatBilerpReadXY.c_str()); |
523 | ifStr = "else if" ; |
524 | } |
525 | if (filterLogic[0] == FilterLogic::kRepeatBilerp) { |
526 | fb->codeAppendf( |
527 | "%s (errX != 0) {" |
528 | " textureColor = mix(textureColor, %s, abs(errX));" |
529 | "}" , |
530 | ifStr, repeatBilerpReadX.c_str()); |
531 | } |
532 | if (filterLogic[1] == FilterLogic::kRepeatBilerp) { |
533 | fb->codeAppendf( |
534 | "%s (errY != 0) {" |
535 | " textureColor = mix(textureColor, %s, abs(errY));" |
536 | "}" , |
537 | ifStr, repeatBilerpReadY.c_str()); |
538 | } |
539 | |
540 | // Do soft edge shader filtering against border color for kClampToBorderFilter using |
541 | // the err values calculated above. |
542 | if (filterLogic[0] == FilterLogic::kClampToBorderFilter) { |
543 | fb->codeAppendf("textureColor = mix(textureColor, %s, min(abs(errX), 1));" , |
544 | borderName); |
545 | } |
546 | if (filterLogic[1] == FilterLogic::kClampToBorderFilter) { |
547 | fb->codeAppendf("textureColor = mix(textureColor, %s, min(abs(errY), 1));" , |
548 | borderName); |
549 | } |
550 | |
551 | // Do hard-edge shader transition to border color for kClampToBorderNearest at the |
552 | // subset boundaries. |
553 | if (filterLogic[0] == FilterLogic::kClampToBorderNearest) { |
554 | fb->codeAppendf( |
555 | "if (inCoord.x < %s.x || inCoord.x > %s.z) {" |
556 | " textureColor = %s;" |
557 | "}" , |
558 | subsetName, subsetName, borderName); |
559 | } |
560 | if (filterLogic[1] == FilterLogic::kClampToBorderNearest) { |
561 | fb->codeAppendf( |
562 | "if (inCoord.y < %s.y || inCoord.y > %s.w) {" |
563 | " textureColor = %s;" |
564 | "}" , |
565 | subsetName, subsetName, borderName); |
566 | } |
567 | fb->codeAppendf("%s = %s * textureColor;" , args.fOutputColor, args.fInputColor); |
568 | } |
569 | } |
570 | |
571 | protected: |
572 | void onSetData(const GrGLSLProgramDataManager& pdm, |
573 | const GrFragmentProcessor& fp) override { |
574 | const auto& te = fp.cast<GrTextureEffect>(); |
575 | |
576 | const float w = te.fSampler.peekTexture()->width(); |
577 | const float h = te.fSampler.peekTexture()->height(); |
578 | const auto& s = te.fSubset; |
579 | const auto& c = te.fClamp; |
580 | |
581 | auto type = te.fSampler.peekTexture()->texturePriv().textureType(); |
582 | |
583 | float norm[4] = {w, h, 1.f/w, 1.f/h}; |
584 | |
585 | if (fNormUni.isValid()) { |
586 | pdm.set4fv(fNormUni, 1, norm); |
587 | SkASSERT(type != GrTextureType::kRectangle); |
588 | } |
589 | |
590 | auto pushRect = [&](float rect[4], UniformHandle uni) { |
591 | if (te.fSampler.view().origin() == kBottomLeft_GrSurfaceOrigin) { |
592 | rect[1] = h - rect[1]; |
593 | rect[3] = h - rect[3]; |
594 | std::swap(rect[1], rect[3]); |
595 | } |
596 | if (!fNormUni.isValid() && type != GrTextureType::kRectangle) { |
597 | rect[0] *= norm[2]; |
598 | rect[2] *= norm[2]; |
599 | rect[1] *= norm[3]; |
600 | rect[3] *= norm[3]; |
601 | } |
602 | pdm.set4fv(uni, 1, rect); |
603 | }; |
604 | |
605 | if (fSubsetUni.isValid()) { |
606 | float subset[] = {s.fLeft, s.fTop, s.fRight, s.fBottom}; |
607 | pushRect(subset, fSubsetUni); |
608 | } |
609 | if (fClampUni.isValid()) { |
610 | float subset[] = {c.fLeft, c.fTop, c.fRight, c.fBottom}; |
611 | pushRect(subset, fClampUni); |
612 | } |
613 | if (fBorderUni.isValid()) { |
614 | pdm.set4fv(fBorderUni, 1, te.fBorder); |
615 | } |
616 | } |
617 | }; |
618 | return new Impl; |
619 | } |
620 | |
621 | void GrTextureEffect::onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const { |
622 | auto m0 = static_cast<uint32_t>(fShaderModes[0]); |
623 | auto m1 = static_cast<uint32_t>(fShaderModes[1]); |
624 | auto filter = fSampler.samplerState().filter(); |
625 | auto l0 = static_cast<uint32_t>(GetFilterLogic(fShaderModes[0], filter)); |
626 | auto l1 = static_cast<uint32_t>(GetFilterLogic(fShaderModes[1], filter)); |
627 | b->add32((l0 << 24) | (l1 << 16) | (m0 << 8) | m1); |
628 | } |
629 | |
630 | bool GrTextureEffect::onIsEqual(const GrFragmentProcessor& other) const { |
631 | auto& that = other.cast<GrTextureEffect>(); |
632 | if (fShaderModes[0] != that.fShaderModes[0] || fShaderModes[1] != that.fShaderModes[1]) { |
633 | return false; |
634 | } |
635 | if (fSubset != that.fSubset) { |
636 | return false; |
637 | } |
638 | if ((fShaderModes[0] == ShaderMode::kClampToBorder || |
639 | fShaderModes[1] == ShaderMode::kClampToBorder) && |
640 | !std::equal(fBorder, fBorder + 4, that.fBorder)) { |
641 | return false; |
642 | } |
643 | return true; |
644 | } |
645 | |
646 | GrTextureEffect::GrTextureEffect(GrSurfaceProxyView view, SkAlphaType alphaType, |
647 | const SkMatrix& matrix, const Sampling& sampling) |
648 | : GrFragmentProcessor(kGrTextureEffect_ClassID, |
649 | ModulateForSamplerOptFlags(alphaType, sampling.hasBorderAlpha())) |
650 | , fCoordTransform(matrix, view.proxy(), view.origin()) |
651 | , fSampler(std::move(view), sampling.fHWSampler) |
652 | , fSubset(sampling.fShaderSubset) |
653 | , fClamp(sampling.fShaderClamp) |
654 | , fShaderModes{sampling.fShaderModes[0], sampling.fShaderModes[1]} { |
655 | // We always compare the range even when it isn't used so assert we have canonical don't care |
656 | // values. |
657 | SkASSERT(fShaderModes[0] != ShaderMode::kNone || (fSubset.fLeft == 0 && fSubset.fRight == 0)); |
658 | SkASSERT(fShaderModes[1] != ShaderMode::kNone || (fSubset.fTop == 0 && fSubset.fBottom == 0)); |
659 | this->setTextureSamplerCnt(1); |
660 | this->addCoordTransform(&fCoordTransform); |
661 | std::copy_n(sampling.fBorder, 4, fBorder); |
662 | } |
663 | |
664 | GrTextureEffect::GrTextureEffect(const GrTextureEffect& src) |
665 | : INHERITED(kGrTextureEffect_ClassID, src.optimizationFlags()) |
666 | , fCoordTransform(src.fCoordTransform) |
667 | , fSampler(src.fSampler) |
668 | , fSubset(src.fSubset) |
669 | , fClamp(src.fClamp) |
670 | , fShaderModes{src.fShaderModes[0], src.fShaderModes[1]} { |
671 | std::copy_n(src.fBorder, 4, fBorder); |
672 | this->setTextureSamplerCnt(1); |
673 | this->addCoordTransform(&fCoordTransform); |
674 | } |
675 | |
676 | std::unique_ptr<GrFragmentProcessor> GrTextureEffect::clone() const { |
677 | return std::unique_ptr<GrFragmentProcessor>(new GrTextureEffect(*this)); |
678 | } |
679 | |
680 | const GrFragmentProcessor::TextureSampler& GrTextureEffect::onTextureSampler(int) const { |
681 | return fSampler; |
682 | } |
683 | |
684 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTextureEffect); |
685 | #if GR_TEST_UTILS |
686 | std::unique_ptr<GrFragmentProcessor> GrTextureEffect::TestCreate(GrProcessorTestData* testData) { |
687 | auto [view, ct, at] = testData->randomView(); |
688 | Mode wrapModes[2]; |
689 | GrTest::TestWrapModes(testData->fRandom, wrapModes); |
690 | |
691 | Filter filter; |
692 | if (view.asTextureProxy()->mipMapped() == GrMipMapped::kYes) { |
693 | switch (testData->fRandom->nextULessThan(3)) { |
694 | case 0: |
695 | filter = Filter::kNearest; |
696 | break; |
697 | case 1: |
698 | filter = Filter::kBilerp; |
699 | break; |
700 | default: |
701 | filter = Filter::kMipMap; |
702 | break; |
703 | } |
704 | } else { |
705 | filter = testData->fRandom->nextBool() ? Filter::kBilerp : Filter::kNearest; |
706 | } |
707 | GrSamplerState params(wrapModes, filter); |
708 | |
709 | const SkMatrix& matrix = GrTest::TestMatrix(testData->fRandom); |
710 | return GrTextureEffect::Make(std::move(view), at, matrix, params, *testData->caps()); |
711 | } |
712 | #endif |
713 | |