tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 "GrConvolutionEffect.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 9 | #include "gl/GrGLProcessor.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 10 | #include "gl/GrGLTexture.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 11 | #include "gl/builders/GrGLProgramBuilder.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 12 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 13 | // For brevity |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 14 | typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 15 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 16 | class GrGLConvolutionEffect : public GrGLFragmentProcessor { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 17 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 18 | GrGLConvolutionEffect(const GrProcessor&); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame^] | 19 | |
| 20 | virtual void emitCode(GrGLFPBuilder*, |
| 21 | const GrFragmentProcessor&, |
| 22 | const char* outputColor, |
| 23 | const char* inputColor, |
| 24 | const TransformedCoordsArray&, |
| 25 | const TextureSamplerArray&) override; |
| 26 | |
| 27 | void setData(const GrGLProgramDataManager& pdman, const GrProcessor&) override; |
| 28 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 29 | static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 30 | |
ericrk | 0f38612 | 2015-07-21 13:15:47 -0700 | [diff] [blame] | 31 | private: |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame^] | 32 | int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); } |
| 33 | bool useBounds() const { return fUseBounds; } |
| 34 | Gr1DKernelEffect::Direction direction() const { return fDirection; } |
| 35 | |
| 36 | int fRadius; |
| 37 | bool fUseBounds; |
| 38 | Gr1DKernelEffect::Direction fDirection; |
| 39 | UniformHandle fKernelUni; |
| 40 | UniformHandle fImageIncrementUni; |
| 41 | UniformHandle fBoundsUni; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 42 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 43 | typedef GrGLFragmentProcessor INHERITED; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 46 | GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProcessor& processor) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 47 | const GrConvolutionEffect& c = processor.cast<GrConvolutionEffect>(); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 48 | fRadius = c.radius(); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame^] | 49 | fUseBounds = c.useBounds(); |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 50 | fDirection = c.direction(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 51 | } |
| 52 | |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame^] | 53 | void GrGLConvolutionEffect::emitCode(GrGLFPBuilder* builder, |
| 54 | const GrFragmentProcessor&, |
| 55 | const char* outputColor, |
| 56 | const char* inputColor, |
| 57 | const TransformedCoordsArray& coords, |
| 58 | const TextureSamplerArray& samplers) { |
| 59 | fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| 60 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 61 | "ImageIncrement"); |
| 62 | if (this->useBounds()) { |
| 63 | fBoundsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| 64 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 65 | "Bounds"); |
| 66 | } |
| 67 | fKernelUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility, |
| 68 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 69 | "Kernel", this->width()); |
| 70 | |
| 71 | GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 72 | SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); |
| 73 | |
| 74 | fsBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); |
| 75 | |
| 76 | int width = this->width(); |
| 77 | const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni); |
| 78 | const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 79 | |
| 80 | fsBuilder->codeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(), fRadius, imgInc); |
| 81 | |
| 82 | // Manually unroll loop because some drivers don't; yields 20-30% speedup. |
| 83 | for (int i = 0; i < width; i++) { |
| 84 | SkString index; |
| 85 | SkString kernelIndex; |
| 86 | index.appendS32(i); |
| 87 | kernel.appendArrayAccess(index.c_str(), &kernelIndex); |
| 88 | |
| 89 | if (this->useBounds()) { |
| 90 | // We used to compute a bool indicating whether we're in bounds or not, cast it to a |
| 91 | // float, and then mul weight*texture_sample by the float. However, the Adreno 430 seems |
| 92 | // to have a bug that caused corruption. |
| 93 | const char* bounds = builder->getUniformCStr(fBoundsUni); |
| 94 | const char* component = this->direction() == Gr1DKernelEffect::kY_Direction ? "y" : "x"; |
| 95 | fsBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y) {", |
| 96 | component, bounds, component, bounds); |
| 97 | } |
| 98 | fsBuilder->codeAppendf("\t\t%s += ", outputColor); |
| 99 | fsBuilder->appendTextureLookup(samplers[0], "coord"); |
| 100 | fsBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str()); |
| 101 | if (this->useBounds()) { |
| 102 | fsBuilder->codeAppend("}"); |
| 103 | } |
| 104 | fsBuilder->codeAppendf("\t\tcoord += %s;\n", imgInc); |
| 105 | } |
| 106 | |
| 107 | SkString modulate; |
| 108 | GrGLSLMulVarBy4f(&modulate, outputColor, inputColor); |
| 109 | fsBuilder->codeAppend(modulate.c_str()); |
| 110 | } |
| 111 | |
| 112 | void GrGLConvolutionEffect::setData(const GrGLProgramDataManager& pdman, |
| 113 | const GrProcessor& processor) { |
| 114 | const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>(); |
| 115 | GrTexture& texture = *conv.texture(0); |
| 116 | // the code we generated was for a specific kernel radius |
| 117 | SkASSERT(conv.radius() == fRadius); |
| 118 | float imageIncrement[2] = { 0 }; |
| 119 | float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
| 120 | switch (conv.direction()) { |
| 121 | case Gr1DKernelEffect::kX_Direction: |
| 122 | imageIncrement[0] = 1.0f / texture.width(); |
| 123 | break; |
| 124 | case Gr1DKernelEffect::kY_Direction: |
| 125 | imageIncrement[1] = ySign / texture.height(); |
| 126 | break; |
| 127 | default: |
| 128 | SkFAIL("Unknown filter direction."); |
| 129 | } |
| 130 | pdman.set2fv(fImageIncrementUni, 1, imageIncrement); |
| 131 | if (conv.useBounds()) { |
| 132 | const float* bounds = conv.bounds(); |
| 133 | if (Gr1DKernelEffect::kY_Direction == conv.direction() && |
| 134 | texture.origin() != kTopLeft_GrSurfaceOrigin) { |
| 135 | pdman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]); |
| 136 | } else { |
| 137 | pdman.set2f(fBoundsUni, bounds[0], bounds[1]); |
| 138 | } |
| 139 | } |
| 140 | pdman.set1fv(fKernelUni, this->width(), conv.kernel()); |
| 141 | } |
| 142 | |
| 143 | void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 144 | GrProcessorKeyBuilder* b) { |
| 145 | const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 146 | uint32_t key = conv.radius(); |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 147 | key <<= 2; |
| 148 | if (conv.useBounds()) { |
| 149 | key |= 0x2; |
| 150 | key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0; |
| 151 | } |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 152 | b->add32(key); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 153 | } |
| 154 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 155 | /////////////////////////////////////////////////////////////////////////////// |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 156 | |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 157 | GrConvolutionEffect::GrConvolutionEffect(GrProcessorDataManager* procDataManager, |
| 158 | GrTexture* texture, |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 159 | Direction direction, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 160 | int radius, |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 161 | const float* kernel, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 162 | bool useBounds, |
| 163 | float bounds[2]) |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame^] | 164 | : INHERITED(procDataManager, texture, direction, radius), fUseBounds(useBounds) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 165 | this->initClassID<GrConvolutionEffect>(); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 166 | SkASSERT(radius <= kMaxKernelRadius); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 167 | SkASSERT(kernel); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 168 | int width = this->width(); |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 169 | for (int i = 0; i < width; i++) { |
| 170 | fKernel[i] = kernel[i]; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 171 | } |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 172 | memcpy(fBounds, bounds, sizeof(fBounds)); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 173 | } |
| 174 | |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 175 | GrConvolutionEffect::GrConvolutionEffect(GrProcessorDataManager* procDataManager, |
| 176 | GrTexture* texture, |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 177 | Direction direction, |
| 178 | int radius, |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 179 | float gaussianSigma, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 180 | bool useBounds, |
| 181 | float bounds[2]) |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame^] | 182 | : INHERITED(procDataManager, texture, direction, radius), fUseBounds(useBounds) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 183 | this->initClassID<GrConvolutionEffect>(); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 184 | SkASSERT(radius <= kMaxKernelRadius); |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 185 | int width = this->width(); |
| 186 | |
| 187 | float sum = 0.0f; |
| 188 | float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma); |
| 189 | for (int i = 0; i < width; ++i) { |
| 190 | float x = static_cast<float>(i - this->radius()); |
| 191 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
| 192 | // is dropped here, since we renormalize the kernel below. |
| 193 | fKernel[i] = sk_float_exp(- x * x * denom); |
| 194 | sum += fKernel[i]; |
| 195 | } |
| 196 | // Normalize the kernel |
| 197 | float scale = 1.0f / sum; |
| 198 | for (int i = 0; i < width; ++i) { |
| 199 | fKernel[i] *= scale; |
| 200 | } |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 201 | memcpy(fBounds, bounds, sizeof(fBounds)); |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 202 | } |
| 203 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 204 | GrConvolutionEffect::~GrConvolutionEffect() { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 205 | } |
| 206 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 207 | void GrConvolutionEffect::getGLProcessorKey(const GrGLSLCaps& caps, |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 208 | GrProcessorKeyBuilder* b) const { |
| 209 | GrGLConvolutionEffect::GenKey(*this, caps, b); |
| 210 | } |
| 211 | |
| 212 | GrGLFragmentProcessor* GrConvolutionEffect::createGLInstance() const { |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame^] | 213 | return SkNEW_ARGS(GrGLConvolutionEffect, (*this)); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 214 | } |
| 215 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 216 | bool GrConvolutionEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 217 | const GrConvolutionEffect& s = sBase.cast<GrConvolutionEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 218 | return (this->radius() == s.radius() && |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 219 | this->direction() == s.direction() && |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 220 | this->useBounds() == s.useBounds() && |
| 221 | 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) && |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 222 | 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float))); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 223 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 224 | |
| 225 | /////////////////////////////////////////////////////////////////////////////// |
| 226 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 227 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvolutionEffect); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 228 | |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 229 | GrFragmentProcessor* GrConvolutionEffect::TestCreate(GrProcessorTestData* d) { |
| 230 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 231 | GrProcessorUnitTest::kAlphaTextureIdx; |
| 232 | Direction dir = d->fRandom->nextBool() ? kX_Direction : kY_Direction; |
| 233 | int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius); |
bungeman@google.com | 4348663 | 2013-08-20 15:20:34 +0000 | [diff] [blame] | 234 | float kernel[kMaxKernelWidth]; |
| 235 | for (size_t i = 0; i < SK_ARRAY_COUNT(kernel); ++i) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 236 | kernel[i] = d->fRandom->nextSScalar1(); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 237 | } |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 238 | float bounds[2]; |
bungeman@google.com | 4348663 | 2013-08-20 15:20:34 +0000 | [diff] [blame] | 239 | for (size_t i = 0; i < SK_ARRAY_COUNT(bounds); ++i) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 240 | bounds[i] = d->fRandom->nextF(); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 241 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 242 | |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 243 | bool useBounds = d->fRandom->nextBool(); |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 244 | return GrConvolutionEffect::Create(d->fProcDataManager, |
| 245 | d->fTextures[texIdx], |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 246 | dir, |
| 247 | radius, |
| 248 | kernel, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 249 | useBounds, |
| 250 | bounds); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 251 | } |