blob: 568311b3ca6694a8eebedb0abde2fff094e5f0e1 [file] [log] [blame]
humper@google.com3aad3b02013-09-04 19:23:53 +00001#include "GrBicubicEffect.h"
2
3#define DS(x) SkDoubleToScalar(x)
4
5const 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
13class GrGLBicubicEffect : public GrGLEffect {
14public:
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.com77af6802013-10-02 13:04:56 +000022 const TransformedCoordsArray&,
humper@google.com3aad3b02013-09-04 19:23:53 +000023 const TextureSamplerArray&) SK_OVERRIDE;
24
humper@google.com3aad3b02013-09-04 19:23:53 +000025 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
26
27private:
28 typedef GrGLUniformManager::UniformHandle UniformHandle;
29
30 UniformHandle fCoefficientsUni;
31 UniformHandle fImageIncrementUni;
32
humper@google.com3aad3b02013-09-04 19:23:53 +000033 typedef GrGLEffect INHERITED;
34};
35
bsalomon@google.com77af6802013-10-02 13:04:56 +000036GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
37 : INHERITED(factory) {
humper@google.com3aad3b02013-09-04 19:23:53 +000038}
39
40void GrGLBicubicEffect::emitCode(GrGLShaderBuilder* builder,
41 const GrDrawEffect&,
42 EffectKey key,
43 const char* outputColor,
44 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000045 const TransformedCoordsArray& coords,
humper@google.com3aad3b02013-09-04 19:23:53 +000046 const TextureSamplerArray& samplers) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000047 sk_ignore_unused_variable(inputColor);
48
bsalomon@google.com77af6802013-10-02 13:04:56 +000049 SkString coords2D = builder->ensureFSCoords2D(coords, 0);
humper@google.com3aad3b02013-09-04 19:23:53 +000050 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);
bsalomon@google.com77af6802013-10-02 13:04:56 +000076 builder->fsCodeAppendf("\tvec2 coord = %s - %s * vec2(0.5, 0.5);\n", coords2D.c_str(), imgInc);
humper@google.com3aad3b02013-09-04 19:23:53 +000077 builder->fsCodeAppendf("\tvec2 f = fract(coord / %s);\n", imgInc);
78 for (int y = 0; y < 4; ++y) {
79 for (int x = 0; x < 4; ++x) {
80 SkString coord;
81 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1);
82 builder->fsCodeAppendf("\tvec4 s%d%d = ", x, y);
83 builder->fsAppendTextureLookup(samplers[0], coord.c_str());
84 builder->fsCodeAppend(";\n");
85 }
86 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);
87 }
88 builder->fsCodeAppendf("\t%s = %s(%s, f.y, s0, s1, s2, s3);\n", outputColor, cubicBlendName.c_str(), coeff);
89}
90
humper@google.com3aad3b02013-09-04 19:23:53 +000091void GrGLBicubicEffect::setData(const GrGLUniformManager& uman,
92 const GrDrawEffect& drawEffect) {
93 const GrBicubicEffect& effect = drawEffect.castEffect<GrBicubicEffect>();
94 GrTexture& texture = *effect.texture(0);
95 float imageIncrement[2];
96 imageIncrement[0] = 1.0f / texture.width();
97 imageIncrement[1] = 1.0f / texture.height();
98 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement);
99 uman.setMatrix4f(fCoefficientsUni, effect.coefficients());
humper@google.com3aad3b02013-09-04 19:23:53 +0000100}
101
102GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
103 const SkScalar coefficients[16])
104 : INHERITED(texture, MakeDivByTextureWHMatrix(texture)) {
105 for (int y = 0; y < 4; y++) {
106 for (int x = 0; x < 4; x++) {
107 // Convert from row-major scalars to column-major floats.
108 fCoefficients[x * 4 + y] = SkScalarToFloat(coefficients[y * 4 + x]);
109 }
110 }
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000111 this->setWillNotUseInputColor();
humper@google.com3aad3b02013-09-04 19:23:53 +0000112}
113
114GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
115 const SkScalar coefficients[16],
116 const SkMatrix &matrix,
117 const GrTextureParams &params,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000118 GrCoordSet coordSet)
commit-bot@chromium.org759befb2013-10-23 16:18:50 +0000119 : INHERITED(texture, matrix, params, coordSet) {
humper@google.com3aad3b02013-09-04 19:23:53 +0000120 for (int y = 0; y < 4; y++) {
121 for (int x = 0; x < 4; x++) {
122 // Convert from row-major scalars to column-major floats.
123 fCoefficients[x * 4 + y] = SkScalarToFloat(coefficients[y * 4 + x]);
124 }
125 }
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000126 this->setWillNotUseInputColor();
humper@google.com3aad3b02013-09-04 19:23:53 +0000127}
128
129GrBicubicEffect::~GrBicubicEffect() {
130}
131
132const GrBackendEffectFactory& GrBicubicEffect::getFactory() const {
133 return GrTBackendEffectFactory<GrBicubicEffect>::getInstance();
134}
135
136bool GrBicubicEffect::onIsEqual(const GrEffect& sBase) const {
137 const GrBicubicEffect& s = CastEffect<GrBicubicEffect>(sBase);
humper@google.comd1af2372013-09-04 20:32:58 +0000138 return this->textureAccess(0) == s.textureAccess(0) &&
humper@google.com3aad3b02013-09-04 19:23:53 +0000139 !memcmp(fCoefficients, s.coefficients(), 16);
140}
141
142void GrBicubicEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
143 // FIXME: Perhaps we can do better.
144 *validFlags = 0;
145 return;
146}
147
148GR_DEFINE_EFFECT_TEST(GrBicubicEffect);
149
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000150GrEffectRef* GrBicubicEffect::TestCreate(SkRandom* random,
humper@google.com3aad3b02013-09-04 19:23:53 +0000151 GrContext* context,
152 const GrDrawTargetCaps&,
153 GrTexture* textures[]) {
154 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
155 GrEffectUnitTest::kAlphaTextureIdx;
156 SkScalar coefficients[16];
157 for (int i = 0; i < 16; i++) {
158 coefficients[i] = random->nextSScalar1();
159 }
160 return GrBicubicEffect::Create(textures[texIdx], coefficients);
161}