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