| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | #include "GrMatrixConvolutionEffect.h" |
| 8 | #include "gl/GrGLShaderBuilder.h" |
| 9 | #include "gl/GrGLEffect.h" |
| 10 | #include "gl/GrGLSL.h" |
| 11 | #include "gl/GrGLTexture.h" |
| 12 | #include "GrTBackendEffectFactory.h" |
| 13 | |
| 14 | class GrGLMatrixConvolutionEffect : public GrGLEffect { |
| 15 | public: |
| 16 | GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, |
| 17 | const GrDrawEffect& effect); |
| 18 | virtual void emitCode(GrGLShaderBuilder*, |
| 19 | const GrDrawEffect&, |
| 20 | const GrEffectKey&, |
| 21 | const char* outputColor, |
| 22 | const char* inputColor, |
| 23 | const TransformedCoordsArray&, |
| 24 | const TextureSamplerArray&) SK_OVERRIDE; |
| 25 | |
| 26 | static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder*); |
| 27 | |
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 28 | virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE; |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 29 | |
| 30 | private: |
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 31 | typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 32 | SkISize fKernelSize; |
| 33 | bool fConvolveAlpha; |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 34 | |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 35 | UniformHandle fBoundsUni; |
| 36 | UniformHandle fKernelUni; |
| 37 | UniformHandle fImageIncrementUni; |
| 38 | UniformHandle fKernelOffsetUni; |
| 39 | UniformHandle fGainUni; |
| 40 | UniformHandle fBiasUni; |
| 41 | GrTextureDomain::GLDomain fDomain; |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 42 | |
| 43 | typedef GrGLEffect INHERITED; |
| 44 | }; |
| 45 | |
| 46 | GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, |
| 47 | const GrDrawEffect& drawEffect) |
| 48 | : INHERITED(factory) { |
| 49 | const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvolutionEffect>(); |
| 50 | fKernelSize = m.kernelSize(); |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 51 | fConvolveAlpha = m.convolveAlpha(); |
| 52 | } |
| 53 | |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 54 | void GrGLMatrixConvolutionEffect::emitCode(GrGLShaderBuilder* builder, |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 55 | const GrDrawEffect& drawEffect, |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 56 | const GrEffectKey& key, |
| 57 | const char* outputColor, |
| 58 | const char* inputColor, |
| 59 | const TransformedCoordsArray& coords, |
| 60 | const TextureSamplerArray& samplers) { |
| 61 | sk_ignore_unused_variable(inputColor); |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 62 | const GrTextureDomain& domain = drawEffect.castEffect<GrMatrixConvolutionEffect>().domain(); |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 63 | SkString coords2D = builder->ensureFSCoords2D(coords, 0); |
| 64 | fBoundsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 65 | kVec4f_GrSLType, "Bounds"); |
| 66 | fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 67 | kVec2f_GrSLType, "ImageIncrement"); |
| 68 | fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibility, |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 69 | kFloat_GrSLType, |
| 70 | "Kernel", |
| 71 | fKernelSize.width() * fKernelSize.height()); |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 72 | fKernelOffsetUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 73 | kVec2f_GrSLType, "KernelOffset"); |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 74 | fGainUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 75 | kFloat_GrSLType, "Gain"); |
| 76 | fBiasUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 77 | kFloat_GrSLType, "Bias"); |
| 78 | |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 79 | const char* kernelOffset = builder->getUniformCStr(fKernelOffsetUni); |
| 80 | const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 81 | const char* kernel = builder->getUniformCStr(fKernelUni); |
| 82 | const char* gain = builder->getUniformCStr(fGainUni); |
| 83 | const char* bias = builder->getUniformCStr(fBiasUni); |
| 84 | int kWidth = fKernelSize.width(); |
| 85 | int kHeight = fKernelSize.height(); |
| 86 | |
| joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame^] | 87 | builder->fsCodeAppend("vec4 sum = vec4(0, 0, 0, 0);"); |
| 88 | builder->fsCodeAppendf("vec2 coord = %s - %s * %s;", coords2D.c_str(), kernelOffset, |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 89 | imgInc); |
| joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame^] | 90 | builder->fsCodeAppend("vec4 c;"); |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 91 | |
| 92 | for (int y = 0; y < kHeight; y++) { |
| 93 | for (int x = 0; x < kWidth; x++) { |
| 94 | GrGLShaderBuilder::FSBlock block(builder); |
| joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame^] | 95 | builder->fsCodeAppendf("float k = %s[%d * %d + %d];", kernel, y, kWidth, x); |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 96 | SkString coord; |
| 97 | coord.printf("coord + vec2(%d, %d) * %s", x, y, imgInc); |
| 98 | fDomain.sampleTexture(builder, domain, "c", coord, samplers[0]); |
| 99 | if (!fConvolveAlpha) { |
| joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame^] | 100 | builder->fsCodeAppend("c.rgb /= c.a;"); |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 101 | } |
| joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame^] | 102 | builder->fsCodeAppend("sum += c * k;"); |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 103 | } |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 104 | } |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 105 | if (fConvolveAlpha) { |
| joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame^] | 106 | builder->fsCodeAppendf("%s = sum * %s + %s;", outputColor, gain, bias); |
| 107 | builder->fsCodeAppendf("%s.rgb = clamp(%s.rgb, 0.0, %s.a);", |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 108 | outputColor, outputColor, outputColor); |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 109 | } else { |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 110 | fDomain.sampleTexture(builder, domain, "c", coords2D, samplers[0]); |
| joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame^] | 111 | builder->fsCodeAppendf("%s.a = c.a;", outputColor); |
| 112 | builder->fsCodeAppendf("%s.rgb = sum.rgb * %s + %s;", outputColor, gain, bias); |
| 113 | builder->fsCodeAppendf("%s.rgb *= %s.a;", outputColor, outputColor); |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 114 | } |
| joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame^] | 115 | |
| 116 | SkString modulate; |
| 117 | GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
| 118 | builder->fsCodeAppend(modulate.c_str()); |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 121 | void GrGLMatrixConvolutionEffect::GenKey(const GrDrawEffect& drawEffect, |
| 122 | const GrGLCaps&, GrEffectKeyBuilder* b) { |
| 123 | const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvolutionEffect>(); |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 124 | SkASSERT(m.kernelSize().width() <= 0x7FFF && m.kernelSize().height() <= 0xFFFF); |
| 125 | uint32_t key = m.kernelSize().width() << 16 | m.kernelSize().height(); |
| 126 | key |= m.convolveAlpha() ? 1 << 31 : 0; |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 127 | b->add32(key); |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 128 | b->add32(GrTextureDomain::GLDomain::DomainKey(m.domain())); |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 131 | void GrGLMatrixConvolutionEffect::setData(const GrGLProgramDataManager& pdman, |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 132 | const GrDrawEffect& drawEffect) { |
| 133 | const GrMatrixConvolutionEffect& conv = drawEffect.castEffect<GrMatrixConvolutionEffect>(); |
| 134 | GrTexture& texture = *conv.texture(0); |
| 135 | // the code we generated was for a specific kernel size |
| 136 | SkASSERT(conv.kernelSize() == fKernelSize); |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 137 | float imageIncrement[2]; |
| 138 | float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
| 139 | imageIncrement[0] = 1.0f / texture.width(); |
| 140 | imageIncrement[1] = ySign / texture.height(); |
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 141 | pdman.set2fv(fImageIncrementUni, 1, imageIncrement); |
| 142 | pdman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset()); |
| 143 | pdman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.kernel()); |
| 144 | pdman.set1f(fGainUni, conv.gain()); |
| 145 | pdman.set1f(fBiasUni, conv.bias()); |
| 146 | fDomain.setData(pdman, conv.domain(), texture.origin()); |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture, |
| 150 | const SkIRect& bounds, |
| 151 | const SkISize& kernelSize, |
| 152 | const SkScalar* kernel, |
| 153 | SkScalar gain, |
| 154 | SkScalar bias, |
| 155 | const SkIPoint& kernelOffset, |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 156 | GrTextureDomain::Mode tileMode, |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 157 | bool convolveAlpha) |
| 158 | : INHERITED(texture, MakeDivByTextureWHMatrix(texture)), |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 159 | fKernelSize(kernelSize), |
| 160 | fGain(SkScalarToFloat(gain)), |
| 161 | fBias(SkScalarToFloat(bias) / 255.0f), |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 162 | fConvolveAlpha(convolveAlpha), |
| 163 | fDomain(GrTextureDomain::MakeTexelDomain(texture, bounds), tileMode) { |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 164 | for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) { |
| 165 | fKernel[i] = SkScalarToFloat(kernel[i]); |
| 166 | } |
| 167 | fKernelOffset[0] = static_cast<float>(kernelOffset.x()); |
| 168 | fKernelOffset[1] = static_cast<float>(kernelOffset.y()); |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() { |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | const GrBackendEffectFactory& GrMatrixConvolutionEffect::getFactory() const { |
| 175 | return GrTBackendEffectFactory<GrMatrixConvolutionEffect>::getInstance(); |
| 176 | } |
| 177 | |
| 178 | bool GrMatrixConvolutionEffect::onIsEqual(const GrEffect& sBase) const { |
| 179 | const GrMatrixConvolutionEffect& s = CastEffect<GrMatrixConvolutionEffect>(sBase); |
| 180 | return this->texture(0) == s.texture(0) && |
| 181 | fKernelSize == s.kernelSize() && |
| 182 | !memcmp(fKernel, s.kernel(), |
| 183 | fKernelSize.width() * fKernelSize.height() * sizeof(float)) && |
| 184 | fGain == s.gain() && |
| 185 | fBias == s.bias() && |
| 186 | fKernelOffset == s.kernelOffset() && |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 187 | fConvolveAlpha == s.convolveAlpha() && |
| 188 | fDomain == s.domain(); |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame^] | 191 | // Static function to create a 2D convolution |
| 192 | GrEffect* GrMatrixConvolutionEffect::CreateGaussian(GrTexture* texture, |
| 193 | const SkIRect& bounds, |
| 194 | const SkISize& kernelSize, |
| 195 | SkScalar gain, |
| 196 | SkScalar bias, |
| 197 | const SkIPoint& kernelOffset, |
| 198 | GrTextureDomain::Mode tileMode, |
| 199 | bool convolveAlpha, |
| 200 | SkScalar sigmaX, |
| 201 | SkScalar sigmaY) { |
| 202 | float kernel[MAX_KERNEL_SIZE]; |
| 203 | int width = kernelSize.width(); |
| 204 | int height = kernelSize.height(); |
| 205 | SkASSERT(width * height <= MAX_KERNEL_SIZE); |
| 206 | float sum = 0.0f; |
| 207 | float sigmaXDenom = 1.0f / (2.0f * SkScalarToFloat(SkScalarSquare(sigmaX))); |
| 208 | float sigmaYDenom = 1.0f / (2.0f * SkScalarToFloat(SkScalarSquare(sigmaY))); |
| 209 | int xRadius = width / 2; |
| 210 | int yRadius = height / 2; |
| 211 | for (int x = 0; x < width; x++) { |
| 212 | float xTerm = static_cast<float>(x - xRadius); |
| 213 | xTerm = xTerm * xTerm * sigmaXDenom; |
| 214 | for (int y = 0; y < height; y++) { |
| 215 | float yTerm = static_cast<float>(y - yRadius); |
| 216 | float xyTerm = sk_float_exp(-(xTerm + yTerm * yTerm * sigmaYDenom)); |
| 217 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
| 218 | // is dropped here, since we renormalize the kernel below. |
| 219 | kernel[y * width + x] = xyTerm; |
| 220 | sum += xyTerm; |
| 221 | } |
| 222 | } |
| 223 | // Normalize the kernel |
| 224 | float scale = 1.0f / sum; |
| 225 | for (int i = 0; i < width * height; ++i) { |
| 226 | kernel[i] *= scale; |
| 227 | } |
| 228 | return SkNEW_ARGS(GrMatrixConvolutionEffect, (texture, |
| 229 | bounds, |
| 230 | kernelSize, |
| 231 | kernel, |
| 232 | gain, |
| 233 | bias, |
| 234 | kernelOffset, |
| 235 | tileMode, |
| 236 | convolveAlpha)); |
| 237 | } |
| 238 | |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 239 | GR_DEFINE_EFFECT_TEST(GrMatrixConvolutionEffect); |
| 240 | |
| 241 | GrEffect* GrMatrixConvolutionEffect::TestCreate(SkRandom* random, |
| 242 | GrContext* context, |
| 243 | const GrDrawTargetCaps&, |
| 244 | GrTexture* textures[]) { |
| 245 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 246 | GrEffectUnitTest::kAlphaTextureIdx; |
| 247 | int width = random->nextRangeU(1, MAX_KERNEL_SIZE); |
| 248 | int height = random->nextRangeU(1, MAX_KERNEL_SIZE / width); |
| 249 | SkISize kernelSize = SkISize::Make(width, height); |
| 250 | SkAutoTDeleteArray<SkScalar> kernel(new SkScalar[width * height]); |
| 251 | for (int i = 0; i < width * height; i++) { |
| 252 | kernel.get()[i] = random->nextSScalar1(); |
| 253 | } |
| 254 | SkScalar gain = random->nextSScalar1(); |
| 255 | SkScalar bias = random->nextSScalar1(); |
| 256 | SkIPoint kernelOffset = SkIPoint::Make(random->nextRangeU(0, kernelSize.width()), |
| 257 | random->nextRangeU(0, kernelSize.height())); |
| 258 | SkIRect bounds = SkIRect::MakeXYWH(random->nextRangeU(0, textures[texIdx]->width()), |
| 259 | random->nextRangeU(0, textures[texIdx]->height()), |
| 260 | random->nextRangeU(0, textures[texIdx]->width()), |
| 261 | random->nextRangeU(0, textures[texIdx]->height())); |
| joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 262 | GrTextureDomain::Mode tileMode = static_cast<GrTextureDomain::Mode>(random->nextRangeU(0, 2)); |
| joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 263 | bool convolveAlpha = random->nextBool(); |
| 264 | return GrMatrixConvolutionEffect::Create(textures[texIdx], |
| 265 | bounds, |
| 266 | kernelSize, |
| 267 | kernel.get(), |
| 268 | gain, |
| 269 | bias, |
| 270 | kernelOffset, |
| 271 | tileMode, |
| 272 | convolveAlpha); |
| 273 | } |