blob: 4970d6270d15a6bd163a0b5fcadd8fde8a42a5cd [file] [log] [blame]
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001/*
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.comd8f856c2012-05-10 12:13:36 +000014class GrGLConvolutionEffect : public GrGLProgramStage {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000015public:
bsalomon@google.com289efe02012-05-21 20:57:59 +000016 GrGLConvolutionEffect(const GrProgramStageFactory& factory,
bsalomon@google.comb505a122012-05-31 18:40:36 +000017 const GrCustomStage& stage);
18
tomhudson@google.com23cb2292012-05-30 18:26:03 +000019 virtual void setupVariables(GrGLShaderBuilder* state,
20 int stage) SK_OVERRIDE;
tomhudson@google.com6a820b62012-05-24 15:10:14 +000021 virtual void emitVS(GrGLShaderBuilder* state,
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000022 const char* vertexCoords) SK_OVERRIDE;
tomhudson@google.com6a820b62012-05-24 15:10:14 +000023 virtual void emitFS(GrGLShaderBuilder* state,
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000024 const char* outputColor,
25 const char* inputColor,
tomhudson@google.com6a820b62012-05-24 15:10:14 +000026 const char* samplerName) SK_OVERRIDE;
bsalomon@google.comb505a122012-05-31 18:40:36 +000027
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000028 virtual void initUniforms(const GrGLInterface*, int programID) SK_OVERRIDE;
29
bsalomon@google.comb505a122012-05-31 18:40:36 +000030 virtual void setData(const GrGLInterface*,
bsalomon@google.comb505a122012-05-31 18:40:36 +000031 const GrCustomStage&,
tomhudson@google.com6a820b62012-05-24 15:10:14 +000032 int stageNum) SK_OVERRIDE;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000033
bsalomon@google.comb505a122012-05-31 18:40:36 +000034 static inline StageKey GenKey(const GrCustomStage&);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000035
36private:
bsalomon@google.comb505a122012-05-31 18:40:36 +000037 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.comd8f856c2012-05-10 12:13:36 +000044
45 typedef GrGLProgramStage INHERITED;
46};
47
bsalomon@google.comb505a122012-05-31 18:40:36 +000048GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProgramStageFactory& factory,
49 const GrCustomStage& stage)
bsalomon@google.com289efe02012-05-21 20:57:59 +000050 : GrGLProgramStage(factory)
51 , fKernelVar(NULL)
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000052 , fKernelLocation(0)
bsalomon@google.comb505a122012-05-31 18:40:36 +000053 , fImageIncrementVar(NULL)
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000054 , fImageIncrementLocation(0) {
bsalomon@google.comb505a122012-05-31 18:40:36 +000055 const GrConvolutionEffect& c =
56 static_cast<const GrConvolutionEffect&>(stage);
57 fRadius = c.radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000058}
59
tomhudson@google.com23cb2292012-05-30 18:26:03 +000060void GrGLConvolutionEffect::setupVariables(GrGLShaderBuilder* state,
61 int stage) {
bsalomon@google.comeb715c82012-07-11 15:03:31 +000062 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.comd8f856c2012-05-10 12:13:36 +000067
68 fImageIncrementLocation = kUseUniform;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000069 fKernelLocation = kUseUniform;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000070}
71
tomhudson@google.com6a820b62012-05-24 15:10:14 +000072void GrGLConvolutionEffect::emitVS(GrGLShaderBuilder* state,
bsalomon@google.comb505a122012-05-31 18:40:36 +000073 const char* vertexCoords) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000074 SkString* code = &state->fVSCode;
bsalomon@google.comb505a122012-05-31 18:40:36 +000075 code->appendf("\t\t%s -= vec2(%d, %d) * %s;\n",
76 vertexCoords, fRadius, fRadius,
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000077 fImageIncrementVar->getName().c_str());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000078}
79
tomhudson@google.com6a820b62012-05-24 15:10:14 +000080void GrGLConvolutionEffect::emitFS(GrGLShaderBuilder* state,
bsalomon@google.comb505a122012-05-31 18:40:36 +000081 const char* outputColor,
82 const char* inputColor,
83 const char* samplerName) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000084 SkString* code = &state->fFSCode;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000085
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000086 code->appendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
bsalomon@google.comb505a122012-05-31 18:40:36 +000087
88 code->appendf("\t\tvec2 coord = %s;\n", state->fSampleCoords.c_str());
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000089
90 int width = this ->width();
tomhudson@google.comf8a22892012-06-11 12:42:24 +000091 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000092 for (int i = 0; i < width; i++) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000093 SkString index;
94 SkString kernelIndex;
tomhudson@google.comf8a22892012-06-11 12:42:24 +000095 index.appendS32(i);
96 fKernelVar->appendArrayAccess(index.c_str(), &kernelIndex);
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000097 code->appendf("\t\t%s += ", outputColor);
tomhudson@google.comf8a22892012-06-11 12:42:24 +000098 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.comd8f856c2012-05-10 12:13:36 +0000103
tomhudson@google.comdb3a1542012-06-13 18:12:57 +0000104 if (state->fModulate.size()) {
105 code->appendf("\t\t%s = %s%s;\n", outputColor, outputColor,
106 state->fModulate.c_str());
107 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000108}
109
110void GrGLConvolutionEffect::initUniforms(const GrGLInterface* gl,
tomhudson@google.comf1d88062012-05-10 12:43:21 +0000111 int programID) {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000112 GR_GL_CALL_RET(gl, fImageIncrementLocation,
113 GetUniformLocation(programID,
114 fImageIncrementVar->getName().c_str()));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000115 GR_GL_CALL_RET(gl, fKernelLocation,
116 GetUniformLocation(programID, fKernelVar->getName().c_str()));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000117}
118
119void GrGLConvolutionEffect::setData(const GrGLInterface* gl,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000120 const GrCustomStage& data,
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000121 int stageNum) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000122 const GrConvolutionEffect& conv =
123 static_cast<const GrConvolutionEffect&>(data);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000124 GrTexture& texture = *data.texture(0);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000125 // the code we generated was for a specific kernel radius
126 GrAssert(conv.radius() == fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000127 float imageIncrement[2] = { 0 };
bsalomon@google.comb505a122012-05-31 18:40:36 +0000128 switch (conv.direction()) {
129 case Gr1DKernelEffect::kX_Direction:
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000130 imageIncrement[0] = 1.0f / texture.width();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000131 break;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000132 case Gr1DKernelEffect::kY_Direction:
133 imageIncrement[1] = 1.0f / texture.height();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000134 break;
135 default:
136 GrCrash("Unknown filter direction.");
137 }
138 GR_GL_CALL(gl, Uniform2fv(fImageIncrementLocation, 1, imageIncrement));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000139
140 GR_GL_CALL(gl, Uniform1fv(fKernelLocation, this->width(), conv.kernel()));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000141}
142
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000143GrGLProgramStage::StageKey GrGLConvolutionEffect::GenKey(
bsalomon@google.comb505a122012-05-31 18:40:36 +0000144 const GrCustomStage& s) {
145 return static_cast<const GrConvolutionEffect&>(s).radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000146}
147
bsalomon@google.comb505a122012-05-31 18:40:36 +0000148///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000149
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000150GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
151 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000152 int radius,
153 const float* kernel)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000154 : Gr1DKernelEffect(texture, direction, radius) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000155 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.comd8f856c2012-05-10 12:13:36 +0000161 }
162}
163
164GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000165}
166
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000167const GrProgramStageFactory& GrConvolutionEffect::getFactory() const {
168 return GrTProgramStageFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000169}
170
bsalomon@google.comb505a122012-05-31 18:40:36 +0000171bool GrConvolutionEffect::isEqual(const GrCustomStage& sBase) const {
172 const GrConvolutionEffect& s =
173 static_cast<const GrConvolutionEffect&>(sBase);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000174 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.comd8f856c2012-05-10 12:13:36 +0000178}
179
bsalomon@google.comb505a122012-05-31 18:40:36 +0000180void 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}