2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "GrConvolutionEffect.h"
9 #include "gl/GrGLProcessor.h"
10 #include "gl/GrGLSL.h"
11 #include "gl/GrGLTexture.h"
12 #include "gl/builders/GrGLProgramBuilder.h"
15 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
17 class GrGLConvolutionEffect : public GrGLFragmentProcessor {
19 GrGLConvolutionEffect(const GrProcessor&);
21 virtual void emitCode(GrGLFPBuilder*,
22 const GrFragmentProcessor&,
23 const char* outputColor,
24 const char* inputColor,
25 const TransformedCoordsArray&,
26 const TextureSamplerArray&) override;
28 void setData(const GrGLProgramDataManager& pdman, const GrProcessor&) override;
30 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
33 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); }
34 bool useBounds() const { return fUseBounds; }
35 Gr1DKernelEffect::Direction direction() const { return fDirection; }
39 Gr1DKernelEffect::Direction fDirection;
40 UniformHandle fKernelUni;
41 UniformHandle fImageIncrementUni;
42 UniformHandle fBoundsUni;
44 typedef GrGLFragmentProcessor INHERITED;
47 GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProcessor& processor) {
48 const GrConvolutionEffect& c = processor.cast<GrConvolutionEffect>();
50 fUseBounds = c.useBounds();
51 fDirection = c.direction();
54 void GrGLConvolutionEffect::emitCode(GrGLFPBuilder* builder,
55 const GrFragmentProcessor&,
56 const char* outputColor,
57 const char* inputColor,
58 const TransformedCoordsArray& coords,
59 const TextureSamplerArray& samplers) {
60 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
61 kVec2f_GrSLType, kDefault_GrSLPrecision,
63 if (this->useBounds()) {
64 fBoundsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
65 kVec2f_GrSLType, kDefault_GrSLPrecision,
68 fKernelUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility,
69 kFloat_GrSLType, kDefault_GrSLPrecision,
70 "Kernel", this->width());
72 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
73 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0);
75 fsBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
77 int width = this->width();
78 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
79 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
81 fsBuilder->codeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(), fRadius, imgInc);
83 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
84 for (int i = 0; i < width; i++) {
88 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
89 fsBuilder->codeAppendf("\t\t%s += ", outputColor);
90 fsBuilder->appendTextureLookup(samplers[0], "coord");
91 if (this->useBounds()) {
92 const char* bounds = builder->getUniformCStr(fBoundsUni);
93 const char* component = this->direction() == Gr1DKernelEffect::kY_Direction ? "y" : "x";
94 fsBuilder->codeAppendf(" * float(coord.%s >= %s.x && coord.%s <= %s.y)",
95 component, bounds, component, bounds);
97 fsBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str());
98 fsBuilder->codeAppendf("\t\tcoord += %s;\n", imgInc);
102 GrGLSLMulVarBy4f(&modulate, outputColor, inputColor);
103 fsBuilder->codeAppend(modulate.c_str());
106 void GrGLConvolutionEffect::setData(const GrGLProgramDataManager& pdman,
107 const GrProcessor& processor) {
108 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>();
109 GrTexture& texture = *conv.texture(0);
110 // the code we generated was for a specific kernel radius
111 SkASSERT(conv.radius() == fRadius);
112 float imageIncrement[2] = { 0 };
113 float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
114 switch (conv.direction()) {
115 case Gr1DKernelEffect::kX_Direction:
116 imageIncrement[0] = 1.0f / texture.width();
118 case Gr1DKernelEffect::kY_Direction:
119 imageIncrement[1] = ySign / texture.height();
122 SkFAIL("Unknown filter direction.");
124 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
125 if (conv.useBounds()) {
126 const float* bounds = conv.bounds();
127 if (Gr1DKernelEffect::kY_Direction == conv.direction() &&
128 texture.origin() != kTopLeft_GrSurfaceOrigin) {
129 pdman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]);
131 pdman.set2f(fBoundsUni, bounds[0], bounds[1]);
134 pdman.set1fv(fKernelUni, this->width(), conv.kernel());
137 void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrGLCaps&,
138 GrProcessorKeyBuilder* b) {
139 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>();
140 uint32_t key = conv.radius();
142 if (conv.useBounds()) {
144 key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0;
149 ///////////////////////////////////////////////////////////////////////////////
151 GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
157 : Gr1DKernelEffect(texture, direction, radius), fUseBounds(useBounds) {
158 this->initClassID<GrConvolutionEffect>();
159 SkASSERT(radius <= kMaxKernelRadius);
161 int width = this->width();
162 for (int i = 0; i < width; i++) {
163 fKernel[i] = kernel[i];
165 memcpy(fBounds, bounds, sizeof(fBounds));
168 GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
174 : Gr1DKernelEffect(texture, direction, radius), fUseBounds(useBounds) {
175 this->initClassID<GrConvolutionEffect>();
176 SkASSERT(radius <= kMaxKernelRadius);
177 int width = this->width();
180 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
181 for (int i = 0; i < width; ++i) {
182 float x = static_cast<float>(i - this->radius());
183 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
184 // is dropped here, since we renormalize the kernel below.
185 fKernel[i] = sk_float_exp(- x * x * denom);
188 // Normalize the kernel
189 float scale = 1.0f / sum;
190 for (int i = 0; i < width; ++i) {
193 memcpy(fBounds, bounds, sizeof(fBounds));
196 GrConvolutionEffect::~GrConvolutionEffect() {
199 void GrConvolutionEffect::getGLProcessorKey(const GrGLCaps& caps,
200 GrProcessorKeyBuilder* b) const {
201 GrGLConvolutionEffect::GenKey(*this, caps, b);
204 GrGLFragmentProcessor* GrConvolutionEffect::createGLInstance() const {
205 return SkNEW_ARGS(GrGLConvolutionEffect, (*this));
208 bool GrConvolutionEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
209 const GrConvolutionEffect& s = sBase.cast<GrConvolutionEffect>();
210 return (this->radius() == s.radius() &&
211 this->direction() == s.direction() &&
212 this->useBounds() == s.useBounds() &&
213 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) &&
214 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
217 ///////////////////////////////////////////////////////////////////////////////
219 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvolutionEffect);
221 GrFragmentProcessor* GrConvolutionEffect::TestCreate(SkRandom* random,
223 const GrDrawTargetCaps&,
224 GrTexture* textures[]) {
225 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
226 GrProcessorUnitTest::kAlphaTextureIdx;
227 Direction dir = random->nextBool() ? kX_Direction : kY_Direction;
228 int radius = random->nextRangeU(1, kMaxKernelRadius);
229 float kernel[kMaxKernelWidth];
230 for (size_t i = 0; i < SK_ARRAY_COUNT(kernel); ++i) {
231 kernel[i] = random->nextSScalar1();
234 for (size_t i = 0; i < SK_ARRAY_COUNT(bounds); ++i) {
235 bounds[i] = random->nextF();
238 bool useBounds = random->nextBool();
239 return GrConvolutionEffect::Create(textures[texIdx],