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 "src/gpu/GrTexture.h"
11#include "src/gpu/GrTextureProxy.h"
12#include "src/gpu/effects/GrTextureEffect.h"
13#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
14#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
15#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
16#include "src/gpu/glsl/GrGLSLUniformHandler.h"
17
18// For brevity
19using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
20using Direction = GrGaussianConvolutionFragmentProcessor::Direction;
21
22static constexpr int radius_to_width(int r) { return 2*r + 1; }
23
24class GrGaussianConvolutionFragmentProcessor::Impl : public GrGLSLFragmentProcessor {
25public:
26 void emitCode(EmitArgs&) override;
27
28 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*);
29
30protected:
31 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
32
33private:
34 UniformHandle fKernelUni;
35 UniformHandle fIncrementUni;
36
37 typedef GrGLSLFragmentProcessor INHERITED;
38};
39
40void GrGaussianConvolutionFragmentProcessor::Impl::emitCode(EmitArgs& args) {
41 const GrGaussianConvolutionFragmentProcessor& ce =
42 args.fFp.cast<GrGaussianConvolutionFragmentProcessor>();
43
44 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
45
46 const char* inc;
47 fIncrementUni = uniformHandler->addUniform(&ce, kFragment_GrShaderFlag, kHalf2_GrSLType,
48 "Increment", &inc);
49
50 int width = radius_to_width(ce.fRadius);
51
52 int arrayCount = (width + 3) / 4;
53 SkASSERT(4 * arrayCount >= width);
54
55 const char* kernel;
56 fKernelUni = uniformHandler->addUniformArray(&ce, kFragment_GrShaderFlag, kHalf4_GrSLType,
57 "Kernel", arrayCount, &kernel);
58
59 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
60
61 fragBuilder->codeAppendf("%s = half4(0, 0, 0, 0);", args.fOutputColor);
62
63 fragBuilder->codeAppendf("float2 coord = %s - %d.0 * %s;", args.fSampleCoord, ce.fRadius, inc);
64 fragBuilder->codeAppend("float2 coordSampled = half2(0, 0);");
65
66 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
67 static constexpr const char* kVecSuffix[4] = {".x", ".y", ".z", ".w"};
68 for (int i = 0; i < width; i++) {
69 SkString kernelIndex;
70 kernelIndex.printf("%s[%d]", kernel, i/4);
71 kernelIndex.append(kVecSuffix[i & 0x3]);
72
73 fragBuilder->codeAppend("coordSampled = coord;");
74 auto sample = this->invokeChild(0, args, "coordSampled");
75 fragBuilder->codeAppendf("%s += %s", args.fOutputColor, sample.c_str());
76 fragBuilder->codeAppendf(" * %s;", kernelIndex.c_str());
77 fragBuilder->codeAppendf("coord += %s;", inc);
78 }
79 fragBuilder->codeAppendf("%s *= %s;", args.fOutputColor, args.fInputColor);
80}
81
82void GrGaussianConvolutionFragmentProcessor::Impl::onSetData(const GrGLSLProgramDataManager& pdman,
83 const GrFragmentProcessor& processor) {
84 const auto& conv = processor.cast<GrGaussianConvolutionFragmentProcessor>();
85
86 float increment[2] = {};
87 increment[static_cast<int>(conv.fDirection)] = 1;
88 pdman.set2fv(fIncrementUni, 1, increment);
89
90 int width = radius_to_width(conv.fRadius);
91 int arrayCount = (width + 3)/4;
92 SkDEBUGCODE(size_t arraySize = 4*arrayCount;)
93 SkASSERT(arraySize >= static_cast<size_t>(width));
94 SkASSERT(arraySize <= SK_ARRAY_COUNT(GrGaussianConvolutionFragmentProcessor::fKernel));
95 pdman.set4fv(fKernelUni, arrayCount, conv.fKernel);
96}
97
98void GrGaussianConvolutionFragmentProcessor::Impl::GenKey(const GrProcessor& processor,
99 const GrShaderCaps&,
100 GrProcessorKeyBuilder* b) {
101 const auto& conv = processor.cast<GrGaussianConvolutionFragmentProcessor>();
102 b->add32(conv.fRadius);
103}
104
105///////////////////////////////////////////////////////////////////////////////
106
107static void fill_in_1D_gaussian_kernel(float* kernel, float gaussianSigma, int radius) {
108 const float twoSigmaSqrd = 2.0f * gaussianSigma * gaussianSigma;
109 int width = radius_to_width(radius);
110 if (SkScalarNearlyZero(twoSigmaSqrd, SK_ScalarNearlyZero)) {
111 for (int i = 0; i < width; ++i) {
112 kernel[i] = 0.0f;
113 }
114 return;
115 }
116
117 const float denom = 1.0f / twoSigmaSqrd;
118
119 float sum = 0.0f;
120 for (int i = 0; i < width; ++i) {
121 float x = static_cast<float>(i - radius);
122 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
123 // is dropped here, since we renormalize the kernel below.
124 kernel[i] = sk_float_exp(-x * x * denom);
125 sum += kernel[i];
126 }
127 // Normalize the kernel
128 float scale = 1.0f / sum;
129 for (int i = 0; i < width; ++i) {
130 kernel[i] *= scale;
131 }
132}
133
134std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::Make(
135 GrSurfaceProxyView view,
136 SkAlphaType alphaType,
137 Direction dir,
138 int halfWidth,
139 float gaussianSigma,
140 GrSamplerState::WrapMode wm,
141 const SkIRect& subset,
142 const SkIRect* pixelDomain,
143 const GrCaps& caps) {
144 std::unique_ptr<GrFragmentProcessor> child;
145 GrSamplerState sampler(wm, GrSamplerState::Filter::kNearest);
146 if (pixelDomain) {
147 // Inset because we expect to be invoked at pixel centers.
148 SkRect domain = SkRect::Make(*pixelDomain).makeInset(0.5, 0.5f);
149 switch (dir) {
150 case Direction::kX: domain.outset(halfWidth, 0); break;
151 case Direction::kY: domain.outset(0, halfWidth); break;
152 }
153 child = GrTextureEffect::MakeSubset(std::move(view), alphaType, SkMatrix::I(), sampler,
154 SkRect::Make(subset), domain, caps);
155 } else {
156 child = GrTextureEffect::MakeSubset(std::move(view), alphaType, SkMatrix::I(), sampler,
157 SkRect::Make(subset), caps);
158 }
159 return std::unique_ptr<GrFragmentProcessor>(new GrGaussianConvolutionFragmentProcessor(
160 std::move(child), dir, halfWidth, gaussianSigma));
161}
162
163GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
164 std::unique_ptr<GrFragmentProcessor> child,
165 Direction direction,
166 int radius,
167 float gaussianSigma)
168 : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID,
169 ProcessorOptimizationFlags(child.get()))
170 , fRadius(radius)
171 , fDirection(direction) {
172 this->registerChild(std::move(child), SkSL::SampleUsage::Explicit());
173 SkASSERT(radius <= kMaxKernelRadius);
174 fill_in_1D_gaussian_kernel(fKernel, gaussianSigma, fRadius);
175 this->setUsesSampleCoordsDirectly();
176}
177
178GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
179 const GrGaussianConvolutionFragmentProcessor& that)
180 : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID, that.optimizationFlags())
181 , fRadius(that.fRadius)
182 , fDirection(that.fDirection) {
183 this->cloneAndRegisterAllChildProcessors(that);
184 memcpy(fKernel, that.fKernel, radius_to_width(fRadius) * sizeof(float));
185 this->setUsesSampleCoordsDirectly();
186}
187
188void GrGaussianConvolutionFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
189 GrProcessorKeyBuilder* b) const {
190 Impl::GenKey(*this, caps, b);
191}
192
193GrGLSLFragmentProcessor* GrGaussianConvolutionFragmentProcessor::onCreateGLSLInstance() const {
194 return new Impl;
195}
196
197bool GrGaussianConvolutionFragmentProcessor::onIsEqual(const GrFragmentProcessor& sBase) const {
198 const auto& that = sBase.cast<GrGaussianConvolutionFragmentProcessor>();
199 return fRadius == that.fRadius && fDirection == that.fDirection &&
200 std::equal(fKernel, fKernel + radius_to_width(fRadius), that.fKernel);
201}
202
203///////////////////////////////////////////////////////////////////////////////
204
205GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrGaussianConvolutionFragmentProcessor);
206
207#if GR_TEST_UTILS
208std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::TestCreate(
209 GrProcessorTestData* d) {
210 auto [view, ct, at] = d->randomView();
211
212 Direction dir = d->fRandom->nextBool() ? Direction::kY : Direction::kX;
213 SkIRect subset{
214 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
215 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
216 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
217 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
218 };
219 subset.sort();
220
221 auto wm = static_cast<GrSamplerState::WrapMode>(
222 d->fRandom->nextULessThan(GrSamplerState::kWrapModeCount));
223 int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius);
224 float sigma = radius / 3.f;
225 SkIRect temp;
226 SkIRect* domain = nullptr;
227 if (d->fRandom->nextBool()) {
228 temp = {
229 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
230 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
231 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
232 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
233 };
234 temp.sort();
235 domain = &temp;
236 }
237
238 return GrGaussianConvolutionFragmentProcessor::Make(std::move(view), at, dir, radius, sigma, wm,
239 subset, domain, *d->caps());
240}
241#endif
242