blob: fb68a29420cf5dd72b6e030857d1f8ca36b1cf0e [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"
egdaniel64c47282015-11-13 06:54:19 -08009#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080010#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070011#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080012#include "glsl/GrGLSLUniformHandler.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
egdaniel64c47282015-11-13 06:54:19 -080017class GrGLConvolutionEffect : public GrGLSLFragmentProcessor {
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
egdaniel64c47282015-11-13 06:54:19 -080040 typedef GrGLSLFragmentProcessor 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) {
egdaniel7ea439b2015-12-03 09:20:44 -080051 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
52 fImageIncrementUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility,
53 kVec2f_GrSLType, kDefault_GrSLPrecision,
54 "ImageIncrement");
ericrk7a787b42015-07-21 14:06:16 -070055 if (this->useBounds()) {
egdaniel7ea439b2015-12-03 09:20:44 -080056 fBoundsUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility,
57 kVec2f_GrSLType, kDefault_GrSLPrecision,
58 "Bounds");
ericrk7a787b42015-07-21 14:06:16 -070059 }
egdaniel7ea439b2015-12-03 09:20:44 -080060 fKernelUni = uniformHandler->addUniformArray(GrGLSLUniformHandler::kFragment_Visibility,
61 kFloat_GrSLType, kDefault_GrSLPrecision,
62 "Kernel", this->width());
ericrk7a787b42015-07-21 14:06:16 -070063
egdaniel4ca2e602015-11-18 08:01:26 -080064 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
65 SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0);
ericrk7a787b42015-07-21 14:06:16 -070066
egdaniel4ca2e602015-11-18 08:01:26 -080067 fragBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", args.fOutputColor);
ericrk7a787b42015-07-21 14:06:16 -070068
69 int width = this->width();
egdaniel7ea439b2015-12-03 09:20:44 -080070 const GrGLSLShaderVar& kernel = uniformHandler->getUniformVariable(fKernelUni);
71 const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni);
ericrk7a787b42015-07-21 14:06:16 -070072
egdaniel4ca2e602015-11-18 08:01:26 -080073 fragBuilder->codeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(), fRadius, imgInc);
ericrk7a787b42015-07-21 14:06:16 -070074
75 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
76 for (int i = 0; i < width; i++) {
77 SkString index;
78 SkString kernelIndex;
79 index.appendS32(i);
80 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
81
82 if (this->useBounds()) {
83 // We used to compute a bool indicating whether we're in bounds or not, cast it to a
84 // float, and then mul weight*texture_sample by the float. However, the Adreno 430 seems
85 // to have a bug that caused corruption.
egdaniel7ea439b2015-12-03 09:20:44 -080086 const char* bounds = uniformHandler->getUniformCStr(fBoundsUni);
ericrk7a787b42015-07-21 14:06:16 -070087 const char* component = this->direction() == Gr1DKernelEffect::kY_Direction ? "y" : "x";
egdaniel4ca2e602015-11-18 08:01:26 -080088 fragBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y) {",
89 component, bounds, component, bounds);
ericrk7a787b42015-07-21 14:06:16 -070090 }
egdaniel4ca2e602015-11-18 08:01:26 -080091 fragBuilder->codeAppendf("\t\t%s += ", args.fOutputColor);
92 fragBuilder->appendTextureLookup(args.fSamplers[0], "coord");
93 fragBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str());
ericrk7a787b42015-07-21 14:06:16 -070094 if (this->useBounds()) {
egdaniel4ca2e602015-11-18 08:01:26 -080095 fragBuilder->codeAppend("}");
ericrk7a787b42015-07-21 14:06:16 -070096 }
egdaniel4ca2e602015-11-18 08:01:26 -080097 fragBuilder->codeAppendf("\t\tcoord += %s;\n", imgInc);
ericrk7a787b42015-07-21 14:06:16 -070098 }
99
100 SkString modulate;
wangyix7c157a92015-07-22 15:08:53 -0700101 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800102 fragBuilder->codeAppend(modulate.c_str());
ericrk7a787b42015-07-21 14:06:16 -0700103}
104
egdaniel018fb622015-10-28 07:26:40 -0700105void GrGLConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman,
106 const GrProcessor& processor) {
ericrk7a787b42015-07-21 14:06:16 -0700107 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>();
108 GrTexture& texture = *conv.texture(0);
109 // the code we generated was for a specific kernel radius
110 SkASSERT(conv.radius() == fRadius);
111 float imageIncrement[2] = { 0 };
112 float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
113 switch (conv.direction()) {
114 case Gr1DKernelEffect::kX_Direction:
115 imageIncrement[0] = 1.0f / texture.width();
116 break;
117 case Gr1DKernelEffect::kY_Direction:
118 imageIncrement[1] = ySign / texture.height();
119 break;
120 default:
121 SkFAIL("Unknown filter direction.");
122 }
123 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
124 if (conv.useBounds()) {
125 const float* bounds = conv.bounds();
126 if (Gr1DKernelEffect::kY_Direction == conv.direction() &&
127 texture.origin() != kTopLeft_GrSurfaceOrigin) {
128 pdman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]);
129 } else {
130 pdman.set2f(fBoundsUni, bounds[0], bounds[1]);
131 }
132 }
133 pdman.set1fv(fKernelUni, this->width(), conv.kernel());
134}
135
136void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700137 GrProcessorKeyBuilder* b) {
138 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700139 uint32_t key = conv.radius();
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000140 key <<= 2;
141 if (conv.useBounds()) {
142 key |= 0x2;
143 key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0;
144 }
bsalomon63e99f72014-07-21 08:03:14 -0700145 b->add32(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
bsalomon4a339522015-10-06 08:40:50 -0700150GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000151 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])
bsalomon4a339522015-10-06 08:40:50 -0700156 : INHERITED(texture, direction, radius), fUseBounds(useBounds) {
joshualitteb2a6762014-12-04 11:35:33 -0800157 this->initClassID<GrConvolutionEffect>();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000158 SkASSERT(radius <= kMaxKernelRadius);
bsalomon49f085d2014-09-05 13:34:00 -0700159 SkASSERT(kernel);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000160 int width = this->width();
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000161 for (int i = 0; i < width; i++) {
162 fKernel[i] = kernel[i];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000163 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000164 memcpy(fBounds, bounds, sizeof(fBounds));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000165}
166
bsalomon4a339522015-10-06 08:40:50 -0700167GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000168 Direction direction,
169 int radius,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000170 float gaussianSigma,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000171 bool useBounds,
172 float bounds[2])
bsalomon4a339522015-10-06 08:40:50 -0700173 : INHERITED(texture, direction, radius), fUseBounds(useBounds) {
joshualitteb2a6762014-12-04 11:35:33 -0800174 this->initClassID<GrConvolutionEffect>();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000175 SkASSERT(radius <= kMaxKernelRadius);
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000176 int width = this->width();
177
178 float sum = 0.0f;
179 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
180 for (int i = 0; i < width; ++i) {
181 float x = static_cast<float>(i - this->radius());
182 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
183 // is dropped here, since we renormalize the kernel below.
184 fKernel[i] = sk_float_exp(- x * x * denom);
185 sum += fKernel[i];
186 }
187 // Normalize the kernel
188 float scale = 1.0f / sum;
189 for (int i = 0; i < width; ++i) {
190 fKernel[i] *= scale;
191 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000192 memcpy(fBounds, bounds, sizeof(fBounds));
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000193}
194
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000195GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000196}
197
egdaniel57d3b032015-11-13 11:57:27 -0800198void GrConvolutionEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
199 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800200 GrGLConvolutionEffect::GenKey(*this, caps, b);
201}
202
egdaniel57d3b032015-11-13 11:57:27 -0800203GrGLSLFragmentProcessor* GrConvolutionEffect::onCreateGLSLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700204 return new GrGLConvolutionEffect(*this);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000205}
206
bsalomon0e08fc12014-10-15 08:19:04 -0700207bool GrConvolutionEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700208 const GrConvolutionEffect& s = sBase.cast<GrConvolutionEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700209 return (this->radius() == s.radius() &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000210 this->direction() == s.direction() &&
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000211 this->useBounds() == s.useBounds() &&
212 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000213 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000214}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000215
216///////////////////////////////////////////////////////////////////////////////
217
joshualittb0a8a372014-09-23 09:50:21 -0700218GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvolutionEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000219
bsalomonc21b09e2015-08-28 18:46:56 -0700220const GrFragmentProcessor* GrConvolutionEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700221 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
222 GrProcessorUnitTest::kAlphaTextureIdx;
223 Direction dir = d->fRandom->nextBool() ? kX_Direction : kY_Direction;
224 int radius = d->fRandom->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) {
joshualitt0067ff52015-07-08 14:26:19 -0700227 kernel[i] = d->fRandom->nextSScalar1();
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000228 }
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) {
joshualitt0067ff52015-07-08 14:26:19 -0700231 bounds[i] = d->fRandom->nextF();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000232 }
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000233
joshualitt0067ff52015-07-08 14:26:19 -0700234 bool useBounds = d->fRandom->nextBool();
bsalomon4a339522015-10-06 08:40:50 -0700235 return GrConvolutionEffect::Create(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}