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 "glsl/GrGLSLFragmentProcessor.h"
10 #include "glsl/GrGLSLFragmentShaderBuilder.h"
11 #include "glsl/GrGLSLProgramBuilder.h"
12 #include "glsl/GrGLSLProgramDataManager.h"
15 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
17 class GrGLConvolutionEffect : public GrGLSLFragmentProcessor {
19 GrGLConvolutionEffect(const GrProcessor&);
21 virtual void emitCode(EmitArgs&) override;
23 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
26 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor&) override;
29 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); }
30 bool useBounds() const { return fUseBounds; }
31 Gr1DKernelEffect::Direction direction() const { return fDirection; }
35 Gr1DKernelEffect::Direction fDirection;
36 UniformHandle fKernelUni;
37 UniformHandle fImageIncrementUni;
38 UniformHandle fBoundsUni;
40 typedef GrGLSLFragmentProcessor INHERITED;
43 GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProcessor& processor) {
44 const GrConvolutionEffect& c = processor.cast<GrConvolutionEffect>();
46 fUseBounds = c.useBounds();
47 fDirection = c.direction();
50 void GrGLConvolutionEffect::emitCode(EmitArgs& args) {
51 fImageIncrementUni = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragment_Visibility,
52 kVec2f_GrSLType, kDefault_GrSLPrecision,
54 if (this->useBounds()) {
55 fBoundsUni = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragment_Visibility,
56 kVec2f_GrSLType, kDefault_GrSLPrecision,
59 fKernelUni = args.fBuilder->addUniformArray(GrGLSLProgramBuilder::kFragment_Visibility,
60 kFloat_GrSLType, kDefault_GrSLPrecision,
61 "Kernel", this->width());
63 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
64 SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0);
66 fragBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", args.fOutputColor);
68 int width = this->width();
69 const GrGLSLShaderVar& kernel = args.fBuilder->getUniformVariable(fKernelUni);
70 const char* imgInc = args.fBuilder->getUniformCStr(fImageIncrementUni);
72 fragBuilder->codeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(), fRadius, imgInc);
74 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
75 for (int i = 0; i < width; i++) {
79 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
81 if (this->useBounds()) {
82 // We used to compute a bool indicating whether we're in bounds or not, cast it to a
83 // float, and then mul weight*texture_sample by the float. However, the Adreno 430 seems
84 // to have a bug that caused corruption.
85 const char* bounds = args.fBuilder->getUniformCStr(fBoundsUni);
86 const char* component = this->direction() == Gr1DKernelEffect::kY_Direction ? "y" : "x";
87 fragBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y) {",
88 component, bounds, component, bounds);
90 fragBuilder->codeAppendf("\t\t%s += ", args.fOutputColor);
91 fragBuilder->appendTextureLookup(args.fSamplers[0], "coord");
92 fragBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str());
93 if (this->useBounds()) {
94 fragBuilder->codeAppend("}");
96 fragBuilder->codeAppendf("\t\tcoord += %s;\n", imgInc);
100 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor);
101 fragBuilder->codeAppend(modulate.c_str());
104 void GrGLConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman,
105 const GrProcessor& processor) {
106 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>();
107 GrTexture& texture = *conv.texture(0);
108 // the code we generated was for a specific kernel radius
109 SkASSERT(conv.radius() == fRadius);
110 float imageIncrement[2] = { 0 };
111 float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
112 switch (conv.direction()) {
113 case Gr1DKernelEffect::kX_Direction:
114 imageIncrement[0] = 1.0f / texture.width();
116 case Gr1DKernelEffect::kY_Direction:
117 imageIncrement[1] = ySign / texture.height();
120 SkFAIL("Unknown filter direction.");
122 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
123 if (conv.useBounds()) {
124 const float* bounds = conv.bounds();
125 if (Gr1DKernelEffect::kY_Direction == conv.direction() &&
126 texture.origin() != kTopLeft_GrSurfaceOrigin) {
127 pdman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]);
129 pdman.set2f(fBoundsUni, bounds[0], bounds[1]);
132 pdman.set1fv(fKernelUni, this->width(), conv.kernel());
135 void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
136 GrProcessorKeyBuilder* b) {
137 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>();
138 uint32_t key = conv.radius();
140 if (conv.useBounds()) {
142 key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0;
147 ///////////////////////////////////////////////////////////////////////////////
149 GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
155 : INHERITED(texture, direction, radius), fUseBounds(useBounds) {
156 this->initClassID<GrConvolutionEffect>();
157 SkASSERT(radius <= kMaxKernelRadius);
159 int width = this->width();
160 for (int i = 0; i < width; i++) {
161 fKernel[i] = kernel[i];
163 memcpy(fBounds, bounds, sizeof(fBounds));
166 GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture,
172 : INHERITED(texture, direction, radius), fUseBounds(useBounds) {
173 this->initClassID<GrConvolutionEffect>();
174 SkASSERT(radius <= kMaxKernelRadius);
175 int width = this->width();
178 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma);
179 for (int i = 0; i < width; ++i) {
180 float x = static_cast<float>(i - this->radius());
181 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
182 // is dropped here, since we renormalize the kernel below.
183 fKernel[i] = sk_float_exp(- x * x * denom);
186 // Normalize the kernel
187 float scale = 1.0f / sum;
188 for (int i = 0; i < width; ++i) {
191 memcpy(fBounds, bounds, sizeof(fBounds));
194 GrConvolutionEffect::~GrConvolutionEffect() {
197 void GrConvolutionEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
198 GrProcessorKeyBuilder* b) const {
199 GrGLConvolutionEffect::GenKey(*this, caps, b);
202 GrGLSLFragmentProcessor* GrConvolutionEffect::onCreateGLSLInstance() const {
203 return new GrGLConvolutionEffect(*this);
206 bool GrConvolutionEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
207 const GrConvolutionEffect& s = sBase.cast<GrConvolutionEffect>();
208 return (this->radius() == s.radius() &&
209 this->direction() == s.direction() &&
210 this->useBounds() == s.useBounds() &&
211 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) &&
212 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
215 ///////////////////////////////////////////////////////////////////////////////
217 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvolutionEffect);
219 const GrFragmentProcessor* GrConvolutionEffect::TestCreate(GrProcessorTestData* d) {
220 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
221 GrProcessorUnitTest::kAlphaTextureIdx;
222 Direction dir = d->fRandom->nextBool() ? kX_Direction : kY_Direction;
223 int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius);
224 float kernel[kMaxKernelWidth];
225 for (size_t i = 0; i < SK_ARRAY_COUNT(kernel); ++i) {
226 kernel[i] = d->fRandom->nextSScalar1();
229 for (size_t i = 0; i < SK_ARRAY_COUNT(bounds); ++i) {
230 bounds[i] = d->fRandom->nextF();
233 bool useBounds = d->fRandom->nextBool();
234 return GrConvolutionEffect::Create(d->fTextures[texIdx],