blob: e7fb8e5403d9c6bc60c6f2a923d252c83e757b6d [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.orge8232bc2013-07-29 18:45:44 +000036 bool useBounds() const { return fUseBounds; }
37 Gr1DKernelEffect::Direction direction() const { return fDirection; }
bsalomon@google.comb505a122012-05-31 18:40:36 +000038
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000039 int fRadius;
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000040 bool fUseBounds;
41 Gr1DKernelEffect::Direction fDirection;
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000042 UniformHandle fKernelUni;
43 UniformHandle fImageIncrementUni;
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000044 UniformHandle fBoundsUni;
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000045 GrGLEffectMatrix fEffectMatrix;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000046
bsalomon@google.com47d7a882012-10-29 12:47:51 +000047 typedef GrGLEffect INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000048};
49
bsalomon@google.com396e61f2012-10-25 19:00:29 +000050GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& factory,
bsalomon@google.comc7818882013-03-20 19:19:53 +000051 const GrDrawEffect& drawEffect)
bsalomon@google.com374e7592012-10-23 17:30:45 +000052 : INHERITED(factory)
bsalomon@google.com032b2212012-07-16 13:36:18 +000053 , fKernelUni(kInvalidUniformHandle)
bsalomon@google.comc7818882013-03-20 19:19:53 +000054 , fImageIncrementUni(kInvalidUniformHandle)
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000055 , fBoundsUni(kInvalidUniformHandle)
bsalomon@google.comc7818882013-03-20 19:19:53 +000056 , fEffectMatrix(drawEffect.castEffect<GrConvolutionEffect>().coordsType()) {
57 const GrConvolutionEffect& c = drawEffect.castEffect<GrConvolutionEffect>();
bsalomon@google.comb505a122012-05-31 18:40:36 +000058 fRadius = c.radius();
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000059 fUseBounds = c.useBounds();
60 fDirection = c.direction();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000061}
62
bsalomon@google.com47d7a882012-10-29 12:47:51 +000063void GrGLConvolutionEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +000064 const GrDrawEffect&,
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000065 EffectKey key,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000066 const char* outputColor,
67 const char* inputColor,
68 const TextureSamplerArray& samplers) {
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000069 const char* coords;
bsalomon@google.comc7818882013-03-20 19:19:53 +000070 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000071 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000072 kVec2f_GrSLType, "ImageIncrement");
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000073 if (this->useBounds()) {
74 fBoundsUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
75 kVec2f_GrSLType, "Bounds");
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000076 }
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000077 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
78 kFloat_GrSLType, "Kernel", this->width());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000079
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000080 builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000081
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000082 int width = this->width();
bsalomon@google.com032b2212012-07-16 13:36:18 +000083 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
84 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000085
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000086 builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords, fRadius, imgInc);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000087
tomhudson@google.comf8a22892012-06-11 12:42:24 +000088 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
bsalomon@google.comfc0d23a2012-06-15 21:50:15 +000089 for (int i = 0; i < width; i++) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000090 SkString index;
91 SkString kernelIndex;
tomhudson@google.comf8a22892012-06-11 12:42:24 +000092 index.appendS32(i);
bsalomon@google.com032b2212012-07-16 13:36:18 +000093 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000094 builder->fsCodeAppendf("\t\t%s += ", outputColor);
95 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, samplers[0], "coord");
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000096 if (this->useBounds()) {
97 const char* bounds = builder->getUniformCStr(fBoundsUni);
98 const char* component = this->direction() == Gr1DKernelEffect::kY_Direction ? "y" : "x";
99 builder->fsCodeAppendf(" * float(coord.%s >= %s.x && coord.%s <= %s.y)",
100 component, bounds, component, bounds);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000101 }
robertphillips@google.com58c856a2013-07-24 13:18:06 +0000102 builder->fsCodeAppendf(" * %s;\n", kernelIndex.c_str());
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000103 builder->fsCodeAppendf("\t\tcoord += %s;\n", imgInc);
tomhudson@google.comf8a22892012-06-11 12:42:24 +0000104 }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000105
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000106 SkString modulate;
107 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
108 builder->fsCodeAppend(modulate.c_str());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000109}
110
bsalomon@google.comc7818882013-03-20 19:19:53 +0000111void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman,
112 const GrDrawEffect& drawEffect) {
113 const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>();
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000114 GrTexture& texture = *conv.texture(0);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000115 // the code we generated was for a specific kernel radius
116 GrAssert(conv.radius() == fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000117 float imageIncrement[2] = { 0 };
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000118 float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000119 switch (conv.direction()) {
120 case Gr1DKernelEffect::kX_Direction:
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000121 imageIncrement[0] = 1.0f / texture.width();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000122 break;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000123 case Gr1DKernelEffect::kY_Direction:
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000124 imageIncrement[1] = ySign / texture.height();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000125 break;
126 default:
127 GrCrash("Unknown filter direction.");
128 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000129 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement);
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000130 if (conv.useBounds()) {
131 const float* bounds = conv.bounds();
132 if (Gr1DKernelEffect::kY_Direction == conv.direction() &&
133 texture.origin() != kTopLeft_GrSurfaceOrigin) {
134 uman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]);
135 } else {
136 uman.set2f(fBoundsUni, bounds[0], bounds[1]);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000137 }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000138 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000139 uman.set1fv(fKernelUni, 0, this->width(), conv.kernel());
bsalomon@google.comc7818882013-03-20 19:19:53 +0000140 fEffectMatrix.setData(uman, conv.getMatrix(), drawEffect, conv.texture(0));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000141}
142
bsalomon@google.comc7818882013-03-20 19:19:53 +0000143GrGLEffect::EffectKey GrGLConvolutionEffect::GenKey(const GrDrawEffect& drawEffect,
144 const GrGLCaps&) {
145 const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>();
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000146 EffectKey key = conv.radius();
147 key <<= 2;
148 if (conv.useBounds()) {
149 key |= 0x2;
150 key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0;
151 }
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000152 key <<= GrGLEffectMatrix::kKeyBits;
153 EffectKey matrixKey = GrGLEffectMatrix::GenKey(conv.getMatrix(),
bsalomon@google.comc7818882013-03-20 19:19:53 +0000154 drawEffect,
155 conv.coordsType(),
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000156 conv.texture(0));
157 return key | matrixKey;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000158}
159
bsalomon@google.comb505a122012-05-31 18:40:36 +0000160///////////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000161
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000162GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
163 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000164 int radius,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000165 const float* kernel,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000166 bool useBounds,
167 float bounds[2])
168 : Gr1DKernelEffect(texture, direction, radius), fUseBounds(useBounds) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000169 GrAssert(radius <= kMaxKernelRadius);
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000170 GrAssert(NULL != kernel);
bsalomon@google.comb505a122012-05-31 18:40:36 +0000171 int width = this->width();
bsalomon@google.comb4a55b72012-11-02 20:45:37 +0000172 for (int i = 0; i < width; i++) {
173 fKernel[i] = kernel[i];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000174 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000175 memcpy(fBounds, bounds, sizeof(fBounds));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000176}
177
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000178GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
179 Direction direction,
180 int radius,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000181 float gaussianSigma,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000182 bool useBounds,
183 float bounds[2])
184 : Gr1DKernelEffect(texture, direction, radius), fUseBounds(useBounds) {
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000185 GrAssert(radius <= kMaxKernelRadius);
186 int width = this->width();
187
188 float sum = 0.0f;
189 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
190 for (int i = 0; i < width; ++i) {
191 float x = static_cast<float>(i - this->radius());
192 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
193 // is dropped here, since we renormalize the kernel below.
194 fKernel[i] = sk_float_exp(- x * x * denom);
195 sum += fKernel[i];
196 }
197 // Normalize the kernel
198 float scale = 1.0f / sum;
199 for (int i = 0; i < width; ++i) {
200 fKernel[i] *= scale;
201 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000202 memcpy(fBounds, bounds, sizeof(fBounds));
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000203}
204
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000205GrConvolutionEffect::~GrConvolutionEffect() {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000206}
207
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000208const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const {
209 return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000210}
211
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000212bool GrConvolutionEffect::onIsEqual(const GrEffect& sBase) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000213 const GrConvolutionEffect& s = CastEffect<GrConvolutionEffect>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000214 return (this->texture(0) == s.texture(0) &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000215 this->radius() == s.radius() &&
216 this->direction() == s.direction() &&
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000217 this->useBounds() == s.useBounds() &&
218 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) &&
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000219 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000220}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000221
222///////////////////////////////////////////////////////////////////////////////
223
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000224GR_DEFINE_EFFECT_TEST(GrConvolutionEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000225
bsalomon@google.com73a96942013-02-13 16:31:19 +0000226GrEffectRef* GrConvolutionEffect::TestCreate(SkMWCRandom* random,
sugoi@google.come0e385c2013-03-11 18:50:03 +0000227 GrContext*,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000228 const GrDrawTargetCaps&,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000229 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000230 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
231 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000232 Direction dir = random->nextBool() ? kX_Direction : kY_Direction;
233 int radius = random->nextRangeU(1, kMaxKernelRadius);
234 float kernel[kMaxKernelRadius];
235 for (int i = 0; i < kMaxKernelRadius; ++i) {
236 kernel[i] = random->nextSScalar1();
237 }
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000238 float bounds[2];
239 for (int i = 0; i < 2; ++i) {
240 bounds[i] = random->nextF();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000241 }
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000242
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000243 bool useBounds = random->nextBool();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000244 return GrConvolutionEffect::Create(textures[texIdx],
245 dir,
246 radius,
247 kernel,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000248 useBounds,
249 bounds);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000250}