blob: b3bd5e23e658df36895ffd8753ed9e22b7db8709 [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
humper@google.com3aad3b02013-09-04 19:23:53 +00008#include "GrBicubicEffect.h"
Robert Phillips296b1cc2017-03-15 10:42:12 -04009
Robert Phillips646e4292017-06-13 12:44:56 -040010#include "GrTexture.h"
egdaniel2d721d32015-11-11 13:06:05 -080011#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070012#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080013#include "glsl/GrGLSLUniformHandler.h"
bsalomon848faf02014-07-11 10:01:02 -070014
egdaniel64c47282015-11-13 06:54:19 -080015class GrGLBicubicEffect : public GrGLSLFragmentProcessor {
humper@google.com3aad3b02013-09-04 19:23:53 +000016public:
robertphillips9cdb9922016-02-03 12:25:40 -080017 void emitCode(EmitArgs&) override;
humper@google.com3aad3b02013-09-04 19:23:53 +000018
Brian Salomon94efbf52016-11-29 13:43:05 -050019 static inline void GenKey(const GrProcessor& effect, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -070020 GrProcessorKeyBuilder* b) {
brianosman54f30c12016-07-18 10:53:52 -070021 const GrBicubicEffect& bicubicEffect = effect.cast<GrBicubicEffect>();
22 b->add32(GrTextureDomain::GLDomain::DomainKey(bicubicEffect.domain()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000023 }
24
wangyixb1daa862015-08-18 11:29:31 -070025protected:
Brian Salomonab015ef2017-04-04 10:15:51 -040026 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -070027
humper@google.com3aad3b02013-09-04 19:23:53 +000028private:
egdaniel018fb622015-10-28 07:26:40 -070029 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
humper@google.com3aad3b02013-09-04 19:23:53 +000030
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000031 UniformHandle fImageIncrementUni;
32 GrTextureDomain::GLDomain fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +000033
egdaniel64c47282015-11-13 06:54:19 -080034 typedef GrGLSLFragmentProcessor INHERITED;
humper@google.com3aad3b02013-09-04 19:23:53 +000035};
36
wangyix7c157a92015-07-22 15:08:53 -070037void GrGLBicubicEffect::emitCode(EmitArgs& args) {
brianosman54f30c12016-07-18 10:53:52 -070038 const GrBicubicEffect& bicubicEffect = args.fFp.cast<GrBicubicEffect>();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000039
egdaniel7ea439b2015-12-03 09:20:44 -080040 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040041 fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -080042 "ImageIncrement");
humper@google.com3aad3b02013-09-04 19:23:53 +000043
egdaniel7ea439b2015-12-03 09:20:44 -080044 const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni);
humper@google.com3aad3b02013-09-04 19:23:53 +000045
cdalton85285412016-02-18 12:37:07 -080046 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
bsalomon1a1aa932016-09-12 09:30:36 -070047 SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
Brian Osmana3fa2ef2017-01-06 16:13:06 +000048
49 /*
50 * Filter weights come from Don Mitchell & Arun Netravali's 'Reconstruction Filters in Computer
51 * Graphics', ACM SIGGRAPH Computer Graphics 22, 4 (Aug. 1988).
52 * ACM DL: http://dl.acm.org/citation.cfm?id=378514
53 * Free : http://www.cs.utexas.edu/users/fussell/courses/cs384g/lectures/mitchell/Mitchell.pdf
54 *
55 * The authors define a family of cubic filters with two free parameters (B and C):
56 *
57 * { (12 - 9B - 6C)|x|^3 + (-18 + 12B + 6C)|x|^2 + (6 - 2B) if |x| < 1
58 * k(x) = 1/6 { (-B - 6C)|x|^3 + (6B + 30C)|x|^2 + (-12B - 48C)|x| + (8B + 24C) if 1 <= |x| < 2
59 * { 0 otherwise
60 *
61 * Various well-known cubic splines can be generated, and the authors select (1/3, 1/3) as their
62 * favorite overall spline - this is now commonly known as the Mitchell filter, and is the
63 * source of the specific weights below.
64 *
65 * This is GLSL, so the matrix is column-major (transposed from standard matrix notation).
66 */
Ethan Nicholasf7b88202017-09-18 14:10:39 -040067 fragBuilder->codeAppend("half4x4 kMitchellCoefficients = half4x4("
Brian Osmana3fa2ef2017-01-06 16:13:06 +000068 " 1.0 / 18.0, 16.0 / 18.0, 1.0 / 18.0, 0.0 / 18.0,"
69 "-9.0 / 18.0, 0.0 / 18.0, 9.0 / 18.0, 0.0 / 18.0,"
70 "15.0 / 18.0, -36.0 / 18.0, 27.0 / 18.0, -6.0 / 18.0,"
71 "-7.0 / 18.0, 21.0 / 18.0, -21.0 / 18.0, 7.0 / 18.0);");
Ethan Nicholas8aa45692017-09-20 11:24:15 -040072 fragBuilder->codeAppendf("float2 coord = %s - %s * float2(0.5);", coords2D.c_str(), imgInc);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000073 // We unnormalize the coord in order to determine our fractional offset (f) within the texel
74 // We then snap coord to a texel center and renormalize. The snap prevents cases where the
75 // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/
76 // double hit a texel.
Brian Osmana3fa2ef2017-01-06 16:13:06 +000077 fragBuilder->codeAppendf("coord /= %s;", imgInc);
Ethan Nicholase1f55022019-02-05 17:17:40 -050078 fragBuilder->codeAppend("half2 f = half2(fract(coord));");
79 fragBuilder->codeAppendf("coord = (coord - f + half2(0.5)) * %s;", imgInc);
Ethan Nicholasf7b88202017-09-18 14:10:39 -040080 fragBuilder->codeAppend("half4 wx = kMitchellCoefficients * half4(1.0, f.x, f.x * f.x, f.x * f.x * f.x);");
81 fragBuilder->codeAppend("half4 wy = kMitchellCoefficients * half4(1.0, f.y, f.y * f.y, f.y * f.y * f.y);");
82 fragBuilder->codeAppend("half4 rowColors[4];");
humper@google.com3aad3b02013-09-04 19:23:53 +000083 for (int y = 0; y < 4; ++y) {
84 for (int x = 0; x < 4; ++x) {
85 SkString coord;
Ethan Nicholas8aa45692017-09-20 11:24:15 -040086 coord.printf("coord + %s * float2(%d, %d)", imgInc, x - 1, y - 1);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000087 SkString sampleVar;
88 sampleVar.printf("rowColors[%d]", x);
egdaniela2e3e0f2015-11-19 07:23:45 -080089 fDomain.sampleTexture(fragBuilder,
egdaniel7ea439b2015-12-03 09:20:44 -080090 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -050091 args.fShaderCaps,
brianosman54f30c12016-07-18 10:53:52 -070092 bicubicEffect.domain(),
egdaniela2e3e0f2015-11-19 07:23:45 -080093 sampleVar.c_str(),
94 coord,
cdalton3f6f76f2016-04-11 12:18:09 -070095 args.fTexSamplers[0]);
humper@google.com3aad3b02013-09-04 19:23:53 +000096 }
egdaniel4ca2e602015-11-18 08:01:26 -080097 fragBuilder->codeAppendf(
Ethan Nicholasf7b88202017-09-18 14:10:39 -040098 "half4 s%d = wx.x * rowColors[0] + wx.y * rowColors[1] + wx.z * rowColors[2] + wx.w * rowColors[3];",
Brian Osmana3fa2ef2017-01-06 16:13:06 +000099 y);
humper@google.com3aad3b02013-09-04 19:23:53 +0000100 }
Brian Osmana3fa2ef2017-01-06 16:13:06 +0000101 SkString bicubicColor("(wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3)");
Ethan Nicholas2983f402017-05-08 09:36:08 -0400102 fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputColor, bicubicColor.c_str(),
103 args.fInputColor);
humper@google.com3aad3b02013-09-04 19:23:53 +0000104}
105
egdaniel018fb622015-10-28 07:26:40 -0700106void GrGLBicubicEffect::onSetData(const GrGLSLProgramDataManager& pdman,
Brian Salomonab015ef2017-04-04 10:15:51 -0400107 const GrFragmentProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -0700108 const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>();
Brian Salomon7da2fc72018-12-10 13:40:07 -0500109 GrTextureProxy* proxy = processor.textureSampler(0).proxy();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400110 GrTexture* texture = proxy->peekTexture();
Robert Phillips9bee2e52017-05-29 12:37:20 -0400111
humper@google.com3aad3b02013-09-04 19:23:53 +0000112 float imageIncrement[2];
Brian Salomon0bbecb22016-11-17 11:38:22 -0500113 imageIncrement[0] = 1.0f / texture->width();
114 imageIncrement[1] = 1.0f / texture->height();
kkinnunen7510b222014-07-30 00:04:16 -0700115 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
Michael Ludwigbe315a22018-12-17 09:50:51 -0500116 fDomain.setData(pdman, bicubicEffect.domain(), proxy,
117 processor.textureSampler(0).samplerState());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000118}
119
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400120GrBicubicEffect::GrBicubicEffect(sk_sp<GrTextureProxy> proxy,
Brian Salomon6cd51b52017-07-26 19:07:15 -0400121 const SkMatrix& matrix,
Michael Ludwigbe315a22018-12-17 09:50:51 -0500122 const GrSamplerState::WrapMode wrapModes[2],
123 GrTextureDomain::Mode modeX, GrTextureDomain::Mode modeY)
Michael Ludwig257a03d2018-12-13 14:07:07 -0500124 : INHERITED{kGrBicubicEffect_ClassID,
125 ModulateForSamplerOptFlags(proxy->config(),
126 GrTextureDomain::IsDecalSampled(wrapModes, modeX,modeY))}
Brian Salomon6cd51b52017-07-26 19:07:15 -0400127 , fCoordTransform(matrix, proxy.get())
Michael Ludwigbe315a22018-12-17 09:50:51 -0500128 , fDomain(proxy.get(),
129 GrTextureDomain::MakeTexelDomain(
130 SkIRect::MakeWH(proxy->width(), proxy->height()), modeX, modeY),
131 modeX, modeY)
Brian Salomon6cd51b52017-07-26 19:07:15 -0400132 , fTextureSampler(std::move(proxy),
Brian Osman5e341672017-10-18 10:23:18 -0400133 GrSamplerState(wrapModes, GrSamplerState::Filter::kNearest)) {
Brian Salomon6cd51b52017-07-26 19:07:15 -0400134 this->addCoordTransform(&fCoordTransform);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400135 this->setTextureSamplerCnt(1);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500136}
137
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400138GrBicubicEffect::GrBicubicEffect(sk_sp<GrTextureProxy> proxy,
Brian Salomon6cd51b52017-07-26 19:07:15 -0400139 const SkMatrix& matrix,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500140 const SkRect& domain)
Michael Ludwig257a03d2018-12-13 14:07:07 -0500141 : INHERITED(kGrBicubicEffect_ClassID, ModulateForClampedSamplerOptFlags(proxy->config()))
Brian Salomon6cd51b52017-07-26 19:07:15 -0400142 , fCoordTransform(matrix, proxy.get())
Michael Ludwigbe315a22018-12-17 09:50:51 -0500143 , fDomain(proxy.get(), domain, GrTextureDomain::kClamp_Mode, GrTextureDomain::kClamp_Mode)
Brian Osman5e341672017-10-18 10:23:18 -0400144 , fTextureSampler(std::move(proxy)) {
Michael Ludwig257a03d2018-12-13 14:07:07 -0500145 // Make sure the sampler's ctor uses the clamp wrap mode
146 SkASSERT(fTextureSampler.samplerState().wrapModeX() == GrSamplerState::WrapMode::kClamp &&
147 fTextureSampler.samplerState().wrapModeY() == GrSamplerState::WrapMode::kClamp);
Brian Salomon6cd51b52017-07-26 19:07:15 -0400148 this->addCoordTransform(&fCoordTransform);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400149 this->setTextureSamplerCnt(1);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500150}
151
Brian Salomon3f6f9652017-07-28 07:34:05 -0400152GrBicubicEffect::GrBicubicEffect(const GrBicubicEffect& that)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400153 : INHERITED(kGrBicubicEffect_ClassID, that.optimizationFlags())
Brian Salomon3f6f9652017-07-28 07:34:05 -0400154 , fCoordTransform(that.fCoordTransform)
155 , fDomain(that.fDomain)
Brian Osman5e341672017-10-18 10:23:18 -0400156 , fTextureSampler(that.fTextureSampler) {
Brian Salomon3f6f9652017-07-28 07:34:05 -0400157 this->addCoordTransform(&fCoordTransform);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400158 this->setTextureSamplerCnt(1);
humper@google.com3aad3b02013-09-04 19:23:53 +0000159}
160
Brian Salomon94efbf52016-11-29 13:43:05 -0500161void GrBicubicEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800162 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800163 GrGLBicubicEffect::GenKey(*this, caps, b);
164}
165
egdaniel57d3b032015-11-13 11:57:27 -0800166GrGLSLFragmentProcessor* GrBicubicEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800167 return new GrGLBicubicEffect;
humper@google.com3aad3b02013-09-04 19:23:53 +0000168}
169
bsalomon0e08fc12014-10-15 08:19:04 -0700170bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700171 const GrBicubicEffect& s = sBase.cast<GrBicubicEffect>();
Brian Osman08575272016-12-21 15:26:37 -0500172 return fDomain == s.fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +0000173}
174
joshualittb0a8a372014-09-23 09:50:21 -0700175GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect);
humper@google.com3aad3b02013-09-04 19:23:53 +0000176
Hal Canary6f6961e2017-01-31 13:50:44 -0500177#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400178std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) {
Robert Phillips40fd7c92017-01-30 08:06:27 -0500179 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
180 : GrProcessorUnitTest::kAlphaTextureIdx;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400181 static const GrSamplerState::WrapMode kClampClamp[] = {GrSamplerState::WrapMode::kClamp,
182 GrSamplerState::WrapMode::kClamp};
Brian Osman5e341672017-10-18 10:23:18 -0400183 return GrBicubicEffect::Make(d->textureProxy(texIdx), SkMatrix::I(), kClampClamp);
humper@google.com3aad3b02013-09-04 19:23:53 +0000184}
Hal Canary6f6961e2017-01-31 13:50:44 -0500185#endif
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000186
187//////////////////////////////////////////////////////////////////////////////
188
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400189bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix, GrSamplerState::Filter* filterMode) {
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000190 if (matrix.isIdentity()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400191 *filterMode = GrSamplerState::Filter::kNearest;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000192 return false;
193 }
194
195 SkScalar scales[2];
196 if (!matrix.getMinMaxScales(scales) || scales[0] < SK_Scalar1) {
197 // Bicubic doesn't handle arbitrary minimization well, as src texels can be skipped
198 // entirely,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400199 *filterMode = GrSamplerState::Filter::kMipMap;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000200 return false;
201 }
202 // At this point if scales[1] == SK_Scalar1 then the matrix doesn't do any scaling.
203 if (scales[1] == SK_Scalar1) {
204 if (matrix.rectStaysRect() && SkScalarIsInt(matrix.getTranslateX()) &&
205 SkScalarIsInt(matrix.getTranslateY())) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400206 *filterMode = GrSamplerState::Filter::kNearest;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000207 } else {
208 // Use bilerp to handle rotation or fractional translation.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400209 *filterMode = GrSamplerState::Filter::kBilerp;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000210 }
211 return false;
212 }
213 // When we use the bicubic filtering effect each sample is read from the texture using
214 // nearest neighbor sampling.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400215 *filterMode = GrSamplerState::Filter::kNearest;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000216 return true;
217}