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