blob: 743b9e094fb60444b14a19492e24dffffccacfd6 [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"
brianosman54f30c12016-07-18 10:53:52 -070011#include "glsl/GrGLSLColorSpaceXformHelper.h"
egdaniel2d721d32015-11-11 13:06:05 -080012#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070013#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080014#include "glsl/GrGLSLUniformHandler.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050015#include "../private/GrGLSL.h"
bsalomon848faf02014-07-11 10:01:02 -070016
egdaniel64c47282015-11-13 06:54:19 -080017class GrGLBicubicEffect : public GrGLSLFragmentProcessor {
humper@google.com3aad3b02013-09-04 19:23:53 +000018public:
robertphillips9cdb9922016-02-03 12:25:40 -080019 void emitCode(EmitArgs&) override;
humper@google.com3aad3b02013-09-04 19:23:53 +000020
Brian Salomon94efbf52016-11-29 13:43:05 -050021 static inline void GenKey(const GrProcessor& effect, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -070022 GrProcessorKeyBuilder* b) {
brianosman54f30c12016-07-18 10:53:52 -070023 const GrBicubicEffect& bicubicEffect = effect.cast<GrBicubicEffect>();
24 b->add32(GrTextureDomain::GLDomain::DomainKey(bicubicEffect.domain()));
brianosman77320db2016-09-07 08:09:10 -070025 b->add32(GrColorSpaceXform::XformKey(bicubicEffect.colorSpaceXform()));
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
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000034 UniformHandle fImageIncrementUni;
Brian Osmanc624d9d2017-03-08 11:42:02 -050035 GrGLSLColorSpaceXformHelper fColorSpaceHelper;
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000036 GrTextureDomain::GLDomain fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +000037
egdaniel64c47282015-11-13 06:54:19 -080038 typedef GrGLSLFragmentProcessor INHERITED;
humper@google.com3aad3b02013-09-04 19:23:53 +000039};
40
wangyix7c157a92015-07-22 15:08:53 -070041void GrGLBicubicEffect::emitCode(EmitArgs& args) {
brianosman54f30c12016-07-18 10:53:52 -070042 const GrBicubicEffect& bicubicEffect = args.fFp.cast<GrBicubicEffect>();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000043
egdaniel7ea439b2015-12-03 09:20:44 -080044 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040045 fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -080046 "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
Brian Osmanc624d9d2017-03-08 11:42:02 -050050 fColorSpaceHelper.emitCode(uniformHandler, bicubicEffect.colorSpaceXform());
brianosman54f30c12016-07-18 10:53:52 -070051
cdalton85285412016-02-18 12:37:07 -080052 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
bsalomon1a1aa932016-09-12 09:30:36 -070053 SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
Brian Osmana3fa2ef2017-01-06 16:13:06 +000054
55 /*
56 * Filter weights come from Don Mitchell & Arun Netravali's 'Reconstruction Filters in Computer
57 * Graphics', ACM SIGGRAPH Computer Graphics 22, 4 (Aug. 1988).
58 * ACM DL: http://dl.acm.org/citation.cfm?id=378514
59 * Free : http://www.cs.utexas.edu/users/fussell/courses/cs384g/lectures/mitchell/Mitchell.pdf
60 *
61 * The authors define a family of cubic filters with two free parameters (B and C):
62 *
63 * { (12 - 9B - 6C)|x|^3 + (-18 + 12B + 6C)|x|^2 + (6 - 2B) if |x| < 1
64 * k(x) = 1/6 { (-B - 6C)|x|^3 + (6B + 30C)|x|^2 + (-12B - 48C)|x| + (8B + 24C) if 1 <= |x| < 2
65 * { 0 otherwise
66 *
67 * Various well-known cubic splines can be generated, and the authors select (1/3, 1/3) as their
68 * favorite overall spline - this is now commonly known as the Mitchell filter, and is the
69 * source of the specific weights below.
70 *
71 * This is GLSL, so the matrix is column-major (transposed from standard matrix notation).
72 */
Ethan Nicholasf7b88202017-09-18 14:10:39 -040073 fragBuilder->codeAppend("half4x4 kMitchellCoefficients = half4x4("
Brian Osmana3fa2ef2017-01-06 16:13:06 +000074 " 1.0 / 18.0, 16.0 / 18.0, 1.0 / 18.0, 0.0 / 18.0,"
75 "-9.0 / 18.0, 0.0 / 18.0, 9.0 / 18.0, 0.0 / 18.0,"
76 "15.0 / 18.0, -36.0 / 18.0, 27.0 / 18.0, -6.0 / 18.0,"
77 "-7.0 / 18.0, 21.0 / 18.0, -21.0 / 18.0, 7.0 / 18.0);");
Ethan Nicholasf7b88202017-09-18 14:10:39 -040078 fragBuilder->codeAppendf("highfloat2 coord = %s - %s * highfloat2(0.5);", coords2D.c_str(), imgInc);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000079 // We unnormalize the coord in order to determine our fractional offset (f) within the texel
80 // We then snap coord to a texel center and renormalize. The snap prevents cases where the
81 // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/
82 // double hit a texel.
Brian Osmana3fa2ef2017-01-06 16:13:06 +000083 fragBuilder->codeAppendf("coord /= %s;", imgInc);
Ethan Nicholasf7b88202017-09-18 14:10:39 -040084 fragBuilder->codeAppend("highfloat2 f = fract(coord);");
85 fragBuilder->codeAppendf("coord = (coord - f + highfloat2(0.5)) * %s;", imgInc);
86 fragBuilder->codeAppend("half4 wx = kMitchellCoefficients * half4(1.0, f.x, f.x * f.x, f.x * f.x * f.x);");
87 fragBuilder->codeAppend("half4 wy = kMitchellCoefficients * half4(1.0, f.y, f.y * f.y, f.y * f.y * f.y);");
88 fragBuilder->codeAppend("half4 rowColors[4];");
humper@google.com3aad3b02013-09-04 19:23:53 +000089 for (int y = 0; y < 4; ++y) {
90 for (int x = 0; x < 4; ++x) {
91 SkString coord;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040092 coord.printf("coord + %s * highfloat2(%d, %d)", imgInc, x - 1, y - 1);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000093 SkString sampleVar;
94 sampleVar.printf("rowColors[%d]", x);
egdaniela2e3e0f2015-11-19 07:23:45 -080095 fDomain.sampleTexture(fragBuilder,
egdaniel7ea439b2015-12-03 09:20:44 -080096 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -050097 args.fShaderCaps,
brianosman54f30c12016-07-18 10:53:52 -070098 bicubicEffect.domain(),
egdaniela2e3e0f2015-11-19 07:23:45 -080099 sampleVar.c_str(),
100 coord,
cdalton3f6f76f2016-04-11 12:18:09 -0700101 args.fTexSamplers[0]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000102 }
egdaniel4ca2e602015-11-18 08:01:26 -0800103 fragBuilder->codeAppendf(
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400104 "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 +0000105 y);
humper@google.com3aad3b02013-09-04 19:23:53 +0000106 }
Brian Osmana3fa2ef2017-01-06 16:13:06 +0000107 SkString bicubicColor("(wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3)");
Brian Osmanc624d9d2017-03-08 11:42:02 -0500108 if (fColorSpaceHelper.isValid()) {
brianosman77320db2016-09-07 08:09:10 -0700109 SkString xformedColor;
Brian Osmanc624d9d2017-03-08 11:42:02 -0500110 fragBuilder->appendColorGamutXform(&xformedColor, bicubicColor.c_str(), &fColorSpaceHelper);
brianosman77320db2016-09-07 08:09:10 -0700111 bicubicColor.swap(xformedColor);
brianosman54f30c12016-07-18 10:53:52 -0700112 }
Ethan Nicholas2983f402017-05-08 09:36:08 -0400113 fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputColor, bicubicColor.c_str(),
114 args.fInputColor);
humper@google.com3aad3b02013-09-04 19:23:53 +0000115}
116
egdaniel018fb622015-10-28 07:26:40 -0700117void GrGLBicubicEffect::onSetData(const GrGLSLProgramDataManager& pdman,
Brian Salomonab015ef2017-04-04 10:15:51 -0400118 const GrFragmentProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -0700119 const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>();
Robert Phillipsc686ce32017-07-21 14:12:29 -0400120 GrSurfaceProxy* proxy = processor.textureSampler(0).proxy();
121 GrTexture* texture = proxy->priv().peekTexture();
Robert Phillips9bee2e52017-05-29 12:37:20 -0400122
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);
Robert Phillipsc686ce32017-07-21 14:12:29 -0400127 fDomain.setData(pdman, bicubicEffect.domain(), proxy);
brianosman54f30c12016-07-18 10:53:52 -0700128 if (SkToBool(bicubicEffect.colorSpaceXform())) {
Brian Osmanc624d9d2017-03-08 11:42:02 -0500129 fColorSpaceHelper.setData(pdman, bicubicEffect.colorSpaceXform());
brianosman54f30c12016-07-18 10:53:52 -0700130 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000131}
132
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400133GrBicubicEffect::GrBicubicEffect(sk_sp<GrTextureProxy> proxy,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500134 sk_sp<GrColorSpaceXform> colorSpaceXform,
Brian Salomon6cd51b52017-07-26 19:07:15 -0400135 const SkMatrix& matrix,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400136 const GrSamplerState::WrapMode wrapModes[2])
Brian Salomon6cd51b52017-07-26 19:07:15 -0400137 : INHERITED{ModulateByConfigOptimizationFlags(proxy->config())}
138 , fCoordTransform(matrix, proxy.get())
139 , fDomain(GrTextureDomain::IgnoredDomain())
140 , fTextureSampler(std::move(proxy),
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400141 GrSamplerState(wrapModes, GrSamplerState::Filter::kNearest))
Brian Salomon6cd51b52017-07-26 19:07:15 -0400142 , fColorSpaceXform(std::move(colorSpaceXform)) {
Robert Phillips40fd7c92017-01-30 08:06:27 -0500143 this->initClassID<GrBicubicEffect>();
Brian Salomon6cd51b52017-07-26 19:07:15 -0400144 this->addCoordTransform(&fCoordTransform);
145 this->addTextureSampler(&fTextureSampler);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500146}
147
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400148GrBicubicEffect::GrBicubicEffect(sk_sp<GrTextureProxy> proxy,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500149 sk_sp<GrColorSpaceXform> colorSpaceXform,
Brian Salomon6cd51b52017-07-26 19:07:15 -0400150 const SkMatrix& matrix,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500151 const SkRect& domain)
Brian Salomon6cd51b52017-07-26 19:07:15 -0400152 : INHERITED(ModulateByConfigOptimizationFlags(proxy->config()))
153 , fCoordTransform(matrix, proxy.get())
154 , fDomain(proxy.get(), domain, GrTextureDomain::kClamp_Mode)
155 , fTextureSampler(std::move(proxy))
156 , fColorSpaceXform(std::move(colorSpaceXform)) {
Robert Phillips40fd7c92017-01-30 08:06:27 -0500157 this->initClassID<GrBicubicEffect>();
Brian Salomon6cd51b52017-07-26 19:07:15 -0400158 this->addCoordTransform(&fCoordTransform);
159 this->addTextureSampler(&fTextureSampler);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500160}
161
Brian Salomon3f6f9652017-07-28 07:34:05 -0400162GrBicubicEffect::GrBicubicEffect(const GrBicubicEffect& that)
163 : INHERITED(that.optimizationFlags())
164 , fCoordTransform(that.fCoordTransform)
165 , fDomain(that.fDomain)
166 , fTextureSampler(that.fTextureSampler)
167 , fColorSpaceXform(that.fColorSpaceXform) {
168 this->initClassID<GrBicubicEffect>();
169 this->addCoordTransform(&fCoordTransform);
170 this->addTextureSampler(&fTextureSampler);
humper@google.com3aad3b02013-09-04 19:23:53 +0000171}
172
Brian Salomon94efbf52016-11-29 13:43:05 -0500173void GrBicubicEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800174 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800175 GrGLBicubicEffect::GenKey(*this, caps, b);
176}
177
egdaniel57d3b032015-11-13 11:57:27 -0800178GrGLSLFragmentProcessor* GrBicubicEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800179 return new GrGLBicubicEffect;
humper@google.com3aad3b02013-09-04 19:23:53 +0000180}
181
bsalomon0e08fc12014-10-15 08:19:04 -0700182bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700183 const GrBicubicEffect& s = sBase.cast<GrBicubicEffect>();
Brian Osman08575272016-12-21 15:26:37 -0500184 return fDomain == s.fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +0000185}
186
joshualittb0a8a372014-09-23 09:50:21 -0700187GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect);
humper@google.com3aad3b02013-09-04 19:23:53 +0000188
Hal Canary6f6961e2017-01-31 13:50:44 -0500189#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400190std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) {
Robert Phillips40fd7c92017-01-30 08:06:27 -0500191 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
192 : GrProcessorUnitTest::kAlphaTextureIdx;
193 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(d->fRandom);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400194 static const GrSamplerState::WrapMode kClampClamp[] = {GrSamplerState::WrapMode::kClamp,
195 GrSamplerState::WrapMode::kClamp};
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400196 return GrBicubicEffect::Make(d->textureProxy(texIdx), std::move(colorSpaceXform),
Robert Phillips67c18d62017-01-20 12:44:06 -0500197 SkMatrix::I(), kClampClamp);
humper@google.com3aad3b02013-09-04 19:23:53 +0000198}
Hal Canary6f6961e2017-01-31 13:50:44 -0500199#endif
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000200
201//////////////////////////////////////////////////////////////////////////////
202
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400203bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix, GrSamplerState::Filter* filterMode) {
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000204 if (matrix.isIdentity()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400205 *filterMode = GrSamplerState::Filter::kNearest;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000206 return false;
207 }
208
209 SkScalar scales[2];
210 if (!matrix.getMinMaxScales(scales) || scales[0] < SK_Scalar1) {
211 // Bicubic doesn't handle arbitrary minimization well, as src texels can be skipped
212 // entirely,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400213 *filterMode = GrSamplerState::Filter::kMipMap;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000214 return false;
215 }
216 // At this point if scales[1] == SK_Scalar1 then the matrix doesn't do any scaling.
217 if (scales[1] == SK_Scalar1) {
218 if (matrix.rectStaysRect() && SkScalarIsInt(matrix.getTranslateX()) &&
219 SkScalarIsInt(matrix.getTranslateY())) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400220 *filterMode = GrSamplerState::Filter::kNearest;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000221 } else {
222 // Use bilerp to handle rotation or fractional translation.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400223 *filterMode = GrSamplerState::Filter::kBilerp;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000224 }
225 return false;
226 }
227 // When we use the bicubic filtering effect each sample is read from the texture using
228 // nearest neighbor sampling.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400229 *filterMode = GrSamplerState::Filter::kNearest;
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000230 return true;
231}