blob: 1df80066308c84e0be746f5a2042c32ea9ad72fd [file] [log] [blame]
bsalomon848faf02014-07-11 10:01:02 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/effects/GrBicubicEffect.h"
Robert Phillips296b1cc2017-03-15 10:42:12 -04009
Brian Salomond0d033a2020-02-18 16:59:28 -050010#include "src/core/SkMatrixPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000011#include "src/gpu/GrTexture.h"
Michael Ludwigb5bd1b32020-06-26 10:23:23 -040012#include "src/gpu/effects/GrMatrixEffect.h"
Brian Salomond0d033a2020-02-18 16:59:28 -050013#include "src/gpu/effects/GrTextureEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
15#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
16#include "src/gpu/glsl/GrGLSLUniformHandler.h"
bsalomon848faf02014-07-11 10:01:02 -070017
Brian Salomond0d033a2020-02-18 16:59:28 -050018class GrBicubicEffect::Impl : public GrGLSLFragmentProcessor {
humper@google.com3aad3b02013-09-04 19:23:53 +000019public:
robertphillips9cdb9922016-02-03 12:25:40 -080020 void emitCode(EmitArgs&) override;
humper@google.com3aad3b02013-09-04 19:23:53 +000021
humper@google.com3aad3b02013-09-04 19:23:53 +000022private:
egdaniel64c47282015-11-13 06:54:19 -080023 typedef GrGLSLFragmentProcessor INHERITED;
humper@google.com3aad3b02013-09-04 19:23:53 +000024};
25
Brian Salomond0d033a2020-02-18 16:59:28 -050026void GrBicubicEffect::Impl::emitCode(EmitArgs& args) {
brianosman54f30c12016-07-18 10:53:52 -070027 const GrBicubicEffect& bicubicEffect = args.fFp.cast<GrBicubicEffect>();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000028
cdalton85285412016-02-18 12:37:07 -080029 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Osmana3fa2ef2017-01-06 16:13:06 +000030
Brian Salomon1af72d12020-06-25 10:47:26 -040031 if (bicubicEffect.fKernel == GrBicubicEffect::Kernel::kMitchell) {
32 /*
33 * Filter weights come from Don Mitchell & Arun Netravali's 'Reconstruction Filters in\
34 * Computer * Graphics', ACM SIGGRAPH Computer Graphics 22, 4 (Aug. 1988).
35 * ACM DL: http://dl.acm.org/citation.cfm?id=378514
36 * Free:
37 * http://www.cs.utexas.edu/users/fussell/courses/cs384g/lectures/mitchell/Mitchell.pdf
38 *
39 * The authors define a family of cubic filters with two free parameters (B and C):
40 *
41 * { (12 - 9B - 6C)|x|^3 + (-18 + 12B + 6C)|x|^2 + (6 - 2B) |x| < 1
42 * k(x) = 1/6 { (-B - 6C)|x|^3 + (6B + 30C)|x|^2 + (-12B - 48C)|x| + (8B + 24C) 1 <= |x| < 2
43 * { 0 otherwise
44 *
45 * Various well-known cubic splines can be generated, and the authors select (1/3, 1/3) as
46 * their favorite overall spline - this is now commonly known as the Mitchell filter, and
47 * is the source of the specific weights below.
48 *
49 * This is SkSL, so the matrix is column-major (transposed from standard matrix notation).
50 */
51 fragBuilder->codeAppend(
52 "half4x4 kCoefficients = half4x4("
53 " 1.0 / 18.0, 16.0 / 18.0, 1.0 / 18.0, 0.0 / 18.0,"
54 "-9.0 / 18.0, 0.0 / 18.0, 9.0 / 18.0, 0.0 / 18.0,"
55 "15.0 / 18.0, -36.0 / 18.0, 27.0 / 18.0, -6.0 / 18.0,"
56 "-7.0 / 18.0, 21.0 / 18.0, -21.0 / 18.0, 7.0 / 18.0);");
57 } else {
58 /*
59 * Centripetal variant of the Catmull-Rom spline.
60 *
61 * Catmull, Edwin; Rom, Raphael (1974). "A class of local interpolating splines". In
62 * Barnhill, Robert E.; Riesenfeld, Richard F. (eds.). Computer Aided Geometric Design.
63 * pp. 317–326.
64 */
65 SkASSERT(bicubicEffect.fKernel == GrBicubicEffect::Kernel::kCatmullRom);
66 fragBuilder->codeAppend(
67 "half4x4 kCoefficients = 0.5 * half4x4("
68 " 0, 2, 0, 0,"
69 "-1, 0, 1, 0,"
70 " 2, -5, 4, -1,"
71 "-1, 3, -3, 1);");
72 }
Brian Salomon5c4c61e2020-03-25 14:26:01 -040073 // We determine our fractional offset (f) within the texel. We then snap coord to a texel
74 // center. The snap prevents cases where the starting coords are near a texel boundary and
75 // offsets with imperfect precision would cause us to skip/double hit a texel.
76 // The use of "texel" above is somewhat abstract as we're sampling a child processor. It is
77 // assumed the child processor represents something akin to a nearest neighbor sampled texture.
Brian Salomond0d033a2020-02-18 16:59:28 -050078 if (bicubicEffect.fDirection == GrBicubicEffect::Direction::kXY) {
Michael Ludwige88320b2020-06-24 09:04:56 -040079 fragBuilder->codeAppendf("float2 coord = %s - float2(0.5);", args.fSampleCoord);
Brian Salomon5c4c61e2020-03-25 14:26:01 -040080 fragBuilder->codeAppend("half2 f = half2(fract(coord));");
81 fragBuilder->codeAppend("coord += 0.5 - f;");
Brian Salomona86fc7a2019-05-28 20:42:58 -040082 fragBuilder->codeAppend(
Brian Salomon1af72d12020-06-25 10:47:26 -040083 "half4 wx = kCoefficients * half4(1.0, f.x, f.x * f.x, f.x * f.x * f.x);");
Brian Salomona86fc7a2019-05-28 20:42:58 -040084 fragBuilder->codeAppend(
Brian Salomon1af72d12020-06-25 10:47:26 -040085 "half4 wy = kCoefficients * half4(1.0, f.y, f.y * f.y, f.y * f.y * f.y);");
Brian Salomona86fc7a2019-05-28 20:42:58 -040086 fragBuilder->codeAppend("half4 rowColors[4];");
87 for (int y = 0; y < 4; ++y) {
88 for (int x = 0; x < 4; ++x) {
89 SkString coord;
Brian Salomon5c4c61e2020-03-25 14:26:01 -040090 coord.printf("coord + float2(%d, %d)", x - 1, y - 1);
Brian Salomond0d033a2020-02-18 16:59:28 -050091 auto childStr =
92 this->invokeChild(0, args, SkSL::String(coord.c_str(), coord.size()));
93 fragBuilder->codeAppendf("rowColors[%d] = %s;", x, childStr.c_str());
Brian Salomona86fc7a2019-05-28 20:42:58 -040094 }
95 fragBuilder->codeAppendf(
96 "half4 s%d = wx.x * rowColors[0] + wx.y * rowColors[1] + wx.z * rowColors[2] + "
97 "wx.w * rowColors[3];",
98 y);
99 }
100 fragBuilder->codeAppend(
101 "half4 bicubicColor = wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3;");
102 } else {
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400103 const char* d = bicubicEffect.fDirection == Direction::kX ? "x" : "y";
Michael Ludwige88320b2020-06-24 09:04:56 -0400104 fragBuilder->codeAppendf("float coord = %s.%s - 0.5;", args.fSampleCoord, d);
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400105 fragBuilder->codeAppend("half f = half(fract(coord));");
106 fragBuilder->codeAppend("coord += 0.5 - f;");
107 fragBuilder->codeAppend("half f2 = f * f;");
Brian Salomon1af72d12020-06-25 10:47:26 -0400108 fragBuilder->codeAppend("half4 w = kCoefficients * half4(1.0, f, f2, f2 * f);");
Brian Salomona86fc7a2019-05-28 20:42:58 -0400109 fragBuilder->codeAppend("half4 c[4];");
110 for (int i = 0; i < 4; ++i) {
humper@google.com3aad3b02013-09-04 19:23:53 +0000111 SkString coord;
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400112 if (bicubicEffect.fDirection == Direction::kX) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400113 coord.printf("float2(coord + %d, %s.y)", i - 1, args.fSampleCoord);
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400114 } else {
Michael Ludwige88320b2020-06-24 09:04:56 -0400115 coord.printf("float2(%s.x, coord + %d)", args.fSampleCoord, i - 1);
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400116 }
Brian Salomond0d033a2020-02-18 16:59:28 -0500117 auto childStr = this->invokeChild(0, args, SkSL::String(coord.c_str(), coord.size()));
118 fragBuilder->codeAppendf("c[%d] = %s;", i, childStr.c_str());
humper@google.com3aad3b02013-09-04 19:23:53 +0000119 }
Brian Salomona86fc7a2019-05-28 20:42:58 -0400120 fragBuilder->codeAppend(
121 "half4 bicubicColor = c[0] * w.x + c[1] * w.y + c[2] * w.z + c[3] * w.w;");
humper@google.com3aad3b02013-09-04 19:23:53 +0000122 }
Brian Osmane22dba82019-03-13 10:22:28 -0400123 // Bicubic can send colors out of range, so clamp to get them back in (source) gamut.
124 // The kind of clamp we have to do depends on the alpha type.
Brian Salomond0d033a2020-02-18 16:59:28 -0500125 switch (bicubicEffect.fClamp) {
126 case Clamp::kUnpremul:
127 fragBuilder->codeAppend("bicubicColor = saturate(bicubicColor);");
128 break;
129 case Clamp::kPremul:
130 fragBuilder->codeAppend(
131 "bicubicColor.rgb = max(half3(0.0), min(bicubicColor.rgb, bicubicColor.aaa));");
132 break;
Brian Osmane22dba82019-03-13 10:22:28 -0400133 }
134 fragBuilder->codeAppendf("%s = bicubicColor * %s;", args.fOutputColor, args.fInputColor);
humper@google.com3aad3b02013-09-04 19:23:53 +0000135}
136
Brian Salomond0d033a2020-02-18 16:59:28 -0500137std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(GrSurfaceProxyView view,
138 SkAlphaType alphaType,
139 const SkMatrix& matrix,
Brian Salomon1af72d12020-06-25 10:47:26 -0400140 Kernel kernel,
Brian Salomond0d033a2020-02-18 16:59:28 -0500141 Direction direction) {
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400142 auto fp = GrTextureEffect::Make(std::move(view), alphaType, SkMatrix::I());
Brian Salomond0d033a2020-02-18 16:59:28 -0500143 auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul;
Michael Ludwigb5bd1b32020-06-26 10:23:23 -0400144 return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>(
145 new GrBicubicEffect(std::move(fp), kernel, direction, clamp)));
Brian Salomond0d033a2020-02-18 16:59:28 -0500146}
147
148std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(GrSurfaceProxyView view,
149 SkAlphaType alphaType,
150 const SkMatrix& matrix,
151 const GrSamplerState::WrapMode wrapX,
152 const GrSamplerState::WrapMode wrapY,
Brian Salomon1af72d12020-06-25 10:47:26 -0400153 Kernel kernel,
Brian Salomond0d033a2020-02-18 16:59:28 -0500154 Direction direction,
155 const GrCaps& caps) {
156 GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest);
157 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400158 fp = GrTextureEffect::Make(std::move(view), alphaType, SkMatrix::I(), sampler, caps);
Brian Salomond0d033a2020-02-18 16:59:28 -0500159 auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul;
Michael Ludwigb5bd1b32020-06-26 10:23:23 -0400160 return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>(
161 new GrBicubicEffect(std::move(fp), kernel, direction, clamp)));
Brian Salomond0d033a2020-02-18 16:59:28 -0500162}
163
164std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::MakeSubset(
165 GrSurfaceProxyView view,
166 SkAlphaType alphaType,
167 const SkMatrix& matrix,
168 const GrSamplerState::WrapMode wrapX,
169 const GrSamplerState::WrapMode wrapY,
170 const SkRect& subset,
Brian Salomon1af72d12020-06-25 10:47:26 -0400171 Kernel kernel,
Brian Salomond0d033a2020-02-18 16:59:28 -0500172 Direction direction,
173 const GrCaps& caps) {
174 GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest);
175 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400176 fp = GrTextureEffect::MakeSubset(
177 std::move(view), alphaType, SkMatrix::I(), sampler, subset, caps);
Brian Salomond0d033a2020-02-18 16:59:28 -0500178 auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul;
Michael Ludwigb5bd1b32020-06-26 10:23:23 -0400179 return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>(
180 new GrBicubicEffect(std::move(fp), kernel, direction, clamp)));
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400181}
182
183std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(std::unique_ptr<GrFragmentProcessor> fp,
184 SkAlphaType alphaType,
185 const SkMatrix& matrix,
Brian Salomon1af72d12020-06-25 10:47:26 -0400186 Kernel kernel,
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400187 Direction direction) {
188 auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul;
Michael Ludwigb5bd1b32020-06-26 10:23:23 -0400189 return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>(
190 new GrBicubicEffect(std::move(fp), kernel, direction, clamp)));
Brian Salomond0d033a2020-02-18 16:59:28 -0500191}
192
193GrBicubicEffect::GrBicubicEffect(std::unique_ptr<GrFragmentProcessor> fp,
Brian Salomon1af72d12020-06-25 10:47:26 -0400194 Kernel kernel,
Brian Salomond0d033a2020-02-18 16:59:28 -0500195 Direction direction,
196 Clamp clamp)
197 : INHERITED(kGrBicubicEffect_ClassID, ProcessorOptimizationFlags(fp.get()))
Brian Salomon1af72d12020-06-25 10:47:26 -0400198 , fKernel(kernel)
Brian Salomond0d033a2020-02-18 16:59:28 -0500199 , fDirection(direction)
200 , fClamp(clamp) {
Michael Ludwigb5bd1b32020-06-26 10:23:23 -0400201 this->setUsesSampleCoordsDirectly();
Brian Osman1298bc42020-06-30 13:39:35 -0400202 this->registerChild(std::move(fp), SkSL::SampleUsage::Explicit());
Robert Phillips40fd7c92017-01-30 08:06:27 -0500203}
204
Brian Salomon3f6f9652017-07-28 07:34:05 -0400205GrBicubicEffect::GrBicubicEffect(const GrBicubicEffect& that)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400206 : INHERITED(kGrBicubicEffect_ClassID, that.optimizationFlags())
Brian Salomon1af72d12020-06-25 10:47:26 -0400207 , fKernel(that.fKernel)
Brian Salomond0d033a2020-02-18 16:59:28 -0500208 , fDirection(that.fDirection)
209 , fClamp(that.fClamp) {
Michael Ludwigb5bd1b32020-06-26 10:23:23 -0400210 this->setUsesSampleCoordsDirectly();
Michael Ludwig9aba6252020-06-22 14:46:36 -0400211 this->cloneAndRegisterAllChildProcessors(that);
humper@google.com3aad3b02013-09-04 19:23:53 +0000212}
213
Brian Salomon94efbf52016-11-29 13:43:05 -0500214void GrBicubicEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800215 GrProcessorKeyBuilder* b) const {
Brian Salomon1af72d12020-06-25 10:47:26 -0400216 uint32_t key = (static_cast<uint32_t>(fKernel) << 0)
217 | (static_cast<uint32_t>(fDirection) << 1)
218 | (static_cast<uint32_t>(fClamp) << 3);
Brian Salomond0d033a2020-02-18 16:59:28 -0500219 b->add32(key);
joshualitteb2a6762014-12-04 11:35:33 -0800220}
221
Brian Salomond0d033a2020-02-18 16:59:28 -0500222GrGLSLFragmentProcessor* GrBicubicEffect::onCreateGLSLInstance() const { return new Impl(); }
humper@google.com3aad3b02013-09-04 19:23:53 +0000223
Brian Salomond0d033a2020-02-18 16:59:28 -0500224bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& other) const {
225 const auto& that = other.cast<GrBicubicEffect>();
226 return fDirection == that.fDirection && fClamp == that.fClamp;
humper@google.com3aad3b02013-09-04 19:23:53 +0000227}
228
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400229SkPMColor4f GrBicubicEffect::constantOutputForConstantInput(const SkPMColor4f& input) const {
230 return GrFragmentProcessor::ConstantOutputForConstantInput(this->childProcessor(0), input);
231}
232
joshualittb0a8a372014-09-23 09:50:21 -0700233GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect);
humper@google.com3aad3b02013-09-04 19:23:53 +0000234
Hal Canary6f6961e2017-01-31 13:50:44 -0500235#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400236std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) {
Brian Salomona86fc7a2019-05-28 20:42:58 -0400237 Direction direction = Direction::kX;
238 switch (d->fRandom->nextULessThan(3)) {
239 case 0:
240 direction = Direction::kX;
241 break;
242 case 1:
243 direction = Direction::kY;
244 break;
245 case 2:
246 direction = Direction::kXY;
247 break;
248 }
Brian Salomon1af72d12020-06-25 10:47:26 -0400249 auto kernel = d->fRandom->nextBool() ? GrBicubicEffect::Kernel::kMitchell
250 : GrBicubicEffect::Kernel::kCatmullRom;
Brian Salomond0d033a2020-02-18 16:59:28 -0500251 auto m = GrTest::TestMatrix(d->fRandom);
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400252 switch (d->fRandom->nextULessThan(3)) {
253 case 0: {
254 auto [view, ct, at] = d->randomView();
255 GrSamplerState::WrapMode wm[2];
256 GrTest::TestWrapModes(d->fRandom, wm);
Greg Danielba51d682020-02-05 13:12:13 -0500257
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400258 if (d->fRandom->nextBool()) {
259 SkRect subset;
260 subset.fLeft = d->fRandom->nextSScalar1() * view.width();
261 subset.fTop = d->fRandom->nextSScalar1() * view.height();
262 subset.fRight = d->fRandom->nextSScalar1() * view.width();
263 subset.fBottom = d->fRandom->nextSScalar1() * view.height();
264 subset.sort();
Brian Salomon1af72d12020-06-25 10:47:26 -0400265 return MakeSubset(std::move(view),
266 at,
267 m,
268 wm[0],
269 wm[1],
270 subset,
271 kernel,
272 direction,
273 *d->caps());
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400274 }
Brian Salomon1af72d12020-06-25 10:47:26 -0400275 return Make(std::move(view), at, m, wm[0], wm[1], kernel, direction, *d->caps());
Brian Salomond0d033a2020-02-18 16:59:28 -0500276 }
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400277 case 1: {
278 auto [view, ct, at] = d->randomView();
Brian Salomon1af72d12020-06-25 10:47:26 -0400279 return Make(std::move(view), at, m, kernel, direction);
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400280 }
281 default: {
282 SkAlphaType at;
283 do {
284 at = static_cast<SkAlphaType>(d->fRandom->nextULessThan(kLastEnum_SkAlphaType + 1));
Brian Osman609f1592020-07-01 15:14:39 -0400285 } while (at == kUnknown_SkAlphaType);
286 return Make(GrProcessorUnitTest::MakeChildFP(d), at, m, kernel, direction);
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400287 }
Brian Salomond0d033a2020-02-18 16:59:28 -0500288 }
humper@google.com3aad3b02013-09-04 19:23:53 +0000289}
Hal Canary6f6961e2017-01-31 13:50:44 -0500290#endif
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000291
292//////////////////////////////////////////////////////////////////////////////
293
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400294bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix, GrSamplerState::Filter* filterMode) {
Mike Reedb2e3c642019-08-02 11:57:10 -0400295 switch (SkMatrixPriv::AdjustHighQualityFilterLevel(matrix)) {
Mike Reedd2f4be32019-08-01 13:52:38 -0400296 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400297 *filterMode = GrSamplerState::Filter::kNearest;
Mike Reedd2f4be32019-08-01 13:52:38 -0400298 break;
299 case kLow_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400300 *filterMode = GrSamplerState::Filter::kBilerp;
Mike Reedd2f4be32019-08-01 13:52:38 -0400301 break;
302 case kMedium_SkFilterQuality:
303 *filterMode = GrSamplerState::Filter::kMipMap;
304 break;
305 case kHigh_SkFilterQuality:
306 // When we use the bicubic filtering effect each sample is read from the texture using
307 // nearest neighbor sampling.
308 *filterMode = GrSamplerState::Filter::kNearest;
309 return true;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000310 }
Mike Reedd2f4be32019-08-01 13:52:38 -0400311 return false;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000312}