blob: 9d8e728efcef156f74998792505fc5ab9c05bd82 [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"
egdaniel605dd0f2014-11-12 08:35:25 -08009#include "GrInvariantOutput.h"
brianosman54f30c12016-07-18 10:53:52 -070010#include "glsl/GrGLSLColorSpaceXformHelper.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"
Brian Salomon94efbf52016-11-29 13:43:05 -050014#include "../private/GrGLSL.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()));
brianosman77320db2016-09-07 08:09:10 -070024 b->add32(GrColorSpaceXform::XformKey(bicubicEffect.colorSpaceXform()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000025 }
26
wangyixb1daa862015-08-18 11:29:31 -070027protected:
egdaniel018fb622015-10-28 07:26:40 -070028 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -070029
humper@google.com3aad3b02013-09-04 19:23:53 +000030private:
egdaniel018fb622015-10-28 07:26:40 -070031 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
humper@google.com3aad3b02013-09-04 19:23:53 +000032
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000033 UniformHandle fImageIncrementUni;
brianosman54f30c12016-07-18 10:53:52 -070034 UniformHandle fColorSpaceXformUni;
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +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;
cdalton5e58cee2016-02-11 12:49:47 -080044 fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -080045 kVec2f_GrSLType, kDefault_GrSLPrecision,
46 "ImageIncrement");
humper@google.com3aad3b02013-09-04 19:23:53 +000047
egdaniel7ea439b2015-12-03 09:20:44 -080048 const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni);
humper@google.com3aad3b02013-09-04 19:23:53 +000049
brianosman54f30c12016-07-18 10:53:52 -070050 GrGLSLColorSpaceXformHelper colorSpaceHelper(uniformHandler, bicubicEffect.colorSpaceXform(),
51 &fColorSpaceXformUni);
52
cdalton85285412016-02-18 12:37:07 -080053 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
bsalomon1a1aa932016-09-12 09:30:36 -070054 SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
Brian Osmana3fa2ef2017-01-06 16:13:06 +000055
56 /*
57 * Filter weights come from Don Mitchell & Arun Netravali's 'Reconstruction Filters in Computer
58 * Graphics', ACM SIGGRAPH Computer Graphics 22, 4 (Aug. 1988).
59 * ACM DL: http://dl.acm.org/citation.cfm?id=378514
60 * Free : http://www.cs.utexas.edu/users/fussell/courses/cs384g/lectures/mitchell/Mitchell.pdf
61 *
62 * The authors define a family of cubic filters with two free parameters (B and C):
63 *
64 * { (12 - 9B - 6C)|x|^3 + (-18 + 12B + 6C)|x|^2 + (6 - 2B) if |x| < 1
65 * k(x) = 1/6 { (-B - 6C)|x|^3 + (6B + 30C)|x|^2 + (-12B - 48C)|x| + (8B + 24C) if 1 <= |x| < 2
66 * { 0 otherwise
67 *
68 * Various well-known cubic splines can be generated, and the authors select (1/3, 1/3) as their
69 * favorite overall spline - this is now commonly known as the Mitchell filter, and is the
70 * source of the specific weights below.
71 *
72 * This is GLSL, so the matrix is column-major (transposed from standard matrix notation).
73 */
74 fragBuilder->codeAppend("mat4 kMitchellCoefficients = mat4("
75 " 1.0 / 18.0, 16.0 / 18.0, 1.0 / 18.0, 0.0 / 18.0,"
76 "-9.0 / 18.0, 0.0 / 18.0, 9.0 / 18.0, 0.0 / 18.0,"
77 "15.0 / 18.0, -36.0 / 18.0, 27.0 / 18.0, -6.0 / 18.0,"
78 "-7.0 / 18.0, 21.0 / 18.0, -21.0 / 18.0, 7.0 / 18.0);");
79 fragBuilder->codeAppendf("vec2 coord = %s - %s * vec2(0.5);", coords2D.c_str(), imgInc);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000080 // We unnormalize the coord in order to determine our fractional offset (f) within the texel
81 // We then snap coord to a texel center and renormalize. The snap prevents cases where the
82 // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/
83 // double hit a texel.
Brian Osmana3fa2ef2017-01-06 16:13:06 +000084 fragBuilder->codeAppendf("coord /= %s;", imgInc);
85 fragBuilder->codeAppend("vec2 f = fract(coord);");
86 fragBuilder->codeAppendf("coord = (coord - f + vec2(0.5)) * %s;", imgInc);
87 fragBuilder->codeAppend("vec4 wx = kMitchellCoefficients * vec4(1.0, f.x, f.x * f.x, f.x * f.x * f.x);");
88 fragBuilder->codeAppend("vec4 wy = kMitchellCoefficients * vec4(1.0, f.y, f.y * f.y, f.y * f.y * f.y);");
89 fragBuilder->codeAppend("vec4 rowColors[4];");
humper@google.com3aad3b02013-09-04 19:23:53 +000090 for (int y = 0; y < 4; ++y) {
91 for (int x = 0; x < 4; ++x) {
92 SkString coord;
93 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000094 SkString sampleVar;
95 sampleVar.printf("rowColors[%d]", x);
egdaniela2e3e0f2015-11-19 07:23:45 -080096 fDomain.sampleTexture(fragBuilder,
egdaniel7ea439b2015-12-03 09:20:44 -080097 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -050098 args.fShaderCaps,
brianosman54f30c12016-07-18 10:53:52 -070099 bicubicEffect.domain(),
egdaniela2e3e0f2015-11-19 07:23:45 -0800100 sampleVar.c_str(),
101 coord,
cdalton3f6f76f2016-04-11 12:18:09 -0700102 args.fTexSamplers[0]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000103 }
egdaniel4ca2e602015-11-18 08:01:26 -0800104 fragBuilder->codeAppendf(
Brian Osmana3fa2ef2017-01-06 16:13:06 +0000105 "vec4 s%d = wx.x * rowColors[0] + wx.y * rowColors[1] + wx.z * rowColors[2] + wx.w * rowColors[3];",
106 y);
humper@google.com3aad3b02013-09-04 19:23:53 +0000107 }
Brian Osmana3fa2ef2017-01-06 16:13:06 +0000108 SkString bicubicColor("(wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3)");
brianosman54f30c12016-07-18 10:53:52 -0700109 if (colorSpaceHelper.getXformMatrix()) {
brianosman77320db2016-09-07 08:09:10 -0700110 SkString xformedColor;
111 fragBuilder->appendColorGamutXform(&xformedColor, bicubicColor.c_str(), &colorSpaceHelper);
112 bicubicColor.swap(xformedColor);
brianosman54f30c12016-07-18 10:53:52 -0700113 }
Brian Osmana3fa2ef2017-01-06 16:13:06 +0000114 fragBuilder->codeAppendf("%s = %s;",
egdaniel4ca2e602015-11-18 08:01:26 -0800115 args.fOutputColor, (GrGLSLExpr4(bicubicColor.c_str()) *
116 GrGLSLExpr4(args.fInputColor)).c_str());
humper@google.com3aad3b02013-09-04 19:23:53 +0000117}
118
egdaniel018fb622015-10-28 07:26:40 -0700119void GrGLBicubicEffect::onSetData(const GrGLSLProgramDataManager& pdman,
120 const GrProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -0700121 const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>();
Brian Salomondb4183d2016-11-17 12:48:40 -0500122 GrTexture* texture = processor.textureSampler(0).texture();
humper@google.com3aad3b02013-09-04 19:23:53 +0000123 float imageIncrement[2];
Brian Salomon0bbecb22016-11-17 11:38:22 -0500124 imageIncrement[0] = 1.0f / texture->width();
125 imageIncrement[1] = 1.0f / texture->height();
kkinnunen7510b222014-07-30 00:04:16 -0700126 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
Brian Salomon0bbecb22016-11-17 11:38:22 -0500127 fDomain.setData(pdman, bicubicEffect.domain(), texture->origin());
brianosman54f30c12016-07-18 10:53:52 -0700128 if (SkToBool(bicubicEffect.colorSpaceXform())) {
brianosman51924752016-09-12 08:50:19 -0700129 pdman.setSkMatrix44(fColorSpaceXformUni, bicubicEffect.colorSpaceXform()->srcToDst());
brianosman54f30c12016-07-18 10:53:52 -0700130 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000131}
132
bsalomon4a339522015-10-06 08:40:50 -0700133GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
brianosman54f30c12016-07-18 10:53:52 -0700134 sk_sp<GrColorSpaceXform> colorSpaceXform,
humper@google.com3aad3b02013-09-04 19:23:53 +0000135 const SkMatrix &matrix,
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +0000136 const SkShader::TileMode tileModes[2])
Brian Osman08575272016-12-21 15:26:37 -0500137 : INHERITED(texture, std::move(colorSpaceXform), matrix,
Brian Salomon514baff2016-11-17 15:17:07 -0500138 GrSamplerParams(tileModes, GrSamplerParams::kNone_FilterMode))
Brian Osman08575272016-12-21 15:26:37 -0500139 , fDomain(GrTextureDomain::IgnoredDomain()) {
joshualitteb2a6762014-12-04 11:35:33 -0800140 this->initClassID<GrBicubicEffect>();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000141}
142
bsalomon4a339522015-10-06 08:40:50 -0700143GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
brianosman54f30c12016-07-18 10:53:52 -0700144 sk_sp<GrColorSpaceXform> colorSpaceXform,
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000145 const SkMatrix &matrix,
146 const SkRect& domain)
Brian Osman08575272016-12-21 15:26:37 -0500147 : INHERITED(texture, std::move(colorSpaceXform), matrix,
Brian Salomon514baff2016-11-17 15:17:07 -0500148 GrSamplerParams(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode))
Brian Osman08575272016-12-21 15:26:37 -0500149 , fDomain(domain, GrTextureDomain::kClamp_Mode) {
joshualitteb2a6762014-12-04 11:35:33 -0800150 this->initClassID<GrBicubicEffect>();
humper@google.com3aad3b02013-09-04 19:23:53 +0000151}
152
153GrBicubicEffect::~GrBicubicEffect() {
154}
155
Brian Salomon94efbf52016-11-29 13:43:05 -0500156void GrBicubicEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800157 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800158 GrGLBicubicEffect::GenKey(*this, caps, b);
159}
160
egdaniel57d3b032015-11-13 11:57:27 -0800161GrGLSLFragmentProcessor* GrBicubicEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800162 return new GrGLBicubicEffect;
humper@google.com3aad3b02013-09-04 19:23:53 +0000163}
164
bsalomon0e08fc12014-10-15 08:19:04 -0700165bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700166 const GrBicubicEffect& s = sBase.cast<GrBicubicEffect>();
Brian Osman08575272016-12-21 15:26:37 -0500167 return fDomain == s.fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +0000168}
169
egdaniel605dd0f2014-11-12 08:35:25 -0800170void GrBicubicEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000171 // FIXME: Perhaps we can do better.
joshualitt56995b52014-12-11 15:44:02 -0800172 inout->mulByUnknownSingleComponent();
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
bungeman06ca8ec2016-06-09 08:01:03 -0700177sk_sp<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700178 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
179 GrProcessorUnitTest::kAlphaTextureIdx;
Brian Osmane2f732f2016-10-03 14:23:50 -0400180 auto colorSpaceXform = GrTest::TestColorXform(d->fRandom);
Brian Osman08575272016-12-21 15:26:37 -0500181 static const SkShader::TileMode kClampClamp[] =
182 { SkShader::kClamp_TileMode, SkShader::kClamp_TileMode };
183 return GrBicubicEffect::Make(d->fTextures[texIdx], colorSpaceXform,
184 GrCoordTransform::MakeDivByTextureWHMatrix(d->fTextures[texIdx]),
185 kClampClamp);
humper@google.com3aad3b02013-09-04 19:23:53 +0000186}
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000187
188//////////////////////////////////////////////////////////////////////////////
189
190bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix,
Brian Salomon514baff2016-11-17 15:17:07 -0500191 GrSamplerParams::FilterMode* filterMode) {
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000192 if (matrix.isIdentity()) {
Brian Salomon514baff2016-11-17 15:17:07 -0500193 *filterMode = GrSamplerParams::kNone_FilterMode;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000194 return false;
195 }
196
197 SkScalar scales[2];
198 if (!matrix.getMinMaxScales(scales) || scales[0] < SK_Scalar1) {
199 // Bicubic doesn't handle arbitrary minimization well, as src texels can be skipped
200 // entirely,
Brian Salomon514baff2016-11-17 15:17:07 -0500201 *filterMode = GrSamplerParams::kMipMap_FilterMode;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000202 return false;
203 }
204 // At this point if scales[1] == SK_Scalar1 then the matrix doesn't do any scaling.
205 if (scales[1] == SK_Scalar1) {
206 if (matrix.rectStaysRect() && SkScalarIsInt(matrix.getTranslateX()) &&
207 SkScalarIsInt(matrix.getTranslateY())) {
Brian Salomon514baff2016-11-17 15:17:07 -0500208 *filterMode = GrSamplerParams::kNone_FilterMode;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000209 } else {
210 // Use bilerp to handle rotation or fractional translation.
Brian Salomon514baff2016-11-17 15:17:07 -0500211 *filterMode = GrSamplerParams::kBilerp_FilterMode;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000212 }
213 return false;
214 }
215 // When we use the bicubic filtering effect each sample is read from the texture using
216 // nearest neighbor sampling.
Brian Salomon514baff2016-11-17 15:17:07 -0500217 *filterMode = GrSamplerParams::kNone_FilterMode;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000218 return true;
219}