blob: cc38d626a9fbb6819bbf9d0885c8ea4b901c9030 [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"
egdaniel2d721d32015-11-11 13:06:05 -080010#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070011#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080012#include "glsl/GrGLSLUniformHandler.h"
bsalomon848faf02014-07-11 10:01:02 -070013
humper@google.com3aad3b02013-09-04 19:23:53 +000014#define DS(x) SkDoubleToScalar(x)
15
16const SkScalar GrBicubicEffect::gMitchellCoefficients[16] = {
17 DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0),
18 DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0),
19 DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0),
20 DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0),
21};
22
23
egdaniel64c47282015-11-13 06:54:19 -080024class GrGLBicubicEffect : public GrGLSLFragmentProcessor {
humper@google.com3aad3b02013-09-04 19:23:53 +000025public:
robertphillips9cdb9922016-02-03 12:25:40 -080026 void emitCode(EmitArgs&) override;
humper@google.com3aad3b02013-09-04 19:23:53 +000027
jvanverthcfc18862015-04-28 08:48:20 -070028 static inline void GenKey(const GrProcessor& effect, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -070029 GrProcessorKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -070030 const GrTextureDomain& domain = effect.cast<GrBicubicEffect>().domain();
bsalomon63e99f72014-07-21 08:03:14 -070031 b->add32(GrTextureDomain::GLDomain::DomainKey(domain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000032 }
33
wangyixb1daa862015-08-18 11:29:31 -070034protected:
egdaniel018fb622015-10-28 07:26:40 -070035 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -070036
humper@google.com3aad3b02013-09-04 19:23:53 +000037private:
egdaniel018fb622015-10-28 07:26:40 -070038 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
humper@google.com3aad3b02013-09-04 19:23:53 +000039
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000040 UniformHandle fCoefficientsUni;
41 UniformHandle fImageIncrementUni;
42 GrTextureDomain::GLDomain fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +000043
egdaniel64c47282015-11-13 06:54:19 -080044 typedef GrGLSLFragmentProcessor INHERITED;
humper@google.com3aad3b02013-09-04 19:23:53 +000045};
46
wangyix7c157a92015-07-22 15:08:53 -070047void GrGLBicubicEffect::emitCode(EmitArgs& args) {
48 const GrTextureDomain& domain = args.fFp.cast<GrBicubicEffect>().domain();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000049
egdaniel7ea439b2015-12-03 09:20:44 -080050 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
51 fCoefficientsUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility,
52 kMat44f_GrSLType, kDefault_GrSLPrecision,
53 "Coefficients");
54 fImageIncrementUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility,
55 kVec2f_GrSLType, kDefault_GrSLPrecision,
56 "ImageIncrement");
humper@google.com3aad3b02013-09-04 19:23:53 +000057
egdaniel7ea439b2015-12-03 09:20:44 -080058 const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni);
59 const char* coeff = uniformHandler->getUniformCStr(fCoefficientsUni);
humper@google.com3aad3b02013-09-04 19:23:53 +000060
61 SkString cubicBlendName;
62
egdaniel0d3f0612015-10-21 10:45:48 -070063 static const GrGLSLShaderVar gCubicBlendArgs[] = {
64 GrGLSLShaderVar("coefficients", kMat44f_GrSLType),
65 GrGLSLShaderVar("t", kFloat_GrSLType),
66 GrGLSLShaderVar("c0", kVec4f_GrSLType),
67 GrGLSLShaderVar("c1", kVec4f_GrSLType),
68 GrGLSLShaderVar("c2", kVec4f_GrSLType),
69 GrGLSLShaderVar("c3", kVec4f_GrSLType),
humper@google.com3aad3b02013-09-04 19:23:53 +000070 };
egdaniel4ca2e602015-11-18 08:01:26 -080071 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
72 SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0);
73 fragBuilder->emitFunction(kVec4f_GrSLType,
74 "cubicBlend",
75 SK_ARRAY_COUNT(gCubicBlendArgs),
76 gCubicBlendArgs,
77 "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n"
78 "\tvec4 c = coefficients * ts;\n"
79 "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;\n",
80 &cubicBlendName);
81 fragBuilder->codeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_str(), imgInc);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000082 // We unnormalize the coord in order to determine our fractional offset (f) within the texel
83 // We then snap coord to a texel center and renormalize. The snap prevents cases where the
84 // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/
85 // double hit a texel.
egdaniel4ca2e602015-11-18 08:01:26 -080086 fragBuilder->codeAppendf("\tcoord /= %s;\n", imgInc);
87 fragBuilder->codeAppend("\tvec2 f = fract(coord);\n");
88 fragBuilder->codeAppendf("\tcoord = (coord - f + vec2(0.5)) * %s;\n", imgInc);
89 fragBuilder->codeAppend("\tvec4 rowColors[4];\n");
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,
egdaniela2e3e0f2015-11-19 07:23:45 -080098 args.fGLSLCaps,
99 domain,
100 sampleVar.c_str(),
101 coord,
102 args.fSamplers[0]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000103 }
egdaniel4ca2e602015-11-18 08:01:26 -0800104 fragBuilder->codeAppendf(
105 "\tvec4 s%d = %s(%s, f.x, rowColors[0], rowColors[1], rowColors[2], rowColors[3]);\n",
106 y, cubicBlendName.c_str(), coeff);
humper@google.com3aad3b02013-09-04 19:23:53 +0000107 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000108 SkString bicubicColor;
109 bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), coeff);
egdaniel4ca2e602015-11-18 08:01:26 -0800110 fragBuilder->codeAppendf("\t%s = %s;\n",
111 args.fOutputColor, (GrGLSLExpr4(bicubicColor.c_str()) *
112 GrGLSLExpr4(args.fInputColor)).c_str());
humper@google.com3aad3b02013-09-04 19:23:53 +0000113}
114
egdaniel018fb622015-10-28 07:26:40 -0700115void GrGLBicubicEffect::onSetData(const GrGLSLProgramDataManager& pdman,
116 const GrProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -0700117 const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>();
118 const GrTexture& texture = *processor.texture(0);
humper@google.com3aad3b02013-09-04 19:23:53 +0000119 float imageIncrement[2];
120 imageIncrement[0] = 1.0f / texture.width();
121 imageIncrement[1] = 1.0f / texture.height();
kkinnunen7510b222014-07-30 00:04:16 -0700122 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
joshualitt49586be2014-09-16 08:21:41 -0700123 pdman.setMatrix4f(fCoefficientsUni, bicubicEffect.coefficients());
124 fDomain.setData(pdman, bicubicEffect.domain(), texture.origin());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000125}
126
127static inline void convert_row_major_scalar_coeffs_to_column_major_floats(float dst[16],
128 const SkScalar src[16]) {
129 for (int y = 0; y < 4; y++) {
130 for (int x = 0; x < 4; x++) {
131 dst[x * 4 + y] = SkScalarToFloat(src[y * 4 + x]);
132 }
133 }
humper@google.com3aad3b02013-09-04 19:23:53 +0000134}
135
bsalomon4a339522015-10-06 08:40:50 -0700136GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
humper@google.com3aad3b02013-09-04 19:23:53 +0000137 const SkScalar coefficients[16],
138 const SkMatrix &matrix,
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +0000139 const SkShader::TileMode tileModes[2])
bsalomon4a339522015-10-06 08:40:50 -0700140 : INHERITED(texture, matrix, GrTextureParams(tileModes, GrTextureParams::kNone_FilterMode))
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000141 , fDomain(GrTextureDomain::IgnoredDomain()) {
joshualitteb2a6762014-12-04 11:35:33 -0800142 this->initClassID<GrBicubicEffect>();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000143 convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients);
144}
145
bsalomon4a339522015-10-06 08:40:50 -0700146GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000147 const SkScalar coefficients[16],
148 const SkMatrix &matrix,
149 const SkRect& domain)
bsalomon4a339522015-10-06 08:40:50 -0700150 : INHERITED(texture, matrix,
joshualitt5f10b5c2015-07-09 10:24:35 -0700151 GrTextureParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode))
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000152 , fDomain(domain, GrTextureDomain::kClamp_Mode) {
joshualitteb2a6762014-12-04 11:35:33 -0800153 this->initClassID<GrBicubicEffect>();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000154 convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients);
humper@google.com3aad3b02013-09-04 19:23:53 +0000155}
156
157GrBicubicEffect::~GrBicubicEffect() {
158}
159
egdaniel57d3b032015-11-13 11:57:27 -0800160void GrBicubicEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
161 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800162 GrGLBicubicEffect::GenKey(*this, caps, b);
163}
164
egdaniel57d3b032015-11-13 11:57:27 -0800165GrGLSLFragmentProcessor* GrBicubicEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800166 return new GrGLBicubicEffect;
humper@google.com3aad3b02013-09-04 19:23:53 +0000167}
168
bsalomon0e08fc12014-10-15 08:19:04 -0700169bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700170 const GrBicubicEffect& s = sBase.cast<GrBicubicEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700171 return !memcmp(fCoefficients, s.coefficients(), 16) &&
bsalomon838f62d2014-08-05 07:15:57 -0700172 fDomain == s.fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +0000173}
174
egdaniel605dd0f2014-11-12 08:35:25 -0800175void GrBicubicEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000176 // FIXME: Perhaps we can do better.
joshualitt56995b52014-12-11 15:44:02 -0800177 inout->mulByUnknownSingleComponent();
humper@google.com3aad3b02013-09-04 19:23:53 +0000178}
179
joshualittb0a8a372014-09-23 09:50:21 -0700180GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect);
humper@google.com3aad3b02013-09-04 19:23:53 +0000181
bsalomonc21b09e2015-08-28 18:46:56 -0700182const GrFragmentProcessor* GrBicubicEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700183 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
184 GrProcessorUnitTest::kAlphaTextureIdx;
humper@google.com3aad3b02013-09-04 19:23:53 +0000185 SkScalar coefficients[16];
186 for (int i = 0; i < 16; i++) {
joshualitt0067ff52015-07-08 14:26:19 -0700187 coefficients[i] = d->fRandom->nextSScalar1();
humper@google.com3aad3b02013-09-04 19:23:53 +0000188 }
bsalomon4a339522015-10-06 08:40:50 -0700189 return GrBicubicEffect::Create(d->fTextures[texIdx], coefficients);
humper@google.com3aad3b02013-09-04 19:23:53 +0000190}
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000191
192//////////////////////////////////////////////////////////////////////////////
193
194bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix,
195 GrTextureParams::FilterMode* filterMode) {
196 if (matrix.isIdentity()) {
197 *filterMode = GrTextureParams::kNone_FilterMode;
198 return false;
199 }
200
201 SkScalar scales[2];
202 if (!matrix.getMinMaxScales(scales) || scales[0] < SK_Scalar1) {
203 // Bicubic doesn't handle arbitrary minimization well, as src texels can be skipped
204 // entirely,
205 *filterMode = GrTextureParams::kMipMap_FilterMode;
206 return false;
207 }
208 // At this point if scales[1] == SK_Scalar1 then the matrix doesn't do any scaling.
209 if (scales[1] == SK_Scalar1) {
210 if (matrix.rectStaysRect() && SkScalarIsInt(matrix.getTranslateX()) &&
211 SkScalarIsInt(matrix.getTranslateY())) {
212 *filterMode = GrTextureParams::kNone_FilterMode;
213 } else {
214 // Use bilerp to handle rotation or fractional translation.
215 *filterMode = GrTextureParams::kBilerp_FilterMode;
216 }
217 return false;
218 }
219 // When we use the bicubic filtering effect each sample is read from the texture using
220 // nearest neighbor sampling.
221 *filterMode = GrTextureParams::kNone_FilterMode;
222 return true;
223}