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