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