blob: b0abdcf9ed075ea228014dee56e4268be23dc25e [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
joshualitt30ba4362014-08-21 20:18:45 -07008#include "gl/builders/GrGLProgramBuilder.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00009#include "GrConvolutionEffect.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +000010#include "gl/GrGLEffect.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
kkinnunen7510b222014-07-30 00:04:16 -070016typedef GrGLProgramDataManager::UniformHandle UniformHandle;
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:
joshualitt49586be2014-09-16 08:21:41 -070020 GrGLConvolutionEffect(const GrBackendEffectFactory&, const GrEffect&);
bsalomon@google.comb505a122012-05-31 18:40:36 +000021
joshualitt30ba4362014-08-21 20:18:45 -070022 virtual void emitCode(GrGLProgramBuilder*,
joshualitt49586be2014-09-16 08:21:41 -070023 const GrEffect&,
bsalomon63e99f72014-07-21 08:03:14 -070024 const GrEffectKey&,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000025 const char* outputColor,
26 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000027 const TransformedCoordsArray&,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000028 const TextureSamplerArray&) SK_OVERRIDE;
bsalomon@google.comb505a122012-05-31 18:40:36 +000029
joshualitt49586be2014-09-16 08:21:41 -070030 virtual void setData(const GrGLProgramDataManager& pdman, const GrEffect&) SK_OVERRIDE;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000031
joshualitt49586be2014-09-16 08:21:41 -070032 static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuilder*);
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); }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000036 bool useBounds() const { return fUseBounds; }
37 Gr1DKernelEffect::Direction direction() const { return fDirection; }
bsalomon@google.comb505a122012-05-31 18:40:36 +000038
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000039 int fRadius;
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000040 bool fUseBounds;
41 Gr1DKernelEffect::Direction fDirection;
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000042 UniformHandle fKernelUni;
43 UniformHandle fImageIncrementUni;
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000044 UniformHandle fBoundsUni;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000045
bsalomon@google.com47d7a882012-10-29 12:47:51 +000046 typedef GrGLEffect INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000047};
48
bsalomon@google.com396e61f2012-10-25 19:00:29 +000049GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& factory,
joshualitt49586be2014-09-16 08:21:41 -070050 const GrEffect& effect)
bsalomon@google.com77af6802013-10-02 13:04:56 +000051 : INHERITED(factory) {
joshualitt49586be2014-09-16 08:21:41 -070052 const GrConvolutionEffect& c = effect.cast<GrConvolutionEffect>();
bsalomon@google.comb505a122012-05-31 18:40:36 +000053 fRadius = c.radius();
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000054 fUseBounds = c.useBounds();
55 fDirection = c.direction();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000056}
57
joshualitt30ba4362014-08-21 20:18:45 -070058void GrGLConvolutionEffect::emitCode(GrGLProgramBuilder* builder,
joshualitt49586be2014-09-16 08:21:41 -070059 const GrEffect&,
bsalomon63e99f72014-07-21 08:03:14 -070060 const GrEffectKey& key,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000061 const char* outputColor,
62 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000063 const TransformedCoordsArray& coords,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000064 const TextureSamplerArray& samplers) {
joshualitt30ba4362014-08-21 20:18:45 -070065 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::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()) {
joshualitt30ba4362014-08-21 20:18:45 -070068 fBoundsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000069 kVec2f_GrSLType, "Bounds");
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000070 }
joshualitt30ba4362014-08-21 20:18:45 -070071 fKernelUni = builder->addUniformArray(GrGLProgramBuilder::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
joshualitt30ba4362014-08-21 20:18:45 -070074 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
75 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0);
76
77 fsBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000078
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000079 int width = this->width();
bsalomon@google.com032b2212012-07-16 13:36:18 +000080 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
81 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000082
joshualitt30ba4362014-08-21 20:18:45 -070083 fsBuilder->codeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(), fRadius, imgInc);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000084
tomhudson@google.comf8a22892012-06-11 12:42:24 +000085 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000086 for (int i = 0; i < width; i++) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000087 SkString index;
88 SkString kernelIndex;
tomhudson@google.comf8a22892012-06-11 12:42:24 +000089 index.appendS32(i);
bsalomon@google.com032b2212012-07-16 13:36:18 +000090 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
joshualitt30ba4362014-08-21 20:18:45 -070091 fsBuilder->codeAppendf("\t\t%s += ", outputColor);
92 fsBuilder->appendTextureLookup(samplers[0], "coord");
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000093 if (this->useBounds()) {
94 const char* bounds = builder->getUniformCStr(fBoundsUni);
95 const char* component = this->direction() == Gr1DKernelEffect::kY_Direction ? "y" : "x";
joshualitt30ba4362014-08-21 20:18:45 -070096 fsBuilder->codeAppendf(" * float(coord.%s >= %s.x && coord.%s <= %s.y)",
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000097 component, bounds, component, bounds);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000098 }
joshualitt30ba4362014-08-21 20:18:45 -070099 fsBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str());
100 fsBuilder->codeAppendf("\t\tcoord += %s;\n", imgInc);
tomhudson@google.comf8a22892012-06-11 12:42:24 +0000101 }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000102
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000103 SkString modulate;
104 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
joshualitt30ba4362014-08-21 20:18:45 -0700105 fsBuilder->codeAppend(modulate.c_str());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000106}
107
kkinnunen7510b222014-07-30 00:04:16 -0700108void GrGLConvolutionEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt49586be2014-09-16 08:21:41 -0700109 const GrEffect& effect) {
110 const GrConvolutionEffect& conv = effect.cast<GrConvolutionEffect>();
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000111 GrTexture& texture = *conv.texture(0);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000112 // the code we generated was for a specific kernel radius
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000113 SkASSERT(conv.radius() == fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000114 float imageIncrement[2] = { 0 };
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000115 float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000116 switch (conv.direction()) {
117 case Gr1DKernelEffect::kX_Direction:
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000118 imageIncrement[0] = 1.0f / texture.width();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000119 break;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000120 case Gr1DKernelEffect::kY_Direction:
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000121 imageIncrement[1] = ySign / texture.height();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000122 break;
123 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000124 SkFAIL("Unknown filter direction.");
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000125 }
kkinnunen7510b222014-07-30 00:04:16 -0700126 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000127 if (conv.useBounds()) {
128 const float* bounds = conv.bounds();
129 if (Gr1DKernelEffect::kY_Direction == conv.direction() &&
130 texture.origin() != kTopLeft_GrSurfaceOrigin) {
kkinnunen7510b222014-07-30 00:04:16 -0700131 pdman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]);
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000132 } else {
kkinnunen7510b222014-07-30 00:04:16 -0700133 pdman.set2f(fBoundsUni, bounds[0], bounds[1]);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000134 }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000135 }
kkinnunen7510b222014-07-30 00:04:16 -0700136 pdman.set1fv(fKernelUni, this->width(), conv.kernel());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000137}
138
joshualitt49586be2014-09-16 08:21:41 -0700139void GrGLConvolutionEffect::GenKey(const GrEffect& effect, const GrGLCaps&,
bsalomon63e99f72014-07-21 08:03:14 -0700140 GrEffectKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -0700141 const GrConvolutionEffect& conv = effect.cast<GrConvolutionEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700142 uint32_t key = conv.radius();
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000143 key <<= 2;
144 if (conv.useBounds()) {
145 key |= 0x2;
146 key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0;
147 }
bsalomon63e99f72014-07-21 08:03:14 -0700148 b->add32(key);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000149}
150
bsalomon@google.comb505a122012-05-31 18:40:36 +0000151///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000152
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000153GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
154 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000155 int radius,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000156 const float* kernel,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000157 bool useBounds,
158 float bounds[2])
159 : Gr1DKernelEffect(texture, direction, radius), fUseBounds(useBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000160 SkASSERT(radius <= kMaxKernelRadius);
bsalomon49f085d2014-09-05 13:34:00 -0700161 SkASSERT(kernel);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000162 int width = this->width();
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000163 for (int i = 0; i < width; i++) {
164 fKernel[i] = kernel[i];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000165 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000166 memcpy(fBounds, bounds, sizeof(fBounds));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000167}
168
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000169GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
170 Direction direction,
171 int radius,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000172 float gaussianSigma,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000173 bool useBounds,
174 float bounds[2])
175 : Gr1DKernelEffect(texture, direction, radius), fUseBounds(useBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000176 SkASSERT(radius <= kMaxKernelRadius);
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000177 int width = this->width();
178
179 float sum = 0.0f;
180 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
181 for (int i = 0; i < width; ++i) {
182 float x = static_cast<float>(i - this->radius());
183 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
184 // is dropped here, since we renormalize the kernel below.
185 fKernel[i] = sk_float_exp(- x * x * denom);
186 sum += fKernel[i];
187 }
188 // Normalize the kernel
189 float scale = 1.0f / sum;
190 for (int i = 0; i < width; ++i) {
191 fKernel[i] *= scale;
192 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000193 memcpy(fBounds, bounds, sizeof(fBounds));
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000194}
195
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000196GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000197}
198
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000199const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const {
200 return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000201}
202
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000203bool GrConvolutionEffect::onIsEqual(const GrEffect& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700204 const GrConvolutionEffect& s = sBase.cast<GrConvolutionEffect>();
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000205 return (this->texture(0) == s.texture(0) &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000206 this->radius() == s.radius() &&
207 this->direction() == s.direction() &&
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000208 this->useBounds() == s.useBounds() &&
209 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000210 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000211}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000212
213///////////////////////////////////////////////////////////////////////////////
214
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000215GR_DEFINE_EFFECT_TEST(GrConvolutionEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000216
bsalomon83d081a2014-07-08 09:56:10 -0700217GrEffect* GrConvolutionEffect::TestCreate(SkRandom* random,
218 GrContext*,
219 const GrDrawTargetCaps&,
220 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000221 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
222 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000223 Direction dir = random->nextBool() ? kX_Direction : kY_Direction;
224 int radius = random->nextRangeU(1, kMaxKernelRadius);
bungeman@google.com43486632013-08-20 15:20:34 +0000225 float kernel[kMaxKernelWidth];
226 for (size_t i = 0; i < SK_ARRAY_COUNT(kernel); ++i) {
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000227 kernel[i] = random->nextSScalar1();
228 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000229 float bounds[2];
bungeman@google.com43486632013-08-20 15:20:34 +0000230 for (size_t i = 0; i < SK_ARRAY_COUNT(bounds); ++i) {
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000231 bounds[i] = random->nextF();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000232 }
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000233
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000234 bool useBounds = random->nextBool();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000235 return GrConvolutionEffect::Create(textures[texIdx],
236 dir,
237 radius,
238 kernel,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000239 useBounds,
240 bounds);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000241}