blob: a16313eac8acfa86eb9684907fa5a6e1b830e7f2 [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
bsalomon@google.com032b2212012-07-16 13:36:18 +000014// For brevity, and these definitions are likely to move to a different class soon.
15typedef GrGLShaderBuilder::UniformHandle UniformHandle;
16static const UniformHandle kInvalidUniformHandle = GrGLShaderBuilder::kInvalidUniformHandle;
17
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000018class GrGLConvolutionEffect : public GrGLProgramStage {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000019public:
bsalomon@google.com289efe02012-05-21 20:57:59 +000020 GrGLConvolutionEffect(const GrProgramStageFactory& factory,
bsalomon@google.comb505a122012-05-31 18:40:36 +000021 const GrCustomStage& stage);
22
bsalomon@google.com032b2212012-07-16 13:36:18 +000023 virtual void setupVariables(GrGLShaderBuilder* builder,
tomhudson@google.com23cb2292012-05-30 18:26:03 +000024 int stage) SK_OVERRIDE;
bsalomon@google.com032b2212012-07-16 13:36:18 +000025 virtual void emitVS(GrGLShaderBuilder* builder,
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000026 const char* vertexCoords) SK_OVERRIDE;
bsalomon@google.com032b2212012-07-16 13:36:18 +000027 virtual void emitFS(GrGLShaderBuilder* builder,
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000028 const char* outputColor,
29 const char* inputColor,
tomhudson@google.com6a820b62012-05-24 15:10:14 +000030 const char* samplerName) SK_OVERRIDE;
bsalomon@google.comb505a122012-05-31 18:40:36 +000031
bsalomon@google.com032b2212012-07-16 13:36:18 +000032 virtual void initUniforms(const GrGLShaderBuilder*,
33 const GrGLInterface*,
34 int programID) SK_OVERRIDE;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000035
bsalomon@google.comb505a122012-05-31 18:40:36 +000036 virtual void setData(const GrGLInterface*,
bsalomon@google.comb505a122012-05-31 18:40:36 +000037 const GrCustomStage&,
senorblanco@chromium.orgf4770d72012-07-13 18:25:06 +000038 const GrRenderTarget*,
tomhudson@google.com6a820b62012-05-24 15:10:14 +000039 int stageNum) SK_OVERRIDE;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000040
bsalomon@google.comb505a122012-05-31 18:40:36 +000041 static inline StageKey GenKey(const GrCustomStage&);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000042
43private:
bsalomon@google.comb505a122012-05-31 18:40:36 +000044 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); }
45
bsalomon@google.com032b2212012-07-16 13:36:18 +000046 int fRadius;
47 UniformHandle fKernelUni;
48 GrGLint fKernelLocation;
49 UniformHandle fImageIncrementUni;
50 GrGLint fImageIncrementLocation;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000051
52 typedef GrGLProgramStage INHERITED;
53};
54
bsalomon@google.comb505a122012-05-31 18:40:36 +000055GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProgramStageFactory& factory,
56 const GrCustomStage& stage)
bsalomon@google.com289efe02012-05-21 20:57:59 +000057 : GrGLProgramStage(factory)
bsalomon@google.com032b2212012-07-16 13:36:18 +000058 , fKernelUni(kInvalidUniformHandle)
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000059 , fKernelLocation(0)
bsalomon@google.com032b2212012-07-16 13:36:18 +000060 , fImageIncrementUni(kInvalidUniformHandle)
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000061 , fImageIncrementLocation(0) {
bsalomon@google.comb505a122012-05-31 18:40:36 +000062 const GrConvolutionEffect& c =
63 static_cast<const GrConvolutionEffect&>(stage);
64 fRadius = c.radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000065}
66
bsalomon@google.com032b2212012-07-16 13:36:18 +000067void GrGLConvolutionEffect::setupVariables(GrGLShaderBuilder* builder,
tomhudson@google.com23cb2292012-05-30 18:26:03 +000068 int stage) {
bsalomon@google.com032b2212012-07-16 13:36:18 +000069 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType |
70 GrGLShaderBuilder::kVertex_ShaderType,
71 kVec2f_GrSLType, "uImageIncrement", stage);
72 fKernelUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
73 kFloat_GrSLType, "uKernel", stage, this->width());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000074
75 fImageIncrementLocation = kUseUniform;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000076 fKernelLocation = kUseUniform;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000077}
78
bsalomon@google.com032b2212012-07-16 13:36:18 +000079void GrGLConvolutionEffect::emitVS(GrGLShaderBuilder* builder,
bsalomon@google.comb505a122012-05-31 18:40:36 +000080 const char* vertexCoords) {
bsalomon@google.com032b2212012-07-16 13:36:18 +000081 SkString* code = &builder->fVSCode;
82 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
83 code->appendf("\t\t%s -= vec2(%d, %d) * %s;\n", vertexCoords, fRadius, fRadius, imgInc);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000084}
85
bsalomon@google.com032b2212012-07-16 13:36:18 +000086void GrGLConvolutionEffect::emitFS(GrGLShaderBuilder* builder,
bsalomon@google.comb505a122012-05-31 18:40:36 +000087 const char* outputColor,
88 const char* inputColor,
89 const char* samplerName) {
bsalomon@google.com032b2212012-07-16 13:36:18 +000090 SkString* code = &builder->fFSCode;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000091
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000092 code->appendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
bsalomon@google.comb505a122012-05-31 18:40:36 +000093
bsalomon@google.com032b2212012-07-16 13:36:18 +000094 code->appendf("\t\tvec2 coord = %s;\n", builder->fSampleCoords.c_str());
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000095
96 int width = this ->width();
bsalomon@google.com032b2212012-07-16 13:36:18 +000097 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
98 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
tomhudson@google.comf8a22892012-06-11 12:42:24 +000099 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +0000100 for (int i = 0; i < width; i++) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000101 SkString index;
102 SkString kernelIndex;
tomhudson@google.comf8a22892012-06-11 12:42:24 +0000103 index.appendS32(i);
bsalomon@google.com032b2212012-07-16 13:36:18 +0000104 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
tomhudson@google.comdb3a1542012-06-13 18:12:57 +0000105 code->appendf("\t\t%s += ", outputColor);
bsalomon@google.com032b2212012-07-16 13:36:18 +0000106 builder->emitTextureLookup(samplerName, "coord");
tomhudson@google.comf8a22892012-06-11 12:42:24 +0000107 code->appendf(" * %s;\n", kernelIndex.c_str());
bsalomon@google.com032b2212012-07-16 13:36:18 +0000108 code->appendf("\t\tcoord += %s;\n", imgInc);
tomhudson@google.comf8a22892012-06-11 12:42:24 +0000109 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000110
bsalomon@google.com032b2212012-07-16 13:36:18 +0000111 if (builder->fModulate.size()) {
tomhudson@google.comdb3a1542012-06-13 18:12:57 +0000112 code->appendf("\t\t%s = %s%s;\n", outputColor, outputColor,
bsalomon@google.com032b2212012-07-16 13:36:18 +0000113 builder->fModulate.c_str());
tomhudson@google.comdb3a1542012-06-13 18:12:57 +0000114 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000115}
116
bsalomon@google.com032b2212012-07-16 13:36:18 +0000117void GrGLConvolutionEffect::initUniforms(const GrGLShaderBuilder* builder,
118 const GrGLInterface* gl,
tomhudson@google.comf1d88062012-05-10 12:43:21 +0000119 int programID) {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000120 const char* kernel = builder->getUniformCStr(fKernelUni);
121 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
122 GR_GL_CALL_RET(gl, fImageIncrementLocation, GetUniformLocation(programID, imgInc));
123 GR_GL_CALL_RET(gl, fKernelLocation, GetUniformLocation(programID, kernel));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000124}
125
126void GrGLConvolutionEffect::setData(const GrGLInterface* gl,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000127 const GrCustomStage& data,
senorblanco@chromium.orgf4770d72012-07-13 18:25:06 +0000128 const GrRenderTarget*,
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000129 int stageNum) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000130 const GrConvolutionEffect& conv =
131 static_cast<const GrConvolutionEffect&>(data);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000132 GrTexture& texture = *data.texture(0);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000133 // the code we generated was for a specific kernel radius
134 GrAssert(conv.radius() == fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000135 float imageIncrement[2] = { 0 };
bsalomon@google.comb505a122012-05-31 18:40:36 +0000136 switch (conv.direction()) {
137 case Gr1DKernelEffect::kX_Direction:
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000138 imageIncrement[0] = 1.0f / texture.width();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000139 break;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000140 case Gr1DKernelEffect::kY_Direction:
141 imageIncrement[1] = 1.0f / texture.height();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000142 break;
143 default:
144 GrCrash("Unknown filter direction.");
145 }
146 GR_GL_CALL(gl, Uniform2fv(fImageIncrementLocation, 1, imageIncrement));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000147
148 GR_GL_CALL(gl, Uniform1fv(fKernelLocation, this->width(), conv.kernel()));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000149}
150
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000151GrGLProgramStage::StageKey GrGLConvolutionEffect::GenKey(
bsalomon@google.comb505a122012-05-31 18:40:36 +0000152 const GrCustomStage& s) {
153 return static_cast<const GrConvolutionEffect&>(s).radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000154}
155
bsalomon@google.comb505a122012-05-31 18:40:36 +0000156///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000157
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000158GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
159 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000160 int radius,
161 const float* kernel)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000162 : Gr1DKernelEffect(texture, direction, radius) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000163 GrAssert(radius <= kMaxKernelRadius);
164 int width = this->width();
165 if (NULL != kernel) {
166 for (int i = 0; i < width; i++) {
167 fKernel[i] = kernel[i];
168 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000169 }
170}
171
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000172GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
173 Direction direction,
174 int radius,
175 float gaussianSigma)
176 : Gr1DKernelEffect(texture, direction, radius) {
177 GrAssert(radius <= kMaxKernelRadius);
178 int width = this->width();
179
180 float sum = 0.0f;
181 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
182 for (int i = 0; i < width; ++i) {
183 float x = static_cast<float>(i - this->radius());
184 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
185 // is dropped here, since we renormalize the kernel below.
186 fKernel[i] = sk_float_exp(- x * x * denom);
187 sum += fKernel[i];
188 }
189 // Normalize the kernel
190 float scale = 1.0f / sum;
191 for (int i = 0; i < width; ++i) {
192 fKernel[i] *= scale;
193 }
194}
195
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000196GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000197}
198
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000199const GrProgramStageFactory& GrConvolutionEffect::getFactory() const {
200 return GrTProgramStageFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000201}
202
bsalomon@google.comb505a122012-05-31 18:40:36 +0000203bool GrConvolutionEffect::isEqual(const GrCustomStage& sBase) const {
204 const GrConvolutionEffect& s =
205 static_cast<const GrConvolutionEffect&>(sBase);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000206 return (INHERITED::isEqual(sBase) &&
207 this->radius() == s.radius() &&
208 this->direction() == s.direction() &&
209 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000210}
211