blob: f01caa390191a18812500f1518f6f4db16c6c45d [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.comdbbc4e22012-07-25 17:48:39 +000014// For brevity
15typedef GrGLUniformManager::UniformHandle UniformHandle;
16static const UniformHandle kInvalidUniformHandle = GrGLUniformManager::kInvalidUniformHandle;
bsalomon@google.com032b2212012-07-16 13:36:18 +000017
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.comdbbc4e22012-07-25 17:48:39 +000032 virtual void setData(const GrGLUniformManager& uman,
bsalomon@google.comb505a122012-05-31 18:40:36 +000033 const GrCustomStage&,
senorblanco@chromium.orgf4770d72012-07-13 18:25:06 +000034 const GrRenderTarget*,
tomhudson@google.com6a820b62012-05-24 15:10:14 +000035 int stageNum) SK_OVERRIDE;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000036
bsalomon@google.comb505a122012-05-31 18:40:36 +000037 static inline StageKey GenKey(const GrCustomStage&);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000038
39private:
bsalomon@google.comb505a122012-05-31 18:40:36 +000040 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); }
41
bsalomon@google.com032b2212012-07-16 13:36:18 +000042 int fRadius;
43 UniformHandle fKernelUni;
bsalomon@google.com032b2212012-07-16 13:36:18 +000044 UniformHandle fImageIncrementUni;
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)
bsalomon@google.com032b2212012-07-16 13:36:18 +000052 , fKernelUni(kInvalidUniformHandle)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000053 , fImageIncrementUni(kInvalidUniformHandle) {
bsalomon@google.comb505a122012-05-31 18:40:36 +000054 const GrConvolutionEffect& c =
55 static_cast<const GrConvolutionEffect&>(stage);
56 fRadius = c.radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000057}
58
bsalomon@google.com032b2212012-07-16 13:36:18 +000059void GrGLConvolutionEffect::setupVariables(GrGLShaderBuilder* builder,
tomhudson@google.com23cb2292012-05-30 18:26:03 +000060 int stage) {
bsalomon@google.com032b2212012-07-16 13:36:18 +000061 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType |
62 GrGLShaderBuilder::kVertex_ShaderType,
63 kVec2f_GrSLType, "uImageIncrement", stage);
64 fKernelUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
65 kFloat_GrSLType, "uKernel", stage, this->width());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000066}
67
bsalomon@google.com032b2212012-07-16 13:36:18 +000068void GrGLConvolutionEffect::emitVS(GrGLShaderBuilder* builder,
bsalomon@google.comb505a122012-05-31 18:40:36 +000069 const char* vertexCoords) {
bsalomon@google.com032b2212012-07-16 13:36:18 +000070 SkString* code = &builder->fVSCode;
71 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
72 code->appendf("\t\t%s -= vec2(%d, %d) * %s;\n", vertexCoords, fRadius, fRadius, imgInc);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000073}
74
bsalomon@google.com032b2212012-07-16 13:36:18 +000075void GrGLConvolutionEffect::emitFS(GrGLShaderBuilder* builder,
bsalomon@google.comb505a122012-05-31 18:40:36 +000076 const char* outputColor,
77 const char* inputColor,
78 const char* samplerName) {
bsalomon@google.com032b2212012-07-16 13:36:18 +000079 SkString* code = &builder->fFSCode;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000080
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000081 code->appendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
bsalomon@google.comb505a122012-05-31 18:40:36 +000082
bsalomon@google.com032b2212012-07-16 13:36:18 +000083 code->appendf("\t\tvec2 coord = %s;\n", builder->fSampleCoords.c_str());
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000084
85 int width = this ->width();
bsalomon@google.com032b2212012-07-16 13:36:18 +000086 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
87 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
tomhudson@google.comf8a22892012-06-11 12:42:24 +000088 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000089 for (int i = 0; i < width; i++) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000090 SkString index;
91 SkString kernelIndex;
tomhudson@google.comf8a22892012-06-11 12:42:24 +000092 index.appendS32(i);
bsalomon@google.com032b2212012-07-16 13:36:18 +000093 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000094 code->appendf("\t\t%s += ", outputColor);
bsalomon@google.com032b2212012-07-16 13:36:18 +000095 builder->emitTextureLookup(samplerName, "coord");
tomhudson@google.comf8a22892012-06-11 12:42:24 +000096 code->appendf(" * %s;\n", kernelIndex.c_str());
bsalomon@google.com032b2212012-07-16 13:36:18 +000097 code->appendf("\t\tcoord += %s;\n", imgInc);
tomhudson@google.comf8a22892012-06-11 12:42:24 +000098 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000099
bsalomon@google.com032b2212012-07-16 13:36:18 +0000100 if (builder->fModulate.size()) {
tomhudson@google.comdb3a1542012-06-13 18:12:57 +0000101 code->appendf("\t\t%s = %s%s;\n", outputColor, outputColor,
bsalomon@google.com032b2212012-07-16 13:36:18 +0000102 builder->fModulate.c_str());
tomhudson@google.comdb3a1542012-06-13 18:12:57 +0000103 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000104}
105
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000106void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000107 const GrCustomStage& data,
senorblanco@chromium.orgf4770d72012-07-13 18:25:06 +0000108 const GrRenderTarget*,
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000109 int stageNum) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000110 const GrConvolutionEffect& conv =
111 static_cast<const GrConvolutionEffect&>(data);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000112 GrTexture& texture = *data.texture(0);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000113 // the code we generated was for a specific kernel radius
114 GrAssert(conv.radius() == fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000115 float imageIncrement[2] = { 0 };
bsalomon@google.comb505a122012-05-31 18:40:36 +0000116 switch (conv.direction()) {
117 case Gr1DKernelEffect::kX_Direction:
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000118 imageIncrement[0] = 1.0f / texture.width();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000119 break;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000120 case Gr1DKernelEffect::kY_Direction:
121 imageIncrement[1] = 1.0f / texture.height();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000122 break;
123 default:
124 GrCrash("Unknown filter direction.");
125 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000126 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement);
127 uman.set1fv(fKernelUni, 0, this->width(), conv.kernel());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000128}
129
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000130GrGLProgramStage::StageKey GrGLConvolutionEffect::GenKey(
bsalomon@google.comb505a122012-05-31 18:40:36 +0000131 const GrCustomStage& s) {
132 return static_cast<const GrConvolutionEffect&>(s).radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000133}
134
bsalomon@google.comb505a122012-05-31 18:40:36 +0000135///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000136
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000137GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
138 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000139 int radius,
140 const float* kernel)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000141 : Gr1DKernelEffect(texture, direction, radius) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000142 GrAssert(radius <= kMaxKernelRadius);
143 int width = this->width();
144 if (NULL != kernel) {
145 for (int i = 0; i < width; i++) {
146 fKernel[i] = kernel[i];
147 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000148 }
149}
150
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000151GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
152 Direction direction,
153 int radius,
154 float gaussianSigma)
155 : Gr1DKernelEffect(texture, direction, radius) {
156 GrAssert(radius <= kMaxKernelRadius);
157 int width = this->width();
158
159 float sum = 0.0f;
160 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
161 for (int i = 0; i < width; ++i) {
162 float x = static_cast<float>(i - this->radius());
163 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
164 // is dropped here, since we renormalize the kernel below.
165 fKernel[i] = sk_float_exp(- x * x * denom);
166 sum += fKernel[i];
167 }
168 // Normalize the kernel
169 float scale = 1.0f / sum;
170 for (int i = 0; i < width; ++i) {
171 fKernel[i] *= scale;
172 }
173}
174
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000175GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000176}
177
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000178const GrProgramStageFactory& GrConvolutionEffect::getFactory() const {
179 return GrTProgramStageFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000180}
181
bsalomon@google.comb505a122012-05-31 18:40:36 +0000182bool GrConvolutionEffect::isEqual(const GrCustomStage& sBase) const {
183 const GrConvolutionEffect& s =
184 static_cast<const GrConvolutionEffect&>(sBase);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000185 return (INHERITED::isEqual(sBase) &&
186 this->radius() == s.radius() &&
187 this->direction() == s.direction() &&
188 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000189}
190