blob: 5682b9c65cb5a671c2eb6ade2a4648e94655b2f2 [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); }
36
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000037 int fRadius;
38 UniformHandle fKernelUni;
39 UniformHandle fImageIncrementUni;
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +000040 UniformHandle fCropRectUni;
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000041 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.comc7818882013-03-20 19:19:53 +000047 const GrDrawEffect& drawEffect)
bsalomon@google.com374e7592012-10-23 17:30:45 +000048 : INHERITED(factory)
bsalomon@google.com032b2212012-07-16 13:36:18 +000049 , fKernelUni(kInvalidUniformHandle)
bsalomon@google.comc7818882013-03-20 19:19:53 +000050 , fImageIncrementUni(kInvalidUniformHandle)
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +000051 , fCropRectUni(kInvalidUniformHandle)
bsalomon@google.comc7818882013-03-20 19:19:53 +000052 , fEffectMatrix(drawEffect.castEffect<GrConvolutionEffect>().coordsType()) {
53 const GrConvolutionEffect& c = drawEffect.castEffect<GrConvolutionEffect>();
bsalomon@google.comb505a122012-05-31 18:40:36 +000054 fRadius = c.radius();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000055}
56
bsalomon@google.com47d7a882012-10-29 12:47:51 +000057void GrGLConvolutionEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +000058 const GrDrawEffect&,
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000059 EffectKey key,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000060 const char* outputColor,
61 const char* inputColor,
62 const TextureSamplerArray& samplers) {
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000063 const char* coords;
bsalomon@google.comc7818882013-03-20 19:19:53 +000064 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000065 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000066 kVec2f_GrSLType, "ImageIncrement");
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +000067 fCropRectUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
68 kVec4f_GrSLType, "CropRect");
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000069 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
70 kFloat_GrSLType, "Kernel", this->width());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000071
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000072 builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000073
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000074 int width = this ->width();
bsalomon@google.com032b2212012-07-16 13:36:18 +000075 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
76 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +000077 const char* cropRect = builder->getUniformCStr(fCropRectUni);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000078
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000079 builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords, fRadius, imgInc);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000080
tomhudson@google.comf8a22892012-06-11 12:42:24 +000081 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000082 for (int i = 0; i < width; i++) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000083 SkString index;
84 SkString kernelIndex;
tomhudson@google.comf8a22892012-06-11 12:42:24 +000085 index.appendS32(i);
bsalomon@google.com032b2212012-07-16 13:36:18 +000086 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000087 builder->fsCodeAppendf("\t\t%s += ", outputColor);
88 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, samplers[0], "coord");
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +000089 builder->fsCodeAppendf(" * float(coord.x >= %s.x && coord.x <= %s.y && coord.y >= %s.z && coord.y <= %s.w) * %s;\n",
90 cropRect, cropRect, cropRect, cropRect, kernelIndex.c_str());
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000091 builder->fsCodeAppendf("\t\tcoord += %s;\n", imgInc);
tomhudson@google.comf8a22892012-06-11 12:42:24 +000092 }
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +000093
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000094 SkString modulate;
95 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
96 builder->fsCodeAppend(modulate.c_str());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000097}
98
bsalomon@google.comc7818882013-03-20 19:19:53 +000099void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman,
100 const GrDrawEffect& drawEffect) {
101 const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>();
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000102 GrTexture& texture = *conv.texture(0);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000103 // the code we generated was for a specific kernel radius
104 GrAssert(conv.radius() == fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000105 float imageIncrement[2] = { 0 };
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +0000106 float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000107 switch (conv.direction()) {
108 case Gr1DKernelEffect::kX_Direction:
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000109 imageIncrement[0] = 1.0f / texture.width();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000110 break;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000111 case Gr1DKernelEffect::kY_Direction:
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +0000112 imageIncrement[1] = ySign / texture.height();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000113 break;
114 default:
115 GrCrash("Unknown filter direction.");
116 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000117 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement);
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +0000118 float c[4];
119 memcpy(c, conv.cropRect(), sizeof(c));
120 if (texture.origin() != kTopLeft_GrSurfaceOrigin) {
121 float tmp = 1.0f - c[2];
122 c[2] = 1.0f - c[3];
123 c[3] = tmp;
124 }
125 uman.set4fv(fCropRectUni, 0, 1, c);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000126 uman.set1fv(fKernelUni, 0, this->width(), conv.kernel());
bsalomon@google.comc7818882013-03-20 19:19:53 +0000127 fEffectMatrix.setData(uman, conv.getMatrix(), drawEffect, conv.texture(0));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000128}
129
bsalomon@google.comc7818882013-03-20 19:19:53 +0000130GrGLEffect::EffectKey GrGLConvolutionEffect::GenKey(const GrDrawEffect& drawEffect,
131 const GrGLCaps&) {
132 const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>();
bsalomon@google.com6340a412013-01-22 19:55:59 +0000133 EffectKey key = conv.radius();
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000134 key <<= GrGLEffectMatrix::kKeyBits;
135 EffectKey matrixKey = GrGLEffectMatrix::GenKey(conv.getMatrix(),
bsalomon@google.comc7818882013-03-20 19:19:53 +0000136 drawEffect,
137 conv.coordsType(),
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000138 conv.texture(0));
139 return key | matrixKey;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000140}
141
bsalomon@google.comb505a122012-05-31 18:40:36 +0000142///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000143
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000144GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
145 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000146 int radius,
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +0000147 const float* kernel,
148 float cropRect[4])
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000149 : Gr1DKernelEffect(texture, direction, radius) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000150 GrAssert(radius <= kMaxKernelRadius);
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000151 GrAssert(NULL != kernel);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000152 int width = this->width();
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000153 for (int i = 0; i < width; i++) {
154 fKernel[i] = kernel[i];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000155 }
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +0000156 memcpy(fCropRect, cropRect, sizeof(fCropRect));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000157}
158
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000159GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
160 Direction direction,
161 int radius,
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +0000162 float gaussianSigma,
163 float cropRect[4])
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000164 : Gr1DKernelEffect(texture, direction, radius) {
165 GrAssert(radius <= kMaxKernelRadius);
166 int width = this->width();
167
168 float sum = 0.0f;
169 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
170 for (int i = 0; i < width; ++i) {
171 float x = static_cast<float>(i - this->radius());
172 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
173 // is dropped here, since we renormalize the kernel below.
174 fKernel[i] = sk_float_exp(- x * x * denom);
175 sum += fKernel[i];
176 }
177 // Normalize the kernel
178 float scale = 1.0f / sum;
179 for (int i = 0; i < width; ++i) {
180 fKernel[i] *= scale;
181 }
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +0000182 memcpy(fCropRect, cropRect, sizeof(fCropRect));
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000183}
184
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000185GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000186}
187
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000188const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const {
189 return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000190}
191
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000192bool GrConvolutionEffect::onIsEqual(const GrEffect& sBase) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000193 const GrConvolutionEffect& s = CastEffect<GrConvolutionEffect>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000194 return (this->texture(0) == s.texture(0) &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000195 this->radius() == s.radius() &&
196 this->direction() == s.direction() &&
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +0000197 0 == memcmp(fCropRect, s.fCropRect, sizeof(fCropRect)) &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000198 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000199}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000200
201///////////////////////////////////////////////////////////////////////////////
202
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000203GR_DEFINE_EFFECT_TEST(GrConvolutionEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000204
bsalomon@google.com73a96942013-02-13 16:31:19 +0000205GrEffectRef* GrConvolutionEffect::TestCreate(SkMWCRandom* random,
sugoi@google.come0e385c2013-03-11 18:50:03 +0000206 GrContext*,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000207 const GrDrawTargetCaps&,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000208 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000209 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
210 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000211 Direction dir = random->nextBool() ? kX_Direction : kY_Direction;
212 int radius = random->nextRangeU(1, kMaxKernelRadius);
213 float kernel[kMaxKernelRadius];
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +0000214 float cropRect[4];
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000215 for (int i = 0; i < kMaxKernelRadius; ++i) {
216 kernel[i] = random->nextSScalar1();
217 }
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +0000218 for (int i = 0; i < 4; ++i) {
219 cropRect[i] = random->nextF();
220 }
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000221
senorblanco@chromium.orgccf225c2013-07-22 20:03:22 +0000222 return GrConvolutionEffect::Create(textures[texIdx], dir, radius, kernel, cropRect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000223}