blob: 794a9367df63f15b3c5a314decd1c10706463f65 [file] [log] [blame]
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrConvolutionEffect.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +00009#include "gl/GrGLEffect.h"
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000010#include "gl/GrGLEffectMatrix.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000011#include "gl/GrGLSL.h"
12#include "gl/GrGLTexture.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000013#include "GrTBackendEffectFactory.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000014
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000015// For brevity
16typedef GrGLUniformManager::UniformHandle UniformHandle;
17static const UniformHandle kInvalidUniformHandle = GrGLUniformManager::kInvalidUniformHandle;
bsalomon@google.com032b2212012-07-16 13:36:18 +000018
bsalomon@google.com47d7a882012-10-29 12:47:51 +000019class GrGLConvolutionEffect : public GrGLEffect {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000020public:
bsalomon@google.com6340a412013-01-22 19:55:59 +000021 GrGLConvolutionEffect(const GrBackendEffectFactory&, const GrEffectRef&);
bsalomon@google.comb505a122012-05-31 18:40:36 +000022
bsalomon@google.com47d7a882012-10-29 12:47:51 +000023 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000024 const GrEffectStage&,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000025 EffectKey,
26 const char* vertexCoords,
27 const char* outputColor,
28 const char* inputColor,
29 const TextureSamplerArray&) SK_OVERRIDE;
bsalomon@google.comb505a122012-05-31 18:40:36 +000030
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000031 virtual void setData(const GrGLUniformManager& uman, const GrEffectStage&) SK_OVERRIDE;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000032
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000033 static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000034
35private:
bsalomon@google.comb505a122012-05-31 18:40:36 +000036 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); }
37
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000038 int fRadius;
39 UniformHandle fKernelUni;
40 UniformHandle fImageIncrementUni;
41 GrGLEffectMatrix fEffectMatrix;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000042
bsalomon@google.com47d7a882012-10-29 12:47:51 +000043 typedef GrGLEffect INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000044};
45
bsalomon@google.com396e61f2012-10-25 19:00:29 +000046GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& factory,
bsalomon@google.com6340a412013-01-22 19:55:59 +000047 const GrEffectRef& effect)
bsalomon@google.com374e7592012-10-23 17:30:45 +000048 : INHERITED(factory)
bsalomon@google.com032b2212012-07-16 13:36:18 +000049 , fKernelUni(kInvalidUniformHandle)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000050 , fImageIncrementUni(kInvalidUniformHandle) {
bsalomon@google.com6340a412013-01-22 19:55:59 +000051 const GrConvolutionEffect& c = CastEffect<GrConvolutionEffect>(effect);
bsalomon@google.comb505a122012-05-31 18:40:36 +000052 fRadius = c.radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000053}
54
bsalomon@google.com47d7a882012-10-29 12:47:51 +000055void GrGLConvolutionEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000056 const GrEffectStage&,
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000057 EffectKey key,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000058 const char* vertexCoords,
59 const char* outputColor,
60 const char* inputColor,
61 const TextureSamplerArray& samplers) {
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000062 const char* coords;
63 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000064 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000065 kVec2f_GrSLType, "ImageIncrement");
66 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
67 kFloat_GrSLType, "Kernel", this->width());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000068
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000069 builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000070
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000071 int width = this ->width();
bsalomon@google.com032b2212012-07-16 13:36:18 +000072 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
73 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000074
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000075 builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords, fRadius, imgInc);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000076
tomhudson@google.comf8a22892012-06-11 12:42:24 +000077 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000078 for (int i = 0; i < width; i++) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000079 SkString index;
80 SkString kernelIndex;
tomhudson@google.comf8a22892012-06-11 12:42:24 +000081 index.appendS32(i);
bsalomon@google.com032b2212012-07-16 13:36:18 +000082 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000083 builder->fsCodeAppendf("\t\t%s += ", outputColor);
84 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, samplers[0], "coord");
85 builder->fsCodeAppendf(" * %s;\n", kernelIndex.c_str());
86 builder->fsCodeAppendf("\t\tcoord += %s;\n", imgInc);
tomhudson@google.comf8a22892012-06-11 12:42:24 +000087 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000088 SkString modulate;
89 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
90 builder->fsCodeAppend(modulate.c_str());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000091}
92
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000093void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
bsalomon@google.com6340a412013-01-22 19:55:59 +000094 const GrConvolutionEffect& conv = GetEffectFromStage<GrConvolutionEffect>(stage);
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000095 GrTexture& texture = *conv.texture(0);
bsalomon@google.comb505a122012-05-31 18:40:36 +000096 // the code we generated was for a specific kernel radius
97 GrAssert(conv.radius() == fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000098 float imageIncrement[2] = { 0 };
bsalomon@google.comb505a122012-05-31 18:40:36 +000099 switch (conv.direction()) {
100 case Gr1DKernelEffect::kX_Direction:
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000101 imageIncrement[0] = 1.0f / texture.width();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000102 break;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000103 case Gr1DKernelEffect::kY_Direction:
104 imageIncrement[1] = 1.0f / texture.height();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000105 break;
106 default:
107 GrCrash("Unknown filter direction.");
108 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000109 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement);
110 uman.set1fv(fKernelUni, 0, this->width(), conv.kernel());
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000111 fEffectMatrix.setData(uman, conv.getMatrix(), stage.getCoordChangeMatrix(), conv.texture(0));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000112}
113
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000114GrGLEffect::EffectKey GrGLConvolutionEffect::GenKey(const GrEffectStage& s, const GrGLCaps&) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000115 const GrConvolutionEffect& conv = GetEffectFromStage<GrConvolutionEffect>(s);
116 EffectKey key = conv.radius();
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000117 key <<= GrGLEffectMatrix::kKeyBits;
118 EffectKey matrixKey = GrGLEffectMatrix::GenKey(conv.getMatrix(),
119 s.getCoordChangeMatrix(),
120 conv.texture(0));
121 return key | matrixKey;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000122}
123
bsalomon@google.comb505a122012-05-31 18:40:36 +0000124///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000125
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000126GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
127 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000128 int radius,
129 const float* kernel)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000130 : Gr1DKernelEffect(texture, direction, radius) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000131 GrAssert(radius <= kMaxKernelRadius);
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000132 GrAssert(NULL != kernel);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000133 int width = this->width();
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000134 for (int i = 0; i < width; i++) {
135 fKernel[i] = kernel[i];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000136 }
137}
138
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000139GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
140 Direction direction,
141 int radius,
142 float gaussianSigma)
143 : Gr1DKernelEffect(texture, direction, radius) {
144 GrAssert(radius <= kMaxKernelRadius);
145 int width = this->width();
146
147 float sum = 0.0f;
148 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
149 for (int i = 0; i < width; ++i) {
150 float x = static_cast<float>(i - this->radius());
151 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
152 // is dropped here, since we renormalize the kernel below.
153 fKernel[i] = sk_float_exp(- x * x * denom);
154 sum += fKernel[i];
155 }
156 // Normalize the kernel
157 float scale = 1.0f / sum;
158 for (int i = 0; i < width; ++i) {
159 fKernel[i] *= scale;
160 }
161}
162
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000163GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000164}
165
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000166const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const {
167 return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000168}
169
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000170bool GrConvolutionEffect::onIsEqual(const GrEffect& sBase) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000171 const GrConvolutionEffect& s = CastEffect<GrConvolutionEffect>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000172 return (this->texture(0) == s.texture(0) &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000173 this->radius() == s.radius() &&
174 this->direction() == s.direction() &&
175 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000176}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000177
178///////////////////////////////////////////////////////////////////////////////
179
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000180GR_DEFINE_EFFECT_TEST(GrConvolutionEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000181
bsalomon@google.com73a96942013-02-13 16:31:19 +0000182GrEffectRef* GrConvolutionEffect::TestCreate(SkMWCRandom* random,
sugoi@google.come0e385c2013-03-11 18:50:03 +0000183 GrContext*,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000184 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000185 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
186 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000187 Direction dir = random->nextBool() ? kX_Direction : kY_Direction;
188 int radius = random->nextRangeU(1, kMaxKernelRadius);
189 float kernel[kMaxKernelRadius];
190 for (int i = 0; i < kMaxKernelRadius; ++i) {
191 kernel[i] = random->nextSScalar1();
192 }
193
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000194 return GrConvolutionEffect::Create(textures[texIdx], dir, radius,kernel);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000195}