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 | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 22 | virtual void setupVSUnis(VarArray* vsUnis, int stage) SK_OVERRIDE; |
| 23 | virtual void setupFSUnis(VarArray* fsUnis, 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 | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame^] | 41 | unsigned int fKernelWidth; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 42 | GrGLShaderVar* fKernelVar; |
| 43 | GrGLShaderVar* fImageIncrementVar; |
| 44 | |
| 45 | GrGLint fKernelLocation; |
| 46 | GrGLint fImageIncrementLocation; |
| 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 | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 64 | void GrGLConvolutionEffect::setupVSUnis(VarArray* vsUnis, |
tomhudson@google.com | f1d8806 | 2012-05-10 12:43:21 +0000 | [diff] [blame] | 65 | int stage) { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 66 | fImageIncrementVar = &vsUnis->push_back(); |
| 67 | fImageIncrementVar->setType(kVec2f_GrSLType); |
| 68 | fImageIncrementVar->setTypeModifier( |
| 69 | GrGLShaderVar::kUniform_TypeModifier); |
| 70 | (*fImageIncrementVar->accessName()) = "uImageIncrement"; |
| 71 | fImageIncrementVar->accessName()->appendS32(stage); |
| 72 | fImageIncrementVar->setEmitPrecision(true); |
| 73 | |
| 74 | fImageIncrementLocation = kUseUniform; |
| 75 | } |
| 76 | |
| 77 | void GrGLConvolutionEffect::setupFSUnis(VarArray* fsUnis, |
tomhudson@google.com | f1d8806 | 2012-05-10 12:43:21 +0000 | [diff] [blame] | 78 | int stage) { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 79 | fKernelVar = &fsUnis->push_back(); |
| 80 | fKernelVar->setType(kFloat_GrSLType); |
| 81 | fKernelVar->setTypeModifier( |
| 82 | GrGLShaderVar::kUniform_TypeModifier); |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 83 | fKernelVar->setArrayCount(fKernelWidth); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 84 | (*fKernelVar->accessName()) = "uKernel"; |
| 85 | fKernelVar->accessName()->appendS32(stage); |
| 86 | |
| 87 | fKernelLocation = kUseUniform; |
| 88 | |
| 89 | // Image increment is used in both vertex & fragment shaders. |
| 90 | fsUnis->push_back(*fImageIncrementVar).setEmitPrecision(false); |
| 91 | } |
| 92 | |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame^] | 93 | void GrGLConvolutionEffect::emitVS(GrGLShaderBuilder* state, |
tomhudson@google.com | f1d8806 | 2012-05-10 12:43:21 +0000 | [diff] [blame] | 94 | const char* vertexCoords) { |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame^] | 95 | GrStringBuilder* code = &state->fVSCode; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 96 | float scale = (fKernelWidth - 1) * 0.5f; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 97 | code->appendf("\t\t%s -= vec2(%g, %g) * %s;\n", |
| 98 | vertexCoords, scale, scale, |
| 99 | fImageIncrementVar->getName().c_str()); |
| 100 | |
| 101 | } |
| 102 | |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame^] | 103 | void GrGLConvolutionEffect::emitFS(GrGLShaderBuilder* state, |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 104 | const char* outputColor, |
| 105 | const char* inputColor, |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame^] | 106 | const char* samplerName) { |
| 107 | GrStringBuilder* code = &state->fFSCode; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 108 | const char* texFunc = "texture2D"; |
| 109 | bool complexCoord = false; |
| 110 | |
| 111 | GrStringBuilder modulate; |
| 112 | if (NULL != inputColor) { |
| 113 | modulate.printf(" * %s", inputColor); |
| 114 | } |
| 115 | |
| 116 | // Creates the string "kernel[i]" with workarounds for |
| 117 | // possible driver bugs |
| 118 | GrStringBuilder kernelIndex; |
| 119 | fKernelVar->appendArrayAccess("i", &kernelIndex); |
| 120 | |
| 121 | code->appendf("\t\tvec4 sum = vec4(0, 0, 0, 0);\n"); |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame^] | 122 | code->appendf("\t\tvec2 coord = %s;\n", state->fSampleCoords.c_str()); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 123 | code->appendf("\t\tfor (int i = 0; i < %d; i++) {\n", |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 124 | fKernelWidth); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 125 | |
| 126 | code->appendf("\t\t\tsum += "); |
| 127 | this->emitTextureLookup(code, samplerName, "coord"); |
| 128 | code->appendf(" * %s;\n", kernelIndex.c_str()); |
| 129 | |
| 130 | code->appendf("\t\t\tcoord += %s;\n", |
| 131 | fImageIncrementVar->getName().c_str()); |
| 132 | code->appendf("\t\t}\n"); |
| 133 | code->appendf("\t\t%s = sum%s;\n", outputColor, modulate.c_str()); |
| 134 | } |
| 135 | |
| 136 | void GrGLConvolutionEffect::initUniforms(const GrGLInterface* gl, |
tomhudson@google.com | f1d8806 | 2012-05-10 12:43:21 +0000 | [diff] [blame] | 137 | int programID) { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 138 | GR_GL_CALL_RET(gl, fKernelLocation, |
| 139 | GetUniformLocation(programID, fKernelVar->getName().c_str())); |
| 140 | GR_GL_CALL_RET(gl, fImageIncrementLocation, |
| 141 | GetUniformLocation(programID, |
| 142 | fImageIncrementVar->getName().c_str())); |
| 143 | } |
| 144 | |
| 145 | void GrGLConvolutionEffect::setData(const GrGLInterface* gl, |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame^] | 146 | const GrGLTexture& texture, |
| 147 | GrCustomStage* data, |
| 148 | int stageNum) { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 149 | const GrConvolutionEffect* conv = |
| 150 | static_cast<const GrConvolutionEffect*>(data); |
| 151 | // the code we generated was for a specific kernel width |
| 152 | GrAssert(conv->width() == fKernelWidth); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 153 | GR_GL_CALL(gl, Uniform1fv(fKernelLocation, |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 154 | fKernelWidth, |
| 155 | conv->kernel())); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 156 | float imageIncrement[2] = { 0 }; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 157 | switch (conv->direction()) { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 158 | case GrSamplerState::kX_FilterDirection: |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame^] | 159 | imageIncrement[0] = 1.0f / texture.width(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 160 | break; |
| 161 | case GrSamplerState::kY_FilterDirection: |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame^] | 162 | imageIncrement[1] = 1.0f / texture.width(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 163 | break; |
| 164 | default: |
| 165 | GrCrash("Unknown filter direction."); |
| 166 | } |
| 167 | GR_GL_CALL(gl, Uniform2fv(fImageIncrementLocation, 1, imageIncrement)); |
| 168 | } |
| 169 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 170 | GrGLProgramStage::StageKey GrGLConvolutionEffect::GenKey( |
| 171 | const GrCustomStage* s) { |
| 172 | return static_cast<const GrConvolutionEffect*>(s)->width(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | ///////////////////////////////////////////////////////////////////// |
| 176 | |
| 177 | GrConvolutionEffect::GrConvolutionEffect( |
| 178 | GrSamplerState::FilterDirection direction, |
| 179 | unsigned int kernelWidth, |
| 180 | const float* kernel) |
| 181 | : fDirection (direction) |
| 182 | , fKernelWidth (kernelWidth) { |
| 183 | GrAssert(kernelWidth <= MAX_KERNEL_WIDTH); |
| 184 | for (unsigned int i = 0; i < kernelWidth; i++) { |
| 185 | fKernel[i] = kernel[i]; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | GrConvolutionEffect::~GrConvolutionEffect() { |
| 190 | |
| 191 | } |
| 192 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 193 | const GrProgramStageFactory& GrConvolutionEffect::getFactory() const { |
| 194 | return GrTProgramStageFactory<GrConvolutionEffect>::getInstance(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | bool GrConvolutionEffect::isEqual(const GrCustomStage * sBase) const { |
| 198 | const GrConvolutionEffect* s = |
| 199 | static_cast<const GrConvolutionEffect*>(sBase); |
| 200 | |
| 201 | return (fKernelWidth == s->fKernelWidth && |
| 202 | fDirection == s->fDirection && |
| 203 | 0 == memcmp(fKernel, s->fKernel, fKernelWidth * sizeof(float))); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | |