michael@0: #include "GrBicubicEffect.h" michael@0: michael@0: #define DS(x) SkDoubleToScalar(x) michael@0: michael@0: const SkScalar GrBicubicEffect::gMitchellCoefficients[16] = { michael@0: DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0), michael@0: DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0), michael@0: DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0), michael@0: DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0), michael@0: }; michael@0: michael@0: michael@0: class GrGLBicubicEffect : public GrGLEffect { michael@0: public: michael@0: GrGLBicubicEffect(const GrBackendEffectFactory& factory, michael@0: const GrDrawEffect&); michael@0: michael@0: virtual void emitCode(GrGLShaderBuilder*, michael@0: const GrDrawEffect&, michael@0: EffectKey, michael@0: const char* outputColor, michael@0: const char* inputColor, michael@0: const TransformedCoordsArray&, michael@0: const TextureSamplerArray&) SK_OVERRIDE; michael@0: michael@0: virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; michael@0: michael@0: static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { michael@0: const GrTextureDomain& domain = drawEffect.castEffect().domain(); michael@0: return GrTextureDomain::GLDomain::DomainKey(domain); michael@0: } michael@0: michael@0: private: michael@0: typedef GrGLUniformManager::UniformHandle UniformHandle; michael@0: michael@0: UniformHandle fCoefficientsUni; michael@0: UniformHandle fImageIncrementUni; michael@0: GrTextureDomain::GLDomain fDomain; michael@0: michael@0: typedef GrGLEffect INHERITED; michael@0: }; michael@0: michael@0: GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) michael@0: : INHERITED(factory) { michael@0: } michael@0: michael@0: void GrGLBicubicEffect::emitCode(GrGLShaderBuilder* builder, michael@0: const GrDrawEffect& drawEffect, michael@0: EffectKey key, michael@0: const char* outputColor, michael@0: const char* inputColor, michael@0: const TransformedCoordsArray& coords, michael@0: const TextureSamplerArray& samplers) { michael@0: const GrTextureDomain& domain = drawEffect.castEffect().domain(); michael@0: michael@0: SkString coords2D = builder->ensureFSCoords2D(coords, 0); michael@0: fCoefficientsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, michael@0: kMat44f_GrSLType, "Coefficients"); michael@0: fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, michael@0: kVec2f_GrSLType, "ImageIncrement"); michael@0: michael@0: const char* imgInc = builder->getUniformCStr(fImageIncrementUni); michael@0: const char* coeff = builder->getUniformCStr(fCoefficientsUni); michael@0: michael@0: SkString cubicBlendName; michael@0: michael@0: static const GrGLShaderVar gCubicBlendArgs[] = { michael@0: GrGLShaderVar("coefficients", kMat44f_GrSLType), michael@0: GrGLShaderVar("t", kFloat_GrSLType), michael@0: GrGLShaderVar("c0", kVec4f_GrSLType), michael@0: GrGLShaderVar("c1", kVec4f_GrSLType), michael@0: GrGLShaderVar("c2", kVec4f_GrSLType), michael@0: GrGLShaderVar("c3", kVec4f_GrSLType), michael@0: }; michael@0: builder->fsEmitFunction(kVec4f_GrSLType, michael@0: "cubicBlend", michael@0: SK_ARRAY_COUNT(gCubicBlendArgs), michael@0: gCubicBlendArgs, michael@0: "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n" michael@0: "\tvec4 c = coefficients * ts;\n" michael@0: "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;\n", michael@0: &cubicBlendName); michael@0: builder->fsCodeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_str(), imgInc); michael@0: // We unnormalize the coord in order to determine our fractional offset (f) within the texel michael@0: // We then snap coord to a texel center and renormalize. The snap prevents cases where the michael@0: // starting coords are near a texel boundary and accumulations of imgInc would cause us to skip/ michael@0: // double hit a texel. michael@0: builder->fsCodeAppendf("\tcoord /= %s;\n", imgInc); michael@0: builder->fsCodeAppend("\tvec2 f = fract(coord);\n"); michael@0: builder->fsCodeAppendf("\tcoord = (coord - f + vec2(0.5)) * %s;\n", imgInc); michael@0: builder->fsCodeAppend("\tvec4 rowColors[4];\n"); michael@0: for (int y = 0; y < 4; ++y) { michael@0: for (int x = 0; x < 4; ++x) { michael@0: SkString coord; michael@0: coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1); michael@0: SkString sampleVar; michael@0: sampleVar.printf("rowColors[%d]", x); michael@0: fDomain.sampleTexture(builder, domain, sampleVar.c_str(), coord, samplers[0]); michael@0: } michael@0: builder->fsCodeAppendf("\tvec4 s%d = %s(%s, f.x, rowColors[0], rowColors[1], rowColors[2], rowColors[3]);\n", y, cubicBlendName.c_str(), coeff); michael@0: } michael@0: SkString bicubicColor; michael@0: bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), coeff); michael@0: builder->fsCodeAppendf("\t%s = %s;\n", outputColor, (GrGLSLExpr4(bicubicColor.c_str()) * GrGLSLExpr4(inputColor)).c_str()); michael@0: } michael@0: michael@0: void GrGLBicubicEffect::setData(const GrGLUniformManager& uman, michael@0: const GrDrawEffect& drawEffect) { michael@0: const GrBicubicEffect& effect = drawEffect.castEffect(); michael@0: const GrTexture& texture = *effect.texture(0); michael@0: float imageIncrement[2]; michael@0: imageIncrement[0] = 1.0f / texture.width(); michael@0: imageIncrement[1] = 1.0f / texture.height(); michael@0: uman.set2fv(fImageIncrementUni, 1, imageIncrement); michael@0: uman.setMatrix4f(fCoefficientsUni, effect.coefficients()); michael@0: fDomain.setData(uman, effect.domain(), texture.origin()); michael@0: } michael@0: michael@0: static inline void convert_row_major_scalar_coeffs_to_column_major_floats(float dst[16], michael@0: const SkScalar src[16]) { michael@0: for (int y = 0; y < 4; y++) { michael@0: for (int x = 0; x < 4; x++) { michael@0: dst[x * 4 + y] = SkScalarToFloat(src[y * 4 + x]); michael@0: } michael@0: } michael@0: } michael@0: michael@0: GrBicubicEffect::GrBicubicEffect(GrTexture* texture, michael@0: const SkScalar coefficients[16], michael@0: const SkMatrix &matrix, michael@0: const SkShader::TileMode tileModes[2]) michael@0: : INHERITED(texture, matrix, GrTextureParams(tileModes, GrTextureParams::kNone_FilterMode)) michael@0: , fDomain(GrTextureDomain::IgnoredDomain()) { michael@0: convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients); michael@0: } michael@0: michael@0: GrBicubicEffect::GrBicubicEffect(GrTexture* texture, michael@0: const SkScalar coefficients[16], michael@0: const SkMatrix &matrix, michael@0: const SkRect& domain) michael@0: : INHERITED(texture, matrix, GrTextureParams(SkShader::kClamp_TileMode, michael@0: GrTextureParams::kNone_FilterMode)) michael@0: , fDomain(domain, GrTextureDomain::kClamp_Mode) { michael@0: convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients); michael@0: } michael@0: michael@0: GrBicubicEffect::~GrBicubicEffect() { michael@0: } michael@0: michael@0: const GrBackendEffectFactory& GrBicubicEffect::getFactory() const { michael@0: return GrTBackendEffectFactory::getInstance(); michael@0: } michael@0: michael@0: bool GrBicubicEffect::onIsEqual(const GrEffect& sBase) const { michael@0: const GrBicubicEffect& s = CastEffect(sBase); michael@0: return this->textureAccess(0) == s.textureAccess(0) && michael@0: !memcmp(fCoefficients, s.coefficients(), 16); michael@0: } michael@0: michael@0: void GrBicubicEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { michael@0: // FIXME: Perhaps we can do better. michael@0: *validFlags = 0; michael@0: return; michael@0: } michael@0: michael@0: GR_DEFINE_EFFECT_TEST(GrBicubicEffect); michael@0: michael@0: GrEffectRef* GrBicubicEffect::TestCreate(SkRandom* random, michael@0: GrContext* context, michael@0: const GrDrawTargetCaps&, michael@0: GrTexture* textures[]) { michael@0: int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : michael@0: GrEffectUnitTest::kAlphaTextureIdx; michael@0: SkScalar coefficients[16]; michael@0: for (int i = 0; i < 16; i++) { michael@0: coefficients[i] = random->nextSScalar1(); michael@0: } michael@0: return GrBicubicEffect::Create(textures[texIdx], coefficients); michael@0: }