blob: 6d7b431ab3ddb00bcf1cb60c192fd8055c19e821 [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 Reedd2f4be32019-08-01 13:52:38 -04008#include "src/core/SkMatrixPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/effects/GrBicubicEffect.h"
Robert Phillips296b1cc2017-03-15 10:42:12 -040010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrTexture.h"
12#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
13#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
14#include "src/gpu/glsl/GrGLSLUniformHandler.h"
bsalomon848faf02014-07-11 10:01:02 -070015
egdaniel64c47282015-11-13 06:54:19 -080016class GrGLBicubicEffect : public GrGLSLFragmentProcessor {
humper@google.com3aad3b02013-09-04 19:23:53 +000017public:
robertphillips9cdb9922016-02-03 12:25:40 -080018 void emitCode(EmitArgs&) override;
humper@google.com3aad3b02013-09-04 19:23:53 +000019
Brian Salomon94efbf52016-11-29 13:43:05 -050020 static inline void GenKey(const GrProcessor& effect, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -070021 GrProcessorKeyBuilder* b) {
brianosman54f30c12016-07-18 10:53:52 -070022 const GrBicubicEffect& bicubicEffect = effect.cast<GrBicubicEffect>();
23 b->add32(GrTextureDomain::GLDomain::DomainKey(bicubicEffect.domain()));
Brian Salomon1127c0b2019-06-13 20:22:10 +000024 uint32_t bidir = bicubicEffect.direction() == GrBicubicEffect::Direction::kXY ? 1 : 0;
25 b->add32(bidir | (bicubicEffect.alphaType() << 1));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000026 }
27
wangyixb1daa862015-08-18 11:29:31 -070028protected:
Brian Salomonab015ef2017-04-04 10:15:51 -040029 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -070030
humper@google.com3aad3b02013-09-04 19:23:53 +000031private:
egdaniel018fb622015-10-28 07:26:40 -070032 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
humper@google.com3aad3b02013-09-04 19:23:53 +000033
Brian Salomona86fc7a2019-05-28 20:42:58 -040034 UniformHandle fDimensions;
Brian Salomon1127c0b2019-06-13 20:22:10 +000035 GrTextureDomain::GLDomain fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +000036
egdaniel64c47282015-11-13 06:54:19 -080037 typedef GrGLSLFragmentProcessor INHERITED;
humper@google.com3aad3b02013-09-04 19:23:53 +000038};
39
wangyix7c157a92015-07-22 15:08:53 -070040void GrGLBicubicEffect::emitCode(EmitArgs& args) {
brianosman54f30c12016-07-18 10:53:52 -070041 const GrBicubicEffect& bicubicEffect = args.fFp.cast<GrBicubicEffect>();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000042
egdaniel7ea439b2015-12-03 09:20:44 -080043 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
Brian Salomona86fc7a2019-05-28 20:42:58 -040044 fDimensions = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, "Dimensions");
humper@google.com3aad3b02013-09-04 19:23:53 +000045
Brian Salomona86fc7a2019-05-28 20:42:58 -040046 const char* dims = uniformHandler->getUniformCStr(fDimensions);
humper@google.com3aad3b02013-09-04 19:23:53 +000047
cdalton85285412016-02-18 12:37:07 -080048 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasd4efe682019-08-29 16:10:13 -040049 SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint);
Brian Osmana3fa2ef2017-01-06 16:13:06 +000050
Brian Salomon1127c0b2019-06-13 20:22:10 +000051 /*
52 * Filter weights come from Don Mitchell & Arun Netravali's 'Reconstruction Filters in Computer
53 * Graphics', ACM SIGGRAPH Computer Graphics 22, 4 (Aug. 1988).
54 * ACM DL: http://dl.acm.org/citation.cfm?id=378514
55 * Free : http://www.cs.utexas.edu/users/fussell/courses/cs384g/lectures/mitchell/Mitchell.pdf
56 *
57 * The authors define a family of cubic filters with two free parameters (B and C):
58 *
59 * { (12 - 9B - 6C)|x|^3 + (-18 + 12B + 6C)|x|^2 + (6 - 2B) if |x| < 1
60 * k(x) = 1/6 { (-B - 6C)|x|^3 + (6B + 30C)|x|^2 + (-12B - 48C)|x| + (8B + 24C) if 1 <= |x| < 2
61 * { 0 otherwise
62 *
63 * Various well-known cubic splines can be generated, and the authors select (1/3, 1/3) as their
64 * favorite overall spline - this is now commonly known as the Mitchell filter, and is the
65 * source of the specific weights below.
66 *
67 * This is GLSL, so the matrix is column-major (transposed from standard matrix notation).
68 */
69 fragBuilder->codeAppend("half4x4 kMitchellCoefficients = half4x4("
70 " 1.0 / 18.0, 16.0 / 18.0, 1.0 / 18.0, 0.0 / 18.0,"
71 "-9.0 / 18.0, 0.0 / 18.0, 9.0 / 18.0, 0.0 / 18.0,"
72 "15.0 / 18.0, -36.0 / 18.0, 27.0 / 18.0, -6.0 / 18.0,"
73 "-7.0 / 18.0, 21.0 / 18.0, -21.0 / 18.0, 7.0 / 18.0);");
Brian Salomona86fc7a2019-05-28 20:42:58 -040074 fragBuilder->codeAppendf("float2 coord = %s - %s.xy * float2(0.5);", coords2D.c_str(), dims);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000075 // We unnormalize the coord in order to determine our fractional offset (f) within the texel
76 // We then snap coord to a texel center and renormalize. The snap prevents cases where the
Brian Salomona86fc7a2019-05-28 20:42:58 -040077 // starting coords are near a texel boundary and accumulations of dims would cause us to skip/
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000078 // double hit a texel.
Brian Salomona86fc7a2019-05-28 20:42:58 -040079 fragBuilder->codeAppendf("half2 f = half2(fract(coord * %s.zw));", dims);
80 fragBuilder->codeAppendf("coord = coord + (half2(0.5) - f) * %s.xy;", dims);
81 if (bicubicEffect.direction() == GrBicubicEffect::Direction::kXY) {
82 fragBuilder->codeAppend(
Brian Salomon1127c0b2019-06-13 20:22:10 +000083 "half4 wx = kMitchellCoefficients * 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 Salomon1127c0b2019-06-13 20:22:10 +000085 "half4 wy = kMitchellCoefficients * 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;
90 coord.printf("coord + %s.xy * float2(%d, %d)", dims, x - 1, y - 1);
91 SkString sampleVar;
92 sampleVar.printf("rowColors[%d]", x);
93 fDomain.sampleTexture(fragBuilder,
94 args.fUniformHandler,
95 args.fShaderCaps,
96 bicubicEffect.domain(),
97 sampleVar.c_str(),
98 coord,
99 args.fTexSamplers[0]);
100 }
101 fragBuilder->codeAppendf(
102 "half4 s%d = wx.x * rowColors[0] + wx.y * rowColors[1] + wx.z * rowColors[2] + "
103 "wx.w * rowColors[3];",
104 y);
105 }
106 fragBuilder->codeAppend(
107 "half4 bicubicColor = wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3;");
108 } else {
109 // One of the dims.xy values will be zero. So v here selects the nonzero value of f.
110 fragBuilder->codeAppend("half v = f.x + f.y;");
111 fragBuilder->codeAppend("half v2 = v * v;");
Brian Salomon1127c0b2019-06-13 20:22:10 +0000112 fragBuilder->codeAppend("half4 w = kMitchellCoefficients * half4(1.0, v, v2, v2 * v);");
Brian Salomona86fc7a2019-05-28 20:42:58 -0400113 fragBuilder->codeAppend("half4 c[4];");
114 for (int i = 0; i < 4; ++i) {
humper@google.com3aad3b02013-09-04 19:23:53 +0000115 SkString coord;
Brian Salomona86fc7a2019-05-28 20:42:58 -0400116 coord.printf("coord + %s.xy * half(%d)", dims, i - 1);
117 SkString samplerVar;
118 samplerVar.printf("c[%d]", i);
119 // With added complexity we could apply the domain once in X or Y depending on
120 // direction rather than for each of the four lookups, but then we might not be
121 // be able to share code for Direction::kX and ::kY.
egdaniela2e3e0f2015-11-19 07:23:45 -0800122 fDomain.sampleTexture(fragBuilder,
egdaniel7ea439b2015-12-03 09:20:44 -0800123 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500124 args.fShaderCaps,
brianosman54f30c12016-07-18 10:53:52 -0700125 bicubicEffect.domain(),
Brian Salomona86fc7a2019-05-28 20:42:58 -0400126 samplerVar.c_str(),
egdaniela2e3e0f2015-11-19 07:23:45 -0800127 coord,
cdalton3f6f76f2016-04-11 12:18:09 -0700128 args.fTexSamplers[0]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000129 }
Brian Salomona86fc7a2019-05-28 20:42:58 -0400130 fragBuilder->codeAppend(
131 "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 +0000132 }
Brian Osmane22dba82019-03-13 10:22:28 -0400133 // Bicubic can send colors out of range, so clamp to get them back in (source) gamut.
134 // The kind of clamp we have to do depends on the alpha type.
135 if (kPremul_SkAlphaType == bicubicEffect.alphaType()) {
136 fragBuilder->codeAppend("bicubicColor.a = saturate(bicubicColor.a);");
137 fragBuilder->codeAppend(
138 "bicubicColor.rgb = max(half3(0.0), min(bicubicColor.rgb, bicubicColor.aaa));");
139 } else {
140 fragBuilder->codeAppend("bicubicColor = saturate(bicubicColor);");
141 }
142 fragBuilder->codeAppendf("%s = bicubicColor * %s;", args.fOutputColor, args.fInputColor);
humper@google.com3aad3b02013-09-04 19:23:53 +0000143}
144
egdaniel018fb622015-10-28 07:26:40 -0700145void GrGLBicubicEffect::onSetData(const GrGLSLProgramDataManager& pdman,
Brian Salomonab015ef2017-04-04 10:15:51 -0400146 const GrFragmentProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -0700147 const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>();
Brian Salomon7da2fc72018-12-10 13:40:07 -0500148 GrTextureProxy* proxy = processor.textureSampler(0).proxy();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400149 GrTexture* texture = proxy->peekTexture();
Robert Phillips9bee2e52017-05-29 12:37:20 -0400150
Brian Salomona86fc7a2019-05-28 20:42:58 -0400151 float dims[4] = {0, 0, 0, 0};
152 if (bicubicEffect.direction() != GrBicubicEffect::Direction::kY) {
153 dims[0] = 1.0f / texture->width();
154 dims[2] = texture->width();
155 }
156 if (bicubicEffect.direction() != GrBicubicEffect::Direction::kX) {
157 dims[1] = 1.0f / texture->height();
158 dims[3] = texture->height();
159 }
160 pdman.set4fv(fDimensions, 1, dims);
Michael Ludwigbe315a22018-12-17 09:50:51 -0500161 fDomain.setData(pdman, bicubicEffect.domain(), proxy,
162 processor.textureSampler(0).samplerState());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000163}
164
Brian Salomon078e8fa2019-11-22 04:10:18 +0000165GrBicubicEffect::GrBicubicEffect(sk_sp<GrTextureProxy> proxy, GrColorType srcColorType,
166 const SkMatrix& matrix, const SkRect& domain,
167 const GrSamplerState::WrapMode wrapModes[2],
Brian Osmane22dba82019-03-13 10:22:28 -0400168 GrTextureDomain::Mode modeX, GrTextureDomain::Mode modeY,
Brian Salomon1127c0b2019-06-13 20:22:10 +0000169 Direction direction, SkAlphaType alphaType)
Michael Ludwig257a03d2018-12-13 14:07:07 -0500170 : INHERITED{kGrBicubicEffect_ClassID,
Brian Salomona86fc7a2019-05-28 20:42:58 -0400171 ModulateForSamplerOptFlags(
Brian Salomon078e8fa2019-11-22 04:10:18 +0000172 srcColorType,
173 GrTextureDomain::IsDecalSampled(wrapModes, modeX, modeY))}
Brian Salomon6cd51b52017-07-26 19:07:15 -0400174 , fCoordTransform(matrix, proxy.get())
Michael Ludwigddeed372019-02-20 16:50:10 -0500175 , fDomain(proxy.get(), domain, modeX, modeY)
Brian Salomon6cd51b52017-07-26 19:07:15 -0400176 , fTextureSampler(std::move(proxy),
Brian Osmane22dba82019-03-13 10:22:28 -0400177 GrSamplerState(wrapModes, GrSamplerState::Filter::kNearest))
Brian Salomona86fc7a2019-05-28 20:42:58 -0400178 , fAlphaType(alphaType)
179 , fDirection(direction) {
Brian Salomon6cd51b52017-07-26 19:07:15 -0400180 this->addCoordTransform(&fCoordTransform);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400181 this->setTextureSamplerCnt(1);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500182}
183
Brian Salomon3f6f9652017-07-28 07:34:05 -0400184GrBicubicEffect::GrBicubicEffect(const GrBicubicEffect& that)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400185 : INHERITED(kGrBicubicEffect_ClassID, that.optimizationFlags())
Brian Salomon3f6f9652017-07-28 07:34:05 -0400186 , fCoordTransform(that.fCoordTransform)
187 , fDomain(that.fDomain)
Brian Osmane22dba82019-03-13 10:22:28 -0400188 , fTextureSampler(that.fTextureSampler)
Brian Salomona86fc7a2019-05-28 20:42:58 -0400189 , fAlphaType(that.fAlphaType)
190 , fDirection(that.fDirection) {
Brian Salomon3f6f9652017-07-28 07:34:05 -0400191 this->addCoordTransform(&fCoordTransform);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400192 this->setTextureSamplerCnt(1);
humper@google.com3aad3b02013-09-04 19:23:53 +0000193}
194
Brian Salomon94efbf52016-11-29 13:43:05 -0500195void GrBicubicEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800196 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800197 GrGLBicubicEffect::GenKey(*this, caps, b);
198}
199
egdaniel57d3b032015-11-13 11:57:27 -0800200GrGLSLFragmentProcessor* GrBicubicEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800201 return new GrGLBicubicEffect;
humper@google.com3aad3b02013-09-04 19:23:53 +0000202}
203
bsalomon0e08fc12014-10-15 08:19:04 -0700204bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700205 const GrBicubicEffect& s = sBase.cast<GrBicubicEffect>();
Brian Salomona86fc7a2019-05-28 20:42:58 -0400206 return fDomain == s.fDomain && fDirection == s.fDirection && fAlphaType == s.fAlphaType;
humper@google.com3aad3b02013-09-04 19:23:53 +0000207}
208
joshualittb0a8a372014-09-23 09:50:21 -0700209GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect);
humper@google.com3aad3b02013-09-04 19:23:53 +0000210
Hal Canary6f6961e2017-01-31 13:50:44 -0500211#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400212std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) {
Robert Phillips40fd7c92017-01-30 08:06:27 -0500213 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
214 : GrProcessorUnitTest::kAlphaTextureIdx;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400215 static const GrSamplerState::WrapMode kClampClamp[] = {GrSamplerState::WrapMode::kClamp,
216 GrSamplerState::WrapMode::kClamp};
Brian Osmane22dba82019-03-13 10:22:28 -0400217 SkAlphaType alphaType = d->fRandom->nextBool() ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
Brian Salomona86fc7a2019-05-28 20:42:58 -0400218 Direction direction = Direction::kX;
219 switch (d->fRandom->nextULessThan(3)) {
220 case 0:
221 direction = Direction::kX;
222 break;
223 case 1:
224 direction = Direction::kY;
225 break;
226 case 2:
227 direction = Direction::kXY;
228 break;
229 }
Brian Salomon078e8fa2019-11-22 04:10:18 +0000230 return GrBicubicEffect::Make(d->textureProxy(texIdx), d->textureProxyColorType(texIdx),
231 SkMatrix::I(), kClampClamp, direction, alphaType);
humper@google.com3aad3b02013-09-04 19:23:53 +0000232}
Hal Canary6f6961e2017-01-31 13:50:44 -0500233#endif
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000234
235//////////////////////////////////////////////////////////////////////////////
236
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400237bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix, GrSamplerState::Filter* filterMode) {
Mike Reedb2e3c642019-08-02 11:57:10 -0400238 switch (SkMatrixPriv::AdjustHighQualityFilterLevel(matrix)) {
Mike Reedd2f4be32019-08-01 13:52:38 -0400239 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400240 *filterMode = GrSamplerState::Filter::kNearest;
Mike Reedd2f4be32019-08-01 13:52:38 -0400241 break;
242 case kLow_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400243 *filterMode = GrSamplerState::Filter::kBilerp;
Mike Reedd2f4be32019-08-01 13:52:38 -0400244 break;
245 case kMedium_SkFilterQuality:
246 *filterMode = GrSamplerState::Filter::kMipMap;
247 break;
248 case kHigh_SkFilterQuality:
249 // When we use the bicubic filtering effect each sample is read from the texture using
250 // nearest neighbor sampling.
251 *filterMode = GrSamplerState::Filter::kNearest;
252 return true;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000253 }
Mike Reedd2f4be32019-08-01 13:52:38 -0400254 return false;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000255}