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