1/*
2 * Copyright 2018 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/**************************************************************************************************
9 *** This file was autogenerated from GrCircleBlurFragmentProcessor.fp; do not modify.
10 **************************************************************************************************/
11#include "GrCircleBlurFragmentProcessor.h"
12#if SK_SUPPORT_GPU
13
14#include "GrProxyProvider.h"
15
16// Computes an unnormalized half kernel (right side). Returns the summation of all the half
17// kernel values.
18static float make_unnormalized_half_kernel(float* halfKernel, int halfKernelSize, float sigma) {
19    const float invSigma = 1.f / sigma;
20    const float b = -0.5f * invSigma * invSigma;
21    float tot = 0.0f;
22    // Compute half kernel values at half pixel steps out from the center.
23    float t = 0.5f;
24    for (int i = 0; i < halfKernelSize; ++i) {
25        float value = expf(t * t * b);
26        tot += value;
27        halfKernel[i] = value;
28        t += 1.f;
29    }
30    return tot;
31}
32
33// Create a Gaussian half-kernel (right side) and a summed area table given a sigma and number
34// of discrete steps. The half kernel is normalized to sum to 0.5.
35static void make_half_kernel_and_summed_table(float* halfKernel, float* summedHalfKernel,
36                                              int halfKernelSize, float sigma) {
37    // The half kernel should sum to 0.5 not 1.0.
38    const float tot = 2.f * make_unnormalized_half_kernel(halfKernel, halfKernelSize, sigma);
39    float sum = 0.f;
40    for (int i = 0; i < halfKernelSize; ++i) {
41        halfKernel[i] /= tot;
42        sum += halfKernel[i];
43        summedHalfKernel[i] = sum;
44    }
45}
46
47// Applies the 1D half kernel vertically at points along the x axis to a circle centered at the
48// origin with radius circleR.
49void apply_kernel_in_y(float* results, int numSteps, float firstX, float circleR,
50                       int halfKernelSize, const float* summedHalfKernelTable) {
51    float x = firstX;
52    for (int i = 0; i < numSteps; ++i, x += 1.f) {
53        if (x < -circleR || x > circleR) {
54            results[i] = 0;
55            continue;
56        }
57        float y = sqrtf(circleR * circleR - x * x);
58        // In the column at x we exit the circle at +y and -y
59        // The summed table entry j is actually reflects an offset of j + 0.5.
60        y -= 0.5f;
61        int yInt = SkScalarFloorToInt(y);
62        SkASSERT(yInt >= -1);
63        if (y < 0) {
64            results[i] = (y + 0.5f) * summedHalfKernelTable[0];
65        } else if (yInt >= halfKernelSize - 1) {
66            results[i] = 0.5f;
67        } else {
68            float yFrac = y - yInt;
69            results[i] = (1.f - yFrac) * summedHalfKernelTable[yInt] +
70                         yFrac * summedHalfKernelTable[yInt + 1];
71        }
72    }
73}
74
75// Apply a Gaussian at point (evalX, 0) to a circle centered at the origin with radius circleR.
76// This relies on having a half kernel computed for the Gaussian and a table of applications of
77// the half kernel in y to columns at (evalX - halfKernel, evalX - halfKernel + 1, ..., evalX +
78// halfKernel) passed in as yKernelEvaluations.
79static uint8_t eval_at(float evalX, float circleR, const float* halfKernel, int halfKernelSize,
80                       const float* yKernelEvaluations) {
81    float acc = 0;
82
83    float x = evalX - halfKernelSize;
84    for (int i = 0; i < halfKernelSize; ++i, x += 1.f) {
85        if (x < -circleR || x > circleR) {
86            continue;
87        }
88        float verticalEval = yKernelEvaluations[i];
89        acc += verticalEval * halfKernel[halfKernelSize - i - 1];
90    }
91    for (int i = 0; i < halfKernelSize; ++i, x += 1.f) {
92        if (x < -circleR || x > circleR) {
93            continue;
94        }
95        float verticalEval = yKernelEvaluations[i + halfKernelSize];
96        acc += verticalEval * halfKernel[i];
97    }
98    // Since we applied a half kernel in y we multiply acc by 2 (the circle is symmetric about
99    // the x axis).
100    return SkUnitScalarClampToByte(2.f * acc);
101}
102
103// This function creates a profile of a blurred circle. It does this by computing a kernel for
104// half the Gaussian and a matching summed area table. The summed area table is used to compute
105// an array of vertical applications of the half kernel to the circle along the x axis. The
106// table of y evaluations has 2 * k + n entries where k is the size of the half kernel and n is
107// the size of the profile being computed. Then for each of the n profile entries we walk out k
108// steps in each horizontal direction multiplying the corresponding y evaluation by the half
109// kernel entry and sum these values to compute the profile entry.
110static uint8_t* create_circle_profile(float sigma, float circleR, int profileTextureWidth) {
111    const int numSteps = profileTextureWidth;
112    uint8_t* weights = new uint8_t[numSteps];
113
114    // The full kernel is 6 sigmas wide.
115    int halfKernelSize = SkScalarCeilToInt(6.0f * sigma);
116    // round up to next multiple of 2 and then divide by 2
117    halfKernelSize = ((halfKernelSize + 1) & ~1) >> 1;
118
119    // Number of x steps at which to apply kernel in y to cover all the profile samples in x.
120    int numYSteps = numSteps + 2 * halfKernelSize;
121
122    SkAutoTArray<float> bulkAlloc(halfKernelSize + halfKernelSize + numYSteps);
123    float* halfKernel = bulkAlloc.get();
124    float* summedKernel = bulkAlloc.get() + halfKernelSize;
125    float* yEvals = bulkAlloc.get() + 2 * halfKernelSize;
126    make_half_kernel_and_summed_table(halfKernel, summedKernel, halfKernelSize, sigma);
127
128    float firstX = -halfKernelSize + 0.5f;
129    apply_kernel_in_y(yEvals, numYSteps, firstX, circleR, halfKernelSize, summedKernel);
130
131    for (int i = 0; i < numSteps - 1; ++i) {
132        float evalX = i + 0.5f;
133        weights[i] = eval_at(evalX, circleR, halfKernel, halfKernelSize, yEvals + i);
134    }
135    // Ensure the tail of the Gaussian goes to zero.
136    weights[numSteps - 1] = 0;
137    return weights;
138}
139
140static uint8_t* create_half_plane_profile(int profileWidth) {
141    SkASSERT(!(profileWidth & 0x1));
142    // The full kernel is 6 sigmas wide.
143    float sigma = profileWidth / 6.f;
144    int halfKernelSize = profileWidth / 2;
145
146    SkAutoTArray<float> halfKernel(halfKernelSize);
147    uint8_t* profile = new uint8_t[profileWidth];
148
149    // The half kernel should sum to 0.5.
150    const float tot = 2.f * make_unnormalized_half_kernel(halfKernel.get(), halfKernelSize, sigma);
151    float sum = 0.f;
152    // Populate the profile from the right edge to the middle.
153    for (int i = 0; i < halfKernelSize; ++i) {
154        halfKernel[halfKernelSize - i - 1] /= tot;
155        sum += halfKernel[halfKernelSize - i - 1];
156        profile[profileWidth - i - 1] = SkUnitScalarClampToByte(sum);
157    }
158    // Populate the profile from the middle to the left edge (by flipping the half kernel and
159    // continuing the summation).
160    for (int i = 0; i < halfKernelSize; ++i) {
161        sum += halfKernel[i];
162        profile[halfKernelSize - i - 1] = SkUnitScalarClampToByte(sum);
163    }
164    // Ensure tail goes to 0.
165    profile[profileWidth - 1] = 0;
166    return profile;
167}
168
169static sk_sp<GrTextureProxy> create_profile_texture(GrProxyProvider* proxyProvider,
170                                                    const SkRect& circle, float sigma,
171                                                    float* solidRadius, float* textureRadius) {
172    float circleR = circle.width() / 2.0f;
173    if (circleR < SK_ScalarNearlyZero) {
174        return nullptr;
175    }
176    // Profile textures are cached by the ratio of sigma to circle radius and by the size of the
177    // profile texture (binned by powers of 2).
178    SkScalar sigmaToCircleRRatio = sigma / circleR;
179    // When sigma is really small this becomes a equivalent to convolving a Gaussian with a
180    // half-plane. Similarly, in the extreme high ratio cases circle becomes a point WRT to the
181    // Guassian and the profile texture is a just a Gaussian evaluation. However, we haven't yet
182    // implemented this latter optimization.
183    sigmaToCircleRRatio = SkTMin(sigmaToCircleRRatio, 8.f);
184    SkFixed sigmaToCircleRRatioFixed;
185    static const SkScalar kHalfPlaneThreshold = 0.1f;
186    bool useHalfPlaneApprox = false;
187    if (sigmaToCircleRRatio <= kHalfPlaneThreshold) {
188        useHalfPlaneApprox = true;
189        sigmaToCircleRRatioFixed = 0;
190        *solidRadius = circleR - 3 * sigma;
191        *textureRadius = 6 * sigma;
192    } else {
193        // Convert to fixed point for the key.
194        sigmaToCircleRRatioFixed = SkScalarToFixed(sigmaToCircleRRatio);
195        // We shave off some bits to reduce the number of unique entries. We could probably
196        // shave off more than we do.
197        sigmaToCircleRRatioFixed &= ~0xff;
198        sigmaToCircleRRatio = SkFixedToScalar(sigmaToCircleRRatioFixed);
199        sigma = circleR * sigmaToCircleRRatio;
200        *solidRadius = 0;
201        *textureRadius = circleR + 3 * sigma;
202    }
203
204    static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
205    GrUniqueKey key;
206    GrUniqueKey::Builder builder(&key, kDomain, 1, "1-D Circular Blur");
207    builder[0] = sigmaToCircleRRatioFixed;
208    builder.finish();
209
210    sk_sp<GrTextureProxy> blurProfile =
211            proxyProvider->findOrCreateProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin);
212    if (!blurProfile) {
213        static constexpr int kProfileTextureWidth = 512;
214        GrSurfaceDesc texDesc;
215        texDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
216        texDesc.fWidth = kProfileTextureWidth;
217        texDesc.fHeight = 1;
218        texDesc.fConfig = kAlpha_8_GrPixelConfig;
219
220        std::unique_ptr<uint8_t[]> profile(nullptr);
221        if (useHalfPlaneApprox) {
222            profile.reset(create_half_plane_profile(kProfileTextureWidth));
223        } else {
224            // Rescale params to the size of the texture we're creating.
225            SkScalar scale = kProfileTextureWidth / *textureRadius;
226            profile.reset(
227                    create_circle_profile(sigma * scale, circleR * scale, kProfileTextureWidth));
228        }
229
230        blurProfile =
231                proxyProvider->createTextureProxy(texDesc, SkBudgeted::kYes, profile.get(), 0);
232        if (!blurProfile) {
233            return nullptr;
234        }
235
236        SkASSERT(blurProfile->origin() == kTopLeft_GrSurfaceOrigin);
237        proxyProvider->assignUniqueKeyToProxy(key, blurProfile.get());
238    }
239
240    return blurProfile;
241}
242
243std::unique_ptr<GrFragmentProcessor> GrCircleBlurFragmentProcessor::Make(
244        GrProxyProvider* proxyProvider, const SkRect& circle, float sigma) {
245    float solidRadius;
246    float textureRadius;
247    sk_sp<GrTextureProxy> profile(
248            create_profile_texture(proxyProvider, circle, sigma, &solidRadius, &textureRadius));
249    if (!profile) {
250        return nullptr;
251    }
252    return std::unique_ptr<GrFragmentProcessor>(new GrCircleBlurFragmentProcessor(
253            circle, textureRadius, solidRadius, std::move(profile)));
254}
255#include "glsl/GrGLSLFragmentProcessor.h"
256#include "glsl/GrGLSLFragmentShaderBuilder.h"
257#include "glsl/GrGLSLProgramBuilder.h"
258#include "GrTexture.h"
259#include "SkSLCPP.h"
260#include "SkSLUtil.h"
261class GrGLSLCircleBlurFragmentProcessor : public GrGLSLFragmentProcessor {
262public:
263    GrGLSLCircleBlurFragmentProcessor() {}
264    void emitCode(EmitArgs& args) override {
265        GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
266        const GrCircleBlurFragmentProcessor& _outer =
267                args.fFp.cast<GrCircleBlurFragmentProcessor>();
268        (void)_outer;
269        auto circleRect = _outer.circleRect();
270        (void)circleRect;
271        auto textureRadius = _outer.textureRadius();
272        (void)textureRadius;
273        auto solidRadius = _outer.solidRadius();
274        (void)solidRadius;
275        fCircleDataVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
276                                                          kDefault_GrSLPrecision, "circleData");
277        fragBuilder->codeAppendf(
278                "half2 vec = half2(half((sk_FragCoord.x - float(%s.x)) * float(%s.w)), "
279                "half((sk_FragCoord.y - float(%s.y)) * float(%s.w)));\nhalf dist = "
280                "float(length(vec)) + (0.5 - float(%s.z)) * float(%s.w);\n%s = %s * texture(%s, "
281                "float2(half2(dist, 0.5))).%s.w;\n",
282                args.fUniformHandler->getUniformCStr(fCircleDataVar),
283                args.fUniformHandler->getUniformCStr(fCircleDataVar),
284                args.fUniformHandler->getUniformCStr(fCircleDataVar),
285                args.fUniformHandler->getUniformCStr(fCircleDataVar),
286                args.fUniformHandler->getUniformCStr(fCircleDataVar),
287                args.fUniformHandler->getUniformCStr(fCircleDataVar), args.fOutputColor,
288                args.fInputColor ? args.fInputColor : "half4(1)",
289                fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(),
290                fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str());
291    }
292
293private:
294    void onSetData(const GrGLSLProgramDataManager& data,
295                   const GrFragmentProcessor& _proc) override {
296        const GrCircleBlurFragmentProcessor& _outer = _proc.cast<GrCircleBlurFragmentProcessor>();
297        auto circleRect = _outer.circleRect();
298        (void)circleRect;
299        auto textureRadius = _outer.textureRadius();
300        (void)textureRadius;
301        auto solidRadius = _outer.solidRadius();
302        (void)solidRadius;
303        GrSurfaceProxy& blurProfileSamplerProxy = *_outer.textureSampler(0).proxy();
304        GrTexture& blurProfileSampler = *blurProfileSamplerProxy.priv().peekTexture();
305        (void)blurProfileSampler;
306        UniformHandle& circleData = fCircleDataVar;
307        (void)circleData;
308
309        data.set4f(circleData, circleRect.centerX(), circleRect.centerY(), solidRadius,
310                   1.f / textureRadius);
311    }
312    UniformHandle fCircleDataVar;
313};
314GrGLSLFragmentProcessor* GrCircleBlurFragmentProcessor::onCreateGLSLInstance() const {
315    return new GrGLSLCircleBlurFragmentProcessor();
316}
317void GrCircleBlurFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
318                                                          GrProcessorKeyBuilder* b) const {}
319bool GrCircleBlurFragmentProcessor::onIsEqual(const GrFragmentProcessor& other) const {
320    const GrCircleBlurFragmentProcessor& that = other.cast<GrCircleBlurFragmentProcessor>();
321    (void)that;
322    if (fCircleRect != that.fCircleRect) return false;
323    if (fTextureRadius != that.fTextureRadius) return false;
324    if (fSolidRadius != that.fSolidRadius) return false;
325    if (fBlurProfileSampler != that.fBlurProfileSampler) return false;
326    return true;
327}
328GrCircleBlurFragmentProcessor::GrCircleBlurFragmentProcessor(
329        const GrCircleBlurFragmentProcessor& src)
330        : INHERITED(kGrCircleBlurFragmentProcessor_ClassID, src.optimizationFlags())
331        , fCircleRect(src.fCircleRect)
332        , fTextureRadius(src.fTextureRadius)
333        , fSolidRadius(src.fSolidRadius)
334        , fBlurProfileSampler(src.fBlurProfileSampler) {
335    this->addTextureSampler(&fBlurProfileSampler);
336}
337std::unique_ptr<GrFragmentProcessor> GrCircleBlurFragmentProcessor::clone() const {
338    return std::unique_ptr<GrFragmentProcessor>(new GrCircleBlurFragmentProcessor(*this));
339}
340GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrCircleBlurFragmentProcessor);
341#if GR_TEST_UTILS
342std::unique_ptr<GrFragmentProcessor> GrCircleBlurFragmentProcessor::TestCreate(
343        GrProcessorTestData* testData) {
344    SkScalar wh = testData->fRandom->nextRangeScalar(100.f, 1000.f);
345    SkScalar sigma = testData->fRandom->nextRangeF(1.f, 10.f);
346    SkRect circle = SkRect::MakeWH(wh, wh);
347    return GrCircleBlurFragmentProcessor::Make(testData->proxyProvider(), circle, sigma);
348}
349#endif
350#endif
351