robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 1 | /* |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 2 | * Copyright 2017 Google Inc. |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 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 Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 8 | /* |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 9 | * This file was autogenerated from GrCircleBlurFragmentProcessor.fp; do not modify. |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 10 | */ |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 11 | #include "GrCircleBlurFragmentProcessor.h" |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 12 | #if SK_SUPPORT_GPU |
| 13 | |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 14 | #include "GrResourceProvider.h" |
Ethan Nicholas | 818ac5a | 2017-07-10 13:12:07 +0000 | [diff] [blame] | 15 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 16 | // Computes an unnormalized half kernel (right side). Returns the summation of all the half |
| 17 | // kernel values. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 18 | static 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; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 22 | // Compute half kernel values at half pixel steps out from the center. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 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; |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 29 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 30 | return tot; |
| 31 | } |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 32 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 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. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 35 | static void make_half_kernel_and_summed_table(float* halfKernel, float* summedHalfKernel, |
| 36 | int halfKernelSize, float sigma) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 37 | // The half kernel should sum to 0.5 not 1.0. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 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; |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 44 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 45 | } |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 46 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 47 | // Applies the 1D half kernel vertically at points along the x axis to a circle centered at the |
| 48 | // origin with radius circleR. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 49 | void 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; |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 56 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 57 | float y = sqrtf(circleR * circleR - x * x); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 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. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 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; |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 67 | } else { |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 68 | float yFrac = y - yInt; |
| 69 | results[i] = (1.f - yFrac) * summedHalfKernelTable[yInt] + |
| 70 | yFrac * summedHalfKernelTable[yInt + 1]; |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 71 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 72 | } |
| 73 | } |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 74 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 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. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 79 | static uint8_t eval_at(float evalX, float circleR, const float* halfKernel, int halfKernelSize, |
| 80 | const float* yKernelEvaluations) { |
| 81 | float acc = 0; |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 82 | |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 83 | float x = evalX - halfKernelSize; |
| 84 | for (int i = 0; i < halfKernelSize; ++i, x += 1.f) { |
| 85 | if (x < -circleR || x > circleR) { |
| 86 | continue; |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 87 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 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]; |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 97 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 98 | // Since we applied a half kernel in y we multiply acc by 2 (the circle is symmetric about |
| 99 | // the x axis). |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 100 | return SkUnitScalarClampToByte(2.f * acc); |
| 101 | } |
| 102 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 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. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 110 | static 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 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 114 | // The full kernel is 6 sigmas wide. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 115 | int halfKernelSize = SkScalarCeilToInt(6.0f * sigma); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 116 | // round up to next multiple of 2 and then divide by 2 |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 117 | halfKernelSize = ((halfKernelSize + 1) & ~1) >> 1; |
| 118 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 119 | // Number of x steps at which to apply kernel in y to cover all the profile samples in x. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 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 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 135 | // Ensure the tail of the Gaussian goes to zero. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 136 | weights[numSteps - 1] = 0; |
| 137 | return weights; |
| 138 | } |
| 139 | |
| 140 | static uint8_t* create_half_plane_profile(int profileWidth) { |
| 141 | SkASSERT(!(profileWidth & 0x1)); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 142 | // The full kernel is 6 sigmas wide. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 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 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 149 | // The half kernel should sum to 0.5. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 150 | const float tot = 2.f * make_unnormalized_half_kernel(halfKernel.get(), halfKernelSize, sigma); |
| 151 | float sum = 0.f; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 152 | // Populate the profile from the right edge to the middle. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 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 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 158 | // Populate the profile from the middle to the left edge (by flipping the half kernel and |
| 159 | // continuing the summation). |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 160 | for (int i = 0; i < halfKernelSize; ++i) { |
| 161 | sum += halfKernel[i]; |
| 162 | profile[halfKernelSize - i - 1] = SkUnitScalarClampToByte(sum); |
| 163 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 164 | // Ensure tail goes to 0. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 165 | profile[profileWidth - 1] = 0; |
| 166 | return profile; |
| 167 | } |
| 168 | |
| 169 | static 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; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 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). |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 175 | SkScalar sigmaToCircleRRatio = sigma / circleR; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 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. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 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 { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 190 | // Convert to fixed point for the key. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 191 | sigmaToCircleRRatioFixed = SkScalarToFixed(sigmaToCircleRRatio); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 192 | // We shave off some bits to reduce the number of unique entries. We could probably |
| 193 | // shave off more than we do. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 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 | |
Ethan Nicholas | 480c90a | 2017-07-25 16:45:15 -0400 | [diff] [blame] | 207 | sk_sp<GrTextureProxy> blurProfile = |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 208 | resourceProvider->findOrCreateProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 209 | if (!blurProfile) { |
| 210 | static constexpr int kProfileTextureWidth = 512; |
| 211 | GrSurfaceDesc texDesc; |
Robert Phillips | c686ce3 | 2017-07-21 14:12:29 -0400 | [diff] [blame] | 212 | texDesc.fOrigin = kTopLeft_GrSurfaceOrigin; |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 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 { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 221 | // Rescale params to the size of the texture we're creating. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 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) { |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 230 | return nullptr; |
| 231 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 232 | |
Robert Phillips | c686ce3 | 2017-07-21 14:12:29 -0400 | [diff] [blame] | 233 | SkASSERT(blurProfile->origin() == kTopLeft_GrSurfaceOrigin); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 234 | resourceProvider->assignUniqueKeyToProxy(key, blurProfile.get()); |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 235 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 236 | |
| 237 | return blurProfile; |
| 238 | } |
| 239 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 240 | std::unique_ptr<GrFragmentProcessor> GrCircleBlurFragmentProcessor::Make( |
| 241 | GrResourceProvider* resourceProvider, const SkRect& circle, float sigma) { |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 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 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 249 | return std::unique_ptr<GrFragmentProcessor>(new GrCircleBlurFragmentProcessor( |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 250 | circle, textureRadius, solidRadius, std::move(profile), resourceProvider)); |
| 251 | } |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 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" |
| 258 | class GrGLSLCircleBlurFragmentProcessor : public GrGLSLFragmentProcessor { |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 259 | public: |
| 260 | GrGLSLCircleBlurFragmentProcessor() {} |
| 261 | void emitCode(EmitArgs& args) override { |
| 262 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 263 | const GrCircleBlurFragmentProcessor& _outer = |
| 264 | args.fFp.cast<GrCircleBlurFragmentProcessor>(); |
| 265 | (void)_outer; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 266 | fCircleDataVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 267 | kDefault_GrSLPrecision, "circleData"); |
| 268 | fragBuilder->codeAppendf( |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 269 | "half2 vec = half2(half((sk_FragCoord.x - float(%s.x)) * float(%s.w)), " |
| 270 | "half((sk_FragCoord.y - float(%s.y)) * float(%s.w)));\nhalf dist = " |
| 271 | "float(length(vec)) + (0.5 - float(%s.z)) * float(%s.w);\n%s = %s * texture(%s, " |
| 272 | "float2(half2(dist, 0.5))).%s.w;\n", |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 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), |
| 278 | args.fUniformHandler->getUniformCStr(fCircleDataVar), args.fOutputColor, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 279 | args.fInputColor ? args.fInputColor : "half4(1)", |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 280 | fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(), |
| 281 | fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str()); |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 282 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 283 | |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 284 | private: |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 285 | void onSetData(const GrGLSLProgramDataManager& data, |
| 286 | const GrFragmentProcessor& _proc) override { |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 287 | const GrCircleBlurFragmentProcessor& _outer = _proc.cast<GrCircleBlurFragmentProcessor>(); |
| 288 | auto circleRect = _outer.circleRect(); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 289 | (void)circleRect; |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 290 | auto textureRadius = _outer.textureRadius(); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 291 | (void)textureRadius; |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 292 | auto solidRadius = _outer.solidRadius(); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 293 | (void)solidRadius; |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 294 | UniformHandle& blurProfileSampler = fBlurProfileSamplerVar; |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 295 | (void)blurProfileSampler; |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 296 | UniformHandle& circleData = fCircleDataVar; |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 297 | (void)circleData; |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 298 | |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 299 | data.set4f(circleData, circleRect.centerX(), circleRect.centerY(), solidRadius, |
| 300 | 1.f / textureRadius); |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 301 | } |
| 302 | UniformHandle fCircleDataVar; |
| 303 | UniformHandle fBlurProfileSamplerVar; |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 304 | }; |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 305 | GrGLSLFragmentProcessor* GrCircleBlurFragmentProcessor::onCreateGLSLInstance() const { |
| 306 | return new GrGLSLCircleBlurFragmentProcessor(); |
Ethan Nicholas | 818ac5a | 2017-07-10 13:12:07 +0000 | [diff] [blame] | 307 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 308 | void GrCircleBlurFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 309 | GrProcessorKeyBuilder* b) const {} |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 310 | bool GrCircleBlurFragmentProcessor::onIsEqual(const GrFragmentProcessor& other) const { |
| 311 | const GrCircleBlurFragmentProcessor& that = other.cast<GrCircleBlurFragmentProcessor>(); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 312 | (void)that; |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 313 | if (fCircleRect != that.fCircleRect) return false; |
| 314 | if (fTextureRadius != that.fTextureRadius) return false; |
| 315 | if (fSolidRadius != that.fSolidRadius) return false; |
| 316 | if (fBlurProfileSampler != that.fBlurProfileSampler) return false; |
| 317 | return true; |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 318 | } |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 319 | GrCircleBlurFragmentProcessor::GrCircleBlurFragmentProcessor( |
| 320 | const GrCircleBlurFragmentProcessor& src) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 321 | : INHERITED(kGrCircleBlurFragmentProcessor_ClassID, src.optimizationFlags()) |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 322 | , fCircleRect(src.fCircleRect) |
| 323 | , fTextureRadius(src.fTextureRadius) |
| 324 | , fSolidRadius(src.fSolidRadius) |
| 325 | , fBlurProfileSampler(src.fBlurProfileSampler) { |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 326 | this->addTextureSampler(&fBlurProfileSampler); |
| 327 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 328 | std::unique_ptr<GrFragmentProcessor> GrCircleBlurFragmentProcessor::clone() const { |
| 329 | return std::unique_ptr<GrFragmentProcessor>(new GrCircleBlurFragmentProcessor(*this)); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 330 | } |
Ethan Nicholas | 818ac5a | 2017-07-10 13:12:07 +0000 | [diff] [blame] | 331 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrCircleBlurFragmentProcessor); |
Ethan Nicholas | 818ac5a | 2017-07-10 13:12:07 +0000 | [diff] [blame] | 332 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 333 | std::unique_ptr<GrFragmentProcessor> GrCircleBlurFragmentProcessor::TestCreate( |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 334 | GrProcessorTestData* testData) { |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 335 | SkScalar wh = testData->fRandom->nextRangeScalar(100.f, 1000.f); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 336 | SkScalar sigma = testData->fRandom->nextRangeF(1.f, 10.f); |
Brian Salomon | cda2015 | 2017-07-17 16:44:32 -0400 | [diff] [blame] | 337 | SkRect circle = SkRect::MakeWH(wh, wh); |
| 338 | return GrCircleBlurFragmentProcessor::Make(testData->resourceProvider(), circle, sigma); |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 339 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 340 | #endif |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 341 | #endif |