2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
7 #include "GrMatrixConvolutionEffect.h"
8 #include "gl/GrGLProcessor.h"
10 #include "gl/GrGLTexture.h"
11 #include "gl/builders/GrGLProgramBuilder.h"
13 class GrGLMatrixConvolutionEffect : public GrGLFragmentProcessor {
15 GrGLMatrixConvolutionEffect(const GrProcessor&);
16 virtual void emitCode(GrGLFPBuilder*,
17 const GrFragmentProcessor&,
18 const char* outputColor,
19 const char* inputColor,
20 const TransformedCoordsArray&,
21 const TextureSamplerArray&) override;
23 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
25 void setData(const GrGLProgramDataManager&, const GrProcessor&) override;
28 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
32 UniformHandle fKernelUni;
33 UniformHandle fImageIncrementUni;
34 UniformHandle fKernelOffsetUni;
35 UniformHandle fGainUni;
36 UniformHandle fBiasUni;
37 GrTextureDomain::GLDomain fDomain;
39 typedef GrGLFragmentProcessor INHERITED;
42 GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrProcessor& processor) {
43 const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffect>();
44 fKernelSize = m.kernelSize();
45 fConvolveAlpha = m.convolveAlpha();
48 void GrGLMatrixConvolutionEffect::emitCode(GrGLFPBuilder* builder,
49 const GrFragmentProcessor& fp,
50 const char* outputColor,
51 const char* inputColor,
52 const TransformedCoordsArray& coords,
53 const TextureSamplerArray& samplers) {
54 const GrTextureDomain& domain = fp.cast<GrMatrixConvolutionEffect>().domain();
55 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
56 kVec2f_GrSLType, kDefault_GrSLPrecision,
58 fKernelUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility,
59 kFloat_GrSLType, kDefault_GrSLPrecision,
61 fKernelSize.width() * fKernelSize.height());
62 fKernelOffsetUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
63 kVec2f_GrSLType, kDefault_GrSLPrecision, "KernelOffset");
64 fGainUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
65 kFloat_GrSLType, kDefault_GrSLPrecision, "Gain");
66 fBiasUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
67 kFloat_GrSLType, kDefault_GrSLPrecision, "Bias");
69 const char* kernelOffset = builder->getUniformCStr(fKernelOffsetUni);
70 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
71 const char* kernel = builder->getUniformCStr(fKernelUni);
72 const char* gain = builder->getUniformCStr(fGainUni);
73 const char* bias = builder->getUniformCStr(fBiasUni);
74 int kWidth = fKernelSize.width();
75 int kHeight = fKernelSize.height();
77 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
78 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0);
79 fsBuilder->codeAppend("vec4 sum = vec4(0, 0, 0, 0);");
80 fsBuilder->codeAppendf("vec2 coord = %s - %s * %s;", coords2D.c_str(), kernelOffset,
82 fsBuilder->codeAppend("vec4 c;");
84 for (int y = 0; y < kHeight; y++) {
85 for (int x = 0; x < kWidth; x++) {
86 GrGLShaderBuilder::ShaderBlock block(fsBuilder);
87 fsBuilder->codeAppendf("float k = %s[%d * %d + %d];", kernel, y, kWidth, x);
89 coord.printf("coord + vec2(%d, %d) * %s", x, y, imgInc);
90 fDomain.sampleTexture(fsBuilder, domain, "c", coord, samplers[0]);
91 if (!fConvolveAlpha) {
92 fsBuilder->codeAppend("c.rgb /= c.a;");
94 fsBuilder->codeAppend("sum += c * k;");
98 fsBuilder->codeAppendf("%s = sum * %s + %s;", outputColor, gain, bias);
99 fsBuilder->codeAppendf("%s.rgb = clamp(%s.rgb, 0.0, %s.a);",
100 outputColor, outputColor, outputColor);
102 fDomain.sampleTexture(fsBuilder, domain, "c", coords2D, samplers[0]);
103 fsBuilder->codeAppendf("%s.a = c.a;", outputColor);
104 fsBuilder->codeAppendf("%s.rgb = sum.rgb * %s + %s;", outputColor, gain, bias);
105 fsBuilder->codeAppendf("%s.rgb *= %s.a;", outputColor, outputColor);
109 GrGLSLMulVarBy4f(&modulate, outputColor, inputColor);
110 fsBuilder->codeAppend(modulate.c_str());
113 void GrGLMatrixConvolutionEffect::GenKey(const GrProcessor& processor,
114 const GrGLCaps&, GrProcessorKeyBuilder* b) {
115 const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffect>();
116 SkASSERT(m.kernelSize().width() <= 0x7FFF && m.kernelSize().height() <= 0xFFFF);
117 uint32_t key = m.kernelSize().width() << 16 | m.kernelSize().height();
118 key |= m.convolveAlpha() ? 1 << 31 : 0;
120 b->add32(GrTextureDomain::GLDomain::DomainKey(m.domain()));
123 void GrGLMatrixConvolutionEffect::setData(const GrGLProgramDataManager& pdman,
124 const GrProcessor& processor) {
125 const GrMatrixConvolutionEffect& conv = processor.cast<GrMatrixConvolutionEffect>();
126 GrTexture& texture = *conv.texture(0);
127 // the code we generated was for a specific kernel size
128 SkASSERT(conv.kernelSize() == fKernelSize);
129 float imageIncrement[2];
130 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
131 imageIncrement[0] = 1.0f / texture.width();
132 imageIncrement[1] = ySign / texture.height();
133 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
134 pdman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset());
135 pdman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.kernel());
136 pdman.set1f(fGainUni, conv.gain());
137 pdman.set1f(fBiasUni, conv.bias());
138 fDomain.setData(pdman, conv.domain(), texture.origin());
141 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture,
142 const SkIRect& bounds,
143 const SkISize& kernelSize,
144 const SkScalar* kernel,
147 const SkIPoint& kernelOffset,
148 GrTextureDomain::Mode tileMode,
150 : INHERITED(texture, GrCoordTransform::MakeDivByTextureWHMatrix(texture)),
151 fKernelSize(kernelSize),
152 fGain(SkScalarToFloat(gain)),
153 fBias(SkScalarToFloat(bias) / 255.0f),
154 fConvolveAlpha(convolveAlpha),
155 fDomain(GrTextureDomain::MakeTexelDomain(texture, bounds), tileMode) {
156 this->initClassID<GrMatrixConvolutionEffect>();
157 for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) {
158 fKernel[i] = SkScalarToFloat(kernel[i]);
160 fKernelOffset[0] = static_cast<float>(kernelOffset.x());
161 fKernelOffset[1] = static_cast<float>(kernelOffset.y());
164 GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() {
167 void GrMatrixConvolutionEffect::getGLProcessorKey(const GrGLCaps& caps,
168 GrProcessorKeyBuilder* b) const {
169 GrGLMatrixConvolutionEffect::GenKey(*this, caps, b);
172 GrGLFragmentProcessor* GrMatrixConvolutionEffect::createGLInstance() const {
173 return SkNEW_ARGS(GrGLMatrixConvolutionEffect, (*this));
176 bool GrMatrixConvolutionEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
177 const GrMatrixConvolutionEffect& s = sBase.cast<GrMatrixConvolutionEffect>();
178 return fKernelSize == s.kernelSize() &&
179 !memcmp(fKernel, s.kernel(),
180 fKernelSize.width() * fKernelSize.height() * sizeof(float)) &&
183 fKernelOffset == s.kernelOffset() &&
184 fConvolveAlpha == s.convolveAlpha() &&
185 fDomain == s.domain();
188 // Static function to create a 2D convolution
190 GrMatrixConvolutionEffect::CreateGaussian(GrTexture* texture,
191 const SkIRect& bounds,
192 const SkISize& kernelSize,
195 const SkIPoint& kernelOffset,
196 GrTextureDomain::Mode tileMode,
200 float kernel[MAX_KERNEL_SIZE];
201 int width = kernelSize.width();
202 int height = kernelSize.height();
203 SkASSERT(width * height <= MAX_KERNEL_SIZE);
205 float sigmaXDenom = 1.0f / (2.0f * SkScalarToFloat(SkScalarSquare(sigmaX)));
206 float sigmaYDenom = 1.0f / (2.0f * SkScalarToFloat(SkScalarSquare(sigmaY)));
207 int xRadius = width / 2;
208 int yRadius = height / 2;
209 for (int x = 0; x < width; x++) {
210 float xTerm = static_cast<float>(x - xRadius);
211 xTerm = xTerm * xTerm * sigmaXDenom;
212 for (int y = 0; y < height; y++) {
213 float yTerm = static_cast<float>(y - yRadius);
214 float xyTerm = sk_float_exp(-(xTerm + yTerm * yTerm * sigmaYDenom));
215 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
216 // is dropped here, since we renormalize the kernel below.
217 kernel[y * width + x] = xyTerm;
221 // Normalize the kernel
222 float scale = 1.0f / sum;
223 for (int i = 0; i < width * height; ++i) {
226 return SkNEW_ARGS(GrMatrixConvolutionEffect, (texture,
237 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMatrixConvolutionEffect);
239 GrFragmentProcessor* GrMatrixConvolutionEffect::TestCreate(SkRandom* random,
241 const GrDrawTargetCaps&,
242 GrTexture* textures[]) {
243 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
244 GrProcessorUnitTest::kAlphaTextureIdx;
245 int width = random->nextRangeU(1, MAX_KERNEL_SIZE);
246 int height = random->nextRangeU(1, MAX_KERNEL_SIZE / width);
247 SkISize kernelSize = SkISize::Make(width, height);
248 SkAutoTDeleteArray<SkScalar> kernel(new SkScalar[width * height]);
249 for (int i = 0; i < width * height; i++) {
250 kernel.get()[i] = random->nextSScalar1();
252 SkScalar gain = random->nextSScalar1();
253 SkScalar bias = random->nextSScalar1();
254 SkIPoint kernelOffset = SkIPoint::Make(random->nextRangeU(0, kernelSize.width()),
255 random->nextRangeU(0, kernelSize.height()));
256 SkIRect bounds = SkIRect::MakeXYWH(random->nextRangeU(0, textures[texIdx]->width()),
257 random->nextRangeU(0, textures[texIdx]->height()),
258 random->nextRangeU(0, textures[texIdx]->width()),
259 random->nextRangeU(0, textures[texIdx]->height()));
260 GrTextureDomain::Mode tileMode = static_cast<GrTextureDomain::Mode>(random->nextRangeU(0, 2));
261 bool convolveAlpha = random->nextBool();
262 return GrMatrixConvolutionEffect::Create(textures[texIdx],