blob: 96d1578f08932e990ffc8419da91d0d4925cbb68 [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"
wangyix6af0c932015-07-22 10:21:17 -07009#include "gl/GrGLFragmentProcessor.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"
egdaniel018fb622015-10-28 07:26:40 -070012#include "glsl/GrGLSLProgramDataManager.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000013
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000014// For brevity
egdaniel018fb622015-10-28 07:26:40 -070015typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
bsalomon@google.com032b2212012-07-16 13:36:18 +000016
joshualittb0a8a372014-09-23 09:50:21 -070017class GrGLConvolutionEffect : public GrGLFragmentProcessor {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000018public:
joshualitteb2a6762014-12-04 11:35:33 -080019 GrGLConvolutionEffect(const GrProcessor&);
ericrk7a787b42015-07-21 14:06:16 -070020
wangyix7c157a92015-07-22 15:08:53 -070021 virtual void emitCode(EmitArgs&) override;
ericrk7a787b42015-07-21 14:06:16 -070022
jvanverthcfc18862015-04-28 08:48:20 -070023 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000024
wangyixb1daa862015-08-18 11:29:31 -070025protected:
egdaniel018fb622015-10-28 07:26:40 -070026 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -070027
ericrk0f386122015-07-21 13:15:47 -070028private:
ericrk7a787b42015-07-21 14:06:16 -070029 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); }
30 bool useBounds() const { return fUseBounds; }
31 Gr1DKernelEffect::Direction direction() const { return fDirection; }
32
33 int fRadius;
34 bool fUseBounds;
35 Gr1DKernelEffect::Direction fDirection;
36 UniformHandle fKernelUni;
37 UniformHandle fImageIncrementUni;
38 UniformHandle fBoundsUni;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000039
joshualittb0a8a372014-09-23 09:50:21 -070040 typedef GrGLFragmentProcessor INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000041};
42
joshualitteb2a6762014-12-04 11:35:33 -080043GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -070044 const GrConvolutionEffect& c = processor.cast<GrConvolutionEffect>();
bsalomon@google.comb505a122012-05-31 18:40:36 +000045 fRadius = c.radius();
ericrk7a787b42015-07-21 14:06:16 -070046 fUseBounds = c.useBounds();
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000047 fDirection = c.direction();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000048}
49
wangyix7c157a92015-07-22 15:08:53 -070050void GrGLConvolutionEffect::emitCode(EmitArgs& args) {
51 fImageIncrementUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
ericrk7a787b42015-07-21 14:06:16 -070052 kVec2f_GrSLType, kDefault_GrSLPrecision,
53 "ImageIncrement");
54 if (this->useBounds()) {
wangyix7c157a92015-07-22 15:08:53 -070055 fBoundsUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
ericrk7a787b42015-07-21 14:06:16 -070056 kVec2f_GrSLType, kDefault_GrSLPrecision,
57 "Bounds");
58 }
wangyix7c157a92015-07-22 15:08:53 -070059 fKernelUni = args.fBuilder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility,
ericrk7a787b42015-07-21 14:06:16 -070060 kFloat_GrSLType, kDefault_GrSLPrecision,
61 "Kernel", this->width());
62
wangyix7c157a92015-07-22 15:08:53 -070063 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
64 SkString coords2D = fsBuilder->ensureFSCoords2D(args.fCoords, 0);
ericrk7a787b42015-07-21 14:06:16 -070065
wangyix7c157a92015-07-22 15:08:53 -070066 fsBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", args.fOutputColor);
ericrk7a787b42015-07-21 14:06:16 -070067
68 int width = this->width();
egdaniel0d3f0612015-10-21 10:45:48 -070069 const GrGLSLShaderVar& kernel = args.fBuilder->getUniformVariable(fKernelUni);
wangyix7c157a92015-07-22 15:08:53 -070070 const char* imgInc = args.fBuilder->getUniformCStr(fImageIncrementUni);
ericrk7a787b42015-07-21 14:06:16 -070071
72 fsBuilder->codeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(), fRadius, imgInc);
73
74 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
75 for (int i = 0; i < width; i++) {
76 SkString index;
77 SkString kernelIndex;
78 index.appendS32(i);
79 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
80
81 if (this->useBounds()) {
82 // We used to compute a bool indicating whether we're in bounds or not, cast it to a
83 // float, and then mul weight*texture_sample by the float. However, the Adreno 430 seems
84 // to have a bug that caused corruption.
wangyix7c157a92015-07-22 15:08:53 -070085 const char* bounds = args.fBuilder->getUniformCStr(fBoundsUni);
ericrk7a787b42015-07-21 14:06:16 -070086 const char* component = this->direction() == Gr1DKernelEffect::kY_Direction ? "y" : "x";
87 fsBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y) {",
88 component, bounds, component, bounds);
89 }
wangyix7c157a92015-07-22 15:08:53 -070090 fsBuilder->codeAppendf("\t\t%s += ", args.fOutputColor);
91 fsBuilder->appendTextureLookup(args.fSamplers[0], "coord");
ericrk7a787b42015-07-21 14:06:16 -070092 fsBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str());
93 if (this->useBounds()) {
94 fsBuilder->codeAppend("}");
95 }
96 fsBuilder->codeAppendf("\t\tcoord += %s;\n", imgInc);
97 }
98
99 SkString modulate;
wangyix7c157a92015-07-22 15:08:53 -0700100 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor);
ericrk7a787b42015-07-21 14:06:16 -0700101 fsBuilder->codeAppend(modulate.c_str());
102}
103
egdaniel018fb622015-10-28 07:26:40 -0700104void GrGLConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman,
105 const GrProcessor& processor) {
ericrk7a787b42015-07-21 14:06:16 -0700106 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>();
107 GrTexture& texture = *conv.texture(0);
108 // the code we generated was for a specific kernel radius
109 SkASSERT(conv.radius() == fRadius);
110 float imageIncrement[2] = { 0 };
111 float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
112 switch (conv.direction()) {
113 case Gr1DKernelEffect::kX_Direction:
114 imageIncrement[0] = 1.0f / texture.width();
115 break;
116 case Gr1DKernelEffect::kY_Direction:
117 imageIncrement[1] = ySign / texture.height();
118 break;
119 default:
120 SkFAIL("Unknown filter direction.");
121 }
122 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
123 if (conv.useBounds()) {
124 const float* bounds = conv.bounds();
125 if (Gr1DKernelEffect::kY_Direction == conv.direction() &&
126 texture.origin() != kTopLeft_GrSurfaceOrigin) {
127 pdman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]);
128 } else {
129 pdman.set2f(fBoundsUni, bounds[0], bounds[1]);
130 }
131 }
132 pdman.set1fv(fKernelUni, this->width(), conv.kernel());
133}
134
135void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700136 GrProcessorKeyBuilder* b) {
137 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700138 uint32_t key = conv.radius();
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000139 key <<= 2;
140 if (conv.useBounds()) {
141 key |= 0x2;
142 key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0;
143 }
bsalomon63e99f72014-07-21 08:03:14 -0700144 b->add32(key);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000145}
146
bsalomon@google.comb505a122012-05-31 18:40:36 +0000147///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000148
bsalomon4a339522015-10-06 08:40:50 -0700149GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000150 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000151 int radius,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000152 const float* kernel,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000153 bool useBounds,
154 float bounds[2])
bsalomon4a339522015-10-06 08:40:50 -0700155 : INHERITED(texture, direction, radius), fUseBounds(useBounds) {
joshualitteb2a6762014-12-04 11:35:33 -0800156 this->initClassID<GrConvolutionEffect>();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000157 SkASSERT(radius <= kMaxKernelRadius);
bsalomon49f085d2014-09-05 13:34:00 -0700158 SkASSERT(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
bsalomon4a339522015-10-06 08:40:50 -0700166GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000167 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])
bsalomon4a339522015-10-06 08:40:50 -0700172 : INHERITED(texture, direction, radius), fUseBounds(useBounds) {
joshualitteb2a6762014-12-04 11:35:33 -0800173 this->initClassID<GrConvolutionEffect>();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000174 SkASSERT(radius <= kMaxKernelRadius);
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000175 int width = this->width();
176
177 float sum = 0.0f;
178 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
179 for (int i = 0; i < width; ++i) {
180 float x = static_cast<float>(i - this->radius());
181 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
182 // is dropped here, since we renormalize the kernel below.
183 fKernel[i] = sk_float_exp(- x * x * denom);
184 sum += fKernel[i];
185 }
186 // Normalize the kernel
187 float scale = 1.0f / sum;
188 for (int i = 0; i < width; ++i) {
189 fKernel[i] *= scale;
190 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000191 memcpy(fBounds, bounds, sizeof(fBounds));
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000192}
193
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000194GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000195}
196
wangyix4b3050b2015-08-04 07:59:37 -0700197void GrConvolutionEffect::onGetGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800198 GrProcessorKeyBuilder* b) const {
199 GrGLConvolutionEffect::GenKey(*this, caps, b);
200}
201
wangyixb1daa862015-08-18 11:29:31 -0700202GrGLFragmentProcessor* GrConvolutionEffect::onCreateGLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700203 return new GrGLConvolutionEffect(*this);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000204}
205
bsalomon0e08fc12014-10-15 08:19:04 -0700206bool GrConvolutionEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700207 const GrConvolutionEffect& s = sBase.cast<GrConvolutionEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700208 return (this->radius() == s.radius() &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000209 this->direction() == s.direction() &&
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000210 this->useBounds() == s.useBounds() &&
211 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000212 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000213}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000214
215///////////////////////////////////////////////////////////////////////////////
216
joshualittb0a8a372014-09-23 09:50:21 -0700217GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvolutionEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000218
bsalomonc21b09e2015-08-28 18:46:56 -0700219const GrFragmentProcessor* GrConvolutionEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700220 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
221 GrProcessorUnitTest::kAlphaTextureIdx;
222 Direction dir = d->fRandom->nextBool() ? kX_Direction : kY_Direction;
223 int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius);
bungeman@google.com43486632013-08-20 15:20:34 +0000224 float kernel[kMaxKernelWidth];
225 for (size_t i = 0; i < SK_ARRAY_COUNT(kernel); ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700226 kernel[i] = d->fRandom->nextSScalar1();
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000227 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000228 float bounds[2];
bungeman@google.com43486632013-08-20 15:20:34 +0000229 for (size_t i = 0; i < SK_ARRAY_COUNT(bounds); ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700230 bounds[i] = d->fRandom->nextF();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000231 }
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000232
joshualitt0067ff52015-07-08 14:26:19 -0700233 bool useBounds = d->fRandom->nextBool();
bsalomon4a339522015-10-06 08:40:50 -0700234 return GrConvolutionEffect::Create(d->fTextures[texIdx],
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000235 dir,
236 radius,
237 kernel,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000238 useBounds,
239 bounds);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000240}