blob: 1eb1fc25aedaba4dfacda3e834c22f4930804bab [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,
22 const TextureSamplerArray&) SK_OVERRIDE;
23
24 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
25
26 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
27
28private:
29 typedef GrGLUniformManager::UniformHandle UniformHandle;
30
31 UniformHandle fCoefficientsUni;
32 UniformHandle fImageIncrementUni;
33
34 GrGLEffectMatrix fEffectMatrix;
35
36 typedef GrGLEffect INHERITED;
37};
38
39GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory,
40 const GrDrawEffect& drawEffect)
41 : INHERITED(factory)
42 , fEffectMatrix(drawEffect.castEffect<GrBicubicEffect>().coordsType()) {
43}
44
45void GrGLBicubicEffect::emitCode(GrGLShaderBuilder* builder,
46 const GrDrawEffect&,
47 EffectKey key,
48 const char* outputColor,
49 const char* inputColor,
50 const TextureSamplerArray& samplers) {
51 SkString coords;
52 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords);
53 fCoefficientsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
54 kMat44f_GrSLType, "Coefficients");
55 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
56 kVec2f_GrSLType, "ImageIncrement");
57
58 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
59 const char* coeff = builder->getUniformCStr(fCoefficientsUni);
60
61 SkString cubicBlendName;
62
63 static const GrGLShaderVar gCubicBlendArgs[] = {
64 GrGLShaderVar("coefficients", kMat44f_GrSLType),
65 GrGLShaderVar("t", kFloat_GrSLType),
66 GrGLShaderVar("c0", kVec4f_GrSLType),
67 GrGLShaderVar("c1", kVec4f_GrSLType),
68 GrGLShaderVar("c2", kVec4f_GrSLType),
69 GrGLShaderVar("c3", kVec4f_GrSLType),
70 };
71 builder->fsEmitFunction(kVec4f_GrSLType,
72 "cubicBlend",
73 SK_ARRAY_COUNT(gCubicBlendArgs),
74 gCubicBlendArgs,
75 "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n"
76 "\tvec4 c = coefficients * ts;\n"
77 "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;\n",
78 &cubicBlendName);
79 builder->fsCodeAppendf("\tvec2 coord = %s - %s * vec2(0.5, 0.5);\n", coords.c_str(), imgInc);
80 builder->fsCodeAppendf("\tvec2 f = fract(coord / %s);\n", imgInc);
81 for (int y = 0; y < 4; ++y) {
82 for (int x = 0; x < 4; ++x) {
83 SkString coord;
84 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1);
85 builder->fsCodeAppendf("\tvec4 s%d%d = ", x, y);
86 builder->fsAppendTextureLookup(samplers[0], coord.c_str());
87 builder->fsCodeAppend(";\n");
88 }
89 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);
90 }
91 builder->fsCodeAppendf("\t%s = %s(%s, f.y, s0, s1, s2, s3);\n", outputColor, cubicBlendName.c_str(), coeff);
92}
93
94GrGLEffect::EffectKey GrGLBicubicEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
95 const GrBicubicEffect& bicubic = drawEffect.castEffect<GrBicubicEffect>();
96 EffectKey matrixKey = GrGLEffectMatrix::GenKey(bicubic.getMatrix(),
97 drawEffect,
98 bicubic.coordsType(),
99 bicubic.texture(0));
100 return matrixKey;
101}
102
103void GrGLBicubicEffect::setData(const GrGLUniformManager& uman,
104 const GrDrawEffect& drawEffect) {
105 const GrBicubicEffect& effect = drawEffect.castEffect<GrBicubicEffect>();
106 GrTexture& texture = *effect.texture(0);
107 float imageIncrement[2];
108 imageIncrement[0] = 1.0f / texture.width();
109 imageIncrement[1] = 1.0f / texture.height();
110 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement);
111 uman.setMatrix4f(fCoefficientsUni, effect.coefficients());
112 fEffectMatrix.setData(uman,
113 effect.getMatrix(),
114 drawEffect,
115 effect.texture(0));
116}
117
118GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
119 const SkScalar coefficients[16])
120 : INHERITED(texture, MakeDivByTextureWHMatrix(texture)) {
121 for (int y = 0; y < 4; y++) {
122 for (int x = 0; x < 4; x++) {
123 // Convert from row-major scalars to column-major floats.
124 fCoefficients[x * 4 + y] = SkScalarToFloat(coefficients[y * 4 + x]);
125 }
126 }
127}
128
129GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
130 const SkScalar coefficients[16],
131 const SkMatrix &matrix,
132 const GrTextureParams &params,
133 CoordsType coordsType)
134 : INHERITED(texture, MakeDivByTextureWHMatrix(texture), params, coordsType) {
135 for (int y = 0; y < 4; y++) {
136 for (int x = 0; x < 4; x++) {
137 // Convert from row-major scalars to column-major floats.
138 fCoefficients[x * 4 + y] = SkScalarToFloat(coefficients[y * 4 + x]);
139 }
140 }
141}
142
143GrBicubicEffect::~GrBicubicEffect() {
144}
145
146const GrBackendEffectFactory& GrBicubicEffect::getFactory() const {
147 return GrTBackendEffectFactory<GrBicubicEffect>::getInstance();
148}
149
150bool GrBicubicEffect::onIsEqual(const GrEffect& sBase) const {
151 const GrBicubicEffect& s = CastEffect<GrBicubicEffect>(sBase);
humper@google.comd1af2372013-09-04 20:32:58 +0000152 return this->textureAccess(0) == s.textureAccess(0) &&
humper@google.com3aad3b02013-09-04 19:23:53 +0000153 !memcmp(fCoefficients, s.coefficients(), 16);
154}
155
156void GrBicubicEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
157 // FIXME: Perhaps we can do better.
158 *validFlags = 0;
159 return;
160}
161
162GR_DEFINE_EFFECT_TEST(GrBicubicEffect);
163
164GrEffectRef* GrBicubicEffect::TestCreate(SkMWCRandom* random,
165 GrContext* context,
166 const GrDrawTargetCaps&,
167 GrTexture* textures[]) {
168 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
169 GrEffectUnitTest::kAlphaTextureIdx;
170 SkScalar coefficients[16];
171 for (int i = 0; i < 16; i++) {
172 coefficients[i] = random->nextSScalar1();
173 }
174 return GrBicubicEffect::Create(textures[texIdx], coefficients);
175}