blob: 3804d5713086019973c3a916c74b5d6aa04a0509 [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&,
senorblanco@chromium.orgf4770d72012-07-13 18:25:06 +000032 const GrRenderTarget*,
tomhudson@google.com6a820b62012-05-24 15:10:14 +000033 int stageNum) SK_OVERRIDE;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000034
bsalomon@google.comb505a122012-05-31 18:40:36 +000035 static inline StageKey GenKey(const GrCustomStage&);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000036
37private:
bsalomon@google.comb505a122012-05-31 18:40:36 +000038 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.comd8f856c2012-05-10 12:13:36 +000045
46 typedef GrGLProgramStage INHERITED;
47};
48
bsalomon@google.comb505a122012-05-31 18:40:36 +000049GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProgramStageFactory& factory,
50 const GrCustomStage& stage)
bsalomon@google.com289efe02012-05-21 20:57:59 +000051 : GrGLProgramStage(factory)
52 , fKernelVar(NULL)
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000053 , fKernelLocation(0)
bsalomon@google.comb505a122012-05-31 18:40:36 +000054 , fImageIncrementVar(NULL)
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000055 , fImageIncrementLocation(0) {
bsalomon@google.comb505a122012-05-31 18:40:36 +000056 const GrConvolutionEffect& c =
57 static_cast<const GrConvolutionEffect&>(stage);
58 fRadius = c.radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000059}
60
tomhudson@google.com23cb2292012-05-30 18:26:03 +000061void GrGLConvolutionEffect::setupVariables(GrGLShaderBuilder* state,
62 int stage) {
bsalomon@google.comeb715c82012-07-11 15:03:31 +000063 fImageIncrementVar = &state->addUniform(GrGLShaderBuilder::kFragment_ShaderType |
64 GrGLShaderBuilder::kVertex_ShaderType,
65 kVec2f_GrSLType, "uImageIncrement", stage);
66 fKernelVar = &state->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
67 kFloat_GrSLType, "uKernel", stage, this->width());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000068
69 fImageIncrementLocation = kUseUniform;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000070 fKernelLocation = kUseUniform;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000071}
72
tomhudson@google.com6a820b62012-05-24 15:10:14 +000073void GrGLConvolutionEffect::emitVS(GrGLShaderBuilder* state,
bsalomon@google.comb505a122012-05-31 18:40:36 +000074 const char* vertexCoords) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000075 SkString* code = &state->fVSCode;
bsalomon@google.comb505a122012-05-31 18:40:36 +000076 code->appendf("\t\t%s -= vec2(%d, %d) * %s;\n",
77 vertexCoords, fRadius, fRadius,
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000078 fImageIncrementVar->getName().c_str());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000079}
80
tomhudson@google.com6a820b62012-05-24 15:10:14 +000081void GrGLConvolutionEffect::emitFS(GrGLShaderBuilder* state,
bsalomon@google.comb505a122012-05-31 18:40:36 +000082 const char* outputColor,
83 const char* inputColor,
84 const char* samplerName) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000085 SkString* code = &state->fFSCode;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000086
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000087 code->appendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
bsalomon@google.comb505a122012-05-31 18:40:36 +000088
89 code->appendf("\t\tvec2 coord = %s;\n", state->fSampleCoords.c_str());
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000090
91 int width = this ->width();
tomhudson@google.comf8a22892012-06-11 12:42:24 +000092 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000093 for (int i = 0; i < width; i++) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000094 SkString index;
95 SkString kernelIndex;
tomhudson@google.comf8a22892012-06-11 12:42:24 +000096 index.appendS32(i);
97 fKernelVar->appendArrayAccess(index.c_str(), &kernelIndex);
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000098 code->appendf("\t\t%s += ", outputColor);
tomhudson@google.comf8a22892012-06-11 12:42:24 +000099 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.comd8f856c2012-05-10 12:13:36 +0000104
tomhudson@google.comdb3a1542012-06-13 18:12:57 +0000105 if (state->fModulate.size()) {
106 code->appendf("\t\t%s = %s%s;\n", outputColor, outputColor,
107 state->fModulate.c_str());
108 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000109}
110
111void GrGLConvolutionEffect::initUniforms(const GrGLInterface* gl,
tomhudson@google.comf1d88062012-05-10 12:43:21 +0000112 int programID) {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000113 GR_GL_CALL_RET(gl, fImageIncrementLocation,
114 GetUniformLocation(programID,
115 fImageIncrementVar->getName().c_str()));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000116 GR_GL_CALL_RET(gl, fKernelLocation,
117 GetUniformLocation(programID, fKernelVar->getName().c_str()));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000118}
119
120void GrGLConvolutionEffect::setData(const GrGLInterface* gl,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000121 const GrCustomStage& data,
senorblanco@chromium.orgf4770d72012-07-13 18:25:06 +0000122 const GrRenderTarget*,
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000123 int stageNum) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000124 const GrConvolutionEffect& conv =
125 static_cast<const GrConvolutionEffect&>(data);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000126 GrTexture& texture = *data.texture(0);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000127 // the code we generated was for a specific kernel radius
128 GrAssert(conv.radius() == fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000129 float imageIncrement[2] = { 0 };
bsalomon@google.comb505a122012-05-31 18:40:36 +0000130 switch (conv.direction()) {
131 case Gr1DKernelEffect::kX_Direction:
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000132 imageIncrement[0] = 1.0f / texture.width();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000133 break;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000134 case Gr1DKernelEffect::kY_Direction:
135 imageIncrement[1] = 1.0f / texture.height();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000136 break;
137 default:
138 GrCrash("Unknown filter direction.");
139 }
140 GR_GL_CALL(gl, Uniform2fv(fImageIncrementLocation, 1, imageIncrement));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000141
142 GR_GL_CALL(gl, Uniform1fv(fKernelLocation, this->width(), conv.kernel()));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000143}
144
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000145GrGLProgramStage::StageKey GrGLConvolutionEffect::GenKey(
bsalomon@google.comb505a122012-05-31 18:40:36 +0000146 const GrCustomStage& s) {
147 return static_cast<const GrConvolutionEffect&>(s).radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000148}
149
bsalomon@google.comb505a122012-05-31 18:40:36 +0000150///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000151
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000152GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
153 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000154 int radius,
155 const float* kernel)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000156 : Gr1DKernelEffect(texture, direction, radius) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000157 GrAssert(radius <= kMaxKernelRadius);
158 int width = this->width();
159 if (NULL != kernel) {
160 for (int i = 0; i < width; i++) {
161 fKernel[i] = kernel[i];
162 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000163 }
164}
165
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000166GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
167 Direction direction,
168 int radius,
169 float gaussianSigma)
170 : Gr1DKernelEffect(texture, direction, radius) {
171 GrAssert(radius <= kMaxKernelRadius);
172 int width = this->width();
173
174 float sum = 0.0f;
175 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
176 for (int i = 0; i < width; ++i) {
177 float x = static_cast<float>(i - this->radius());
178 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
179 // is dropped here, since we renormalize the kernel below.
180 fKernel[i] = sk_float_exp(- x * x * denom);
181 sum += fKernel[i];
182 }
183 // Normalize the kernel
184 float scale = 1.0f / sum;
185 for (int i = 0; i < width; ++i) {
186 fKernel[i] *= scale;
187 }
188}
189
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000190GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000191}
192
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000193const GrProgramStageFactory& GrConvolutionEffect::getFactory() const {
194 return GrTProgramStageFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000195}
196
bsalomon@google.comb505a122012-05-31 18:40:36 +0000197bool GrConvolutionEffect::isEqual(const GrCustomStage& sBase) const {
198 const GrConvolutionEffect& s =
199 static_cast<const GrConvolutionEffect&>(sBase);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000200 return (INHERITED::isEqual(sBase) &&
201 this->radius() == s.radius() &&
202 this->direction() == s.direction() &&
203 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000204}
205