blob: 3e80ef517c860b95e88e34fd2a30b04846bdcd74 [file] [log] [blame]
robertphillips30c4cae2015-09-15 10:20:55 -07001/*
2 * Copyright 2015 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#include "GrCircleBlurFragmentProcessor.h"
9
10#if SK_SUPPORT_GPU
11
12#include "GrContext.h"
egdaniel7ea439b2015-12-03 09:20:44 -080013#include "GrInvariantOutput.h"
robertphillips30c4cae2015-09-15 10:20:55 -070014#include "GrTextureProvider.h"
15
egdaniel64c47282015-11-13 06:54:19 -080016#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080017#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070018#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080019#include "glsl/GrGLSLUniformHandler.h"
robertphillips30c4cae2015-09-15 10:20:55 -070020
benjaminwagner6c71e0a2016-04-07 08:49:31 -070021#include "SkFixed.h"
22
bsalomonf7fcdb22016-06-14 14:37:12 -070023class GrCircleBlurFragmentProcessor::GLSLProcessor : public GrGLSLFragmentProcessor {
robertphillips30c4cae2015-09-15 10:20:55 -070024public:
robertphillips30c4cae2015-09-15 10:20:55 -070025 void emitCode(EmitArgs&) override;
26
27protected:
egdaniel018fb622015-10-28 07:26:40 -070028 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
robertphillips30c4cae2015-09-15 10:20:55 -070029
30private:
egdaniel018fb622015-10-28 07:26:40 -070031 GrGLSLProgramDataManager::UniformHandle fDataUniform;
robertphillips30c4cae2015-09-15 10:20:55 -070032
egdaniel64c47282015-11-13 06:54:19 -080033 typedef GrGLSLFragmentProcessor INHERITED;
robertphillips30c4cae2015-09-15 10:20:55 -070034};
35
bsalomonf7fcdb22016-06-14 14:37:12 -070036void GrCircleBlurFragmentProcessor::GLSLProcessor::emitCode(EmitArgs& args) {
robertphillips30c4cae2015-09-15 10:20:55 -070037 const char *dataName;
38
39 // The data is formatted as:
40 // x,y - the center of the circle
bsalomonaf68fa12016-08-16 09:24:57 -070041 // z - inner radius that should map to 0th entry in the texture.
bsalomonf7fcdb22016-06-14 14:37:12 -070042 // w - the inverse of the distance over which the texture is stretched.
cdalton5e58cee2016-02-11 12:49:47 -080043 fDataUniform = args.fUniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -080044 kVec4f_GrSLType,
45 kDefault_GrSLPrecision,
46 "data",
47 &dataName);
robertphillips30c4cae2015-09-15 10:20:55 -070048
cdalton85285412016-02-18 12:37:07 -080049 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel4ca2e602015-11-18 08:01:26 -080050 const char *fragmentPos = fragBuilder->fragmentPosition();
robertphillips30c4cae2015-09-15 10:20:55 -070051
52 if (args.fInputColor) {
egdaniel4ca2e602015-11-18 08:01:26 -080053 fragBuilder->codeAppendf("vec4 src=%s;", args.fInputColor);
robertphillips30c4cae2015-09-15 10:20:55 -070054 } else {
egdaniel4ca2e602015-11-18 08:01:26 -080055 fragBuilder->codeAppendf("vec4 src=vec4(1);");
robertphillips30c4cae2015-09-15 10:20:55 -070056 }
57
bsalomonaf68fa12016-08-16 09:24:57 -070058 // We just want to compute "(length(vec) - %s.z + 0.5) * %s.w" but need to rearrange
59 // for precision.
halcanary9d524f22016-03-29 09:03:52 -070060 fragBuilder->codeAppendf("vec2 vec = vec2( (%s.x - %s.x) * %s.w , (%s.y - %s.y) * %s.w );",
robertphillips4e567722015-12-10 13:29:14 -080061 fragmentPos, dataName, dataName,
62 fragmentPos, dataName, dataName);
bsalomonaf68fa12016-08-16 09:24:57 -070063 fragBuilder->codeAppendf("float dist = length(vec) + (0.5 - %s.z) * %s.w;",
robertphillips4e567722015-12-10 13:29:14 -080064 dataName, dataName);
robertphillips30c4cae2015-09-15 10:20:55 -070065
egdaniel4ca2e602015-11-18 08:01:26 -080066 fragBuilder->codeAppendf("float intensity = ");
cdalton3f6f76f2016-04-11 12:18:09 -070067 fragBuilder->appendTextureLookup(args.fTexSamplers[0], "vec2(dist, 0.5)");
egdaniel4ca2e602015-11-18 08:01:26 -080068 fragBuilder->codeAppend(".a;");
robertphillips30c4cae2015-09-15 10:20:55 -070069
egdaniel4ca2e602015-11-18 08:01:26 -080070 fragBuilder->codeAppendf("%s = src * intensity;\n", args.fOutputColor );
robertphillips30c4cae2015-09-15 10:20:55 -070071}
72
bsalomonf7fcdb22016-06-14 14:37:12 -070073void GrCircleBlurFragmentProcessor::GLSLProcessor::onSetData(const GrGLSLProgramDataManager& pdman,
74 const GrProcessor& proc) {
robertphillips30c4cae2015-09-15 10:20:55 -070075 const GrCircleBlurFragmentProcessor& cbfp = proc.cast<GrCircleBlurFragmentProcessor>();
bsalomonf7fcdb22016-06-14 14:37:12 -070076 const SkRect& circle = cbfp.fCircle;
robertphillips30c4cae2015-09-15 10:20:55 -070077
78 // The data is formatted as:
79 // x,y - the center of the circle
bsalomonaf68fa12016-08-16 09:24:57 -070080 // z - inner radius that should map to 0th entry in the texture.
bsalomonf7fcdb22016-06-14 14:37:12 -070081 // w - the inverse of the distance over which the profile texture is stretched.
82 pdman.set4f(fDataUniform, circle.centerX(), circle.centerY(), cbfp.fSolidRadius,
83 1.f / cbfp.fTextureRadius);
robertphillips30c4cae2015-09-15 10:20:55 -070084}
85
86///////////////////////////////////////////////////////////////////////////////
87
88GrCircleBlurFragmentProcessor::GrCircleBlurFragmentProcessor(const SkRect& circle,
bsalomonf7fcdb22016-06-14 14:37:12 -070089 float textureRadius,
bsalomonaf68fa12016-08-16 09:24:57 -070090 float solidRadius,
halcanary9d524f22016-03-29 09:03:52 -070091 GrTexture* blurProfile)
robertphillips30c4cae2015-09-15 10:20:55 -070092 : fCircle(circle)
bsalomonf7fcdb22016-06-14 14:37:12 -070093 , fSolidRadius(solidRadius)
94 , fTextureRadius(textureRadius)
robertphillips30c4cae2015-09-15 10:20:55 -070095 , fBlurProfileAccess(blurProfile, GrTextureParams::kBilerp_FilterMode) {
96 this->initClassID<GrCircleBlurFragmentProcessor>();
97 this->addTextureAccess(&fBlurProfileAccess);
98 this->setWillReadFragmentPosition();
99}
100
egdaniel57d3b032015-11-13 11:57:27 -0800101GrGLSLFragmentProcessor* GrCircleBlurFragmentProcessor::onCreateGLSLInstance() const {
bsalomonf7fcdb22016-06-14 14:37:12 -0700102 return new GLSLProcessor;
robertphillips30c4cae2015-09-15 10:20:55 -0700103}
104
egdaniel57d3b032015-11-13 11:57:27 -0800105void GrCircleBlurFragmentProcessor::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
106 GrProcessorKeyBuilder* b) const {
bsalomonf7fcdb22016-06-14 14:37:12 -0700107 // The code for this processor is always the same so there is nothing to add to the key.
108 return;
robertphillips30c4cae2015-09-15 10:20:55 -0700109}
110
111void GrCircleBlurFragmentProcessor::onComputeInvariantOutput(GrInvariantOutput* inout) const {
112 inout->mulByUnknownSingleComponent();
113}
114
bsalomonaf68fa12016-08-16 09:24:57 -0700115// Computes an unnormalized half kernel (right side). Returns the summation of all the half kernel
116// values.
117static float make_unnormalized_half_kernel(float* halfKernel, int halfKernelSize, float sigma) {
bsalomonb5257212016-05-19 15:52:34 -0700118 const float invSigma = 1.f / sigma;
119 const float b = -0.5f * invSigma * invSigma;
robertphillips30c4cae2015-09-15 10:20:55 -0700120 float tot = 0.0f;
bsalomonb5257212016-05-19 15:52:34 -0700121 // Compute half kernel values at half pixel steps out from the center.
122 float t = 0.5f;
123 for (int i = 0; i < halfKernelSize; ++i) {
124 float value = expf(t * t * b);
125 tot += value;
126 halfKernel[i] = value;
127 t += 1.f;
robertphillips30c4cae2015-09-15 10:20:55 -0700128 }
bsalomonaf68fa12016-08-16 09:24:57 -0700129 return tot;
130}
131
132// Create a Gaussian half-kernel (right side) and a summed area table given a sigma and number of
133// discrete steps. The half kernel is normalized to sum to 0.5.
134static void make_half_kernel_and_summed_table(float* halfKernel, float* summedHalfKernel,
135 int halfKernelSize, float sigma) {
bsalomonb5257212016-05-19 15:52:34 -0700136 // The half kernel should sum to 0.5 not 1.0.
bsalomonaf68fa12016-08-16 09:24:57 -0700137 const float tot = 2.f * make_unnormalized_half_kernel(halfKernel, halfKernelSize, sigma);
138 float sum = 0.f;
bsalomonb5257212016-05-19 15:52:34 -0700139 for (int i = 0; i < halfKernelSize; ++i) {
140 halfKernel[i] /= tot;
141 sum += halfKernel[i];
142 summedHalfKernel[i] = sum;
robertphillips30c4cae2015-09-15 10:20:55 -0700143 }
144}
145
bsalomon82ad93c2016-05-20 09:11:17 -0700146// Applies the 1D half kernel vertically at points along the x axis to a circle centered at the
147// origin with radius circleR.
148void apply_kernel_in_y(float* results, int numSteps, float firstX, float circleR,
149 int halfKernelSize, const float* summedHalfKernelTable) {
150 float x = firstX;
151 for (int i = 0; i < numSteps; ++i, x += 1.f) {
152 if (x < -circleR || x > circleR) {
153 results[i] = 0;
154 continue;
155 }
156 float y = sqrtf(circleR * circleR - x * x);
157 // In the column at x we exit the circle at +y and -y
158 // The summed table entry j is actually reflects an offset of j + 0.5.
159 y -= 0.5f;
160 int yInt = SkScalarFloorToInt(y);
161 SkASSERT(yInt >= -1);
162 if (y < 0) {
163 results[i] = (y + 0.5f) * summedHalfKernelTable[0];
164 } else if (yInt >= halfKernelSize - 1) {
165 results[i] = 0.5f;
166 } else {
167 float yFrac = y - yInt;
168 results[i] = (1.f - yFrac) * summedHalfKernelTable[yInt] +
169 yFrac * summedHalfKernelTable[yInt + 1];
170 }
bsalomonb5257212016-05-19 15:52:34 -0700171 }
172}
robertphillips30c4cae2015-09-15 10:20:55 -0700173
bsalomon82ad93c2016-05-20 09:11:17 -0700174// Apply a Gaussian at point (evalX, 0) to a circle centered at the origin with radius circleR.
175// This relies on having a half kernel computed for the Gaussian and a table of applications of
176// the half kernel in y to columns at (evalX - halfKernel, evalX - halfKernel + 1, ..., evalX +
177// halfKernel) passed in as yKernelEvaluations.
178static uint8_t eval_at(float evalX, float circleR, const float* halfKernel, int halfKernelSize,
179 const float* yKernelEvaluations) {
robertphillips30c4cae2015-09-15 10:20:55 -0700180 float acc = 0;
181
bsalomon82ad93c2016-05-20 09:11:17 -0700182 float x = evalX - halfKernelSize;
183 for (int i = 0; i < halfKernelSize; ++i, x += 1.f) {
bsalomonb5257212016-05-19 15:52:34 -0700184 if (x < -circleR || x > circleR) {
robertphillips30c4cae2015-09-15 10:20:55 -0700185 continue;
186 }
bsalomon82ad93c2016-05-20 09:11:17 -0700187 float verticalEval = yKernelEvaluations[i];
188 acc += verticalEval * halfKernel[halfKernelSize - i - 1];
robertphillips30c4cae2015-09-15 10:20:55 -0700189 }
bsalomon82ad93c2016-05-20 09:11:17 -0700190 for (int i = 0; i < halfKernelSize; ++i, x += 1.f) {
bsalomonb5257212016-05-19 15:52:34 -0700191 if (x < -circleR || x > circleR) {
192 continue;
193 }
bsalomon82ad93c2016-05-20 09:11:17 -0700194 float verticalEval = yKernelEvaluations[i + halfKernelSize];
bsalomonb5257212016-05-19 15:52:34 -0700195 acc += verticalEval * halfKernel[i];
196 }
197 // Since we applied a half kernel in y we multiply acc by 2 (the circle is symmetric about the
198 // x axis).
199 return SkUnitScalarClampToByte(2.f * acc);
robertphillips30c4cae2015-09-15 10:20:55 -0700200}
201
bsalomonb5257212016-05-19 15:52:34 -0700202// This function creates a profile of a blurred circle. It does this by computing a kernel for
bsalomon82ad93c2016-05-20 09:11:17 -0700203// half the Gaussian and a matching summed area table. The summed area table is used to compute
204// an array of vertical applications of the half kernel to the circle along the x axis. The table
205// of y evaluations has 2 * k + n entries where k is the size of the half kernel and n is the size
206// of the profile being computed. Then for each of the n profile entries we walk out k steps in each
207// horizontal direction multiplying the corresponding y evaluation by the half kernel entry and
208// sum these values to compute the profile entry.
bsalomonaf68fa12016-08-16 09:24:57 -0700209static uint8_t* create_circle_profile(float sigma, float circleR, int profileTextureWidth) {
bsalomonf7fcdb22016-06-14 14:37:12 -0700210 const int numSteps = profileTextureWidth;
robertphillips30c4cae2015-09-15 10:20:55 -0700211 uint8_t* weights = new uint8_t[numSteps];
bsalomonb5257212016-05-19 15:52:34 -0700212
213 // The full kernel is 6 sigmas wide.
214 int halfKernelSize = SkScalarCeilToInt(6.0f*sigma);
215 // round up to next multiple of 2 and then divide by 2
216 halfKernelSize = ((halfKernelSize + 1) & ~1) >> 1;
bsalomon82ad93c2016-05-20 09:11:17 -0700217
218 // Number of x steps at which to apply kernel in y to cover all the profile samples in x.
219 int numYSteps = numSteps + 2 * halfKernelSize;
220
221 SkAutoTArray<float> bulkAlloc(halfKernelSize + halfKernelSize + numYSteps);
222 float* halfKernel = bulkAlloc.get();
223 float* summedKernel = bulkAlloc.get() + halfKernelSize;
224 float* yEvals = bulkAlloc.get() + 2 * halfKernelSize;
225 make_half_kernel_and_summed_table(halfKernel, summedKernel, halfKernelSize, sigma);
226
bsalomonaf68fa12016-08-16 09:24:57 -0700227 float firstX = -halfKernelSize + 0.5f;
bsalomon82ad93c2016-05-20 09:11:17 -0700228 apply_kernel_in_y(yEvals, numYSteps, firstX, circleR, halfKernelSize, summedKernel);
229
benjaminwagner9d240232016-02-24 07:51:33 -0800230 for (int i = 0; i < numSteps - 1; ++i) {
bsalomonaf68fa12016-08-16 09:24:57 -0700231 float evalX = i + 0.5f;
bsalomon82ad93c2016-05-20 09:11:17 -0700232 weights[i] = eval_at(evalX, circleR, halfKernel, halfKernelSize, yEvals + i);
robertphillips30c4cae2015-09-15 10:20:55 -0700233 }
benjaminwagner9d240232016-02-24 07:51:33 -0800234 // Ensure the tail of the Gaussian goes to zero.
bsalomon3ab53d02016-05-18 07:15:46 -0700235 weights[numSteps - 1] = 0;
robertphillips30c4cae2015-09-15 10:20:55 -0700236 return weights;
237}
238
bsalomonaf68fa12016-08-16 09:24:57 -0700239static uint8_t* create_half_plane_profile(int profileWidth) {
240 SkASSERT(!(profileWidth & 0x1));
241 // The full kernel is 6 sigmas wide.
242 float sigma = profileWidth / 6.f;
243 int halfKernelSize = profileWidth / 2;
244
245 SkAutoTArray<float> halfKernel(halfKernelSize);
246 uint8_t* profile = new uint8_t[profileWidth];
247
248 // The half kernel should sum to 0.5.
249 const float tot = 2.f * make_unnormalized_half_kernel(halfKernel.get(), halfKernelSize, sigma);
250 float sum = 0.f;
251 // Populate the profile from the right edge to the middle.
252 for (int i = 0; i < halfKernelSize; ++i) {
253 halfKernel[halfKernelSize - i - 1] /= tot;
254 sum += halfKernel[halfKernelSize - i - 1];
255 profile[profileWidth - i - 1] = SkUnitScalarClampToByte(sum);
256 }
257 // Populate the profile from the middle to the left edge (by flipping the half kernel and
258 // continuing the summation).
259 for (int i = 0; i < halfKernelSize; ++i) {
260 sum += halfKernel[i];
261 profile[halfKernelSize - i - 1] = SkUnitScalarClampToByte(sum);
262 }
263 // Ensure tail goes to 0.
264 profile[profileWidth - 1] = 0;
265 return profile;
bsalomonf7fcdb22016-06-14 14:37:12 -0700266}
267
bsalomonaf68fa12016-08-16 09:24:57 -0700268static GrTexture* create_profile_texture(GrTextureProvider* textureProvider, const SkRect& circle,
269 float sigma, float* solidRadius, float* textureRadius) {
bsalomon3ab53d02016-05-18 07:15:46 -0700270 float circleR = circle.width() / 2.0f;
bsalomonf7fcdb22016-06-14 14:37:12 -0700271 // Profile textures are cached by the ratio of sigma to circle radius and by the size of the
272 // profile texture (binned by powers of 2).
273 SkScalar sigmaToCircleRRatio = sigma / circleR;
274 // When sigma is really small this becomes a equivalent to convolving a Gaussian with a half-
bsalomonaf68fa12016-08-16 09:24:57 -0700275 // plane. Similarly, in the extreme high ratio cases circle becomes a point WRT to the Guassian
276 // and the profile texture is a just a Gaussian evaluation. However, we haven't yet implemented
277 // this latter optimization.
278 sigmaToCircleRRatio = SkTMin(sigmaToCircleRRatio, 8.f);
279 SkFixed sigmaToCircleRRatioFixed;
280 static const SkScalar kHalfPlaneThreshold = 0.1f;
281 bool useHalfPlaneApprox = false;
282 if (sigmaToCircleRRatio <= kHalfPlaneThreshold) {
283 useHalfPlaneApprox = true;
284 sigmaToCircleRRatioFixed = 0;
285 *solidRadius = circleR - 3 * sigma;
286 *textureRadius = 6 * sigma;
bsalomonf7fcdb22016-06-14 14:37:12 -0700287 } else {
bsalomonaf68fa12016-08-16 09:24:57 -0700288 // Convert to fixed point for the key.
289 sigmaToCircleRRatioFixed = SkScalarToFixed(sigmaToCircleRRatio);
290 // We shave off some bits to reduce the number of unique entries. We could probably shave
291 // off more than we do.
292 sigmaToCircleRRatioFixed &= ~0xff;
293 sigmaToCircleRRatio = SkFixedToScalar(sigmaToCircleRRatioFixed);
294 sigma = circleR * sigmaToCircleRRatio;
295 *solidRadius = 0;
296 *textureRadius = circleR + 3 * sigma;
bsalomonf7fcdb22016-06-14 14:37:12 -0700297 }
robertphillips30c4cae2015-09-15 10:20:55 -0700298
299 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
300 GrUniqueKey key;
bsalomonaf68fa12016-08-16 09:24:57 -0700301 GrUniqueKey::Builder builder(&key, kDomain, 1);
bsalomonf7fcdb22016-06-14 14:37:12 -0700302 builder[0] = sigmaToCircleRRatioFixed;
robertphillips30c4cae2015-09-15 10:20:55 -0700303 builder.finish();
304
305 GrTexture *blurProfile = textureProvider->findAndRefTextureByUniqueKey(key);
robertphillips30c4cae2015-09-15 10:20:55 -0700306 if (!blurProfile) {
bsalomonaf68fa12016-08-16 09:24:57 -0700307 static constexpr int kProfileTextureWidth = 512;
bsalomon3ab53d02016-05-18 07:15:46 -0700308 GrSurfaceDesc texDesc;
bsalomonaf68fa12016-08-16 09:24:57 -0700309 texDesc.fWidth = kProfileTextureWidth;
bsalomon3ab53d02016-05-18 07:15:46 -0700310 texDesc.fHeight = 1;
311 texDesc.fConfig = kAlpha_8_GrPixelConfig;
312
bsalomonaf68fa12016-08-16 09:24:57 -0700313 SkAutoTDeleteArray<uint8_t> profile(nullptr);
314 if (useHalfPlaneApprox) {
315 profile.reset(create_half_plane_profile(kProfileTextureWidth));
316 } else {
317 // Rescale params to the size of the texture we're creating.
318 SkScalar scale = kProfileTextureWidth / *textureRadius;
319 profile.reset(create_circle_profile(sigma * scale, circleR * scale,
320 kProfileTextureWidth));
321 }
robertphillips30c4cae2015-09-15 10:20:55 -0700322
bsalomon5ec26ae2016-02-25 08:33:02 -0800323 blurProfile = textureProvider->createTexture(texDesc, SkBudgeted::kYes, profile.get(), 0);
robertphillips30c4cae2015-09-15 10:20:55 -0700324 if (blurProfile) {
325 textureProvider->assignUniqueKeyToTexture(key, blurProfile);
326 }
327 }
328
329 return blurProfile;
330}
331
bsalomonaf68fa12016-08-16 09:24:57 -0700332//////////////////////////////////////////////////////////////////////////////
333
334sk_sp<GrFragmentProcessor> GrCircleBlurFragmentProcessor::Make(GrTextureProvider*textureProvider,
335 const SkRect& circle, float sigma) {
336 float solidRadius;
337 float textureRadius;
338 SkAutoTUnref<GrTexture> profile(create_profile_texture(textureProvider, circle, sigma,
339 &solidRadius, &textureRadius));
340 if (!profile) {
341 return nullptr;
342 }
343 return sk_sp<GrFragmentProcessor>(new GrCircleBlurFragmentProcessor(circle, textureRadius,
344 solidRadius, profile));
345}
346
347//////////////////////////////////////////////////////////////////////////////
348
robertphillips30c4cae2015-09-15 10:20:55 -0700349GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrCircleBlurFragmentProcessor);
350
bungeman06ca8ec2016-06-09 08:01:03 -0700351sk_sp<GrFragmentProcessor> GrCircleBlurFragmentProcessor::TestCreate(GrProcessorTestData* d) {
robertphillips30c4cae2015-09-15 10:20:55 -0700352 SkScalar wh = d->fRandom->nextRangeScalar(100.f, 1000.f);
353 SkScalar sigma = d->fRandom->nextRangeF(1.f,10.f);
354 SkRect circle = SkRect::MakeWH(wh, wh);
bungeman06ca8ec2016-06-09 08:01:03 -0700355 return GrCircleBlurFragmentProcessor::Make(d->fContext->textureProvider(), circle, sigma);
robertphillips30c4cae2015-09-15 10:20:55 -0700356}
357
358#endif