Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1 | in half4 circleRect; |
| 2 | in half textureRadius; |
| 3 | in half solidRadius; |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 4 | in uniform sampler2D blurProfileSampler; |
| 5 | |
| 6 | // The data is formatted as: |
| 7 | // x, y - the center of the circle |
| 8 | // z - inner radius that should map to 0th entry in the texture. |
| 9 | // w - the inverse of the distance over which the texture is stretched. |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 10 | uniform half4 circleData; |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 11 | |
| 12 | @optimizationFlags { |
| 13 | kCompatibleWithCoverageAsAlpha_OptimizationFlag |
| 14 | } |
| 15 | |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 16 | @make { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 17 | static std::unique_ptr<GrFragmentProcessor> Make(GrProxyProvider*, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 18 | const SkRect& circle, float sigma); |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | @setData(data) { |
| 22 | data.set4f(circleData, circleRect.centerX(), circleRect.centerY(), solidRadius, |
| 23 | 1.f / textureRadius); |
| 24 | } |
| 25 | |
| 26 | @cpp { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 27 | #include "GrProxyProvider.h" |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 28 | |
| 29 | // Computes an unnormalized half kernel (right side). Returns the summation of all the half |
| 30 | // kernel values. |
| 31 | static float make_unnormalized_half_kernel(float* halfKernel, int halfKernelSize, float sigma) { |
| 32 | const float invSigma = 1.f / sigma; |
| 33 | const float b = -0.5f * invSigma * invSigma; |
| 34 | float tot = 0.0f; |
| 35 | // Compute half kernel values at half pixel steps out from the center. |
| 36 | float t = 0.5f; |
| 37 | for (int i = 0; i < halfKernelSize; ++i) { |
| 38 | float value = expf(t * t * b); |
| 39 | tot += value; |
| 40 | halfKernel[i] = value; |
| 41 | t += 1.f; |
| 42 | } |
| 43 | return tot; |
| 44 | } |
| 45 | |
| 46 | // Create a Gaussian half-kernel (right side) and a summed area table given a sigma and number |
| 47 | // of discrete steps. The half kernel is normalized to sum to 0.5. |
| 48 | static void make_half_kernel_and_summed_table(float* halfKernel, float* summedHalfKernel, |
| 49 | int halfKernelSize, float sigma) { |
| 50 | // The half kernel should sum to 0.5 not 1.0. |
| 51 | const float tot = 2.f * make_unnormalized_half_kernel(halfKernel, halfKernelSize, sigma); |
| 52 | float sum = 0.f; |
| 53 | for (int i = 0; i < halfKernelSize; ++i) { |
| 54 | halfKernel[i] /= tot; |
| 55 | sum += halfKernel[i]; |
| 56 | summedHalfKernel[i] = sum; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Applies the 1D half kernel vertically at points along the x axis to a circle centered at the |
| 61 | // origin with radius circleR. |
| 62 | void apply_kernel_in_y(float* results, int numSteps, float firstX, float circleR, |
| 63 | int halfKernelSize, const float* summedHalfKernelTable) { |
| 64 | float x = firstX; |
| 65 | for (int i = 0; i < numSteps; ++i, x += 1.f) { |
| 66 | if (x < -circleR || x > circleR) { |
| 67 | results[i] = 0; |
| 68 | continue; |
| 69 | } |
| 70 | float y = sqrtf(circleR * circleR - x * x); |
| 71 | // In the column at x we exit the circle at +y and -y |
| 72 | // The summed table entry j is actually reflects an offset of j + 0.5. |
| 73 | y -= 0.5f; |
| 74 | int yInt = SkScalarFloorToInt(y); |
| 75 | SkASSERT(yInt >= -1); |
| 76 | if (y < 0) { |
| 77 | results[i] = (y + 0.5f) * summedHalfKernelTable[0]; |
| 78 | } else if (yInt >= halfKernelSize - 1) { |
| 79 | results[i] = 0.5f; |
| 80 | } else { |
| 81 | float yFrac = y - yInt; |
| 82 | results[i] = (1.f - yFrac) * summedHalfKernelTable[yInt] + |
| 83 | yFrac * summedHalfKernelTable[yInt + 1]; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Apply a Gaussian at point (evalX, 0) to a circle centered at the origin with radius circleR. |
| 89 | // This relies on having a half kernel computed for the Gaussian and a table of applications of |
| 90 | // the half kernel in y to columns at (evalX - halfKernel, evalX - halfKernel + 1, ..., evalX + |
| 91 | // halfKernel) passed in as yKernelEvaluations. |
| 92 | static uint8_t eval_at(float evalX, float circleR, const float* halfKernel, int halfKernelSize, |
| 93 | const float* yKernelEvaluations) { |
| 94 | float acc = 0; |
| 95 | |
| 96 | float x = evalX - halfKernelSize; |
| 97 | for (int i = 0; i < halfKernelSize; ++i, x += 1.f) { |
| 98 | if (x < -circleR || x > circleR) { |
| 99 | continue; |
| 100 | } |
| 101 | float verticalEval = yKernelEvaluations[i]; |
| 102 | acc += verticalEval * halfKernel[halfKernelSize - i - 1]; |
| 103 | } |
| 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 + halfKernelSize]; |
| 109 | acc += verticalEval * halfKernel[i]; |
| 110 | } |
| 111 | // Since we applied a half kernel in y we multiply acc by 2 (the circle is symmetric about |
| 112 | // the x axis). |
| 113 | return SkUnitScalarClampToByte(2.f * acc); |
| 114 | } |
| 115 | |
| 116 | // This function creates a profile of a blurred circle. It does this by computing a kernel for |
| 117 | // half the Gaussian and a matching summed area table. The summed area table is used to compute |
| 118 | // an array of vertical applications of the half kernel to the circle along the x axis. The |
| 119 | // table of y evaluations has 2 * k + n entries where k is the size of the half kernel and n is |
| 120 | // the size of the profile being computed. Then for each of the n profile entries we walk out k |
| 121 | // steps in each horizontal direction multiplying the corresponding y evaluation by the half |
| 122 | // kernel entry and sum these values to compute the profile entry. |
| 123 | static uint8_t* create_circle_profile(float sigma, float circleR, int profileTextureWidth) { |
| 124 | const int numSteps = profileTextureWidth; |
| 125 | uint8_t* weights = new uint8_t[numSteps]; |
| 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 | return weights; |
| 151 | } |
| 152 | |
| 153 | static uint8_t* create_half_plane_profile(int profileWidth) { |
| 154 | SkASSERT(!(profileWidth & 0x1)); |
| 155 | // The full kernel is 6 sigmas wide. |
| 156 | float sigma = profileWidth / 6.f; |
| 157 | int halfKernelSize = profileWidth / 2; |
| 158 | |
| 159 | SkAutoTArray<float> halfKernel(halfKernelSize); |
| 160 | uint8_t* profile = new uint8_t[profileWidth]; |
| 161 | |
| 162 | // The half kernel should sum to 0.5. |
| 163 | const float tot = 2.f * make_unnormalized_half_kernel(halfKernel.get(), halfKernelSize, |
| 164 | sigma); |
| 165 | float sum = 0.f; |
| 166 | // Populate the profile from the right edge to the middle. |
| 167 | for (int i = 0; i < halfKernelSize; ++i) { |
| 168 | halfKernel[halfKernelSize - i - 1] /= tot; |
| 169 | sum += halfKernel[halfKernelSize - i - 1]; |
| 170 | profile[profileWidth - i - 1] = SkUnitScalarClampToByte(sum); |
| 171 | } |
| 172 | // Populate the profile from the middle to the left edge (by flipping the half kernel and |
| 173 | // continuing the summation). |
| 174 | for (int i = 0; i < halfKernelSize; ++i) { |
| 175 | sum += halfKernel[i]; |
| 176 | profile[halfKernelSize - i - 1] = SkUnitScalarClampToByte(sum); |
| 177 | } |
| 178 | // Ensure tail goes to 0. |
| 179 | profile[profileWidth - 1] = 0; |
| 180 | return profile; |
| 181 | } |
| 182 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 183 | static sk_sp<GrTextureProxy> create_profile_texture(GrProxyProvider* proxyProvider, |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 184 | const SkRect& circle, |
| 185 | float sigma, |
| 186 | float* solidRadius, float* textureRadius) { |
| 187 | float circleR = circle.width() / 2.0f; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 188 | if (circleR < SK_ScalarNearlyZero) { |
| 189 | return nullptr; |
| 190 | } |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 191 | // Profile textures are cached by the ratio of sigma to circle radius and by the size of the |
| 192 | // profile texture (binned by powers of 2). |
| 193 | SkScalar sigmaToCircleRRatio = sigma / circleR; |
| 194 | // When sigma is really small this becomes a equivalent to convolving a Gaussian with a |
| 195 | // half-plane. Similarly, in the extreme high ratio cases circle becomes a point WRT to the |
| 196 | // Guassian and the profile texture is a just a Gaussian evaluation. However, we haven't yet |
| 197 | // implemented this latter optimization. |
| 198 | sigmaToCircleRRatio = SkTMin(sigmaToCircleRRatio, 8.f); |
| 199 | SkFixed sigmaToCircleRRatioFixed; |
| 200 | static const SkScalar kHalfPlaneThreshold = 0.1f; |
| 201 | bool useHalfPlaneApprox = false; |
| 202 | if (sigmaToCircleRRatio <= kHalfPlaneThreshold) { |
| 203 | useHalfPlaneApprox = true; |
| 204 | sigmaToCircleRRatioFixed = 0; |
| 205 | *solidRadius = circleR - 3 * sigma; |
| 206 | *textureRadius = 6 * sigma; |
| 207 | } else { |
| 208 | // Convert to fixed point for the key. |
| 209 | sigmaToCircleRRatioFixed = SkScalarToFixed(sigmaToCircleRRatio); |
| 210 | // We shave off some bits to reduce the number of unique entries. We could probably |
| 211 | // shave off more than we do. |
| 212 | sigmaToCircleRRatioFixed &= ~0xff; |
| 213 | sigmaToCircleRRatio = SkFixedToScalar(sigmaToCircleRRatioFixed); |
| 214 | sigma = circleR * sigmaToCircleRRatio; |
| 215 | *solidRadius = 0; |
| 216 | *textureRadius = circleR + 3 * sigma; |
| 217 | } |
| 218 | |
| 219 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 220 | GrUniqueKey key; |
| 221 | GrUniqueKey::Builder builder(&key, kDomain, 1); |
| 222 | builder[0] = sigmaToCircleRRatioFixed; |
| 223 | builder.finish(); |
| 224 | |
Ethan Nicholas | 480c90a | 2017-07-25 16:45:15 -0400 | [diff] [blame] | 225 | sk_sp<GrTextureProxy> blurProfile = |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 226 | proxyProvider->findOrCreateProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin); |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 227 | if (!blurProfile) { |
| 228 | static constexpr int kProfileTextureWidth = 512; |
| 229 | GrSurfaceDesc texDesc; |
Ethan Nicholas | 480c90a | 2017-07-25 16:45:15 -0400 | [diff] [blame] | 230 | texDesc.fOrigin = kTopLeft_GrSurfaceOrigin; |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 231 | texDesc.fWidth = kProfileTextureWidth; |
| 232 | texDesc.fHeight = 1; |
| 233 | texDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 234 | |
| 235 | std::unique_ptr<uint8_t[]> profile(nullptr); |
| 236 | if (useHalfPlaneApprox) { |
| 237 | profile.reset(create_half_plane_profile(kProfileTextureWidth)); |
| 238 | } else { |
| 239 | // Rescale params to the size of the texture we're creating. |
| 240 | SkScalar scale = kProfileTextureWidth / *textureRadius; |
| 241 | profile.reset(create_circle_profile(sigma * scale, circleR * scale, |
| 242 | kProfileTextureWidth)); |
| 243 | } |
| 244 | |
Robert Phillips | 20df20c | 2018-01-16 10:54:33 -0500 | [diff] [blame] | 245 | blurProfile = proxyProvider->createTextureProxy(texDesc, SkBudgeted::kYes, |
| 246 | profile.get(), 0); |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 247 | if (!blurProfile) { |
| 248 | return nullptr; |
| 249 | } |
| 250 | |
Ethan Nicholas | 480c90a | 2017-07-25 16:45:15 -0400 | [diff] [blame] | 251 | SkASSERT(blurProfile->origin() == kTopLeft_GrSurfaceOrigin); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 252 | proxyProvider->assignUniqueKeyToProxy(key, blurProfile.get()); |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | return blurProfile; |
| 256 | } |
| 257 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 258 | std::unique_ptr<GrFragmentProcessor> GrCircleBlurFragmentProcessor::Make( |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 259 | GrProxyProvider* proxyProvider, const SkRect& circle, float sigma) { |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 260 | float solidRadius; |
| 261 | float textureRadius; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 262 | sk_sp<GrTextureProxy> profile(create_profile_texture(proxyProvider, circle, sigma, |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 263 | &solidRadius, &textureRadius)); |
| 264 | if (!profile) { |
| 265 | return nullptr; |
| 266 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 267 | return std::unique_ptr<GrFragmentProcessor>(new GrCircleBlurFragmentProcessor( |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 268 | circle, textureRadius, solidRadius, std::move(profile))); |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | void main() { |
| 273 | // We just want to compute "(length(vec) - circleData.z + 0.5) * circleData.w" but need to |
| 274 | // rearrange for precision. |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 275 | half2 vec = half2((sk_FragCoord.x - circleData.x) * circleData.w, |
| 276 | (sk_FragCoord.y - circleData.y) * circleData.w); |
| 277 | half dist = length(vec) + (0.5 - circleData.z) * circleData.w; |
| 278 | sk_OutColor = sk_InColor * texture(blurProfileSampler, half2(dist, 0.5)).a; |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | @test(testData) { |
| 282 | SkScalar wh = testData->fRandom->nextRangeScalar(100.f, 1000.f); |
| 283 | SkScalar sigma = testData->fRandom->nextRangeF(1.f,10.f); |
| 284 | SkRect circle = SkRect::MakeWH(wh, wh); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 285 | return GrCircleBlurFragmentProcessor::Make(testData->proxyProvider(), circle, sigma); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 286 | } |