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 | |
| 14 | ///////////////////////////////////////////////////////////////////// |
| 15 | |
| 16 | class GrGLConvolutionEffect : public GrGLProgramStage { |
| 17 | |
| 18 | public: |
| 19 | |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 20 | GrGLConvolutionEffect(const GrProgramStageFactory& factory, |
| 21 | const GrCustomStage* stage); |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 22 | virtual void setupVariables(GrGLShaderBuilder* state, |
| 23 | int stage) SK_OVERRIDE; |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 24 | virtual void emitVS(GrGLShaderBuilder* state, |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 25 | const char* vertexCoords) SK_OVERRIDE; |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 26 | virtual void emitFS(GrGLShaderBuilder* state, |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 27 | const char* outputColor, |
| 28 | const char* inputColor, |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 29 | const char* samplerName) SK_OVERRIDE; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 30 | virtual void initUniforms(const GrGLInterface*, int programID) SK_OVERRIDE; |
| 31 | |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 32 | virtual void setData(const GrGLInterface*, |
| 33 | const GrGLTexture&, |
| 34 | GrCustomStage*, |
| 35 | int stageNum) SK_OVERRIDE; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 36 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 37 | static inline StageKey GenKey(const GrCustomStage* s); |
| 38 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 39 | protected: |
| 40 | |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 41 | unsigned int fKernelWidth; |
| 42 | const GrGLShaderVar* fKernelVar; |
| 43 | const GrGLShaderVar* fImageIncrementVar; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 44 | |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 45 | GrGLint fKernelLocation; |
| 46 | GrGLint fImageIncrementLocation; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 47 | |
| 48 | private: |
| 49 | |
| 50 | typedef GrGLProgramStage INHERITED; |
| 51 | }; |
| 52 | |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 53 | GrGLConvolutionEffect::GrGLConvolutionEffect( |
| 54 | const GrProgramStageFactory& factory, |
| 55 | const GrCustomStage* data) |
| 56 | : GrGLProgramStage(factory) |
| 57 | , fKernelVar(NULL) |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 58 | , fImageIncrementVar(NULL) |
| 59 | , fKernelLocation(0) |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 60 | , fImageIncrementLocation(0) { |
| 61 | fKernelWidth = static_cast<const GrConvolutionEffect*>(data)->width(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 62 | } |
| 63 | |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 64 | void GrGLConvolutionEffect::setupVariables(GrGLShaderBuilder* state, |
| 65 | int stage) { |
| 66 | fImageIncrementVar = &state->addUniform( |
| 67 | GrGLShaderBuilder::kBoth_VariableLifetime, |
| 68 | kVec2f_GrSLType, "uImageIncrement", stage); |
| 69 | fKernelVar = &state->addUniform( |
| 70 | GrGLShaderBuilder::kFragment_VariableLifetime, |
| 71 | kFloat_GrSLType, "uKernel", stage, fKernelWidth); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 72 | |
| 73 | fImageIncrementLocation = kUseUniform; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 74 | fKernelLocation = kUseUniform; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 75 | } |
| 76 | |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 77 | void GrGLConvolutionEffect::emitVS(GrGLShaderBuilder* state, |
tomhudson@google.com | f1d8806 | 2012-05-10 12:43:21 +0000 | [diff] [blame] | 78 | const char* vertexCoords) { |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 79 | GrStringBuilder* code = &state->fVSCode; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 80 | float scale = (fKernelWidth - 1) * 0.5f; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 81 | code->appendf("\t\t%s -= vec2(%g, %g) * %s;\n", |
| 82 | vertexCoords, scale, scale, |
| 83 | fImageIncrementVar->getName().c_str()); |
| 84 | |
| 85 | } |
| 86 | |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 87 | void GrGLConvolutionEffect::emitFS(GrGLShaderBuilder* state, |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 88 | const char* outputColor, |
| 89 | const char* inputColor, |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 90 | const char* samplerName) { |
| 91 | GrStringBuilder* code = &state->fFSCode; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 92 | const char* texFunc = "texture2D"; |
| 93 | bool complexCoord = false; |
| 94 | |
| 95 | GrStringBuilder modulate; |
| 96 | if (NULL != inputColor) { |
| 97 | modulate.printf(" * %s", inputColor); |
| 98 | } |
| 99 | |
| 100 | // Creates the string "kernel[i]" with workarounds for |
| 101 | // possible driver bugs |
| 102 | GrStringBuilder kernelIndex; |
| 103 | fKernelVar->appendArrayAccess("i", &kernelIndex); |
| 104 | |
| 105 | code->appendf("\t\tvec4 sum = vec4(0, 0, 0, 0);\n"); |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 106 | code->appendf("\t\tvec2 coord = %s;\n", state->fSampleCoords.c_str()); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 107 | code->appendf("\t\tfor (int i = 0; i < %d; i++) {\n", |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 108 | fKernelWidth); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 109 | |
| 110 | code->appendf("\t\t\tsum += "); |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 111 | state->emitTextureLookup(samplerName, "coord"); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 112 | code->appendf(" * %s;\n", kernelIndex.c_str()); |
| 113 | |
| 114 | code->appendf("\t\t\tcoord += %s;\n", |
| 115 | fImageIncrementVar->getName().c_str()); |
| 116 | code->appendf("\t\t}\n"); |
| 117 | code->appendf("\t\t%s = sum%s;\n", outputColor, modulate.c_str()); |
| 118 | } |
| 119 | |
| 120 | void GrGLConvolutionEffect::initUniforms(const GrGLInterface* gl, |
tomhudson@google.com | f1d8806 | 2012-05-10 12:43:21 +0000 | [diff] [blame] | 121 | int programID) { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 122 | GR_GL_CALL_RET(gl, fKernelLocation, |
| 123 | GetUniformLocation(programID, fKernelVar->getName().c_str())); |
| 124 | GR_GL_CALL_RET(gl, fImageIncrementLocation, |
| 125 | GetUniformLocation(programID, |
| 126 | fImageIncrementVar->getName().c_str())); |
| 127 | } |
| 128 | |
| 129 | void GrGLConvolutionEffect::setData(const GrGLInterface* gl, |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 130 | const GrGLTexture& texture, |
| 131 | GrCustomStage* data, |
| 132 | int stageNum) { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 133 | const GrConvolutionEffect* conv = |
| 134 | static_cast<const GrConvolutionEffect*>(data); |
| 135 | // the code we generated was for a specific kernel width |
| 136 | GrAssert(conv->width() == fKernelWidth); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 137 | GR_GL_CALL(gl, Uniform1fv(fKernelLocation, |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 138 | fKernelWidth, |
| 139 | conv->kernel())); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 140 | float imageIncrement[2] = { 0 }; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 141 | switch (conv->direction()) { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 142 | case GrSamplerState::kX_FilterDirection: |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 143 | imageIncrement[0] = 1.0f / texture.width(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 144 | break; |
| 145 | case GrSamplerState::kY_FilterDirection: |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 146 | imageIncrement[1] = 1.0f / texture.width(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 147 | break; |
| 148 | default: |
| 149 | GrCrash("Unknown filter direction."); |
| 150 | } |
| 151 | GR_GL_CALL(gl, Uniform2fv(fImageIncrementLocation, 1, imageIncrement)); |
| 152 | } |
| 153 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 154 | GrGLProgramStage::StageKey GrGLConvolutionEffect::GenKey( |
| 155 | const GrCustomStage* s) { |
| 156 | return static_cast<const GrConvolutionEffect*>(s)->width(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | ///////////////////////////////////////////////////////////////////// |
| 160 | |
| 161 | GrConvolutionEffect::GrConvolutionEffect( |
| 162 | GrSamplerState::FilterDirection direction, |
| 163 | unsigned int kernelWidth, |
| 164 | const float* kernel) |
| 165 | : fDirection (direction) |
| 166 | , fKernelWidth (kernelWidth) { |
| 167 | GrAssert(kernelWidth <= MAX_KERNEL_WIDTH); |
| 168 | for (unsigned int i = 0; i < kernelWidth; i++) { |
| 169 | fKernel[i] = kernel[i]; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | GrConvolutionEffect::~GrConvolutionEffect() { |
| 174 | |
| 175 | } |
| 176 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 177 | const GrProgramStageFactory& GrConvolutionEffect::getFactory() const { |
| 178 | return GrTProgramStageFactory<GrConvolutionEffect>::getInstance(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | bool GrConvolutionEffect::isEqual(const GrCustomStage * sBase) const { |
| 182 | const GrConvolutionEffect* s = |
| 183 | static_cast<const GrConvolutionEffect*>(sBase); |
| 184 | |
| 185 | return (fKernelWidth == s->fKernelWidth && |
| 186 | fDirection == s->fDirection && |
| 187 | 0 == memcmp(fKernel, s->fKernel, fKernelWidth * sizeof(float))); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | |