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