blob: 12c32f05c5a99289c784bca66b73104eeb6b73e5 [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"
bsalomon@google.comd698f772012-10-25 13:22:00 +00009#include "gl/GrGLEffect.h"
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000010#include "gl/GrGLEffectMatrix.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000011#include "gl/GrGLSL.h"
12#include "gl/GrGLTexture.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000013#include "GrTBackendEffectFactory.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000014
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000015// For brevity
16typedef GrGLUniformManager::UniformHandle UniformHandle;
17static const UniformHandle kInvalidUniformHandle = GrGLUniformManager::kInvalidUniformHandle;
bsalomon@google.com032b2212012-07-16 13:36:18 +000018
bsalomon@google.com47d7a882012-10-29 12:47:51 +000019class GrGLConvolutionEffect : public GrGLEffect {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000020public:
bsalomon@google.com6340a412013-01-22 19:55:59 +000021 GrGLConvolutionEffect(const GrBackendEffectFactory&, const GrEffectRef&);
bsalomon@google.comb505a122012-05-31 18:40:36 +000022
bsalomon@google.com47d7a882012-10-29 12:47:51 +000023 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000024 const GrEffectStage&,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000025 EffectKey,
26 const char* vertexCoords,
27 const char* outputColor,
28 const char* inputColor,
29 const TextureSamplerArray&) SK_OVERRIDE;
bsalomon@google.comb505a122012-05-31 18:40:36 +000030
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000031 virtual void setData(const GrGLUniformManager& uman, const GrEffectStage&) SK_OVERRIDE;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000032
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000033 static inline EffectKey GenKey(const GrEffectStage&, 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.comb4a55b72012-11-02 20:45:37 +000038 int fRadius;
39 UniformHandle fKernelUni;
40 UniformHandle fImageIncrementUni;
41 GrGLEffectMatrix fEffectMatrix;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000042
bsalomon@google.com47d7a882012-10-29 12:47:51 +000043 typedef GrGLEffect INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000044};
45
bsalomon@google.com396e61f2012-10-25 19:00:29 +000046GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& factory,
bsalomon@google.com6340a412013-01-22 19:55:59 +000047 const GrEffectRef& effect)
bsalomon@google.com374e7592012-10-23 17:30:45 +000048 : INHERITED(factory)
bsalomon@google.com032b2212012-07-16 13:36:18 +000049 , fKernelUni(kInvalidUniformHandle)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000050 , fImageIncrementUni(kInvalidUniformHandle) {
bsalomon@google.com6340a412013-01-22 19:55:59 +000051 const GrConvolutionEffect& c = CastEffect<GrConvolutionEffect>(effect);
bsalomon@google.comb505a122012-05-31 18:40:36 +000052 fRadius = c.radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000053}
54
bsalomon@google.com47d7a882012-10-29 12:47:51 +000055void GrGLConvolutionEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000056 const GrEffectStage&,
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000057 EffectKey key,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000058 const char* vertexCoords,
59 const char* outputColor,
60 const char* inputColor,
61 const TextureSamplerArray& samplers) {
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000062 const char* coords;
63 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000064 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000065 kVec2f_GrSLType, "ImageIncrement");
66 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
67 kFloat_GrSLType, "Kernel", this->width());
bsalomon@google.com032b2212012-07-16 13:36:18 +000068 SkString* code = &builder->fFSCode;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000069
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000070 code->appendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000071
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000072 int width = this ->width();
bsalomon@google.com032b2212012-07-16 13:36:18 +000073 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
74 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000075
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000076 code->appendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords, fRadius, imgInc);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000077
tomhudson@google.comf8a22892012-06-11 12:42:24 +000078 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000079 for (int i = 0; i < width; i++) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000080 SkString index;
81 SkString kernelIndex;
tomhudson@google.comf8a22892012-06-11 12:42:24 +000082 index.appendS32(i);
bsalomon@google.com032b2212012-07-16 13:36:18 +000083 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
tomhudson@google.comdb3a1542012-06-13 18:12:57 +000084 code->appendf("\t\t%s += ", outputColor);
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000085 builder->appendTextureLookup(&builder->fFSCode, samplers[0], "coord");
tomhudson@google.comf8a22892012-06-11 12:42:24 +000086 code->appendf(" * %s;\n", kernelIndex.c_str());
bsalomon@google.com032b2212012-07-16 13:36:18 +000087 code->appendf("\t\tcoord += %s;\n", imgInc);
tomhudson@google.comf8a22892012-06-11 12:42:24 +000088 }
bsalomon@google.com868a8e72012-08-30 19:11:34 +000089 GrGLSLMulVarBy4f(&builder->fFSCode, 2, outputColor, inputColor);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000090}
91
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000092void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
bsalomon@google.com6340a412013-01-22 19:55:59 +000093 const GrConvolutionEffect& conv = GetEffectFromStage<GrConvolutionEffect>(stage);
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000094 GrTexture& texture = *conv.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());
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000110 fEffectMatrix.setData(uman, conv.getMatrix(), stage.getCoordChangeMatrix(), conv.texture(0));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000111}
112
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000113GrGLEffect::EffectKey GrGLConvolutionEffect::GenKey(const GrEffectStage& s, const GrGLCaps&) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000114 const GrConvolutionEffect& conv = GetEffectFromStage<GrConvolutionEffect>(s);
115 EffectKey key = conv.radius();
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000116 key <<= GrGLEffectMatrix::kKeyBits;
117 EffectKey matrixKey = GrGLEffectMatrix::GenKey(conv.getMatrix(),
118 s.getCoordChangeMatrix(),
119 conv.texture(0));
120 return key | matrixKey;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000121}
122
bsalomon@google.comb505a122012-05-31 18:40:36 +0000123///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000124
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000125GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
126 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000127 int radius,
128 const float* kernel)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000129 : Gr1DKernelEffect(texture, direction, radius) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000130 GrAssert(radius <= kMaxKernelRadius);
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000131 GrAssert(NULL != kernel);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000132 int width = this->width();
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000133 for (int i = 0; i < width; i++) {
134 fKernel[i] = kernel[i];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000135 }
136}
137
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000138GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
139 Direction direction,
140 int radius,
141 float gaussianSigma)
142 : Gr1DKernelEffect(texture, direction, radius) {
143 GrAssert(radius <= kMaxKernelRadius);
144 int width = this->width();
145
146 float sum = 0.0f;
147 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
148 for (int i = 0; i < width; ++i) {
149 float x = static_cast<float>(i - this->radius());
150 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
151 // is dropped here, since we renormalize the kernel below.
152 fKernel[i] = sk_float_exp(- x * x * denom);
153 sum += fKernel[i];
154 }
155 // Normalize the kernel
156 float scale = 1.0f / sum;
157 for (int i = 0; i < width; ++i) {
158 fKernel[i] *= scale;
159 }
160}
161
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000162GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000163}
164
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000165const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const {
166 return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000167}
168
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000169bool GrConvolutionEffect::onIsEqual(const GrEffect& sBase) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000170 const GrConvolutionEffect& s = CastEffect<GrConvolutionEffect>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000171 return (this->texture(0) == s.texture(0) &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000172 this->radius() == s.radius() &&
173 this->direction() == s.direction() &&
174 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000175}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000176
177///////////////////////////////////////////////////////////////////////////////
178
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000179GR_DEFINE_EFFECT_TEST(GrConvolutionEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000180
bsalomon@google.com73a96942013-02-13 16:31:19 +0000181GrEffectRef* GrConvolutionEffect::TestCreate(SkMWCRandom* random,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000182 GrContext* context,
183 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000184 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
185 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000186 Direction dir = random->nextBool() ? kX_Direction : kY_Direction;
187 int radius = random->nextRangeU(1, kMaxKernelRadius);
188 float kernel[kMaxKernelRadius];
189 for (int i = 0; i < kMaxKernelRadius; ++i) {
190 kernel[i] = random->nextSScalar1();
191 }
192
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000193 return GrConvolutionEffect::Create(textures[texIdx], dir, radius,kernel);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000194}