8e05f4789b7a067810d4ba8413b4b6b3afaf1db7
[platform/upstream/libSkiaSharp.git] / src / gpu / effects / GrConvolutionEffect.cpp
1 /*
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"
9 #include "glsl/GrGLSLFragmentProcessor.h"
10 #include "glsl/GrGLSLFragmentShaderBuilder.h"
11 #include "glsl/GrGLSLProgramBuilder.h"
12 #include "glsl/GrGLSLProgramDataManager.h"
13
14 // For brevity
15 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
16
17 class GrGLConvolutionEffect : public GrGLSLFragmentProcessor {
18 public:
19     GrGLConvolutionEffect(const GrProcessor&);
20
21     virtual void emitCode(EmitArgs&) override;
22
23     static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
24
25 protected:
26     void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor&) override;
27
28 private:
29     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;
39
40     typedef GrGLSLFragmentProcessor INHERITED;
41 };
42
43 GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProcessor& processor) {
44     const GrConvolutionEffect& c = processor.cast<GrConvolutionEffect>();
45     fRadius = c.radius();
46     fUseBounds = c.useBounds();
47     fDirection = c.direction();
48 }
49
50 void GrGLConvolutionEffect::emitCode(EmitArgs& args) {
51     fImageIncrementUni = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragment_Visibility,
52                                              kVec2f_GrSLType, kDefault_GrSLPrecision,
53                                              "ImageIncrement");
54     if (this->useBounds()) {
55         fBoundsUni = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragment_Visibility,
56                                          kVec2f_GrSLType, kDefault_GrSLPrecision,
57                                          "Bounds");
58     }
59     fKernelUni = args.fBuilder->addUniformArray(GrGLSLProgramBuilder::kFragment_Visibility,
60                                           kFloat_GrSLType, kDefault_GrSLPrecision,
61                                           "Kernel", this->width());
62
63     GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
64     SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0);
65
66     fragBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", args.fOutputColor);
67
68     int width = this->width();
69     const GrGLSLShaderVar& kernel = args.fBuilder->getUniformVariable(fKernelUni);
70     const char* imgInc = args.fBuilder->getUniformCStr(fImageIncrementUni);
71
72     fragBuilder->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.
85             const char* bounds = args.fBuilder->getUniformCStr(fBoundsUni);
86             const char* component = this->direction() == Gr1DKernelEffect::kY_Direction ? "y" : "x";
87             fragBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y) {",
88                                      component, bounds, component, bounds);
89         }
90         fragBuilder->codeAppendf("\t\t%s += ", args.fOutputColor);
91         fragBuilder->appendTextureLookup(args.fSamplers[0], "coord");
92         fragBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str());
93         if (this->useBounds()) {
94             fragBuilder->codeAppend("}");
95         }
96         fragBuilder->codeAppendf("\t\tcoord += %s;\n", imgInc);
97     }
98
99     SkString modulate;
100     GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor);
101     fragBuilder->codeAppend(modulate.c_str());
102 }
103
104 void GrGLConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman,
105                                       const GrProcessor& processor) {
106     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
135 void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
136                                    GrProcessorKeyBuilder* b) {
137     const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>();
138     uint32_t key = conv.radius();
139     key <<= 2;
140     if (conv.useBounds()) {
141         key |= 0x2;
142         key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0;
143     }
144     b->add32(key);
145 }
146
147 ///////////////////////////////////////////////////////////////////////////////
148
149 GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
150                                          Direction direction,
151                                          int radius,
152                                          const float* kernel,
153                                          bool useBounds,
154                                          float bounds[2])
155     : INHERITED(texture, direction, radius), fUseBounds(useBounds) {
156     this->initClassID<GrConvolutionEffect>();
157     SkASSERT(radius <= kMaxKernelRadius);
158     SkASSERT(kernel);
159     int width = this->width();
160     for (int i = 0; i < width; i++) {
161         fKernel[i] = kernel[i];
162     }
163     memcpy(fBounds, bounds, sizeof(fBounds));
164 }
165
166 GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
167                                          Direction direction,
168                                          int radius,
169                                          float gaussianSigma,
170                                          bool useBounds,
171                                          float bounds[2])
172     : INHERITED(texture, direction, radius), fUseBounds(useBounds) {
173     this->initClassID<GrConvolutionEffect>();
174     SkASSERT(radius <= kMaxKernelRadius);
175     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     }
191     memcpy(fBounds, bounds, sizeof(fBounds));
192 }
193
194 GrConvolutionEffect::~GrConvolutionEffect() {
195 }
196
197 void GrConvolutionEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
198                                                 GrProcessorKeyBuilder* b) const {
199     GrGLConvolutionEffect::GenKey(*this, caps, b);
200 }
201
202 GrGLSLFragmentProcessor* GrConvolutionEffect::onCreateGLSLInstance() const  {
203     return new GrGLConvolutionEffect(*this);
204 }
205
206 bool GrConvolutionEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
207     const GrConvolutionEffect& s = sBase.cast<GrConvolutionEffect>();
208     return (this->radius() == s.radius() &&
209             this->direction() == s.direction() &&
210             this->useBounds() == s.useBounds() &&
211             0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) &&
212             0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
213 }
214
215 ///////////////////////////////////////////////////////////////////////////////
216
217 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvolutionEffect);
218
219 const GrFragmentProcessor* 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);
224     float kernel[kMaxKernelWidth];
225     for (size_t i = 0; i < SK_ARRAY_COUNT(kernel); ++i) {
226         kernel[i] = d->fRandom->nextSScalar1();
227     }
228     float bounds[2];
229     for (size_t i = 0; i < SK_ARRAY_COUNT(bounds); ++i) {
230         bounds[i] = d->fRandom->nextF();
231     }
232
233     bool useBounds = d->fRandom->nextBool();
234     return GrConvolutionEffect::Create(d->fTextures[texIdx],
235                                        dir,
236                                        radius,
237                                        kernel,
238                                        useBounds,
239                                        bounds);
240 }