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