OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 #include "GrMatrixConvolutionEffect.h" | 7 #include "GrMatrixConvolutionEffect.h" |
8 #include "gl/GrGLFragmentProcessor.h" | 8 #include "gl/GrGLFragmentProcessor.h" |
9 #include "gl/GrGLTexture.h" | 9 #include "gl/GrGLTexture.h" |
10 #include "gl/builders/GrGLProgramBuilder.h" | 10 #include "gl/builders/GrGLProgramBuilder.h" |
11 | 11 |
12 class GrGLMatrixConvolutionEffect : public GrGLFragmentProcessor { | 12 class GrGLMatrixConvolutionEffect : public GrGLFragmentProcessor { |
13 public: | 13 public: |
14 GrGLMatrixConvolutionEffect(const GrProcessor&); | 14 GrGLMatrixConvolutionEffect(const GrProcessor&); |
15 virtual void emitCode(GrGLFPBuilder*, | 15 virtual void emitCode(EmitArgs&) override; |
16 const GrFragmentProcessor&, | |
17 const char* outputColor, | |
18 const char* inputColor, | |
19 const TransformedCoordsArray&, | |
20 const TextureSamplerArray&) override; | |
21 | 16 |
22 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessor
KeyBuilder*); | 17 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessor
KeyBuilder*); |
23 | 18 |
24 void setData(const GrGLProgramDataManager&, const GrProcessor&) override; | 19 void setData(const GrGLProgramDataManager&, const GrProcessor&) override; |
25 | 20 |
26 private: | 21 private: |
27 typedef GrGLProgramDataManager::UniformHandle UniformHandle; | 22 typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
28 SkISize fKernelSize; | 23 SkISize fKernelSize; |
29 bool fConvolveAlpha; | 24 bool fConvolveAlpha; |
30 | 25 |
31 UniformHandle fKernelUni; | 26 UniformHandle fKernelUni; |
32 UniformHandle fImageIncrementUni; | 27 UniformHandle fImageIncrementUni; |
33 UniformHandle fKernelOffsetUni; | 28 UniformHandle fKernelOffsetUni; |
34 UniformHandle fGainUni; | 29 UniformHandle fGainUni; |
35 UniformHandle fBiasUni; | 30 UniformHandle fBiasUni; |
36 GrTextureDomain::GLDomain fDomain; | 31 GrTextureDomain::GLDomain fDomain; |
37 | 32 |
38 typedef GrGLFragmentProcessor INHERITED; | 33 typedef GrGLFragmentProcessor INHERITED; |
39 }; | 34 }; |
40 | 35 |
41 GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrProcessor& proc
essor) { | 36 GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrProcessor& proc
essor) { |
42 const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffec
t>(); | 37 const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffec
t>(); |
43 fKernelSize = m.kernelSize(); | 38 fKernelSize = m.kernelSize(); |
44 fConvolveAlpha = m.convolveAlpha(); | 39 fConvolveAlpha = m.convolveAlpha(); |
45 } | 40 } |
46 | 41 |
47 void GrGLMatrixConvolutionEffect::emitCode(GrGLFPBuilder* builder, | 42 void GrGLMatrixConvolutionEffect::emitCode(EmitArgs& args) { |
48 const GrFragmentProcessor& fp, | 43 const GrTextureDomain& domain = args.fFp.cast<GrMatrixConvolutionEffect>().d
omain(); |
49 const char* outputColor, | 44 fImageIncrementUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment
_Visibility, |
50 const char* inputColor, | |
51 const TransformedCoordsArray& coords, | |
52 const TextureSamplerArray& samplers)
{ | |
53 const GrTextureDomain& domain = fp.cast<GrMatrixConvolutionEffect>().domain(
); | |
54 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, | |
55 kVec2f_GrSLType, kDefault_GrSLPreci
sion, | 45 kVec2f_GrSLType, kDefault_GrSLPreci
sion, |
56 "ImageIncrement"); | 46 "ImageIncrement"); |
57 fKernelUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibili
ty, | 47 fKernelUni = args.fBuilder->addUniformArray(GrGLProgramBuilder::kFragment_Vi
sibility, |
58 kFloat_GrSLType, kDefault_GrSLPrecisio
n, | 48 kFloat_GrSLType, kDefault_GrSLPrecisio
n, |
59 "Kernel", | 49 "Kernel", |
60 fKernelSize.width() * fKernelSize.heig
ht()); | 50 fKernelSize.width() * fKernelSize.heig
ht()); |
61 fKernelOffsetUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibil
ity, | 51 fKernelOffsetUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_V
isibility, |
62 kVec2f_GrSLType, kDefault_GrSLPrecisi
on, "KernelOffset"); | 52 kVec2f_GrSLType, kDefault_GrSLPrecisi
on, "KernelOffset"); |
63 fGainUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, | 53 fGainUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibilit
y, |
64 kFloat_GrSLType, kDefault_GrSLPrecision, "Gai
n"); | 54 kFloat_GrSLType, kDefault_GrSLPrecision, "Gai
n"); |
65 fBiasUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, | 55 fBiasUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibilit
y, |
66 kFloat_GrSLType, kDefault_GrSLPrecision, "Bia
s"); | 56 kFloat_GrSLType, kDefault_GrSLPrecision, "Bia
s"); |
67 | 57 |
68 const char* kernelOffset = builder->getUniformCStr(fKernelOffsetUni); | 58 const char* kernelOffset = args.fBuilder->getUniformCStr(fKernelOffsetUni); |
69 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); | 59 const char* imgInc = args.fBuilder->getUniformCStr(fImageIncrementUni); |
70 const char* kernel = builder->getUniformCStr(fKernelUni); | 60 const char* kernel = args.fBuilder->getUniformCStr(fKernelUni); |
71 const char* gain = builder->getUniformCStr(fGainUni); | 61 const char* gain = args.fBuilder->getUniformCStr(fGainUni); |
72 const char* bias = builder->getUniformCStr(fBiasUni); | 62 const char* bias = args.fBuilder->getUniformCStr(fBiasUni); |
73 int kWidth = fKernelSize.width(); | 63 int kWidth = fKernelSize.width(); |
74 int kHeight = fKernelSize.height(); | 64 int kHeight = fKernelSize.height(); |
75 | 65 |
76 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | 66 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); |
77 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); | 67 SkString coords2D = fsBuilder->ensureFSCoords2D(args.fCoords, 0); |
78 fsBuilder->codeAppend("vec4 sum = vec4(0, 0, 0, 0);"); | 68 fsBuilder->codeAppend("vec4 sum = vec4(0, 0, 0, 0);"); |
79 fsBuilder->codeAppendf("vec2 coord = %s - %s * %s;", coords2D.c_str(), kerne
lOffset, | 69 fsBuilder->codeAppendf("vec2 coord = %s - %s * %s;", coords2D.c_str(), kerne
lOffset, |
80 imgInc); | 70 imgInc); |
81 fsBuilder->codeAppend("vec4 c;"); | 71 fsBuilder->codeAppend("vec4 c;"); |
82 | 72 |
83 for (int y = 0; y < kHeight; y++) { | 73 for (int y = 0; y < kHeight; y++) { |
84 for (int x = 0; x < kWidth; x++) { | 74 for (int x = 0; x < kWidth; x++) { |
85 GrGLShaderBuilder::ShaderBlock block(fsBuilder); | 75 GrGLShaderBuilder::ShaderBlock block(fsBuilder); |
86 fsBuilder->codeAppendf("float k = %s[%d * %d + %d];", kernel, y, kWi
dth, x); | 76 fsBuilder->codeAppendf("float k = %s[%d * %d + %d];", kernel, y, kWi
dth, x); |
87 SkString coord; | 77 SkString coord; |
88 coord.printf("coord + vec2(%d, %d) * %s", x, y, imgInc); | 78 coord.printf("coord + vec2(%d, %d) * %s", x, y, imgInc); |
89 fDomain.sampleTexture(fsBuilder, domain, "c", coord, samplers[0]); | 79 fDomain.sampleTexture(fsBuilder, domain, "c", coord, args.fSamplers[
0]); |
90 if (!fConvolveAlpha) { | 80 if (!fConvolveAlpha) { |
91 fsBuilder->codeAppend("c.rgb /= c.a;"); | 81 fsBuilder->codeAppend("c.rgb /= c.a;"); |
92 fsBuilder->codeAppend("c.rgb = clamp(c.rgb, 0.0, 1.0);"); | 82 fsBuilder->codeAppend("c.rgb = clamp(c.rgb, 0.0, 1.0);"); |
93 } | 83 } |
94 fsBuilder->codeAppend("sum += c * k;"); | 84 fsBuilder->codeAppend("sum += c * k;"); |
95 } | 85 } |
96 } | 86 } |
97 if (fConvolveAlpha) { | 87 if (fConvolveAlpha) { |
98 fsBuilder->codeAppendf("%s = sum * %s + %s;", outputColor, gain, bias); | 88 fsBuilder->codeAppendf("%s = sum * %s + %s;", args.fOutputColor, gain, b
ias); |
99 fsBuilder->codeAppendf("%s.rgb = clamp(%s.rgb, 0.0, %s.a);", | 89 fsBuilder->codeAppendf("%s.rgb = clamp(%s.rgb, 0.0, %s.a);", |
100 outputColor, outputColor, outputColor); | 90 args.fOutputColor, args.fOutputColor, args.fOutpu
tColor); |
101 } else { | 91 } else { |
102 fDomain.sampleTexture(fsBuilder, domain, "c", coords2D, samplers[0]); | 92 fDomain.sampleTexture(fsBuilder, domain, "c", coords2D, args.fSamplers[0
]); |
103 fsBuilder->codeAppendf("%s.a = c.a;", outputColor); | 93 fsBuilder->codeAppendf("%s.a = c.a;", args.fOutputColor); |
104 fsBuilder->codeAppendf("%s.rgb = sum.rgb * %s + %s;", outputColor, gain,
bias); | 94 fsBuilder->codeAppendf("%s.rgb = sum.rgb * %s + %s;", args.fOutputColor,
gain, bias); |
105 fsBuilder->codeAppendf("%s.rgb *= %s.a;", outputColor, outputColor); | 95 fsBuilder->codeAppendf("%s.rgb *= %s.a;", args.fOutputColor, args.fOutpu
tColor); |
106 } | 96 } |
107 | 97 |
108 SkString modulate; | 98 SkString modulate; |
109 GrGLSLMulVarBy4f(&modulate, outputColor, inputColor); | 99 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor); |
110 fsBuilder->codeAppend(modulate.c_str()); | 100 fsBuilder->codeAppend(modulate.c_str()); |
111 } | 101 } |
112 | 102 |
113 void GrGLMatrixConvolutionEffect::GenKey(const GrProcessor& processor, | 103 void GrGLMatrixConvolutionEffect::GenKey(const GrProcessor& processor, |
114 const GrGLSLCaps&, GrProcessorKeyBuilde
r* b) { | 104 const GrGLSLCaps&, GrProcessorKeyBuilde
r* b) { |
115 const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffec
t>(); | 105 const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffec
t>(); |
116 SkASSERT(m.kernelSize().width() <= 0x7FFF && m.kernelSize().height() <= 0xFF
FF); | 106 SkASSERT(m.kernelSize().width() <= 0x7FFF && m.kernelSize().height() <= 0xFF
FF); |
117 uint32_t key = m.kernelSize().width() << 16 | m.kernelSize().height(); | 107 uint32_t key = m.kernelSize().width() << 16 | m.kernelSize().height(); |
118 key |= m.convolveAlpha() ? 1 << 31 : 0; | 108 key |= m.convolveAlpha() ? 1 << 31 : 0; |
119 b->add32(key); | 109 b->add32(key); |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 d->fTextures[texIdx], | 254 d->fTextures[texIdx], |
265 bounds, | 255 bounds, |
266 kernelSize, | 256 kernelSize, |
267 kernel.get(), | 257 kernel.get(), |
268 gain, | 258 gain, |
269 bias, | 259 bias, |
270 kernelOffset, | 260 kernelOffset, |
271 tileMode, | 261 tileMode, |
272 convolveAlpha); | 262 convolveAlpha); |
273 } | 263 } |
OLD | NEW |