blob: 5aaa1858a0a5b117084a7c312eb7b55f49841fb1 [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*,
tomhudson@google.com6a820b62012-05-24 15:10:14 +000031 const GrGLTexture&,
bsalomon@google.comb505a122012-05-31 18:40:36 +000032 const GrCustomStage&,
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) {
63 fImageIncrementVar = &state->addUniform(
64 GrGLShaderBuilder::kBoth_VariableLifetime,
65 kVec2f_GrSLType, "uImageIncrement", stage);
66 fKernelVar = &state->addUniform(
67 GrGLShaderBuilder::kFragment_VariableLifetime,
bsalomon@google.comb505a122012-05-31 18:40:36 +000068 kFloat_GrSLType, "uKernel", stage, this->width());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000069
70 fImageIncrementLocation = kUseUniform;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000071 fKernelLocation = kUseUniform;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000072}
73
tomhudson@google.com6a820b62012-05-24 15:10:14 +000074void GrGLConvolutionEffect::emitVS(GrGLShaderBuilder* state,
bsalomon@google.comb505a122012-05-31 18:40:36 +000075 const char* vertexCoords) {
tomhudson@google.com6a820b62012-05-24 15:10:14 +000076 GrStringBuilder* code = &state->fVSCode;
bsalomon@google.comb505a122012-05-31 18:40:36 +000077 code->appendf("\t\t%s -= vec2(%d, %d) * %s;\n",
78 vertexCoords, fRadius, fRadius,
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000079 fImageIncrementVar->getName().c_str());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000080}
81
tomhudson@google.com6a820b62012-05-24 15:10:14 +000082void GrGLConvolutionEffect::emitFS(GrGLShaderBuilder* state,
bsalomon@google.comb505a122012-05-31 18:40:36 +000083 const char* outputColor,
84 const char* inputColor,
85 const char* samplerName) {
tomhudson@google.com6a820b62012-05-24 15:10:14 +000086 GrStringBuilder* code = &state->fFSCode;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000087
tomhudson@google.comf8a22892012-06-11 12:42:24 +000088 code->appendf("\t\tvec4 sum = vec4(0, 0, 0, 0);\n");
bsalomon@google.comb505a122012-05-31 18:40:36 +000089
90 code->appendf("\t\tvec2 coord = %s;\n", state->fSampleCoords.c_str());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000091
tomhudson@google.comf8a22892012-06-11 12:42:24 +000092 // 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);
98 code->appendf("\t\tsum += ");
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.comd8f856c2012-05-10 12:13:36 +0000104
tomhudson@google.comf8a22892012-06-11 12:42:24 +0000105 code->appendf("\t\t%s = sum%s;\n", outputColor, state->fModulate.c_str());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000106}
107
108void GrGLConvolutionEffect::initUniforms(const GrGLInterface* gl,
tomhudson@google.comf1d88062012-05-10 12:43:21 +0000109 int programID) {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000110 GR_GL_CALL_RET(gl, fImageIncrementLocation,
111 GetUniformLocation(programID,
112 fImageIncrementVar->getName().c_str()));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000113 GR_GL_CALL_RET(gl, fKernelLocation,
114 GetUniformLocation(programID, fKernelVar->getName().c_str()));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000115}
116
117void GrGLConvolutionEffect::setData(const GrGLInterface* gl,
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000118 const GrGLTexture& texture,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000119 const GrCustomStage& data,
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000120 int stageNum) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000121 const GrConvolutionEffect& conv =
122 static_cast<const GrConvolutionEffect&>(data);
123 // the code we generated was for a specific kernel radius
124 GrAssert(conv.radius() == fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000125 float imageIncrement[2] = { 0 };
bsalomon@google.comb505a122012-05-31 18:40:36 +0000126 switch (conv.direction()) {
127 case Gr1DKernelEffect::kX_Direction:
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000128 imageIncrement[0] = 1.0f / texture.width();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000129 break;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000130 case Gr1DKernelEffect::kY_Direction:
131 imageIncrement[1] = 1.0f / texture.height();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000132 break;
133 default:
134 GrCrash("Unknown filter direction.");
135 }
136 GR_GL_CALL(gl, Uniform2fv(fImageIncrementLocation, 1, imageIncrement));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000137
138 GR_GL_CALL(gl, Uniform1fv(fKernelLocation, this->width(), conv.kernel()));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000139}
140
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000141GrGLProgramStage::StageKey GrGLConvolutionEffect::GenKey(
bsalomon@google.comb505a122012-05-31 18:40:36 +0000142 const GrCustomStage& s) {
143 return static_cast<const GrConvolutionEffect&>(s).radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000144}
145
bsalomon@google.comb505a122012-05-31 18:40:36 +0000146///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000147
bsalomon@google.comb505a122012-05-31 18:40:36 +0000148GrConvolutionEffect::GrConvolutionEffect(Direction direction,
149 int radius,
150 const float* kernel)
151 : Gr1DKernelEffect(direction, radius) {
152 GrAssert(radius <= kMaxKernelRadius);
153 int width = this->width();
154 if (NULL != kernel) {
155 for (int i = 0; i < width; i++) {
156 fKernel[i] = kernel[i];
157 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000158 }
159}
160
161GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000162}
163
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000164const GrProgramStageFactory& GrConvolutionEffect::getFactory() const {
165 return GrTProgramStageFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000166}
167
bsalomon@google.comb505a122012-05-31 18:40:36 +0000168bool GrConvolutionEffect::isEqual(const GrCustomStage& sBase) const {
169 const GrConvolutionEffect& s =
170 static_cast<const GrConvolutionEffect&>(sBase);
171 return (this->radius() == s.radius() &&
172 this->direction() == s.direction() &&
173 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000174}
175
bsalomon@google.comb505a122012-05-31 18:40:36 +0000176void GrConvolutionEffect::setGaussianKernel(float sigma) {
177 int width = this->width();
178 float sum = 0.0f;
179 float denom = 1.0f / (2.0f * sigma * sigma);
180 for (int i = 0; i < width; ++i) {
181 float x = static_cast<float>(i - this->radius());
182 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
183 // is dropped here, since we renormalize the kernel below.
184 fKernel[i] = sk_float_exp(- x * x * denom);
185 sum += fKernel[i];
186 }
187 // Normalize the kernel
188 float scale = 1.0f / sum;
189 for (int i = 0; i < width; ++i) {
190 fKernel[i] *= scale;
191 }
192}