bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 1 | /* |
| 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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 8 | #include "GrBicubicEffect.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 9 | #include "GrInvariantOutput.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame^] | 10 | #include "gl/builders/GrGLProgramBuilder.h" |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 11 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 12 | #define DS(x) SkDoubleToScalar(x) |
| 13 | |
| 14 | const 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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 22 | class GrGLBicubicEffect : public GrGLFragmentProcessor { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 23 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame^] | 24 | GrGLBicubicEffect(const GrProcessor&); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 25 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 26 | virtual void emitCode(GrGLFPBuilder*, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 27 | const GrFragmentProcessor&, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 28 | const char* outputColor, |
| 29 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 30 | const TransformedCoordsArray&, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 31 | const TextureSamplerArray&) SK_OVERRIDE; |
| 32 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 33 | virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 34 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 35 | static inline void GenKey(const GrProcessor& effect, const GrGLCaps&, |
| 36 | GrProcessorKeyBuilder* b) { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 37 | const GrTextureDomain& domain = effect.cast<GrBicubicEffect>().domain(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 38 | b->add32(GrTextureDomain::GLDomain::DomainKey(domain)); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 39 | } |
| 40 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 41 | private: |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 42 | typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 43 | |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 44 | UniformHandle fCoefficientsUni; |
| 45 | UniformHandle fImageIncrementUni; |
| 46 | GrTextureDomain::GLDomain fDomain; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 47 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 48 | typedef GrGLFragmentProcessor INHERITED; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame^] | 51 | GrGLBicubicEffect::GrGLBicubicEffect(const GrProcessor&) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 52 | } |
| 53 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 54 | void GrGLBicubicEffect::emitCode(GrGLFPBuilder* builder, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 55 | const GrFragmentProcessor& effect, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 56 | const char* outputColor, |
| 57 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 58 | const TransformedCoordsArray& coords, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 59 | const TextureSamplerArray& samplers) { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 60 | const GrTextureDomain& domain = effect.cast<GrBicubicEffect>().domain(); |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 61 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 62 | fCoefficientsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 63 | kMat44f_GrSLType, "Coefficients"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 64 | fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 65 | 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 | }; |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 80 | GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 81 | SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); |
| 82 | fsBuilder->emitFunction(kVec4f_GrSLType, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 83 | "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); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 90 | fsBuilder->codeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_str(), imgInc); |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 91 | // 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. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 95 | 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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 99 | 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.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 103 | SkString sampleVar; |
| 104 | sampleVar.printf("rowColors[%d]", x); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 105 | fDomain.sampleTexture(fsBuilder, domain, sampleVar.c_str(), coord, samplers[0]); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 106 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 107 | 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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 108 | } |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 109 | SkString bicubicColor; |
| 110 | bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), coeff); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 111 | fsBuilder->codeAppendf("\t%s = %s;\n", outputColor, (GrGLSLExpr4(bicubicColor.c_str()) * GrGLSLExpr4(inputColor)).c_str()); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 112 | } |
| 113 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 114 | void GrGLBicubicEffect::setData(const GrGLProgramDataManager& pdman, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 115 | const GrProcessor& processor) { |
| 116 | const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>(); |
| 117 | const GrTexture& texture = *processor.texture(0); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 118 | float imageIncrement[2]; |
| 119 | imageIncrement[0] = 1.0f / texture.width(); |
| 120 | imageIncrement[1] = 1.0f / texture.height(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 121 | pdman.set2fv(fImageIncrementUni, 1, imageIncrement); |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 122 | pdman.setMatrix4f(fCoefficientsUni, bicubicEffect.coefficients()); |
| 123 | fDomain.setData(pdman, bicubicEffect.domain(), texture.origin()); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static 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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | GrBicubicEffect::GrBicubicEffect(GrTexture* texture, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 136 | const SkScalar coefficients[16], |
| 137 | const SkMatrix &matrix, |
commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 138 | const SkShader::TileMode tileModes[2]) |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 139 | : INHERITED(texture, matrix, GrTextureParams(tileModes, GrTextureParams::kNone_FilterMode)) |
| 140 | , fDomain(GrTextureDomain::IgnoredDomain()) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame^] | 141 | this->initClassID<GrBicubicEffect>(); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 142 | convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients); |
| 143 | } |
| 144 | |
| 145 | GrBicubicEffect::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) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame^] | 152 | this->initClassID<GrBicubicEffect>(); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 153 | convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | GrBicubicEffect::~GrBicubicEffect() { |
| 157 | } |
| 158 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame^] | 159 | void GrBicubicEffect::getGLProcessorKey(const GrGLCaps& caps, |
| 160 | GrProcessorKeyBuilder* b) const { |
| 161 | GrGLBicubicEffect::GenKey(*this, caps, b); |
| 162 | } |
| 163 | |
| 164 | GrGLFragmentProcessor* GrBicubicEffect::createGLInstance() const { |
| 165 | return SkNEW_ARGS(GrGLBicubicEffect, (*this)); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 166 | } |
| 167 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 168 | bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 169 | const GrBicubicEffect& s = sBase.cast<GrBicubicEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 170 | return !memcmp(fCoefficients, s.coefficients(), 16) && |
bsalomon | 838f62d | 2014-08-05 07:15:57 -0700 | [diff] [blame] | 171 | fDomain == s.fDomain; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 172 | } |
| 173 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 174 | void GrBicubicEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 175 | // FIXME: Perhaps we can do better. |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 176 | inout->mulByUnknownAlpha(); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 177 | } |
| 178 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 179 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 180 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 181 | GrFragmentProcessor* 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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 187 | 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.org | 9927bd3 | 2014-05-20 17:51:13 +0000 | [diff] [blame] | 193 | |
| 194 | ////////////////////////////////////////////////////////////////////////////// |
| 195 | |
| 196 | bool 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 | } |