blob: e49302ef9454dd518e88d876530ce8be9f064561 [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
bsalomon@google.com374e7592012-10-23 17:30:45 +000018class GrGLConvolutionEffect : public GrGLLegacyProgramStage {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000019public:
bsalomon@google.com021fc732012-10-25 12:47:42 +000020 GrGLConvolutionEffect(const GrProgramStageFactory&, const GrEffect&);
bsalomon@google.comb505a122012-05-31 18:40:36 +000021
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000022 virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
bsalomon@google.com032b2212012-07-16 13:36:18 +000023 virtual void emitVS(GrGLShaderBuilder* builder,
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000024 const char* vertexCoords) SK_OVERRIDE {};
bsalomon@google.com032b2212012-07-16 13:36:18 +000025 virtual void emitFS(GrGLShaderBuilder* builder,
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000026 const char* outputColor,
27 const char* inputColor,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000028 const TextureSamplerArray&) SK_OVERRIDE;
bsalomon@google.comb505a122012-05-31 18:40:36 +000029
bsalomon@google.coma469c282012-10-24 18:28:34 +000030 virtual void setData(const GrGLUniformManager& uman, const GrEffect&) SK_OVERRIDE;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000031
bsalomon@google.coma469c282012-10-24 18:28:34 +000032 static inline StageKey GenKey(const GrEffect&, const GrGLCaps&);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000033
34private:
bsalomon@google.comb505a122012-05-31 18:40:36 +000035 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); }
36
bsalomon@google.com032b2212012-07-16 13:36:18 +000037 int fRadius;
38 UniformHandle fKernelUni;
bsalomon@google.com032b2212012-07-16 13:36:18 +000039 UniformHandle fImageIncrementUni;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000040
bsalomon@google.com374e7592012-10-23 17:30:45 +000041 typedef GrGLLegacyProgramStage INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000042};
43
bsalomon@google.comb505a122012-05-31 18:40:36 +000044GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProgramStageFactory& factory,
bsalomon@google.com021fc732012-10-25 12:47:42 +000045 const GrEffect& effect)
bsalomon@google.com374e7592012-10-23 17:30:45 +000046 : INHERITED(factory)
bsalomon@google.com032b2212012-07-16 13:36:18 +000047 , fKernelUni(kInvalidUniformHandle)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000048 , fImageIncrementUni(kInvalidUniformHandle) {
bsalomon@google.comb505a122012-05-31 18:40:36 +000049 const GrConvolutionEffect& c =
bsalomon@google.com021fc732012-10-25 12:47:42 +000050 static_cast<const GrConvolutionEffect&>(effect);
bsalomon@google.comb505a122012-05-31 18:40:36 +000051 fRadius = c.radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000052}
53
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000054void GrGLConvolutionEffect::setupVariables(GrGLShaderBuilder* builder) {
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000055 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000056 kVec2f_GrSLType, "ImageIncrement");
57 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
58 kFloat_GrSLType, "Kernel", this->width());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000059}
60
bsalomon@google.com032b2212012-07-16 13:36:18 +000061void GrGLConvolutionEffect::emitFS(GrGLShaderBuilder* builder,
bsalomon@google.comb505a122012-05-31 18:40:36 +000062 const char* outputColor,
63 const char* inputColor,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000064 const TextureSamplerArray& samplers) {
bsalomon@google.com032b2212012-07-16 13:36:18 +000065 SkString* code = &builder->fFSCode;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000066
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000067 code->appendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000068
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000069 int width = this ->width();
bsalomon@google.com032b2212012-07-16 13:36:18 +000070 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
71 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000072
bsalomon@google.com5c33b712012-08-03 18:22:53 +000073 code->appendf("\t\tvec2 coord = %s - %d.0 * %s;\n",
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000074 builder->defaultTexCoordsName(), fRadius, imgInc);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000075
tomhudson@google.comf8a22892012-06-11 12:42:24 +000076 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000077 for (int i = 0; i < width; i++) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000078 SkString index;
79 SkString kernelIndex;
tomhudson@google.comf8a22892012-06-11 12:42:24 +000080 index.appendS32(i);
bsalomon@google.com032b2212012-07-16 13:36:18 +000081 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000082 code->appendf("\t\t%s += ", outputColor);
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000083 builder->appendTextureLookup(&builder->fFSCode, samplers[0], "coord");
tomhudson@google.comf8a22892012-06-11 12:42:24 +000084 code->appendf(" * %s;\n", kernelIndex.c_str());
bsalomon@google.com032b2212012-07-16 13:36:18 +000085 code->appendf("\t\tcoord += %s;\n", imgInc);
tomhudson@google.comf8a22892012-06-11 12:42:24 +000086 }
bsalomon@google.com868a8e72012-08-30 19:11:34 +000087 GrGLSLMulVarBy4f(&builder->fFSCode, 2, outputColor, inputColor);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000088}
89
bsalomon@google.coma469c282012-10-24 18:28:34 +000090void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman, const GrEffect& data) {
bsalomon@google.comb505a122012-05-31 18:40:36 +000091 const GrConvolutionEffect& conv =
92 static_cast<const GrConvolutionEffect&>(data);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000093 GrTexture& texture = *data.texture(0);
bsalomon@google.comb505a122012-05-31 18:40:36 +000094 // the code we generated was for a specific kernel radius
95 GrAssert(conv.radius() == fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000096 float imageIncrement[2] = { 0 };
bsalomon@google.comb505a122012-05-31 18:40:36 +000097 switch (conv.direction()) {
98 case Gr1DKernelEffect::kX_Direction:
tomhudson@google.com6a820b62012-05-24 15:10:14 +000099 imageIncrement[0] = 1.0f / texture.width();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000100 break;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000101 case Gr1DKernelEffect::kY_Direction:
102 imageIncrement[1] = 1.0f / texture.height();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000103 break;
104 default:
105 GrCrash("Unknown filter direction.");
106 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000107 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement);
108 uman.set1fv(fKernelUni, 0, this->width(), conv.kernel());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000109}
110
bsalomon@google.coma469c282012-10-24 18:28:34 +0000111GrGLProgramStage::StageKey GrGLConvolutionEffect::GenKey(const GrEffect& s,
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000112 const GrGLCaps& caps) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000113 return static_cast<const GrConvolutionEffect&>(s).radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000114}
115
bsalomon@google.comb505a122012-05-31 18:40:36 +0000116///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000117
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000118GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
119 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000120 int radius,
121 const float* kernel)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000122 : Gr1DKernelEffect(texture, direction, radius) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000123 GrAssert(radius <= kMaxKernelRadius);
124 int width = this->width();
125 if (NULL != kernel) {
126 for (int i = 0; i < width; i++) {
127 fKernel[i] = kernel[i];
128 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000129 }
130}
131
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000132GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
133 Direction direction,
134 int radius,
135 float gaussianSigma)
136 : Gr1DKernelEffect(texture, direction, radius) {
137 GrAssert(radius <= kMaxKernelRadius);
138 int width = this->width();
139
140 float sum = 0.0f;
141 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
142 for (int i = 0; i < width; ++i) {
143 float x = static_cast<float>(i - this->radius());
144 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
145 // is dropped here, since we renormalize the kernel below.
146 fKernel[i] = sk_float_exp(- x * x * denom);
147 sum += fKernel[i];
148 }
149 // Normalize the kernel
150 float scale = 1.0f / sum;
151 for (int i = 0; i < width; ++i) {
152 fKernel[i] *= scale;
153 }
154}
155
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000156GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000157}
158
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000159const GrProgramStageFactory& GrConvolutionEffect::getFactory() const {
160 return GrTProgramStageFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000161}
162
bsalomon@google.coma469c282012-10-24 18:28:34 +0000163bool GrConvolutionEffect::isEqual(const GrEffect& sBase) const {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000164 const GrConvolutionEffect& s =
165 static_cast<const GrConvolutionEffect&>(sBase);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000166 return (INHERITED::isEqual(sBase) &&
167 this->radius() == s.radius() &&
168 this->direction() == s.direction() &&
169 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000170}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000171
172///////////////////////////////////////////////////////////////////////////////
173
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000174GR_DEFINE_EFFECT_TEST(GrConvolutionEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000175
bsalomon@google.coma469c282012-10-24 18:28:34 +0000176GrEffect* GrConvolutionEffect::TestCreate(SkRandom* random,
177 GrContext* context,
178 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000179 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
180 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000181 Direction dir = random->nextBool() ? kX_Direction : kY_Direction;
182 int radius = random->nextRangeU(1, kMaxKernelRadius);
183 float kernel[kMaxKernelRadius];
184 for (int i = 0; i < kMaxKernelRadius; ++i) {
185 kernel[i] = random->nextSScalar1();
186 }
187
188 return SkNEW_ARGS(GrConvolutionEffect, (textures[texIdx], dir, radius, kernel));
189}
190