blob: 76edff0dc6c48701791a94e409109fe5253da635 [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,
bsalomon422f56f2014-12-09 10:18:12 -080063 kMat44f_GrSLType, kDefault_GrSLPrecision,
64 "Coefficients");
joshualitt30ba4362014-08-21 20:18:45 -070065 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -080066 kVec2f_GrSLType, kDefault_GrSLPrecision,
67 "ImageIncrement");
humper@google.com3aad3b02013-09-04 19:23:53 +000068
69 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
70 const char* coeff = builder->getUniformCStr(fCoefficientsUni);
71
72 SkString cubicBlendName;
73
74 static const GrGLShaderVar gCubicBlendArgs[] = {
75 GrGLShaderVar("coefficients", kMat44f_GrSLType),
76 GrGLShaderVar("t", kFloat_GrSLType),
77 GrGLShaderVar("c0", kVec4f_GrSLType),
78 GrGLShaderVar("c1", kVec4f_GrSLType),
79 GrGLShaderVar("c2", kVec4f_GrSLType),
80 GrGLShaderVar("c3", kVec4f_GrSLType),
81 };
joshualitt15988992014-10-09 15:04:05 -070082 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -070083 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0);
84 fsBuilder->emitFunction(kVec4f_GrSLType,
humper@google.com3aad3b02013-09-04 19:23:53 +000085 "cubicBlend",
86 SK_ARRAY_COUNT(gCubicBlendArgs),
87 gCubicBlendArgs,
88 "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n"
89 "\tvec4 c = coefficients * ts;\n"
90 "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;\n",
91 &cubicBlendName);
joshualitt30ba4362014-08-21 20:18:45 -070092 fsBuilder->codeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_str(), imgInc);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000093 // We unnormalize the coord in order to determine our fractional offset (f) within the texel
94 // We then snap coord to a texel center and renormalize. The snap prevents cases where the
95 // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/
96 // double hit a texel.
joshualitt30ba4362014-08-21 20:18:45 -070097 fsBuilder->codeAppendf("\tcoord /= %s;\n", imgInc);
98 fsBuilder->codeAppend("\tvec2 f = fract(coord);\n");
99 fsBuilder->codeAppendf("\tcoord = (coord - f + vec2(0.5)) * %s;\n", imgInc);
100 fsBuilder->codeAppend("\tvec4 rowColors[4];\n");
humper@google.com3aad3b02013-09-04 19:23:53 +0000101 for (int y = 0; y < 4; ++y) {
102 for (int x = 0; x < 4; ++x) {
103 SkString coord;
104 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000105 SkString sampleVar;
106 sampleVar.printf("rowColors[%d]", x);
joshualitt30ba4362014-08-21 20:18:45 -0700107 fDomain.sampleTexture(fsBuilder, domain, sampleVar.c_str(), coord, samplers[0]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000108 }
joshualitt30ba4362014-08-21 20:18:45 -0700109 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 +0000110 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000111 SkString bicubicColor;
112 bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), coeff);
joshualitt30ba4362014-08-21 20:18:45 -0700113 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor, (GrGLSLExpr4(bicubicColor.c_str()) * GrGLSLExpr4(inputColor)).c_str());
humper@google.com3aad3b02013-09-04 19:23:53 +0000114}
115
kkinnunen7510b222014-07-30 00:04:16 -0700116void GrGLBicubicEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700117 const GrProcessor& processor) {
118 const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>();
119 const GrTexture& texture = *processor.texture(0);
humper@google.com3aad3b02013-09-04 19:23:53 +0000120 float imageIncrement[2];
121 imageIncrement[0] = 1.0f / texture.width();
122 imageIncrement[1] = 1.0f / texture.height();
kkinnunen7510b222014-07-30 00:04:16 -0700123 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
joshualitt49586be2014-09-16 08:21:41 -0700124 pdman.setMatrix4f(fCoefficientsUni, bicubicEffect.coefficients());
125 fDomain.setData(pdman, bicubicEffect.domain(), texture.origin());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000126}
127
128static inline void convert_row_major_scalar_coeffs_to_column_major_floats(float dst[16],
129 const SkScalar src[16]) {
130 for (int y = 0; y < 4; y++) {
131 for (int x = 0; x < 4; x++) {
132 dst[x * 4 + y] = SkScalarToFloat(src[y * 4 + x]);
133 }
134 }
humper@google.com3aad3b02013-09-04 19:23:53 +0000135}
136
137GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
humper@google.com3aad3b02013-09-04 19:23:53 +0000138 const SkScalar coefficients[16],
139 const SkMatrix &matrix,
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +0000140 const SkShader::TileMode tileModes[2])
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000141 : INHERITED(texture, matrix, GrTextureParams(tileModes, GrTextureParams::kNone_FilterMode))
142 , fDomain(GrTextureDomain::IgnoredDomain()) {
joshualitteb2a6762014-12-04 11:35:33 -0800143 this->initClassID<GrBicubicEffect>();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000144 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) {
joshualitteb2a6762014-12-04 11:35:33 -0800154 this->initClassID<GrBicubicEffect>();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000155 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
joshualitteb2a6762014-12-04 11:35:33 -0800161void GrBicubicEffect::getGLProcessorKey(const GrGLCaps& caps,
162 GrProcessorKeyBuilder* b) const {
163 GrGLBicubicEffect::GenKey(*this, caps, b);
164}
165
166GrGLFragmentProcessor* GrBicubicEffect::createGLInstance() const {
167 return SkNEW_ARGS(GrGLBicubicEffect, (*this));
humper@google.com3aad3b02013-09-04 19:23:53 +0000168}
169
bsalomon0e08fc12014-10-15 08:19:04 -0700170bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700171 const GrBicubicEffect& s = sBase.cast<GrBicubicEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700172 return !memcmp(fCoefficients, s.coefficients(), 16) &&
bsalomon838f62d2014-08-05 07:15:57 -0700173 fDomain == s.fDomain;
humper@google.com3aad3b02013-09-04 19:23:53 +0000174}
175
egdaniel605dd0f2014-11-12 08:35:25 -0800176void GrBicubicEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000177 // FIXME: Perhaps we can do better.
joshualitt56995b52014-12-11 15:44:02 -0800178 inout->mulByUnknownSingleComponent();
humper@google.com3aad3b02013-09-04 19:23:53 +0000179}
180
joshualittb0a8a372014-09-23 09:50:21 -0700181GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect);
humper@google.com3aad3b02013-09-04 19:23:53 +0000182
joshualittb0a8a372014-09-23 09:50:21 -0700183GrFragmentProcessor* GrBicubicEffect::TestCreate(SkRandom* random,
184 GrContext* context,
185 const GrDrawTargetCaps&,
186 GrTexture* textures[]) {
187 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
188 GrProcessorUnitTest::kAlphaTextureIdx;
humper@google.com3aad3b02013-09-04 19:23:53 +0000189 SkScalar coefficients[16];
190 for (int i = 0; i < 16; i++) {
191 coefficients[i] = random->nextSScalar1();
192 }
193 return GrBicubicEffect::Create(textures[texIdx], coefficients);
194}
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +0000195
196//////////////////////////////////////////////////////////////////////////////
197
198bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix,
199 GrTextureParams::FilterMode* filterMode) {
200 if (matrix.isIdentity()) {
201 *filterMode = GrTextureParams::kNone_FilterMode;
202 return false;
203 }
204
205 SkScalar scales[2];
206 if (!matrix.getMinMaxScales(scales) || scales[0] < SK_Scalar1) {
207 // Bicubic doesn't handle arbitrary minimization well, as src texels can be skipped
208 // entirely,
209 *filterMode = GrTextureParams::kMipMap_FilterMode;
210 return false;
211 }
212 // At this point if scales[1] == SK_Scalar1 then the matrix doesn't do any scaling.
213 if (scales[1] == SK_Scalar1) {
214 if (matrix.rectStaysRect() && SkScalarIsInt(matrix.getTranslateX()) &&
215 SkScalarIsInt(matrix.getTranslateY())) {
216 *filterMode = GrTextureParams::kNone_FilterMode;
217 } else {
218 // Use bilerp to handle rotation or fractional translation.
219 *filterMode = GrTextureParams::kBilerp_FilterMode;
220 }
221 return false;
222 }
223 // When we use the bicubic filtering effect each sample is read from the texture using
224 // nearest neighbor sampling.
225 *filterMode = GrTextureParams::kNone_FilterMode;
226 return true;
227}