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