blob: 2b819f68424ce9fc9a8f6f71a7083e96d2dc3ea6 [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"
9
bsalomon848faf02014-07-11 10:01:02 -070010#include "gl/GrGLShaderBuilder.h"
11
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
22class GrGLBicubicEffect : public GrGLEffect {
23public:
24 GrGLBicubicEffect(const GrBackendEffectFactory& factory,
25 const GrDrawEffect&);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000026
humper@google.com3aad3b02013-09-04 19:23:53 +000027 virtual void emitCode(GrGLShaderBuilder*,
28 const GrDrawEffect&,
bsalomon63e99f72014-07-21 08:03:14 -070029 const GrEffectKey&,
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
humper@google.com3aad3b02013-09-04 19:23:53 +000035 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
36
bsalomon63e99f72014-07-21 08:03:14 -070037 static inline void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
38 GrEffectKeyBuilder* b) {
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000039 const GrTextureDomain& domain = drawEffect.castEffect<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:
44 typedef GrGLUniformManager::UniformHandle UniformHandle;
45
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
humper@google.com3aad3b02013-09-04 19:23:53 +000050 typedef GrGLEffect INHERITED;
51};
52
bsalomon@google.com77af6802013-10-02 13:04:56 +000053GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
54 : INHERITED(factory) {
humper@google.com3aad3b02013-09-04 19:23:53 +000055}
56
57void GrGLBicubicEffect::emitCode(GrGLShaderBuilder* builder,
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000058 const GrDrawEffect& drawEffect,
bsalomon63e99f72014-07-21 08:03:14 -070059 const GrEffectKey& 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) {
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000064 const GrTextureDomain& domain = drawEffect.castEffect<GrBicubicEffect>().domain();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000065
bsalomon@google.com77af6802013-10-02 13:04:56 +000066 SkString coords2D = builder->ensureFSCoords2D(coords, 0);
humper@google.com3aad3b02013-09-04 19:23:53 +000067 fCoefficientsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
68 kMat44f_GrSLType, "Coefficients");
69 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
70 kVec2f_GrSLType, "ImageIncrement");
71
72 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
73 const char* coeff = builder->getUniformCStr(fCoefficientsUni);
74
75 SkString cubicBlendName;
76
77 static const GrGLShaderVar gCubicBlendArgs[] = {
78 GrGLShaderVar("coefficients", kMat44f_GrSLType),
79 GrGLShaderVar("t", kFloat_GrSLType),
80 GrGLShaderVar("c0", kVec4f_GrSLType),
81 GrGLShaderVar("c1", kVec4f_GrSLType),
82 GrGLShaderVar("c2", kVec4f_GrSLType),
83 GrGLShaderVar("c3", kVec4f_GrSLType),
84 };
85 builder->fsEmitFunction(kVec4f_GrSLType,
86 "cubicBlend",
87 SK_ARRAY_COUNT(gCubicBlendArgs),
88 gCubicBlendArgs,
89 "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n"
90 "\tvec4 c = coefficients * ts;\n"
91 "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;\n",
92 &cubicBlendName);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000093 builder->fsCodeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_str(), imgInc);
94 // We unnormalize the coord in order to determine our fractional offset (f) within the texel
95 // We then snap coord to a texel center and renormalize. The snap prevents cases where the
96 // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/
97 // double hit a texel.
98 builder->fsCodeAppendf("\tcoord /= %s;\n", imgInc);
99 builder->fsCodeAppend("\tvec2 f = fract(coord);\n");
100 builder->fsCodeAppendf("\tcoord = (coord - f + vec2(0.5)) * %s;\n", imgInc);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000101 builder->fsCodeAppend("\tvec4 rowColors[4];\n");
humper@google.com3aad3b02013-09-04 19:23:53 +0000102 for (int y = 0; y < 4; ++y) {
103 for (int x = 0; x < 4; ++x) {
104 SkString coord;
105 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000106 SkString sampleVar;
107 sampleVar.printf("rowColors[%d]", x);
108 fDomain.sampleTexture(builder, domain, sampleVar.c_str(), coord, samplers[0]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000109 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000110 builder->fsCodeAppendf("\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 +0000111 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000112 SkString bicubicColor;
113 bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), coeff);
114 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, (GrGLSLExpr4(bicubicColor.c_str()) * GrGLSLExpr4(inputColor)).c_str());
humper@google.com3aad3b02013-09-04 19:23:53 +0000115}
116
humper@google.com3aad3b02013-09-04 19:23:53 +0000117void GrGLBicubicEffect::setData(const GrGLUniformManager& uman,
118 const GrDrawEffect& drawEffect) {
119 const GrBicubicEffect& effect = drawEffect.castEffect<GrBicubicEffect>();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000120 const GrTexture& texture = *effect.texture(0);
humper@google.com3aad3b02013-09-04 19:23:53 +0000121 float imageIncrement[2];
122 imageIncrement[0] = 1.0f / texture.width();
123 imageIncrement[1] = 1.0f / texture.height();
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000124 uman.set2fv(fImageIncrementUni, 1, imageIncrement);
humper@google.com3aad3b02013-09-04 19:23:53 +0000125 uman.setMatrix4f(fCoefficientsUni, effect.coefficients());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000126 fDomain.setData(uman, effect.domain(), texture.origin());
127}
128
129static inline void convert_row_major_scalar_coeffs_to_column_major_floats(float dst[16],
130 const SkScalar src[16]) {
131 for (int y = 0; y < 4; y++) {
132 for (int x = 0; x < 4; x++) {
133 dst[x * 4 + y] = SkScalarToFloat(src[y * 4 + x]);
134 }
135 }
humper@google.com3aad3b02013-09-04 19:23:53 +0000136}
137
138GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
humper@google.com3aad3b02013-09-04 19:23:53 +0000139 const SkScalar coefficients[16],
140 const SkMatrix &matrix,
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +0000141 const SkShader::TileMode tileModes[2])
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000142 : INHERITED(texture, matrix, GrTextureParams(tileModes, GrTextureParams::kNone_FilterMode))
143 , fDomain(GrTextureDomain::IgnoredDomain()) {
144 convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients);
145}
146
147GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
148 const SkScalar coefficients[16],
149 const SkMatrix &matrix,
150 const SkRect& domain)
151 : INHERITED(texture, matrix, GrTextureParams(SkShader::kClamp_TileMode,
152 GrTextureParams::kNone_FilterMode))
153 , fDomain(domain, GrTextureDomain::kClamp_Mode) {
154 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
160const GrBackendEffectFactory& GrBicubicEffect::getFactory() const {
161 return GrTBackendEffectFactory<GrBicubicEffect>::getInstance();
162}
163
164bool GrBicubicEffect::onIsEqual(const GrEffect& sBase) const {
165 const GrBicubicEffect& s = CastEffect<GrBicubicEffect>(sBase);
humper@google.comd1af2372013-09-04 20:32:58 +0000166 return this->textureAccess(0) == s.textureAccess(0) &&
humper@google.com3aad3b02013-09-04 19:23:53 +0000167 !memcmp(fCoefficients, s.coefficients(), 16);
168}
169
170void GrBicubicEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000171 // FIXME: Perhaps we can do better.
humper@google.com3aad3b02013-09-04 19:23:53 +0000172 *validFlags = 0;
173 return;
174}
175
176GR_DEFINE_EFFECT_TEST(GrBicubicEffect);
177
bsalomon83d081a2014-07-08 09:56:10 -0700178GrEffect* GrBicubicEffect::TestCreate(SkRandom* random,
179 GrContext* context,
180 const GrDrawTargetCaps&,
181 GrTexture* textures[]) {
humper@google.com3aad3b02013-09-04 19:23:53 +0000182 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
183 GrEffectUnitTest::kAlphaTextureIdx;
184 SkScalar coefficients[16];
185 for (int i = 0; i < 16; i++) {
186 coefficients[i] = random->nextSScalar1();
187 }
188 return GrBicubicEffect::Create(textures[texIdx], coefficients);
189}
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000190
191//////////////////////////////////////////////////////////////////////////////
192
193bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix,
194 GrTextureParams::FilterMode* filterMode) {
195 if (matrix.isIdentity()) {
196 *filterMode = GrTextureParams::kNone_FilterMode;
197 return false;
198 }
199
200 SkScalar scales[2];
201 if (!matrix.getMinMaxScales(scales) || scales[0] < SK_Scalar1) {
202 // Bicubic doesn't handle arbitrary minimization well, as src texels can be skipped
203 // entirely,
204 *filterMode = GrTextureParams::kMipMap_FilterMode;
205 return false;
206 }
207 // At this point if scales[1] == SK_Scalar1 then the matrix doesn't do any scaling.
208 if (scales[1] == SK_Scalar1) {
209 if (matrix.rectStaysRect() && SkScalarIsInt(matrix.getTranslateX()) &&
210 SkScalarIsInt(matrix.getTranslateY())) {
211 *filterMode = GrTextureParams::kNone_FilterMode;
212 } else {
213 // Use bilerp to handle rotation or fractional translation.
214 *filterMode = GrTextureParams::kBilerp_FilterMode;
215 }
216 return false;
217 }
218 // When we use the bicubic filtering effect each sample is read from the texture using
219 // nearest neighbor sampling.
220 *filterMode = GrTextureParams::kNone_FilterMode;
221 return true;
222}