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" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 9 | #include "gl/GrGLEffect.h" |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 10 | #include "gl/GrGLEffectMatrix.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 11 | #include "gl/GrGLSL.h" |
| 12 | #include "gl/GrGLTexture.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 13 | #include "GrTBackendEffectFactory.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 14 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 15 | // For brevity |
| 16 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 17 | static const UniformHandle kInvalidUniformHandle = GrGLUniformManager::kInvalidUniformHandle; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 18 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 19 | class GrGLConvolutionEffect : public GrGLEffect { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 20 | public: |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 21 | GrGLConvolutionEffect(const GrBackendEffectFactory&, const GrEffectRef&); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 22 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 23 | virtual void emitCode(GrGLShaderBuilder*, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 24 | const GrEffectStage&, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 25 | EffectKey, |
| 26 | const char* vertexCoords, |
| 27 | const char* outputColor, |
| 28 | const char* inputColor, |
| 29 | const TextureSamplerArray&) SK_OVERRIDE; |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 30 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 31 | virtual void setData(const GrGLUniformManager& uman, const GrEffectStage&) SK_OVERRIDE; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 32 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 33 | static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 34 | |
| 35 | private: |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 36 | int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); } |
| 37 | |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 38 | int fRadius; |
| 39 | UniformHandle fKernelUni; |
| 40 | UniformHandle fImageIncrementUni; |
| 41 | GrGLEffectMatrix fEffectMatrix; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 42 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 43 | typedef GrGLEffect INHERITED; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 46 | GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 47 | const GrEffectRef& effect) |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 48 | : INHERITED(factory) |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 49 | , fKernelUni(kInvalidUniformHandle) |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 50 | , fImageIncrementUni(kInvalidUniformHandle) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 51 | const GrConvolutionEffect& c = CastEffect<GrConvolutionEffect>(effect); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 52 | fRadius = c.radius(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 53 | } |
| 54 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 55 | void GrGLConvolutionEffect::emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 56 | const GrEffectStage&, |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 57 | EffectKey key, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 58 | const char* vertexCoords, |
| 59 | const char* outputColor, |
| 60 | const char* inputColor, |
| 61 | const TextureSamplerArray& samplers) { |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 62 | const char* coords; |
| 63 | fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 64 | fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 65 | kVec2f_GrSLType, "ImageIncrement"); |
| 66 | fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType, |
| 67 | kFloat_GrSLType, "Kernel", this->width()); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 68 | SkString* code = &builder->fFSCode; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 69 | |
tomhudson@google.com | db3a154 | 2012-06-13 18:12:57 +0000 | [diff] [blame] | 70 | code->appendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 71 | |
bsalomon@google.com | fc0d23a | 2012-06-15 21:50:15 +0000 | [diff] [blame] | 72 | int width = this ->width(); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 73 | const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni); |
| 74 | const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 75 | |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 76 | code->appendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords, fRadius, imgInc); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 77 | |
tomhudson@google.com | f8a2289 | 2012-06-11 12:42:24 +0000 | [diff] [blame] | 78 | // Manually unroll loop because some drivers don't; yields 20-30% speedup. |
bsalomon@google.com | fc0d23a | 2012-06-15 21:50:15 +0000 | [diff] [blame] | 79 | for (int i = 0; i < width; i++) { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 80 | SkString index; |
| 81 | SkString kernelIndex; |
tomhudson@google.com | f8a2289 | 2012-06-11 12:42:24 +0000 | [diff] [blame] | 82 | index.appendS32(i); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 83 | kernel.appendArrayAccess(index.c_str(), &kernelIndex); |
tomhudson@google.com | db3a154 | 2012-06-13 18:12:57 +0000 | [diff] [blame] | 84 | code->appendf("\t\t%s += ", outputColor); |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 85 | builder->appendTextureLookup(&builder->fFSCode, samplers[0], "coord"); |
tomhudson@google.com | f8a2289 | 2012-06-11 12:42:24 +0000 | [diff] [blame] | 86 | code->appendf(" * %s;\n", kernelIndex.c_str()); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 87 | code->appendf("\t\tcoord += %s;\n", imgInc); |
tomhudson@google.com | f8a2289 | 2012-06-11 12:42:24 +0000 | [diff] [blame] | 88 | } |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 89 | GrGLSLMulVarBy4f(&builder->fFSCode, 2, outputColor, inputColor); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 90 | } |
| 91 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 92 | void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 93 | const GrConvolutionEffect& conv = GetEffectFromStage<GrConvolutionEffect>(stage); |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 94 | GrTexture& texture = *conv.texture(0); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 95 | // the code we generated was for a specific kernel radius |
| 96 | GrAssert(conv.radius() == fRadius); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 97 | float imageIncrement[2] = { 0 }; |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 98 | switch (conv.direction()) { |
| 99 | case Gr1DKernelEffect::kX_Direction: |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 100 | imageIncrement[0] = 1.0f / texture.width(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 101 | break; |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 102 | case Gr1DKernelEffect::kY_Direction: |
| 103 | imageIncrement[1] = 1.0f / texture.height(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 104 | break; |
| 105 | default: |
| 106 | GrCrash("Unknown filter direction."); |
| 107 | } |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 108 | uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); |
| 109 | uman.set1fv(fKernelUni, 0, this->width(), conv.kernel()); |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 110 | fEffectMatrix.setData(uman, conv.getMatrix(), stage.getCoordChangeMatrix(), conv.texture(0)); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 111 | } |
| 112 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 113 | GrGLEffect::EffectKey GrGLConvolutionEffect::GenKey(const GrEffectStage& s, const GrGLCaps&) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 114 | const GrConvolutionEffect& conv = GetEffectFromStage<GrConvolutionEffect>(s); |
| 115 | EffectKey key = conv.radius(); |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 116 | key <<= GrGLEffectMatrix::kKeyBits; |
| 117 | EffectKey matrixKey = GrGLEffectMatrix::GenKey(conv.getMatrix(), |
| 118 | s.getCoordChangeMatrix(), |
| 119 | conv.texture(0)); |
| 120 | return key | matrixKey; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 121 | } |
| 122 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 123 | /////////////////////////////////////////////////////////////////////////////// |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 124 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 125 | GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture, |
| 126 | Direction direction, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 127 | int radius, |
| 128 | const float* kernel) |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 129 | : Gr1DKernelEffect(texture, direction, radius) { |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 130 | GrAssert(radius <= kMaxKernelRadius); |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 131 | GrAssert(NULL != kernel); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 132 | int width = this->width(); |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 133 | for (int i = 0; i < width; i++) { |
| 134 | fKernel[i] = kernel[i]; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 138 | GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture, |
| 139 | Direction direction, |
| 140 | int radius, |
| 141 | float gaussianSigma) |
| 142 | : Gr1DKernelEffect(texture, direction, radius) { |
| 143 | GrAssert(radius <= kMaxKernelRadius); |
| 144 | int width = this->width(); |
| 145 | |
| 146 | float sum = 0.0f; |
| 147 | float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma); |
| 148 | for (int i = 0; i < width; ++i) { |
| 149 | float x = static_cast<float>(i - this->radius()); |
| 150 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
| 151 | // is dropped here, since we renormalize the kernel below. |
| 152 | fKernel[i] = sk_float_exp(- x * x * denom); |
| 153 | sum += fKernel[i]; |
| 154 | } |
| 155 | // Normalize the kernel |
| 156 | float scale = 1.0f / sum; |
| 157 | for (int i = 0; i < width; ++i) { |
| 158 | fKernel[i] *= scale; |
| 159 | } |
| 160 | } |
| 161 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 162 | GrConvolutionEffect::~GrConvolutionEffect() { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 163 | } |
| 164 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 165 | const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const { |
| 166 | return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 167 | } |
| 168 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 169 | bool GrConvolutionEffect::onIsEqual(const GrEffect& sBase) const { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 170 | const GrConvolutionEffect& s = CastEffect<GrConvolutionEffect>(sBase); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 171 | return (this->texture(0) == s.texture(0) && |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 172 | this->radius() == s.radius() && |
| 173 | this->direction() == s.direction() && |
| 174 | 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float))); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 175 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 176 | |
| 177 | /////////////////////////////////////////////////////////////////////////////// |
| 178 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 179 | GR_DEFINE_EFFECT_TEST(GrConvolutionEffect); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 180 | |
bsalomon@google.com | 73a9694 | 2013-02-13 16:31:19 +0000 | [diff] [blame] | 181 | GrEffectRef* GrConvolutionEffect::TestCreate(SkMWCRandom* random, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 182 | GrContext* context, |
| 183 | GrTexture* textures[]) { |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 184 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 185 | GrEffectUnitTest::kAlphaTextureIdx; |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 186 | Direction dir = random->nextBool() ? kX_Direction : kY_Direction; |
| 187 | int radius = random->nextRangeU(1, kMaxKernelRadius); |
| 188 | float kernel[kMaxKernelRadius]; |
| 189 | for (int i = 0; i < kMaxKernelRadius; ++i) { |
| 190 | kernel[i] = random->nextSScalar1(); |
| 191 | } |
| 192 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 193 | return GrConvolutionEffect::Create(textures[texIdx], dir, radius,kernel); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 194 | } |