blob: 0ab215c12601236caed47eeb6926478dfa129185 [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.com396e61f2012-10-25 19:00:29 +000021 GrGLConvolutionEffect(const GrBackendEffectFactory&, const GrEffect&);
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.com021fc732012-10-25 12:47:42 +000047 const GrEffect& 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.comb505a122012-05-31 18:40:36 +000051 const GrConvolutionEffect& c =
bsalomon@google.com021fc732012-10-25 12:47:42 +000052 static_cast<const GrConvolutionEffect&>(effect);
bsalomon@google.comb505a122012-05-31 18:40:36 +000053 fRadius = c.radius();
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000054 fRequiresTextureMatrix = false;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000055}
56
bsalomon@google.com47d7a882012-10-29 12:47:51 +000057void GrGLConvolutionEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000058 const GrEffectStage&,
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000059 EffectKey key,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000060 const char* vertexCoords,
61 const char* outputColor,
62 const char* inputColor,
63 const TextureSamplerArray& samplers) {
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000064 const char* coords;
65 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000066 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000067 kVec2f_GrSLType, "ImageIncrement");
68 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
69 kFloat_GrSLType, "Kernel", this->width());
bsalomon@google.com032b2212012-07-16 13:36:18 +000070 SkString* code = &builder->fFSCode;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000071
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000072 code->appendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000073
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000074 int width = this ->width();
bsalomon@google.com032b2212012-07-16 13:36:18 +000075 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
76 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000077
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000078 code->appendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords, fRadius, imgInc);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000079
tomhudson@google.comf8a22892012-06-11 12:42:24 +000080 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000081 for (int i = 0; i < width; i++) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000082 SkString index;
83 SkString kernelIndex;
tomhudson@google.comf8a22892012-06-11 12:42:24 +000084 index.appendS32(i);
bsalomon@google.com032b2212012-07-16 13:36:18 +000085 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000086 code->appendf("\t\t%s += ", outputColor);
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000087 builder->appendTextureLookup(&builder->fFSCode, samplers[0], "coord");
tomhudson@google.comf8a22892012-06-11 12:42:24 +000088 code->appendf(" * %s;\n", kernelIndex.c_str());
bsalomon@google.com032b2212012-07-16 13:36:18 +000089 code->appendf("\t\tcoord += %s;\n", imgInc);
tomhudson@google.comf8a22892012-06-11 12:42:24 +000090 }
bsalomon@google.com868a8e72012-08-30 19:11:34 +000091 GrGLSLMulVarBy4f(&builder->fFSCode, 2, outputColor, inputColor);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000092}
93
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000094void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
95 const GrConvolutionEffect& conv = static_cast<const GrConvolutionEffect&>(*stage.getEffect());
96 GrTexture& texture = *conv.texture(0);
bsalomon@google.comb505a122012-05-31 18:40:36 +000097 // the code we generated was for a specific kernel radius
98 GrAssert(conv.radius() == fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000099 float imageIncrement[2] = { 0 };
bsalomon@google.comb505a122012-05-31 18:40:36 +0000100 switch (conv.direction()) {
101 case Gr1DKernelEffect::kX_Direction:
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000102 imageIncrement[0] = 1.0f / texture.width();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000103 break;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000104 case Gr1DKernelEffect::kY_Direction:
105 imageIncrement[1] = 1.0f / texture.height();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000106 break;
107 default:
108 GrCrash("Unknown filter direction.");
109 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000110 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement);
111 uman.set1fv(fKernelUni, 0, this->width(), conv.kernel());
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000112 fEffectMatrix.setData(uman, conv.getMatrix(), stage.getCoordChangeMatrix(), conv.texture(0));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000113}
114
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000115GrGLEffect::EffectKey GrGLConvolutionEffect::GenKey(const GrEffectStage& s, const GrGLCaps&) {
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000116 const GrConvolutionEffect& conv = static_cast<const GrConvolutionEffect&>(*s.getEffect());
117 EffectKey key = static_cast<const GrConvolutionEffect&>(*s.getEffect()).radius();
118 key <<= GrGLEffectMatrix::kKeyBits;
119 EffectKey matrixKey = GrGLEffectMatrix::GenKey(conv.getMatrix(),
120 s.getCoordChangeMatrix(),
121 conv.texture(0));
122 return key | matrixKey;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000123}
124
bsalomon@google.comb505a122012-05-31 18:40:36 +0000125///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000126
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000127GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
128 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000129 int radius,
130 const float* kernel)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000131 : Gr1DKernelEffect(texture, direction, radius) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000132 GrAssert(radius <= kMaxKernelRadius);
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000133 GrAssert(NULL != kernel);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000134 int width = this->width();
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000135 for (int i = 0; i < width; i++) {
136 fKernel[i] = kernel[i];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000137 }
138}
139
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000140GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
141 Direction direction,
142 int radius,
143 float gaussianSigma)
144 : Gr1DKernelEffect(texture, direction, radius) {
145 GrAssert(radius <= kMaxKernelRadius);
146 int width = this->width();
147
148 float sum = 0.0f;
149 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
150 for (int i = 0; i < width; ++i) {
151 float x = static_cast<float>(i - this->radius());
152 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
153 // is dropped here, since we renormalize the kernel below.
154 fKernel[i] = sk_float_exp(- x * x * denom);
155 sum += fKernel[i];
156 }
157 // Normalize the kernel
158 float scale = 1.0f / sum;
159 for (int i = 0; i < width; ++i) {
160 fKernel[i] *= scale;
161 }
162}
163
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000164GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000165}
166
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000167const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const {
168 return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000169}
170
bsalomon@google.coma469c282012-10-24 18:28:34 +0000171bool GrConvolutionEffect::isEqual(const GrEffect& sBase) const {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000172 const GrConvolutionEffect& s =
173 static_cast<const GrConvolutionEffect&>(sBase);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000174 return (INHERITED::isEqual(sBase) &&
175 this->radius() == s.radius() &&
176 this->direction() == s.direction() &&
177 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000178}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000179
180///////////////////////////////////////////////////////////////////////////////
181
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000182GR_DEFINE_EFFECT_TEST(GrConvolutionEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000183
bsalomon@google.coma469c282012-10-24 18:28:34 +0000184GrEffect* GrConvolutionEffect::TestCreate(SkRandom* random,
185 GrContext* context,
186 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000187 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
188 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000189 Direction dir = random->nextBool() ? kX_Direction : kY_Direction;
190 int radius = random->nextRangeU(1, kMaxKernelRadius);
191 float kernel[kMaxKernelRadius];
192 for (int i = 0; i < kMaxKernelRadius; ++i) {
193 kernel[i] = random->nextSScalar1();
194 }
195
196 return SkNEW_ARGS(GrConvolutionEffect, (textures[texIdx], dir, radius, kernel));
197}
198