blob: 7a47a4f08264fc34b6d193a5b7010272ca5cc04f [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
joshualitt30ba4362014-08-21 20:18:45 -07008#include "gl/builders/GrGLProgramBuilder.h"
humper@google.com3aad3b02013-09-04 19:23:53 +00009#include "GrBicubicEffect.h"
10
bsalomon848faf02014-07-11 10:01:02 -070011
humper@google.com3aad3b02013-09-04 19:23:53 +000012#define DS(x) SkDoubleToScalar(x)
13
14const SkScalar GrBicubicEffect::gMitchellCoefficients[16] = {
15 DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0),
16 DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0),
17 DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0),
18 DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0),
19};
20
21
joshualittb0a8a372014-09-23 09:50:21 -070022class GrGLBicubicEffect : public GrGLFragmentProcessor {
humper@google.com3aad3b02013-09-04 19:23:53 +000023public:
joshualittb0a8a372014-09-23 09:50:21 -070024 GrGLBicubicEffect(const GrBackendProcessorFactory& factory,
25 const GrProcessor&);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000026
joshualitt15988992014-10-09 15:04:05 -070027 virtual void emitCode(GrGLFPBuilder*,
joshualittb0a8a372014-09-23 09:50:21 -070028 const GrFragmentProcessor&,
29 const GrProcessorKey&,
humper@google.com3aad3b02013-09-04 19:23:53 +000030 const char* outputColor,
31 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000032 const TransformedCoordsArray&,
humper@google.com3aad3b02013-09-04 19:23:53 +000033 const TextureSamplerArray&) SK_OVERRIDE;
34
joshualittb0a8a372014-09-23 09:50:21 -070035 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
humper@google.com3aad3b02013-09-04 19:23:53 +000036
joshualittb0a8a372014-09-23 09:50:21 -070037 static inline void GenKey(const GrProcessor& effect, const GrGLCaps&,
38 GrProcessorKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -070039 const GrTextureDomain& domain = effect.cast<GrBicubicEffect>().domain();
bsalomon63e99f72014-07-21 08:03:14 -070040 b->add32(GrTextureDomain::GLDomain::DomainKey(domain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000041 }
42
humper@google.com3aad3b02013-09-04 19:23:53 +000043private:
kkinnunen7510b222014-07-30 00:04:16 -070044 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
humper@google.com3aad3b02013-09-04 19:23:53 +000045
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000046 UniformHandle fCoefficientsUni;
47 UniformHandle fImageIncrementUni;
48 GrTextureDomain::GLDomain fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +000049
joshualittb0a8a372014-09-23 09:50:21 -070050 typedef GrGLFragmentProcessor INHERITED;
humper@google.com3aad3b02013-09-04 19:23:53 +000051};
52
joshualittb0a8a372014-09-23 09:50:21 -070053GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendProcessorFactory& factory, const GrProcessor&)
bsalomon@google.com77af6802013-10-02 13:04:56 +000054 : INHERITED(factory) {
humper@google.com3aad3b02013-09-04 19:23:53 +000055}
56
joshualitt15988992014-10-09 15:04:05 -070057void GrGLBicubicEffect::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -070058 const GrFragmentProcessor& effect,
59 const GrProcessorKey& key,
humper@google.com3aad3b02013-09-04 19:23:53 +000060 const char* outputColor,
61 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000062 const TransformedCoordsArray& coords,
humper@google.com3aad3b02013-09-04 19:23:53 +000063 const TextureSamplerArray& samplers) {
joshualitt49586be2014-09-16 08:21:41 -070064 const GrTextureDomain& domain = effect.cast<GrBicubicEffect>().domain();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000065
joshualitt30ba4362014-08-21 20:18:45 -070066 fCoefficientsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
humper@google.com3aad3b02013-09-04 19:23:53 +000067 kMat44f_GrSLType, "Coefficients");
joshualitt30ba4362014-08-21 20:18:45 -070068 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
humper@google.com3aad3b02013-09-04 19:23:53 +000069 kVec2f_GrSLType, "ImageIncrement");
70
71 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
72 const char* coeff = builder->getUniformCStr(fCoefficientsUni);
73
74 SkString cubicBlendName;
75
76 static const GrGLShaderVar gCubicBlendArgs[] = {
77 GrGLShaderVar("coefficients", kMat44f_GrSLType),
78 GrGLShaderVar("t", kFloat_GrSLType),
79 GrGLShaderVar("c0", kVec4f_GrSLType),
80 GrGLShaderVar("c1", kVec4f_GrSLType),
81 GrGLShaderVar("c2", kVec4f_GrSLType),
82 GrGLShaderVar("c3", kVec4f_GrSLType),
83 };
joshualitt15988992014-10-09 15:04:05 -070084 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -070085 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0);
86 fsBuilder->emitFunction(kVec4f_GrSLType,
humper@google.com3aad3b02013-09-04 19:23:53 +000087 "cubicBlend",
88 SK_ARRAY_COUNT(gCubicBlendArgs),
89 gCubicBlendArgs,
90 "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n"
91 "\tvec4 c = coefficients * ts;\n"
92 "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;\n",
93 &cubicBlendName);
joshualitt30ba4362014-08-21 20:18:45 -070094 fsBuilder->codeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_str(), imgInc);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000095 // We unnormalize the coord in order to determine our fractional offset (f) within the texel
96 // We then snap coord to a texel center and renormalize. The snap prevents cases where the
97 // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/
98 // double hit a texel.
joshualitt30ba4362014-08-21 20:18:45 -070099 fsBuilder->codeAppendf("\tcoord /= %s;\n", imgInc);
100 fsBuilder->codeAppend("\tvec2 f = fract(coord);\n");
101 fsBuilder->codeAppendf("\tcoord = (coord - f + vec2(0.5)) * %s;\n", imgInc);
102 fsBuilder->codeAppend("\tvec4 rowColors[4];\n");
humper@google.com3aad3b02013-09-04 19:23:53 +0000103 for (int y = 0; y < 4; ++y) {
104 for (int x = 0; x < 4; ++x) {
105 SkString coord;
106 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000107 SkString sampleVar;
108 sampleVar.printf("rowColors[%d]", x);
joshualitt30ba4362014-08-21 20:18:45 -0700109 fDomain.sampleTexture(fsBuilder, domain, sampleVar.c_str(), coord, samplers[0]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000110 }
joshualitt30ba4362014-08-21 20:18:45 -0700111 fsBuilder->codeAppendf("\tvec4 s%d = %s(%s, f.x, rowColors[0], rowColors[1], rowColors[2], rowColors[3]);\n", y, cubicBlendName.c_str(), coeff);
humper@google.com3aad3b02013-09-04 19:23:53 +0000112 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000113 SkString bicubicColor;
114 bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), coeff);
joshualitt30ba4362014-08-21 20:18:45 -0700115 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor, (GrGLSLExpr4(bicubicColor.c_str()) * GrGLSLExpr4(inputColor)).c_str());
humper@google.com3aad3b02013-09-04 19:23:53 +0000116}
117
kkinnunen7510b222014-07-30 00:04:16 -0700118void GrGLBicubicEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700119 const GrProcessor& processor) {
120 const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>();
121 const GrTexture& texture = *processor.texture(0);
humper@google.com3aad3b02013-09-04 19:23:53 +0000122 float imageIncrement[2];
123 imageIncrement[0] = 1.0f / texture.width();
124 imageIncrement[1] = 1.0f / texture.height();
kkinnunen7510b222014-07-30 00:04:16 -0700125 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
joshualitt49586be2014-09-16 08:21:41 -0700126 pdman.setMatrix4f(fCoefficientsUni, bicubicEffect.coefficients());
127 fDomain.setData(pdman, bicubicEffect.domain(), texture.origin());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000128}
129
130static inline void convert_row_major_scalar_coeffs_to_column_major_floats(float dst[16],
131 const SkScalar src[16]) {
132 for (int y = 0; y < 4; y++) {
133 for (int x = 0; x < 4; x++) {
134 dst[x * 4 + y] = SkScalarToFloat(src[y * 4 + x]);
135 }
136 }
humper@google.com3aad3b02013-09-04 19:23:53 +0000137}
138
139GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
humper@google.com3aad3b02013-09-04 19:23:53 +0000140 const SkScalar coefficients[16],
141 const SkMatrix &matrix,
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +0000142 const SkShader::TileMode tileModes[2])
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000143 : INHERITED(texture, matrix, GrTextureParams(tileModes, GrTextureParams::kNone_FilterMode))
144 , fDomain(GrTextureDomain::IgnoredDomain()) {
145 convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients);
146}
147
148GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
149 const SkScalar coefficients[16],
150 const SkMatrix &matrix,
151 const SkRect& domain)
152 : INHERITED(texture, matrix, GrTextureParams(SkShader::kClamp_TileMode,
153 GrTextureParams::kNone_FilterMode))
154 , fDomain(domain, GrTextureDomain::kClamp_Mode) {
155 convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients);
humper@google.com3aad3b02013-09-04 19:23:53 +0000156}
157
158GrBicubicEffect::~GrBicubicEffect() {
159}
160
joshualittb0a8a372014-09-23 09:50:21 -0700161const GrBackendFragmentProcessorFactory& GrBicubicEffect::getFactory() const {
162 return GrTBackendFragmentProcessorFactory<GrBicubicEffect>::getInstance();
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>();
humper@google.comd1af2372013-09-04 20:32:58 +0000167 return this->textureAccess(0) == s.textureAccess(0) &&
bsalomon838f62d2014-08-05 07:15:57 -0700168 !memcmp(fCoefficients, s.coefficients(), 16) &&
169 fDomain == s.fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +0000170}
171
egdaniel1a8ecdf2014-10-03 06:24:12 -0700172void GrBicubicEffect::onComputeInvariantOutput(InvariantOutput* inout) const {
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000173 // FIXME: Perhaps we can do better.
egdanielccb2e382014-10-13 12:53:46 -0700174 inout->mulByUnknownAlpha();
humper@google.com3aad3b02013-09-04 19:23:53 +0000175}
176
joshualittb0a8a372014-09-23 09:50:21 -0700177GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect);
humper@google.com3aad3b02013-09-04 19:23:53 +0000178
joshualittb0a8a372014-09-23 09:50:21 -0700179GrFragmentProcessor* GrBicubicEffect::TestCreate(SkRandom* random,
180 GrContext* context,
181 const GrDrawTargetCaps&,
182 GrTexture* textures[]) {
183 int texIdx = random->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++) {
187 coefficients[i] = random->nextSScalar1();
188 }
189 return GrBicubicEffect::Create(textures[texIdx], coefficients);
190}
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}