blob: 5e0c7f6f5f954d0220c4eb2266d4b99307afbf0e [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"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000010#include "gl/GrGLSL.h"
11#include "gl/GrGLTexture.h"
bsalomon@google.com396e61f2012-10-25 19:00:29 +000012#include "GrBackendEffectFactory.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000013
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000014// For brevity
15typedef GrGLUniformManager::UniformHandle UniformHandle;
16static const UniformHandle kInvalidUniformHandle = GrGLUniformManager::kInvalidUniformHandle;
bsalomon@google.com032b2212012-07-16 13:36:18 +000017
bsalomon@google.com47d7a882012-10-29 12:47:51 +000018class GrGLConvolutionEffect : public GrGLEffect {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000019public:
bsalomon@google.com396e61f2012-10-25 19:00:29 +000020 GrGLConvolutionEffect(const GrBackendEffectFactory&, const GrEffect&);
bsalomon@google.comb505a122012-05-31 18:40:36 +000021
bsalomon@google.com47d7a882012-10-29 12:47:51 +000022 virtual void emitCode(GrGLShaderBuilder*,
23 const GrEffect&,
24 EffectKey,
25 const char* vertexCoords,
26 const char* outputColor,
27 const char* inputColor,
28 const TextureSamplerArray&) SK_OVERRIDE;
bsalomon@google.comb505a122012-05-31 18:40:36 +000029
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000030 virtual void setData(const GrGLUniformManager& uman, const GrEffectStage&) SK_OVERRIDE;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000031
bsalomon@google.com46fba0d2012-10-25 21:42:05 +000032 static inline EffectKey GenKey(const GrEffect&, const GrGLCaps&);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000033
34private:
bsalomon@google.comb505a122012-05-31 18:40:36 +000035 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); }
36
bsalomon@google.com032b2212012-07-16 13:36:18 +000037 int fRadius;
38 UniformHandle fKernelUni;
bsalomon@google.com032b2212012-07-16 13:36:18 +000039 UniformHandle fImageIncrementUni;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000040
bsalomon@google.com47d7a882012-10-29 12:47:51 +000041 typedef GrGLEffect INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000042};
43
bsalomon@google.com396e61f2012-10-25 19:00:29 +000044GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& factory,
bsalomon@google.com021fc732012-10-25 12:47:42 +000045 const GrEffect& effect)
bsalomon@google.com374e7592012-10-23 17:30:45 +000046 : INHERITED(factory)
bsalomon@google.com032b2212012-07-16 13:36:18 +000047 , fKernelUni(kInvalidUniformHandle)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000048 , fImageIncrementUni(kInvalidUniformHandle) {
bsalomon@google.comb505a122012-05-31 18:40:36 +000049 const GrConvolutionEffect& c =
bsalomon@google.com021fc732012-10-25 12:47:42 +000050 static_cast<const GrConvolutionEffect&>(effect);
bsalomon@google.comb505a122012-05-31 18:40:36 +000051 fRadius = c.radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000052}
53
bsalomon@google.com47d7a882012-10-29 12:47:51 +000054void GrGLConvolutionEffect::emitCode(GrGLShaderBuilder* builder,
55 const GrEffect&,
56 EffectKey,
57 const char* vertexCoords,
58 const char* outputColor,
59 const char* inputColor,
60 const TextureSamplerArray& samplers) {
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000061 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000062 kVec2f_GrSLType, "ImageIncrement");
63 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
64 kFloat_GrSLType, "Kernel", this->width());
bsalomon@google.com032b2212012-07-16 13:36:18 +000065 SkString* code = &builder->fFSCode;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000066
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000067 code->appendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000068
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000069 int width = this ->width();
bsalomon@google.com032b2212012-07-16 13:36:18 +000070 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
71 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000072
bsalomon@google.com5c33b712012-08-03 18:22:53 +000073 code->appendf("\t\tvec2 coord = %s - %d.0 * %s;\n",
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000074 builder->defaultTexCoordsName(), fRadius, imgInc);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000075
tomhudson@google.comf8a22892012-06-11 12:42:24 +000076 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000077 for (int i = 0; i < width; i++) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000078 SkString index;
79 SkString kernelIndex;
tomhudson@google.comf8a22892012-06-11 12:42:24 +000080 index.appendS32(i);
bsalomon@google.com032b2212012-07-16 13:36:18 +000081 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000082 code->appendf("\t\t%s += ", outputColor);
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000083 builder->appendTextureLookup(&builder->fFSCode, samplers[0], "coord");
tomhudson@google.comf8a22892012-06-11 12:42:24 +000084 code->appendf(" * %s;\n", kernelIndex.c_str());
bsalomon@google.com032b2212012-07-16 13:36:18 +000085 code->appendf("\t\tcoord += %s;\n", imgInc);
tomhudson@google.comf8a22892012-06-11 12:42:24 +000086 }
bsalomon@google.com868a8e72012-08-30 19:11:34 +000087 GrGLSLMulVarBy4f(&builder->fFSCode, 2, outputColor, inputColor);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000088}
89
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000090void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
91 const GrConvolutionEffect& conv = static_cast<const GrConvolutionEffect&>(*stage.getEffect());
92 GrTexture& texture = *conv.texture(0);
bsalomon@google.comb505a122012-05-31 18:40:36 +000093 // the code we generated was for a specific kernel radius
94 GrAssert(conv.radius() == fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000095 float imageIncrement[2] = { 0 };
bsalomon@google.comb505a122012-05-31 18:40:36 +000096 switch (conv.direction()) {
97 case Gr1DKernelEffect::kX_Direction:
tomhudson@google.com6a820b62012-05-24 15:10:14 +000098 imageIncrement[0] = 1.0f / texture.width();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000099 break;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000100 case Gr1DKernelEffect::kY_Direction:
101 imageIncrement[1] = 1.0f / texture.height();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000102 break;
103 default:
104 GrCrash("Unknown filter direction.");
105 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000106 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement);
107 uman.set1fv(fKernelUni, 0, this->width(), conv.kernel());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000108}
109
bsalomon@google.com46fba0d2012-10-25 21:42:05 +0000110GrGLEffect::EffectKey GrGLConvolutionEffect::GenKey(const GrEffect& s,
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000111 const GrGLCaps& caps) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000112 return static_cast<const GrConvolutionEffect&>(s).radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000113}
114
bsalomon@google.comb505a122012-05-31 18:40:36 +0000115///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000116
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000117GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
118 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000119 int radius,
120 const float* kernel)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000121 : Gr1DKernelEffect(texture, direction, radius) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000122 GrAssert(radius <= kMaxKernelRadius);
123 int width = this->width();
124 if (NULL != kernel) {
125 for (int i = 0; i < width; i++) {
126 fKernel[i] = kernel[i];
127 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000128 }
129}
130
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000131GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
132 Direction direction,
133 int radius,
134 float gaussianSigma)
135 : Gr1DKernelEffect(texture, direction, radius) {
136 GrAssert(radius <= kMaxKernelRadius);
137 int width = this->width();
138
139 float sum = 0.0f;
140 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
141 for (int i = 0; i < width; ++i) {
142 float x = static_cast<float>(i - this->radius());
143 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
144 // is dropped here, since we renormalize the kernel below.
145 fKernel[i] = sk_float_exp(- x * x * denom);
146 sum += fKernel[i];
147 }
148 // Normalize the kernel
149 float scale = 1.0f / sum;
150 for (int i = 0; i < width; ++i) {
151 fKernel[i] *= scale;
152 }
153}
154
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000155GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000156}
157
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000158const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const {
159 return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000160}
161
bsalomon@google.coma469c282012-10-24 18:28:34 +0000162bool GrConvolutionEffect::isEqual(const GrEffect& sBase) const {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000163 const GrConvolutionEffect& s =
164 static_cast<const GrConvolutionEffect&>(sBase);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000165 return (INHERITED::isEqual(sBase) &&
166 this->radius() == s.radius() &&
167 this->direction() == s.direction() &&
168 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000169}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000170
171///////////////////////////////////////////////////////////////////////////////
172
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000173GR_DEFINE_EFFECT_TEST(GrConvolutionEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000174
bsalomon@google.coma469c282012-10-24 18:28:34 +0000175GrEffect* GrConvolutionEffect::TestCreate(SkRandom* random,
176 GrContext* context,
177 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000178 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
179 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000180 Direction dir = random->nextBool() ? kX_Direction : kY_Direction;
181 int radius = random->nextRangeU(1, kMaxKernelRadius);
182 float kernel[kMaxKernelRadius];
183 for (int i = 0; i < kMaxKernelRadius; ++i) {
184 kernel[i] = random->nextSScalar1();
185 }
186
187 return SkNEW_ARGS(GrConvolutionEffect, (textures[texIdx], dir, radius, kernel));
188}
189