blob: a28d44d76b8a1ce4241a0b8a4e6d814dbd43b6ce [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.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,
tomhudson@google.com6a820b62012-05-24 15:10:14 +000029 const char* samplerName) SK_OVERRIDE;
bsalomon@google.comb505a122012-05-31 18:40:36 +000030
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000031 virtual void setData(const GrGLUniformManager& uman,
bsalomon@google.comb505a122012-05-31 18:40:36 +000032 const GrCustomStage&,
senorblanco@chromium.orgf4770d72012-07-13 18:25:06 +000033 const GrRenderTarget*,
tomhudson@google.com6a820b62012-05-24 15:10:14 +000034 int stageNum) SK_OVERRIDE;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000035
twiz@google.coma5e65ec2012-08-02 15:15:16 +000036 static inline StageKey GenKey(const GrCustomStage&, const GrGLCaps&);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000037
38private:
bsalomon@google.comb505a122012-05-31 18:40:36 +000039 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); }
40
bsalomon@google.com032b2212012-07-16 13:36:18 +000041 int fRadius;
42 UniformHandle fKernelUni;
bsalomon@google.com032b2212012-07-16 13:36:18 +000043 UniformHandle fImageIncrementUni;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000044
45 typedef GrGLProgramStage INHERITED;
46};
47
bsalomon@google.comb505a122012-05-31 18:40:36 +000048GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProgramStageFactory& factory,
49 const GrCustomStage& stage)
bsalomon@google.com289efe02012-05-21 20:57:59 +000050 : GrGLProgramStage(factory)
bsalomon@google.com032b2212012-07-16 13:36:18 +000051 , fKernelUni(kInvalidUniformHandle)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000052 , fImageIncrementUni(kInvalidUniformHandle) {
bsalomon@google.comb505a122012-05-31 18:40:36 +000053 const GrConvolutionEffect& c =
54 static_cast<const GrConvolutionEffect&>(stage);
55 fRadius = c.radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000056}
57
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000058void GrGLConvolutionEffect::setupVariables(GrGLShaderBuilder* builder) {
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000059 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000060 kVec2f_GrSLType, "ImageIncrement");
61 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
62 kFloat_GrSLType, "Kernel", this->width());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000063}
64
bsalomon@google.com032b2212012-07-16 13:36:18 +000065void GrGLConvolutionEffect::emitFS(GrGLShaderBuilder* builder,
bsalomon@google.comb505a122012-05-31 18:40:36 +000066 const char* outputColor,
67 const char* inputColor,
68 const char* samplerName) {
bsalomon@google.com032b2212012-07-16 13:36:18 +000069 SkString* code = &builder->fFSCode;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000070
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000071 code->appendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000072
73 int width = this ->width();
bsalomon@google.com032b2212012-07-16 13:36:18 +000074 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
75 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000076
77 code->appendf("\t\tvec2 coord = %s - %d * %s;\n",
78 builder->fSampleCoords.c_str(), fRadius, imgInc);
79
tomhudson@google.comf8a22892012-06-11 12:42:24 +000080 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000081 for (int i = 0; i < width; i++) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000082 SkString index;
83 SkString kernelIndex;
tomhudson@google.comf8a22892012-06-11 12:42:24 +000084 index.appendS32(i);
bsalomon@google.com032b2212012-07-16 13:36:18 +000085 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000086 code->appendf("\t\t%s += ", outputColor);
bsalomon@google.com032b2212012-07-16 13:36:18 +000087 builder->emitTextureLookup(samplerName, "coord");
tomhudson@google.comf8a22892012-06-11 12:42:24 +000088 code->appendf(" * %s;\n", kernelIndex.c_str());
bsalomon@google.com032b2212012-07-16 13:36:18 +000089 code->appendf("\t\tcoord += %s;\n", imgInc);
tomhudson@google.comf8a22892012-06-11 12:42:24 +000090 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000091
bsalomon@google.com032b2212012-07-16 13:36:18 +000092 if (builder->fModulate.size()) {
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000093 code->appendf("\t\t%s = %s%s;\n", outputColor, outputColor,
bsalomon@google.com032b2212012-07-16 13:36:18 +000094 builder->fModulate.c_str());
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000095 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000096}
97
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000098void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman,
bsalomon@google.comb505a122012-05-31 18:40:36 +000099 const GrCustomStage& data,
senorblanco@chromium.orgf4770d72012-07-13 18:25:06 +0000100 const GrRenderTarget*,
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000101 int stageNum) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000102 const GrConvolutionEffect& conv =
103 static_cast<const GrConvolutionEffect&>(data);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000104 GrTexture& texture = *data.texture(0);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000105 // the code we generated was for a specific kernel radius
106 GrAssert(conv.radius() == fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000107 float imageIncrement[2] = { 0 };
bsalomon@google.comb505a122012-05-31 18:40:36 +0000108 switch (conv.direction()) {
109 case Gr1DKernelEffect::kX_Direction:
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000110 imageIncrement[0] = 1.0f / texture.width();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000111 break;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000112 case Gr1DKernelEffect::kY_Direction:
113 imageIncrement[1] = 1.0f / texture.height();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000114 break;
115 default:
116 GrCrash("Unknown filter direction.");
117 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000118 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement);
119 uman.set1fv(fKernelUni, 0, this->width(), conv.kernel());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000120}
121
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000122GrGLProgramStage::StageKey GrGLConvolutionEffect::GenKey(const GrCustomStage& s,
123 const GrGLCaps& caps) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000124 return static_cast<const GrConvolutionEffect&>(s).radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000125}
126
bsalomon@google.comb505a122012-05-31 18:40:36 +0000127///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000128
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000129GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
130 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000131 int radius,
132 const float* kernel)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000133 : Gr1DKernelEffect(texture, direction, radius) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000134 GrAssert(radius <= kMaxKernelRadius);
135 int width = this->width();
136 if (NULL != kernel) {
137 for (int i = 0; i < width; i++) {
138 fKernel[i] = kernel[i];
139 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000140 }
141}
142
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000143GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
144 Direction direction,
145 int radius,
146 float gaussianSigma)
147 : Gr1DKernelEffect(texture, direction, radius) {
148 GrAssert(radius <= kMaxKernelRadius);
149 int width = this->width();
150
151 float sum = 0.0f;
152 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
153 for (int i = 0; i < width; ++i) {
154 float x = static_cast<float>(i - this->radius());
155 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
156 // is dropped here, since we renormalize the kernel below.
157 fKernel[i] = sk_float_exp(- x * x * denom);
158 sum += fKernel[i];
159 }
160 // Normalize the kernel
161 float scale = 1.0f / sum;
162 for (int i = 0; i < width; ++i) {
163 fKernel[i] *= scale;
164 }
165}
166
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000167GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000168}
169
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000170const GrProgramStageFactory& GrConvolutionEffect::getFactory() const {
171 return GrTProgramStageFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000172}
173
bsalomon@google.comb505a122012-05-31 18:40:36 +0000174bool GrConvolutionEffect::isEqual(const GrCustomStage& sBase) const {
175 const GrConvolutionEffect& s =
176 static_cast<const GrConvolutionEffect&>(sBase);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000177 return (INHERITED::isEqual(sBase) &&
178 this->radius() == s.radius() &&
179 this->direction() == s.direction() &&
180 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000181}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000182
183///////////////////////////////////////////////////////////////////////////////
184
185GR_DEFINE_CUSTOM_STAGE_TEST(GrConvolutionEffect);
186
187GrCustomStage* GrConvolutionEffect::TestCreate(SkRandom* random,
188 GrContext* context,
189 GrTexture* textures[]) {
190 int texIdx = random->nextBool() ? GrCustomStageTestFactory::kSkiaPMTextureIdx :
191 GrCustomStageTestFactory::kAlphaTextureIdx;
192 Direction dir = random->nextBool() ? kX_Direction : kY_Direction;
193 int radius = random->nextRangeU(1, kMaxKernelRadius);
194 float kernel[kMaxKernelRadius];
195 for (int i = 0; i < kMaxKernelRadius; ++i) {
196 kernel[i] = random->nextSScalar1();
197 }
198
199 return SkNEW_ARGS(GrConvolutionEffect, (textures[texIdx], dir, radius, kernel));
200}
201