blob: 32086ca57bc97348bde0068b0315c952d3ced5cc [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.com289efe02012-05-21 20:57:59 +000020 GrGLConvolutionEffect(const GrProgramStageFactory& factory,
bsalomon@google.coma469c282012-10-24 18:28:34 +000021 const GrEffect& stage);
bsalomon@google.comb505a122012-05-31 18:40:36 +000022
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000023 virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
bsalomon@google.com032b2212012-07-16 13:36:18 +000024 virtual void emitVS(GrGLShaderBuilder* builder,
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000025 const char* vertexCoords) SK_OVERRIDE {};
bsalomon@google.com032b2212012-07-16 13:36:18 +000026 virtual void emitFS(GrGLShaderBuilder* builder,
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000027 const char* outputColor,
28 const char* inputColor,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000029 const TextureSamplerArray&) SK_OVERRIDE;
bsalomon@google.comb505a122012-05-31 18:40:36 +000030
bsalomon@google.coma469c282012-10-24 18:28:34 +000031 virtual void setData(const GrGLUniformManager& uman, const GrEffect&) SK_OVERRIDE;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000032
bsalomon@google.coma469c282012-10-24 18:28:34 +000033 static inline StageKey GenKey(const GrEffect&, const GrGLCaps&);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000034
35private:
bsalomon@google.comb505a122012-05-31 18:40:36 +000036 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); }
37
bsalomon@google.com032b2212012-07-16 13:36:18 +000038 int fRadius;
39 UniformHandle fKernelUni;
bsalomon@google.com032b2212012-07-16 13:36:18 +000040 UniformHandle fImageIncrementUni;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000041
bsalomon@google.com374e7592012-10-23 17:30:45 +000042 typedef GrGLLegacyProgramStage INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000043};
44
bsalomon@google.comb505a122012-05-31 18:40:36 +000045GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProgramStageFactory& factory,
bsalomon@google.coma469c282012-10-24 18:28:34 +000046 const GrEffect& stage)
bsalomon@google.com374e7592012-10-23 17:30:45 +000047 : INHERITED(factory)
bsalomon@google.com032b2212012-07-16 13:36:18 +000048 , fKernelUni(kInvalidUniformHandle)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000049 , fImageIncrementUni(kInvalidUniformHandle) {
bsalomon@google.comb505a122012-05-31 18:40:36 +000050 const GrConvolutionEffect& c =
51 static_cast<const GrConvolutionEffect&>(stage);
52 fRadius = c.radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000053}
54
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000055void GrGLConvolutionEffect::setupVariables(GrGLShaderBuilder* builder) {
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000056 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000057 kVec2f_GrSLType, "ImageIncrement");
58 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
59 kFloat_GrSLType, "Kernel", this->width());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000060}
61
bsalomon@google.com032b2212012-07-16 13:36:18 +000062void GrGLConvolutionEffect::emitFS(GrGLShaderBuilder* builder,
bsalomon@google.comb505a122012-05-31 18:40:36 +000063 const char* outputColor,
64 const char* inputColor,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000065 const TextureSamplerArray& samplers) {
bsalomon@google.com032b2212012-07-16 13:36:18 +000066 SkString* code = &builder->fFSCode;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000067
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000068 code->appendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000069
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000070 int width = this ->width();
bsalomon@google.com032b2212012-07-16 13:36:18 +000071 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
72 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000073
bsalomon@google.com5c33b712012-08-03 18:22:53 +000074 code->appendf("\t\tvec2 coord = %s - %d.0 * %s;\n",
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000075 builder->defaultTexCoordsName(), fRadius, imgInc);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000076
tomhudson@google.comf8a22892012-06-11 12:42:24 +000077 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000078 for (int i = 0; i < width; i++) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000079 SkString index;
80 SkString kernelIndex;
tomhudson@google.comf8a22892012-06-11 12:42:24 +000081 index.appendS32(i);
bsalomon@google.com032b2212012-07-16 13:36:18 +000082 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000083 code->appendf("\t\t%s += ", outputColor);
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000084 builder->appendTextureLookup(&builder->fFSCode, samplers[0], "coord");
tomhudson@google.comf8a22892012-06-11 12:42:24 +000085 code->appendf(" * %s;\n", kernelIndex.c_str());
bsalomon@google.com032b2212012-07-16 13:36:18 +000086 code->appendf("\t\tcoord += %s;\n", imgInc);
tomhudson@google.comf8a22892012-06-11 12:42:24 +000087 }
bsalomon@google.com868a8e72012-08-30 19:11:34 +000088 GrGLSLMulVarBy4f(&builder->fFSCode, 2, outputColor, inputColor);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000089}
90
bsalomon@google.coma469c282012-10-24 18:28:34 +000091void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman, const GrEffect& data) {
bsalomon@google.comb505a122012-05-31 18:40:36 +000092 const GrConvolutionEffect& conv =
93 static_cast<const GrConvolutionEffect&>(data);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000094 GrTexture& texture = *data.texture(0);
bsalomon@google.comb505a122012-05-31 18:40:36 +000095 // the code we generated was for a specific kernel radius
96 GrAssert(conv.radius() == fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000097 float imageIncrement[2] = { 0 };
bsalomon@google.comb505a122012-05-31 18:40:36 +000098 switch (conv.direction()) {
99 case Gr1DKernelEffect::kX_Direction:
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000100 imageIncrement[0] = 1.0f / texture.width();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000101 break;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000102 case Gr1DKernelEffect::kY_Direction:
103 imageIncrement[1] = 1.0f / texture.height();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000104 break;
105 default:
106 GrCrash("Unknown filter direction.");
107 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000108 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement);
109 uman.set1fv(fKernelUni, 0, this->width(), conv.kernel());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000110}
111
bsalomon@google.coma469c282012-10-24 18:28:34 +0000112GrGLProgramStage::StageKey GrGLConvolutionEffect::GenKey(const GrEffect& s,
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000113 const GrGLCaps& caps) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000114 return static_cast<const GrConvolutionEffect&>(s).radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000115}
116
bsalomon@google.comb505a122012-05-31 18:40:36 +0000117///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000118
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000119GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
120 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000121 int radius,
122 const float* kernel)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000123 : Gr1DKernelEffect(texture, direction, radius) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000124 GrAssert(radius <= kMaxKernelRadius);
125 int width = this->width();
126 if (NULL != kernel) {
127 for (int i = 0; i < width; i++) {
128 fKernel[i] = kernel[i];
129 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000130 }
131}
132
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000133GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
134 Direction direction,
135 int radius,
136 float gaussianSigma)
137 : Gr1DKernelEffect(texture, direction, radius) {
138 GrAssert(radius <= kMaxKernelRadius);
139 int width = this->width();
140
141 float sum = 0.0f;
142 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
143 for (int i = 0; i < width; ++i) {
144 float x = static_cast<float>(i - this->radius());
145 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
146 // is dropped here, since we renormalize the kernel below.
147 fKernel[i] = sk_float_exp(- x * x * denom);
148 sum += fKernel[i];
149 }
150 // Normalize the kernel
151 float scale = 1.0f / sum;
152 for (int i = 0; i < width; ++i) {
153 fKernel[i] *= scale;
154 }
155}
156
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000157GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000158}
159
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000160const GrProgramStageFactory& GrConvolutionEffect::getFactory() const {
161 return GrTProgramStageFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000162}
163
bsalomon@google.coma469c282012-10-24 18:28:34 +0000164bool GrConvolutionEffect::isEqual(const GrEffect& sBase) const {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000165 const GrConvolutionEffect& s =
166 static_cast<const GrConvolutionEffect&>(sBase);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000167 return (INHERITED::isEqual(sBase) &&
168 this->radius() == s.radius() &&
169 this->direction() == s.direction() &&
170 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000171}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000172
173///////////////////////////////////////////////////////////////////////////////
174
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000175GR_DEFINE_EFFECT_TEST(GrConvolutionEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000176
bsalomon@google.coma469c282012-10-24 18:28:34 +0000177GrEffect* GrConvolutionEffect::TestCreate(SkRandom* random,
178 GrContext* context,
179 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000180 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
181 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000182 Direction dir = random->nextBool() ? kX_Direction : kY_Direction;
183 int radius = random->nextRangeU(1, kMaxKernelRadius);
184 float kernel[kMaxKernelRadius];
185 for (int i = 0; i < kMaxKernelRadius; ++i) {
186 kernel[i] = random->nextSScalar1();
187 }
188
189 return SkNEW_ARGS(GrConvolutionEffect, (textures[texIdx], dir, radius, kernel));
190}
191