GrCircleBlurFragmentProcessor.cpp revision 5b5f096a038259b8d9084834f877588a0db80250
1/* 2 * Copyright 2017 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 "GrResourceProvider.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(GrResourceProvider* resourceProvider, 170 const SkRect& circle, float sigma, 171 float* solidRadius, float* textureRadius) { 172 float circleR = circle.width() / 2.0f; 173 // Profile textures are cached by the ratio of sigma to circle radius and by the size of the 174 // profile texture (binned by powers of 2). 175 SkScalar sigmaToCircleRRatio = sigma / circleR; 176 // When sigma is really small this becomes a equivalent to convolving a Gaussian with a 177 // half-plane. Similarly, in the extreme high ratio cases circle becomes a point WRT to the 178 // Guassian and the profile texture is a just a Gaussian evaluation. However, we haven't yet 179 // implemented this latter optimization. 180 sigmaToCircleRRatio = SkTMin(sigmaToCircleRRatio, 8.f); 181 SkFixed sigmaToCircleRRatioFixed; 182 static const SkScalar kHalfPlaneThreshold = 0.1f; 183 bool useHalfPlaneApprox = false; 184 if (sigmaToCircleRRatio <= kHalfPlaneThreshold) { 185 useHalfPlaneApprox = true; 186 sigmaToCircleRRatioFixed = 0; 187 *solidRadius = circleR - 3 * sigma; 188 *textureRadius = 6 * sigma; 189 } else { 190 // Convert to fixed point for the key. 191 sigmaToCircleRRatioFixed = SkScalarToFixed(sigmaToCircleRRatio); 192 // We shave off some bits to reduce the number of unique entries. We could probably 193 // shave off more than we do. 194 sigmaToCircleRRatioFixed &= ~0xff; 195 sigmaToCircleRRatio = SkFixedToScalar(sigmaToCircleRRatioFixed); 196 sigma = circleR * sigmaToCircleRRatio; 197 *solidRadius = 0; 198 *textureRadius = circleR + 3 * sigma; 199 } 200 201 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); 202 GrUniqueKey key; 203 GrUniqueKey::Builder builder(&key, kDomain, 1); 204 builder[0] = sigmaToCircleRRatioFixed; 205 builder.finish(); 206 207 sk_sp<GrTextureProxy> blurProfile = 208 resourceProvider->findProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin); 209 if (!blurProfile) { 210 static constexpr int kProfileTextureWidth = 512; 211 GrSurfaceDesc texDesc; 212 texDesc.fOrigin = kTopLeft_GrSurfaceOrigin; 213 texDesc.fWidth = kProfileTextureWidth; 214 texDesc.fHeight = 1; 215 texDesc.fConfig = kAlpha_8_GrPixelConfig; 216 217 std::unique_ptr<uint8_t[]> profile(nullptr); 218 if (useHalfPlaneApprox) { 219 profile.reset(create_half_plane_profile(kProfileTextureWidth)); 220 } else { 221 // Rescale params to the size of the texture we're creating. 222 SkScalar scale = kProfileTextureWidth / *textureRadius; 223 profile.reset( 224 create_circle_profile(sigma * scale, circleR * scale, kProfileTextureWidth)); 225 } 226 227 blurProfile = GrSurfaceProxy::MakeDeferred(resourceProvider, texDesc, SkBudgeted::kYes, 228 profile.get(), 0); 229 if (!blurProfile) { 230 return nullptr; 231 } 232 233 SkASSERT(blurProfile->origin() == kTopLeft_GrSurfaceOrigin); 234 resourceProvider->assignUniqueKeyToProxy(key, blurProfile.get()); 235 } 236 237 return blurProfile; 238} 239 240std::unique_ptr<GrFragmentProcessor> GrCircleBlurFragmentProcessor::Make( 241 GrResourceProvider* resourceProvider, const SkRect& circle, float sigma) { 242 float solidRadius; 243 float textureRadius; 244 sk_sp<GrTextureProxy> profile( 245 create_profile_texture(resourceProvider, circle, sigma, &solidRadius, &textureRadius)); 246 if (!profile) { 247 return nullptr; 248 } 249 return std::unique_ptr<GrFragmentProcessor>(new GrCircleBlurFragmentProcessor( 250 circle, textureRadius, solidRadius, std::move(profile), resourceProvider)); 251} 252#include "glsl/GrGLSLColorSpaceXformHelper.h" 253#include "glsl/GrGLSLFragmentProcessor.h" 254#include "glsl/GrGLSLFragmentShaderBuilder.h" 255#include "glsl/GrGLSLProgramBuilder.h" 256#include "SkSLCPP.h" 257#include "SkSLUtil.h" 258class GrGLSLCircleBlurFragmentProcessor : public GrGLSLFragmentProcessor { 259public: 260 GrGLSLCircleBlurFragmentProcessor() {} 261 void emitCode(EmitArgs& args) override { 262 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; 263 const GrCircleBlurFragmentProcessor& _outer = 264 args.fFp.cast<GrCircleBlurFragmentProcessor>(); 265 (void)_outer; 266 fCircleDataVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kVec4f_GrSLType, 267 kDefault_GrSLPrecision, "circleData"); 268 fragBuilder->codeAppendf( 269 "float2 vec = float2((sk_FragCoord.x - %s.x) * %s.w, (sk_FragCoord.y - %s.y) * " 270 "%s.w);\nfloat dist = length(vec) + (0.5 - %s.z) * %s.w;\n%s = %s * texture(%s, " 271 "float2(dist, 0.5)).%s.w;\n", 272 args.fUniformHandler->getUniformCStr(fCircleDataVar), 273 args.fUniformHandler->getUniformCStr(fCircleDataVar), 274 args.fUniformHandler->getUniformCStr(fCircleDataVar), 275 args.fUniformHandler->getUniformCStr(fCircleDataVar), 276 args.fUniformHandler->getUniformCStr(fCircleDataVar), 277 args.fUniformHandler->getUniformCStr(fCircleDataVar), args.fOutputColor, 278 args.fInputColor ? args.fInputColor : "float4(1)", 279 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(), 280 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str()); 281 } 282 283private: 284 void onSetData(const GrGLSLProgramDataManager& data, 285 const GrFragmentProcessor& _proc) override { 286 const GrCircleBlurFragmentProcessor& _outer = _proc.cast<GrCircleBlurFragmentProcessor>(); 287 auto circleRect = _outer.circleRect(); 288 (void)circleRect; 289 auto textureRadius = _outer.textureRadius(); 290 (void)textureRadius; 291 auto solidRadius = _outer.solidRadius(); 292 (void)solidRadius; 293 UniformHandle& blurProfileSampler = fBlurProfileSamplerVar; 294 (void)blurProfileSampler; 295 UniformHandle& circleData = fCircleDataVar; 296 (void)circleData; 297 298 data.set4f(circleData, circleRect.centerX(), circleRect.centerY(), solidRadius, 299 1.f / textureRadius); 300 } 301 UniformHandle fCircleDataVar; 302 UniformHandle fBlurProfileSamplerVar; 303}; 304GrGLSLFragmentProcessor* GrCircleBlurFragmentProcessor::onCreateGLSLInstance() const { 305 return new GrGLSLCircleBlurFragmentProcessor(); 306} 307void GrCircleBlurFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps, 308 GrProcessorKeyBuilder* b) const {} 309bool GrCircleBlurFragmentProcessor::onIsEqual(const GrFragmentProcessor& other) const { 310 const GrCircleBlurFragmentProcessor& that = other.cast<GrCircleBlurFragmentProcessor>(); 311 (void)that; 312 if (fCircleRect != that.fCircleRect) return false; 313 if (fTextureRadius != that.fTextureRadius) return false; 314 if (fSolidRadius != that.fSolidRadius) return false; 315 if (fBlurProfileSampler != that.fBlurProfileSampler) return false; 316 return true; 317} 318GrCircleBlurFragmentProcessor::GrCircleBlurFragmentProcessor( 319 const GrCircleBlurFragmentProcessor& src) 320 : INHERITED(src.optimizationFlags()) 321 , fCircleRect(src.fCircleRect) 322 , fTextureRadius(src.fTextureRadius) 323 , fSolidRadius(src.fSolidRadius) 324 , fBlurProfileSampler(src.fBlurProfileSampler) { 325 this->initClassID<GrCircleBlurFragmentProcessor>(); 326 this->addTextureSampler(&fBlurProfileSampler); 327} 328std::unique_ptr<GrFragmentProcessor> GrCircleBlurFragmentProcessor::clone() const { 329 return std::unique_ptr<GrFragmentProcessor>(new GrCircleBlurFragmentProcessor(*this)); 330} 331GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrCircleBlurFragmentProcessor); 332#if GR_TEST_UTILS 333std::unique_ptr<GrFragmentProcessor> GrCircleBlurFragmentProcessor::TestCreate( 334 GrProcessorTestData* testData) { 335 SkScalar wh = testData->fRandom->nextRangeScalar(100.f, 1000.f); 336 SkScalar sigma = testData->fRandom->nextRangeF(1.f, 10.f); 337 SkRect circle = SkRect::MakeWH(wh, wh); 338 return GrCircleBlurFragmentProcessor::Make(testData->resourceProvider(), circle, sigma); 339} 340#endif 341#endif 342