humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 1 | #include "GrBicubicEffect.h" |
| 2 | |
| 3 | #define DS(x) SkDoubleToScalar(x) |
| 4 | |
| 5 | const SkScalar GrBicubicEffect::gMitchellCoefficients[16] = { |
| 6 | DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0), |
| 7 | DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0), |
| 8 | DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0), |
| 9 | DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0), |
| 10 | }; |
| 11 | |
| 12 | |
| 13 | class GrGLBicubicEffect : public GrGLEffect { |
| 14 | public: |
| 15 | GrGLBicubicEffect(const GrBackendEffectFactory& factory, |
| 16 | const GrDrawEffect&); |
| 17 | virtual void emitCode(GrGLShaderBuilder*, |
| 18 | const GrDrawEffect&, |
| 19 | EffectKey, |
| 20 | const char* outputColor, |
| 21 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 22 | const TransformedCoordsArray&, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 23 | const TextureSamplerArray&) SK_OVERRIDE; |
| 24 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 25 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
| 26 | |
| 27 | private: |
| 28 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 29 | |
| 30 | UniformHandle fCoefficientsUni; |
| 31 | UniformHandle fImageIncrementUni; |
| 32 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 33 | typedef GrGLEffect INHERITED; |
| 34 | }; |
| 35 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 36 | GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 37 | : INHERITED(factory) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void GrGLBicubicEffect::emitCode(GrGLShaderBuilder* builder, |
| 41 | const GrDrawEffect&, |
| 42 | EffectKey key, |
| 43 | const char* outputColor, |
| 44 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 45 | const TransformedCoordsArray& coords, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 46 | const TextureSamplerArray& samplers) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 47 | sk_ignore_unused_variable(inputColor); |
| 48 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 49 | SkString coords2D = builder->ensureFSCoords2D(coords, 0); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 50 | fCoefficientsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 51 | kMat44f_GrSLType, "Coefficients"); |
| 52 | fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 53 | kVec2f_GrSLType, "ImageIncrement"); |
| 54 | |
| 55 | const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 56 | const char* coeff = builder->getUniformCStr(fCoefficientsUni); |
| 57 | |
| 58 | SkString cubicBlendName; |
| 59 | |
| 60 | static const GrGLShaderVar gCubicBlendArgs[] = { |
| 61 | GrGLShaderVar("coefficients", kMat44f_GrSLType), |
| 62 | GrGLShaderVar("t", kFloat_GrSLType), |
| 63 | GrGLShaderVar("c0", kVec4f_GrSLType), |
| 64 | GrGLShaderVar("c1", kVec4f_GrSLType), |
| 65 | GrGLShaderVar("c2", kVec4f_GrSLType), |
| 66 | GrGLShaderVar("c3", kVec4f_GrSLType), |
| 67 | }; |
| 68 | builder->fsEmitFunction(kVec4f_GrSLType, |
| 69 | "cubicBlend", |
| 70 | SK_ARRAY_COUNT(gCubicBlendArgs), |
| 71 | gCubicBlendArgs, |
| 72 | "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n" |
| 73 | "\tvec4 c = coefficients * ts;\n" |
| 74 | "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;\n", |
| 75 | &cubicBlendName); |
commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame^] | 76 | builder->fsCodeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_str(), imgInc); |
| 77 | // We unnormalize the coord in order to determine our fractional offset (f) within the texel |
| 78 | // We then snap coord to a texel center and renormalize. The snap prevents cases where the |
| 79 | // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/ |
| 80 | // double hit a texel. |
| 81 | builder->fsCodeAppendf("\tcoord /= %s;\n", imgInc); |
| 82 | builder->fsCodeAppend("\tvec2 f = fract(coord);\n"); |
| 83 | builder->fsCodeAppendf("\tcoord = (coord - f + vec2(0.5)) * %s;\n", imgInc); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 84 | for (int y = 0; y < 4; ++y) { |
| 85 | for (int x = 0; x < 4; ++x) { |
| 86 | SkString coord; |
| 87 | coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1); |
| 88 | builder->fsCodeAppendf("\tvec4 s%d%d = ", x, y); |
| 89 | builder->fsAppendTextureLookup(samplers[0], coord.c_str()); |
| 90 | builder->fsCodeAppend(";\n"); |
| 91 | } |
| 92 | builder->fsCodeAppendf("\tvec4 s%d = %s(%s, f.x, s0%d, s1%d, s2%d, s3%d);\n", y, cubicBlendName.c_str(), coeff, y, y, y, y); |
| 93 | } |
| 94 | builder->fsCodeAppendf("\t%s = %s(%s, f.y, s0, s1, s2, s3);\n", outputColor, cubicBlendName.c_str(), coeff); |
| 95 | } |
| 96 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 97 | void GrGLBicubicEffect::setData(const GrGLUniformManager& uman, |
| 98 | const GrDrawEffect& drawEffect) { |
| 99 | const GrBicubicEffect& effect = drawEffect.castEffect<GrBicubicEffect>(); |
| 100 | GrTexture& texture = *effect.texture(0); |
| 101 | float imageIncrement[2]; |
| 102 | imageIncrement[0] = 1.0f / texture.width(); |
| 103 | imageIncrement[1] = 1.0f / texture.height(); |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 104 | uman.set2fv(fImageIncrementUni, 1, imageIncrement); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 105 | uman.setMatrix4f(fCoefficientsUni, effect.coefficients()); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | GrBicubicEffect::GrBicubicEffect(GrTexture* texture, |
| 109 | const SkScalar coefficients[16]) |
| 110 | : INHERITED(texture, MakeDivByTextureWHMatrix(texture)) { |
| 111 | for (int y = 0; y < 4; y++) { |
| 112 | for (int x = 0; x < 4; x++) { |
| 113 | // Convert from row-major scalars to column-major floats. |
| 114 | fCoefficients[x * 4 + y] = SkScalarToFloat(coefficients[y * 4 + x]); |
| 115 | } |
| 116 | } |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 117 | this->setWillNotUseInputColor(); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | GrBicubicEffect::GrBicubicEffect(GrTexture* texture, |
| 121 | const SkScalar coefficients[16], |
| 122 | const SkMatrix &matrix, |
| 123 | const GrTextureParams ¶ms, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 124 | GrCoordSet coordSet) |
commit-bot@chromium.org | 759befb | 2013-10-23 16:18:50 +0000 | [diff] [blame] | 125 | : INHERITED(texture, matrix, params, coordSet) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 126 | for (int y = 0; y < 4; y++) { |
| 127 | for (int x = 0; x < 4; x++) { |
| 128 | // Convert from row-major scalars to column-major floats. |
| 129 | fCoefficients[x * 4 + y] = SkScalarToFloat(coefficients[y * 4 + x]); |
| 130 | } |
| 131 | } |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 132 | this->setWillNotUseInputColor(); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | GrBicubicEffect::~GrBicubicEffect() { |
| 136 | } |
| 137 | |
| 138 | const GrBackendEffectFactory& GrBicubicEffect::getFactory() const { |
| 139 | return GrTBackendEffectFactory<GrBicubicEffect>::getInstance(); |
| 140 | } |
| 141 | |
| 142 | bool GrBicubicEffect::onIsEqual(const GrEffect& sBase) const { |
| 143 | const GrBicubicEffect& s = CastEffect<GrBicubicEffect>(sBase); |
humper@google.com | d1af237 | 2013-09-04 20:32:58 +0000 | [diff] [blame] | 144 | return this->textureAccess(0) == s.textureAccess(0) && |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 145 | !memcmp(fCoefficients, s.coefficients(), 16); |
| 146 | } |
| 147 | |
| 148 | void GrBicubicEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 149 | // FIXME: Perhaps we can do better. |
| 150 | *validFlags = 0; |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | GR_DEFINE_EFFECT_TEST(GrBicubicEffect); |
| 155 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 156 | GrEffectRef* GrBicubicEffect::TestCreate(SkRandom* random, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 157 | GrContext* context, |
| 158 | const GrDrawTargetCaps&, |
| 159 | GrTexture* textures[]) { |
| 160 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 161 | GrEffectUnitTest::kAlphaTextureIdx; |
| 162 | SkScalar coefficients[16]; |
| 163 | for (int i = 0; i < 16; i++) { |
| 164 | coefficients[i] = random->nextSScalar1(); |
| 165 | } |
| 166 | return GrBicubicEffect::Create(textures[texIdx], coefficients); |
| 167 | } |