blob: 0f32c9c5e45adf050d5af2521ccd1e513eeae557 [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"
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
wangyix7c157a92015-07-22 15:08:53 -070020 virtual void emitCode(EmitArgs&) override;
ericrk7a787b42015-07-21 14:06:16 -070021
22 void setData(const GrGLProgramDataManager& pdman, const GrProcessor&) override;
23
jvanverthcfc18862015-04-28 08:48:20 -070024 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000025
ericrk0f386122015-07-21 13:15:47 -070026private:
ericrk7a787b42015-07-21 14:06:16 -070027 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); }
28 bool useBounds() const { return fUseBounds; }
29 Gr1DKernelEffect::Direction direction() const { return fDirection; }
30
31 int fRadius;
32 bool fUseBounds;
33 Gr1DKernelEffect::Direction fDirection;
34 UniformHandle fKernelUni;
35 UniformHandle fImageIncrementUni;
36 UniformHandle fBoundsUni;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000037
joshualittb0a8a372014-09-23 09:50:21 -070038 typedef GrGLFragmentProcessor INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000039};
40
joshualitteb2a6762014-12-04 11:35:33 -080041GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -070042 const GrConvolutionEffect& c = processor.cast<GrConvolutionEffect>();
bsalomon@google.comb505a122012-05-31 18:40:36 +000043 fRadius = c.radius();
ericrk7a787b42015-07-21 14:06:16 -070044 fUseBounds = c.useBounds();
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000045 fDirection = c.direction();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000046}
47
wangyix7c157a92015-07-22 15:08:53 -070048void GrGLConvolutionEffect::emitCode(EmitArgs& args) {
49 fImageIncrementUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
ericrk7a787b42015-07-21 14:06:16 -070050 kVec2f_GrSLType, kDefault_GrSLPrecision,
51 "ImageIncrement");
52 if (this->useBounds()) {
wangyix7c157a92015-07-22 15:08:53 -070053 fBoundsUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
ericrk7a787b42015-07-21 14:06:16 -070054 kVec2f_GrSLType, kDefault_GrSLPrecision,
55 "Bounds");
56 }
wangyix7c157a92015-07-22 15:08:53 -070057 fKernelUni = args.fBuilder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility,
ericrk7a787b42015-07-21 14:06:16 -070058 kFloat_GrSLType, kDefault_GrSLPrecision,
59 "Kernel", this->width());
60
wangyix7c157a92015-07-22 15:08:53 -070061 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
62 SkString coords2D = fsBuilder->ensureFSCoords2D(args.fCoords, 0);
ericrk7a787b42015-07-21 14:06:16 -070063
wangyix7c157a92015-07-22 15:08:53 -070064 fsBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", args.fOutputColor);
ericrk7a787b42015-07-21 14:06:16 -070065
66 int width = this->width();
wangyix7c157a92015-07-22 15:08:53 -070067 const GrGLShaderVar& kernel = args.fBuilder->getUniformVariable(fKernelUni);
68 const char* imgInc = args.fBuilder->getUniformCStr(fImageIncrementUni);
ericrk7a787b42015-07-21 14:06:16 -070069
70 fsBuilder->codeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(), fRadius, imgInc);
71
72 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
73 for (int i = 0; i < width; i++) {
74 SkString index;
75 SkString kernelIndex;
76 index.appendS32(i);
77 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
78
79 if (this->useBounds()) {
80 // We used to compute a bool indicating whether we're in bounds or not, cast it to a
81 // float, and then mul weight*texture_sample by the float. However, the Adreno 430 seems
82 // to have a bug that caused corruption.
wangyix7c157a92015-07-22 15:08:53 -070083 const char* bounds = args.fBuilder->getUniformCStr(fBoundsUni);
ericrk7a787b42015-07-21 14:06:16 -070084 const char* component = this->direction() == Gr1DKernelEffect::kY_Direction ? "y" : "x";
85 fsBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y) {",
86 component, bounds, component, bounds);
87 }
wangyix7c157a92015-07-22 15:08:53 -070088 fsBuilder->codeAppendf("\t\t%s += ", args.fOutputColor);
89 fsBuilder->appendTextureLookup(args.fSamplers[0], "coord");
ericrk7a787b42015-07-21 14:06:16 -070090 fsBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str());
91 if (this->useBounds()) {
92 fsBuilder->codeAppend("}");
93 }
94 fsBuilder->codeAppendf("\t\tcoord += %s;\n", imgInc);
95 }
96
97 SkString modulate;
wangyix7c157a92015-07-22 15:08:53 -070098 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor);
ericrk7a787b42015-07-21 14:06:16 -070099 fsBuilder->codeAppend(modulate.c_str());
100}
101
102void GrGLConvolutionEffect::setData(const GrGLProgramDataManager& pdman,
103 const GrProcessor& processor) {
104 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>();
105 GrTexture& texture = *conv.texture(0);
106 // the code we generated was for a specific kernel radius
107 SkASSERT(conv.radius() == fRadius);
108 float imageIncrement[2] = { 0 };
109 float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
110 switch (conv.direction()) {
111 case Gr1DKernelEffect::kX_Direction:
112 imageIncrement[0] = 1.0f / texture.width();
113 break;
114 case Gr1DKernelEffect::kY_Direction:
115 imageIncrement[1] = ySign / texture.height();
116 break;
117 default:
118 SkFAIL("Unknown filter direction.");
119 }
120 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
121 if (conv.useBounds()) {
122 const float* bounds = conv.bounds();
123 if (Gr1DKernelEffect::kY_Direction == conv.direction() &&
124 texture.origin() != kTopLeft_GrSurfaceOrigin) {
125 pdman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]);
126 } else {
127 pdman.set2f(fBoundsUni, bounds[0], bounds[1]);
128 }
129 }
130 pdman.set1fv(fKernelUni, this->width(), conv.kernel());
131}
132
133void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700134 GrProcessorKeyBuilder* b) {
135 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700136 uint32_t key = conv.radius();
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000137 key <<= 2;
138 if (conv.useBounds()) {
139 key |= 0x2;
140 key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0;
141 }
bsalomon63e99f72014-07-21 08:03:14 -0700142 b->add32(key);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000143}
144
bsalomon@google.comb505a122012-05-31 18:40:36 +0000145///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000146
joshualitt5f10b5c2015-07-09 10:24:35 -0700147GrConvolutionEffect::GrConvolutionEffect(GrProcessorDataManager* procDataManager,
148 GrTexture* texture,
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000149 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000150 int radius,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000151 const float* kernel,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000152 bool useBounds,
153 float bounds[2])
ericrk7a787b42015-07-21 14:06:16 -0700154 : INHERITED(procDataManager, texture, direction, radius), fUseBounds(useBounds) {
joshualitteb2a6762014-12-04 11:35:33 -0800155 this->initClassID<GrConvolutionEffect>();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000156 SkASSERT(radius <= kMaxKernelRadius);
bsalomon49f085d2014-09-05 13:34:00 -0700157 SkASSERT(kernel);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000158 int width = this->width();
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000159 for (int i = 0; i < width; i++) {
160 fKernel[i] = kernel[i];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000161 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000162 memcpy(fBounds, bounds, sizeof(fBounds));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000163}
164
joshualitt5f10b5c2015-07-09 10:24:35 -0700165GrConvolutionEffect::GrConvolutionEffect(GrProcessorDataManager* procDataManager,
166 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])
ericrk7a787b42015-07-21 14:06:16 -0700172 : INHERITED(procDataManager, 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
jvanverthcfc18862015-04-28 08:48:20 -0700197void GrConvolutionEffect::getGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800198 GrProcessorKeyBuilder* b) const {
199 GrGLConvolutionEffect::GenKey(*this, caps, b);
200}
201
202GrGLFragmentProcessor* GrConvolutionEffect::createGLInstance() const {
ericrk7a787b42015-07-21 14:06:16 -0700203 return SkNEW_ARGS(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
joshualitt0067ff52015-07-08 14:26:19 -0700219GrFragmentProcessor* GrConvolutionEffect::TestCreate(GrProcessorTestData* d) {
220 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();
joshualitt5f10b5c2015-07-09 10:24:35 -0700234 return GrConvolutionEffect::Create(d->fProcDataManager,
235 d->fTextures[texIdx],
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000236 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}