blob: 3fa1cb9feab77453d8c48d390cd5aa3b24577f8c [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.comc7818882013-03-20 19:19:53 +000021 GrGLConvolutionEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
bsalomon@google.comb505a122012-05-31 18:40:36 +000022
bsalomon@google.com47d7a882012-10-29 12:47:51 +000023 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.comc7818882013-03-20 19:19:53 +000024 const GrDrawEffect&,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000025 EffectKey,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000026 const char* outputColor,
27 const char* inputColor,
28 const TextureSamplerArray&) SK_OVERRIDE;
bsalomon@google.comb505a122012-05-31 18:40:36 +000029
bsalomon@google.comc7818882013-03-20 19:19:53 +000030 virtual void setData(const GrGLUniformManager& uman, const GrDrawEffect&) SK_OVERRIDE;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000031
bsalomon@google.comc7818882013-03-20 19:19:53 +000032 static inline EffectKey GenKey(const GrDrawEffect&, 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); }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000036 bool useCropRect() const { return fUseCropRect; }
bsalomon@google.comb505a122012-05-31 18:40:36 +000037
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000038 int fRadius;
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000039 bool fUseCropRect;
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000040 UniformHandle fKernelUni;
41 UniformHandle fImageIncrementUni;
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000042 UniformHandle fCropRectUni;
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000043 GrGLEffectMatrix fEffectMatrix;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000044
bsalomon@google.com47d7a882012-10-29 12:47:51 +000045 typedef GrGLEffect INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000046};
47
bsalomon@google.com396e61f2012-10-25 19:00:29 +000048GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& factory,
bsalomon@google.comc7818882013-03-20 19:19:53 +000049 const GrDrawEffect& drawEffect)
bsalomon@google.com374e7592012-10-23 17:30:45 +000050 : INHERITED(factory)
bsalomon@google.com032b2212012-07-16 13:36:18 +000051 , fKernelUni(kInvalidUniformHandle)
bsalomon@google.comc7818882013-03-20 19:19:53 +000052 , fImageIncrementUni(kInvalidUniformHandle)
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000053 , fCropRectUni(kInvalidUniformHandle)
bsalomon@google.comc7818882013-03-20 19:19:53 +000054 , fEffectMatrix(drawEffect.castEffect<GrConvolutionEffect>().coordsType()) {
55 const GrConvolutionEffect& c = drawEffect.castEffect<GrConvolutionEffect>();
bsalomon@google.comb505a122012-05-31 18:40:36 +000056 fRadius = c.radius();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000057 fUseCropRect = c.useCropRect();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000058}
59
bsalomon@google.com47d7a882012-10-29 12:47:51 +000060void GrGLConvolutionEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +000061 const GrDrawEffect&,
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000062 EffectKey key,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000063 const char* outputColor,
64 const char* inputColor,
65 const TextureSamplerArray& samplers) {
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000066 const char* coords;
bsalomon@google.comc7818882013-03-20 19:19:53 +000067 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000068 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000069 kVec2f_GrSLType, "ImageIncrement");
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000070 if (this->useCropRect()) {
71 fCropRectUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
72 kVec4f_GrSLType, "CropRect");
73 }
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000074 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
75 kFloat_GrSLType, "Kernel", this->width());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000076
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000077 builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000078
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000079 int width = this->width();
bsalomon@google.com032b2212012-07-16 13:36:18 +000080 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
81 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000082
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000083 builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords, fRadius, imgInc);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000084
tomhudson@google.comf8a22892012-06-11 12:42:24 +000085 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000086 for (int i = 0; i < width; i++) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000087 SkString index;
88 SkString kernelIndex;
tomhudson@google.comf8a22892012-06-11 12:42:24 +000089 index.appendS32(i);
bsalomon@google.com032b2212012-07-16 13:36:18 +000090 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000091 builder->fsCodeAppendf("\t\t%s += ", outputColor);
92 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, samplers[0], "coord");
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000093 if (this->useCropRect()) {
94 const char* cropRect = builder->getUniformCStr(fCropRectUni);
95 builder->fsCodeAppendf(" * float(coord.x >= %s.x && coord.x <= %s.y && coord.y >= %s.z && coord.y <= %s.w)",
96 cropRect, cropRect, cropRect, cropRect);
97 }
robertphillips@google.com58c856a2013-07-24 13:18:06 +000098 builder->fsCodeAppendf(" * %s;\n", kernelIndex.c_str());
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000099 builder->fsCodeAppendf("\t\tcoord += %s;\n", imgInc);
tomhudson@google.comf8a22892012-06-11 12:42:24 +0000100 }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000101
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000102 SkString modulate;
103 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
104 builder->fsCodeAppend(modulate.c_str());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000105}
106
bsalomon@google.comc7818882013-03-20 19:19:53 +0000107void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman,
108 const GrDrawEffect& drawEffect) {
109 const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>();
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000110 GrTexture& texture = *conv.texture(0);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000111 // the code we generated was for a specific kernel radius
112 GrAssert(conv.radius() == fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000113 float imageIncrement[2] = { 0 };
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000114 float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000115 switch (conv.direction()) {
116 case Gr1DKernelEffect::kX_Direction:
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000117 imageIncrement[0] = 1.0f / texture.width();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000118 break;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000119 case Gr1DKernelEffect::kY_Direction:
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000120 imageIncrement[1] = ySign / texture.height();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000121 break;
122 default:
123 GrCrash("Unknown filter direction.");
124 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000125 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000126 if (conv.useCropRect()) {
127 float c[4];
128 memcpy(c, conv.cropRect(), sizeof(c));
129 if (texture.origin() != kTopLeft_GrSurfaceOrigin) {
130 float tmp = 1.0f - c[2];
131 c[2] = 1.0f - c[3];
132 c[3] = tmp;
133 }
134 uman.set4fv(fCropRectUni, 0, 1, c);
135 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000136 uman.set1fv(fKernelUni, 0, this->width(), conv.kernel());
bsalomon@google.comc7818882013-03-20 19:19:53 +0000137 fEffectMatrix.setData(uman, conv.getMatrix(), drawEffect, conv.texture(0));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000138}
139
bsalomon@google.comc7818882013-03-20 19:19:53 +0000140GrGLEffect::EffectKey GrGLConvolutionEffect::GenKey(const GrDrawEffect& drawEffect,
141 const GrGLCaps&) {
142 const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000143 EffectKey key = conv.radius() << 1;
144 key |= conv.useCropRect() ? 0x1 : 0x0;
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000145 key <<= GrGLEffectMatrix::kKeyBits;
146 EffectKey matrixKey = GrGLEffectMatrix::GenKey(conv.getMatrix(),
bsalomon@google.comc7818882013-03-20 19:19:53 +0000147 drawEffect,
148 conv.coordsType(),
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000149 conv.texture(0));
150 return key | matrixKey;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000151}
152
bsalomon@google.comb505a122012-05-31 18:40:36 +0000153///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000154
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000155GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
156 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000157 int radius,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000158 const float* kernel,
159 bool useCropRect,
160 float cropRect[4])
161 : Gr1DKernelEffect(texture, direction, radius), fUseCropRect(useCropRect) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000162 GrAssert(radius <= kMaxKernelRadius);
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000163 GrAssert(NULL != kernel);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000164 int width = this->width();
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000165 for (int i = 0; i < width; i++) {
166 fKernel[i] = kernel[i];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000167 }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000168 memcpy(fCropRect, cropRect, sizeof(fCropRect));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000169}
170
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000171GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
172 Direction direction,
173 int radius,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000174 float gaussianSigma,
175 bool useCropRect,
176 float cropRect[4])
177 : Gr1DKernelEffect(texture, direction, radius), fUseCropRect(useCropRect) {
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000178 GrAssert(radius <= kMaxKernelRadius);
179 int width = this->width();
180
181 float sum = 0.0f;
182 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
183 for (int i = 0; i < width; ++i) {
184 float x = static_cast<float>(i - this->radius());
185 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
186 // is dropped here, since we renormalize the kernel below.
187 fKernel[i] = sk_float_exp(- x * x * denom);
188 sum += fKernel[i];
189 }
190 // Normalize the kernel
191 float scale = 1.0f / sum;
192 for (int i = 0; i < width; ++i) {
193 fKernel[i] *= scale;
194 }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000195 memcpy(fCropRect, cropRect, sizeof(fCropRect));
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000196}
197
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000198GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000199}
200
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000201const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const {
202 return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000203}
204
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000205bool GrConvolutionEffect::onIsEqual(const GrEffect& sBase) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000206 const GrConvolutionEffect& s = CastEffect<GrConvolutionEffect>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000207 return (this->texture(0) == s.texture(0) &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000208 this->radius() == s.radius() &&
209 this->direction() == s.direction() &&
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000210 0 == memcmp(fCropRect, s.fCropRect, sizeof(fCropRect)) &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000211 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000212}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000213
214///////////////////////////////////////////////////////////////////////////////
215
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000216GR_DEFINE_EFFECT_TEST(GrConvolutionEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000217
bsalomon@google.com73a96942013-02-13 16:31:19 +0000218GrEffectRef* GrConvolutionEffect::TestCreate(SkMWCRandom* random,
sugoi@google.come0e385c2013-03-11 18:50:03 +0000219 GrContext*,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000220 const GrDrawTargetCaps&,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000221 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000222 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
223 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000224 Direction dir = random->nextBool() ? kX_Direction : kY_Direction;
225 int radius = random->nextRangeU(1, kMaxKernelRadius);
226 float kernel[kMaxKernelRadius];
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000227 float cropRect[4];
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000228 for (int i = 0; i < kMaxKernelRadius; ++i) {
229 kernel[i] = random->nextSScalar1();
230 }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000231 for (int i = 0; i < 4; ++i) {
232 cropRect[i] = random->nextF();
233 }
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000234
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000235 bool useCropRect = random->nextBool();
236 return GrConvolutionEffect::Create(textures[texIdx],
237 dir,
238 radius,
239 kernel,
240 useCropRect,
241 cropRect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000242}