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) { |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 47 | SkString coords2D = builder->ensureFSCoords2D(coords, 0); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 48 | fCoefficientsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 49 | kMat44f_GrSLType, "Coefficients"); |
| 50 | fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 51 | kVec2f_GrSLType, "ImageIncrement"); |
| 52 | |
| 53 | const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 54 | const char* coeff = builder->getUniformCStr(fCoefficientsUni); |
| 55 | |
| 56 | SkString cubicBlendName; |
| 57 | |
| 58 | static const GrGLShaderVar gCubicBlendArgs[] = { |
| 59 | GrGLShaderVar("coefficients", kMat44f_GrSLType), |
| 60 | GrGLShaderVar("t", kFloat_GrSLType), |
| 61 | GrGLShaderVar("c0", kVec4f_GrSLType), |
| 62 | GrGLShaderVar("c1", kVec4f_GrSLType), |
| 63 | GrGLShaderVar("c2", kVec4f_GrSLType), |
| 64 | GrGLShaderVar("c3", kVec4f_GrSLType), |
| 65 | }; |
| 66 | builder->fsEmitFunction(kVec4f_GrSLType, |
| 67 | "cubicBlend", |
| 68 | SK_ARRAY_COUNT(gCubicBlendArgs), |
| 69 | gCubicBlendArgs, |
| 70 | "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n" |
| 71 | "\tvec4 c = coefficients * ts;\n" |
| 72 | "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;\n", |
| 73 | &cubicBlendName); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 74 | builder->fsCodeAppendf("\tvec2 coord = %s - %s * vec2(0.5, 0.5);\n", coords2D.c_str(), imgInc); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 75 | builder->fsCodeAppendf("\tvec2 f = fract(coord / %s);\n", imgInc); |
| 76 | for (int y = 0; y < 4; ++y) { |
| 77 | for (int x = 0; x < 4; ++x) { |
| 78 | SkString coord; |
| 79 | coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1); |
| 80 | builder->fsCodeAppendf("\tvec4 s%d%d = ", x, y); |
| 81 | builder->fsAppendTextureLookup(samplers[0], coord.c_str()); |
| 82 | builder->fsCodeAppend(";\n"); |
| 83 | } |
| 84 | 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); |
| 85 | } |
| 86 | builder->fsCodeAppendf("\t%s = %s(%s, f.y, s0, s1, s2, s3);\n", outputColor, cubicBlendName.c_str(), coeff); |
| 87 | } |
| 88 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 89 | void GrGLBicubicEffect::setData(const GrGLUniformManager& uman, |
| 90 | const GrDrawEffect& drawEffect) { |
| 91 | const GrBicubicEffect& effect = drawEffect.castEffect<GrBicubicEffect>(); |
| 92 | GrTexture& texture = *effect.texture(0); |
| 93 | float imageIncrement[2]; |
| 94 | imageIncrement[0] = 1.0f / texture.width(); |
| 95 | imageIncrement[1] = 1.0f / texture.height(); |
| 96 | uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); |
| 97 | uman.setMatrix4f(fCoefficientsUni, effect.coefficients()); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | GrBicubicEffect::GrBicubicEffect(GrTexture* texture, |
| 101 | const SkScalar coefficients[16]) |
| 102 | : INHERITED(texture, MakeDivByTextureWHMatrix(texture)) { |
| 103 | for (int y = 0; y < 4; y++) { |
| 104 | for (int x = 0; x < 4; x++) { |
| 105 | // Convert from row-major scalars to column-major floats. |
| 106 | fCoefficients[x * 4 + y] = SkScalarToFloat(coefficients[y * 4 + x]); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | GrBicubicEffect::GrBicubicEffect(GrTexture* texture, |
| 112 | const SkScalar coefficients[16], |
| 113 | const SkMatrix &matrix, |
| 114 | const GrTextureParams ¶ms, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 115 | GrCoordSet coordSet) |
| 116 | : INHERITED(texture, MakeDivByTextureWHMatrix(texture), params, coordSet) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 117 | for (int y = 0; y < 4; y++) { |
| 118 | for (int x = 0; x < 4; x++) { |
| 119 | // Convert from row-major scalars to column-major floats. |
| 120 | fCoefficients[x * 4 + y] = SkScalarToFloat(coefficients[y * 4 + x]); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | GrBicubicEffect::~GrBicubicEffect() { |
| 126 | } |
| 127 | |
| 128 | const GrBackendEffectFactory& GrBicubicEffect::getFactory() const { |
| 129 | return GrTBackendEffectFactory<GrBicubicEffect>::getInstance(); |
| 130 | } |
| 131 | |
| 132 | bool GrBicubicEffect::onIsEqual(const GrEffect& sBase) const { |
| 133 | const GrBicubicEffect& s = CastEffect<GrBicubicEffect>(sBase); |
humper@google.com | d1af237 | 2013-09-04 20:32:58 +0000 | [diff] [blame] | 134 | return this->textureAccess(0) == s.textureAccess(0) && |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 135 | !memcmp(fCoefficients, s.coefficients(), 16); |
| 136 | } |
| 137 | |
| 138 | void GrBicubicEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 139 | // FIXME: Perhaps we can do better. |
| 140 | *validFlags = 0; |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | GR_DEFINE_EFFECT_TEST(GrBicubicEffect); |
| 145 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 146 | GrEffectRef* GrBicubicEffect::TestCreate(SkRandom* random, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 147 | GrContext* context, |
| 148 | const GrDrawTargetCaps&, |
| 149 | GrTexture* textures[]) { |
| 150 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 151 | GrEffectUnitTest::kAlphaTextureIdx; |
| 152 | SkScalar coefficients[16]; |
| 153 | for (int i = 0; i < 16; i++) { |
| 154 | coefficients[i] = random->nextSScalar1(); |
| 155 | } |
| 156 | return GrBicubicEffect::Create(textures[texIdx], coefficients); |
| 157 | } |