blob: 804a087b16e547ed83237c54c453cea4e0b43582 [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"
joshualitteb2a6762014-12-04 11:35:33 -080010#include "gl/builders/GrGLProgramBuilder.h"
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:
joshualitteb2a6762014-12-04 11:35:33 -080024 GrGLBicubicEffect(const GrProcessor&);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000025
joshualitt15988992014-10-09 15:04:05 -070026 virtual void emitCode(GrGLFPBuilder*,
joshualittb0a8a372014-09-23 09:50:21 -070027 const GrFragmentProcessor&,
humper@google.com3aad3b02013-09-04 19:23:53 +000028 const char* outputColor,
29 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000030 const TransformedCoordsArray&,
humper@google.com3aad3b02013-09-04 19:23:53 +000031 const TextureSamplerArray&) SK_OVERRIDE;
32
joshualittb0a8a372014-09-23 09:50:21 -070033 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
humper@google.com3aad3b02013-09-04 19:23:53 +000034
joshualittb0a8a372014-09-23 09:50:21 -070035 static inline void GenKey(const GrProcessor& effect, const GrGLCaps&,
36 GrProcessorKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -070037 const GrTextureDomain& domain = effect.cast<GrBicubicEffect>().domain();
bsalomon63e99f72014-07-21 08:03:14 -070038 b->add32(GrTextureDomain::GLDomain::DomainKey(domain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000039 }
40
humper@google.com3aad3b02013-09-04 19:23:53 +000041private:
kkinnunen7510b222014-07-30 00:04:16 -070042 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
humper@google.com3aad3b02013-09-04 19:23:53 +000043
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000044 UniformHandle fCoefficientsUni;
45 UniformHandle fImageIncrementUni;
46 GrTextureDomain::GLDomain fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +000047
joshualittb0a8a372014-09-23 09:50:21 -070048 typedef GrGLFragmentProcessor INHERITED;
humper@google.com3aad3b02013-09-04 19:23:53 +000049};
50
joshualitteb2a6762014-12-04 11:35:33 -080051GrGLBicubicEffect::GrGLBicubicEffect(const GrProcessor&) {
humper@google.com3aad3b02013-09-04 19:23:53 +000052}
53
joshualitt15988992014-10-09 15:04:05 -070054void GrGLBicubicEffect::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -070055 const GrFragmentProcessor& effect,
humper@google.com3aad3b02013-09-04 19:23:53 +000056 const char* outputColor,
57 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000058 const TransformedCoordsArray& coords,
humper@google.com3aad3b02013-09-04 19:23:53 +000059 const TextureSamplerArray& samplers) {
joshualitt49586be2014-09-16 08:21:41 -070060 const GrTextureDomain& domain = effect.cast<GrBicubicEffect>().domain();
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000061
joshualitt30ba4362014-08-21 20:18:45 -070062 fCoefficientsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
humper@google.com3aad3b02013-09-04 19:23:53 +000063 kMat44f_GrSLType, "Coefficients");
joshualitt30ba4362014-08-21 20:18:45 -070064 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
humper@google.com3aad3b02013-09-04 19:23:53 +000065 kVec2f_GrSLType, "ImageIncrement");
66
67 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
68 const char* coeff = builder->getUniformCStr(fCoefficientsUni);
69
70 SkString cubicBlendName;
71
72 static const GrGLShaderVar gCubicBlendArgs[] = {
73 GrGLShaderVar("coefficients", kMat44f_GrSLType),
74 GrGLShaderVar("t", kFloat_GrSLType),
75 GrGLShaderVar("c0", kVec4f_GrSLType),
76 GrGLShaderVar("c1", kVec4f_GrSLType),
77 GrGLShaderVar("c2", kVec4f_GrSLType),
78 GrGLShaderVar("c3", kVec4f_GrSLType),
79 };
joshualitt15988992014-10-09 15:04:05 -070080 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -070081 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0);
82 fsBuilder->emitFunction(kVec4f_GrSLType,
humper@google.com3aad3b02013-09-04 19:23:53 +000083 "cubicBlend",
84 SK_ARRAY_COUNT(gCubicBlendArgs),
85 gCubicBlendArgs,
86 "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n"
87 "\tvec4 c = coefficients * ts;\n"
88 "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;\n",
89 &cubicBlendName);
joshualitt30ba4362014-08-21 20:18:45 -070090 fsBuilder->codeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_str(), imgInc);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000091 // We unnormalize the coord in order to determine our fractional offset (f) within the texel
92 // We then snap coord to a texel center and renormalize. The snap prevents cases where the
93 // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/
94 // double hit a texel.
joshualitt30ba4362014-08-21 20:18:45 -070095 fsBuilder->codeAppendf("\tcoord /= %s;\n", imgInc);
96 fsBuilder->codeAppend("\tvec2 f = fract(coord);\n");
97 fsBuilder->codeAppendf("\tcoord = (coord - f + vec2(0.5)) * %s;\n", imgInc);
98 fsBuilder->codeAppend("\tvec4 rowColors[4];\n");
humper@google.com3aad3b02013-09-04 19:23:53 +000099 for (int y = 0; y < 4; ++y) {
100 for (int x = 0; x < 4; ++x) {
101 SkString coord;
102 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000103 SkString sampleVar;
104 sampleVar.printf("rowColors[%d]", x);
joshualitt30ba4362014-08-21 20:18:45 -0700105 fDomain.sampleTexture(fsBuilder, domain, sampleVar.c_str(), coord, samplers[0]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000106 }
joshualitt30ba4362014-08-21 20:18:45 -0700107 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 +0000108 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000109 SkString bicubicColor;
110 bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), coeff);
joshualitt30ba4362014-08-21 20:18:45 -0700111 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor, (GrGLSLExpr4(bicubicColor.c_str()) * GrGLSLExpr4(inputColor)).c_str());
humper@google.com3aad3b02013-09-04 19:23:53 +0000112}
113
kkinnunen7510b222014-07-30 00:04:16 -0700114void GrGLBicubicEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700115 const GrProcessor& processor) {
116 const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>();
117 const GrTexture& texture = *processor.texture(0);
humper@google.com3aad3b02013-09-04 19:23:53 +0000118 float imageIncrement[2];
119 imageIncrement[0] = 1.0f / texture.width();
120 imageIncrement[1] = 1.0f / texture.height();
kkinnunen7510b222014-07-30 00:04:16 -0700121 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
joshualitt49586be2014-09-16 08:21:41 -0700122 pdman.setMatrix4f(fCoefficientsUni, bicubicEffect.coefficients());
123 fDomain.setData(pdman, bicubicEffect.domain(), texture.origin());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000124}
125
126static inline void convert_row_major_scalar_coeffs_to_column_major_floats(float dst[16],
127 const SkScalar src[16]) {
128 for (int y = 0; y < 4; y++) {
129 for (int x = 0; x < 4; x++) {
130 dst[x * 4 + y] = SkScalarToFloat(src[y * 4 + x]);
131 }
132 }
humper@google.com3aad3b02013-09-04 19:23:53 +0000133}
134
135GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
humper@google.com3aad3b02013-09-04 19:23:53 +0000136 const SkScalar coefficients[16],
137 const SkMatrix &matrix,
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +0000138 const SkShader::TileMode tileModes[2])
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000139 : INHERITED(texture, matrix, GrTextureParams(tileModes, GrTextureParams::kNone_FilterMode))
140 , fDomain(GrTextureDomain::IgnoredDomain()) {
joshualitteb2a6762014-12-04 11:35:33 -0800141 this->initClassID<GrBicubicEffect>();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000142 convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients);
143}
144
145GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
146 const SkScalar coefficients[16],
147 const SkMatrix &matrix,
148 const SkRect& domain)
149 : INHERITED(texture, matrix, GrTextureParams(SkShader::kClamp_TileMode,
150 GrTextureParams::kNone_FilterMode))
151 , fDomain(domain, GrTextureDomain::kClamp_Mode) {
joshualitteb2a6762014-12-04 11:35:33 -0800152 this->initClassID<GrBicubicEffect>();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000153 convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients);
humper@google.com3aad3b02013-09-04 19:23:53 +0000154}
155
156GrBicubicEffect::~GrBicubicEffect() {
157}
158
joshualitteb2a6762014-12-04 11:35:33 -0800159void GrBicubicEffect::getGLProcessorKey(const GrGLCaps& caps,
160 GrProcessorKeyBuilder* b) const {
161 GrGLBicubicEffect::GenKey(*this, caps, b);
162}
163
164GrGLFragmentProcessor* GrBicubicEffect::createGLInstance() const {
165 return SkNEW_ARGS(GrGLBicubicEffect, (*this));
humper@google.com3aad3b02013-09-04 19:23:53 +0000166}
167
bsalomon0e08fc12014-10-15 08:19:04 -0700168bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700169 const GrBicubicEffect& s = sBase.cast<GrBicubicEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700170 return !memcmp(fCoefficients, s.coefficients(), 16) &&
bsalomon838f62d2014-08-05 07:15:57 -0700171 fDomain == s.fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +0000172}
173
egdaniel605dd0f2014-11-12 08:35:25 -0800174void GrBicubicEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000175 // FIXME: Perhaps we can do better.
egdanielccb2e382014-10-13 12:53:46 -0700176 inout->mulByUnknownAlpha();
humper@google.com3aad3b02013-09-04 19:23:53 +0000177}
178
joshualittb0a8a372014-09-23 09:50:21 -0700179GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect);
humper@google.com3aad3b02013-09-04 19:23:53 +0000180
joshualittb0a8a372014-09-23 09:50:21 -0700181GrFragmentProcessor* GrBicubicEffect::TestCreate(SkRandom* random,
182 GrContext* context,
183 const GrDrawTargetCaps&,
184 GrTexture* textures[]) {
185 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
186 GrProcessorUnitTest::kAlphaTextureIdx;
humper@google.com3aad3b02013-09-04 19:23:53 +0000187 SkScalar coefficients[16];
188 for (int i = 0; i < 16; i++) {
189 coefficients[i] = random->nextSScalar1();
190 }
191 return GrBicubicEffect::Create(textures[texIdx], coefficients);
192}
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000193
194//////////////////////////////////////////////////////////////////////////////
195
196bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix,
197 GrTextureParams::FilterMode* filterMode) {
198 if (matrix.isIdentity()) {
199 *filterMode = GrTextureParams::kNone_FilterMode;
200 return false;
201 }
202
203 SkScalar scales[2];
204 if (!matrix.getMinMaxScales(scales) || scales[0] < SK_Scalar1) {
205 // Bicubic doesn't handle arbitrary minimization well, as src texels can be skipped
206 // entirely,
207 *filterMode = GrTextureParams::kMipMap_FilterMode;
208 return false;
209 }
210 // At this point if scales[1] == SK_Scalar1 then the matrix doesn't do any scaling.
211 if (scales[1] == SK_Scalar1) {
212 if (matrix.rectStaysRect() && SkScalarIsInt(matrix.getTranslateX()) &&
213 SkScalarIsInt(matrix.getTranslateY())) {
214 *filterMode = GrTextureParams::kNone_FilterMode;
215 } else {
216 // Use bilerp to handle rotation or fractional translation.
217 *filterMode = GrTextureParams::kBilerp_FilterMode;
218 }
219 return false;
220 }
221 // When we use the bicubic filtering effect each sample is read from the texture using
222 // nearest neighbor sampling.
223 *filterMode = GrTextureParams::kNone_FilterMode;
224 return true;
225}