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" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 10 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 11 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 12 | #include "glsl/GrGLSLUniformHandler.h" |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 13 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 14 | #define DS(x) SkDoubleToScalar(x) |
| 15 | |
| 16 | const SkScalar GrBicubicEffect::gMitchellCoefficients[16] = { |
| 17 | DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0), |
| 18 | DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0), |
| 19 | DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0), |
| 20 | DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0), |
| 21 | }; |
| 22 | |
| 23 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 24 | class GrGLBicubicEffect : public GrGLSLFragmentProcessor { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 25 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 26 | void emitCode(EmitArgs&) override; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 27 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 28 | static inline void GenKey(const GrProcessor& effect, const GrGLSLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 29 | GrProcessorKeyBuilder* b) { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 30 | const GrTextureDomain& domain = effect.cast<GrBicubicEffect>().domain(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 31 | b->add32(GrTextureDomain::GLDomain::DomainKey(domain)); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 32 | } |
| 33 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 34 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 35 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 36 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 37 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 38 | typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 39 | |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 40 | UniformHandle fCoefficientsUni; |
| 41 | UniformHandle fImageIncrementUni; |
| 42 | GrTextureDomain::GLDomain fDomain; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 43 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 44 | typedef GrGLSLFragmentProcessor INHERITED; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 47 | void GrGLBicubicEffect::emitCode(EmitArgs& args) { |
| 48 | const GrTextureDomain& domain = args.fFp.cast<GrBicubicEffect>().domain(); |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 49 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 50 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 51 | fCoefficientsUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 52 | kMat44f_GrSLType, kDefault_GrSLPrecision, |
| 53 | "Coefficients"); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 54 | fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 55 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 56 | "ImageIncrement"); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 57 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 58 | const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni); |
| 59 | const char* coeff = uniformHandler->getUniformCStr(fCoefficientsUni); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 60 | |
| 61 | SkString cubicBlendName; |
| 62 | |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 63 | static const GrGLSLShaderVar gCubicBlendArgs[] = { |
| 64 | GrGLSLShaderVar("coefficients", kMat44f_GrSLType), |
| 65 | GrGLSLShaderVar("t", kFloat_GrSLType), |
| 66 | GrGLSLShaderVar("c0", kVec4f_GrSLType), |
| 67 | GrGLSLShaderVar("c1", kVec4f_GrSLType), |
| 68 | GrGLSLShaderVar("c2", kVec4f_GrSLType), |
| 69 | GrGLSLShaderVar("c3", kVec4f_GrSLType), |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 70 | }; |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 71 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 72 | SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0); |
| 73 | fragBuilder->emitFunction(kVec4f_GrSLType, |
| 74 | "cubicBlend", |
| 75 | SK_ARRAY_COUNT(gCubicBlendArgs), |
| 76 | gCubicBlendArgs, |
| 77 | "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n" |
| 78 | "\tvec4 c = coefficients * ts;\n" |
| 79 | "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;\n", |
| 80 | &cubicBlendName); |
| 81 | fragBuilder->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] | 82 | // We unnormalize the coord in order to determine our fractional offset (f) within the texel |
| 83 | // We then snap coord to a texel center and renormalize. The snap prevents cases where the |
| 84 | // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/ |
| 85 | // double hit a texel. |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 86 | fragBuilder->codeAppendf("\tcoord /= %s;\n", imgInc); |
| 87 | fragBuilder->codeAppend("\tvec2 f = fract(coord);\n"); |
| 88 | fragBuilder->codeAppendf("\tcoord = (coord - f + vec2(0.5)) * %s;\n", imgInc); |
| 89 | fragBuilder->codeAppend("\tvec4 rowColors[4];\n"); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 90 | for (int y = 0; y < 4; ++y) { |
| 91 | for (int x = 0; x < 4; ++x) { |
| 92 | SkString coord; |
| 93 | 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] | 94 | SkString sampleVar; |
| 95 | sampleVar.printf("rowColors[%d]", x); |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 96 | fDomain.sampleTexture(fragBuilder, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 97 | args.fUniformHandler, |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 98 | args.fGLSLCaps, |
| 99 | domain, |
| 100 | sampleVar.c_str(), |
| 101 | coord, |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 102 | args.fTexSamplers[0]); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 103 | } |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 104 | fragBuilder->codeAppendf( |
| 105 | "\tvec4 s%d = %s(%s, f.x, rowColors[0], rowColors[1], rowColors[2], rowColors[3]);\n", |
| 106 | y, cubicBlendName.c_str(), coeff); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 107 | } |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 108 | SkString bicubicColor; |
| 109 | bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), coeff); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 110 | fragBuilder->codeAppendf("\t%s = %s;\n", |
| 111 | args.fOutputColor, (GrGLSLExpr4(bicubicColor.c_str()) * |
| 112 | GrGLSLExpr4(args.fInputColor)).c_str()); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 113 | } |
| 114 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 115 | void GrGLBicubicEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| 116 | const GrProcessor& processor) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 117 | const GrBicubicEffect& bicubicEffect = processor.cast<GrBicubicEffect>(); |
| 118 | const GrTexture& texture = *processor.texture(0); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 119 | float imageIncrement[2]; |
| 120 | imageIncrement[0] = 1.0f / texture.width(); |
| 121 | imageIncrement[1] = 1.0f / texture.height(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 122 | pdman.set2fv(fImageIncrementUni, 1, imageIncrement); |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 123 | pdman.setMatrix4f(fCoefficientsUni, bicubicEffect.coefficients()); |
| 124 | fDomain.setData(pdman, bicubicEffect.domain(), texture.origin()); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static inline void convert_row_major_scalar_coeffs_to_column_major_floats(float dst[16], |
| 128 | const SkScalar src[16]) { |
| 129 | for (int y = 0; y < 4; y++) { |
| 130 | for (int x = 0; x < 4; x++) { |
| 131 | dst[x * 4 + y] = SkScalarToFloat(src[y * 4 + x]); |
| 132 | } |
| 133 | } |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 134 | } |
| 135 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 136 | GrBicubicEffect::GrBicubicEffect(GrTexture* texture, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 137 | const SkScalar coefficients[16], |
| 138 | const SkMatrix &matrix, |
commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 139 | const SkShader::TileMode tileModes[2]) |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 140 | : INHERITED(texture, matrix, GrTextureParams(tileModes, GrTextureParams::kNone_FilterMode)) |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 141 | , fDomain(GrTextureDomain::IgnoredDomain()) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 142 | this->initClassID<GrBicubicEffect>(); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 143 | convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients); |
| 144 | } |
| 145 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 146 | GrBicubicEffect::GrBicubicEffect(GrTexture* texture, |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 147 | const SkScalar coefficients[16], |
| 148 | const SkMatrix &matrix, |
| 149 | const SkRect& domain) |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 150 | : INHERITED(texture, matrix, |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 151 | GrTextureParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode)) |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 152 | , fDomain(domain, GrTextureDomain::kClamp_Mode) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 153 | this->initClassID<GrBicubicEffect>(); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 154 | convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | GrBicubicEffect::~GrBicubicEffect() { |
| 158 | } |
| 159 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 160 | void GrBicubicEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 161 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 162 | GrGLBicubicEffect::GenKey(*this, caps, b); |
| 163 | } |
| 164 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 165 | GrGLSLFragmentProcessor* GrBicubicEffect::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 166 | return new GrGLBicubicEffect; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 167 | } |
| 168 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 169 | bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 170 | const GrBicubicEffect& s = sBase.cast<GrBicubicEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 171 | return !memcmp(fCoefficients, s.coefficients(), 16) && |
bsalomon | 838f62d | 2014-08-05 07:15:57 -0700 | [diff] [blame] | 172 | fDomain == s.fDomain; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 173 | } |
| 174 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 175 | void GrBicubicEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 176 | // FIXME: Perhaps we can do better. |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 177 | inout->mulByUnknownSingleComponent(); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 178 | } |
| 179 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 180 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 181 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame^] | 182 | sk_sp<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 183 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 184 | GrProcessorUnitTest::kAlphaTextureIdx; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 185 | SkScalar coefficients[16]; |
| 186 | for (int i = 0; i < 16; i++) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 187 | coefficients[i] = d->fRandom->nextSScalar1(); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 188 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame^] | 189 | return GrBicubicEffect::Make(d->fTextures[texIdx], coefficients); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 190 | } |
commit-bot@chromium.org | 9927bd3 | 2014-05-20 17:51:13 +0000 | [diff] [blame] | 191 | |
| 192 | ////////////////////////////////////////////////////////////////////////////// |
| 193 | |
| 194 | bool 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 | } |