blob: 4228a0333fcf88f76509f972624d998293a45c57 [file] [log] [blame]
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05001/*
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
Ethan Nicholasf7b88202017-09-18 14:10:39 -04008in half4 circleRect;
9in half textureRadius;
10in half solidRadius;
Ethan Nicholasceb4d482017-07-10 15:40:20 -040011in uniform sampler2D blurProfileSampler;
12
13// The data is formatted as:
14// x, y - the center of the circle
15// z - inner radius that should map to 0th entry in the texture.
16// w - the inverse of the distance over which the texture is stretched.
Ethan Nicholasf7b88202017-09-18 14:10:39 -040017uniform half4 circleData;
Ethan Nicholasceb4d482017-07-10 15:40:20 -040018
19@optimizationFlags {
20 kCompatibleWithCoverageAsAlpha_OptimizationFlag
21}
22
Ethan Nicholasceb4d482017-07-10 15:40:20 -040023@make {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050024 static std::unique_ptr<GrFragmentProcessor> Make(GrProxyProvider*,
Brian Salomonaff329b2017-08-11 09:40:37 -040025 const SkRect& circle, float sigma);
Ethan Nicholasceb4d482017-07-10 15:40:20 -040026}
27
28@setData(data) {
29 data.set4f(circleData, circleRect.centerX(), circleRect.centerY(), solidRadius,
30 1.f / textureRadius);
31}
32
33@cpp {
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034 #include "src/gpu/GrProxyProvider.h"
Ethan Nicholasceb4d482017-07-10 15:40:20 -040035
36 // Computes an unnormalized half kernel (right side). Returns the summation of all the half
37 // kernel values.
38 static float make_unnormalized_half_kernel(float* halfKernel, int halfKernelSize, float sigma) {
39 const float invSigma = 1.f / sigma;
40 const float b = -0.5f * invSigma * invSigma;
41 float tot = 0.0f;
42 // Compute half kernel values at half pixel steps out from the center.
43 float t = 0.5f;
44 for (int i = 0; i < halfKernelSize; ++i) {
45 float value = expf(t * t * b);
46 tot += value;
47 halfKernel[i] = value;
48 t += 1.f;
49 }
50 return tot;
51 }
52
53 // Create a Gaussian half-kernel (right side) and a summed area table given a sigma and number
54 // of discrete steps. The half kernel is normalized to sum to 0.5.
55 static void make_half_kernel_and_summed_table(float* halfKernel, float* summedHalfKernel,
56 int halfKernelSize, float sigma) {
57 // The half kernel should sum to 0.5 not 1.0.
58 const float tot = 2.f * make_unnormalized_half_kernel(halfKernel, halfKernelSize, sigma);
59 float sum = 0.f;
60 for (int i = 0; i < halfKernelSize; ++i) {
61 halfKernel[i] /= tot;
62 sum += halfKernel[i];
63 summedHalfKernel[i] = sum;
64 }
65 }
66
67 // Applies the 1D half kernel vertically at points along the x axis to a circle centered at the
68 // origin with radius circleR.
69 void apply_kernel_in_y(float* results, int numSteps, float firstX, float circleR,
70 int halfKernelSize, const float* summedHalfKernelTable) {
71 float x = firstX;
72 for (int i = 0; i < numSteps; ++i, x += 1.f) {
73 if (x < -circleR || x > circleR) {
74 results[i] = 0;
75 continue;
76 }
77 float y = sqrtf(circleR * circleR - x * x);
78 // In the column at x we exit the circle at +y and -y
79 // The summed table entry j is actually reflects an offset of j + 0.5.
80 y -= 0.5f;
81 int yInt = SkScalarFloorToInt(y);
82 SkASSERT(yInt >= -1);
83 if (y < 0) {
84 results[i] = (y + 0.5f) * summedHalfKernelTable[0];
85 } else if (yInt >= halfKernelSize - 1) {
86 results[i] = 0.5f;
87 } else {
88 float yFrac = y - yInt;
89 results[i] = (1.f - yFrac) * summedHalfKernelTable[yInt] +
90 yFrac * summedHalfKernelTable[yInt + 1];
91 }
92 }
93 }
94
95 // Apply a Gaussian at point (evalX, 0) to a circle centered at the origin with radius circleR.
96 // This relies on having a half kernel computed for the Gaussian and a table of applications of
97 // the half kernel in y to columns at (evalX - halfKernel, evalX - halfKernel + 1, ..., evalX +
98 // halfKernel) passed in as yKernelEvaluations.
99 static uint8_t eval_at(float evalX, float circleR, const float* halfKernel, int halfKernelSize,
100 const float* yKernelEvaluations) {
101 float acc = 0;
102
103 float x = evalX - halfKernelSize;
104 for (int i = 0; i < halfKernelSize; ++i, x += 1.f) {
105 if (x < -circleR || x > circleR) {
106 continue;
107 }
108 float verticalEval = yKernelEvaluations[i];
109 acc += verticalEval * halfKernel[halfKernelSize - i - 1];
110 }
111 for (int i = 0; i < halfKernelSize; ++i, x += 1.f) {
112 if (x < -circleR || x > circleR) {
113 continue;
114 }
115 float verticalEval = yKernelEvaluations[i + halfKernelSize];
116 acc += verticalEval * halfKernel[i];
117 }
118 // Since we applied a half kernel in y we multiply acc by 2 (the circle is symmetric about
119 // the x axis).
120 return SkUnitScalarClampToByte(2.f * acc);
121 }
122
123 // This function creates a profile of a blurred circle. It does this by computing a kernel for
124 // half the Gaussian and a matching summed area table. The summed area table is used to compute
125 // an array of vertical applications of the half kernel to the circle along the x axis. The
126 // table of y evaluations has 2 * k + n entries where k is the size of the half kernel and n is
127 // the size of the profile being computed. Then for each of the n profile entries we walk out k
128 // steps in each horizontal direction multiplying the corresponding y evaluation by the half
129 // kernel entry and sum these values to compute the profile entry.
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500130 static void create_circle_profile(uint8_t* weights, float sigma, float circleR,
131 int profileTextureWidth) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400132 const int numSteps = profileTextureWidth;
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400133
134 // The full kernel is 6 sigmas wide.
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500135 int halfKernelSize = SkScalarCeilToInt(6.0f * sigma);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400136 // round up to next multiple of 2 and then divide by 2
137 halfKernelSize = ((halfKernelSize + 1) & ~1) >> 1;
138
139 // Number of x steps at which to apply kernel in y to cover all the profile samples in x.
140 int numYSteps = numSteps + 2 * halfKernelSize;
141
142 SkAutoTArray<float> bulkAlloc(halfKernelSize + halfKernelSize + numYSteps);
143 float* halfKernel = bulkAlloc.get();
144 float* summedKernel = bulkAlloc.get() + halfKernelSize;
145 float* yEvals = bulkAlloc.get() + 2 * halfKernelSize;
146 make_half_kernel_and_summed_table(halfKernel, summedKernel, halfKernelSize, sigma);
147
148 float firstX = -halfKernelSize + 0.5f;
149 apply_kernel_in_y(yEvals, numYSteps, firstX, circleR, halfKernelSize, summedKernel);
150
151 for (int i = 0; i < numSteps - 1; ++i) {
152 float evalX = i + 0.5f;
153 weights[i] = eval_at(evalX, circleR, halfKernel, halfKernelSize, yEvals + i);
154 }
155 // Ensure the tail of the Gaussian goes to zero.
156 weights[numSteps - 1] = 0;
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400157 }
158
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500159 static void create_half_plane_profile(uint8_t* profile, int profileWidth) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400160 SkASSERT(!(profileWidth & 0x1));
161 // The full kernel is 6 sigmas wide.
162 float sigma = profileWidth / 6.f;
163 int halfKernelSize = profileWidth / 2;
164
165 SkAutoTArray<float> halfKernel(halfKernelSize);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400166
167 // The half kernel should sum to 0.5.
168 const float tot = 2.f * make_unnormalized_half_kernel(halfKernel.get(), halfKernelSize,
169 sigma);
170 float sum = 0.f;
171 // Populate the profile from the right edge to the middle.
172 for (int i = 0; i < halfKernelSize; ++i) {
173 halfKernel[halfKernelSize - i - 1] /= tot;
174 sum += halfKernel[halfKernelSize - i - 1];
175 profile[profileWidth - i - 1] = SkUnitScalarClampToByte(sum);
176 }
177 // Populate the profile from the middle to the left edge (by flipping the half kernel and
178 // continuing the summation).
179 for (int i = 0; i < halfKernelSize; ++i) {
180 sum += halfKernel[i];
181 profile[halfKernelSize - i - 1] = SkUnitScalarClampToByte(sum);
182 }
183 // Ensure tail goes to 0.
184 profile[profileWidth - 1] = 0;
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400185 }
186
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500187 static sk_sp<GrTextureProxy> create_profile_texture(GrProxyProvider* proxyProvider,
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400188 const SkRect& circle,
189 float sigma,
190 float* solidRadius, float* textureRadius) {
191 float circleR = circle.width() / 2.0f;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500192 if (circleR < SK_ScalarNearlyZero) {
193 return nullptr;
194 }
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400195 // Profile textures are cached by the ratio of sigma to circle radius and by the size of the
196 // profile texture (binned by powers of 2).
197 SkScalar sigmaToCircleRRatio = sigma / circleR;
198 // When sigma is really small this becomes a equivalent to convolving a Gaussian with a
199 // half-plane. Similarly, in the extreme high ratio cases circle becomes a point WRT to the
200 // Guassian and the profile texture is a just a Gaussian evaluation. However, we haven't yet
201 // implemented this latter optimization.
202 sigmaToCircleRRatio = SkTMin(sigmaToCircleRRatio, 8.f);
203 SkFixed sigmaToCircleRRatioFixed;
204 static const SkScalar kHalfPlaneThreshold = 0.1f;
205 bool useHalfPlaneApprox = false;
206 if (sigmaToCircleRRatio <= kHalfPlaneThreshold) {
207 useHalfPlaneApprox = true;
208 sigmaToCircleRRatioFixed = 0;
209 *solidRadius = circleR - 3 * sigma;
210 *textureRadius = 6 * sigma;
211 } else {
212 // Convert to fixed point for the key.
213 sigmaToCircleRRatioFixed = SkScalarToFixed(sigmaToCircleRRatio);
214 // We shave off some bits to reduce the number of unique entries. We could probably
215 // shave off more than we do.
216 sigmaToCircleRRatioFixed &= ~0xff;
217 sigmaToCircleRRatio = SkFixedToScalar(sigmaToCircleRRatioFixed);
218 sigma = circleR * sigmaToCircleRRatio;
219 *solidRadius = 0;
220 *textureRadius = circleR + 3 * sigma;
221 }
222
223 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
224 GrUniqueKey key;
Robert Phillipsc46e9b72018-03-29 14:49:43 -0400225 GrUniqueKey::Builder builder(&key, kDomain, 1, "1-D Circular Blur");
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400226 builder[0] = sigmaToCircleRRatioFixed;
227 builder.finish();
228
Brian Salomon2af3e702019-08-11 19:10:31 -0400229 sk_sp<GrTextureProxy> blurProfile = proxyProvider->findOrCreateProxyByUniqueKey(
230 key, GrColorType::kAlpha_8, kTopLeft_GrSurfaceOrigin);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400231 if (!blurProfile) {
232 static constexpr int kProfileTextureWidth = 512;
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400233
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500234 SkBitmap bm;
235 if (!bm.tryAllocPixels(SkImageInfo::MakeA8(kProfileTextureWidth, 1))) {
236 return nullptr;
237 }
238
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400239 if (useHalfPlaneApprox) {
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500240 create_half_plane_profile(bm.getAddr8(0, 0), kProfileTextureWidth);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400241 } else {
242 // Rescale params to the size of the texture we're creating.
243 SkScalar scale = kProfileTextureWidth / *textureRadius;
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500244 create_circle_profile(bm.getAddr8(0, 0), sigma * scale, circleR * scale,
245 kProfileTextureWidth);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400246 }
247
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500248 bm.setImmutable();
249 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bm);
250
Brian Salomon96b383a2019-08-13 16:55:41 -0400251 blurProfile = proxyProvider->createTextureProxy(std::move(image), 1,
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500252 SkBudgeted::kYes, SkBackingFit::kExact);
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400253 if (!blurProfile) {
254 return nullptr;
255 }
256
Ethan Nicholas480c90a2017-07-25 16:45:15 -0400257 SkASSERT(blurProfile->origin() == kTopLeft_GrSurfaceOrigin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500258 proxyProvider->assignUniqueKeyToProxy(key, blurProfile.get());
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400259 }
260
261 return blurProfile;
262 }
263
Brian Salomonaff329b2017-08-11 09:40:37 -0400264 std::unique_ptr<GrFragmentProcessor> GrCircleBlurFragmentProcessor::Make(
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500265 GrProxyProvider* proxyProvider, const SkRect& circle, float sigma) {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400266 float solidRadius;
267 float textureRadius;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500268 sk_sp<GrTextureProxy> profile(create_profile_texture(proxyProvider, circle, sigma,
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400269 &solidRadius, &textureRadius));
270 if (!profile) {
271 return nullptr;
272 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400273 return std::unique_ptr<GrFragmentProcessor>(new GrCircleBlurFragmentProcessor(
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500274 circle, textureRadius, solidRadius, std::move(profile)));
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400275 }
276}
277
278void main() {
279 // We just want to compute "(length(vec) - circleData.z + 0.5) * circleData.w" but need to
280 // rearrange for precision.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500281 half2 vec = half2(half((sk_FragCoord.x - circleData.x) * circleData.w),
282 half((sk_FragCoord.y - circleData.y) * circleData.w));
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400283 half dist = length(vec) + (0.5 - circleData.z) * circleData.w;
Ethan Nicholas13863662019-07-29 13:05:15 -0400284 sk_OutColor = sk_InColor * sample(blurProfileSampler, half2(dist, 0.5)).a;
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400285}
286
287@test(testData) {
288 SkScalar wh = testData->fRandom->nextRangeScalar(100.f, 1000.f);
289 SkScalar sigma = testData->fRandom->nextRangeF(1.f,10.f);
290 SkRect circle = SkRect::MakeWH(wh, wh);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500291 return GrCircleBlurFragmentProcessor::Make(testData->proxyProvider(), circle, sigma);
Brian Salomonaff329b2017-08-11 09:40:37 -0400292}