blob: f5b5e22ce15dfe127bb4b32308eb086fbe0477a8 [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"
joshualittb0a8a372014-09-23 09:50:21 -07009#include "gl/GrGLProcessor.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000010#include "gl/GrGLTexture.h"
joshualitteb2a6762014-12-04 11:35:33 -080011#include "gl/builders/GrGLProgramBuilder.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000012
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000013// For brevity
kkinnunen7510b222014-07-30 00:04:16 -070014typedef GrGLProgramDataManager::UniformHandle UniformHandle;
bsalomon@google.com032b2212012-07-16 13:36:18 +000015
joshualittb0a8a372014-09-23 09:50:21 -070016class GrGLConvolutionEffect : public GrGLFragmentProcessor {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000017public:
joshualitteb2a6762014-12-04 11:35:33 -080018 GrGLConvolutionEffect(const GrProcessor&);
ericrk7a787b42015-07-21 14:06:16 -070019
20 virtual void emitCode(GrGLFPBuilder*,
21 const GrFragmentProcessor&,
22 const char* outputColor,
23 const char* inputColor,
24 const TransformedCoordsArray&,
25 const TextureSamplerArray&) override;
26
27 void setData(const GrGLProgramDataManager& pdman, const GrProcessor&) override;
28
jvanverthcfc18862015-04-28 08:48:20 -070029 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000030
ericrk0f386122015-07-21 13:15:47 -070031private:
ericrk7a787b42015-07-21 14:06:16 -070032 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); }
33 bool useBounds() const { return fUseBounds; }
34 Gr1DKernelEffect::Direction direction() const { return fDirection; }
35
36 int fRadius;
37 bool fUseBounds;
38 Gr1DKernelEffect::Direction fDirection;
39 UniformHandle fKernelUni;
40 UniformHandle fImageIncrementUni;
41 UniformHandle fBoundsUni;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000042
joshualittb0a8a372014-09-23 09:50:21 -070043 typedef GrGLFragmentProcessor INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000044};
45
joshualitteb2a6762014-12-04 11:35:33 -080046GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -070047 const GrConvolutionEffect& c = processor.cast<GrConvolutionEffect>();
bsalomon@google.comb505a122012-05-31 18:40:36 +000048 fRadius = c.radius();
ericrk7a787b42015-07-21 14:06:16 -070049 fUseBounds = c.useBounds();
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000050 fDirection = c.direction();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000051}
52
ericrk7a787b42015-07-21 14:06:16 -070053void GrGLConvolutionEffect::emitCode(GrGLFPBuilder* builder,
54 const GrFragmentProcessor&,
55 const char* outputColor,
56 const char* inputColor,
57 const TransformedCoordsArray& coords,
58 const TextureSamplerArray& samplers) {
59 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
60 kVec2f_GrSLType, kDefault_GrSLPrecision,
61 "ImageIncrement");
62 if (this->useBounds()) {
63 fBoundsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
64 kVec2f_GrSLType, kDefault_GrSLPrecision,
65 "Bounds");
66 }
67 fKernelUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility,
68 kFloat_GrSLType, kDefault_GrSLPrecision,
69 "Kernel", this->width());
70
71 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
72 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0);
73
74 fsBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
75
76 int width = this->width();
77 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
78 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
79
80 fsBuilder->codeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(), fRadius, imgInc);
81
82 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
83 for (int i = 0; i < width; i++) {
84 SkString index;
85 SkString kernelIndex;
86 index.appendS32(i);
87 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
88
89 if (this->useBounds()) {
90 // We used to compute a bool indicating whether we're in bounds or not, cast it to a
91 // float, and then mul weight*texture_sample by the float. However, the Adreno 430 seems
92 // to have a bug that caused corruption.
93 const char* bounds = builder->getUniformCStr(fBoundsUni);
94 const char* component = this->direction() == Gr1DKernelEffect::kY_Direction ? "y" : "x";
95 fsBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y) {",
96 component, bounds, component, bounds);
97 }
98 fsBuilder->codeAppendf("\t\t%s += ", outputColor);
99 fsBuilder->appendTextureLookup(samplers[0], "coord");
100 fsBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str());
101 if (this->useBounds()) {
102 fsBuilder->codeAppend("}");
103 }
104 fsBuilder->codeAppendf("\t\tcoord += %s;\n", imgInc);
105 }
106
107 SkString modulate;
108 GrGLSLMulVarBy4f(&modulate, outputColor, inputColor);
109 fsBuilder->codeAppend(modulate.c_str());
110}
111
112void GrGLConvolutionEffect::setData(const GrGLProgramDataManager& pdman,
113 const GrProcessor& processor) {
114 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>();
115 GrTexture& texture = *conv.texture(0);
116 // the code we generated was for a specific kernel radius
117 SkASSERT(conv.radius() == fRadius);
118 float imageIncrement[2] = { 0 };
119 float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
120 switch (conv.direction()) {
121 case Gr1DKernelEffect::kX_Direction:
122 imageIncrement[0] = 1.0f / texture.width();
123 break;
124 case Gr1DKernelEffect::kY_Direction:
125 imageIncrement[1] = ySign / texture.height();
126 break;
127 default:
128 SkFAIL("Unknown filter direction.");
129 }
130 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
131 if (conv.useBounds()) {
132 const float* bounds = conv.bounds();
133 if (Gr1DKernelEffect::kY_Direction == conv.direction() &&
134 texture.origin() != kTopLeft_GrSurfaceOrigin) {
135 pdman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]);
136 } else {
137 pdman.set2f(fBoundsUni, bounds[0], bounds[1]);
138 }
139 }
140 pdman.set1fv(fKernelUni, this->width(), conv.kernel());
141}
142
143void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700144 GrProcessorKeyBuilder* b) {
145 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700146 uint32_t key = conv.radius();
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000147 key <<= 2;
148 if (conv.useBounds()) {
149 key |= 0x2;
150 key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0;
151 }
bsalomon63e99f72014-07-21 08:03:14 -0700152 b->add32(key);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000153}
154
bsalomon@google.comb505a122012-05-31 18:40:36 +0000155///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000156
joshualitt5f10b5c2015-07-09 10:24:35 -0700157GrConvolutionEffect::GrConvolutionEffect(GrProcessorDataManager* procDataManager,
158 GrTexture* texture,
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000159 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000160 int radius,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000161 const float* kernel,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000162 bool useBounds,
163 float bounds[2])
ericrk7a787b42015-07-21 14:06:16 -0700164 : INHERITED(procDataManager, texture, direction, radius), fUseBounds(useBounds) {
joshualitteb2a6762014-12-04 11:35:33 -0800165 this->initClassID<GrConvolutionEffect>();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000166 SkASSERT(radius <= kMaxKernelRadius);
bsalomon49f085d2014-09-05 13:34:00 -0700167 SkASSERT(kernel);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000168 int width = this->width();
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000169 for (int i = 0; i < width; i++) {
170 fKernel[i] = kernel[i];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000171 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000172 memcpy(fBounds, bounds, sizeof(fBounds));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000173}
174
joshualitt5f10b5c2015-07-09 10:24:35 -0700175GrConvolutionEffect::GrConvolutionEffect(GrProcessorDataManager* procDataManager,
176 GrTexture* texture,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000177 Direction direction,
178 int radius,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000179 float gaussianSigma,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000180 bool useBounds,
181 float bounds[2])
ericrk7a787b42015-07-21 14:06:16 -0700182 : INHERITED(procDataManager, texture, direction, radius), fUseBounds(useBounds) {
joshualitteb2a6762014-12-04 11:35:33 -0800183 this->initClassID<GrConvolutionEffect>();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000184 SkASSERT(radius <= kMaxKernelRadius);
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000185 int width = this->width();
186
187 float sum = 0.0f;
188 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
189 for (int i = 0; i < width; ++i) {
190 float x = static_cast<float>(i - this->radius());
191 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
192 // is dropped here, since we renormalize the kernel below.
193 fKernel[i] = sk_float_exp(- x * x * denom);
194 sum += fKernel[i];
195 }
196 // Normalize the kernel
197 float scale = 1.0f / sum;
198 for (int i = 0; i < width; ++i) {
199 fKernel[i] *= scale;
200 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000201 memcpy(fBounds, bounds, sizeof(fBounds));
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000202}
203
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000204GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000205}
206
jvanverthcfc18862015-04-28 08:48:20 -0700207void GrConvolutionEffect::getGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800208 GrProcessorKeyBuilder* b) const {
209 GrGLConvolutionEffect::GenKey(*this, caps, b);
210}
211
212GrGLFragmentProcessor* GrConvolutionEffect::createGLInstance() const {
ericrk7a787b42015-07-21 14:06:16 -0700213 return SkNEW_ARGS(GrGLConvolutionEffect, (*this));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000214}
215
bsalomon0e08fc12014-10-15 08:19:04 -0700216bool GrConvolutionEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700217 const GrConvolutionEffect& s = sBase.cast<GrConvolutionEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700218 return (this->radius() == s.radius() &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000219 this->direction() == s.direction() &&
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000220 this->useBounds() == s.useBounds() &&
221 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000222 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000223}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000224
225///////////////////////////////////////////////////////////////////////////////
226
joshualittb0a8a372014-09-23 09:50:21 -0700227GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvolutionEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000228
joshualitt0067ff52015-07-08 14:26:19 -0700229GrFragmentProcessor* GrConvolutionEffect::TestCreate(GrProcessorTestData* d) {
230 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
231 GrProcessorUnitTest::kAlphaTextureIdx;
232 Direction dir = d->fRandom->nextBool() ? kX_Direction : kY_Direction;
233 int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius);
bungeman@google.com43486632013-08-20 15:20:34 +0000234 float kernel[kMaxKernelWidth];
235 for (size_t i = 0; i < SK_ARRAY_COUNT(kernel); ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700236 kernel[i] = d->fRandom->nextSScalar1();
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000237 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000238 float bounds[2];
bungeman@google.com43486632013-08-20 15:20:34 +0000239 for (size_t i = 0; i < SK_ARRAY_COUNT(bounds); ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700240 bounds[i] = d->fRandom->nextF();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000241 }
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000242
joshualitt0067ff52015-07-08 14:26:19 -0700243 bool useBounds = d->fRandom->nextBool();
joshualitt5f10b5c2015-07-09 10:24:35 -0700244 return GrConvolutionEffect::Create(d->fProcDataManager,
245 d->fTextures[texIdx],
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000246 dir,
247 radius,
248 kernel,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000249 useBounds,
250 bounds);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000251}