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