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 "src/gpu/effects/GrGaussianConvolutionFragmentProcessor.h"
9 
10 #include "include/gpu/GrTexture.h"
11 #include "src/gpu/GrTextureProxy.h"
12 #include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
13 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
14 #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
15 #include "src/gpu/glsl/GrGLSLUniformHandler.h"
16 
17 // For brevity
18 using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
19 using Direction = GrGaussianConvolutionFragmentProcessor::Direction;
20 
21 class GrGLConvolutionEffect : public GrGLSLFragmentProcessor {
22 public:
23     void emitCode(EmitArgs&) override;
24 
25     static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*);
26 
27 protected:
28     void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
29 
30 private:
31     UniformHandle fKernelUni;
32     UniformHandle fImageIncrementUni;
33     UniformHandle fBoundsUni;
34 
35     typedef GrGLSLFragmentProcessor INHERITED;
36 };
37 
emitCode(EmitArgs & args)38 void GrGLConvolutionEffect::emitCode(EmitArgs& args) {
39     const GrGaussianConvolutionFragmentProcessor& ce =
40             args.fFp.cast<GrGaussianConvolutionFragmentProcessor>();
41 
42     GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
43     fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType,
44                                                     "ImageIncrement");
45     if (ce.useBounds()) {
46         fBoundsUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType,
47                                                 "Bounds");
48     }
49 
50     int width = ce.width();
51 
52     int arrayCount = (width + 3) / 4;
53     SkASSERT(4 * arrayCount >= width);
54 
55     fKernelUni = uniformHandler->addUniformArray(kFragment_GrShaderFlag, kHalf4_GrSLType,
56                                                  "Kernel", arrayCount);
57 
58     GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
59     SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint);
60 
61     fragBuilder->codeAppendf("%s = half4(0, 0, 0, 0);", args.fOutputColor);
62 
63     const GrShaderVar& kernel = uniformHandler->getUniformVariable(fKernelUni);
64     const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni);
65 
66     fragBuilder->codeAppendf("float2 coord = %s - %d.0 * %s;", coords2D.c_str(), ce.radius(), imgInc);
67     fragBuilder->codeAppend("float2 coordSampled = half2(0, 0);");
68 
69     // Manually unroll loop because some drivers don't; yields 20-30% speedup.
70     const char* kVecSuffix[4] = {".x", ".y", ".z", ".w"};
71     for (int i = 0; i < width; i++) {
72         SkString index;
73         SkString kernelIndex;
74         index.appendS32(i / 4);
75         kernel.appendArrayAccess(index.c_str(), &kernelIndex);
76         kernelIndex.append(kVecSuffix[i & 0x3]);
77 
78         fragBuilder->codeAppend("coordSampled = coord;");
79         if (ce.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.
83             const char* bounds = uniformHandler->getUniformCStr(fBoundsUni);
84             const char* component = ce.direction() == Direction::kY ? "y" : "x";
85 
86             switch (ce.mode()) {
87                 case GrTextureDomain::kClamp_Mode: {
88                     fragBuilder->codeAppendf("coordSampled.%s = clamp(coord.%s, %s.x, %s.y);\n",
89                                              component, component, bounds, bounds);
90                     break;
91                 }
92                 case GrTextureDomain::kRepeat_Mode: {
93                     fragBuilder->codeAppendf("coordSampled.%s = "
94                                              "mod(coord.%s - %s.x, %s.y - %s.x) + %s.x;\n",
95                                              component, component, bounds, bounds, bounds, bounds);
96                     break;
97                 }
98                 case GrTextureDomain::kDecal_Mode: {
99                     fragBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y) {",
100                                              component, bounds, component, bounds);
101                     break;
102                 }
103                 default: {
104                     SK_ABORT("Unsupported operation.");
105                 }
106             }
107         }
108         fragBuilder->codeAppendf("%s += ", args.fOutputColor);
109         fragBuilder->appendTextureLookup(args.fTexSamplers[0], "coordSampled");
110         fragBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str());
111         if (GrTextureDomain::kDecal_Mode == ce.mode()) {
112             fragBuilder->codeAppend("}");
113         }
114         fragBuilder->codeAppendf("coord += %s;\n", imgInc);
115     }
116     fragBuilder->codeAppendf("%s *= %s;\n", args.fOutputColor, args.fInputColor);
117 }
118 
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & processor)119 void GrGLConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman,
120                                       const GrFragmentProcessor& processor) {
121     const GrGaussianConvolutionFragmentProcessor& conv =
122             processor.cast<GrGaussianConvolutionFragmentProcessor>();
123     GrSurfaceProxy* proxy = conv.textureSampler(0).proxy();
124     GrTexture& texture = *proxy->peekTexture();
125 
126     float imageIncrement[2] = {0};
127     float ySign = proxy->origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
128     switch (conv.direction()) {
129         case Direction::kX:
130             imageIncrement[0] = 1.0f / texture.width();
131             break;
132         case Direction::kY:
133             imageIncrement[1] = ySign / texture.height();
134             break;
135         default:
136             SK_ABORT("Unknown filter direction.");
137     }
138     pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
139     if (conv.useBounds()) {
140         float bounds[2] = {0};
141         bounds[0] = conv.bounds()[0];
142         bounds[1] = conv.bounds()[1];
143         if (GrTextureDomain::kClamp_Mode == conv.mode()) {
144             bounds[0] += SK_ScalarHalf;
145             bounds[1] -= SK_ScalarHalf;
146         }
147         if (Direction::kX == conv.direction()) {
148             SkScalar inv = SkScalarInvert(SkIntToScalar(texture.width()));
149             bounds[0] *= inv;
150             bounds[1] *= inv;
151         } else {
152             SkScalar inv = SkScalarInvert(SkIntToScalar(texture.height()));
153             if (proxy->origin() != kTopLeft_GrSurfaceOrigin) {
154                 float tmp = bounds[0];
155                 bounds[0] = 1.0f - (inv * bounds[1]);
156                 bounds[1] = 1.0f - (inv * tmp);
157             } else {
158                 bounds[0] *= inv;
159                 bounds[1] *= inv;
160             }
161         }
162 
163         SkASSERT(bounds[0] <= bounds[1]);
164         pdman.set2f(fBoundsUni, bounds[0], bounds[1]);
165     }
166     int width = conv.width();
167 
168     int arrayCount = (width + 3) / 4;
169     SkASSERT(4 * arrayCount >= width);
170     pdman.set4fv(fKernelUni, arrayCount, conv.kernel());
171 }
172 
GenKey(const GrProcessor & processor,const GrShaderCaps &,GrProcessorKeyBuilder * b)173 void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&,
174                                    GrProcessorKeyBuilder* b) {
175     const GrGaussianConvolutionFragmentProcessor& conv =
176             processor.cast<GrGaussianConvolutionFragmentProcessor>();
177     uint32_t key = conv.radius();
178     key <<= 3;
179     if (conv.useBounds()) {
180         key |= Direction::kY == conv.direction() ? 0x4 : 0x0;
181     }
182     key |= static_cast<uint32_t>(conv.mode());
183     b->add32(key);
184 }
185 
186 ///////////////////////////////////////////////////////////////////////////////
fill_in_1D_gaussian_kernel(float * kernel,int width,float gaussianSigma,int radius)187 static void fill_in_1D_gaussian_kernel(float* kernel, int width, float gaussianSigma, int radius) {
188     const float twoSigmaSqrd = 2.0f * gaussianSigma * gaussianSigma;
189     if (SkScalarNearlyZero(twoSigmaSqrd, SK_ScalarNearlyZero)) {
190         for (int i = 0; i < width; ++i) {
191             kernel[i] = 0.0f;
192         }
193         return;
194     }
195 
196     const float denom = 1.0f / twoSigmaSqrd;
197 
198     float sum = 0.0f;
199     for (int i = 0; i < width; ++i) {
200         float x = static_cast<float>(i - radius);
201         // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
202         // is dropped here, since we renormalize the kernel below.
203         kernel[i] = sk_float_exp(-x * x * denom);
204         sum += kernel[i];
205     }
206     // Normalize the kernel
207     float scale = 1.0f / sum;
208     for (int i = 0; i < width; ++i) {
209         kernel[i] *= scale;
210     }
211 }
212 
GrGaussianConvolutionFragmentProcessor(sk_sp<GrTextureProxy> proxy,GrColorType srcColorType,Direction direction,int radius,float gaussianSigma,GrTextureDomain::Mode mode,int bounds[2])213 GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
214                                                             sk_sp<GrTextureProxy> proxy,
215                                                             GrColorType srcColorType,
216                                                             Direction direction,
217                                                             int radius,
218                                                             float gaussianSigma,
219                                                             GrTextureDomain::Mode mode,
220                                                             int bounds[2])
221         : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID,
222                     ModulateForSamplerOptFlags(srcColorType,
223                                                mode == GrTextureDomain::kDecal_Mode))
224         , fCoordTransform(proxy.get())
225         , fTextureSampler(std::move(proxy))
226         , fRadius(radius)
227         , fDirection(direction)
228         , fMode(mode) {
229     // Make sure the sampler's ctor uses the clamp wrap mode
230     SkASSERT(fTextureSampler.samplerState().wrapModeX() == GrSamplerState::WrapMode::kClamp &&
231              fTextureSampler.samplerState().wrapModeY() == GrSamplerState::WrapMode::kClamp);
232     this->addCoordTransform(&fCoordTransform);
233     this->setTextureSamplerCnt(1);
234     SkASSERT(radius <= kMaxKernelRadius);
235 
236     fill_in_1D_gaussian_kernel(fKernel, this->width(), gaussianSigma, this->radius());
237 
238     memcpy(fBounds, bounds, sizeof(fBounds));
239 }
240 
GrGaussianConvolutionFragmentProcessor(const GrGaussianConvolutionFragmentProcessor & that)241 GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
242         const GrGaussianConvolutionFragmentProcessor& that)
243         : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID, that.optimizationFlags())
244         , fCoordTransform(that.fCoordTransform)
245         , fTextureSampler(that.fTextureSampler)
246         , fRadius(that.fRadius)
247         , fDirection(that.fDirection)
248         , fMode(that.fMode) {
249     this->addCoordTransform(&fCoordTransform);
250     this->setTextureSamplerCnt(1);
251     memcpy(fKernel, that.fKernel, that.width() * sizeof(float));
252     memcpy(fBounds, that.fBounds, sizeof(fBounds));
253 }
254 
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const255 void GrGaussianConvolutionFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
256                                                                    GrProcessorKeyBuilder* b) const {
257     GrGLConvolutionEffect::GenKey(*this, caps, b);
258 }
259 
onCreateGLSLInstance() const260 GrGLSLFragmentProcessor* GrGaussianConvolutionFragmentProcessor::onCreateGLSLInstance() const {
261     return new GrGLConvolutionEffect;
262 }
263 
onIsEqual(const GrFragmentProcessor & sBase) const264 bool GrGaussianConvolutionFragmentProcessor::onIsEqual(const GrFragmentProcessor& sBase) const {
265     const GrGaussianConvolutionFragmentProcessor& s =
266             sBase.cast<GrGaussianConvolutionFragmentProcessor>();
267     return (this->radius() == s.radius() && this->direction() == s.direction() &&
268             this->mode() == s.mode() &&
269             0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) &&
270             0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
271 }
272 
273 ///////////////////////////////////////////////////////////////////////////////
274 
275 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrGaussianConvolutionFragmentProcessor);
276 
277 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * d)278 std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::TestCreate(
279         GrProcessorTestData* d) {
280     int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
281                                         : GrProcessorUnitTest::kAlphaTextureIdx;
282     sk_sp<GrTextureProxy> proxy = d->textureProxy(texIdx);
283 
284     int bounds[2];
285     int modeIdx = d->fRandom->nextRangeU(0, GrTextureDomain::kModeCount-1);
286 
287     Direction dir;
288     if (d->fRandom->nextBool()) {
289         dir = Direction::kX;
290         bounds[0] = d->fRandom->nextRangeU(0, proxy->width()-2);
291         bounds[1] = d->fRandom->nextRangeU(bounds[0]+1, proxy->width()-1);
292     } else {
293         dir = Direction::kY;
294         bounds[0] = d->fRandom->nextRangeU(0, proxy->height()-2);
295         bounds[1] = d->fRandom->nextRangeU(bounds[0]+1, proxy->height()-1);
296     }
297 
298     int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius);
299     float sigma = radius / 3.f;
300 
301     return GrGaussianConvolutionFragmentProcessor::Make(
302             std::move(proxy), d->textureProxyColorType(texIdx),
303             dir, radius, sigma, static_cast<GrTextureDomain::Mode>(modeIdx), bounds);
304 }
305 #endif
306