blob: 57cdece9d8217f59e58b27c192a2dc3ac540b01f [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.com2eaaefd2012-10-29 19:51:22 +000012#include "GrTBackendEffectFactory.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;
bsalomon@google.com032b2212012-07-16 13:36:18 +000016
bsalomon@google.com47d7a882012-10-29 12:47:51 +000017class GrGLConvolutionEffect : public GrGLEffect {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000018public:
bsalomon@google.comc7818882013-03-20 19:19:53 +000019 GrGLConvolutionEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
bsalomon@google.comb505a122012-05-31 18:40:36 +000020
bsalomon@google.com47d7a882012-10-29 12:47:51 +000021 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.comc7818882013-03-20 19:19:53 +000022 const GrDrawEffect&,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000023 EffectKey,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000024 const char* outputColor,
25 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000026 const TransformedCoordsArray&,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000027 const TextureSamplerArray&) SK_OVERRIDE;
bsalomon@google.comb505a122012-05-31 18:40:36 +000028
bsalomon@google.comc7818882013-03-20 19:19:53 +000029 virtual void setData(const GrGLUniformManager& uman, const GrDrawEffect&) SK_OVERRIDE;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000030
bsalomon@google.comc7818882013-03-20 19:19:53 +000031 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000032
33private:
bsalomon@google.comb505a122012-05-31 18:40:36 +000034 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000035 bool useBounds() const { return fUseBounds; }
36 Gr1DKernelEffect::Direction direction() const { return fDirection; }
bsalomon@google.comb505a122012-05-31 18:40:36 +000037
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000038 int fRadius;
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000039 bool fUseBounds;
40 Gr1DKernelEffect::Direction fDirection;
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000041 UniformHandle fKernelUni;
42 UniformHandle fImageIncrementUni;
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000043 UniformHandle fBoundsUni;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000044
bsalomon@google.com47d7a882012-10-29 12:47:51 +000045 typedef GrGLEffect INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000046};
47
bsalomon@google.com396e61f2012-10-25 19:00:29 +000048GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& factory,
bsalomon@google.comc7818882013-03-20 19:19:53 +000049 const GrDrawEffect& drawEffect)
bsalomon@google.com77af6802013-10-02 13:04:56 +000050 : INHERITED(factory) {
bsalomon@google.comc7818882013-03-20 19:19:53 +000051 const GrConvolutionEffect& c = drawEffect.castEffect<GrConvolutionEffect>();
bsalomon@google.comb505a122012-05-31 18:40:36 +000052 fRadius = c.radius();
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000053 fUseBounds = c.useBounds();
54 fDirection = c.direction();
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.comc7818882013-03-20 19:19:53 +000058 const GrDrawEffect&,
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000059 EffectKey key,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000060 const char* outputColor,
61 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000062 const TransformedCoordsArray& coords,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000063 const TextureSamplerArray& samplers) {
bsalomon@google.com77af6802013-10-02 13:04:56 +000064 SkString coords2D = builder->ensureFSCoords2D(coords, 0);
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000065 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000066 kVec2f_GrSLType, "ImageIncrement");
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000067 if (this->useBounds()) {
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000068 fBoundsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000069 kVec2f_GrSLType, "Bounds");
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000070 }
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000071 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibility,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000072 kFloat_GrSLType, "Kernel", this->width());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000073
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000074 builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000075
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000076 int width = this->width();
bsalomon@google.com032b2212012-07-16 13:36:18 +000077 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
78 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000079
bsalomon@google.com77af6802013-10-02 13:04:56 +000080 builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(), fRadius, imgInc);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000081
tomhudson@google.comf8a22892012-06-11 12:42:24 +000082 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000083 for (int i = 0; i < width; i++) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000084 SkString index;
85 SkString kernelIndex;
tomhudson@google.comf8a22892012-06-11 12:42:24 +000086 index.appendS32(i);
bsalomon@google.com032b2212012-07-16 13:36:18 +000087 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000088 builder->fsCodeAppendf("\t\t%s += ", outputColor);
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000089 builder->fsAppendTextureLookup(samplers[0], "coord");
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000090 if (this->useBounds()) {
91 const char* bounds = builder->getUniformCStr(fBoundsUni);
92 const char* component = this->direction() == Gr1DKernelEffect::kY_Direction ? "y" : "x";
93 builder->fsCodeAppendf(" * float(coord.%s >= %s.x && coord.%s <= %s.y)",
94 component, bounds, component, bounds);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000095 }
robertphillips@google.com58c856a2013-07-24 13:18:06 +000096 builder->fsCodeAppendf(" * %s;\n", kernelIndex.c_str());
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000097 builder->fsCodeAppendf("\t\tcoord += %s;\n", imgInc);
tomhudson@google.comf8a22892012-06-11 12:42:24 +000098 }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000099
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000100 SkString modulate;
101 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
102 builder->fsCodeAppend(modulate.c_str());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000103}
104
bsalomon@google.comc7818882013-03-20 19:19:53 +0000105void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman,
106 const GrDrawEffect& drawEffect) {
107 const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>();
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000108 GrTexture& texture = *conv.texture(0);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000109 // the code we generated was for a specific kernel radius
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000110 SkASSERT(conv.radius() == fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000111 float imageIncrement[2] = { 0 };
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000112 float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000113 switch (conv.direction()) {
114 case Gr1DKernelEffect::kX_Direction:
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000115 imageIncrement[0] = 1.0f / texture.width();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000116 break;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000117 case Gr1DKernelEffect::kY_Direction:
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000118 imageIncrement[1] = ySign / texture.height();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000119 break;
120 default:
121 GrCrash("Unknown filter direction.");
122 }
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000123 uman.set2fv(fImageIncrementUni, 1, imageIncrement);
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000124 if (conv.useBounds()) {
125 const float* bounds = conv.bounds();
126 if (Gr1DKernelEffect::kY_Direction == conv.direction() &&
127 texture.origin() != kTopLeft_GrSurfaceOrigin) {
128 uman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]);
129 } else {
130 uman.set2f(fBoundsUni, bounds[0], bounds[1]);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000131 }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000132 }
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000133 uman.set1fv(fKernelUni, this->width(), conv.kernel());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000134}
135
bsalomon@google.comc7818882013-03-20 19:19:53 +0000136GrGLEffect::EffectKey GrGLConvolutionEffect::GenKey(const GrDrawEffect& drawEffect,
137 const GrGLCaps&) {
138 const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>();
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000139 EffectKey key = conv.radius();
140 key <<= 2;
141 if (conv.useBounds()) {
142 key |= 0x2;
143 key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0;
144 }
bsalomon@google.com77af6802013-10-02 13:04:56 +0000145 return key;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000146}
147
bsalomon@google.comb505a122012-05-31 18:40:36 +0000148///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000149
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000150GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
151 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000152 int radius,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000153 const float* kernel,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000154 bool useBounds,
155 float bounds[2])
156 : Gr1DKernelEffect(texture, direction, radius), fUseBounds(useBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000157 SkASSERT(radius <= kMaxKernelRadius);
158 SkASSERT(NULL != kernel);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000159 int width = this->width();
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000160 for (int i = 0; i < width; i++) {
161 fKernel[i] = kernel[i];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000162 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000163 memcpy(fBounds, bounds, sizeof(fBounds));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000164}
165
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000166GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
167 Direction direction,
168 int radius,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000169 float gaussianSigma,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000170 bool useBounds,
171 float bounds[2])
172 : Gr1DKernelEffect(texture, direction, radius), fUseBounds(useBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000173 SkASSERT(radius <= kMaxKernelRadius);
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000174 int width = this->width();
175
176 float sum = 0.0f;
177 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
178 for (int i = 0; i < width; ++i) {
179 float x = static_cast<float>(i - this->radius());
180 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
181 // is dropped here, since we renormalize the kernel below.
182 fKernel[i] = sk_float_exp(- x * x * denom);
183 sum += fKernel[i];
184 }
185 // Normalize the kernel
186 float scale = 1.0f / sum;
187 for (int i = 0; i < width; ++i) {
188 fKernel[i] *= scale;
189 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000190 memcpy(fBounds, bounds, sizeof(fBounds));
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000191}
192
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000193GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000194}
195
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000196const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const {
197 return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000198}
199
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000200bool GrConvolutionEffect::onIsEqual(const GrEffect& sBase) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000201 const GrConvolutionEffect& s = CastEffect<GrConvolutionEffect>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000202 return (this->texture(0) == s.texture(0) &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000203 this->radius() == s.radius() &&
204 this->direction() == s.direction() &&
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000205 this->useBounds() == s.useBounds() &&
206 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000207 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000208}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000209
210///////////////////////////////////////////////////////////////////////////////
211
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000212GR_DEFINE_EFFECT_TEST(GrConvolutionEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000213
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000214GrEffectRef* GrConvolutionEffect::TestCreate(SkRandom* random,
sugoi@google.come0e385c2013-03-11 18:50:03 +0000215 GrContext*,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000216 const GrDrawTargetCaps&,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000217 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000218 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
219 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000220 Direction dir = random->nextBool() ? kX_Direction : kY_Direction;
221 int radius = random->nextRangeU(1, kMaxKernelRadius);
bungeman@google.com43486632013-08-20 15:20:34 +0000222 float kernel[kMaxKernelWidth];
223 for (size_t i = 0; i < SK_ARRAY_COUNT(kernel); ++i) {
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000224 kernel[i] = random->nextSScalar1();
225 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000226 float bounds[2];
bungeman@google.com43486632013-08-20 15:20:34 +0000227 for (size_t i = 0; i < SK_ARRAY_COUNT(bounds); ++i) {
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000228 bounds[i] = random->nextF();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000229 }
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000230
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000231 bool useBounds = random->nextBool();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000232 return GrConvolutionEffect::Create(textures[texIdx],
233 dir,
234 radius,
235 kernel,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000236 useBounds,
237 bounds);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000238}